2006-08-18 Aaron Bockover <abockover@novell.com>
[mono.git] / eglib / test / list.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <glib.h>
4 #include "test.h"
5
6 RESULT
7 test_list_length ()
8 {
9         GList *list = g_list_prepend (NULL, "foo");
10
11         if (g_list_length (list) != 1)
12                 return "length failed. #1";
13
14         list = g_list_prepend (list, "bar");
15         if (g_list_length (list) != 2)
16                 return "length failed. #2";
17
18         list = g_list_append (list, "bar");
19         if (g_list_length (list) != 3)
20                 return "length failed. #3";
21         
22         return NULL;
23 }
24
25 RESULT
26 test_list_nth ()
27 {
28         char *foo = "foo";
29         char *bar = "bar";
30         char *baz = "baz";
31         GList *nth, *list;
32         list = g_list_prepend (NULL, baz);
33         list = g_list_prepend (list, bar);
34         list = g_list_prepend (list, foo);
35
36         nth = g_list_nth (list, 0);
37         if (nth->data != foo)
38                 return "nth failed. #1";
39
40         nth = g_list_nth (list, 1);
41         if (nth->data != bar)
42                 return "nth failed. #2";
43         
44         nth = g_list_nth (list, 2);
45         if (nth->data != baz)
46                 return "nth failed. #3";
47
48         return OK;
49 }
50
51 RESULT
52 test_list_index ()
53 {
54         int i;
55         char *foo = "foo";
56         char *bar = "bar";
57         char *baz = "baz";
58         GList *list;
59         list = g_list_prepend (NULL, baz);
60         list = g_list_prepend (list, bar);
61         list = g_list_prepend (list, foo);
62
63         i = g_list_index (list, foo);
64         if (i != 0)
65                 return "index failed. #1";
66
67         i = g_list_index (list, bar);
68         if (i != 1)
69                 return "index failed. #2";
70         
71         i = g_list_index (list, baz);
72         if (i != 2)
73                 return "index failed. #3";
74
75         return OK;
76 }
77
78 RESULT
79 test_list_append ()
80 {
81         GList *list = g_list_prepend (NULL, "first");
82         if (g_list_length (list) != 1)
83                 return "Prepend failed";
84
85         g_list_append (list, g_list_prepend (NULL, "second"));
86
87         if (g_list_length (list) != 2)
88                 return "Append failed";
89
90         return OK;
91 }
92
93 RESULT
94 test_list_last ()
95 {
96         GList *foo = g_list_prepend (NULL, "foo");
97         GList *bar = g_list_prepend (NULL, "bar");
98         GList *last;
99         
100         foo = g_list_concat (foo, bar);
101         last = g_list_last (foo);
102
103         if (last != bar)
104                 return "last failed. #1";
105
106         foo = g_list_concat (foo, g_list_prepend (NULL, "baz"));
107         foo = g_list_concat (foo, g_list_prepend (NULL, "quux"));
108
109         last = g_list_last (foo);       
110         if (strcmp ("quux", last->data))
111                 return "last failed. #2";
112
113         return OK;
114 }
115
116 RESULT
117 test_list_concat ()
118 {
119         GList *foo = g_list_prepend (NULL, "foo");
120         GList *bar = g_list_prepend (NULL, "bar");
121         GList *list = g_list_concat (foo, bar);
122
123         if (g_list_length (list) != 2)
124                 return "Concat failed. #1";
125
126         if (strcmp (list->data, "foo"))
127                 return "Concat failed. #2";
128
129         if (strcmp (list->next->data, "bar"))
130                 return "Concat failed. #3";
131
132         if (g_list_first (list) != foo)
133                 return "Concat failed. #4";
134         
135         if (g_list_last (list) != bar)
136                 return "Concat failed. #5";
137
138         return OK;
139 }
140
141
142 static gint
143 compare (gconstpointer a, gconstpointer b)
144 {
145         char *foo = (char *) a;
146         char *bar = (char *) b;
147
148         if (strlen (foo) < strlen (bar))
149                 return -1;
150
151         return 1;
152 }
153
154 RESULT
155 test_list_insert_sorted ()
156 {
157         GList *list = g_list_prepend (NULL, "a");
158         list = g_list_append (list, "aaa");
159
160         /* insert at the middle */
161         list = g_list_insert_sorted (list, "aa", compare);
162         if (strcmp ("aa", list->next->data))
163                 return FAILED ("insert_sorted failed. #1");
164
165         /* insert at the beginning */
166         list = g_list_insert_sorted (list, "", compare);
167         
168         if (strcmp ("", list->data))
169                 return FAILED ("insert_sorted failed. #2");             
170
171         /* insert at the end */
172         list = g_list_insert_sorted (list, "aaaa", compare);
173         if (strcmp ("aaaa", g_list_last (list)->data))
174                 return FAILED ("insert_sorted failed. #3");
175
176         return OK;
177 }
178
179 RESULT
180 test_list_copy ()
181 {
182         int i, length;
183         GList *list, *copy;
184         list = g_list_prepend (NULL, "a");
185         list = g_list_append  (list, "aa");
186         list = g_list_append  (list, "aaa");
187         list = g_list_append  (list, "aaaa");
188
189         length = g_list_length (list);
190         copy = g_list_copy (list);
191
192         for (i = 0; i < length; i++)
193                 if (strcmp (g_list_nth (list, i)->data,
194                             g_list_nth (copy, i)->data))
195                         return "copy failed.";
196         return OK;
197 }
198
199 RESULT
200 test_list_reverse ()
201 {
202         guint i, length;
203         GList *list, *reverse;
204         list = g_list_prepend (NULL, "a");
205         list = g_list_append  (list, "aa");
206         list = g_list_append  (list, "aaa");
207         list = g_list_append  (list, "aaaa");
208
209         length  = g_list_length (list);
210         reverse = g_list_reverse (g_list_copy (list));
211
212         if (g_list_length (reverse) != length)
213                 return "reverse failed #1";
214
215         for (i = 0; i < length; i++){
216                 guint j = length - i - 1;
217                 if (strcmp (g_list_nth (list, i)->data,
218                             g_list_nth (reverse, j)->data))
219                         return "reverse failed. #2";
220         }
221         return OK;
222 }
223
224 static Test list_tests [] = {
225         {"list_length", test_list_length},
226         {"list_nth", test_list_nth},
227         {"list_index", test_list_index},        
228         {"list_last", test_list_last},  
229         {"list_append", test_list_append},
230         {"list_concat", test_list_concat},
231         {"list_insert_sorted", test_list_insert_sorted},
232         {"list_copy", test_list_copy},
233         {"list_reverse", test_list_reverse},
234         {NULL, NULL}
235 };
236
237 DEFINE_TEST_GROUP_INIT(list_tests_init, list_tests)