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()
178 array = ptrarray_alloc_and_fill(&i);
180 g_ptr_array_remove(array, (gpointer)items[7]);
182 if(!g_ptr_array_remove(array, (gpointer)items[4])) {
183 return FAILED("Item %s not removed", items[4]);
186 if(g_ptr_array_remove(array, (gpointer)items[4])) {
187 return FAILED("Item %s still in array after removal", items[4]);
190 if(array->pdata[array->len - 1] != items[array->len + 1]) {
191 return FAILED("Last item in GPtrArray not correct");
194 g_ptr_array_free(array, TRUE);
199 static gint ptrarray_sort_compare(gconstpointer a, gconstpointer b)
201 gchar *stra = *(gchar **) a;
202 gchar *strb = *(gchar **) b;
203 return strcmp(stra, strb);
206 RESULT ptrarray_sort()
208 GPtrArray *array = g_ptr_array_new();
210 gchar *letters [] = { "A", "B", "C", "D", "E" };
212 g_ptr_array_add(array, letters[0]);
213 g_ptr_array_add(array, letters[1]);
214 g_ptr_array_add(array, letters[2]);
215 g_ptr_array_add(array, letters[3]);
216 g_ptr_array_add(array, letters[4]);
218 g_ptr_array_sort(array, ptrarray_sort_compare);
220 for(i = 0; i < array->len; i++) {
221 if(array->pdata[i] != letters[i]) {
222 return FAILED("Array out of order, expected %s got %s",
223 (gchar *)array->pdata[i], letters[i]);
227 g_ptr_array_free(array, TRUE);
232 static Test ptrarray_tests [] = {
233 {"alloc", ptrarray_alloc},
234 {"for_iterate", ptrarray_for_iterate},
235 {"foreach_iterate", ptrarray_foreach_iterate},
236 {"set_size", ptrarray_set_size},
237 {"remove_index", ptrarray_remove_index},
238 {"remove", ptrarray_remove},
239 {"sort", ptrarray_sort},
243 DEFINE_TEST_GROUP_INIT(ptrarray_tests_init, ptrarray_tests)