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. */
26 check (const uint8_t *input
, size_t input_length
, ucs4_t
*puc
)
30 /* Test recognition when at the beginning of the string. */
31 if (u8_prev (&uc
, input
+ input_length
, input
) != input
)
34 /* Test recognition when preceded by a 1-unit character. */
43 for (i
= 0; i
< input_length
; i
++)
46 if (u8_prev (&uc1
, ptr
+ input_length
, buf
) != ptr
)
52 /* Test recognition when preceded by a 2-unit character. */
62 for (i
= 0; i
< input_length
; i
++)
65 if (u8_prev (&uc1
, ptr
+ input_length
, buf
) != ptr
)
71 /* Test recognition when preceded by a 3-unit character. */
82 for (i
= 0; i
< input_length
; i
++)
85 if (u8_prev (&uc1
, ptr
+ input_length
, buf
) != ptr
)
91 /* Test recognition when preceded by a 4-unit character. */
103 for (i
= 0; i
< input_length
; i
++)
106 if (u8_prev (&uc1
, ptr
+ input_length
, buf
) != ptr
)
117 check_invalid (const uint8_t *input
, size_t input_length
)
121 /* Test recognition when at the beginning of the string. */
123 if (u8_prev (&uc
, input
+ input_length
, input
) != NULL
)
128 /* Test recognition when preceded by a 1-unit character. */
136 for (i
= 0; i
< input_length
; i
++)
140 if (u8_prev (&uc
, ptr
+ input_length
, buf
) != NULL
)
146 /* Test recognition when preceded by a 2-unit character. */
155 for (i
= 0; i
< input_length
; i
++)
159 if (u8_prev (&uc
, ptr
+ input_length
, buf
) != NULL
)
165 /* Test recognition when preceded by a 3-unit character. */
175 for (i
= 0; i
< input_length
; i
++)
179 if (u8_prev (&uc
, ptr
+ input_length
, buf
) != NULL
)
185 /* Test recognition when preceded by a 4-unit character. */
196 for (i
= 0; i
< input_length
; i
++)
200 if (u8_prev (&uc
, ptr
+ input_length
, buf
) != NULL
)
214 /* Test ISO 646 unit input. */
219 for (c
= 0; c
< 0x80; c
++)
223 ASSERT (check (buf
, 1, &uc
) == 0);
228 /* Test 2-byte character input. */
230 static const uint8_t input
[] = { 0xC3, 0x97 };
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 };
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 };
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);