Add missing ',' between parameters [HEIMDAL-599]
[heimdal.git] / lib / wind / test-utf8.c
blob5fa319b02457804c1a0b087de574cd9619225307
1 /*
2 * Copyright (c) 2004 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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
31 * SUCH DAMAGE.
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"
42 RCSID("$Id$");
44 static const char *failing_testcases[] = {
45 "\x80",
46 "\xFF",
47 "\xC0",
48 "\xDF",
49 "\xE0",
50 "\xEF",
51 "\xF0",
52 "\xF7",
53 "\xC0\x01",
54 "\xC0\x7F",
55 "\xC0\xFF",
56 "\xC0\x80\x80",
57 "\xE0\x01",
58 "\xE0\x7F",
59 "\xE0\x80",
60 "\xE0\xFF",
61 "\xE0\x80\x20",
62 "\xE0\x80\xFF",
63 "\xE0\x80\x80\x80",
64 "\xF0\x01",
65 "\xF0\x80",
66 "\xF0\x80\x01",
67 "\xF0\x80\x80",
68 "\xF0\x80\x80\x01",
69 "\xF0\x80\x80\xFF",
70 NULL
73 #define MAX_LENGTH 10
75 struct testcase {
76 const char *utf8_str;
77 size_t len;
78 uint32_t u[MAX_LENGTH];
79 int invalid_ucs2;
82 static const struct testcase testcases[] = {
83 {"", 0, {}},
84 {"\x01", 1, {1}},
85 {"\x7F", 1, {0x7F}},
86 {"\x01\x7F", 2, {0x01, 0x7F}},
87 {"\xC0\x80", 1, {0}},
88 {"\xC0\x81", 1, {1}},
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},
105 main(void)
107 unsigned failures = 0;
108 unsigned i;
109 const char **s;
110 int ret;
111 size_t len, len2;
112 uint32_t u[MAX_LENGTH];
113 char str[MAX_LENGTH * 4];
115 for (s = failing_testcases; *s != NULL; ++s) {
116 len = MAX_LENGTH;
117 ret = wind_utf8ucs4(*s, u, &len);
118 if (ret == 0) {
119 printf("utf8 decode of \"%s\" should have failed\n", *s);
120 ++failures;
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);
128 if (ret) {
129 printf("utf8ucs4 length of \"%s\" should have succeeded\n",
130 t->utf8_str);
131 ++failures;
132 continue;
134 if (len != t->len) {
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);
138 ++failures;
139 continue;
142 len = MAX_LENGTH;
143 ret = wind_utf8ucs4(t->utf8_str, u, &len);
144 if (ret) {
145 printf("utf8 decode of \"%s\" should have succeeded\n",
146 t->utf8_str);
147 ++failures;
148 continue;
150 if (len != t->len) {
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);
154 ++failures;
155 continue;
157 if (memcmp(t->u, u, len * sizeof(uint32_t)) != 0) {
158 printf("utf8 decode of \"%s\" has wrong data\n",
159 t->utf8_str);
160 ++failures;
161 continue;
163 if (t->invalid_ucs2 == 0) {
164 len2 = sizeof(str);
165 ret = wind_ucs4utf8(u, len, str, &len2);
166 if (ret) {
167 printf("ucs4 decode of \"%s\" should have succeeded\n",
168 t->utf8_str);
169 ++failures;
170 continue;
175 return failures != 0;