Support compiling without -loldnames on native Windows.
[gnulib.git] / tests / unistr / test-u16-prev.c
blobfbd1530b11fdebb29f43d5867d713fdbb4a89c0f
1 /* Test of u16_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. */
19 #include <config.h>
21 #include "unistr.h"
23 #include "macros.h"
25 static int
26 check (const uint16_t *input, size_t input_length, ucs4_t *puc)
28 ucs4_t uc;
30 /* Test recognition when at the beginning of the string. */
31 if (u16_prev (&uc, input + input_length, input) != input)
32 return 1;
34 /* Test recognition when preceded by a 1-unit character. */
36 uint16_t buf[100];
37 uint16_t *ptr;
38 size_t i;
39 ucs4_t uc1;
41 ptr = buf;
42 *ptr++ = 0x2102;
43 for (i = 0; i < input_length; i++)
44 ptr[i] = input[i];
46 if (u16_prev (&uc1, ptr + input_length, buf) != ptr)
47 return 2;
48 if (uc1 != uc)
49 return 3;
52 /* Test recognition when preceded by a 2-unit character. */
54 uint16_t buf[100];
55 uint16_t *ptr;
56 size_t i;
57 ucs4_t uc1;
59 ptr = buf;
60 *ptr++ = 0xD835;
61 *ptr++ = 0xDD1E;
62 for (i = 0; i < input_length; i++)
63 ptr[i] = input[i];
65 if (u16_prev (&uc1, ptr + input_length, buf) != ptr)
66 return 4;
67 if (uc1 != uc)
68 return 5;
71 *puc = uc;
72 return 0;
75 static int
76 check_invalid (const uint16_t *input, size_t input_length)
78 ucs4_t uc;
80 /* Test recognition when at the beginning of the string. */
81 uc = 0xBADFACE;
82 if (u16_prev (&uc, input + input_length, input) != NULL)
83 return 1;
84 if (uc != 0xBADFACE)
85 return 2;
87 /* Test recognition when preceded by a 1-unit character. */
89 uint16_t buf[100];
90 uint16_t *ptr;
91 size_t i;
93 ptr = buf;
94 *ptr++ = 0x2102;
95 for (i = 0; i < input_length; i++)
96 ptr[i] = input[i];
98 uc = 0xBADFACE;
99 if (u16_prev (&uc, ptr + input_length, buf) != NULL)
100 return 3;
101 if (uc != 0xBADFACE)
102 return 4;
105 /* Test recognition when preceded by a 2-unit character. */
107 uint16_t buf[100];
108 uint16_t *ptr;
109 size_t i;
111 ptr = buf;
112 *ptr++ = 0xD835;
113 *ptr++ = 0xDD1E;
114 for (i = 0; i < input_length; i++)
115 ptr[i] = input[i];
117 uc = 0xBADFACE;
118 if (u16_prev (&uc, ptr + input_length, buf) != NULL)
119 return 5;
120 if (uc != 0xBADFACE)
121 return 6;
124 return 0;
128 main ()
130 ucs4_t uc;
132 /* Test ISO 646 unit input. */
134 ucs4_t c;
135 uint16_t buf[1];
137 for (c = 0; c < 0x80; c++)
139 buf[0] = c;
140 uc = 0xBADFACE;
141 ASSERT (check (buf, 1, &uc) == 0);
142 ASSERT (uc == c);
146 /* Test BMP unit input. */
148 static const uint16_t input[] = { 0x20AC };
149 uc = 0xBADFACE;
150 ASSERT (check (input, SIZEOF (input), &uc) == 0);
151 ASSERT (uc == 0x20AC);
154 /* Test 2-units character input. */
156 static const uint16_t input[] = { 0xD835, 0xDD1F };
157 uc = 0xBADFACE;
158 ASSERT (check (input, SIZEOF (input), &uc) == 0);
159 ASSERT (uc == 0x1D51F);
162 /* Test incomplete/invalid 1-unit input. */
164 static const uint16_t input[] = { 0xD835 };
165 ASSERT (check_invalid (input, SIZEOF (input)) == 0);
168 static const uint16_t input[] = { 0xDD1F };
169 ASSERT (check_invalid (input, SIZEOF (input)) == 0);
172 return 0;