1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
#include <usual/wchar.h>
#include <string.h>
#include "test_common.h"
/*
* mbstr_decode()
*/
static const char *decode(const char *s, int inbuf)
{
static char out[128];
wchar_t tmp[128];
wchar_t *res;
int reslen = 4;
unsigned i;
for (i = 0; i < 128; i++)
tmp[i] = '~';
res = mbstr_decode(s, inbuf, &reslen, tmp, sizeof(tmp), true);
if (res == NULL) {
if (errno == EILSEQ) return "EILSEQ";
if (errno == ENOMEM) return "ENOMEM";
return "NULL??";
}
if (res != tmp)
return "EBUF";
if (res[reslen] == 0)
res[reslen] = 'Z';
else
return "reslen fail?";
for (i = 0; i < 128; i++) {
out[i] = tmp[i];
if (out[i] == '~') {
out[i+1] = 0;
break;
} else if (out[i] == 0) {
out[i] = '#';
} else if (tmp[i] > 127) {
out[i] = 'A' + tmp[i] % 26;
}
}
return out;
}
static void test_mbstr_decode(void *p)
{
str_check(decode("", 0), "Z~");
str_check(decode("", 1), "Z~");
str_check(decode("a", 0), "Z~");
str_check(decode("abc", 0), "Z~");
str_check(decode("abc", 1), "aZ~");
str_check(decode("abc", 2), "abZ~");
str_check(decode("abc", 3), "abcZ~");
str_check(decode("abc", 4), "abcZ~");
str_check(decode("abc", 5), "abcZ~");
if (MB_CUR_MAX > 1) {
str_check(decode("aa\200cc", 5), "aaYccZ~");
str_check(decode("a\200cc", 5), "aYccZ~");
str_check(decode("aa\200c", 5), "aaYcZ~");
}
end:;
}
/*
* mbsnrtowcs()
*/
static const char *mbsnr(const char *str, int inbuf, int outbuf)
{
static char out[128];
wchar_t tmp[128];
int res;
unsigned i;
const char *s = str;
mbstate_t ps;
for (i = 0; i < 128; i++)
tmp[i] = '~';
memset(&ps, 0, sizeof(ps));
res = mbsnrtowcs(tmp, &s, inbuf, outbuf, &ps);
if (res < 0) {
if (errno == EILSEQ) {
snprintf(out, sizeof(out), "EILSEQ(%d)", (int)(s - str));
return out;
}
return "unknown error";
}
if (tmp[res] == 0)
tmp[res] = s ? 'z' : 'Z';
for (i = 0; i < 128; i++) {
out[i] = tmp[i];
if (out[i] == '~') {
out[i+1] = 0;
break;
}
}
return out;
}
static void test_mbsnrtowcs(void *p)
{
str_check(mbsnr("", 1, 1), "Z~");
str_check(mbsnr("", 0, 0), "~");
str_check(mbsnr("", 0, 1), "~"); /* XXX */
str_check(mbsnr("", 1, 0), "~");
str_check(mbsnr("x", 1, 1), "x~");
str_check(mbsnr("x", 0, 0), "~");
str_check(mbsnr("x", 0, 1), "~"); /* XXX */
str_check(mbsnr("x", 1, 0), "~");
str_check(mbsnr("abc", 3, 3), "abc~");
str_check(mbsnr("abc", 3, 4), "abc~"); /* XXX */
str_check(mbsnr("abc", 4, 3), "abc~");
str_check(mbsnr("abc", 4, 4), "abcZ~");
end:;
}
/*
* Describe
*/
struct testcase_t wchar_tests[] = {
{ "mbsnrtowcs", test_mbsnrtowcs },
{ "mbstr_decode", test_mbstr_decode },
END_OF_TESTCASES
};
|