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: &argc, argv: &argv, NULL); |
24 | |
25 | filename = g_test_build_filename (file_type: G_TEST_DIST, first_path: "casemap.txt" , NULL); |
26 | |
27 | infile = fopen (filename: filename, modes: "r" ); |
28 | if (!infile) |
29 | { |
30 | fprintf (stderr, format: "Failed to open %s\n" , filename ); |
31 | exit (status: 1); |
32 | } |
33 | |
34 | while (fgets (s: buffer, n: sizeof(buffer), stream: infile)) |
35 | { |
36 | if (buffer[0] == '#') |
37 | continue; |
38 | |
39 | strings = g_strsplit (string: buffer, delimiter: "\t" , max_tokens: -1); |
40 | |
41 | locale = strings[0]; |
42 | |
43 | if (!locale[0]) |
44 | locale = "C" ; |
45 | |
46 | if (strcmp (s1: locale, s2: current_locale) != 0) |
47 | { |
48 | setlocale (LC_CTYPE, locale: locale); |
49 | current_locale = setlocale (LC_CTYPE, NULL); |
50 | |
51 | if (strncmp (s1: current_locale, s2: locale, n: 2) != 0) |
52 | { |
53 | fprintf (stderr, format: "Cannot set locale to %s, skipping\n" , locale); |
54 | goto next; |
55 | } |
56 | } |
57 | |
58 | test = strings[1]; |
59 | |
60 | /* gen-casemap-txt.py 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 (str: test, len: -1); |
69 | expected = strings[4][0] ? strings[4] : test; |
70 | if (strcmp (s1: convert, s2: expected) != 0) |
71 | { |
72 | fprintf (stderr, format: "Failure: toupper(%s) == %s, should have been %s\n" , |
73 | test, convert, expected); |
74 | result = 1; |
75 | } |
76 | g_free (mem: convert); |
77 | |
78 | convert = g_utf8_strdown (str: test, len: -1); |
79 | expected = strings[2][0] ? strings[2] : test; |
80 | if (strcmp (s1: convert, s2: expected) != 0) |
81 | { |
82 | fprintf (stderr, format: "Failure: tolower(%s) == %s, should have been %s\n" , |
83 | test, convert, expected); |
84 | result = 1; |
85 | } |
86 | g_free (mem: convert); |
87 | |
88 | next: |
89 | g_strfreev (str_array: strings); |
90 | } |
91 | |
92 | fclose (stream: infile); |
93 | |
94 | g_free (mem: filename); |
95 | filename = g_test_build_filename (file_type: G_TEST_DIST, first_path: "casefold.txt" , NULL); |
96 | |
97 | infile = fopen (filename: filename, modes: "r" ); |
98 | if (!infile) |
99 | { |
100 | fprintf (stderr, format: "Failed to open %s\n" , filename ); |
101 | g_free (mem: filename); |
102 | exit (status: 1); |
103 | } |
104 | |
105 | while (fgets (s: buffer, n: sizeof(buffer), stream: infile)) |
106 | { |
107 | if (buffer[0] == '#') |
108 | continue; |
109 | |
110 | buffer[strlen(s: buffer) - 1] = '\0'; |
111 | strings = g_strsplit (string: buffer, delimiter: "\t" , max_tokens: -1); |
112 | |
113 | test = strings[0]; |
114 | |
115 | convert = g_utf8_casefold (str: test, len: -1); |
116 | if (strcmp (s1: convert, s2: strings[1]) != 0) |
117 | { |
118 | fprintf (stderr, format: "Failure: casefold(%s) == '%s', should have been '%s'\n" , |
119 | test, convert, strings[1]); |
120 | result = 1; |
121 | } |
122 | g_free (mem: convert); |
123 | |
124 | g_strfreev (str_array: strings); |
125 | } |
126 | |
127 | fclose (stream: infile); |
128 | g_free (mem: filename); |
129 | |
130 | return result; |
131 | } |
132 | |