[kdbus] Add SipHash algorithm
[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 static gboolean missing_locale = FALSE;
7
8 typedef struct {
9   const gchar **input;
10   const gchar **sorted;
11   const gchar **file_sorted;
12 } CollateTest;
13
14 typedef struct {
15   gchar *key;
16   const gchar *str;
17 } Line;
18
19 static void
20 clear_line (Line *line)
21 {
22   g_free (line->key);
23 }
24
25 static int
26 compare_collate (const void *a, const void *b)
27 {
28   const Line *line_a = a;
29   const Line *line_b = b;
30
31   return g_utf8_collate (line_a->str, line_b->str);
32 }
33
34 static int
35 compare_key (const void *a, const void *b)
36 {
37   const Line *line_a = a;
38   const Line *line_b = b;
39
40   return strcmp (line_a->key, line_b->key);
41 }
42
43 static void
44 do_collate (gboolean for_file, gboolean use_key, const CollateTest *test)
45 {
46   GArray *line_array;
47   Line line;
48   gint i;
49
50   if (missing_locale)
51     {
52       g_test_skip ("no en_US locale");
53       return;
54     }
55
56   line_array = g_array_new (FALSE, FALSE, sizeof(Line));
57   g_array_set_clear_func (line_array, (GDestroyNotify)clear_line);
58
59   for (i = 0; test->input[i]; i++)
60     {
61       line.str = test->input[i];
62       if (for_file)
63         line.key = g_utf8_collate_key_for_filename (line.str, -1);
64       else
65         line.key = g_utf8_collate_key (line.str, -1);
66
67       g_array_append_val (line_array, line);
68     }
69
70   qsort (line_array->data, line_array->len, sizeof (Line), use_key ? compare_key : compare_collate);
71
72   for (i = 0; test->input[i]; i++)
73     {
74       const gchar *str;
75       str = g_array_index (line_array, Line, i).str;
76       if (for_file)
77         g_assert_cmpstr (str, ==, test->file_sorted[i]);
78       else
79         g_assert_cmpstr (str, ==, test->sorted[i]);
80     }
81
82   g_array_free (line_array, TRUE);
83 }
84
85 static void
86 test_collate (gconstpointer d)
87 {
88   const CollateTest *test = d;
89   do_collate (FALSE, FALSE, test);
90 }
91
92 static void
93 test_collate_key (gconstpointer d)
94 {
95   const CollateTest *test = d;
96   do_collate (FALSE, TRUE, test);
97 }
98
99 static void
100 test_collate_file (gconstpointer d)
101 {
102   const CollateTest *test = d;
103   do_collate (TRUE, TRUE, test);
104 }
105
106 const gchar *input0[] = {
107   "z",
108   "c",
109   "eer34",
110   "223",
111   "er1",
112   "üĠണ",
113   "foo",
114   "bar",
115   "baz",
116   "GTK+",
117   NULL
118 };
119
120 const gchar *sorted0[] = {
121   "223",
122   "bar",
123   "baz",
124   "c",
125   "eer34",
126   "er1",
127   "foo",
128   "GTK+",
129   "üĠണ",
130   "z",
131   NULL
132 };
133
134 const gchar *file_sorted0[] = {
135   "223",
136   "bar",
137   "baz",
138   "c",
139   "eer34",
140   "er1",
141   "foo",
142   "GTK+",
143   "üĠണ",
144   "z",
145   NULL
146 };
147
148 const gchar *input1[] = {
149   "file.txt",
150   "file2.bla",
151   "file.c",
152   "file3.xx",
153   "bla001",
154   "bla02",
155   "bla03",
156   "bla4",
157   "bla10",
158   "bla100",
159   "event.c",
160   "eventgenerator.c",
161   "event.h",
162   NULL
163 };
164
165 const gchar *sorted1[] = {
166   "bla001",
167   "bla02",
168   "bla03",
169   "bla10",
170   "bla100",
171   "bla4",
172   "event.c",
173   "eventgenerator.c",
174   "event.h",
175   "file2.bla",
176   "file3.xx",
177   "file.c",
178   "file.txt",
179   NULL
180 };
181
182 const gchar *file_sorted1[] = {
183   "bla001",
184   "bla02",
185   "bla03",
186   "bla4",
187   "bla10",
188   "bla100",
189   "event.c",
190   "event.h",
191   "eventgenerator.c",
192   "file.c",
193   "file.txt",
194   "file2.bla",
195   "file3.xx",
196   NULL
197 };
198
199 const gchar *input2[] = {
200   "file26",
201   "file100",
202   "file1",
203   "file:foo",
204   "a.a",
205   "file027",
206   "file10",
207   "aa.a",
208   "file5",
209   "file0027",
210   "a-.a",
211   "file0000",
212   "file000x",
213   NULL
214 };
215
216 const gchar *sorted2[] = {
217   "a-.a",
218   "a.a",
219   "aa.a",
220   "file0000",
221   "file000x",
222   "file0027",
223   "file027",
224   "file1",
225   "file10",
226   "file100",
227   "file26",
228   "file5",
229   "file:foo",
230   NULL
231 };
232
233 const gchar *file_sorted2[] = {
234   "a.a",
235   "a-.a",
236   "aa.a",
237   "file0000",
238   "file000x",
239   "file1",
240   "file5",
241   "file10",
242   "file26",
243   "file027",
244   "file0027",
245   "file100",
246   "file:foo",
247   NULL
248 };
249
250 int
251 main (int argc, char *argv[])
252 {
253   gchar *path;
254   gint i;
255   const gchar *locale;
256   CollateTest test[3];
257
258   g_test_init (&argc, &argv, NULL);
259
260   g_setenv ("LC_ALL", "en_US", TRUE);
261   locale = setlocale (LC_ALL, "");
262   if (locale == NULL || strcmp (locale, "en_US") != 0)
263     {
264       g_test_message ("No suitable locale, skipping tests");
265       missing_locale = TRUE;
266       /* let the tests run to completion so they show up as SKIP'd in TAP
267        * output */
268     }
269
270   test[0].input = input0;
271   test[0].sorted = sorted0;
272   test[0].file_sorted = file_sorted0;
273   test[1].input = input1;
274   test[1].sorted = sorted1;
275   test[1].file_sorted = file_sorted1;
276   test[2].input = input2;
277   test[2].sorted = sorted2;
278   test[2].file_sorted = file_sorted2;
279
280   for (i = 0; i < G_N_ELEMENTS (test); i++)
281     {
282       path = g_strdup_printf ("/unicode/collate/%d", i);
283       g_test_add_data_func (path, &test[i], test_collate);
284       g_free (path);
285       path = g_strdup_printf ("/unicode/collate-key/%d", i);
286       g_test_add_data_func (path, &test[i], test_collate_key);
287       g_free (path);
288       path = g_strdup_printf ("/unicode/collate-filename/%d", i);
289       g_test_add_data_func (path, &test[i], test_collate_file);
290       g_free (path);
291     }
292
293   return g_test_run ();
294 }
295