0042-mbsrtowcs.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
---
0042-mbsrtowcs.c (2770B)
---
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 mbsrtowcs1
12 testing mbsrtowcs2
13 testing mbstowcs
14 done
15 end:
16 */
17
18 #define NELEM(x) (sizeof(x)/sizeof(x[0]))
19
20 static char str[20];
21 static wchar_t wcs[20];
22 static struct mbstests {
23 char *s;
24 char *sexp;
25
26 wchar_t *wcs;
27 wchar_t *wcsexp;
28
29 size_t n;
30 int r;
31 int syserr;
32 int mbstate;
33 } tests[] = {
34 /* s sexp wcs wcsexp n r syserr mbstate */
35 {"\0", NULL, wcs, (wchar_t[]) {0}, 1, 0, 0, 1},
36 {"\0", str, wcs, (wchar_t[]) {0}, 0, 0, 0, 1},
37 {"\0", str, NULL, (wchar_t[]) {0}, 1, 0, 0, 1},
38 {"\0", str, NULL, (wchar_t[]) {0}, 0, 0, 0, 1},
39
40 {"\x31", NULL, wcs, (wchar_t[]) {0x31, 0}, 2, 1, 0, 1},
41 {"\x31", str+1, wcs, (wchar_t[]) {0x31, 0}, 1, 1, 0, 1},
42 {"\x31", str, NULL, (wchar_t[]) {0x31, 0}, 1, 1, 0, 1},
43 {"\x31", str, NULL, (wchar_t[]) {0x31, 0}, 0, 1, 0, 1},
44
45 {"\x21\xc2\xa1\xe2\x80\x94\xf0\x9f\x92\xa9",
46 NULL, wcs,
47 (wchar_t[]) {0x21,0xa1,0x2014,0x1f4A9}, 20, 4, 0, 1},
48
49 {"\xf0\x9f",str, wcs, NULL, 20,-1, EILSEQ, 0},
50 };
51
52 void
53 tests_mbsrtowcs(void)
54 {
55 int r;
56 const char *s;
57 mbstate_t st;
58 struct mbstests *tp;
59
60 puts("testing mbsrtowcs1");
61 for (tp = tests; tp < &tests[NELEM(tests)]; ++tp) {
62 errno = 0;
63 if (tp->s != NULL)
64 s = strcpy(str, tp->s);
65 memset(wcs, -1, sizeof(wcs));
66
67 r = mbsrtowcs(tp->wcs, &s, tp->n, NULL);
68 assert(tp->r == r);
69 assert(tp->syserr == errno);
70 if (tp->r >= 0) {
71 assert(s == tp->sexp);
72 if (tp->wcs)
73 assert(!wcsncmp(tp->wcsexp, tp->wcs, tp->r));
74 }
75 }
76
77 puts("testing mbsrtowcs2");
78 for (tp = tests; tp < &tests[NELEM(tests)]; ++tp) {
79 errno = 0;
80 if (tp->s != NULL)
81 s = strcpy(str, tp->s);
82 memset(wcs, -1, sizeof(wcs));
83 memset(&st, 0, sizeof(st));
84
85 r = mbsrtowcs(tp->wcs, &s, tp->n, &st);
86 assert(tp->r == r);
87 assert(tp->syserr == errno);
88 if (tp->r >= 0) {
89 assert(s == tp->sexp);
90 if (tp->wcs)
91 assert(!wcsncmp(tp->wcsexp, tp->wcs, tp->r));
92 assert(mbsinit(&st) != 0 == tp->mbstate);
93 }
94 }
95 }
96
97 void
98 tests_mbstowcs(void)
99 {
100 int r;
101 struct mbstests *tp;
102
103 puts("testing mbstowcs");
104 for (tp = tests; tp < &tests[NELEM(tests)]; ++tp) {
105 errno = 0;
106 memset(wcs, -1, sizeof(wcs));
107
108 r = mbstowcs(tp->wcs, tp->s, tp->n);
109 assert(tp->r == r);
110 if (tp->r >= 0) {
111 if (tp->wcs)
112 assert(!wcsncmp(tp->wcsexp, tp->wcs, tp->r));
113 }
114 }
115 }
116
117 int
118 main(void)
119 {
120 puts("testing");
121 tests_mbsrtowcs();
122 tests_mbstowcs();
123 puts("done");
124 return 0;
125 }