[runtime] Move eglib into mono/eglib so it becomes a convenience library similar...
[mono.git] / mono / eglib / test / array.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <glib.h>
4 #include "test.h"
5
6 /* example from glib documentation */
7 RESULT
8 test_array_big ()
9 {
10         GArray *garray;
11         gint i;
12
13         /* We create a new array to store gint values.
14            We don't want it zero-terminated or cleared to 0's. */
15         garray = g_array_new (FALSE, FALSE, sizeof (gint));
16         for (i = 0; i < 10000; i++)
17                 g_array_append_val (garray, i);
18
19         for (i = 0; i < 10000; i++)
20                 if (g_array_index (garray, gint, i) != i)
21                         return FAILED ("array value didn't match");
22         
23         g_array_free (garray, TRUE);
24
25         return NULL;
26 }
27
28 RESULT
29 test_array_index ()
30 {
31         GArray *array = g_array_new (FALSE, FALSE, sizeof (int));
32         int v;
33
34         v = 27;
35         g_array_append_val (array, v);
36
37         if (27 != g_array_index (array, int, 0))
38                 return FAILED ("");
39
40         g_array_free (array, TRUE);
41
42         return NULL;
43 }
44
45 RESULT
46 test_array_append_zero_terminated ()
47 {
48         GArray *array = g_array_new (TRUE, FALSE, sizeof (int));
49         int v;
50
51         v = 27;
52         g_array_append_val (array, v);
53
54         if (27 != g_array_index (array, int, 0))
55                 return FAILED ("g_array_append_val failed");
56
57         if (0 != g_array_index (array, int, 1))
58                 return FAILED ("zero_terminated didn't append a zero element");
59
60         g_array_free (array, TRUE);
61
62         return NULL;
63 }
64
65 RESULT
66 test_array_append ()
67 {
68         GArray *array = g_array_new (FALSE, FALSE, sizeof (int));
69         int v;
70
71         if (0 != array->len)
72                 return FAILED ("initial array length not zero");
73
74         v = 27;
75
76         g_array_append_val (array, v);
77
78         if (1 != array->len)
79                 return FAILED ("array append failed");
80
81         g_array_free (array, TRUE);
82
83         return NULL;
84 }
85
86 RESULT
87 test_array_insert_val ()
88 {
89         GArray *array = g_array_new (FALSE, FALSE, sizeof (gpointer));
90         gpointer ptr0, ptr1, ptr2, ptr3;
91
92         g_array_insert_val (array, 0, array);
93
94         if (array != g_array_index (array, gpointer, 0))
95                 return FAILED ("1 The value in the array is incorrect");
96
97         g_array_insert_val (array, 1, array);
98         if (array != g_array_index (array, gpointer, 1))
99                 return FAILED ("2 The value in the array is incorrect");
100
101         g_array_insert_val (array, 2, array);
102         if (array != g_array_index (array, gpointer, 2))
103                 return FAILED ("3 The value in the array is incorrect");
104         
105         g_array_free (array, TRUE);
106         array = g_array_new (FALSE, FALSE, sizeof (gpointer));
107         ptr0 = array;
108         ptr1 = array + 1;
109         ptr2 = array + 2;
110         ptr3 = array + 3;
111
112         g_array_insert_val (array, 0, ptr0);
113         g_array_insert_val (array, 1, ptr1);
114         g_array_insert_val (array, 2, ptr2);
115         g_array_insert_val (array, 1, ptr3);
116         if (ptr0 != g_array_index (array, gpointer, 0))
117                 return FAILED ("4 The value in the array is incorrect");
118         if (ptr3 != g_array_index (array, gpointer, 1))
119                 return FAILED ("5 The value in the array is incorrect");
120         if (ptr1 != g_array_index (array, gpointer, 2))
121                 return FAILED ("6 The value in the array is incorrect");
122         if (ptr2 != g_array_index (array, gpointer, 3))
123                 return FAILED ("7 The value in the array is incorrect");
124
125         g_array_free (array, TRUE);
126         return NULL;
127 }
128
129 RESULT
130 test_array_remove ()
131 {
132         GArray *array = g_array_new (FALSE, FALSE, sizeof (int));
133         int v[] = {30, 29, 28, 27, 26, 25};
134
135         g_array_append_vals (array, v, 6);
136
137         if (6 != array->len)
138                 return FAILED ("append_vals fail");
139
140         g_array_remove_index (array, 3);
141
142         if (5 != array->len)
143                 return FAILED ("remove_index failed to update length");
144
145         if (26 != g_array_index (array, int, 3))
146                 return FAILED ("remove_index failed to update the array");
147
148         g_array_free (array, TRUE);
149
150         return NULL;
151 }
152
153 static Test array_tests [] = {
154         {"big", test_array_big},
155         {"append", test_array_append},
156         {"insert_val", test_array_insert_val},
157         {"index", test_array_index},
158         {"remove", test_array_remove},
159         {"append_zero_term", test_array_append_zero_terminated},
160         {NULL, NULL}
161 };
162
163 DEFINE_TEST_GROUP_INIT(array_tests_init, array_tests)