glib/tests/unicode-caseconv.c
changeset 18 47c74d1534e1
child 34 5fae379060a7
equal deleted inserted replaced
0:e4d67989cc36 18:47c74d1534e1
       
     1 /*
       
     2 * Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 
       
    18 #undef G_DISABLE_ASSERT
       
    19 #undef G_LOG_DOMAIN
       
    20 
       
    21 #include <locale.h>
       
    22 #include <stdlib.h>
       
    23 #include <stdio.h>
       
    24 #include <glib.h>
       
    25 #include <string.h>
       
    26 
       
    27 #ifdef __SYMBIAN32__
       
    28 #include "mrt2_glib2_test.h"
       
    29 #endif /*SYMBIAN*/
       
    30 
       
    31 
       
    32 int main (int argc, char **argv)
       
    33 {
       
    34   FILE *infile;
       
    35   char buffer[1024];
       
    36   char **strings;
       
    37   char *srcdir = getenv ("srcdir");
       
    38   char *filename;
       
    39   const char *locale;
       
    40   const char *test;
       
    41   const char *expected;
       
    42   char *convert;
       
    43   char *current_locale = setlocale (LC_CTYPE, NULL);
       
    44   gint result = 0;
       
    45 	#ifdef __SYMBIAN32__
       
    46 
       
    47   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);
       
    48   g_set_print_handler(mrtPrintHandler);
       
    49   #endif /*__SYMBIAN32__*/
       
    50   	
       
    51   
       
    52   if (!srcdir)
       
    53     srcdir = "c:";
       
    54   filename = g_strconcat (srcdir, G_DIR_SEPARATOR_S, "casemap.txt", NULL);
       
    55   
       
    56   infile = fopen (filename, "r");
       
    57   if (!infile)
       
    58     {
       
    59       fprintf (stderr, "Failed to open %s\n", filename );
       
    60       g_assert(FALSE && "unicode-caseconv failed");
       
    61       #ifdef __SYMBIAN32__
       
    62   	  testResultXml("unicode-caseconv");
       
    63   	  #endif /* EMULATOR */
       
    64       exit (1);
       
    65     }
       
    66   
       
    67   while (fgets (buffer, sizeof(buffer), infile))
       
    68     {
       
    69       if (buffer[0] == '#')
       
    70 	continue;
       
    71 
       
    72       strings = g_strsplit (buffer, "\t", -1);
       
    73 
       
    74       locale = strings[0];
       
    75 
       
    76       if (!locale[0])
       
    77 	locale = "C";
       
    78 	
       
    79       if (strcmp (locale, current_locale) != 0)
       
    80 	{
       
    81 	  setlocale (LC_CTYPE, locale);
       
    82 	  current_locale = setlocale (LC_CTYPE, NULL);
       
    83 
       
    84 	  if (strncmp (current_locale, locale, 2) != 0)
       
    85 	    {
       
    86 	      fprintf (stderr, "Cannot set locale to %s, skipping\n", locale);
       
    87 	      goto next;
       
    88 	    }
       
    89 	}
       
    90       
       
    91       test = strings[1];
       
    92 
       
    93       /* gen-casemap-txt.pl uses an empty string when a single character
       
    94        * doesn't have an equivalent in a particular case; since that behavior
       
    95        * is nonsense for multicharacter strings, it would make more sense
       
    96        * to put the expected result .. the original character unchanged. But 
       
    97        * for now, we just work around it here and take the empty string to mean
       
    98        * "same as original"
       
    99        */
       
   100 
       
   101       convert = g_utf8_strup (test, -1);
       
   102       expected = strings[4][0] ? strings[4] : test;
       
   103       if (strcmp (convert, expected) != 0)
       
   104 	{
       
   105 	  fprintf (stderr, "Failure: toupper(%s) == %s, should have been %s\n",
       
   106 		   test, convert, expected);
       
   107 	  result = 1;
       
   108 	}
       
   109       g_free (convert);
       
   110 
       
   111       convert = g_utf8_strdown (test, -1);
       
   112       expected = strings[2][0] ? strings[2] : test;
       
   113       if (strcmp (convert, expected) != 0)
       
   114 	{
       
   115 	  fprintf (stderr, "Failure: tolower(%s) == %s, should have been %s\n",
       
   116 		   test, convert, expected);
       
   117 	  result = 1;
       
   118 	}
       
   119       g_free (convert);
       
   120 
       
   121     next:
       
   122       g_strfreev (strings);
       
   123     }
       
   124 
       
   125   fclose (infile);
       
   126 
       
   127   g_free (filename);
       
   128   filename = g_strconcat (srcdir, G_DIR_SEPARATOR_S, "casefold.txt", NULL);
       
   129   
       
   130   infile = fopen (filename, "r");
       
   131   if (!infile)
       
   132     {
       
   133       fprintf (stderr, "Failed to open %s\n", filename );
       
   134 	  g_assert(FALSE && "unicode-caseconv failed");
       
   135 	  #ifdef __SYMBIAN32__
       
   136   	  testResultXml("unicode-caseconv");
       
   137       #endif /* EMULATOR */
       
   138       
       
   139       exit (1);
       
   140     }
       
   141   
       
   142   while (fgets (buffer, sizeof(buffer), infile))
       
   143     {
       
   144       if (buffer[0] == '#')
       
   145 	continue;
       
   146 
       
   147       buffer[strlen(buffer) - 1] = '\0';
       
   148       strings = g_strsplit (buffer, "\t", -1);
       
   149 
       
   150       test = strings[0];
       
   151 
       
   152       convert = g_utf8_casefold (test, -1);
       
   153       if (strcmp (convert, strings[1]) != 0)
       
   154 	{
       
   155 	  fprintf (stderr, "Failure: casefold(%s) == '%s', should have been '%s'\n",
       
   156 		   test, convert, strings[1]);
       
   157 	  result = 1;
       
   158 	}
       
   159       g_free (convert);
       
   160 
       
   161       g_strfreev (strings);
       
   162     }
       
   163 
       
   164   fclose (infile);
       
   165   g_free (filename);
       
   166 
       
   167 	#ifdef __SYMBIAN32__
       
   168   			testResultXml("unicode-caseconv");
       
   169   	 #endif /* EMULATOR */
       
   170   
       
   171   return result;
       
   172 }