Add some more cases to the app-id unit tests
[glib.git] / glib / tests / collate.c
blob24a913d24789969f523f011d198b8cc799afe51f
1 #include <glib.h>
2 #include <locale.h>
3 #include <stdlib.h>
4 #include <string.h>
6 static gboolean missing_locale = FALSE;
8 typedef struct {
9 const gchar **input;
10 const gchar **sorted;
11 const gchar **file_sorted;
12 } CollateTest;
14 typedef struct {
15 gchar *key;
16 const gchar *str;
17 } Line;
19 static void
20 clear_line (Line *line)
22 g_free (line->key);
25 static int
26 compare_collate (const void *a, const void *b)
28 const Line *line_a = a;
29 const Line *line_b = b;
31 return g_utf8_collate (line_a->str, line_b->str);
34 static int
35 compare_key (const void *a, const void *b)
37 const Line *line_a = a;
38 const Line *line_b = b;
40 return strcmp (line_a->key, line_b->key);
43 static void
44 do_collate (gboolean for_file, gboolean use_key, const CollateTest *test)
46 GArray *line_array;
47 Line line;
48 gint i;
50 if (missing_locale)
52 g_test_skip ("no en_US locale");
53 return;
56 line_array = g_array_new (FALSE, FALSE, sizeof(Line));
57 g_array_set_clear_func (line_array, (GDestroyNotify)clear_line);
59 for (i = 0; test->input[i]; i++)
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);
67 g_array_append_val (line_array, line);
70 qsort (line_array->data, line_array->len, sizeof (Line), use_key ? compare_key : compare_collate);
72 for (i = 0; test->input[i]; i++)
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]);
82 g_array_free (line_array, TRUE);
85 static void
86 test_collate (gconstpointer d)
88 const CollateTest *test = d;
89 do_collate (FALSE, FALSE, test);
92 static void
93 test_collate_key (gconstpointer d)
95 const CollateTest *test = d;
96 do_collate (FALSE, TRUE, test);
99 static void
100 test_collate_file (gconstpointer d)
102 const CollateTest *test = d;
103 do_collate (TRUE, TRUE, test);
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
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
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
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
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
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
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
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
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
251 main (int argc, char *argv[])
253 gchar *path;
254 gint i;
255 const gchar *locale;
256 CollateTest test[3];
258 g_test_init (&argc, &argv, NULL);
260 g_setenv ("LC_ALL", "en_US", TRUE);
261 locale = setlocale (LC_ALL, "");
262 if (locale == NULL || strcmp (locale, "en_US") != 0)
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 */
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;
280 for (i = 0; i < G_N_ELEMENTS (test); i++)
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);
293 return g_test_run ();