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