Line data Source code
1 : /* Test of u8_mbtoucr() function.
2 : Copyright (C) 2010-2020 Free Software Foundation, Inc.
3 :
4 : This program is free software: you can redistribute it and/or modify
5 : it under the terms of the GNU General Public License as published by
6 : the Free Software Foundation; either version 3 of the License, or
7 : (at your option) any later version.
8 :
9 : This program is distributed in the hope that it will be useful,
10 : but WITHOUT ANY WARRANTY; without even the implied warranty of
11 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 : GNU General Public License for more details.
13 :
14 : You should have received a copy of the GNU General Public License
15 : along with this program. If not, see <https://www.gnu.org/licenses/>. */
16 :
17 : /* Written by Bruno Haible <bruno@clisp.org>, 2010. */
18 :
19 : #include <config.h>
20 :
21 : #include "unistr.h"
22 :
23 : #include "macros.h"
24 :
25 : int
26 1 : main ()
27 : {
28 : ucs4_t uc;
29 : int ret;
30 :
31 : /* Test NUL unit input. */
32 : {
33 : static const uint8_t input[] = "";
34 1 : uc = 0xBADFACE;
35 1 : ret = u8_mbtoucr (&uc, input, 1);
36 1 : ASSERT (ret == 1);
37 1 : ASSERT (uc == 0);
38 : }
39 :
40 : /* Test ISO 646 unit input. */
41 : {
42 : ucs4_t c;
43 : uint8_t buf[1];
44 :
45 129 : for (c = 0; c < 0x80; c++)
46 : {
47 128 : buf[0] = c;
48 128 : uc = 0xBADFACE;
49 128 : ret = u8_mbtoucr (&uc, buf, 1);
50 128 : ASSERT (ret == 1);
51 128 : ASSERT (uc == c);
52 : }
53 : }
54 :
55 : /* Test 2-byte character input. */
56 : {
57 : static const uint8_t input[] = { 0xC3, 0x97 };
58 1 : uc = 0xBADFACE;
59 1 : ret = u8_mbtoucr (&uc, input, 2);
60 1 : ASSERT (ret == 2);
61 1 : ASSERT (uc == 0x00D7);
62 : }
63 :
64 : /* Test 3-byte character input. */
65 : {
66 : static const uint8_t input[] = { 0xE2, 0x82, 0xAC };
67 1 : uc = 0xBADFACE;
68 1 : ret = u8_mbtoucr (&uc, input, 3);
69 1 : ASSERT (ret == 3);
70 1 : ASSERT (uc == 0x20AC);
71 : }
72 :
73 : /* Test 4-byte character input. */
74 : {
75 : static const uint8_t input[] = { 0xF4, 0x8F, 0xBF, 0xBD };
76 1 : uc = 0xBADFACE;
77 1 : ret = u8_mbtoucr (&uc, input, 4);
78 1 : ASSERT (ret == 4);
79 1 : ASSERT (uc == 0x10FFFD);
80 : }
81 :
82 : /* Test incomplete/invalid 1-byte input. */
83 : {
84 : static const uint8_t input[] = { 0xC1 };
85 1 : uc = 0xBADFACE;
86 1 : ret = u8_mbtoucr (&uc, input, 1);
87 1 : ASSERT (ret == -1);
88 1 : ASSERT (uc == 0xFFFD);
89 : }
90 : {
91 : static const uint8_t input[] = { 0xC3 };
92 1 : uc = 0xBADFACE;
93 1 : ret = u8_mbtoucr (&uc, input, 1);
94 1 : ASSERT (ret == -2);
95 1 : ASSERT (uc == 0xFFFD);
96 : }
97 : {
98 : static const uint8_t input[] = { 0xE2 };
99 1 : uc = 0xBADFACE;
100 1 : ret = u8_mbtoucr (&uc, input, 1);
101 1 : ASSERT (ret == -2);
102 1 : ASSERT (uc == 0xFFFD);
103 : }
104 : {
105 : static const uint8_t input[] = { 0xF4 };
106 1 : uc = 0xBADFACE;
107 1 : ret = u8_mbtoucr (&uc, input, 1);
108 1 : ASSERT (ret == -2);
109 1 : ASSERT (uc == 0xFFFD);
110 : }
111 : {
112 : static const uint8_t input[] = { 0xFE };
113 1 : uc = 0xBADFACE;
114 1 : ret = u8_mbtoucr (&uc, input, 1);
115 1 : ASSERT (ret == -1);
116 1 : ASSERT (uc == 0xFFFD);
117 : }
118 :
119 : /* Test incomplete/invalid 2-byte input. */
120 : {
121 : static const uint8_t input[] = { 0xE0, 0x9F };
122 1 : uc = 0xBADFACE;
123 1 : ret = u8_mbtoucr (&uc, input, 2);
124 1 : ASSERT (ret == -1);
125 1 : ASSERT (uc == 0xFFFD);
126 : }
127 : {
128 : static const uint8_t input[] = { 0xE2, 0x82 };
129 1 : uc = 0xBADFACE;
130 1 : ret = u8_mbtoucr (&uc, input, 2);
131 1 : ASSERT (ret == -2);
132 1 : ASSERT (uc == 0xFFFD);
133 : }
134 : {
135 : static const uint8_t input[] = { 0xE2, 0xD0 };
136 1 : uc = 0xBADFACE;
137 1 : ret = u8_mbtoucr (&uc, input, 2);
138 1 : ASSERT (ret == -1);
139 1 : ASSERT (uc == 0xFFFD);
140 : }
141 : {
142 : static const uint8_t input[] = { 0xF0, 0x8F };
143 1 : uc = 0xBADFACE;
144 1 : ret = u8_mbtoucr (&uc, input, 2);
145 1 : ASSERT (ret == -1);
146 1 : ASSERT (uc == 0xFFFD);
147 : }
148 : {
149 : static const uint8_t input[] = { 0xF3, 0x8F };
150 1 : uc = 0xBADFACE;
151 1 : ret = u8_mbtoucr (&uc, input, 2);
152 1 : ASSERT (ret == -2);
153 1 : ASSERT (uc == 0xFFFD);
154 : }
155 : {
156 : static const uint8_t input[] = { 0xF3, 0xD0 };
157 1 : uc = 0xBADFACE;
158 1 : ret = u8_mbtoucr (&uc, input, 2);
159 1 : ASSERT (ret == -1);
160 1 : ASSERT (uc == 0xFFFD);
161 : }
162 :
163 : /* Test incomplete/invalid 3-byte input. */
164 : {
165 : static const uint8_t input[] = { 0xF3, 0x8F, 0xBF };
166 1 : uc = 0xBADFACE;
167 1 : ret = u8_mbtoucr (&uc, input, 3);
168 1 : ASSERT (ret == -2);
169 1 : ASSERT (uc == 0xFFFD);
170 : }
171 : {
172 : static const uint8_t input[] = { 0xF3, 0xD0, 0xBF };
173 1 : uc = 0xBADFACE;
174 1 : ret = u8_mbtoucr (&uc, input, 3);
175 1 : ASSERT (ret == -1);
176 1 : ASSERT (uc == 0xFFFD);
177 : }
178 : {
179 : static const uint8_t input[] = { 0xF3, 0x8F, 0xD0 };
180 1 : uc = 0xBADFACE;
181 1 : ret = u8_mbtoucr (&uc, input, 3);
182 1 : ASSERT (ret == -1);
183 1 : ASSERT (uc == 0xFFFD);
184 : }
185 :
186 1 : return 0;
187 : }
|