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