Support compiling without -loldnames on native Windows.
[gnulib.git] / tests / unistr / test-u8-prev.c
blob8b5142b9a4dd1331ea234c1a29006f7e431384a8
1 /* Test of u8_prev() function.
2 Copyright (C) 2010-2020 Free Software Foundation, Inc.
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.
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.
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/>. */
17 /* Written by Bruno Haible <bruno@clisp.org>, 2010. */
19 #include <config.h>
21 #include "unistr.h"
23 #include "macros.h"
25 static int
26 check (const uint8_t *input, size_t input_length, ucs4_t *puc)
28 ucs4_t uc;
30 /* Test recognition when at the beginning of the string. */
31 if (u8_prev (&uc, input + input_length, input) != input)
32 return 1;
34 /* Test recognition when preceded by a 1-unit character. */
36 uint8_t buf[100];
37 uint8_t *ptr;
38 size_t i;
39 ucs4_t uc1;
41 ptr = buf;
42 *ptr++ = 'x';
43 for (i = 0; i < input_length; i++)
44 ptr[i] = input[i];
46 if (u8_prev (&uc1, ptr + input_length, buf) != ptr)
47 return 2;
48 if (uc1 != uc)
49 return 3;
52 /* Test recognition when preceded by a 2-unit character. */
54 uint8_t buf[100];
55 uint8_t *ptr;
56 size_t i;
57 ucs4_t uc1;
59 ptr = buf;
60 *ptr++ = 0xC3;
61 *ptr++ = 0x97;
62 for (i = 0; i < input_length; i++)
63 ptr[i] = input[i];
65 if (u8_prev (&uc1, ptr + input_length, buf) != ptr)
66 return 4;
67 if (uc1 != uc)
68 return 5;
71 /* Test recognition when preceded by a 3-unit character. */
73 uint8_t buf[100];
74 uint8_t *ptr;
75 size_t i;
76 ucs4_t uc1;
78 ptr = buf;
79 *ptr++ = 0xE2;
80 *ptr++ = 0x84;
81 *ptr++ = 0x82;
82 for (i = 0; i < input_length; i++)
83 ptr[i] = input[i];
85 if (u8_prev (&uc1, ptr + input_length, buf) != ptr)
86 return 6;
87 if (uc1 != uc)
88 return 7;
91 /* Test recognition when preceded by a 4-unit character. */
93 uint8_t buf[100];
94 uint8_t *ptr;
95 size_t i;
96 ucs4_t uc1;
98 ptr = buf;
99 *ptr++ = 0xF0;
100 *ptr++ = 0x9D;
101 *ptr++ = 0x94;
102 *ptr++ = 0x9E;
103 for (i = 0; i < input_length; i++)
104 ptr[i] = input[i];
106 if (u8_prev (&uc1, ptr + input_length, buf) != ptr)
107 return 8;
108 if (uc1 != uc)
109 return 9;
112 *puc = uc;
113 return 0;
116 static int
117 check_invalid (const uint8_t *input, size_t input_length)
119 ucs4_t uc;
121 /* Test recognition when at the beginning of the string. */
122 uc = 0xBADFACE;
123 if (u8_prev (&uc, input + input_length, input) != NULL)
124 return 1;
125 if (uc != 0xBADFACE)
126 return 2;
128 /* Test recognition when preceded by a 1-unit character. */
130 uint8_t buf[100];
131 uint8_t *ptr;
132 size_t i;
134 ptr = buf;
135 *ptr++ = 'x';
136 for (i = 0; i < input_length; i++)
137 ptr[i] = input[i];
139 uc = 0xBADFACE;
140 if (u8_prev (&uc, ptr + input_length, buf) != NULL)
141 return 3;
142 if (uc != 0xBADFACE)
143 return 4;
146 /* Test recognition when preceded by a 2-unit character. */
148 uint8_t buf[100];
149 uint8_t *ptr;
150 size_t i;
152 ptr = buf;
153 *ptr++ = 0xC3;
154 *ptr++ = 0x97;
155 for (i = 0; i < input_length; i++)
156 ptr[i] = input[i];
158 uc = 0xBADFACE;
159 if (u8_prev (&uc, ptr + input_length, buf) != NULL)
160 return 5;
161 if (uc != 0xBADFACE)
162 return 6;
165 /* Test recognition when preceded by a 3-unit character. */
167 uint8_t buf[100];
168 uint8_t *ptr;
169 size_t i;
171 ptr = buf;
172 *ptr++ = 0xE2;
173 *ptr++ = 0x84;
174 *ptr++ = 0x82;
175 for (i = 0; i < input_length; i++)
176 ptr[i] = input[i];
178 uc = 0xBADFACE;
179 if (u8_prev (&uc, ptr + input_length, buf) != NULL)
180 return 7;
181 if (uc != 0xBADFACE)
182 return 8;
185 /* Test recognition when preceded by a 4-unit character. */
187 uint8_t buf[100];
188 uint8_t *ptr;
189 size_t i;
191 ptr = buf;
192 *ptr++ = 0xF0;
193 *ptr++ = 0x9D;
194 *ptr++ = 0x94;
195 *ptr++ = 0x9E;
196 for (i = 0; i < input_length; i++)
197 ptr[i] = input[i];
199 uc = 0xBADFACE;
200 if (u8_prev (&uc, ptr + input_length, buf) != NULL)
201 return 9;
202 if (uc != 0xBADFACE)
203 return 10;
206 return 0;
210 main ()
212 ucs4_t uc;
214 /* Test ISO 646 unit input. */
216 ucs4_t c;
217 uint8_t buf[1];
219 for (c = 0; c < 0x80; c++)
221 buf[0] = c;
222 uc = 0xBADFACE;
223 ASSERT (check (buf, 1, &uc) == 0);
224 ASSERT (uc == c);
228 /* Test 2-byte character input. */
230 static const uint8_t input[] = { 0xC3, 0x97 };
231 uc = 0xBADFACE;
232 ASSERT (check (input, SIZEOF (input), &uc) == 0);
233 ASSERT (uc == 0x00D7);
236 /* Test 3-byte character input. */
238 static const uint8_t input[] = { 0xE2, 0x82, 0xAC };
239 uc = 0xBADFACE;
240 ASSERT (check (input, SIZEOF (input), &uc) == 0);
241 ASSERT (uc == 0x20AC);
244 /* Test 4-byte character input. */
246 static const uint8_t input[] = { 0xF4, 0x8F, 0xBF, 0xBD };
247 uc = 0xBADFACE;
248 ASSERT (check (input, SIZEOF (input), &uc) == 0);
249 ASSERT (uc == 0x10FFFD);
252 /* Test incomplete/invalid 1-byte input. */
254 static const uint8_t input[] = { 0xC1 };
255 ASSERT (check_invalid (input, SIZEOF (input)) == 0);
258 static const uint8_t input[] = { 0xC3 };
259 ASSERT (check_invalid (input, SIZEOF (input)) == 0);
262 static const uint8_t input[] = { 0xE2 };
263 ASSERT (check_invalid (input, SIZEOF (input)) == 0);
266 static const uint8_t input[] = { 0xF4 };
267 ASSERT (check_invalid (input, SIZEOF (input)) == 0);
270 static const uint8_t input[] = { 0xFE };
271 ASSERT (check_invalid (input, SIZEOF (input)) == 0);
274 /* Test incomplete/invalid 2-byte input. */
276 static const uint8_t input[] = { 0xE0, 0x9F };
277 ASSERT (check_invalid (input, SIZEOF (input)) == 0);
280 static const uint8_t input[] = { 0xE2, 0x82 };
281 ASSERT (check_invalid (input, SIZEOF (input)) == 0);
284 static const uint8_t input[] = { 0xE2, 0xD0 };
285 ASSERT (check_invalid (input, SIZEOF (input)) == 0);
288 static const uint8_t input[] = { 0xF0, 0x8F };
289 ASSERT (check_invalid (input, SIZEOF (input)) == 0);
292 static const uint8_t input[] = { 0xF3, 0x8F };
293 ASSERT (check_invalid (input, SIZEOF (input)) == 0);
296 static const uint8_t input[] = { 0xF3, 0xD0 };
297 ASSERT (check_invalid (input, SIZEOF (input)) == 0);
300 /* Test incomplete/invalid 3-byte input. */
302 static const uint8_t input[] = { 0xF3, 0x8F, 0xBF };
303 ASSERT (check_invalid (input, SIZEOF (input)) == 0);
306 static const uint8_t input[] = { 0xF3, 0xE4, 0xBF };
307 ASSERT (check_invalid (input, SIZEOF (input)) == 0);
310 static const uint8_t input[] = { 0xF3, 0x8F, 0xD0 };
311 ASSERT (check_invalid (input, SIZEOF (input)) == 0);
314 return 0;