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