|
1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 #undef G_DISABLE_ASSERT |
|
22 #undef G_LOG_DOMAIN |
|
23 |
|
24 #include <stdio.h> |
|
25 #include <string.h> |
|
26 #include "glib.h" |
|
27 |
|
28 #ifdef __SYMBIAN32__ |
|
29 #include "mrt2_glib2_test.h" |
|
30 #endif /*__SYMBIAN32__*/ |
|
31 |
|
32 gboolean func1(int *data) |
|
33 { |
|
34 *data = 1; |
|
35 return TRUE; |
|
36 } |
|
37 |
|
38 gboolean func2(int *data) |
|
39 { |
|
40 *data = 2; |
|
41 return TRUE; |
|
42 } |
|
43 |
|
44 gboolean func3(int *data) |
|
45 { |
|
46 *data = 3; |
|
47 return FALSE; |
|
48 } |
|
49 |
|
50 gboolean func4(int *data) |
|
51 { |
|
52 *data = 4; |
|
53 return TRUE; |
|
54 } |
|
55 |
|
56 gint sort_func(GHook *new_hook,GHook *sibling) |
|
57 { |
|
58 if(new_hook->hook_id < sibling->hook_id) |
|
59 return 0; |
|
60 else |
|
61 return 1; |
|
62 } |
|
63 |
|
64 gboolean find_func(GHook *hook,gpointer data) |
|
65 { |
|
66 if(hook->hook_id == 1) |
|
67 return TRUE; |
|
68 else |
|
69 return FALSE; |
|
70 } |
|
71 |
|
72 void marshaller(GHook *hook,gpointer mashal_data) |
|
73 { |
|
74 gint *data = (int *)hook->data; |
|
75 *data = -1; |
|
76 } |
|
77 |
|
78 gboolean check_marshaller(GHook *hook,gpointer mashal_data) |
|
79 { |
|
80 if(hook->hook_id == 2) // for hook2 id is 2 |
|
81 return FALSE; |
|
82 else |
|
83 return TRUE; |
|
84 } |
|
85 |
|
86 |
|
87 void hook_test() |
|
88 { |
|
89 GHookList hooklist; |
|
90 GHook *hook1 = NULL,*hook2 = NULL,*hook3 = NULL,*hook4 = NULL,*temp_hook; |
|
91 int data1 = 0,data2 = 0,data3 = 0,data4 = 0; |
|
92 int comp_value; |
|
93 gboolean val; |
|
94 |
|
95 g_hook_list_init(&hooklist,sizeof(GHook)); |
|
96 |
|
97 hook1 = g_hook_alloc(&hooklist); |
|
98 hook1->func = (gpointer)func1; |
|
99 hook1->data = &data1; |
|
100 |
|
101 hook2 = g_hook_alloc(&hooklist); |
|
102 hook2->func = (gpointer)func2; |
|
103 hook2->data = &data2; |
|
104 |
|
105 hook3 = g_hook_alloc(&hooklist); |
|
106 hook3->func = (gpointer)func3; |
|
107 hook3->data = &data3; |
|
108 |
|
109 hook4 = g_hook_alloc(&hooklist); |
|
110 hook4->func = (gpointer)func4; |
|
111 hook4->data = &data4; |
|
112 |
|
113 g_hook_append(&hooklist,hook4); |
|
114 g_hook_prepend(&hooklist,hook3); |
|
115 g_hook_insert_before(&hooklist,hook3,hook2); |
|
116 g_hook_insert_sorted(&hooklist,hook1,sort_func); |
|
117 |
|
118 g_hook_list_invoke(&hooklist,FALSE); |
|
119 |
|
120 // checks g_hook_list_init,g_hook_alloc,g_hook_append,g_hook_prepend,g_hook_insert_before,g_hook_insert_sorted |
|
121 g_assert(data1 == 1 && data2 == 2 && data3 == 3 && data4 == 4); |
|
122 |
|
123 comp_value = g_hook_compare_ids(hook2,hook1); |
|
124 |
|
125 //checks g_hook_compare_ids |
|
126 g_assert(comp_value < 0); |
|
127 |
|
128 temp_hook = g_hook_get(&hooklist,10); |
|
129 |
|
130 //checks g_hook_get |
|
131 g_assert(temp_hook == NULL); |
|
132 |
|
133 temp_hook = g_hook_get(&hooklist,1); |
|
134 |
|
135 //checks g_hook_get |
|
136 g_assert(temp_hook == hook4); |
|
137 |
|
138 temp_hook = NULL; |
|
139 |
|
140 temp_hook = g_hook_find(&hooklist,TRUE,find_func,NULL); |
|
141 |
|
142 //checks g_hook_find |
|
143 g_assert(temp_hook == hook4); |
|
144 |
|
145 temp_hook = NULL; |
|
146 |
|
147 temp_hook = g_hook_find_data(&hooklist,TRUE,&data1); |
|
148 |
|
149 //checks g_hook_find_data |
|
150 g_assert(temp_hook == hook1); |
|
151 |
|
152 temp_hook = NULL; |
|
153 |
|
154 temp_hook = g_hook_find_func(&hooklist,TRUE,(gpointer)func2); |
|
155 |
|
156 //checks g_hook_find_func |
|
157 g_assert(temp_hook == hook2); |
|
158 |
|
159 temp_hook = g_hook_find_func_data(&hooklist,TRUE,(gpointer)func2,&data1); |
|
160 |
|
161 //checks g_hook_find_func_data |
|
162 g_assert(temp_hook == NULL); |
|
163 |
|
164 temp_hook = g_hook_find_func_data(&hooklist,TRUE,(gpointer)func1,&data1); |
|
165 |
|
166 //checks g_hook_find_func_data |
|
167 g_assert(temp_hook == hook1); |
|
168 |
|
169 temp_hook = NULL; |
|
170 |
|
171 temp_hook = g_hook_ref(&hooklist,hook3); |
|
172 |
|
173 //checks g_hook_ref |
|
174 g_assert(hook3->ref_count == 2); |
|
175 |
|
176 g_hook_unref(&hooklist,hook3); |
|
177 |
|
178 //checks g_hook_unref |
|
179 g_assert(hook3->ref_count == 1); |
|
180 |
|
181 g_hook_list_marshal(&hooklist,TRUE,marshaller,NULL); |
|
182 |
|
183 //checks g_hook_list_marshal |
|
184 g_assert(data1 == -1 && data2 == -1 && data3 == -1 && data4 == -1); |
|
185 |
|
186 g_hook_list_marshal_check(&hooklist,TRUE,check_marshaller,NULL); |
|
187 |
|
188 // checks g_hook_list_marshal_check |
|
189 // func3 is for hook3 and the check_marshaller returns FALSE for hook3 |
|
190 // As a rsult the hook is deleted from the hook list. |
|
191 g_assert(g_hook_find_func(&hooklist,TRUE,(gpointer)func3) == NULL); |
|
192 |
|
193 g_hook_list_clear(&hooklist); |
|
194 |
|
195 //checks g_hook_list_clear |
|
196 g_assert(hooklist.hooks == NULL); |
|
197 } |
|
198 |
|
199 int main (int argc, |
|
200 char *argv[]) |
|
201 { |
|
202 #ifdef __SYMBIAN32__ |
|
203 |
|
204 |
|
205 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); |
|
206 #endif /*__SYMBIAN32__*/ |
|
207 |
|
208 hook_test(); |
|
209 |
|
210 #if __SYMBIAN32__ |
|
211 testResultXml("hook_test"); |
|
212 #endif /* EMULATOR */ |
|
213 |
|
214 return 0; |
|
215 |
|
216 } |