tests: add a timing test for adding lots of GSources
[platform/upstream/glib.git] / tests / unicode-normalize.c
index 1c8318d..9679e25 100644 (file)
@@ -1,6 +1,10 @@
+#undef G_DISABLE_ASSERT
+#undef G_LOG_DOMAIN
+
 #include <glib.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 
 gboolean success = TRUE;
 
@@ -10,8 +14,6 @@ decode (const gchar *input)
   unsigned ch;
   int offset = 0;
   GString *result = g_string_new (NULL);
-  int len;
-  char buf[6];
   
   do 
     {
@@ -21,16 +23,7 @@ decode (const gchar *input)
          exit (1);
        }
 
-      /* FIXME: We don't handle the > BMP or Hangul syllables */
-      if (ch > 0xffff ||                /* > BMP */
-         (ch >= 0xac00 && ch <= 0xd7ff))  /* Hangul syllables */
-       {
-         g_string_free (result, TRUE);
-         return NULL;
-       }
-
-      len = g_unichar_to_utf8 (ch, buf);
-      g_string_append_len (result, buf, len);
+      g_string_append_unichar (result, ch);
       
       while (input[offset] && input[offset] != ' ')
        offset++;
@@ -49,6 +42,22 @@ const char *names[4] = {
   "NFKC"
 };
 
+static char *
+encode (const gchar *input)
+{
+  GString *result = g_string_new(NULL);
+
+  const gchar *p = input;
+  while (*p)
+    {
+      gunichar c = g_utf8_get_char (p);
+      g_string_append_printf (result, "%04X ", c);
+      p = g_utf8_next_char(p);
+    }
+
+  return g_string_free (result, FALSE);
+}
+
 static void
 test_form (int            line,
           GNormalizeMode mode,
@@ -66,12 +75,14 @@ test_form (int            line,
     {
       for (i = 0; i < 3; i++)
        {
-         char *result = g_utf8_normalize (c[i], mode);
+         char *result = g_utf8_normalize (c[i], -1, mode);
          if (strcmp (result, c[expected]) != 0)
            {
+             char *result_raw = encode(result);
              fprintf (stderr, "\nFailure: %d/%d: %s\n", line, i + 1, raw[5]);
-             fprintf (stderr, "  g_utf8_normalize (%s, %s) != %s\n",
-                  raw[i], names[mode], raw[expected]);
+             fprintf (stderr, "  g_utf8_normalize (%s, %s) != %s but %s\n",
+                  raw[i], names[mode], raw[expected], result_raw);
+             g_free (result_raw);
              success = FALSE;
            }
          
@@ -82,12 +93,14 @@ test_form (int            line,
     {
       for (i = 3; i < 5; i++)
        {
-         char *result = g_utf8_normalize (c[i], mode);
+         char *result = g_utf8_normalize (c[i], -1, mode);
          if (strcmp (result, c[expected]) != 0)
            {
+             char *result_raw = encode(result);
              fprintf (stderr, "\nFailure: %d/%d: %s\n", line, i, raw[5]);
-             fprintf (stderr, "  g_utf8_normalize (%s, %s) != %s\n",
-                  raw[i], names[mode], raw[expected]);
+             fprintf (stderr, "  g_utf8_normalize (%s, %s) != %s but %s\n",
+                  raw[i], names[mode], raw[expected], result_raw);
+             g_free (result_raw);
              success = FALSE;
            }
          
@@ -143,7 +156,7 @@ int main (int argc, char **argv)
   if (argc == 3)
     line_to_do = atoi(argv[2]);
 
-  in = g_io_channel_new_file (argv[1], G_IO_FILE_MODE_READ, &error);
+  in = g_io_channel_new_file (argv[1], "r", &error);
   if (!in)
     {
       fprintf (stderr, "Cannot open %s: %s\n", argv[1], error->message);
@@ -172,6 +185,9 @@ int main (int argc, char **argv)
        }
       
       columns = g_strsplit (buffer->str, ";", -1);
+      if (!columns[0])
+       goto next;
+      
       if (!process_one (line, columns))
        return 1;
       g_strfreev (columns);
@@ -187,7 +203,7 @@ int main (int argc, char **argv)
       return 1;
     }
 
-  g_io_channel_close (in);
+  g_io_channel_unref (in);
   g_string_free (buffer, TRUE);
 
   return !success;