os/ossrv/glib/tsrc/BC/tests/unicode-normalize.c
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 /* Portion Copyright © 2008-09 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.*/
     2 #undef G_DISABLE_ASSERT
     3 #undef G_LOG_DOMAIN
     4 
     5 #include <glib.h>
     6 #include <stdio.h>
     7 #include <stdlib.h>
     8 #include <string.h>
     9 
    10 #ifdef SYMBIAN
    11 #include "mrt2_glib2_test.h"
    12 #endif /*SYMBIAN*/
    13 
    14 gboolean success = TRUE;
    15 
    16 static char *decode (const gchar *input)
    17 {
    18   unsigned ch;
    19   int offset = 0;
    20   GString *result = g_string_new (NULL);
    21   
    22   do 
    23     {
    24       if (sscanf (input + offset, "%x", &ch) != 1)
    25 	{
    26 	  g_print("Error parsing character string %s\n", input);
    27 	  
    28 	  g_assert(FALSE && "unicode-normalize failed");
    29 	  
    30 	  #ifdef SYMBIAN
    31   	  testResultXml("unicode-normalize");
    32   	  #endif /* EMULATOR */
    33   	  
    34 	  exit (1);
    35     }
    36 
    37       g_string_append_unichar (result, ch);
    38       
    39       while (input[offset] && input[offset] != ' ')
    40 	offset++;
    41       while (input[offset] && input[offset] == ' ')
    42 	offset++;
    43     }
    44   while (input[offset]);
    45   return g_string_free (result, FALSE);
    46 }
    47 
    48 const char *names[4] = {
    49   "NFD",
    50   "NFC",
    51   "NFKD",
    52   "NFKC"
    53 };
    54 
    55 static void
    56 test_form (int line,
    57 	   GNormalizeMode mode,
    58 	   gboolean       do_compat,
    59 	   int            expected,
    60 	   char         **c,
    61 	   char         **raw)
    62 {
    63   int i;
    64   
    65   gboolean mode_is_compat = (mode == G_NORMALIZE_NFKC ||
    66 			     mode == G_NORMALIZE_NFKD);
    67 
    68   if (mode_is_compat || !do_compat)
    69     {
    70       for (i = 0; i < 3; i++)
    71 	{
    72 	  char *result = g_utf8_normalize (c[i], -1, mode);
    73 	  if (strcmp (result, c[expected]) != 0)
    74 	    {
    75 	      g_print("\nFailure: %d/%d: %s\n", line, i + 1, raw[5]);
    76 	      g_print("  g_utf8_normalize (%s, %s) != %s\n",
    77 		   raw[i], names[mode], raw[expected]);
    78 	      success = FALSE;
    79 	    }
    80 	  
    81 	  g_free (result);
    82 	}
    83     }
    84   if (mode_is_compat || do_compat)
    85     {
    86       for (i = 3; i < 5; i++)
    87 	{
    88 	  char *result = g_utf8_normalize (c[i], -1, mode);
    89 	  if (strcmp (result, c[expected]) != 0)
    90 	    {
    91 	      g_print("\nFailure: %d/%d: %s\n", line, i, raw[5]);
    92 	      g_print("  g_utf8_normalize (%s, %s) != %s\n",
    93 		   raw[i], names[mode], raw[expected]);
    94 	      success = FALSE;
    95 	    }
    96 	  
    97 	  g_free (result);
    98 	}
    99     }
   100 }
   101 
   102 static gboolean
   103 process_one (int line, gchar **columns)
   104 {
   105   char *c[5];
   106   int i;
   107   gboolean skip = FALSE;
   108 
   109   for (i=0; i < 5; i++)
   110     {
   111       c[i] = decode(columns[i]);
   112       if (!c[i])
   113 	skip = TRUE;
   114     }
   115 
   116   if (!skip)
   117     {
   118       test_form (line, G_NORMALIZE_NFD, FALSE, 2, c, columns);
   119       test_form (line, G_NORMALIZE_NFD, TRUE, 4, c, columns);
   120       test_form (line, G_NORMALIZE_NFC, FALSE, 1, c, columns);
   121       test_form (line, G_NORMALIZE_NFC, TRUE, 3, c, columns);
   122       test_form (line, G_NORMALIZE_NFKD, TRUE, 4, c, columns);
   123       test_form (line, G_NORMALIZE_NFKC, TRUE, 3, c, columns);
   124     }
   125 
   126   for (i=0; i < 5; i++)
   127     g_free (c[i]);
   128   
   129   return TRUE;
   130 }
   131 
   132 int main (int argc, char **argv)
   133 {
   134   GIOChannel *in;
   135   GError *error = NULL;
   136   GString *buffer;
   137   int line_to_do = 0;
   138   int line = 1;
   139   gchar *srcdir = getenv ("srcdir");
   140   gchar *testfile;
   141   
   142   #ifdef SYMBIAN
   143   g_log_set_handler (NULL,  G_LOG_FLAG_FATAL| G_LOG_FLAG_RECURSION | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_INFO | G_LOG_LEVEL_DEBUG, &mrtLogHandler, NULL);
   144   g_set_print_handler(mrtPrintHandler);
   145   #endif /*SYMBIAN*/
   146 	  
   147   buffer = g_string_new (NULL);
   148 
   149 /*if (argc != 2 && argc != 3)
   150     {
   151       g_print("Usage: unicode-normalize NormalizationTest.txt LINE\n");
   152       return 1;
   153     }
   154 
   155   if (argc == 3)
   156     line_to_do = atoi(argv[2]);
   157 */
   158 
   159   if (!srcdir)
   160 	srcdir = "c:";
   161 
   162   testfile = g_strconcat (srcdir, G_DIR_SEPARATOR_S "NormalizationTest.txt", NULL);
   163   
   164   in = g_io_channel_new_file (testfile, "r", &error);
   165   if (!in)
   166     {
   167       g_print("Cannot open %s: %s\n", testfile, error->message);
   168       g_assert(FALSE && "unicode-normalize failed");
   169       
   170       #ifdef SYMBIAN
   171       testResultXml("unicode-normalize");
   172       #endif /* EMULATOR */
   173       
   174       return 1;
   175     }
   176 
   177   while (TRUE)
   178     {
   179       gsize term_pos;
   180       gchar **columns;
   181 
   182       if (g_io_channel_read_line_string (in, buffer, &term_pos, &error) != G_IO_STATUS_NORMAL)
   183 	break;
   184 	
   185       //if (line_to_do && line != line_to_do)
   186 	//goto next;
   187       
   188       buffer->str[term_pos] = '\0';
   189       
   190       if (buffer->str[0] == '#') /* Comment */
   191 	goto next;
   192       if (buffer->str[0] == '@') /* Part */
   193 	{
   194 	  //g_print("\nProcessing %s\n",buffer->str + 1);
   195 	  goto next;
   196 	}
   197       
   198       columns = g_strsplit (buffer->str, ";", -1);
   199       if (!columns[0])
   200 	goto next;
   201       
   202       if (!process_one (line, columns))
   203 	return 1;
   204       g_strfreev (columns);
   205 
   206     next:
   207       g_string_truncate (buffer, 0);
   208       line++;
   209     }
   210 
   211   if (error)
   212     {
   213       g_print("Error reading test file, %s\n", error->message);
   214        
   215       g_assert(FALSE && "unicode-normalize failed");
   216 	  
   217 	  #ifdef SYMBIAN
   218   	  testResultXml("unicode-normalize");
   219   	  #endif /* EMULATOR */
   220       
   221       return 1;
   222     }
   223 
   224   g_io_channel_unref (in);
   225   g_string_free (buffer, TRUE);
   226     
   227   #ifdef SYMBIAN
   228   assert_failed = !success;
   229   testResultXml("unicode-normalize");
   230   #endif /* EMULATOR */
   231 
   232   return !success;
   233 }