summaryrefslogtreecommitdiff
path: root/glib/tests/collate.c
blob: bd9ea234f759445e224483480d098d5ecbe23ee6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#include "config.h"

#include <glib.h>
#include <locale.h>
#include <stdlib.h>
#include <string.h>

static gboolean missing_locale = FALSE;

typedef struct {
  const gchar **input;
  const gchar **sorted;
  const gchar **file_sorted;
} CollateTest;

typedef struct {
  gchar *key;
  const gchar *str;
} Line;

static void
clear_line (Line *line)
{
  g_free (line->key);
}

static int
compare_collate (const void *a, const void *b)
{
  const Line *line_a = a;
  const Line *line_b = b;

  return g_utf8_collate (line_a->str, line_b->str);
}

static int
compare_key (const void *a, const void *b)
{
  const Line *line_a = a;
  const Line *line_b = b;

  return strcmp (line_a->key, line_b->key);
}

static void
do_collate (gboolean for_file, gboolean use_key, const CollateTest *test)
{
  GArray *line_array;
  Line line;
  gint i;

  if (missing_locale)
    {
      g_test_skip ("no en_US locale");
      return;
    }

  line_array = g_array_new (FALSE, FALSE, sizeof(Line));
  g_array_set_clear_func (line_array, (GDestroyNotify)clear_line);

  for (i = 0; test->input[i]; i++)
    {
      line.str = test->input[i];
      if (for_file)
        line.key = g_utf8_collate_key_for_filename (line.str, -1);
      else
        line.key = g_utf8_collate_key (line.str, -1);

      g_array_append_val (line_array, line);
    }

  qsort (line_array->data, line_array->len, sizeof (Line), use_key ? compare_key : compare_collate);

  for (i = 0; test->input[i]; i++)
    {
      const gchar *str;
      str = g_array_index (line_array, Line, i).str;
      if (for_file)
        g_assert_cmpstr (str, ==, test->file_sorted[i]);
      else
        g_assert_cmpstr (str, ==, test->sorted[i]);
    }

  g_array_free (line_array, TRUE);
}

static void
test_collate (gconstpointer d)
{
  const CollateTest *test = d;
  do_collate (FALSE, FALSE, test);
}

static void
test_collate_key (gconstpointer d)
{
  const CollateTest *test = d;
  do_collate (FALSE, TRUE, test);
}

static void
test_collate_file (gconstpointer d)
{
  const CollateTest *test = d;
  do_collate (TRUE, TRUE, test);
}

const gchar *input0[] = {
  "z",
  "c",
  "eer34",
  "223",
  "er1",
  "üĠണ",
  "foo",
  "bar",
  "baz",
  "GTK+",
  NULL
};

const gchar *sorted0[] = {
  "223",
  "bar",
  "baz",
  "c",
  "eer34",
  "er1",
  "foo",
  "GTK+",
  "üĠണ",
  "z",
  NULL
};

const gchar *file_sorted0[] = {
  "223",
  "bar",
  "baz",
  "c",
  "eer34",
  "er1",
  "foo",
  "GTK+",
  "üĠണ",
  "z",
  NULL
};

const gchar *input1[] = {
  "file.txt",
  "file2.bla",
  "file.c",
  "file3.xx",
  "bla001",
  "bla02",
  "bla03",
  "bla4",
  "bla10",
  "bla100",
  "event.c",
  "eventgenerator.c",
  "event.h",
  NULL
};

const gchar *sorted1[] = {
  "bla001",
  "bla02",
  "bla03",
  "bla10",
  "bla100",
  "bla4",
  "event.c",
  "eventgenerator.c",
  "event.h",
  "file2.bla",
  "file3.xx",
  "file.c",
  "file.txt",
  NULL
};

const gchar *file_sorted1[] = {
  "bla001",
  "bla02",
  "bla03",
  "bla4",
  "bla10",
  "bla100",
  "event.c",
  "event.h",
  "eventgenerator.c",
  "file.c",
  "file.txt",
  "file2.bla",
  "file3.xx",
  NULL
};

const gchar *input2[] = {
  "file26",
  "file100",
  "file1",
  "file:foo",
  "a.a",
  "file027",
  "file10",
  "aa.a",
  "file5",
  "file0027",
  "a-.a",
  "file0000",
  "file000x",
  NULL
};

const gchar *sorted2[] = {
  "a-.a",
  "a.a",
  "aa.a",
  "file0000",
  "file000x",
  "file0027",
  "file027",
  "file1",
  "file10",
  "file100",
  "file26",
  "file5",
  "file:foo",
  NULL
};

const gchar *file_sorted2[] = {
  /* Filename collation in OS X follows Finder style which gives
   * a slightly different order from usual Linux locales. */
#ifdef HAVE_CARBON
  "a-.a",
  "a.a",
  "aa.a",
  "file:foo",
  "file0000",
  "file000x",
  "file1",
  "file5",
  "file10",
  "file26",
  "file0027",
  "file027",
  "file100",
#else
  "a.a",
  "a-.a",
  "aa.a",
  "file0000",
  "file000x",
  "file1",
  "file5",
  "file10",
  "file26",
  "file027",
  "file0027",
  "file100",
  "file:foo",
#endif
  NULL
};

int
main (int argc, char *argv[])
{
  gchar *path;
  guint i;
  const gchar *locale;
  CollateTest test[3];

  g_test_init (&argc, &argv, NULL);

  g_setenv ("LC_ALL", "en_US", TRUE);
  locale = setlocale (LC_ALL, "");
  if (locale == NULL || strcmp (locale, "en_US") != 0)
    {
      g_test_message ("No suitable locale, skipping tests");
      missing_locale = TRUE;
      /* let the tests run to completion so they show up as SKIP'd in TAP
       * output */
    }

  test[0].input = input0;
  test[0].sorted = sorted0;
  test[0].file_sorted = file_sorted0;
  test[1].input = input1;
  test[1].sorted = sorted1;
  test[1].file_sorted = file_sorted1;
  test[2].input = input2;
  test[2].sorted = sorted2;
  test[2].file_sorted = file_sorted2;

  for (i = 0; i < G_N_ELEMENTS (test); i++)
    {
      path = g_strdup_printf ("/unicode/collate/%d", i);
      g_test_add_data_func (path, &test[i], test_collate);
      g_free (path);
      path = g_strdup_printf ("/unicode/collate-key/%d", i);
      g_test_add_data_func (path, &test[i], test_collate_key);
      g_free (path);
      path = g_strdup_printf ("/unicode/collate-filename/%d", i);
      g_test_add_data_func (path, &test[i], test_collate_file);
      g_free (path);
    }

  return g_test_run ();
}