778eb1f9a4b1989e5d6edfb22cebd5674d900cc0
[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(gint *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 gint guess_size(gint length)
37 {
38         gint 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         gint 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         gint 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         gint 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         gint 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         return OK;
169 }
170
171 RESULT ptrarray_remove()
172 {
173         GPtrArray *array;
174         gint i;
175         
176         array = ptrarray_alloc_and_fill(&i);
177
178         g_ptr_array_remove(array, (gpointer)items[7]);
179
180         if(!g_ptr_array_remove(array, (gpointer)items[4])) {
181                 return FAILED("Item %s not removed", items[4]);
182         }
183
184         if(g_ptr_array_remove(array, (gpointer)items[4])) {
185                 return FAILED("Item %s still in array after removal", items[4]);
186         }
187
188         if(array->pdata[array->len - 1] != items[array->len + 1]) {
189                 return FAILED("Last item in GPtrArray not correct");
190         }
191
192         return OK;
193 }
194
195 static gint ptrarray_sort_compare(gconstpointer a, gconstpointer b)
196 {
197         return strcmp(a, b);
198 }
199
200 RESULT ptrarray_sort()
201 {
202         GPtrArray *array = g_ptr_array_new();
203         gint i;
204         static const gchar *letters [] = { "A", "B", "C", "D", "E" };
205         
206         g_ptr_array_add(array, "E");
207         g_ptr_array_add(array, "C");
208         g_ptr_array_add(array, "A");
209         g_ptr_array_add(array, "D");
210         g_ptr_array_add(array, "B");
211
212         g_ptr_array_sort(array, ptrarray_sort_compare);
213
214         for(i = 0; i < array->len; i++) {
215                 if(strcmp((gchar *)array->pdata[i], letters[i]) != 0) {
216                         return FAILED("Array out of order, expected %s got %s", 
217                                 (gchar *)array->pdata[i], letters[i]);
218                 }
219         }
220
221         g_ptr_array_free(array, TRUE);
222         
223         return OK;
224 }
225
226 static Test ptrarray_tests [] = {
227         {"ptrarray_alloc", ptrarray_alloc},
228         {"ptrarray_for_iterate", ptrarray_for_iterate},
229         {"ptrarray_foreach_iterate", ptrarray_foreach_iterate},
230         {"ptrarray_set_size", ptrarray_set_size},
231         {"ptrarray_remove_index", ptrarray_remove_index},
232         {"ptrarray_remove", ptrarray_remove},
233         {"ptrarray_sort", ptrarray_sort},
234         {NULL, NULL}
235 };
236
237 DEFINE_TEST_GROUP_INIT(ptrarray_tests_init, ptrarray_tests)
238