1 /* Test of u8_mbtouc() function.
2 Copyright (C) 2010-2014 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 <http://www.gnu.org/licenses/>. */
17 /* Written by Bruno Haible <bruno@clisp.org>, 2010. */
25 #include "test-u8-mbtouc.h"
28 test_safe_function (int (*my_u8_mbtouc) (ucs4_t *, const uint8_t *, size_t))
33 /* Test behaviour required by ISO 10646-1, sections R.7 and 2.3c, namely,
34 that a "malformed sequence" is interpreted in the same way as
35 "a character that is outside the adopted subset".
37 Markus Kuhn: UTF-8 decoder capability and stress test
38 <http://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-test.txt>
39 <http://www.w3.org/2001/06/utf-8-wrong/UTF-8-test.html>
41 /* 3.1. Test that each unexpected continuation byte is signalled as a
42 malformed sequence of its own. */
44 static const uint8_t input[] = { '"', 0x80, 0xBF, 0x80, 0xBF, '"' };
46 ret = my_u8_mbtouc (&uc, input, 6);
48 ASSERT (uc == 0x0022);
50 ret = my_u8_mbtouc (&uc, input + 1, 5);
52 ASSERT (uc == 0xFFFD);
54 ret = my_u8_mbtouc (&uc, input + 2, 4);
56 ASSERT (uc == 0xFFFD);
58 ret = my_u8_mbtouc (&uc, input + 3, 3);
60 ASSERT (uc == 0xFFFD);
62 ret = my_u8_mbtouc (&uc, input + 4, 2);
64 ASSERT (uc == 0xFFFD);
66 ret = my_u8_mbtouc (&uc, input + 5, 1);
68 ASSERT (uc == 0x0022);
70 /* 3.2. Lonely start characters. */
75 for (c = 0xC0; c <= 0xFF; c++)
81 ret = my_u8_mbtouc (&uc, input, 2);
83 ASSERT (uc == 0xFFFD);
86 /* 3.3. Sequences with last continuation byte missing. */
87 /* 3.3.1. 2-byte sequence with last byte missing. */
89 static const uint8_t input[] = { '"', 0xC0, '"' };
91 ret = my_u8_mbtouc (&uc, input, 3);
93 ASSERT (uc == 0x0022);
95 ret = my_u8_mbtouc (&uc, input + 1, 2);
97 ASSERT (uc == 0xFFFD);
99 ret = my_u8_mbtouc (&uc, input + 2, 1);
101 ASSERT (uc == 0x0022);
103 /* 3.3.6. 2-byte sequence with last byte missing. */
105 static const uint8_t input[] = { '"', 0xDF, '"' };
107 ret = my_u8_mbtouc (&uc, input, 3);
109 ASSERT (uc == 0x0022);
111 ret = my_u8_mbtouc (&uc, input + 1, 2);
113 ASSERT (uc == 0xFFFD);
115 ret = my_u8_mbtouc (&uc, input + 2, 1);
117 ASSERT (uc == 0x0022);
119 /* 3.3.2. 3-byte sequence with last byte missing. */
121 static const uint8_t input[] = { '"', 0xE0, 0x80, '"' };
123 ret = my_u8_mbtouc (&uc, input, 4);
125 ASSERT (uc == 0x0022);
127 ret = my_u8_mbtouc (&uc, input + 1, 3);
129 ASSERT (uc == 0xFFFD);
131 ret = my_u8_mbtouc (&uc, input + 3, 1);
133 ASSERT (uc == 0x0022);
135 /* 3.3.7. 3-byte sequence with last byte missing. */
137 static const uint8_t input[] = { '"', 0xEF, 0xBF, '"' };
139 ret = my_u8_mbtouc (&uc, input, 4);
141 ASSERT (uc == 0x0022);
143 ret = my_u8_mbtouc (&uc, input + 1, 3);
145 ASSERT (uc == 0xFFFD);
147 ret = my_u8_mbtouc (&uc, input + 3, 1);
149 ASSERT (uc == 0x0022);
151 /* 3.3.3. 4-byte sequence with last byte missing. */
153 static const uint8_t input[] = { '"', 0xF0, 0x80, 0x80, '"' };
155 ret = my_u8_mbtouc (&uc, input, 5);
157 ASSERT (uc == 0x0022);
159 ret = my_u8_mbtouc (&uc, input + 1, 4);
161 ASSERT (uc == 0xFFFD);
163 ret = my_u8_mbtouc (&uc, input + 4, 1);
165 ASSERT (uc == 0x0022);
167 /* 3.3.8. 4-byte sequence with last byte missing. */
169 static const uint8_t input[] = { '"', 0xF7, 0xBF, 0xBF, '"' };
171 ret = my_u8_mbtouc (&uc, input, 5);
173 ASSERT (uc == 0x0022);
175 ret = my_u8_mbtouc (&uc, input + 1, 4);
177 ASSERT (uc == 0xFFFD);
179 ret = my_u8_mbtouc (&uc, input + 4, 1);
181 ASSERT (uc == 0x0022);
188 test_function (u8_mbtouc);
189 test_safe_function (u8_mbtouc);