2007-04-27 Jonathan Chambers <joncham@gmail.com>
[mono.git] / eglib / test / ptrarray.c
1 #include <stdio.h>
2 #include <glib.h>
3 #include "test.h"
4
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;
11
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
18 };
19
20 static GPtrArray *ptrarray_alloc_and_fill(guint *item_count)
21 {
22         GPtrArray *array = g_ptr_array_new();
23         gint i;
24         
25         for(i = 0; items[i] != NULL; i++) {
26                 g_ptr_array_add(array, (gpointer)items[i]);
27         }
28
29         if(item_count != NULL) {
30                 *item_count = i;
31         }
32         
33         return array;
34 }
35
36 static guint guess_size(guint length)
37 {
38         guint size = 1;
39
40         while(size < length) {
41                 size <<= 1;
42         }
43
44         return size;
45 }
46
47 RESULT ptrarray_alloc()
48 {
49         GPtrArrayPriv *array;
50         guint i;
51         
52         array = (GPtrArrayPriv *)ptrarray_alloc_and_fill(&i);
53         
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);
57         }
58         
59         if(array->len != i) {
60                 return FAILED("Expected %d node(s) in the array", i);
61         }
62         
63         g_ptr_array_free((GPtrArray *)array, TRUE);
64
65         return OK;
66 }
67
68 RESULT ptrarray_for_iterate()
69 {
70         GPtrArray *array = ptrarray_alloc_and_fill(NULL);
71         guint i;
72
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);
79                 }
80         }
81
82         g_ptr_array_free(array, TRUE);
83
84         return OK;
85 }
86
87 static gint foreach_iterate_index = 0;
88 static char *foreach_iterate_error = NULL;
89
90 void foreach_callback(gpointer data, gpointer user_data)
91 {
92         char *item = (char *)data;
93         const char *item_cmp = items[foreach_iterate_index++];
94
95         if(foreach_iterate_error != NULL) {
96                 return;
97         }
98
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);
103         }
104 }
105
106 RESULT ptrarray_foreach_iterate()
107 {
108         GPtrArray *array = ptrarray_alloc_and_fill(NULL);
109         
110         foreach_iterate_index = 0;
111         foreach_iterate_error = NULL;
112         
113         g_ptr_array_foreach(array, foreach_callback, array);
114         
115         g_ptr_array_free(array, TRUE);
116
117         return foreach_iterate_error;
118 }
119
120 RESULT ptrarray_set_size()
121 {
122         GPtrArray *array = g_ptr_array_new();
123         guint i, grow_length = 50;
124         
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);
128
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]);
135         }
136
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]);
140                 }
141         }
142
143         g_ptr_array_free(array, TRUE);
144
145         return OK;
146 }
147
148 RESULT ptrarray_remove_index()
149 {
150         GPtrArray *array;
151         guint i;
152         
153         array = ptrarray_alloc_and_fill(&i);
154         
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]);
159         }
160
161         g_ptr_array_remove_index(array, array->len - 1);
162         
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]);
166         }
167
168         g_ptr_array_free(array, TRUE);
169
170         return OK;
171 }
172
173 RESULT ptrarray_remove()
174 {
175         GPtrArray *array;
176         guint i;
177         
178         array = ptrarray_alloc_and_fill(&i);
179
180         g_ptr_array_remove(array, (gpointer)items[7]);
181
182         if(!g_ptr_array_remove(array, (gpointer)items[4])) {
183                 return FAILED("Item %s not removed", items[4]);
184         }
185
186         if(g_ptr_array_remove(array, (gpointer)items[4])) {
187                 return FAILED("Item %s still in array after removal", items[4]);
188         }
189
190         if(array->pdata[array->len - 1] != items[array->len + 1]) {
191                 return FAILED("Last item in GPtrArray not correct");
192         }
193
194         g_ptr_array_free(array, TRUE);
195
196         return OK;
197 }
198
199 static gint ptrarray_sort_compare(gconstpointer a, gconstpointer b)
200 {
201         gchar *stra = *(gchar **) a;
202         gchar *strb = *(gchar **) b;
203         return strcmp(stra, strb);
204 }
205
206 RESULT ptrarray_sort()
207 {
208         GPtrArray *array = g_ptr_array_new();
209         guint i;
210         gchar *letters [] = { "A", "B", "C", "D", "E" };
211         
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]);
217         
218         g_ptr_array_sort(array, ptrarray_sort_compare);
219
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]);
224                 }
225         }
226
227         g_ptr_array_free(array, TRUE);
228         
229         return OK;
230 }
231
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}
241 };
242
243 DEFINE_TEST_GROUP_INIT(ptrarray_tests_init, ptrarray_tests)
244
245