1/* Test of u8_mbtouc() and u8_mbtouc_unsafe() functions.
2   Copyright (C) 2010 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 <http://www.gnu.org/licenses/>.  */
16
17/* Written by Bruno Haible <bruno@clisp.org>, 2010.  */
18
19static void
20test_function (int (*my_u8_mbtouc) (ucs4_t *, const uint8_t *, size_t))
21{
22  ucs4_t uc;
23  int ret;
24
25  /* Test NUL unit input.  */
26  {
27    static const uint8_t input[] = "";
28    uc = 0xBADFACE;
29    ret = my_u8_mbtouc (&uc, input, 1);
30    ASSERT (ret == 1);
31    ASSERT (uc == 0);
32  }
33
34  /* Test ISO 646 unit input.  */
35  {
36    ucs4_t c;
37    uint8_t buf[1];
38
39    for (c = 0; c < 0x80; c++)
40      {
41        buf[0] = c;
42        uc = 0xBADFACE;
43        ret = my_u8_mbtouc (&uc, buf, 1);
44        ASSERT (ret == 1);
45        ASSERT (uc == c);
46      }
47  }
48
49  /* Test 2-byte character input.  */
50  {
51    static const uint8_t input[] = { 0xC3, 0x97 };
52    uc = 0xBADFACE;
53    ret = my_u8_mbtouc (&uc, input, 2);
54    ASSERT (ret == 2);
55    ASSERT (uc == 0x00D7);
56  }
57
58  /* Test 3-byte character input.  */
59  {
60    static const uint8_t input[] = { 0xE2, 0x82, 0xAC };
61    uc = 0xBADFACE;
62    ret = my_u8_mbtouc (&uc, input, 3);
63    ASSERT (ret == 3);
64    ASSERT (uc == 0x20AC);
65  }
66
67  /* Test 4-byte character input.  */
68  {
69    static const uint8_t input[] = { 0xF4, 0x8F, 0xBF, 0xBD };
70    uc = 0xBADFACE;
71    ret = my_u8_mbtouc (&uc, input, 4);
72    ASSERT (ret == 4);
73    ASSERT (uc == 0x10FFFD);
74  }
75
76  /* Test incomplete/invalid 1-byte input.  */
77  {
78    static const uint8_t input[] = { 0xC1 };
79    uc = 0xBADFACE;
80    ret = my_u8_mbtouc (&uc, input, 1);
81    ASSERT (ret == 1);
82    ASSERT (uc == 0xFFFD);
83  }
84  {
85    static const uint8_t input[] = { 0xC3 };
86    uc = 0xBADFACE;
87    ret = my_u8_mbtouc (&uc, input, 1);
88    ASSERT (ret == 1);
89    ASSERT (uc == 0xFFFD);
90  }
91  {
92    static const uint8_t input[] = { 0xE2 };
93    uc = 0xBADFACE;
94    ret = my_u8_mbtouc (&uc, input, 1);
95    ASSERT (ret == 1);
96    ASSERT (uc == 0xFFFD);
97  }
98  {
99    static const uint8_t input[] = { 0xF4 };
100    uc = 0xBADFACE;
101    ret = my_u8_mbtouc (&uc, input, 1);
102    ASSERT (ret == 1);
103    ASSERT (uc == 0xFFFD);
104  }
105  {
106    static const uint8_t input[] = { 0xFE };
107    uc = 0xBADFACE;
108    ret = my_u8_mbtouc (&uc, input, 1);
109    ASSERT (ret == 1);
110    ASSERT (uc == 0xFFFD);
111  }
112
113  /* Test incomplete/invalid 2-byte input.  */
114  {
115    static const uint8_t input[] = { 0xE0, 0x9F };
116    uc = 0xBADFACE;
117    ret = my_u8_mbtouc (&uc, input, 2);
118    ASSERT (ret == 1 || ret == 2);
119    ASSERT (uc == 0xFFFD);
120  }
121  {
122    static const uint8_t input[] = { 0xE2, 0x82 };
123    uc = 0xBADFACE;
124    ret = my_u8_mbtouc (&uc, input, 2);
125    ASSERT (ret == 2);
126    ASSERT (uc == 0xFFFD);
127  }
128  {
129    static const uint8_t input[] = { 0xE2, 0xD0 };
130    uc = 0xBADFACE;
131    ret = my_u8_mbtouc (&uc, input, 2);
132    ASSERT (ret == 1 || ret == 2);
133    ASSERT (uc == 0xFFFD);
134  }
135  {
136    static const uint8_t input[] = { 0xF0, 0x8F };
137    uc = 0xBADFACE;
138    ret = my_u8_mbtouc (&uc, input, 2);
139    ASSERT (ret == 1 || ret == 2);
140    ASSERT (uc == 0xFFFD);
141  }
142  {
143    static const uint8_t input[] = { 0xF3, 0x8F };
144    uc = 0xBADFACE;
145    ret = my_u8_mbtouc (&uc, input, 2);
146    ASSERT (ret == 2);
147    ASSERT (uc == 0xFFFD);
148  }
149  {
150    static const uint8_t input[] = { 0xF3, 0xD0 };
151    uc = 0xBADFACE;
152    ret = my_u8_mbtouc (&uc, input, 2);
153    ASSERT (ret == 1 || ret == 2);
154    ASSERT (uc == 0xFFFD);
155  }
156
157  /* Test incomplete/invalid 3-byte input.  */
158  {
159    static const uint8_t input[] = { 0xF3, 0x8F, 0xBF };
160    uc = 0xBADFACE;
161    ret = my_u8_mbtouc (&uc, input, 3);
162    ASSERT (ret == 3);
163    ASSERT (uc == 0xFFFD);
164  }
165  {
166    static const uint8_t input[] = { 0xF3, 0xD0, 0xBF };
167    uc = 0xBADFACE;
168    ret = my_u8_mbtouc (&uc, input, 3);
169    ASSERT (ret == 1 || ret == 3);
170    ASSERT (uc == 0xFFFD);
171  }
172  {
173    static const uint8_t input[] = { 0xF3, 0x8F, 0xD0 };
174    uc = 0xBADFACE;
175    ret = my_u8_mbtouc (&uc, input, 3);
176    ASSERT (ret == 1 || ret == 3);
177    ASSERT (uc == 0xFFFD);
178  }
179}
180