1 | /*
|
---|
2 | * Copyright (c) 2004 Kungliga Tekniska Högskolan
|
---|
3 | * (Royal Institute of Technology, Stockholm, Sweden).
|
---|
4 | * All rights reserved.
|
---|
5 | *
|
---|
6 | * Redistribution and use in source and binary forms, with or without
|
---|
7 | * modification, are permitted provided that the following conditions
|
---|
8 | * are met:
|
---|
9 | *
|
---|
10 | * 1. Redistributions of source code must retain the above copyright
|
---|
11 | * notice, this list of conditions and the following disclaimer.
|
---|
12 | *
|
---|
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.
|
---|
16 | *
|
---|
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.
|
---|
20 | *
|
---|
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
|
---|
31 | * SUCH DAMAGE.
|
---|
32 | */
|
---|
33 |
|
---|
34 | #ifdef HAVE_CONFIG_H
|
---|
35 | #include <config.h>
|
---|
36 | #endif
|
---|
37 | #include <stdio.h>
|
---|
38 | #include <string.h>
|
---|
39 | #include <err.h>
|
---|
40 | #include "windlocl.h"
|
---|
41 |
|
---|
42 | static const char *failing_testcases[] = {
|
---|
43 | "\x80",
|
---|
44 | "\xFF",
|
---|
45 | "\xC0",
|
---|
46 | "\xDF",
|
---|
47 | "\xE0",
|
---|
48 | "\xEF",
|
---|
49 | "\xF0",
|
---|
50 | "\xF7",
|
---|
51 | "\xC0\x01",
|
---|
52 | "\xC0\x7F",
|
---|
53 | "\xC0\xFF",
|
---|
54 | "\xC0\x80\x80",
|
---|
55 | "\xE0\x01",
|
---|
56 | "\xE0\x7F",
|
---|
57 | "\xE0\x80",
|
---|
58 | "\xE0\xFF",
|
---|
59 | "\xE0\x80\x20",
|
---|
60 | "\xE0\x80\xFF",
|
---|
61 | "\xE0\x80\x80\x80",
|
---|
62 | "\xF0\x01",
|
---|
63 | "\xF0\x80",
|
---|
64 | "\xF0\x80\x01",
|
---|
65 | "\xF0\x80\x80",
|
---|
66 | "\xF0\x80\x80\x01",
|
---|
67 | "\xF0\x80\x80\xFF",
|
---|
68 | NULL
|
---|
69 | };
|
---|
70 |
|
---|
71 | #define MAX_LENGTH 10
|
---|
72 |
|
---|
73 | struct testcase {
|
---|
74 | const char *utf8_str;
|
---|
75 | size_t len;
|
---|
76 | uint32_t u[MAX_LENGTH];
|
---|
77 | int invalid_ucs2;
|
---|
78 | };
|
---|
79 |
|
---|
80 | static const struct testcase testcases[] = {
|
---|
81 | {"", 0, {0}},
|
---|
82 | {"\x01", 1, {1}},
|
---|
83 | {"\x7F", 1, {0x7F}},
|
---|
84 | {"\x01\x7F", 2, {0x01, 0x7F}},
|
---|
85 | {"\xC0\x80", 1, {0}},
|
---|
86 | {"\xC0\x81", 1, {1}},
|
---|
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},
|
---|
100 | };
|
---|
101 |
|
---|
102 | int
|
---|
103 | main(void)
|
---|
104 | {
|
---|
105 | unsigned failures = 0;
|
---|
106 | unsigned i;
|
---|
107 | const char **s;
|
---|
108 | int ret;
|
---|
109 | size_t len, len2;
|
---|
110 | uint32_t u[MAX_LENGTH];
|
---|
111 | char str[MAX_LENGTH * 4];
|
---|
112 |
|
---|
113 | for (s = failing_testcases; *s != NULL; ++s) {
|
---|
114 | len = MAX_LENGTH;
|
---|
115 | ret = wind_utf8ucs4(*s, u, &len);
|
---|
116 | if (ret == 0) {
|
---|
117 | printf("utf8 decode of \"%s\" should have failed\n", *s);
|
---|
118 | ++failures;
|
---|
119 | }
|
---|
120 | }
|
---|
121 |
|
---|
122 | for (i = 0; i < sizeof(testcases)/sizeof(testcases[0]); ++i) {
|
---|
123 | const struct testcase *t = &testcases[i];
|
---|
124 |
|
---|
125 | ret = wind_utf8ucs4_length(t->utf8_str, &len);
|
---|
126 | if (ret) {
|
---|
127 | printf("utf8ucs4 length of \"%s\" should have succeeded\n",
|
---|
128 | t->utf8_str);
|
---|
129 | ++failures;
|
---|
130 | continue;
|
---|
131 | }
|
---|
132 | if (len != t->len) {
|
---|
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);
|
---|
136 | ++failures;
|
---|
137 | continue;
|
---|
138 | }
|
---|
139 |
|
---|
140 | len = MAX_LENGTH;
|
---|
141 | ret = wind_utf8ucs4(t->utf8_str, u, &len);
|
---|
142 | if (ret) {
|
---|
143 | printf("utf8 decode of \"%s\" should have succeeded\n",
|
---|
144 | t->utf8_str);
|
---|
145 | ++failures;
|
---|
146 | continue;
|
---|
147 | }
|
---|
148 | if (len != t->len) {
|
---|
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);
|
---|
152 | ++failures;
|
---|
153 | continue;
|
---|
154 | }
|
---|
155 | if (memcmp(t->u, u, len * sizeof(uint32_t)) != 0) {
|
---|
156 | printf("utf8 decode of \"%s\" has wrong data\n",
|
---|
157 | t->utf8_str);
|
---|
158 | ++failures;
|
---|
159 | continue;
|
---|
160 | }
|
---|
161 | if (t->invalid_ucs2 == 0) {
|
---|
162 | len2 = sizeof(str);
|
---|
163 | ret = wind_ucs4utf8(u, len, str, &len2);
|
---|
164 | if (ret) {
|
---|
165 | printf("ucs4 decode of \"%s\" should have succeeded\n",
|
---|
166 | t->utf8_str);
|
---|
167 | ++failures;
|
---|
168 | continue;
|
---|
169 | }
|
---|
170 | }
|
---|
171 | }
|
---|
172 |
|
---|
173 | return failures != 0;
|
---|
174 | }
|
---|