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