2006-08-21 Aaron Bockover <abockover@novell.com>
[mono.git] / 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_remove ()
88 {
89         GArray *array = g_array_new (FALSE, FALSE, sizeof (int));
90         int v[] = {30, 29, 28, 27, 26, 25};
91
92         g_array_append_vals (array, v, 6);
93
94         if (6 != array->len)
95                 return FAILED ("append_vals fail");
96
97         g_array_remove_index (array, 3);
98
99         if (5 != array->len)
100                 return FAILED ("remove_index failed to update length");
101
102         if (26 != g_array_index (array, int, 3))
103                 return FAILED ("remove_index failed to update the array");
104
105         g_array_free (array, TRUE);
106
107         return NULL;
108 }
109
110 static Test array_tests [] = {
111         {"big", test_array_big},
112         {"append", test_array_append},
113         {"index", test_array_index},
114         {"remove", test_array_remove},
115         {"append_zero_term", test_array_append_zero_terminated},
116         {NULL, NULL}
117 };
118
119 DEFINE_TEST_GROUP_INIT(array_tests_init, array_tests)