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
44 static const char *failing_testcases
[] = {
78 uint32_t u
[MAX_LENGTH
];
82 static const struct testcase testcases
[] = {
86 {"\x01\x7F", 2, {0x01, 0x7F}},
89 {"\xC1\x80", 1, {0x40}},
90 {"\xDF\xBF", 1, {0x7FF}},
91 {"\xE0\x80\x80", 1, {0}},
92 {"\xE0\x80\x81", 1, {1}},
93 {"\xE0\x81\x80", 1, {0x40}},
94 {"\xE1\x80\x80", 1, {0x1000}},
95 {"\xEF\xBF\xBF", 1, {0xFFFF}},
96 {"\xF0\x80\x80\x80", 1, {0}},
97 {"\xF0\x80\x80\x81", 1, {1}},
98 {"\xF0\x80\x81\x80", 1, {0x40}},
99 {"\xF0\x81\x80\x80", 1, {0x1000}},
100 {"\xF1\x80\x80\x80", 1, {0x40000}},
101 {"\xF7\xBF\xBF\xBF", 1, {0X1FFFFF}, 1},
107 unsigned failures
= 0;
112 uint32_t u
[MAX_LENGTH
];
113 char str
[MAX_LENGTH
* 4];
115 for (s
= failing_testcases
; *s
!= NULL
; ++s
) {
117 ret
= wind_utf8ucs4(*s
, u
, &len
);
119 printf("utf8 decode of \"%s\" should have failed\n", *s
);
124 for (i
= 0; i
< sizeof(testcases
)/sizeof(testcases
[0]); ++i
) {
125 const struct testcase
*t
= &testcases
[i
];
127 ret
= wind_utf8ucs4_length(t
->utf8_str
, &len
);
129 printf("utf8ucs4 length of \"%s\" should have succeeded\n",
135 printf("utf8ucs4_length of \"%s\" has wrong length: "
136 "expected: %u, actual: %u\n",
137 t
->utf8_str
, (unsigned int)t
->len
, (unsigned int)len
);
143 ret
= wind_utf8ucs4(t
->utf8_str
, u
, &len
);
145 printf("utf8 decode of \"%s\" should have succeeded\n",
151 printf("utf8 decode of \"%s\" has wrong length: "
152 "expected: %u, actual: %u\n",
153 t
->utf8_str
, (unsigned int)t
->len
, (unsigned int)len
);
157 if (memcmp(t
->u
, u
, len
* sizeof(uint32_t)) != 0) {
158 printf("utf8 decode of \"%s\" has wrong data\n",
163 if (t
->invalid_ucs2
== 0) {
165 ret
= wind_ucs4utf8(u
, len
, str
, &len2
);
167 printf("ucs4 decode of \"%s\" should have succeeded\n",
175 return failures
!= 0;