fam: implement gio-nfs-{file,directory}-monitor
[glib.git] / tests / unicode-caseconv.c
blob0563ab6c52141f0d4ec35320f83df4cd49e38608
1 #undef G_DISABLE_ASSERT
2 #undef G_LOG_DOMAIN
4 #include <locale.h>
5 #include <stdlib.h>
6 #include <stdio.h>
7 #include <glib.h>
8 #include <string.h>
10 int main (int argc, char **argv)
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;
24 if (!srcdir)
25 srcdir = ".";
26 filename = g_strconcat (srcdir, G_DIR_SEPARATOR_S, "casemap.txt", NULL);
28 infile = fopen (filename, "r");
29 if (!infile)
31 fprintf (stderr, "Failed to open %s\n", filename );
32 exit (1);
35 while (fgets (buffer, sizeof(buffer), infile))
37 if (buffer[0] == '#')
38 continue;
40 strings = g_strsplit (buffer, "\t", -1);
42 locale = strings[0];
44 if (!locale[0])
45 locale = "C";
47 if (strcmp (locale, current_locale) != 0)
49 setlocale (LC_CTYPE, locale);
50 current_locale = setlocale (LC_CTYPE, NULL);
52 if (strncmp (current_locale, locale, 2) != 0)
54 fprintf (stderr, "Cannot set locale to %s, skipping\n", locale);
55 goto next;
59 test = strings[1];
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"
69 convert = g_utf8_strup (test, -1);
70 expected = strings[4][0] ? strings[4] : test;
71 if (strcmp (convert, expected) != 0)
73 fprintf (stderr, "Failure: toupper(%s) == %s, should have been %s\n",
74 test, convert, expected);
75 result = 1;
77 g_free (convert);
79 convert = g_utf8_strdown (test, -1);
80 expected = strings[2][0] ? strings[2] : test;
81 if (strcmp (convert, expected) != 0)
83 fprintf (stderr, "Failure: tolower(%s) == %s, should have been %s\n",
84 test, convert, expected);
85 result = 1;
87 g_free (convert);
89 next:
90 g_strfreev (strings);
93 fclose (infile);
95 g_free (filename);
96 filename = g_strconcat (srcdir, G_DIR_SEPARATOR_S, "casefold.txt", NULL);
98 infile = fopen (filename, "r");
99 if (!infile)
101 fprintf (stderr, "Failed to open %s\n", filename );
102 g_free (filename);
103 exit (1);
106 while (fgets (buffer, sizeof(buffer), infile))
108 if (buffer[0] == '#')
109 continue;
111 buffer[strlen(buffer) - 1] = '\0';
112 strings = g_strsplit (buffer, "\t", -1);
114 test = strings[0];
116 convert = g_utf8_casefold (test, -1);
117 if (strcmp (convert, strings[1]) != 0)
119 fprintf (stderr, "Failure: casefold(%s) == '%s', should have been '%s'\n",
120 test, convert, strings[1]);
121 result = 1;
123 g_free (convert);
125 g_strfreev (strings);
128 fclose (infile);
129 g_free (filename);
131 return result;