|
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 |
|
17 #undef G_DISABLE_ASSERT |
|
18 #undef G_LOG_DOMAIN |
|
19 |
|
20 #include <glib.h> |
|
21 #define LOG_FILE "c:\\logs\\array1_test_log.txt" |
|
22 #include "std_log_result.h" |
|
23 #define LOG_FILENAME_LINE __FILE__, __LINE__ |
|
24 |
|
25 |
|
26 void create_xml(int result) |
|
27 { |
|
28 if(result) |
|
29 assert_failed = 1; |
|
30 |
|
31 testResultXml("array-test"); |
|
32 close_log_file(); |
|
33 } |
|
34 |
|
35 static gint sort (gconstpointer a, gconstpointer b) |
|
36 { |
|
37 if(*(guint32*)a == *(guint32*)b) |
|
38 return 0; |
|
39 else |
|
40 return *(guint32*)a < *(guint32*)b ? -1 : 1; |
|
41 } |
|
42 |
|
43 |
|
44 static gint sort_userdata (gconstpointer a, gconstpointer b, gpointer user_data) |
|
45 { |
|
46 if(*(guint32*)a == *(guint32*)b) |
|
47 return 0; |
|
48 else |
|
49 return *(guint32*)a < *(guint32*)b ? -1 : 1; |
|
50 } |
|
51 |
|
52 |
|
53 |
|
54 /*Will return TRUE if input GArray and int[] array 2 are same else FALSE*/ |
|
55 gboolean compare_array(GArray* array1, gint* array2, gint size) |
|
56 { |
|
57 |
|
58 int i; |
|
59 if ( size != array1->len) |
|
60 return FALSE; |
|
61 for ( i = 0; i < size ; i++) |
|
62 { |
|
63 if ( g_array_index(array1, gint, i) != array2[i]) |
|
64 return FALSE; |
|
65 } |
|
66 |
|
67 return TRUE; |
|
68 } |
|
69 |
|
70 |
|
71 void test_remove_array_index() |
|
72 { |
|
73 GArray *garray; |
|
74 |
|
75 const gint ARRAY_SIZE = 7; |
|
76 const gint ARRAY_SIZE_AFTER_REMOVE_INDEX = 6; |
|
77 const gint ARRAY_SIZE_AFTER_REMOVE_INDEX_FAST = 5; |
|
78 |
|
79 gint array[ARRAY_SIZE] = {4,5,6,7,8,9,10}; |
|
80 gint array_after_remove_index_1[ARRAY_SIZE_AFTER_REMOVE_INDEX]= {4,6,7,8,9,10}; /*array after removing index 1*/ |
|
81 gint array_after_remove_index_fast_2[ARRAY_SIZE_AFTER_REMOVE_INDEX_FAST] = {4,6,10,8,9}; /* array after removing index 2 fast. Input is array after removing index 1 */ |
|
82 |
|
83 int i; |
|
84 gboolean ret; |
|
85 |
|
86 garray = g_array_new (FALSE,FALSE,sizeof(gint)); |
|
87 if(garray == NULL) |
|
88 { |
|
89 std_log(LOG_FILENAME_LINE, "Array not created"); |
|
90 assert_failed = 1; |
|
91 return ; |
|
92 } |
|
93 |
|
94 /*Insert values into array*/ |
|
95 g_array_insert_vals(garray,0,array,ARRAY_SIZE); |
|
96 |
|
97 /* test for deleting single element in an array. Removing the element with index 1*/ |
|
98 std_log(LOG_FILENAME_LINE, "Delete array element at index 1"); |
|
99 garray = g_array_remove_index(garray,1); |
|
100 |
|
101 if(garray == NULL ) |
|
102 { |
|
103 std_log(LOG_FILENAME_LINE, "NULL return by g_array_remove_index"); |
|
104 assert_failed = 1; |
|
105 return ; |
|
106 } |
|
107 |
|
108 /*Print the array elements after remove*/ |
|
109 for(i=0; i<garray->len; i++) |
|
110 { |
|
111 std_log(LOG_FILENAME_LINE, "Current array element at index %d is %d", i,g_array_index(garray, gint, i)); |
|
112 } |
|
113 |
|
114 /*Check if the array size is now 4 and element at index 1 is not 5 after removal*/ |
|
115 |
|
116 ret = compare_array(garray, array_after_remove_index_1, ARRAY_SIZE_AFTER_REMOVE_INDEX); |
|
117 if ( !ret) |
|
118 { |
|
119 std_log(LOG_FILENAME_LINE, "Array Element not properly deleted by g_array_remove_index"); |
|
120 assert_failed = 1; |
|
121 g_array_free(garray,TRUE); |
|
122 return ; |
|
123 } |
|
124 |
|
125 |
|
126 /* Test to remove index element 2 using g_array_remove_index_fast*/ |
|
127 |
|
128 std_log(LOG_FILENAME_LINE, "Delete array element at index 2"); |
|
129 garray =g_array_remove_index_fast(garray,2); |
|
130 if(garray == NULL) |
|
131 { |
|
132 std_log(LOG_FILENAME_LINE, "NULL return by g_array_remove_index_fast"); |
|
133 assert_failed = 1; |
|
134 return ; |
|
135 } |
|
136 |
|
137 for(i=0; i<garray->len; i++) |
|
138 { |
|
139 std_log(LOG_FILENAME_LINE, "Current array element at index %d is %d", i,g_array_index(garray, gint, i)); |
|
140 } |
|
141 |
|
142 ret = compare_array(garray, array_after_remove_index_fast_2, ARRAY_SIZE_AFTER_REMOVE_INDEX_FAST); |
|
143 if ( !ret) |
|
144 { |
|
145 std_log(LOG_FILENAME_LINE, "Array Element not properly deleted by g_array_remove_index_fast"); |
|
146 assert_failed = 1; |
|
147 g_array_free(garray,TRUE); |
|
148 return; |
|
149 } |
|
150 g_array_free(garray,TRUE); |
|
151 } |
|
152 |
|
153 |
|
154 void test_test_remove_array_index_range() |
|
155 { |
|
156 const int ARRAY_SIZE = 10; |
|
157 const int ARRAY_SIZE_AFTER_REMOVE_INDEX_RANGE = 8; |
|
158 |
|
159 gint array[ARRAY_SIZE]= {10,5,16,7,11,0,20,1,9,8}; |
|
160 gint array_after_remove_index_range[ARRAY_SIZE_AFTER_REMOVE_INDEX_RANGE] = {10,5,16,0,20,1,9,8}; /*after removing 2 elements from index 3*/ |
|
161 |
|
162 gboolean ret; |
|
163 GArray* garray; |
|
164 int i; |
|
165 |
|
166 garray = g_array_new (FALSE,FALSE,sizeof(gint)); |
|
167 if(garray == NULL) |
|
168 { |
|
169 std_log(LOG_FILENAME_LINE, "Array not created"); |
|
170 assert_failed = 1; |
|
171 return ; |
|
172 } |
|
173 |
|
174 g_array_insert_vals(garray,0,array,ARRAY_SIZE); |
|
175 for(i=0; i<garray->len;i++) |
|
176 { |
|
177 std_log(LOG_FILENAME_LINE, "Current array elements %d is %d", i,g_array_index(garray, gint, i)); |
|
178 } |
|
179 |
|
180 garray = g_array_remove_range(garray,3,2); /*remove two elements from index 3 */ |
|
181 |
|
182 if(garray == NULL) |
|
183 { |
|
184 std_log(LOG_FILENAME_LINE,"Elements not deleted properly by g_array_remove_range"); |
|
185 assert_failed = 1; |
|
186 return ; |
|
187 |
|
188 } |
|
189 |
|
190 /*print the array elements */ |
|
191 for(i=0; i<garray->len;i++) |
|
192 { |
|
193 std_log(LOG_FILENAME_LINE, "Curent array element(after deletion) %d is %d", i,g_array_index(garray, gint, i)); |
|
194 } |
|
195 |
|
196 |
|
197 ret = compare_array(garray, array_after_remove_index_range, ARRAY_SIZE_AFTER_REMOVE_INDEX_RANGE); |
|
198 if(!ret) |
|
199 { |
|
200 std_log(LOG_FILENAME_LINE,"Elements not deleted properly"); |
|
201 assert_failed = 1; |
|
202 g_array_free(garray,TRUE); |
|
203 return ; |
|
204 } |
|
205 g_array_free(garray,TRUE); |
|
206 } |
|
207 |
|
208 |
|
209 void test_sort_array() |
|
210 { |
|
211 |
|
212 GArray *garray; |
|
213 const int ARRAY_SIZE = 11; |
|
214 gint array[ARRAY_SIZE] = {10,5,16,7,11,0,20,1,9,8,9}; |
|
215 gint sort_array[ARRAY_SIZE] = {0,1,5,7,8,9,9,10,11,16,20}; |
|
216 |
|
217 gboolean ret; |
|
218 int i; |
|
219 |
|
220 /* Test for sorting the array elements */ |
|
221 |
|
222 garray = g_array_new (FALSE,FALSE,sizeof(gint)); |
|
223 |
|
224 if(garray == NULL) |
|
225 { |
|
226 std_log(LOG_FILENAME_LINE, "Array not created"); |
|
227 assert_failed = 1; |
|
228 return ; |
|
229 } |
|
230 g_array_insert_vals(garray,0,array,ARRAY_SIZE); |
|
231 |
|
232 g_array_sort(garray, sort); |
|
233 |
|
234 if(garray == NULL) |
|
235 { |
|
236 std_log(LOG_FILENAME_LINE, "Array not sorted"); |
|
237 assert_failed = 1; |
|
238 return ; |
|
239 } |
|
240 |
|
241 std_log(LOG_FILENAME_LINE,"SORTED ARRAY"); |
|
242 |
|
243 for(i=0;i<garray->len;i++) |
|
244 { |
|
245 std_log(LOG_FILENAME_LINE, "Element %d is %d", i,g_array_index(garray, gint, i)); |
|
246 } |
|
247 |
|
248 ret = compare_array(garray, sort_array, ARRAY_SIZE); |
|
249 |
|
250 if(!ret) |
|
251 { |
|
252 std_log(LOG_FILENAME_LINE, "Array not sorted correctly"); |
|
253 assert_failed = 1; |
|
254 return ; |
|
255 } |
|
256 |
|
257 g_array_free(garray,TRUE); |
|
258 |
|
259 |
|
260 garray = g_array_new (FALSE,FALSE,sizeof(gint)); |
|
261 if(garray == NULL) |
|
262 { |
|
263 std_log(LOG_FILENAME_LINE, "Array not created"); |
|
264 return ; |
|
265 } |
|
266 g_array_insert_vals(garray,0,array,ARRAY_SIZE); |
|
267 |
|
268 g_array_sort_with_data (garray, sort_userdata, NULL); |
|
269 |
|
270 if(garray == NULL) |
|
271 { |
|
272 std_log(LOG_FILENAME_LINE, "Array not sorted with user data"); |
|
273 assert_failed = 1; |
|
274 return ; |
|
275 } |
|
276 |
|
277 std_log(LOG_FILENAME_LINE,"SORTED ARRAY WITH USERDATA"); |
|
278 for(i=0;i<garray->len;i++) |
|
279 { |
|
280 std_log(LOG_FILENAME_LINE, "Element %d is %d", i,g_array_index(garray, gint, i)); |
|
281 } |
|
282 |
|
283 ret = compare_array(garray, sort_array, ARRAY_SIZE); |
|
284 if(!ret) |
|
285 { |
|
286 std_log(LOG_FILENAME_LINE, "Array not sorted correctly with user data"); |
|
287 assert_failed = 1; |
|
288 return ; |
|
289 } |
|
290 g_array_free(garray,TRUE); |
|
291 } |
|
292 |
|
293 |
|
294 int main (void) |
|
295 { |
|
296 test_test_remove_array_index_range(); |
|
297 test_sort_array(); |
|
298 test_remove_array_index(); |
|
299 |
|
300 if(assert_failed) |
|
301 std_log(LOG_FILENAME_LINE,"Test Failed"); |
|
302 else |
|
303 std_log(LOG_FILENAME_LINE,"Test Successful"); |
|
304 |
|
305 create_xml(0); |
|
306 return 0; |
|
307 } |
|
308 |
|
309 |