2 * Copyright (c) 2004 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42 static const char *failing_testcases
[] = {
76 uint32_t u
[MAX_LENGTH
];
80 static const struct testcase testcases
[] = {
84 {"\x01\x7F", 2, {0x01, 0x7F}},
87 {"\xC1\x80", 1, {0x40}},
88 {"\xDF\xBF", 1, {0x7FF}},
89 {"\xE0\x80\x80", 1, {0}},
90 {"\xE0\x80\x81", 1, {1}},
91 {"\xE0\x81\x80", 1, {0x40}},
92 {"\xE1\x80\x80", 1, {0x1000}},
93 {"\xEF\xBF\xBF", 1, {0xFFFF}},
94 {"\xF0\x80\x80\x80", 1, {0}},
95 {"\xF0\x80\x80\x81", 1, {1}},
96 {"\xF0\x80\x81\x80", 1, {0x40}},
97 {"\xF0\x81\x80\x80", 1, {0x1000}},
98 {"\xF1\x80\x80\x80", 1, {0x40000}},
99 {"\xF7\xBF\xBF\xBF", 1, {0X1FFFFF}, 1},
105 unsigned failures
= 0;
110 uint32_t u
[MAX_LENGTH
];
111 char str
[MAX_LENGTH
* 4];
113 for (s
= failing_testcases
; *s
!= NULL
; ++s
) {
115 ret
= wind_utf8ucs4(*s
, u
, &len
);
117 printf("utf8 decode of \"%s\" should have failed\n", *s
);
122 for (i
= 0; i
< sizeof(testcases
)/sizeof(testcases
[0]); ++i
) {
123 const struct testcase
*t
= &testcases
[i
];
125 ret
= wind_utf8ucs4_length(t
->utf8_str
, &len
);
127 printf("utf8ucs4 length of \"%s\" should have succeeded\n",
133 printf("utf8ucs4_length of \"%s\" has wrong length: "
134 "expected: %u, actual: %u\n",
135 t
->utf8_str
, (unsigned int)t
->len
, (unsigned int)len
);
141 ret
= wind_utf8ucs4(t
->utf8_str
, u
, &len
);
143 printf("utf8 decode of \"%s\" should have succeeded\n",
149 printf("utf8 decode of \"%s\" has wrong length: "
150 "expected: %u, actual: %u\n",
151 t
->utf8_str
, (unsigned int)t
->len
, (unsigned int)len
);
155 if (memcmp(t
->u
, u
, len
* sizeof(uint32_t)) != 0) {
156 printf("utf8 decode of \"%s\" has wrong data\n",
161 if (t
->invalid_ucs2
== 0) {
163 ret
= wind_ucs4utf8(u
, len
, str
, &len2
);
165 printf("ucs4 decode of \"%s\" should have succeeded\n",
173 return failures
!= 0;