1 /* Test of u8_mblen() 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
main()26 main ()
27 {
28   int ret;
29 
30   /* Test zero-length input.  */
31   {
32     static const uint8_t input[] = "";
33     ret = u8_mblen (input, 0);
34     ASSERT (ret == -1);
35   }
36 
37   /* Test NUL unit input.  */
38   {
39     static const uint8_t input[] = "";
40     ret = u8_mblen (input, 1);
41     ASSERT (ret == 0);
42   }
43 
44   /* Test ISO 646 unit input.  */
45   {
46     ucs4_t c;
47     uint8_t buf[1];
48 
49     for (c = 1; c < 0x80; c++)
50       {
51         buf[0] = c;
52         ret = u8_mblen (buf, 1);
53         ASSERT (ret == 1);
54       }
55   }
56 
57   /* Test 2-byte character input.  */
58   {
59     static const uint8_t input[] = { 0xC3, 0x97 };
60     ret = u8_mblen (input, 2);
61     ASSERT (ret == 2);
62   }
63 
64   /* Test 3-byte character input.  */
65   {
66     static const uint8_t input[] = { 0xE2, 0x82, 0xAC };
67     ret = u8_mblen (input, 3);
68     ASSERT (ret == 3);
69   }
70 
71   /* Test 4-byte character input.  */
72   {
73     static const uint8_t input[] = { 0xF4, 0x8F, 0xBF, 0xBD };
74     ret = u8_mblen (input, 4);
75     ASSERT (ret == 4);
76   }
77 
78   /* Test incomplete/invalid 1-byte input.  */
79   {
80     static const uint8_t input[] = { 0xC1 };
81     ret = u8_mblen (input, 1);
82     ASSERT (ret == -1);
83   }
84   {
85     static const uint8_t input[] = { 0xC3 };
86     ret = u8_mblen (input, 1);
87     ASSERT (ret == -1);
88   }
89   {
90     static const uint8_t input[] = { 0xE2 };
91     ret = u8_mblen (input, 1);
92     ASSERT (ret == -1);
93   }
94   {
95     static const uint8_t input[] = { 0xF4 };
96     ret = u8_mblen (input, 1);
97     ASSERT (ret == -1);
98   }
99   {
100     static const uint8_t input[] = { 0xFE };
101     ret = u8_mblen (input, 1);
102     ASSERT (ret == -1);
103   }
104 
105   /* Test incomplete/invalid 2-byte input.  */
106   {
107     static const uint8_t input[] = { 0xE0, 0x9F };
108     ret = u8_mblen (input, 2);
109     ASSERT (ret == -1);
110   }
111   {
112     static const uint8_t input[] = { 0xE2, 0x82 };
113     ret = u8_mblen (input, 2);
114     ASSERT (ret == -1);
115   }
116   {
117     static const uint8_t input[] = { 0xE2, 0xD0 };
118     ret = u8_mblen (input, 2);
119     ASSERT (ret == -1);
120   }
121   {
122     static const uint8_t input[] = { 0xF0, 0x8F };
123     ret = u8_mblen (input, 2);
124     ASSERT (ret == -1);
125   }
126   {
127     static const uint8_t input[] = { 0xF3, 0x8F };
128     ret = u8_mblen (input, 2);
129     ASSERT (ret == -1);
130   }
131   {
132     static const uint8_t input[] = { 0xF3, 0xD0 };
133     ret = u8_mblen (input, 2);
134     ASSERT (ret == -1);
135   }
136 
137   /* Test incomplete/invalid 3-byte input.  */
138   {
139     static const uint8_t input[] = { 0xF3, 0x8F, 0xBF };
140     ret = u8_mblen (input, 3);
141     ASSERT (ret == -1);
142   }
143   {
144     static const uint8_t input[] = { 0xF3, 0xD0, 0xBF };
145     ret = u8_mblen (input, 3);
146     ASSERT (ret == -1);
147   }
148   {
149     static const uint8_t input[] = { 0xF3, 0x8F, 0xD0 };
150     ret = u8_mblen (input, 3);
151     ASSERT (ret == -1);
152   }
153 
154   return 0;
155 }
156