glib/tests: fix leaks
[platform/upstream/glib.git] / glib / tests / collate.c
1 #include <glib.h>
2 #include <locale.h>
3 #include <stdlib.h>
4 #include <string.h>
5
6 typedef struct {
7   const gchar **input;
8   const gchar **sorted;
9   const gchar **file_sorted;
10 } CollateTest;
11
12 typedef struct {
13   gchar *key;
14   const gchar *str;
15 } Line;
16
17 static void
18 clear_line (Line *line)
19 {
20   g_free (line->key);
21 }
22
23 static int
24 compare_collate (const void *a, const void *b)
25 {
26   const Line *line_a = a;
27   const Line *line_b = b;
28
29   return g_utf8_collate (line_a->str, line_b->str);
30 }
31
32 static int
33 compare_key (const void *a, const void *b)
34 {
35   const Line *line_a = a;
36   const Line *line_b = b;
37
38   return strcmp (line_a->key, line_b->key);
39 }
40
41 static void
42 do_collate (gboolean for_file, gboolean use_key, const CollateTest *test)
43 {
44   GArray *line_array;
45   Line line;
46   gint i;
47
48   line_array = g_array_new (FALSE, FALSE, sizeof(Line));
49   g_array_set_clear_func (line_array, (GDestroyNotify)clear_line);
50
51   for (i = 0; test->input[i]; i++)
52     {
53       line.str = test->input[i];
54       if (for_file)
55         line.key = g_utf8_collate_key_for_filename (line.str, -1);
56       else
57         line.key = g_utf8_collate_key (line.str, -1);
58
59       g_array_append_val (line_array, line);
60     }
61
62   qsort (line_array->data, line_array->len, sizeof (Line), use_key ? compare_key : compare_collate);
63
64   for (i = 0; test->input[i]; i++)
65     {
66       const gchar *str;
67       str = g_array_index (line_array, Line, i).str;
68       if (for_file)
69         g_assert_cmpstr (str, ==, test->file_sorted[i]);
70       else
71         g_assert_cmpstr (str, ==, test->sorted[i]);
72     }
73
74   g_array_free (line_array, TRUE);
75 }
76
77 static void
78 test_collate (gconstpointer d)
79 {
80   const CollateTest *test = d;
81   do_collate (FALSE, FALSE, test);
82 }
83
84 static void
85 test_collate_key (gconstpointer d)
86 {
87   const CollateTest *test = d;
88   do_collate (FALSE, TRUE, test);
89 }
90
91 static void
92 test_collate_file (gconstpointer d)
93 {
94   const CollateTest *test = d;
95   do_collate (TRUE, TRUE, test);
96 }
97
98 const gchar *input0[] = {
99   "z",
100   "c",
101   "eer34",
102   "223",
103   "er1",
104   "üĠണ",
105   "foo",
106   "bar",
107   "baz",
108   "GTK+",
109   NULL
110 };
111
112 const gchar *sorted0[] = {
113   "223",
114   "bar",
115   "baz",
116   "c",
117   "eer34",
118   "er1",
119   "foo",
120   "GTK+",
121   "üĠണ",
122   "z",
123   NULL
124 };
125
126 const gchar *file_sorted0[] = {
127   "223",
128   "bar",
129   "baz",
130   "c",
131   "eer34",
132   "er1",
133   "foo",
134   "GTK+",
135   "üĠണ",
136   "z",
137   NULL
138 };
139
140 const gchar *input1[] = {
141   "file.txt",
142   "file2.bla",
143   "file.c",
144   "file3.xx",
145   "bla001",
146   "bla02",
147   "bla03",
148   "bla4",
149   "bla10",
150   "bla100",
151   "event.c",
152   "eventgenerator.c",
153   "event.h",
154   NULL
155 };
156
157 const gchar *sorted1[] = {
158   "bla001",
159   "bla02",
160   "bla03",
161   "bla10",
162   "bla100",
163   "bla4",
164   "event.c",
165   "eventgenerator.c",
166   "event.h",
167   "file2.bla",
168   "file3.xx",
169   "file.c",
170   "file.txt",
171   NULL
172 };
173
174 const gchar *file_sorted1[] = {
175   "bla001",
176   "bla02",
177   "bla03",
178   "bla4",
179   "bla10",
180   "bla100",
181   "event.c",
182   "event.h",
183   "eventgenerator.c",
184   "file.c",
185   "file.txt",
186   "file2.bla",
187   "file3.xx",
188   NULL
189 };
190
191 int
192 main (int argc, char *argv[])
193 {
194   gchar *path;
195   gint i;
196   const gchar *locale;
197   CollateTest test[2];
198
199   g_test_init (&argc, &argv, NULL);
200
201   g_setenv ("LC_ALL", "en_US", TRUE);
202   locale = setlocale (LC_ALL, "");
203   if (locale == NULL || strcmp (locale, "en_US") != 0)
204     {
205       g_test_message ("No suitable locale, skipping test");
206       return 0;
207     }
208
209   test[0].input = input0;
210   test[0].sorted = sorted0;
211   test[0].file_sorted = file_sorted0;
212   test[1].input = input1;
213   test[1].sorted = sorted1;
214   test[1].file_sorted = file_sorted1;
215
216   for (i = 0; i < G_N_ELEMENTS (test); i++)
217     {
218       path = g_strdup_printf ("/unicode/collate/%d", i);
219       g_test_add_data_func (path, &test[i], test_collate);
220       g_free (path);
221       path = g_strdup_printf ("/unicode/collate-key/%d", i);
222       g_test_add_data_func (path, test, test_collate_key);
223       g_free (path);
224       path = g_strdup_printf ("/unicode/collate-filename/%d", i);
225       g_test_add_data_func (path, test, test_collate_file);
226       g_free (path);
227     }
228
229   return g_test_run ();
230 }
231