0040-wcrtomb.c - scc - simple c99 compiler
(HTM) git clone git://git.simple-cc.org/scc
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) Submodules
(DIR) README
(DIR) LICENSE
---
0040-wcrtomb.c (2126B)
---
1 #include <assert.h>
2 #include <errno.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <wchar.h>
7
8 /*
9 output:
10 testing
11 testing wcrtomb1
12 testing wcrtomb2
13 testing wctomb
14 done
15 end:
16 */
17
18 #define NELEM(x) (sizeof(x)/sizeof(x[0]))
19
20 static char str[MB_CUR_MAX+1];
21
22 static struct wctest {
23 wchar_t wc;
24 char *s;
25 char exp[MB_CUR_MAX+1];
26 int r;
27 int syserr;
28 int mbstate;
29 } tests[] = {
30 {0, NULL, "", 1, 0, 1},
31 {0, str, "\0", 1, 0, 1},
32 {0x21, str, "\x21", 1, 0, 1},
33 {0x00A1, str, "\xc2\xa1", 2, 0, 1},
34 {0x2014, str, "\xe2\x80\x94", 3, 0, 1},
35 {0x01F4A9, str, "\xf0\x9f\x92\xa9", 4, 0, 1},
36
37 {0xd800, str, "", -1, EILSEQ, 1},
38 {0xDCFF, str, "", -1, EILSEQ, 1},
39 {0xDD00, str, "\xed\xb4\x80", 3, 0, 1},
40 {0x10ffff, str, "\xf4\x8f\xbf\xbf", 4, 0, 1},
41 {0x110000, str, "", -1, EILSEQ, 1},
42 };
43
44 void
45 tests_wcrtomb(void)
46 {
47 struct wctest *tp;
48 int r;
49 mbstate_t s;
50
51 puts("testing wcrtomb1");
52 for (tp = tests; tp < &tests[NELEM(tests)]; ++tp) {
53 memset(str, 0, MB_CUR_MAX+1);
54 errno = 0;
55 r = wcrtomb(tp->s, tp->wc, NULL);
56 assert(tp->r == r);
57 assert(tp->syserr == errno);
58 if (tp->s && tp->r != -1)
59 assert(!memcmp(tp->s, tp->exp, MB_CUR_MAX+1));
60 }
61
62 puts("testing wcrtomb2");
63 memset(&s, 0, sizeof(s));
64 for (tp = tests; tp < &tests[NELEM(tests)]; ++tp) {
65 memset(str, 0, MB_CUR_MAX+1);
66 errno = 0;
67 r = wcrtomb(tp->s, tp->wc, &s);
68 assert(tp->r == r);
69 assert(tp->syserr == errno);
70 if (tp->s && tp->r != -1)
71 assert(!memcmp(tp->s, tp->exp, MB_CUR_MAX+1));
72 assert(mbsinit(&s) != 0 == tp->mbstate);
73 }
74 }
75
76 void
77 tests_wctomb(void)
78 {
79 struct wctest *tp;
80 int r;
81
82 puts("testing wctomb");
83 for (tp = tests; tp < &tests[NELEM(tests)]; ++tp) {
84 memset(str, 0, MB_CUR_MAX+1);
85 errno = 0;
86 r = wctomb(tp->s, tp->wc);
87 assert(tp->r == r);
88 if (tp->s && tp->r != -1)
89 assert(!memcmp(tp->s, tp->exp, MB_CUR_MAX+1));
90 }
91 }
92
93 int
94 main(void)
95 {
96 puts("testing");
97 tests_wcrtomb();
98 tests_wctomb();
99 puts("done");
100 return 0;
101 }