1*ebfedea0SLionel Sambuc /* $NetBSD: test-utf8.c,v 1.1.1.1 2011/04/13 18:16:00 elric Exp $ */
2*ebfedea0SLionel Sambuc
3*ebfedea0SLionel Sambuc /*
4*ebfedea0SLionel Sambuc * Copyright (c) 2004 Kungliga Tekniska Högskolan
5*ebfedea0SLionel Sambuc * (Royal Institute of Technology, Stockholm, Sweden).
6*ebfedea0SLionel Sambuc * All rights reserved.
7*ebfedea0SLionel Sambuc *
8*ebfedea0SLionel Sambuc * Redistribution and use in source and binary forms, with or without
9*ebfedea0SLionel Sambuc * modification, are permitted provided that the following conditions
10*ebfedea0SLionel Sambuc * are met:
11*ebfedea0SLionel Sambuc *
12*ebfedea0SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
13*ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer.
14*ebfedea0SLionel Sambuc *
15*ebfedea0SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
16*ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
17*ebfedea0SLionel Sambuc * documentation and/or other materials provided with the distribution.
18*ebfedea0SLionel Sambuc *
19*ebfedea0SLionel Sambuc * 3. Neither the name of the Institute nor the names of its contributors
20*ebfedea0SLionel Sambuc * may be used to endorse or promote products derived from this software
21*ebfedea0SLionel Sambuc * without specific prior written permission.
22*ebfedea0SLionel Sambuc *
23*ebfedea0SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24*ebfedea0SLionel Sambuc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25*ebfedea0SLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26*ebfedea0SLionel Sambuc * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27*ebfedea0SLionel Sambuc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28*ebfedea0SLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29*ebfedea0SLionel Sambuc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30*ebfedea0SLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31*ebfedea0SLionel Sambuc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32*ebfedea0SLionel Sambuc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33*ebfedea0SLionel Sambuc * SUCH DAMAGE.
34*ebfedea0SLionel Sambuc */
35*ebfedea0SLionel Sambuc
36*ebfedea0SLionel Sambuc #ifdef HAVE_CONFIG_H
37*ebfedea0SLionel Sambuc #include <config.h>
38*ebfedea0SLionel Sambuc #endif
39*ebfedea0SLionel Sambuc #include <stdio.h>
40*ebfedea0SLionel Sambuc #include <string.h>
41*ebfedea0SLionel Sambuc #include <err.h>
42*ebfedea0SLionel Sambuc #include "windlocl.h"
43*ebfedea0SLionel Sambuc
44*ebfedea0SLionel Sambuc static const char *failing_testcases[] = {
45*ebfedea0SLionel Sambuc "\x80",
46*ebfedea0SLionel Sambuc "\xFF",
47*ebfedea0SLionel Sambuc "\xC0",
48*ebfedea0SLionel Sambuc "\xDF",
49*ebfedea0SLionel Sambuc "\xE0",
50*ebfedea0SLionel Sambuc "\xEF",
51*ebfedea0SLionel Sambuc "\xF0",
52*ebfedea0SLionel Sambuc "\xF7",
53*ebfedea0SLionel Sambuc "\xC0\x01",
54*ebfedea0SLionel Sambuc "\xC0\x7F",
55*ebfedea0SLionel Sambuc "\xC0\xFF",
56*ebfedea0SLionel Sambuc "\xC0\x80\x80",
57*ebfedea0SLionel Sambuc "\xE0\x01",
58*ebfedea0SLionel Sambuc "\xE0\x7F",
59*ebfedea0SLionel Sambuc "\xE0\x80",
60*ebfedea0SLionel Sambuc "\xE0\xFF",
61*ebfedea0SLionel Sambuc "\xE0\x80\x20",
62*ebfedea0SLionel Sambuc "\xE0\x80\xFF",
63*ebfedea0SLionel Sambuc "\xE0\x80\x80\x80",
64*ebfedea0SLionel Sambuc "\xF0\x01",
65*ebfedea0SLionel Sambuc "\xF0\x80",
66*ebfedea0SLionel Sambuc "\xF0\x80\x01",
67*ebfedea0SLionel Sambuc "\xF0\x80\x80",
68*ebfedea0SLionel Sambuc "\xF0\x80\x80\x01",
69*ebfedea0SLionel Sambuc "\xF0\x80\x80\xFF",
70*ebfedea0SLionel Sambuc NULL
71*ebfedea0SLionel Sambuc };
72*ebfedea0SLionel Sambuc
73*ebfedea0SLionel Sambuc #define MAX_LENGTH 10
74*ebfedea0SLionel Sambuc
75*ebfedea0SLionel Sambuc struct testcase {
76*ebfedea0SLionel Sambuc const char *utf8_str;
77*ebfedea0SLionel Sambuc size_t len;
78*ebfedea0SLionel Sambuc uint32_t u[MAX_LENGTH];
79*ebfedea0SLionel Sambuc int invalid_ucs2;
80*ebfedea0SLionel Sambuc };
81*ebfedea0SLionel Sambuc
82*ebfedea0SLionel Sambuc static const struct testcase testcases[] = {
83*ebfedea0SLionel Sambuc {"", 0, {0}},
84*ebfedea0SLionel Sambuc {"\x01", 1, {1}},
85*ebfedea0SLionel Sambuc {"\x7F", 1, {0x7F}},
86*ebfedea0SLionel Sambuc {"\x01\x7F", 2, {0x01, 0x7F}},
87*ebfedea0SLionel Sambuc {"\xC0\x80", 1, {0}},
88*ebfedea0SLionel Sambuc {"\xC0\x81", 1, {1}},
89*ebfedea0SLionel Sambuc {"\xC1\x80", 1, {0x40}},
90*ebfedea0SLionel Sambuc {"\xDF\xBF", 1, {0x7FF}},
91*ebfedea0SLionel Sambuc {"\xE0\x80\x80", 1, {0}},
92*ebfedea0SLionel Sambuc {"\xE0\x80\x81", 1, {1}},
93*ebfedea0SLionel Sambuc {"\xE0\x81\x80", 1, {0x40}},
94*ebfedea0SLionel Sambuc {"\xE1\x80\x80", 1, {0x1000}},
95*ebfedea0SLionel Sambuc {"\xEF\xBF\xBF", 1, {0xFFFF}},
96*ebfedea0SLionel Sambuc {"\xF0\x80\x80\x80", 1, {0}},
97*ebfedea0SLionel Sambuc {"\xF0\x80\x80\x81", 1, {1}},
98*ebfedea0SLionel Sambuc {"\xF0\x80\x81\x80", 1, {0x40}},
99*ebfedea0SLionel Sambuc {"\xF0\x81\x80\x80", 1, {0x1000}},
100*ebfedea0SLionel Sambuc {"\xF1\x80\x80\x80", 1, {0x40000}},
101*ebfedea0SLionel Sambuc {"\xF7\xBF\xBF\xBF", 1, {0X1FFFFF}, 1},
102*ebfedea0SLionel Sambuc };
103*ebfedea0SLionel Sambuc
104*ebfedea0SLionel Sambuc int
main(void)105*ebfedea0SLionel Sambuc main(void)
106*ebfedea0SLionel Sambuc {
107*ebfedea0SLionel Sambuc unsigned failures = 0;
108*ebfedea0SLionel Sambuc unsigned i;
109*ebfedea0SLionel Sambuc const char **s;
110*ebfedea0SLionel Sambuc int ret;
111*ebfedea0SLionel Sambuc size_t len, len2;
112*ebfedea0SLionel Sambuc uint32_t u[MAX_LENGTH];
113*ebfedea0SLionel Sambuc char str[MAX_LENGTH * 4];
114*ebfedea0SLionel Sambuc
115*ebfedea0SLionel Sambuc for (s = failing_testcases; *s != NULL; ++s) {
116*ebfedea0SLionel Sambuc len = MAX_LENGTH;
117*ebfedea0SLionel Sambuc ret = wind_utf8ucs4(*s, u, &len);
118*ebfedea0SLionel Sambuc if (ret == 0) {
119*ebfedea0SLionel Sambuc printf("utf8 decode of \"%s\" should have failed\n", *s);
120*ebfedea0SLionel Sambuc ++failures;
121*ebfedea0SLionel Sambuc }
122*ebfedea0SLionel Sambuc }
123*ebfedea0SLionel Sambuc
124*ebfedea0SLionel Sambuc for (i = 0; i < sizeof(testcases)/sizeof(testcases[0]); ++i) {
125*ebfedea0SLionel Sambuc const struct testcase *t = &testcases[i];
126*ebfedea0SLionel Sambuc
127*ebfedea0SLionel Sambuc ret = wind_utf8ucs4_length(t->utf8_str, &len);
128*ebfedea0SLionel Sambuc if (ret) {
129*ebfedea0SLionel Sambuc printf("utf8ucs4 length of \"%s\" should have succeeded\n",
130*ebfedea0SLionel Sambuc t->utf8_str);
131*ebfedea0SLionel Sambuc ++failures;
132*ebfedea0SLionel Sambuc continue;
133*ebfedea0SLionel Sambuc }
134*ebfedea0SLionel Sambuc if (len != t->len) {
135*ebfedea0SLionel Sambuc printf("utf8ucs4_length of \"%s\" has wrong length: "
136*ebfedea0SLionel Sambuc "expected: %u, actual: %u\n",
137*ebfedea0SLionel Sambuc t->utf8_str, (unsigned int)t->len, (unsigned int)len);
138*ebfedea0SLionel Sambuc ++failures;
139*ebfedea0SLionel Sambuc continue;
140*ebfedea0SLionel Sambuc }
141*ebfedea0SLionel Sambuc
142*ebfedea0SLionel Sambuc len = MAX_LENGTH;
143*ebfedea0SLionel Sambuc ret = wind_utf8ucs4(t->utf8_str, u, &len);
144*ebfedea0SLionel Sambuc if (ret) {
145*ebfedea0SLionel Sambuc printf("utf8 decode of \"%s\" should have succeeded\n",
146*ebfedea0SLionel Sambuc t->utf8_str);
147*ebfedea0SLionel Sambuc ++failures;
148*ebfedea0SLionel Sambuc continue;
149*ebfedea0SLionel Sambuc }
150*ebfedea0SLionel Sambuc if (len != t->len) {
151*ebfedea0SLionel Sambuc printf("utf8 decode of \"%s\" has wrong length: "
152*ebfedea0SLionel Sambuc "expected: %u, actual: %u\n",
153*ebfedea0SLionel Sambuc t->utf8_str, (unsigned int)t->len, (unsigned int)len);
154*ebfedea0SLionel Sambuc ++failures;
155*ebfedea0SLionel Sambuc continue;
156*ebfedea0SLionel Sambuc }
157*ebfedea0SLionel Sambuc if (memcmp(t->u, u, len * sizeof(uint32_t)) != 0) {
158*ebfedea0SLionel Sambuc printf("utf8 decode of \"%s\" has wrong data\n",
159*ebfedea0SLionel Sambuc t->utf8_str);
160*ebfedea0SLionel Sambuc ++failures;
161*ebfedea0SLionel Sambuc continue;
162*ebfedea0SLionel Sambuc }
163*ebfedea0SLionel Sambuc if (t->invalid_ucs2 == 0) {
164*ebfedea0SLionel Sambuc len2 = sizeof(str);
165*ebfedea0SLionel Sambuc ret = wind_ucs4utf8(u, len, str, &len2);
166*ebfedea0SLionel Sambuc if (ret) {
167*ebfedea0SLionel Sambuc printf("ucs4 decode of \"%s\" should have succeeded\n",
168*ebfedea0SLionel Sambuc t->utf8_str);
169*ebfedea0SLionel Sambuc ++failures;
170*ebfedea0SLionel Sambuc continue;
171*ebfedea0SLionel Sambuc }
172*ebfedea0SLionel Sambuc }
173*ebfedea0SLionel Sambuc }
174*ebfedea0SLionel Sambuc
175*ebfedea0SLionel Sambuc return failures != 0;
176*ebfedea0SLionel Sambuc }
177