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