maint.mk: Update system header list for #include syntax checks.
[gnulib.git] / tests / test-wcscmp.c
blob68873612f4423482a5e29c78123f42a696df4fd4
1 /* Test of wcscmp() 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 (wcscmp, int, (const wchar_t *, const wchar_t *));
26 #include "macros.h"
28 int
29 main (int argc, char *argv[])
31 /* Test simple cases. */
33 static const wchar_t input1[] = { 0 };
34 static const wchar_t input2[] = { 0 };
35 ASSERT (wcscmp (input1, input2) == 0);
38 static const wchar_t input1[] = { 0 };
39 static const wchar_t input2[] = { 'f', 'o', 'o', 0 };
40 ASSERT (wcscmp (input1, input2) < 0);
41 ASSERT (wcscmp (input2, input1) > 0);
44 static const wchar_t input1[] = { 'f', 'o', 'o', 0 };
45 static const wchar_t input2[] = { 'f', 'o', 'o', 0 };
46 ASSERT (wcscmp (input1, input2) == 0);
49 static const wchar_t input1[] = { 'f', 'o', 'o', 0 };
50 static const wchar_t input2[] = { 'b', 'a', 'r', 0 };
51 ASSERT (wcscmp (input1, input2) > 0);
52 ASSERT (wcscmp (input2, input1) < 0);
55 static const wchar_t input1[] = { 'f', 'o', 'o', 0 };
56 static const wchar_t input2[] = { 'f', 'o', 'o', 'b', 'a', 'r', 0 };
57 ASSERT (wcscmp (input1, input2) < 0);
58 ASSERT (wcscmp (input2, input1) > 0);
61 static const wchar_t input1[] = { 'o', 'o', 'm', 'p', 'h', 0 };
62 static const wchar_t input2[] = { 'o', 'o', 'p', 's', 0 };
63 ASSERT (wcscmp (input1, input2) < 0);
64 ASSERT (wcscmp (input2, input1) > 0);
67 /* ISO C requires wcscmp to work with all wchar_t values.
68 ISO C 17 § 7.29.4.4 says:
69 "Unless explicitly stated otherwise, the functions described in this
70 subclause order two wide characters the same way as two integers of
71 the underlying integer type designated by wchar_t." */
73 static const wchar_t input1[] = { (wchar_t) 0x76547654, 0 };
74 static const wchar_t input2[] = { (wchar_t) 0x9abc9abc, 0 };
75 if ((wchar_t)-1 < 0)
77 /* wchar_t is signed. */
78 ASSERT (wcscmp (input1, input2) > 0);
79 ASSERT (wcscmp (input2, input1) < 0);
81 else
83 /* wchar_t is unsigned. */
84 ASSERT (wcscmp (input1, input2) < 0);
85 ASSERT (wcscmp (input2, input1) > 0);
89 static const wchar_t input1[] = { (wchar_t) 0x9abc9abc, 0 };
90 static const wchar_t input2[] = { (wchar_t) 0x9bdf9bdf, 0 };
91 ASSERT (wcscmp (input1, input2) < 0);
92 ASSERT (wcscmp (input2, input1) > 0);
95 /* Comparing a negative wchar_t value against a null wchar_t.
96 ISO C 17 § 7.29.4.4.1 says:
97 "The wcscmp function compares the wide string pointed to by s1 to
98 the wide string pointed to by s2."
99 ISO C 17 § 7.1.1 defines the term "wide string":
100 "A wide string is a contiguous sequence of wide characters terminated
101 by and including the first null wide character."
102 This means that the comparison extends up to and *including* the first
103 null wchar_t. */
105 static const wchar_t input1[] = { (wchar_t) 'x', 0 };
106 static const wchar_t input2[] = { (wchar_t) 'x', (wchar_t) 0x9abc9abc, 0 };
107 if ((wchar_t)-1 < 0)
109 /* wchar_t is signed. */
110 ASSERT (wcscmp (input1, input2) > 0);
111 ASSERT (wcscmp (input2, input1) < 0);
113 else
115 /* wchar_t is unsigned. */
116 ASSERT (wcscmp (input1, input2) < 0);
117 ASSERT (wcscmp (input2, input1) > 0);
121 return test_exit_status;