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