f30f06182d79f4a88244bdce0bc3e64749752556
[openafs.git] / src / WINNT / afsd / test / convtest.c
1 #include<stdio.h>
2 #include<wchar.h>
3 #include "..\cm_nls.h"
4
5 #define elements_in(a) (sizeof(a)/sizeof(a[0]))
6
7 struct norm_pair {
8     const cm_unichar_t * left;
9     const cm_normchar_t * right;
10 };
11
12 struct norm_pair normalization_pairs[] = {
13     { L"abcdefghijklmnopq", L"abcdefghijklmnopq" },
14     { L"abcdefghijklmnopqrstuvwxyz1234"
15       L"abcdefghijklmnopqrstuvwxyz1234"
16       L"abcdefghijklmnopqrstuvwxyz1234"
17       L"abcdefghijklmnopqrstuvwxyz1234"
18       L"abcdefghijklmnopqrstuvwxyz1234"
19       L"abcdefghijklmnopqrstuvwxyz1234"
20       L"abcdefghijklmnopqrstuvwxyz1234"
21       L"abcdefghijklmnopqrstuvwxyz1234"
22       L"abcdefghijklmnopqrstuvwxyz1234"
23       L"abcdefghijklmnopqrstuvwxyz1234"
24       L"abcdefghijklmnopqrstuvwxyz1234"
25       L"abcdefghijklmnopqrstuvwxyz1234"
26       L"abcdefghijklmnopqrstuvwxyz1234"
27       L"abcdefghijklmnopqrstuvwxyz1234"
28       L"abcdefghijklmnopqrstuvwxyz1234"
29       L"abcdefghijklmnopqrstuvwxyz1234"
30       L"abcdefghijklmnopqrstuvwxyz1234"
31       L"abcdefghijklmnopqrstuvwxyz1234"
32       L"abcdefghijklmnopqrstuvwxyz1234"
33       L"abcdefghijklmnopqrstuvwxyz1234",
34
35       L"abcdefghijklmnopqrstuvwxyz1234"
36       L"abcdefghijklmnopqrstuvwxyz1234"
37       L"abcdefghijklmnopqrstuvwxyz1234"
38       L"abcdefghijklmnopqrstuvwxyz1234"
39       L"abcdefghijklmnopqrstuvwxyz1234"
40       L"abcdefghijklmnopqrstuvwxyz1234"
41       L"abcdefghijklmnopqrstuvwxyz1234"
42       L"abcdefghijklmnopqrstuvwxyz1234"
43       L"abcdefghijklmnopqrstuvwxyz1234"
44       L"abcdefghijklmnopqrstuvwxyz1234"
45       L"abcdefghijklmnopqrstuvwxyz1234"
46       L"abcdefghijklmnopqrstuvwxyz1234"
47       L"abcdefghijklmnopqrstuvwxyz1234"
48       L"abcdefghijklmnopqrstuvwxyz1234"
49       L"abcdefghijklmnopqrstuvwxyz1234"
50       L"abcdefghijklmnopqrstuvwxyz1234"
51       L"abcdefghijklmnopqrstuvwxyz1234"
52       L"abcdefghijklmnopqrstuvwxyz1234"
53       L"abcdefghijklmnopqrstuvwxyz1234"
54       L"abcdefghijklmnopqrstuvwxyz1234" },
55     { L"12839481flalfoo_)()(*&#@(*&",
56       L"12839481flalfoo_)()(*&#@(*&" }
57 };
58
59 void dumputf8(const unsigned char * s) {
60     while (*s) {
61         printf("%02X ", (int) *s++);
62     }
63 }
64
65 void dumpunicode(const wchar_t * s) {
66     while (*s) {
67         printf("%04X ", (int) *s++);
68     }
69 }
70
71 int cm_NormalizeStringAllocTest(void)
72 {
73     int i;
74
75     for (i=0; i < elements_in(normalization_pairs); i++) {
76         cm_normchar_t * nstr;
77         int cchdest = 0;
78
79         printf ("Test #%d:", i);
80
81         nstr = cm_NormalizeStringAlloc(normalization_pairs[i].left, -1, &cchdest);
82
83         if (nstr == NULL) {
84             printf ("FAILED! returned a NULL\n");
85             return 1;
86         }
87
88         if (wcscmp(nstr, normalization_pairs[i].right)) {
89             printf ("FAILED: Expected [");
90             dumpunicode(normalization_pairs[i].right);
91             printf ("] Received [");
92             dumpunicode(nstr);
93             printf ("]\n");
94             return 1;
95         }
96
97         if (wcslen(nstr) != cchdest - 1) {
98             printf ("FAILED: Length is wrong\n");
99             return 1;
100         }
101
102         printf ("PASS\n");
103
104         free (nstr);
105     }
106
107     return 0;
108 }
109
110 typedef struct norm_test_entry {
111     const wchar_t * str;
112     const wchar_t * nfc;
113     const wchar_t * nfd;
114     const wchar_t * nfkc;
115     const wchar_t * nfkd;
116 } norm_test_entry;
117
118 extern norm_test_entry norm_tests[];
119 extern int n_norm_tests;
120
121 int cm_NormalizeStringTest(void)
122 {
123     int i;
124     int n_failed = 0;
125
126     for (i=0; i < n_norm_tests; i++) {
127         cm_normchar_t * nfc;
128
129         nfc = cm_NormalizeStringAlloc(norm_tests[i].nfd, -1, NULL);
130         if (nfc == NULL) {
131             printf ("FAILED: returned a NULL\n");
132             return 1;
133         }
134
135         if (wcscmp(nfc, norm_tests[i].nfc)) {
136             printf ("FAILED: Expected [");
137             dumpunicode(norm_tests[i].nfc);
138             printf ("] Received [");
139             dumpunicode(nfc);
140             printf ("]\n");
141             n_failed ++;
142         }
143
144         free(nfc);
145     }
146
147     if (n_failed)
148         printf ("Number of failed tests: %d\n", n_failed);
149
150     return 0;
151 }
152
153 typedef struct conv_test_entry {
154     const cm_utf8char_t * str;
155     const cm_unichar_t  * wstr;
156 } conv_test_entry;
157
158 #define CTEST(a) { a, L ## a }
159
160 conv_test_entry conv_tests[] = {
161     CTEST(""),
162     CTEST("a"),
163     CTEST("abcdefghijkl"),
164     CTEST("osidfja*(2312835"),
165     {"\xee\x80\x80", L"\xe000"},
166     {"\xef\xbf\xbd", L"\xfffd"},
167     {"\xf0\x9f\xbf\xbf", L"\xd83f\xdfff"}, /* Surrogates */
168     {"\xF1\x9F\xBF\xBE", L"\xD93F\xDFFE"},
169     {"\xf0\x90\x80\x80", L"\xd800\xdc00"},
170 };
171
172 int cm_Utf16ToUtf8AllocTest(void)
173 {
174     int i;
175
176     for (i=0; i < sizeof(conv_tests)/sizeof(conv_tests[0]); i++) {
177         cm_utf8char_t * c;
178         int len = 0;
179
180         printf ("Test #%d:", i);
181
182         c = cm_Utf16ToUtf8Alloc(conv_tests[i].wstr, -1, &len);
183
184         if (c == NULL) {
185             printf ("FAILED: returned NULL\n");
186             return 1;
187         }
188
189         if (strlen(c) + 1 != len) {
190             printf ("FAILED: Returned wrong length [%d]. Actual length [%d]\n", len,
191                     strlen(c) + 1);
192             return 1;
193         }
194
195         if (strcmp(c, conv_tests[i].str)) {
196             printf ("FAILED: Expected [");
197             dumputf8(conv_tests[i].str);
198             printf ("]. Returned [");
199             dumputf8(c);
200             printf ("]\n");
201             return 1;
202         }
203
204         printf("PASS\n");
205
206         free(c);
207     }
208
209     return 0;
210 }
211
212 int cm_Utf16ToUtf8Test(void)
213 {
214     int i;
215     cm_utf8char_t c[1024];
216
217     for (i=0; i < sizeof(conv_tests)/sizeof(conv_tests[0]); i++) {
218         int len;
219
220         printf ("Test #%d:", i);
221
222         len = cm_Utf16ToUtf8(conv_tests[i].wstr, -1, c, sizeof(c)/sizeof(c[0]));
223
224         if (len == 0) {
225             printf ("FAILED: returned 0\n");
226             return 1;
227         }
228
229         if (strlen(c) + 1 != len) {
230             printf ("FAILED: Returned wrong length [%d]. Actual length [%d]\n", len,
231                     strlen(c) + 1);
232             return 1;
233         }
234
235         if (strcmp(c, conv_tests[i].str)) {
236             printf ("FAILED: Expected [%s]. Returned [%s]\n", conv_tests[i].str, c);
237             return 1;
238         }
239
240         printf("PASS\n");
241     }
242
243     return 0;
244 }
245
246 int cm_Utf8ToUtf16AllocTest(void)
247 {
248     int i;
249
250     for (i=0; i < sizeof(conv_tests)/sizeof(conv_tests[0]); i++) {
251         cm_unichar_t * c;
252         int len = 0;
253
254         printf ("Test #%d:", i);
255
256         c = cm_Utf8ToUtf16Alloc(conv_tests[i].str, -1, &len);
257
258         if (c == NULL) {
259             printf ("FAILED: returned NULL\n");
260             return 1;
261         }
262
263         if (wcslen(c) + 1 != len) {
264             printf ("FAILED: Returned wrong length [%d]. Actual length [%d]\n", len,
265                     wcslen(c) + 1);
266             return 1;
267         }
268
269         if (wcscmp(c, conv_tests[i].wstr)) {
270             printf ("FAILED: Expected [");
271             dumpunicode(conv_tests[i].wstr);
272             printf ("]. Returned [");
273             dumpunicode(c);
274             printf ("]\n");
275             return 1;
276         }
277
278         printf("PASS\n");
279
280         free(c);
281     }
282
283     return 0;
284 }
285
286 int cm_Utf8ToUtf16Test(void)
287 {
288     int i;
289     cm_unichar_t c[1024];
290
291     for (i=0; i < sizeof(conv_tests)/sizeof(conv_tests[0]); i++) {
292         int len = 0;
293
294         printf ("Test #%d:", i);
295
296         len = cm_Utf8ToUtf16(conv_tests[i].str, -1, c, sizeof(c)/sizeof(c[0]));
297
298         if (len == 0) {
299             printf ("FAILED: returned 0\n");
300             return 1;
301         }
302
303         if (wcslen(c) + 1 != len) {
304             printf ("FAILED: Returned wrong length [%d]. Actual length [%d]\n", len,
305                     wcslen(c) + 1);
306             return 1;
307         }
308
309         if (wcscmp(c, conv_tests[i].wstr)) {
310             printf ("FAILED: Expected [");
311             dumpunicode(conv_tests[i].wstr);
312             printf ("]. Returned [");
313             dumpunicode(c);
314             printf ("]\n");
315             return 1;
316         }
317
318         printf("PASS\n");
319     }
320
321     return 0;
322 }
323
324 int main(int argc, char ** argv)
325 {
326     int trv;
327
328     cm_InitNormalization();
329
330 #define RUNTEST(f) printf("Begin " #f "\n"); trv = f(); printf ("End " #f "\n\n"); if (trv != 0) return trv;
331
332     RUNTEST(cm_NormalizeStringAllocTest);
333     RUNTEST(cm_NormalizeStringTest);
334     RUNTEST(cm_Utf16ToUtf8AllocTest);
335     RUNTEST(cm_Utf16ToUtf8Test);
336     RUNTEST(cm_Utf8ToUtf16AllocTest);
337     RUNTEST(cm_Utf8ToUtf16Test);
338     return 0;
339 }