50
|
1 |
// Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
|
|
2 |
// All rights reserved.
|
|
3 |
// This component and the accompanying materials are made available
|
|
4 |
// under the terms of "Eclipse Public License v1.0"
|
|
5 |
// which accompanies this distribution, and is available
|
|
6 |
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
7 |
//
|
|
8 |
// Initial Contributors:
|
|
9 |
// Nokia Corporation - initial contribution.
|
|
10 |
//
|
|
11 |
// Contributors:
|
|
12 |
//
|
|
13 |
// Description:
|
|
14 |
//
|
|
15 |
|
|
16 |
#undef G_DISABLE_ASSERT
|
|
17 |
#undef G_LOG_DOMAIN
|
|
18 |
|
72
|
19 |
#include <sys/stat.h>
|
|
20 |
#include <stdlib.h>
|
50
|
21 |
#include <glib.h>
|
|
22 |
#include <errno.h>
|
72
|
23 |
#include <glib/gprintf.h>
|
|
24 |
#ifdef __SYMBIAN32__
|
|
25 |
#include "mrt2_glib2_test.h"
|
|
26 |
#endif /*__SYMBIAN32__*/
|
50
|
27 |
|
|
28 |
static void
|
|
29 |
gstring_overwrite_int (GString *gstring,
|
|
30 |
guint pos,
|
|
31 |
guint32 vuint)
|
|
32 |
{
|
|
33 |
vuint = g_htonl (vuint);
|
|
34 |
g_string_overwrite_len (gstring, pos, (const gchar*) &vuint, 4);
|
|
35 |
}
|
|
36 |
|
|
37 |
static void
|
|
38 |
gstring_append_int (GString *gstring,
|
|
39 |
guint32 vuint)
|
|
40 |
{
|
|
41 |
vuint = g_htonl (vuint);
|
|
42 |
g_string_append_len (gstring, (const gchar*) &vuint, 4);
|
|
43 |
}
|
|
44 |
|
|
45 |
static void
|
|
46 |
gstring_append_double (GString *gstring,
|
|
47 |
double vdouble)
|
|
48 |
{
|
|
49 |
union { double vdouble; guint64 vuint64; } u;
|
|
50 |
u.vdouble = vdouble;
|
|
51 |
u.vuint64 = GUINT64_TO_BE (u.vuint64);
|
|
52 |
g_string_append_len (gstring, (const gchar*) &u.vuint64, 8);
|
|
53 |
}
|
|
54 |
|
|
55 |
static guint8*
|
|
56 |
g_test_log_dump (GTestLogMsg *msg,
|
|
57 |
guint *len)
|
|
58 |
{
|
|
59 |
GString *gstring = g_string_sized_new (1024);
|
|
60 |
guint ui;
|
|
61 |
gstring_append_int (gstring, 0); /* message length */
|
|
62 |
gstring_append_int (gstring, msg->log_type);
|
|
63 |
gstring_append_int (gstring, msg->n_strings);
|
|
64 |
gstring_append_int (gstring, msg->n_nums);
|
|
65 |
gstring_append_int (gstring, 0); /* reserved */
|
|
66 |
for (ui = 0; ui < msg->n_strings; ui++)
|
|
67 |
{
|
|
68 |
guint l = strlen (msg->strings[ui]);
|
|
69 |
gstring_append_int (gstring, l);
|
|
70 |
g_string_append_len (gstring, msg->strings[ui], l);
|
|
71 |
}
|
|
72 |
for (ui = 0; ui < msg->n_nums; ui++)
|
|
73 |
gstring_append_double (gstring, msg->nums[ui]);
|
|
74 |
*len = gstring->len;
|
|
75 |
gstring_overwrite_int (gstring, 0, *len); /* message length */
|
|
76 |
return (guint8*) g_string_free (gstring, FALSE);
|
|
77 |
}
|
|
78 |
|
|
79 |
void start_timer()
|
|
80 |
{
|
|
81 |
GTimer *timer;
|
|
82 |
timer = g_timer_new ();
|
|
83 |
g_timer_start(timer);
|
|
84 |
g_timer_stop(timer);
|
|
85 |
}
|
|
86 |
|
|
87 |
void test_g_test_trap()
|
|
88 |
{
|
|
89 |
if(g_test_trap_fork(0, G_TEST_TRAP_SILENCE_STDOUT))
|
|
90 |
{
|
|
91 |
exit(0);
|
|
92 |
}
|
|
93 |
|
|
94 |
if(!g_test_trap_has_passed())
|
|
95 |
{
|
72
|
96 |
g_print( "g_test_trap_has_passed didnt work as expected");
|
50
|
97 |
assert_failed = 1;
|
|
98 |
}
|
|
99 |
}
|
|
100 |
|
|
101 |
void test_g_test_log_type_name()
|
|
102 |
{
|
|
103 |
const char *ret;
|
|
104 |
ret = g_test_log_type_name(G_TEST_LOG_MESSAGE);
|
|
105 |
|
|
106 |
if(strcmp(ret, "message"))
|
|
107 |
{
|
72
|
108 |
g_print( "g_test_log_type_name didnt work as expected");
|
50
|
109 |
assert_failed = 1;
|
|
110 |
}
|
|
111 |
}
|
|
112 |
|
|
113 |
void test_g_test_timer()
|
|
114 |
{
|
|
115 |
double ret_time1, ret_time2;
|
|
116 |
|
|
117 |
g_test_timer_start();
|
|
118 |
ret_time1 = g_test_timer_elapsed();
|
|
119 |
ret_time2 = g_test_timer_last();
|
|
120 |
|
|
121 |
if(!(ret_time1 == ret_time2))
|
|
122 |
{
|
72
|
123 |
g_print( "g_test_timer* didnt work as expected");
|
50
|
124 |
assert_failed = 1;
|
|
125 |
}
|
|
126 |
}
|
|
127 |
|
|
128 |
void test_g_log_buffer()
|
|
129 |
{
|
|
130 |
GTestLogBuffer* log_buffer;
|
|
131 |
GTestLogMsg* log_msg;
|
|
132 |
GTestLogMsg msg_ip;
|
|
133 |
gchar *astrings[1] = {NULL};
|
|
134 |
guint8 *dbuffer;
|
|
135 |
guint dbufferlen;
|
|
136 |
int i;
|
|
137 |
|
|
138 |
msg_ip.log_type = G_TEST_LOG_MESSAGE;
|
|
139 |
msg_ip.n_strings = 1;
|
|
140 |
msg_ip.strings = astrings;
|
|
141 |
astrings[0] = (gchar*) "test-log-some-dummy-log";
|
|
142 |
msg_ip.n_nums = 0;
|
|
143 |
msg_ip.nums = 0;
|
|
144 |
dbuffer = (guint8*)g_test_log_dump(&msg_ip, &dbufferlen);
|
|
145 |
|
|
146 |
log_buffer = g_test_log_buffer_new();
|
|
147 |
|
|
148 |
if(log_buffer)
|
|
149 |
{
|
|
150 |
g_test_log_buffer_push(log_buffer, dbufferlen, (const guint8*)dbuffer);
|
|
151 |
|
|
152 |
log_msg = g_test_log_buffer_pop(log_buffer);
|
|
153 |
|
|
154 |
if(log_msg)
|
|
155 |
{
|
|
156 |
g_test_log_msg_free(log_msg);
|
|
157 |
}
|
|
158 |
else
|
|
159 |
{
|
72
|
160 |
g_print( "g_test_log_buffer_pop returned NULL");
|
50
|
161 |
assert_failed = 1;
|
|
162 |
}
|
|
163 |
|
|
164 |
g_test_log_buffer_free(log_buffer);
|
|
165 |
}
|
|
166 |
else
|
|
167 |
{
|
72
|
168 |
g_print( "g_test_log_buffer_new returned NULL");
|
50
|
169 |
assert_failed = 1;
|
|
170 |
}
|
|
171 |
|
|
172 |
g_free (dbuffer);
|
|
173 |
}
|
|
174 |
|
|
175 |
int main (int argc, char *argv[])
|
|
176 |
{
|
72
|
177 |
#ifdef __SYMBIAN32__
|
|
178 |
g_log_set_handler (NULL, G_LOG_FLAG_FATAL| G_LOG_FLAG_RECURSION | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_INFO | G_LOG_LEVEL_DEBUG, &mrtLogHandler, NULL);
|
|
179 |
g_set_print_handler(mrtPrintHandler);
|
|
180 |
#endif /*__SYMBIAN32__*/
|
|
181 |
|
|
182 |
g_print("Test test-utils Start");
|
50
|
183 |
g_test_init(&argc, &argv);
|
|
184 |
|
|
185 |
test_g_test_trap();
|
|
186 |
test_g_test_log_type_name();
|
|
187 |
test_g_test_timer();
|
|
188 |
test_g_log_buffer();
|
|
189 |
|
|
190 |
if(assert_failed)
|
72
|
191 |
g_print("Test test-utils Failed");
|
50
|
192 |
else
|
72
|
193 |
g_print("Test test-utils Successful");
|
50
|
194 |
|
72
|
195 |
#if __SYMBIAN32__
|
|
196 |
testResultXml("test-utils");
|
|
197 |
#endif /* EMULATOR */
|
50
|
198 |
|
|
199 |
return 0;
|
|
200 |
}
|