In System.Windows.Forms:
[mono-project.git] / eglib / test / ptrarray.c
blob926dbee0f593c7fc6fc3599418356c12255aad41
1 #include <stdio.h>
2 #include <glib.h>
3 #include "test.h"
5 /* Redefine the private structure only to verify proper allocations */
6 typedef struct _GPtrArrayPriv {
7 gpointer *pdata;
8 guint len;
9 guint size;
10 } 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",
17 NULL
20 static GPtrArray *ptrarray_alloc_and_fill(gint *item_count)
22 GPtrArray *array = g_ptr_array_new();
23 gint i;
25 for(i = 0; items[i] != NULL; i++) {
26 g_ptr_array_add(array, (gpointer)items[i]);
29 if(item_count != NULL) {
30 *item_count = i;
33 return array;
36 static gint guess_size(gint length)
38 gint size = 1;
40 while(size < length) {
41 size <<= 1;
44 return size;
47 RESULT ptrarray_alloc()
49 GPtrArrayPriv *array;
50 gint i;
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);
59 if(array->len != i) {
60 return FAILED("Expected %d node(s) in the array", i);
63 g_ptr_array_free((GPtrArray *)array, TRUE);
65 return OK;
68 RESULT ptrarray_for_iterate()
70 GPtrArray *array = ptrarray_alloc_and_fill(NULL);
71 gint i;
73 for(i = 0; i < array->len; i++) {
74 char *item = (char *)g_ptr_array_index(array, i);
75 if(item != items[i]) {
76 return FAILED(
77 "Expected item at %d to be %s, but it was %s",
78 i, items[i], item);
82 g_ptr_array_free(array, TRUE);
84 return OK;
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) {
96 return;
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 gint 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);
145 return OK;
148 RESULT ptrarray_remove_index()
150 GPtrArray *array;
151 gint i;
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],
158 array->pdata[0]);
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);
170 return OK;
173 RESULT ptrarray_remove()
175 GPtrArray *array;
176 gint i;
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);
196 return OK;
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();
209 gint i;
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);
229 return OK;
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},
240 {NULL, NULL}
243 DEFINE_TEST_GROUP_INIT(ptrarray_tests_init, ptrarray_tests)