os/ossrv/glib/tsrc/BC/tests/unicode-normalize.c
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/glib/tsrc/BC/tests/unicode-normalize.c	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,233 @@
     1.4 +/* Portion Copyright © 2008-09 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.*/
     1.5 +#undef G_DISABLE_ASSERT
     1.6 +#undef G_LOG_DOMAIN
     1.7 +
     1.8 +#include <glib.h>
     1.9 +#include <stdio.h>
    1.10 +#include <stdlib.h>
    1.11 +#include <string.h>
    1.12 +
    1.13 +#ifdef SYMBIAN
    1.14 +#include "mrt2_glib2_test.h"
    1.15 +#endif /*SYMBIAN*/
    1.16 +
    1.17 +gboolean success = TRUE;
    1.18 +
    1.19 +static char *decode (const gchar *input)
    1.20 +{
    1.21 +  unsigned ch;
    1.22 +  int offset = 0;
    1.23 +  GString *result = g_string_new (NULL);
    1.24 +  
    1.25 +  do 
    1.26 +    {
    1.27 +      if (sscanf (input + offset, "%x", &ch) != 1)
    1.28 +	{
    1.29 +	  g_print("Error parsing character string %s\n", input);
    1.30 +	  
    1.31 +	  g_assert(FALSE && "unicode-normalize failed");
    1.32 +	  
    1.33 +	  #ifdef SYMBIAN
    1.34 +  	  testResultXml("unicode-normalize");
    1.35 +  	  #endif /* EMULATOR */
    1.36 +  	  
    1.37 +	  exit (1);
    1.38 +    }
    1.39 +
    1.40 +      g_string_append_unichar (result, ch);
    1.41 +      
    1.42 +      while (input[offset] && input[offset] != ' ')
    1.43 +	offset++;
    1.44 +      while (input[offset] && input[offset] == ' ')
    1.45 +	offset++;
    1.46 +    }
    1.47 +  while (input[offset]);
    1.48 +  return g_string_free (result, FALSE);
    1.49 +}
    1.50 +
    1.51 +const char *names[4] = {
    1.52 +  "NFD",
    1.53 +  "NFC",
    1.54 +  "NFKD",
    1.55 +  "NFKC"
    1.56 +};
    1.57 +
    1.58 +static void
    1.59 +test_form (int line,
    1.60 +	   GNormalizeMode mode,
    1.61 +	   gboolean       do_compat,
    1.62 +	   int            expected,
    1.63 +	   char         **c,
    1.64 +	   char         **raw)
    1.65 +{
    1.66 +  int i;
    1.67 +  
    1.68 +  gboolean mode_is_compat = (mode == G_NORMALIZE_NFKC ||
    1.69 +			     mode == G_NORMALIZE_NFKD);
    1.70 +
    1.71 +  if (mode_is_compat || !do_compat)
    1.72 +    {
    1.73 +      for (i = 0; i < 3; i++)
    1.74 +	{
    1.75 +	  char *result = g_utf8_normalize (c[i], -1, mode);
    1.76 +	  if (strcmp (result, c[expected]) != 0)
    1.77 +	    {
    1.78 +	      g_print("\nFailure: %d/%d: %s\n", line, i + 1, raw[5]);
    1.79 +	      g_print("  g_utf8_normalize (%s, %s) != %s\n",
    1.80 +		   raw[i], names[mode], raw[expected]);
    1.81 +	      success = FALSE;
    1.82 +	    }
    1.83 +	  
    1.84 +	  g_free (result);
    1.85 +	}
    1.86 +    }
    1.87 +  if (mode_is_compat || do_compat)
    1.88 +    {
    1.89 +      for (i = 3; i < 5; i++)
    1.90 +	{
    1.91 +	  char *result = g_utf8_normalize (c[i], -1, mode);
    1.92 +	  if (strcmp (result, c[expected]) != 0)
    1.93 +	    {
    1.94 +	      g_print("\nFailure: %d/%d: %s\n", line, i, raw[5]);
    1.95 +	      g_print("  g_utf8_normalize (%s, %s) != %s\n",
    1.96 +		   raw[i], names[mode], raw[expected]);
    1.97 +	      success = FALSE;
    1.98 +	    }
    1.99 +	  
   1.100 +	  g_free (result);
   1.101 +	}
   1.102 +    }
   1.103 +}
   1.104 +
   1.105 +static gboolean
   1.106 +process_one (int line, gchar **columns)
   1.107 +{
   1.108 +  char *c[5];
   1.109 +  int i;
   1.110 +  gboolean skip = FALSE;
   1.111 +
   1.112 +  for (i=0; i < 5; i++)
   1.113 +    {
   1.114 +      c[i] = decode(columns[i]);
   1.115 +      if (!c[i])
   1.116 +	skip = TRUE;
   1.117 +    }
   1.118 +
   1.119 +  if (!skip)
   1.120 +    {
   1.121 +      test_form (line, G_NORMALIZE_NFD, FALSE, 2, c, columns);
   1.122 +      test_form (line, G_NORMALIZE_NFD, TRUE, 4, c, columns);
   1.123 +      test_form (line, G_NORMALIZE_NFC, FALSE, 1, c, columns);
   1.124 +      test_form (line, G_NORMALIZE_NFC, TRUE, 3, c, columns);
   1.125 +      test_form (line, G_NORMALIZE_NFKD, TRUE, 4, c, columns);
   1.126 +      test_form (line, G_NORMALIZE_NFKC, TRUE, 3, c, columns);
   1.127 +    }
   1.128 +
   1.129 +  for (i=0; i < 5; i++)
   1.130 +    g_free (c[i]);
   1.131 +  
   1.132 +  return TRUE;
   1.133 +}
   1.134 +
   1.135 +int main (int argc, char **argv)
   1.136 +{
   1.137 +  GIOChannel *in;
   1.138 +  GError *error = NULL;
   1.139 +  GString *buffer;
   1.140 +  int line_to_do = 0;
   1.141 +  int line = 1;
   1.142 +  gchar *srcdir = getenv ("srcdir");
   1.143 +  gchar *testfile;
   1.144 +  
   1.145 +  #ifdef SYMBIAN
   1.146 +  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);
   1.147 +  g_set_print_handler(mrtPrintHandler);
   1.148 +  #endif /*SYMBIAN*/
   1.149 +	  
   1.150 +  buffer = g_string_new (NULL);
   1.151 +
   1.152 +/*if (argc != 2 && argc != 3)
   1.153 +    {
   1.154 +      g_print("Usage: unicode-normalize NormalizationTest.txt LINE\n");
   1.155 +      return 1;
   1.156 +    }
   1.157 +
   1.158 +  if (argc == 3)
   1.159 +    line_to_do = atoi(argv[2]);
   1.160 +*/
   1.161 +
   1.162 +  if (!srcdir)
   1.163 +	srcdir = "c:";
   1.164 +
   1.165 +  testfile = g_strconcat (srcdir, G_DIR_SEPARATOR_S "NormalizationTest.txt", NULL);
   1.166 +  
   1.167 +  in = g_io_channel_new_file (testfile, "r", &error);
   1.168 +  if (!in)
   1.169 +    {
   1.170 +      g_print("Cannot open %s: %s\n", testfile, error->message);
   1.171 +      g_assert(FALSE && "unicode-normalize failed");
   1.172 +      
   1.173 +      #ifdef SYMBIAN
   1.174 +      testResultXml("unicode-normalize");
   1.175 +      #endif /* EMULATOR */
   1.176 +      
   1.177 +      return 1;
   1.178 +    }
   1.179 +
   1.180 +  while (TRUE)
   1.181 +    {
   1.182 +      gsize term_pos;
   1.183 +      gchar **columns;
   1.184 +
   1.185 +      if (g_io_channel_read_line_string (in, buffer, &term_pos, &error) != G_IO_STATUS_NORMAL)
   1.186 +	break;
   1.187 +	
   1.188 +      //if (line_to_do && line != line_to_do)
   1.189 +	//goto next;
   1.190 +      
   1.191 +      buffer->str[term_pos] = '\0';
   1.192 +      
   1.193 +      if (buffer->str[0] == '#') /* Comment */
   1.194 +	goto next;
   1.195 +      if (buffer->str[0] == '@') /* Part */
   1.196 +	{
   1.197 +	  //g_print("\nProcessing %s\n",buffer->str + 1);
   1.198 +	  goto next;
   1.199 +	}
   1.200 +      
   1.201 +      columns = g_strsplit (buffer->str, ";", -1);
   1.202 +      if (!columns[0])
   1.203 +	goto next;
   1.204 +      
   1.205 +      if (!process_one (line, columns))
   1.206 +	return 1;
   1.207 +      g_strfreev (columns);
   1.208 +
   1.209 +    next:
   1.210 +      g_string_truncate (buffer, 0);
   1.211 +      line++;
   1.212 +    }
   1.213 +
   1.214 +  if (error)
   1.215 +    {
   1.216 +      g_print("Error reading test file, %s\n", error->message);
   1.217 +       
   1.218 +      g_assert(FALSE && "unicode-normalize failed");
   1.219 +	  
   1.220 +	  #ifdef SYMBIAN
   1.221 +  	  testResultXml("unicode-normalize");
   1.222 +  	  #endif /* EMULATOR */
   1.223 +      
   1.224 +      return 1;
   1.225 +    }
   1.226 +
   1.227 +  g_io_channel_unref (in);
   1.228 +  g_string_free (buffer, TRUE);
   1.229 +    
   1.230 +  #ifdef SYMBIAN
   1.231 +  assert_failed = !success;
   1.232 +  testResultXml("unicode-normalize");
   1.233 +  #endif /* EMULATOR */
   1.234 +
   1.235 +  return !success;
   1.236 +}