Imported Upstream version 2.66.1
[platform/upstream/glib.git] / glib / tests / collate.c
index eaae049..bd9ea23 100644 (file)
@@ -1,8 +1,12 @@
+#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;
@@ -10,11 +14,17 @@ typedef struct {
 } CollateTest;
 
 typedef struct {
-  const gchar *key;
+  gchar *key;
   const gchar *str;
 } Line;
 
-int
+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;
@@ -23,7 +33,7 @@ compare_collate (const void *a, const void *b)
   return g_utf8_collate (line_a->str, line_b->str);
 }
 
-int
+static int
 compare_key (const void *a, const void *b)
 {
   const Line *line_a = a;
@@ -35,10 +45,19 @@ compare_key (const void *a, const void *b)
 static void
 do_collate (gboolean for_file, gboolean use_key, const CollateTest *test)
 {
-  GArray *line_array = g_array_new (FALSE, FALSE, sizeof(Line));
+  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];
@@ -61,6 +80,8 @@ do_collate (gboolean for_file, gboolean use_key, const CollateTest *test)
       else
         g_assert_cmpstr (str, ==, test->sorted[i]);
     }
+
+  g_array_free (line_array, TRUE);
 }
 
 static void
@@ -90,6 +111,7 @@ const gchar *input0[] = {
   "eer34",
   "223",
   "er1",
+  "üĠണ",
   "foo",
   "bar",
   "baz",
@@ -106,6 +128,7 @@ const gchar *sorted0[] = {
   "er1",
   "foo",
   "GTK+",
+  "üĠണ",
   "z",
   NULL
 };
@@ -119,6 +142,7 @@ const gchar *file_sorted0[] = {
   "er1",
   "foo",
   "GTK+",
+  "üĠണ",
   "z",
   NULL
 };
@@ -174,13 +198,82 @@ const gchar *file_sorted1[] = {
   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;
-  gint i;
+  guint i;
   const gchar *locale;
-  CollateTest test[2];
+  CollateTest test[3];
 
   g_test_init (&argc, &argv, NULL);
 
@@ -188,8 +281,10 @@ main (int argc, char *argv[])
   locale = setlocale (LC_ALL, "");
   if (locale == NULL || strcmp (locale, "en_US") != 0)
     {
-      g_test_message ("No suitable locale, skipping test");
-      return 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;
@@ -198,6 +293,9 @@ main (int argc, char *argv[])
   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++)
     {
@@ -205,13 +303,12 @@ main (int argc, char *argv[])
       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, test_collate_key);
+      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, test_collate_file);
+      g_test_add_data_func (path, &test[i], test_collate_file);
       g_free (path);
     }
 
   return g_test_run ();
 }
-