Merge remote-tracking branch 'gvdb/master'
[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 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 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   "foo",
94   "bar",
95   "baz",
96   "GTK+",
97   NULL
98 };
99
100 const gchar *sorted0[] = {
101   "223",
102   "bar",
103   "baz",
104   "c",
105   "eer34",
106   "er1",
107   "foo",
108   "GTK+",
109   "z",
110   NULL
111 };
112
113 const gchar *file_sorted0[] = {
114   "223",
115   "bar",
116   "baz",
117   "c",
118   "eer34",
119   "er1",
120   "foo",
121   "GTK+",
122   "z",
123   NULL
124 };
125
126 const gchar *input1[] = {
127   "file.txt",
128   "file2.bla",
129   "file.c",
130   "file3.xx",
131   "bla001",
132   "bla02",
133   "bla03",
134   "bla4",
135   "bla10",
136   "bla100",
137   "event.c",
138   "eventgenerator.c",
139   "event.h",
140   NULL
141 };
142
143 const gchar *sorted1[] = {
144   "bla001",
145   "bla02",
146   "bla03",
147   "bla10",
148   "bla100",
149   "bla4",
150   "event.c",
151   "eventgenerator.c",
152   "event.h",
153   "file2.bla",
154   "file3.xx",
155   "file.c",
156   "file.txt",
157   NULL
158 };
159
160 const gchar *file_sorted1[] = {
161   "bla001",
162   "bla02",
163   "bla03",
164   "bla4",
165   "bla10",
166   "bla100",
167   "event.c",
168   "event.h",
169   "eventgenerator.c",
170   "file.c",
171   "file.txt",
172   "file2.bla",
173   "file3.xx",
174   NULL
175 };
176
177 int
178 main (int argc, char *argv[])
179 {
180   gchar *path;
181   gint i;
182   const gchar *locale;
183   CollateTest test[2];
184
185   g_test_init (&argc, &argv, NULL);
186
187   g_setenv ("LC_ALL", "en_US", TRUE);
188   locale = setlocale (LC_ALL, "");
189   if (locale == NULL || strcmp (locale, "en_US") != 0)
190     {
191       g_test_message ("No suitable locale, skipping test");
192       return 0;
193     }
194
195   test[0].input = input0;
196   test[0].sorted = sorted0;
197   test[0].file_sorted = file_sorted0;
198   test[1].input = input1;
199   test[1].sorted = sorted1;
200   test[1].file_sorted = file_sorted1;
201
202   for (i = 0; i < G_N_ELEMENTS (test); i++)
203     {
204       path = g_strdup_printf ("/unicode/collate/%d", i);
205       g_test_add_data_func (path, &test[i], test_collate);
206       g_free (path);
207       path = g_strdup_printf ("/unicode/collate-key/%d", i);
208       g_test_add_data_func (path, test, test_collate_key);
209       g_free (path);
210       path = g_strdup_printf ("/unicode/collate-filename/%d", i);
211       g_test_add_data_func (path, test, test_collate_file);
212       g_free (path);
213     }
214
215   return g_test_run ();
216 }
217