build system updates, port libc-testsuit changes
[libc-test] / src / multibyte / mbc.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <wchar.h>
4 #include <stdlib.h>
5 #include <locale.h>
6 #include <langinfo.h>
7 #include "test.h"
8
9 /* r = place to store result
10  * f = function call to test (or any expression)
11  * x = expected result
12  * m = message to print on failure (with formats for r & x)
13 **/
14
15 #define TEST(r, f, x, m) ( \
16         memset(&st, 0, sizeof st), \
17         ((r) = (f)) == (x) || \
18                 (error("%s failed (" m ")\n", #f, r, x), 0) )
19
20 #define TEST_S(s, x, m) ( \
21         !strcmp((s),(x)) || \
22                 (error("[%s] != [%s] (%s)\n", s, x, m), 0) )
23
24 void test_mbc(void) {
25         const char *cs;
26         int i;
27         mbstate_t st, st2;
28         wchar_t wc, wcs[32];
29         
30         setlocale(LC_CTYPE, "en_US.UTF-8") ||
31         setlocale(LC_CTYPE, "en_GB.UTF-8") ||
32         setlocale(LC_CTYPE, "en.UTF-8") ||
33         setlocale(LC_CTYPE, "POSIX.UTF-8") ||
34         setlocale(LC_CTYPE, "C.UTF-8") ||
35         setlocale(LC_CTYPE, "UTF-8") ||
36         setlocale(LC_CTYPE, "");
37
38         TEST(i, mbsrtowcs(wcs, (cs="abcdef",&cs), 3, &st), 3, "wrong semantics for wcs buf len, %d != %d");
39         TEST(i, mbsrtowcs(wcs, (cs="abcdef",&cs), 8, &st), 6, "wrong semantics for wcs buf len, %d != %d");
40         TEST(i, mbsrtowcs(NULL, (cs="abcdef",&cs), 2, &st), 6, "wrong semantics for NULL wcs, %d != %d");
41
42         if (strcmp(nl_langinfo(CODESET), "UTF-8")) {
43                 error("cannot set UTF-8 locale for test (codeset=%s)\n", nl_langinfo(CODESET));
44                 return;
45         }
46         
47         TEST(i, mbrtowc(&wc, "\x80", 1, &st), -1, "failed to catch error %d != %d");
48         TEST(i, mbrtowc(&wc, "\xc0", 1, &st), -1, "failed to catch illegal initial, %d != %d");
49
50         TEST(i, mbrtowc(&wc, "\xc0\x80", 2, &st), -1, "aliasing nul %d != %d");
51         TEST(i, mbrtowc(&wc, "\xc0\xaf", 2, &st), -1, "aliasing slash %d != %d");
52         TEST(i, mbrtowc(&wc, "\xe0\x80\xaf", 3, &st), -1, "aliasing slash %d != %d");
53         TEST(i, mbrtowc(&wc, "\xf0\x80\x80\xaf", 4, &st), -1, "aliasing slash %d != %d");
54         TEST(i, mbrtowc(&wc, "\xf8\x80\x80\x80\xaf", 5, &st), -1, "aliasing slash %d != %d");
55         TEST(i, mbrtowc(&wc, "\xfc\x80\x80\x80\x80\xaf", 6, &st), -1, "aliasing slash %d != %d");
56         TEST(i, mbrtowc(&wc, "\xe0\x82\x80", 3, &st), -1, "aliasing U+0080 %d != %d");
57         TEST(i, mbrtowc(&wc, "\xe0\x9f\xbf", 3, &st), -1, "aliasing U+07FF %d != %d");
58         TEST(i, mbrtowc(&wc, "\xf0\x80\xa0\x80", 4, &st), -1, "aliasing U+0800 %d != %d");
59         TEST(i, mbrtowc(&wc, "\xf0\x8f\xbf\xbd", 4, &st), -1, "aliasing U+FFFD %d != %d");
60
61         TEST(i, mbrtowc(&wc, "\xed\xa0\x80", 3, &st), -1, "failed to catch surrogate, %d != %d");
62         TEST(i, mbrtowc(&wc, "\xef\xbf\xbe", 3, &st), 3, "failed to accept U+FFFE, %d != %d");
63         TEST(i, mbrtowc(&wc, "\xef\xbf\xbf", 3, &st), 3, "failed to accept U+FFFF, %d != %d");
64         TEST(i, mbrtowc(&wc, "\xf4\x8f\xbf\xbe", 4, &st), 4, "failed to accept U+10FFFE, %d != %d");
65         TEST(i, mbrtowc(&wc, "\xf4\x8f\xbf\xbf", 4, &st), 4, "failed to accept U+10FFFF, %d != %d");
66
67         TEST(i, mbrtowc(&wc, "\xc2\x80", 2, &st), 2, "wrong length %d != %d");
68         TEST(i, (mbrtowc(&wc, "\xc2\x80", 2, &st),wc), 0x80, "wrong char %04x != %04x");
69         TEST(i, mbrtowc(&wc, "\xe0\xa0\x80", 3, &st), 3, "wrong length %d != %d");
70         TEST(i, (mbrtowc(&wc, "\xe0\xa0\x80", 3, &st),wc), 0x800, "wrong char %04x != %04x");
71         TEST(i, mbrtowc(&wc, "\xf0\x90\x80\x80", 4, &st), 4, "wrong length %d != %d");
72         TEST(i, (mbrtowc(&wc, "\xf0\x90\x80\x80", 4, &st),wc), 0x10000, "wrong char %04x != %04x");
73
74         memset(&st2, 0, sizeof st2);
75         TEST(i, mbrtowc(&wc, "\xc2", 1, &st2), -2, "failed to accept initial byte, %d != %d");
76         TEST(i, mbrtowc(&wc, "\x80", 1, &st2), 1, "failed to resume, %d != %d");
77         TEST(i, wc, 0x80, "wrong char %04x != %04x");
78
79         memset(&st2, 0, sizeof st2);
80         TEST(i, mbrtowc(&wc, "\xc2", 1, &st2), -2, "failed to accept initial byte, %d != %d");
81         TEST(i, mbsrtowcs(wcs, (cs="\xa0""abc",&cs), 32, &st2), 4, "failed to resume, %d != %d");
82         TEST(i, wcs[0], 0xa0, "wrong char %04x != %04x");
83         TEST(i, wcs[1], 'a', "wrong char %04x != %04x");
84         TEST(i, !cs, 1, "wrong final position %d != %d");
85 }