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