sl@0: /* Portion Copyright © 2008-09 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.*/
sl@0: #undef G_DISABLE_ASSERT
sl@0: #undef G_LOG_DOMAIN
sl@0: 
sl@0: #include <glib.h>
sl@0: #include <stdio.h>
sl@0: #include <stdlib.h>
sl@0: #include <string.h>
sl@0: 
sl@0: #ifdef SYMBIAN
sl@0: #include "mrt2_glib2_test.h"
sl@0: #endif /*SYMBIAN*/
sl@0: 
sl@0: gboolean success = TRUE;
sl@0: 
sl@0: static char *decode (const gchar *input)
sl@0: {
sl@0:   unsigned ch;
sl@0:   int offset = 0;
sl@0:   GString *result = g_string_new (NULL);
sl@0:   
sl@0:   do 
sl@0:     {
sl@0:       if (sscanf (input + offset, "%x", &ch) != 1)
sl@0: 	{
sl@0: 	  g_print("Error parsing character string %s\n", input);
sl@0: 	  
sl@0: 	  g_assert(FALSE && "unicode-normalize failed");
sl@0: 	  
sl@0: 	  #ifdef SYMBIAN
sl@0:   	  testResultXml("unicode-normalize");
sl@0:   	  #endif /* EMULATOR */
sl@0:   	  
sl@0: 	  exit (1);
sl@0:     }
sl@0: 
sl@0:       g_string_append_unichar (result, ch);
sl@0:       
sl@0:       while (input[offset] && input[offset] != ' ')
sl@0: 	offset++;
sl@0:       while (input[offset] && input[offset] == ' ')
sl@0: 	offset++;
sl@0:     }
sl@0:   while (input[offset]);
sl@0:   return g_string_free (result, FALSE);
sl@0: }
sl@0: 
sl@0: const char *names[4] = {
sl@0:   "NFD",
sl@0:   "NFC",
sl@0:   "NFKD",
sl@0:   "NFKC"
sl@0: };
sl@0: 
sl@0: static void
sl@0: test_form (int line,
sl@0: 	   GNormalizeMode mode,
sl@0: 	   gboolean       do_compat,
sl@0: 	   int            expected,
sl@0: 	   char         **c,
sl@0: 	   char         **raw)
sl@0: {
sl@0:   int i;
sl@0:   
sl@0:   gboolean mode_is_compat = (mode == G_NORMALIZE_NFKC ||
sl@0: 			     mode == G_NORMALIZE_NFKD);
sl@0: 
sl@0:   if (mode_is_compat || !do_compat)
sl@0:     {
sl@0:       for (i = 0; i < 3; i++)
sl@0: 	{
sl@0: 	  char *result = g_utf8_normalize (c[i], -1, mode);
sl@0: 	  if (strcmp (result, c[expected]) != 0)
sl@0: 	    {
sl@0: 	      g_print("\nFailure: %d/%d: %s\n", line, i + 1, raw[5]);
sl@0: 	      g_print("  g_utf8_normalize (%s, %s) != %s\n",
sl@0: 		   raw[i], names[mode], raw[expected]);
sl@0: 	      success = FALSE;
sl@0: 	    }
sl@0: 	  
sl@0: 	  g_free (result);
sl@0: 	}
sl@0:     }
sl@0:   if (mode_is_compat || do_compat)
sl@0:     {
sl@0:       for (i = 3; i < 5; i++)
sl@0: 	{
sl@0: 	  char *result = g_utf8_normalize (c[i], -1, mode);
sl@0: 	  if (strcmp (result, c[expected]) != 0)
sl@0: 	    {
sl@0: 	      g_print("\nFailure: %d/%d: %s\n", line, i, raw[5]);
sl@0: 	      g_print("  g_utf8_normalize (%s, %s) != %s\n",
sl@0: 		   raw[i], names[mode], raw[expected]);
sl@0: 	      success = FALSE;
sl@0: 	    }
sl@0: 	  
sl@0: 	  g_free (result);
sl@0: 	}
sl@0:     }
sl@0: }
sl@0: 
sl@0: static gboolean
sl@0: process_one (int line, gchar **columns)
sl@0: {
sl@0:   char *c[5];
sl@0:   int i;
sl@0:   gboolean skip = FALSE;
sl@0: 
sl@0:   for (i=0; i < 5; i++)
sl@0:     {
sl@0:       c[i] = decode(columns[i]);
sl@0:       if (!c[i])
sl@0: 	skip = TRUE;
sl@0:     }
sl@0: 
sl@0:   if (!skip)
sl@0:     {
sl@0:       test_form (line, G_NORMALIZE_NFD, FALSE, 2, c, columns);
sl@0:       test_form (line, G_NORMALIZE_NFD, TRUE, 4, c, columns);
sl@0:       test_form (line, G_NORMALIZE_NFC, FALSE, 1, c, columns);
sl@0:       test_form (line, G_NORMALIZE_NFC, TRUE, 3, c, columns);
sl@0:       test_form (line, G_NORMALIZE_NFKD, TRUE, 4, c, columns);
sl@0:       test_form (line, G_NORMALIZE_NFKC, TRUE, 3, c, columns);
sl@0:     }
sl@0: 
sl@0:   for (i=0; i < 5; i++)
sl@0:     g_free (c[i]);
sl@0:   
sl@0:   return TRUE;
sl@0: }
sl@0: 
sl@0: int main (int argc, char **argv)
sl@0: {
sl@0:   GIOChannel *in;
sl@0:   GError *error = NULL;
sl@0:   GString *buffer;
sl@0:   int line_to_do = 0;
sl@0:   int line = 1;
sl@0:   gchar *srcdir = getenv ("srcdir");
sl@0:   gchar *testfile;
sl@0:   
sl@0:   #ifdef SYMBIAN
sl@0:   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);
sl@0:   g_set_print_handler(mrtPrintHandler);
sl@0:   #endif /*SYMBIAN*/
sl@0: 	  
sl@0:   buffer = g_string_new (NULL);
sl@0: 
sl@0: /*if (argc != 2 && argc != 3)
sl@0:     {
sl@0:       g_print("Usage: unicode-normalize NormalizationTest.txt LINE\n");
sl@0:       return 1;
sl@0:     }
sl@0: 
sl@0:   if (argc == 3)
sl@0:     line_to_do = atoi(argv[2]);
sl@0: */
sl@0: 
sl@0:   if (!srcdir)
sl@0: 	srcdir = "c:";
sl@0: 
sl@0:   testfile = g_strconcat (srcdir, G_DIR_SEPARATOR_S "NormalizationTest.txt", NULL);
sl@0:   
sl@0:   in = g_io_channel_new_file (testfile, "r", &error);
sl@0:   if (!in)
sl@0:     {
sl@0:       g_print("Cannot open %s: %s\n", testfile, error->message);
sl@0:       g_assert(FALSE && "unicode-normalize failed");
sl@0:       
sl@0:       #ifdef SYMBIAN
sl@0:       testResultXml("unicode-normalize");
sl@0:       #endif /* EMULATOR */
sl@0:       
sl@0:       return 1;
sl@0:     }
sl@0: 
sl@0:   while (TRUE)
sl@0:     {
sl@0:       gsize term_pos;
sl@0:       gchar **columns;
sl@0: 
sl@0:       if (g_io_channel_read_line_string (in, buffer, &term_pos, &error) != G_IO_STATUS_NORMAL)
sl@0: 	break;
sl@0: 	
sl@0:       //if (line_to_do && line != line_to_do)
sl@0: 	//goto next;
sl@0:       
sl@0:       buffer->str[term_pos] = '\0';
sl@0:       
sl@0:       if (buffer->str[0] == '#') /* Comment */
sl@0: 	goto next;
sl@0:       if (buffer->str[0] == '@') /* Part */
sl@0: 	{
sl@0: 	  //g_print("\nProcessing %s\n",buffer->str + 1);
sl@0: 	  goto next;
sl@0: 	}
sl@0:       
sl@0:       columns = g_strsplit (buffer->str, ";", -1);
sl@0:       if (!columns[0])
sl@0: 	goto next;
sl@0:       
sl@0:       if (!process_one (line, columns))
sl@0: 	return 1;
sl@0:       g_strfreev (columns);
sl@0: 
sl@0:     next:
sl@0:       g_string_truncate (buffer, 0);
sl@0:       line++;
sl@0:     }
sl@0: 
sl@0:   if (error)
sl@0:     {
sl@0:       g_print("Error reading test file, %s\n", error->message);
sl@0:        
sl@0:       g_assert(FALSE && "unicode-normalize failed");
sl@0: 	  
sl@0: 	  #ifdef SYMBIAN
sl@0:   	  testResultXml("unicode-normalize");
sl@0:   	  #endif /* EMULATOR */
sl@0:       
sl@0:       return 1;
sl@0:     }
sl@0: 
sl@0:   g_io_channel_unref (in);
sl@0:   g_string_free (buffer, TRUE);
sl@0:     
sl@0:   #ifdef SYMBIAN
sl@0:   assert_failed = !success;
sl@0:   testResultXml("unicode-normalize");
sl@0:   #endif /* EMULATOR */
sl@0: 
sl@0:   return !success;
sl@0: }