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