5 /* Redefine the private structure only to verify proper allocations */
6 typedef struct _GPtrArrayPriv
{
12 /* Don't add more than 32 items to this please */
13 static const char *items
[] = {
14 "Apples", "Oranges", "Plumbs", "Goats", "Snorps", "Grapes",
15 "Tickle", "Place", "Coffee", "Cookies", "Cake", "Cheese",
16 "Tseng", "Holiday", "Avenue", "Smashing", "Water", "Toilet",
20 static GPtrArray
*ptrarray_alloc_and_fill(guint
*item_count
)
22 GPtrArray
*array
= g_ptr_array_new();
25 for(i
= 0; items
[i
] != NULL
; i
++) {
26 g_ptr_array_add(array
, (gpointer
)items
[i
]);
29 if(item_count
!= NULL
) {
36 static guint
guess_size(guint length
)
40 while(size
< length
) {
47 RESULT
ptrarray_alloc()
52 array
= (GPtrArrayPriv
*)ptrarray_alloc_and_fill(&i
);
54 if(array
->size
!= guess_size(array
->len
)) {
55 return FAILED("Size should be %d, but it is %d",
56 guess_size(array
->len
), array
->size
);
60 return FAILED("Expected %d node(s) in the array", i
);
63 g_ptr_array_free((GPtrArray
*)array
, TRUE
);
68 RESULT
ptrarray_for_iterate()
70 GPtrArray
*array
= ptrarray_alloc_and_fill(NULL
);
73 for(i
= 0; i
< array
->len
; i
++) {
74 char *item
= (char *)g_ptr_array_index(array
, i
);
75 if(item
!= items
[i
]) {
77 "Expected item at %d to be %s, but it was %s",
82 g_ptr_array_free(array
, TRUE
);
87 static gint foreach_iterate_index
= 0;
88 static char *foreach_iterate_error
= NULL
;
90 void foreach_callback(gpointer data
, gpointer user_data
)
92 char *item
= (char *)data
;
93 const char *item_cmp
= items
[foreach_iterate_index
++];
95 if(foreach_iterate_error
!= NULL
) {
99 if(item
!= item_cmp
) {
100 foreach_iterate_error
= FAILED(
101 "Expected item at %d to be %s, but it was %s",
102 foreach_iterate_index
- 1, item_cmp
, item
);
106 RESULT
ptrarray_foreach_iterate()
108 GPtrArray
*array
= ptrarray_alloc_and_fill(NULL
);
110 foreach_iterate_index
= 0;
111 foreach_iterate_error
= NULL
;
113 g_ptr_array_foreach(array
, foreach_callback
, array
);
115 g_ptr_array_free(array
, TRUE
);
117 return foreach_iterate_error
;
120 RESULT
ptrarray_set_size()
122 GPtrArray
*array
= g_ptr_array_new();
123 guint i
, grow_length
= 50;
125 g_ptr_array_add(array
, (gpointer
)items
[0]);
126 g_ptr_array_add(array
, (gpointer
)items
[1]);
127 g_ptr_array_set_size(array
, grow_length
);
129 if(array
->len
!= grow_length
) {
130 return FAILED("Array length should be 50, it is %d", array
->len
);
131 } else if(array
->pdata
[0] != items
[0]) {
132 return FAILED("Item 0 was overwritten, should be %s", items
[0]);
133 } else if(array
->pdata
[1] != items
[1]) {
134 return FAILED("Item 1 was overwritten, should be %s", items
[1]);
137 for(i
= 2; i
< array
->len
; i
++) {
138 if(array
->pdata
[i
] != NULL
) {
139 return FAILED("Item %d is not NULL, it is %p", i
, array
->pdata
[i
]);
143 g_ptr_array_free(array
, TRUE
);
148 RESULT
ptrarray_remove_index()
153 array
= ptrarray_alloc_and_fill(&i
);
155 g_ptr_array_remove_index(array
, 0);
156 if(array
->pdata
[0] != items
[1]) {
157 return FAILED("First item is not %s, it is %s", items
[1],
161 g_ptr_array_remove_index(array
, array
->len
- 1);
163 if(array
->pdata
[array
->len
- 1] != items
[array
->len
]) {
164 return FAILED("Last item is not %s, it is %s",
165 items
[array
->len
- 2], array
->pdata
[array
->len
- 1]);
168 g_ptr_array_free(array
, TRUE
);
173 RESULT
ptrarray_remove_index_fast()
178 array
= ptrarray_alloc_and_fill(&i
);
180 g_ptr_array_remove_index_fast(array
, 0);
181 if(array
->pdata
[0] != items
[array
->len
]) {
182 return FAILED("First item is not %s, it is %s", items
[array
->len
],
186 g_ptr_array_remove_index_fast(array
, array
->len
- 1);
187 if(array
->pdata
[array
->len
- 1] != items
[array
->len
- 1]) {
188 return FAILED("Last item is not %s, it is %s",
189 items
[array
->len
- 1], array
->pdata
[array
->len
- 1]);
192 g_ptr_array_free(array
, TRUE
);
197 RESULT
ptrarray_remove()
202 array
= ptrarray_alloc_and_fill(&i
);
204 g_ptr_array_remove(array
, (gpointer
)items
[7]);
206 if(!g_ptr_array_remove(array
, (gpointer
)items
[4])) {
207 return FAILED("Item %s not removed", items
[4]);
210 if(g_ptr_array_remove(array
, (gpointer
)items
[4])) {
211 return FAILED("Item %s still in array after removal", items
[4]);
214 if(array
->pdata
[array
->len
- 1] != items
[array
->len
+ 1]) {
215 return FAILED("Last item in GPtrArray not correct");
218 g_ptr_array_free(array
, TRUE
);
223 static gint
ptrarray_sort_compare(gconstpointer a
, gconstpointer b
)
225 gchar
*stra
= *(gchar
**) a
;
226 gchar
*strb
= *(gchar
**) b
;
227 return strcmp(stra
, strb
);
230 RESULT
ptrarray_sort()
232 GPtrArray
*array
= g_ptr_array_new();
234 gchar
*letters
[] = { "A", "B", "C", "D", "E" };
236 g_ptr_array_add(array
, letters
[0]);
237 g_ptr_array_add(array
, letters
[1]);
238 g_ptr_array_add(array
, letters
[2]);
239 g_ptr_array_add(array
, letters
[3]);
240 g_ptr_array_add(array
, letters
[4]);
242 g_ptr_array_sort(array
, ptrarray_sort_compare
);
244 for(i
= 0; i
< array
->len
; i
++) {
245 if(array
->pdata
[i
] != letters
[i
]) {
246 return FAILED("Array out of order, expected %s got %s at position %d",
247 letters
[i
], (gchar
*) array
->pdata
[i
], i
);
251 g_ptr_array_free(array
, TRUE
);
256 static gint
ptrarray_sort_compare_with_data (gconstpointer a
, gconstpointer b
, gpointer user_data
)
258 gchar
*stra
= *(gchar
**) a
;
259 gchar
*strb
= *(gchar
**) b
;
261 if (strcmp (user_data
, "this is the data for qsort") != 0)
262 fprintf (stderr
, "oops at compare with_data\n");
264 return strcmp(stra
, strb
);
267 RESULT
ptrarray_sort_with_data ()
269 GPtrArray
*array
= g_ptr_array_new();
271 gchar
*letters
[] = { "A", "B", "C", "D", "E" };
273 g_ptr_array_add(array
, letters
[4]);
274 g_ptr_array_add(array
, letters
[1]);
275 g_ptr_array_add(array
, letters
[2]);
276 g_ptr_array_add(array
, letters
[0]);
277 g_ptr_array_add(array
, letters
[3]);
279 g_ptr_array_sort_with_data(array
, ptrarray_sort_compare_with_data
, "this is the data for qsort");
281 for(i
= 0; i
< array
->len
; i
++) {
282 if(array
->pdata
[i
] != letters
[i
]) {
283 return FAILED("Array out of order, expected %s got %s at position %d",
284 letters
[i
], (gchar
*) array
->pdata
[i
], i
);
288 g_ptr_array_free(array
, TRUE
);
293 RESULT
ptrarray_remove_fast()
295 GPtrArray
*array
= g_ptr_array_new();
296 gchar
*letters
[] = { "A", "B", "C", "D", "E" };
298 if (g_ptr_array_remove_fast (array
, NULL
))
299 return FAILED ("Removing NULL succeeded");
301 g_ptr_array_add(array
, letters
[0]);
302 if (!g_ptr_array_remove_fast (array
, letters
[0]) || array
->len
!= 0)
303 return FAILED ("Removing last element failed");
305 g_ptr_array_add(array
, letters
[0]);
306 g_ptr_array_add(array
, letters
[1]);
307 g_ptr_array_add(array
, letters
[2]);
308 g_ptr_array_add(array
, letters
[3]);
309 g_ptr_array_add(array
, letters
[4]);
311 if (!g_ptr_array_remove_fast (array
, letters
[0]) || array
->len
!= 4)
312 return FAILED ("Removing first element failed");
314 if (array
->pdata
[0] != letters
[4])
315 return FAILED ("First element wasn't replaced with last upon removal");
317 if (g_ptr_array_remove_fast (array
, letters
[0]))
318 return FAILED ("Succedeed removing a non-existing element");
320 if (!g_ptr_array_remove_fast (array
, letters
[3]) || array
->len
!= 3)
321 return FAILED ("Failed removing \"D\"");
323 if (!g_ptr_array_remove_fast (array
, letters
[1]) || array
->len
!= 2)
324 return FAILED ("Failed removing \"B\"");
326 if (array
->pdata
[0] != letters
[4] || array
->pdata
[1] != letters
[2])
327 return FAILED ("Last two elements are wrong");
328 g_ptr_array_free(array
, TRUE
);
333 static Test ptrarray_tests
[] = {
334 {"alloc", ptrarray_alloc
},
335 {"for_iterate", ptrarray_for_iterate
},
336 {"foreach_iterate", ptrarray_foreach_iterate
},
337 {"set_size", ptrarray_set_size
},
338 {"remove_index", ptrarray_remove_index
},
339 {"remove_index_fast", ptrarray_remove_index_fast
},
340 {"remove", ptrarray_remove
},
341 {"sort", ptrarray_sort
},
342 {"remove_fast", ptrarray_remove_fast
},
343 {"sort_with_data", ptrarray_sort_with_data
},
347 DEFINE_TEST_GROUP_INIT(ptrarray_tests_init
, ptrarray_tests
)