6e274fc69d8391bb73b6d5dc086b23fb09765329
[platform/upstream/glib2.0.git] / tests / list-test.c
1 #undef G_DISABLE_ASSERT
2 #undef G_LOG_DOMAIN
3
4 #include <glib.h>
5
6 #define DEBUG_MSG(args)
7 /* #define DEBUG_MSG(args) g_printerr args ; g_printerr ("\n");  */
8 #define PRINT_MSG(args)
9 /* #define PRINT_MSG(args) g_print args ; g_print ("\n"); */
10
11 #define SIZE       50
12 #define NUMBER_MIN 0000
13 #define NUMBER_MAX 9999
14
15
16 static guint32 array[SIZE];
17
18
19 static gint
20 sort (gconstpointer p1, gconstpointer p2)
21 {
22   gint32 a, b;
23
24   a = GPOINTER_TO_INT (p1);
25   b = GPOINTER_TO_INT (p2);
26
27   return (a > b ? +1 : a == b ? 0 : -1);
28 }
29
30 /*
31  * glist sort tests
32  */
33 static void
34 test_list_sort (void)
35 {
36   GList *list = NULL;
37   gint   i;
38
39   PRINT_MSG (("testing g_list_sort()"));
40
41   for (i = 0; i < SIZE; i++) {
42     list = g_list_append (list, GINT_TO_POINTER (array[i]));
43   }
44
45   list = g_list_sort (list, sort);
46   for (i = 0; i < SIZE - 1; i++) {
47     gpointer p1, p2;
48
49     p1 = g_list_nth_data (list, i);
50     p2 = g_list_nth_data (list, i+1);
51
52     g_assert (GPOINTER_TO_INT (p1) <= GPOINTER_TO_INT (p2));
53     DEBUG_MSG (("list_sort #%3.3d ---> %d", i, GPOINTER_TO_INT (p1)));
54   }
55
56   g_list_free (list);
57 }
58
59 static void
60 test_list_sort_with_data (void)
61 {
62   GList *list = NULL;
63   gint   i;
64
65   PRINT_MSG (("testing g_list_sort_with_data()"));
66
67   for (i = 0; i < SIZE; i++) {
68     list = g_list_append (list, GINT_TO_POINTER (array[i]));
69   }
70
71   list = g_list_sort_with_data (list, (GCompareDataFunc)sort, NULL);
72   for (i = 0; i < SIZE - 1; i++) {
73     gpointer p1, p2;
74
75     p1 = g_list_nth_data (list, i);
76     p2 = g_list_nth_data (list, i+1);
77
78     g_assert (GPOINTER_TO_INT (p1) <= GPOINTER_TO_INT (p2));
79     DEBUG_MSG (("list_sort_with_data #%3.3d ---> %d", i, GPOINTER_TO_INT (p1)));
80   }
81
82   g_list_free (list);
83 }
84
85 static void
86 test_list_insert_sorted (void)
87 {
88   GList *list = NULL;
89   gint   i;
90
91   PRINT_MSG (("testing g_list_insert_sorted()"));
92
93   for (i = 0; i < SIZE; i++) {
94     list = g_list_insert_sorted (list, GINT_TO_POINTER (array[i]), sort);
95   }
96
97   for (i = 0; i < SIZE - 1; i++) {
98     gpointer p1, p2;
99
100     p1 = g_list_nth_data (list, i);
101     p2 = g_list_nth_data (list, i+1);
102
103     g_assert (GPOINTER_TO_INT (p1) <= GPOINTER_TO_INT (p2));
104     DEBUG_MSG (("list_insert_sorted #%3.3d ---> %d", i, GPOINTER_TO_INT (p1)));
105   }
106
107   g_list_free (list);
108 }
109
110 static void
111 test_list_insert_sorted_with_data (void)
112 {
113   GList *list = NULL;
114   gint   i;
115
116   PRINT_MSG (("testing g_list_insert_sorted_with_data()"));
117
118   for (i = 0; i < SIZE; i++) {
119     list = g_list_insert_sorted_with_data (list, 
120                                            GINT_TO_POINTER (array[i]), 
121                                            (GCompareDataFunc)sort, 
122                                            NULL);
123   }
124
125   for (i = 0; i < SIZE - 1; i++) {
126     gpointer p1, p2;
127
128     p1 = g_list_nth_data (list, i);
129     p2 = g_list_nth_data (list, i+1);
130
131     g_assert (GPOINTER_TO_INT (p1) <= GPOINTER_TO_INT (p2));
132     DEBUG_MSG (("list_insert_sorted_with_data #%3.3d ---> %d", i, GPOINTER_TO_INT (p1)));
133   }
134
135   g_list_free (list);
136 }
137
138 static void
139 test_list_reverse (void)
140 {
141   GList *list = NULL;
142   GList *st;
143   gint   nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
144   gint   i;
145
146   PRINT_MSG (("testing g_list_reverse()"));
147
148   for (i = 0; i < 10; i++) {
149     list = g_list_append (list, &nums[i]);
150   }
151
152   list = g_list_reverse (list);
153
154   for (i = 0; i < 10; i++) {
155     st = g_list_nth (list, i);
156     g_assert (*((gint*) st->data) == (9 - i));
157   }
158
159   g_list_free (list);
160 }
161
162 static void
163 test_list_nth (void)
164 {
165   GList *list = NULL;
166   GList *st;
167   gint   nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
168   gint   i;
169
170   PRINT_MSG (("testing g_list_nth()"));
171
172   for (i = 0; i < 10; i++) {
173     list = g_list_append (list, &nums[i]);
174   }
175
176   for (i = 0; i < 10; i++) {
177     st = g_list_nth (list, i);
178     g_assert (*((gint*) st->data) == i);
179   }
180
181   g_list_free (list);
182 }
183
184 int
185 main (int argc, char *argv[])
186 {
187   gint i;
188
189   DEBUG_MSG (("debugging messages turned on"));
190
191   DEBUG_MSG (("creating %d random numbers", SIZE));
192
193   /* Create an array of random numbers. */
194   for (i = 0; i < SIZE; i++) {
195     array[i] = g_random_int_range (NUMBER_MIN, NUMBER_MAX);
196     DEBUG_MSG (("number #%3.3d ---> %d", i, array[i]));
197   }
198
199   /* Start tests. */
200   test_list_sort ();
201   test_list_sort_with_data ();
202
203   test_list_insert_sorted ();
204   test_list_insert_sorted_with_data ();
205
206   test_list_reverse ();
207   test_list_nth ();
208
209   PRINT_MSG (("testing finished"));
210
211   return 0;
212 }