gettext: Sync with gettext 0.23.
[gnulib.git] / tests / test-wcsncmp.c
blob0c6f228eeb72bbb9acd4bd0952eda4163c992c2f
1 /* Test of wcsncmp() function.
2 Copyright (C) 2010-2024 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>, 2023. */
19 #include <config.h>
21 #include <wchar.h>
23 #include "signature.h"
24 SIGNATURE_CHECK (wcsncmp, int, (const wchar_t *, const wchar_t *, size_t));
26 #include "macros.h"
28 /* Test the library, not the compiler+library. */
29 static int
30 lib_wcsncmp (wchar_t const *ws1, wchar_t const *ws2, size_t n)
32 return wcsncmp (ws1, ws2, n);
34 static int (*volatile volatile_wcsncmp) (wchar_t const *,
35 wchar_t const *, size_t)
36 = lib_wcsncmp;
37 #undef wcsncmp
38 #define wcsncmp volatile_wcsncmp
40 int
41 main (int argc, char *argv[])
43 /* Test simple cases. */
45 static const wchar_t input1[] = { 0 };
46 static const wchar_t input2[] = { 0 };
47 ASSERT (wcsncmp (input1, input2, 0) == 0);
48 ASSERT (wcsncmp (input1, input2, 1) == 0);
49 ASSERT (wcsncmp (input1, input2, 1000000) == 0);
52 static const wchar_t input1[] = { 0 };
53 static const wchar_t input2[] = { 'f', 'o', 'o', 0 };
54 ASSERT (wcsncmp (input1, input2, 0) == 0);
55 ASSERT (wcsncmp (input1, input2, 1) < 0);
56 ASSERT (wcsncmp (input2, input1, 1) > 0);
57 ASSERT (wcsncmp (input1, input2, 3) < 0);
58 ASSERT (wcsncmp (input2, input1, 3) > 0);
59 ASSERT (wcsncmp (input1, input2, 4) < 0);
60 ASSERT (wcsncmp (input2, input1, 4) > 0);
61 ASSERT (wcsncmp (input1, input2, 1000000) < 0);
62 ASSERT (wcsncmp (input2, input1, 1000000) > 0);
65 static const wchar_t input1[] = { 'f', 'o', 'o', 0 };
66 static const wchar_t input2[] = { 'f', 'o', 'o', 0 };
67 ASSERT (wcsncmp (input1, input2, 0) == 0);
68 ASSERT (wcsncmp (input1, input2, 1) == 0);
69 ASSERT (wcsncmp (input1, input2, 2) == 0);
70 ASSERT (wcsncmp (input1, input2, 3) == 0);
71 ASSERT (wcsncmp (input1, input2, 4) == 0);
72 ASSERT (wcsncmp (input1, input2, 1000000) == 0);
75 static const wchar_t input1[] = { 'f', 'o', 'o', 0 };
76 static const wchar_t input2[] = { 'b', 'a', 'r', 0 };
77 ASSERT (wcsncmp (input1, input2, 0) == 0);
78 ASSERT (wcsncmp (input1, input2, 1) > 0);
79 ASSERT (wcsncmp (input2, input1, 1) < 0);
80 ASSERT (wcsncmp (input1, input2, 2) > 0);
81 ASSERT (wcsncmp (input2, input1, 2) < 0);
82 ASSERT (wcsncmp (input1, input2, 1000000) > 0);
83 ASSERT (wcsncmp (input2, input1, 1000000) < 0);
86 static const wchar_t input1[] = { 'f', 'o', 'o', 0 };
87 static const wchar_t input2[] = { 'f', 'o', 'o', 'b', 'a', 'r', 0 };
88 ASSERT (wcsncmp (input1, input2, 0) == 0);
89 ASSERT (wcsncmp (input1, input2, 1) == 0);
90 ASSERT (wcsncmp (input1, input2, 2) == 0);
91 ASSERT (wcsncmp (input1, input2, 3) == 0);
92 ASSERT (wcsncmp (input1, input2, 4) < 0);
93 ASSERT (wcsncmp (input2, input1, 4) > 0);
94 ASSERT (wcsncmp (input1, input2, 1000000) < 0);
95 ASSERT (wcsncmp (input2, input1, 1000000) > 0);
98 static const wchar_t input1[] = { 'o', 'o', 'm', 'p', 'h', 0 };
99 static const wchar_t input2[] = { 'o', 'o', 'p', 's', 0 };
100 ASSERT (wcsncmp (input1, input2, 0) == 0);
101 ASSERT (wcsncmp (input1, input2, 1) == 0);
102 ASSERT (wcsncmp (input1, input2, 2) == 0);
103 ASSERT (wcsncmp (input1, input2, 3) < 0);
104 ASSERT (wcsncmp (input2, input1, 3) > 0);
105 ASSERT (wcsncmp (input1, input2, 4) < 0);
106 ASSERT (wcsncmp (input2, input1, 4) > 0);
107 ASSERT (wcsncmp (input1, input2, 5) < 0);
108 ASSERT (wcsncmp (input2, input1, 5) > 0);
109 ASSERT (wcsncmp (input1, input2, 6) < 0);
110 ASSERT (wcsncmp (input2, input1, 6) > 0);
111 ASSERT (wcsncmp (input1, input2, 1000000) < 0);
112 ASSERT (wcsncmp (input2, input1, 1000000) > 0);
115 /* ISO C requires wcsncmp to work with all wchar_t values.
116 ISO C 17 § 7.29.4.4 says:
117 "Unless explicitly stated otherwise, the functions described in this
118 subclause order two wide characters the same way as two integers of
119 the underlying integer type designated by wchar_t." */
121 static const wchar_t input1[] = { (wchar_t) 0x76547654, 0 };
122 static const wchar_t input2[] = { (wchar_t) 0x9abc9abc, 0 };
123 ASSERT (wcsncmp (input1, input2, 0) == 0);
124 if ((wchar_t)-1 < 0)
126 /* wchar_t is signed. */
127 ASSERT (wcsncmp (input1, input2, 1) > 0);
128 ASSERT (wcsncmp (input2, input1, 1) < 0);
129 ASSERT (wcsncmp (input1, input2, 2) > 0);
130 ASSERT (wcsncmp (input2, input1, 2) < 0);
131 ASSERT (wcsncmp (input1, input2, 1000000) > 0);
132 ASSERT (wcsncmp (input2, input1, 1000000) < 0);
134 else
136 /* wchar_t is unsigned. */
137 ASSERT (wcsncmp (input1, input2, 1) < 0);
138 ASSERT (wcsncmp (input2, input1, 1) > 0);
139 ASSERT (wcsncmp (input1, input2, 2) < 0);
140 ASSERT (wcsncmp (input2, input1, 2) > 0);
141 ASSERT (wcsncmp (input1, input2, 1000000) < 0);
142 ASSERT (wcsncmp (input2, input1, 1000000) > 0);
146 static const wchar_t input1[] = { (wchar_t) 0x9abc9abc, 0 };
147 static const wchar_t input2[] = { (wchar_t) 0x9bdf9bdf, 0 };
148 ASSERT (wcsncmp (input1, input2, 0) == 0);
149 ASSERT (wcsncmp (input1, input2, 1) < 0);
150 ASSERT (wcsncmp (input2, input1, 1) > 0);
151 ASSERT (wcsncmp (input1, input2, 2) < 0);
152 ASSERT (wcsncmp (input2, input1, 2) > 0);
153 ASSERT (wcsncmp (input1, input2, 1000000) < 0);
154 ASSERT (wcsncmp (input2, input1, 1000000) > 0);
157 /* Comparing a negative wchar_t value against a null wchar_t.
158 ISO C 17 § 7.29.4.4.1 says:
159 "The wcsncmp function compares the wide string pointed to by s1 to
160 the wide string pointed to by s2."
161 ISO C 17 § 7.1.1 defines the term "wide string":
162 "A wide string is a contiguous sequence of wide characters terminated
163 by and including the first null wide character."
164 This means that the comparison extends up to and *including* the first
165 null wchar_t. */
167 static const wchar_t input1[] = { (wchar_t) 'x', 0 };
168 static const wchar_t input2[] = { (wchar_t) 'x', (wchar_t) 0x9abc9abc, 0 };
169 ASSERT (wcsncmp (input1, input2, 0) == 0);
170 ASSERT (wcsncmp (input1, input2, 1) == 0);
171 if ((wchar_t)-1 < 0)
173 /* wchar_t is signed. */
174 ASSERT (wcsncmp (input1, input2, 2) > 0);
175 ASSERT (wcsncmp (input2, input1, 2) < 0);
176 ASSERT (wcsncmp (input1, input2, 3) > 0);
177 ASSERT (wcsncmp (input2, input1, 3) < 0);
178 ASSERT (wcsncmp (input1, input2, 1000000) > 0);
179 ASSERT (wcsncmp (input2, input1, 1000000) < 0);
181 else
183 /* wchar_t is unsigned. */
184 ASSERT (wcsncmp (input1, input2, 2) < 0);
185 ASSERT (wcsncmp (input2, input1, 2) > 0);
186 ASSERT (wcsncmp (input1, input2, 3) < 0);
187 ASSERT (wcsncmp (input2, input1, 3) > 0);
188 ASSERT (wcsncmp (input1, input2, 1000000) < 0);
189 ASSERT (wcsncmp (input2, input1, 1000000) > 0);
193 /* Test zero-length operations on NULL pointers, allowed by
194 <https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3322.pdf>. */
195 ASSERT (wcsncmp (NULL, L"x", 0) == 0);
196 ASSERT (wcsncmp (L"x", NULL, 0) == 0);
197 ASSERT (wcsncmp (NULL, NULL, 0) == 0);
199 return test_exit_status;