sl@0
|
1 |
/*
|
sl@0
|
2 |
* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
|
sl@0
|
3 |
*
|
sl@0
|
4 |
* This library is free software; you can redistribute it and/or
|
sl@0
|
5 |
* modify it under the terms of the GNU Lesser General Public
|
sl@0
|
6 |
* License as published by the Free Software Foundation; either
|
sl@0
|
7 |
* version 2 of the License, or (at your option) any later version.
|
sl@0
|
8 |
*
|
sl@0
|
9 |
* This library is distributed in the hope that it will be useful,
|
sl@0
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
sl@0
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
sl@0
|
12 |
* Lesser General Public License for more details.
|
sl@0
|
13 |
*
|
sl@0
|
14 |
* You should have received a copy of the GNU Lesser General Public
|
sl@0
|
15 |
* License along with this library; if not, write to the
|
sl@0
|
16 |
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
sl@0
|
17 |
* Boston, MA 02111-1307, USA.
|
sl@0
|
18 |
*
|
sl@0
|
19 |
* Description:
|
sl@0
|
20 |
*
|
sl@0
|
21 |
*/
|
sl@0
|
22 |
|
sl@0
|
23 |
|
sl@0
|
24 |
|
sl@0
|
25 |
#undef G_DISABLE_ASSERT
|
sl@0
|
26 |
#undef G_LOG_DOMAIN
|
sl@0
|
27 |
|
sl@0
|
28 |
#include <stdio.h>
|
sl@0
|
29 |
#include <string.h>
|
sl@0
|
30 |
#include "glib.h"
|
sl@0
|
31 |
#include <stdlib.h>
|
sl@0
|
32 |
#include <glib/gprintf.h>
|
sl@0
|
33 |
|
sl@0
|
34 |
#ifdef SYMBIAN
|
sl@0
|
35 |
#include "mrt2_glib2_test.h"
|
sl@0
|
36 |
#endif /*SYMBIAN*/
|
sl@0
|
37 |
|
sl@0
|
38 |
void array_test()
|
sl@0
|
39 |
{
|
sl@0
|
40 |
GArray *garray;
|
sl@0
|
41 |
gint i;
|
sl@0
|
42 |
int a = 1;
|
sl@0
|
43 |
int b[3] =
|
sl@0
|
44 |
{
|
sl@0
|
45 |
4,5,6
|
sl@0
|
46 |
};
|
sl@0
|
47 |
|
sl@0
|
48 |
garray = g_array_new (FALSE,FALSE,sizeof(gint));
|
sl@0
|
49 |
|
sl@0
|
50 |
g_array_append_val(garray,a);
|
sl@0
|
51 |
g_array_append_val(garray,a);
|
sl@0
|
52 |
g_array_append_val(garray,a);
|
sl@0
|
53 |
|
sl@0
|
54 |
g_array_insert_vals(garray,0,b,3);
|
sl@0
|
55 |
|
sl@0
|
56 |
g_assert (g_array_index (garray, gint, 0) == 4);
|
sl@0
|
57 |
g_assert (g_array_index (garray, gint, 1) == 5);
|
sl@0
|
58 |
g_assert (g_array_index (garray, gint, 2) == 6);
|
sl@0
|
59 |
|
sl@0
|
60 |
g_array_free(garray,TRUE);
|
sl@0
|
61 |
|
sl@0
|
62 |
}
|
sl@0
|
63 |
|
sl@0
|
64 |
void g_ascii_strdown_test()
|
sl@0
|
65 |
{
|
sl@0
|
66 |
gchar str[] = "ABCd";
|
sl@0
|
67 |
gchar *str1;
|
sl@0
|
68 |
str1 = g_ascii_strdown(str,4);
|
sl@0
|
69 |
g_assert(str1[0] == 'a');
|
sl@0
|
70 |
g_assert(str1[1] == 'b');
|
sl@0
|
71 |
g_assert(str1[2] == 'c');
|
sl@0
|
72 |
g_assert(str1[3] == 'd');
|
sl@0
|
73 |
}
|
sl@0
|
74 |
|
sl@0
|
75 |
void g_ascii_strup_test()
|
sl@0
|
76 |
{
|
sl@0
|
77 |
gchar str[] = "ABCd";
|
sl@0
|
78 |
gchar *str1;
|
sl@0
|
79 |
str1 = g_ascii_strup(str,4);
|
sl@0
|
80 |
g_assert(str1[0] == 'A');
|
sl@0
|
81 |
g_assert(str1[1] == 'B');
|
sl@0
|
82 |
g_assert(str1[2] == 'C');
|
sl@0
|
83 |
g_assert(str1[3] == 'D');
|
sl@0
|
84 |
}
|
sl@0
|
85 |
|
sl@0
|
86 |
void g_fprintf_test()
|
sl@0
|
87 |
{
|
sl@0
|
88 |
FILE *fp;
|
sl@0
|
89 |
char *teststring = "testing";
|
sl@0
|
90 |
int retVal;
|
sl@0
|
91 |
fp = fopen("c:\\test.txt","w");
|
sl@0
|
92 |
if(fp)
|
sl@0
|
93 |
{
|
sl@0
|
94 |
g_assert(g_fprintf(fp,"%s",teststring) == strlen(teststring));
|
sl@0
|
95 |
fclose(fp);
|
sl@0
|
96 |
}
|
sl@0
|
97 |
}
|
sl@0
|
98 |
|
sl@0
|
99 |
void g_application_name_test()
|
sl@0
|
100 |
{
|
sl@0
|
101 |
gchar *app_name = "Test App";
|
sl@0
|
102 |
g_set_application_name(app_name);
|
sl@0
|
103 |
g_assert(!strcmp(g_get_application_name(),app_name));
|
sl@0
|
104 |
}
|
sl@0
|
105 |
|
sl@0
|
106 |
void g_listenv_test()
|
sl@0
|
107 |
{
|
sl@0
|
108 |
gchar **e = NULL;
|
sl@0
|
109 |
int i;
|
sl@0
|
110 |
e = g_listenv();
|
sl@0
|
111 |
g_assert(e!=NULL);
|
sl@0
|
112 |
g_strfreev(e);
|
sl@0
|
113 |
}
|
sl@0
|
114 |
|
sl@0
|
115 |
void g_direct_equal_test()
|
sl@0
|
116 |
{
|
sl@0
|
117 |
int *i,a=1,*j;
|
sl@0
|
118 |
i = &a;
|
sl@0
|
119 |
j=i;
|
sl@0
|
120 |
g_assert(g_direct_equal(i,j));
|
sl@0
|
121 |
j++;
|
sl@0
|
122 |
g_assert(!g_direct_equal(i,j));
|
sl@0
|
123 |
}
|
sl@0
|
124 |
|
sl@0
|
125 |
void g_direct_hash_test()
|
sl@0
|
126 |
{
|
sl@0
|
127 |
int *i,a=1,j;
|
sl@0
|
128 |
int hash_value;
|
sl@0
|
129 |
i = &a;
|
sl@0
|
130 |
j = (gint)i;
|
sl@0
|
131 |
g_assert(g_direct_hash(i) == j);
|
sl@0
|
132 |
}
|
sl@0
|
133 |
|
sl@0
|
134 |
void g_bit_nth_lsf_test()
|
sl@0
|
135 |
{
|
sl@0
|
136 |
gulong mask = 15;
|
sl@0
|
137 |
|
sl@0
|
138 |
// 15 is 00000000 ........ 1111. Therefore the position of first 1 should be 0
|
sl@0
|
139 |
g_assert(g_bit_nth_lsf(mask,-1) == 0);
|
sl@0
|
140 |
}
|
sl@0
|
141 |
|
sl@0
|
142 |
void g_bit_nth_msf_test()
|
sl@0
|
143 |
{
|
sl@0
|
144 |
gulong mask = 15;
|
sl@0
|
145 |
|
sl@0
|
146 |
// 15 is 00000000 ........ 1111. Therefore the position of last 1 should be 3
|
sl@0
|
147 |
g_assert(g_bit_nth_msf(mask,-1) == 3);
|
sl@0
|
148 |
}
|
sl@0
|
149 |
|
sl@0
|
150 |
void g_basename_test()
|
sl@0
|
151 |
{
|
sl@0
|
152 |
const gchar *filename;
|
sl@0
|
153 |
filename = g_basename("c:\\test\\test.txt");
|
sl@0
|
154 |
g_assert(!strcmp(filename,"test.txt"));
|
sl@0
|
155 |
}
|
sl@0
|
156 |
|
sl@0
|
157 |
|
sl@0
|
158 |
void function()
|
sl@0
|
159 |
{
|
sl@0
|
160 |
return;
|
sl@0
|
161 |
}
|
sl@0
|
162 |
|
sl@0
|
163 |
void g_atexit_test()
|
sl@0
|
164 |
{
|
sl@0
|
165 |
g_atexit(function);
|
sl@0
|
166 |
}
|
sl@0
|
167 |
|
sl@0
|
168 |
void g_bit_storage_test()
|
sl@0
|
169 |
{
|
sl@0
|
170 |
g_assert(g_bit_storage(8) == 4);
|
sl@0
|
171 |
}
|
sl@0
|
172 |
|
sl@0
|
173 |
void g_filename_display_basename_test()
|
sl@0
|
174 |
{
|
sl@0
|
175 |
g_assert(!strcmp(g_filename_display_basename("c:/test/test.txt"),"test.txt"));
|
sl@0
|
176 |
}
|
sl@0
|
177 |
|
sl@0
|
178 |
void g_find_program_in_path_test()
|
sl@0
|
179 |
{
|
sl@0
|
180 |
char *program_name = g_find_program_in_path("misc_test.exe");
|
sl@0
|
181 |
}
|
sl@0
|
182 |
|
sl@0
|
183 |
void g_atomic_test()
|
sl@0
|
184 |
{
|
sl@0
|
185 |
int i = 5;
|
sl@0
|
186 |
int j;
|
sl@0
|
187 |
char *p,*q;
|
sl@0
|
188 |
|
sl@0
|
189 |
j = g_atomic_int_get(&i);
|
sl@0
|
190 |
g_assert(j == i);
|
sl@0
|
191 |
|
sl@0
|
192 |
p = g_malloc(1);
|
sl@0
|
193 |
q = g_atomic_pointer_get(&p);
|
sl@0
|
194 |
g_assert(p == q);
|
sl@0
|
195 |
|
sl@0
|
196 |
g_free(p);
|
sl@0
|
197 |
}
|
sl@0
|
198 |
|
sl@0
|
199 |
void g_error_test()
|
sl@0
|
200 |
{
|
sl@0
|
201 |
GError err,*err_copy = NULL;
|
sl@0
|
202 |
err.domain = 1;
|
sl@0
|
203 |
err.code = 5;
|
sl@0
|
204 |
err.message = "test";
|
sl@0
|
205 |
err_copy = g_error_copy(&err);
|
sl@0
|
206 |
g_assert(err_copy->code == 5 && err_copy->code == 5 && !strcmp(err_copy->message,"test"));
|
sl@0
|
207 |
g_error_free(err_copy);
|
sl@0
|
208 |
err_copy = NULL;
|
sl@0
|
209 |
err_copy = g_error_new_literal(err.domain,err.code,"test is %s");
|
sl@0
|
210 |
g_assert(err_copy->code == 5 && err_copy->code == 5 && !strcmp(err_copy->message,"test is %s"));
|
sl@0
|
211 |
g_error_free(err_copy);
|
sl@0
|
212 |
err_copy = NULL;
|
sl@0
|
213 |
}
|
sl@0
|
214 |
|
sl@0
|
215 |
static guint
|
sl@0
|
216 |
my_hash (gconstpointer key)
|
sl@0
|
217 |
{
|
sl@0
|
218 |
return (guint) *((const gint*) key);
|
sl@0
|
219 |
}
|
sl@0
|
220 |
|
sl@0
|
221 |
static gboolean
|
sl@0
|
222 |
my_hash_equal (gconstpointer a,
|
sl@0
|
223 |
gconstpointer b)
|
sl@0
|
224 |
{
|
sl@0
|
225 |
return *((const gint*) a) == *((const gint*) b);
|
sl@0
|
226 |
}
|
sl@0
|
227 |
|
sl@0
|
228 |
static gboolean find_first (gpointer key,
|
sl@0
|
229 |
gpointer value,
|
sl@0
|
230 |
gpointer user_data)
|
sl@0
|
231 |
{
|
sl@0
|
232 |
gint *v = value;
|
sl@0
|
233 |
gint *test = user_data;
|
sl@0
|
234 |
return (*v == *test);
|
sl@0
|
235 |
}
|
sl@0
|
236 |
|
sl@0
|
237 |
gboolean func(gpointer key,gpointer value,gpointer user_data)
|
sl@0
|
238 |
{
|
sl@0
|
239 |
gint *key1 = (int *)key;
|
sl@0
|
240 |
gint *value1 = (int *)value;
|
sl@0
|
241 |
gint *user_data1 = (int *)user_data;
|
sl@0
|
242 |
if(*key1 == *user_data1 && *value1 == *user_data1)
|
sl@0
|
243 |
return TRUE;
|
sl@0
|
244 |
else
|
sl@0
|
245 |
return FALSE;
|
sl@0
|
246 |
}
|
sl@0
|
247 |
|
sl@0
|
248 |
void hash_test()
|
sl@0
|
249 |
{
|
sl@0
|
250 |
GHashTable *hash_table;
|
sl@0
|
251 |
gint i;
|
sl@0
|
252 |
gint value = 4;
|
sl@0
|
253 |
gint *pvalue;
|
sl@0
|
254 |
int array[10];
|
sl@0
|
255 |
int count = 0;
|
sl@0
|
256 |
|
sl@0
|
257 |
hash_table = g_hash_table_new (my_hash, my_hash_equal);
|
sl@0
|
258 |
for (i = 0; i < 10; i++)
|
sl@0
|
259 |
{
|
sl@0
|
260 |
array[i] = i;
|
sl@0
|
261 |
g_hash_table_insert (hash_table, &array[i], &array[i]);
|
sl@0
|
262 |
}
|
sl@0
|
263 |
|
sl@0
|
264 |
g_assert(g_hash_table_steal(hash_table,&value));
|
sl@0
|
265 |
|
sl@0
|
266 |
pvalue = g_hash_table_find (hash_table, find_first, &value);
|
sl@0
|
267 |
|
sl@0
|
268 |
//checks if g_hash_table_steal worked or not
|
sl@0
|
269 |
g_assert(pvalue == NULL);
|
sl@0
|
270 |
|
sl@0
|
271 |
value = 5;
|
sl@0
|
272 |
|
sl@0
|
273 |
pvalue = g_hash_table_find (hash_table, find_first, &value);
|
sl@0
|
274 |
|
sl@0
|
275 |
count = g_hash_table_foreach_steal(hash_table,func,&value);
|
sl@0
|
276 |
|
sl@0
|
277 |
pvalue = g_hash_table_find (hash_table, find_first, &value);
|
sl@0
|
278 |
|
sl@0
|
279 |
g_assert(count == 1 && pvalue == NULL);
|
sl@0
|
280 |
|
sl@0
|
281 |
}
|
sl@0
|
282 |
|
sl@0
|
283 |
void check_version_test()
|
sl@0
|
284 |
{
|
sl@0
|
285 |
const char *x = glib_check_version(2,8,3);
|
sl@0
|
286 |
g_assert(x == NULL);
|
sl@0
|
287 |
}
|
sl@0
|
288 |
|
sl@0
|
289 |
int main (int argc,
|
sl@0
|
290 |
char *argv[])
|
sl@0
|
291 |
{
|
sl@0
|
292 |
#ifdef SYMBIAN
|
sl@0
|
293 |
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);
|
sl@0
|
294 |
#endif /*SYMBIAN*/
|
sl@0
|
295 |
|
sl@0
|
296 |
array_test();
|
sl@0
|
297 |
g_ascii_strup_test();
|
sl@0
|
298 |
g_ascii_strdown_test();
|
sl@0
|
299 |
g_fprintf_test();
|
sl@0
|
300 |
g_application_name_test();
|
sl@0
|
301 |
g_listenv_test();
|
sl@0
|
302 |
g_direct_equal_test();
|
sl@0
|
303 |
g_direct_hash_test();
|
sl@0
|
304 |
g_bit_nth_lsf_test();
|
sl@0
|
305 |
g_bit_nth_msf_test();
|
sl@0
|
306 |
g_basename_test();
|
sl@0
|
307 |
g_atexit_test();
|
sl@0
|
308 |
g_bit_storage_test();
|
sl@0
|
309 |
g_filename_display_basename_test();
|
sl@0
|
310 |
g_find_program_in_path_test();
|
sl@0
|
311 |
g_atomic_test();
|
sl@0
|
312 |
g_error_test();
|
sl@0
|
313 |
hash_test();
|
sl@0
|
314 |
check_version_test();
|
sl@0
|
315 |
|
sl@0
|
316 |
#ifdef SYMBIAN
|
sl@0
|
317 |
testResultXml("misc_test");
|
sl@0
|
318 |
#endif /* EMULATOR */
|
sl@0
|
319 |
|
sl@0
|
320 |
return 0;
|
sl@0
|
321 |
}
|