Fix last ChangeLog entry.
[gnulib.git] / tests / test-wmemcmp.c
blob607a398ea273f6de8b0f5a2bd9316a85de0fa83f
1 /* Test of wmemcmp() function.
2 Copyright (C) 2008-2025 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 (wmemcmp, 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_wmemcmp (wchar_t const *ws1, wchar_t const *ws2, size_t n)
32 return wmemcmp (ws1, ws2, n);
34 int (*volatile volatile_wmemcmp) (wchar_t const *, wchar_t const *, size_t)
35 = lib_wmemcmp;
36 #undef wmemcmp
37 #define wmemcmp volatile_wmemcmp
39 int
40 main (int argc, char *argv[])
42 /* Test equal / not equal distinction. */
44 static const wchar_t input1[] = { 'f', 'o', 'o', 0 };
45 static const wchar_t input2[] = { 'f', 'o', 'o', 'b', 'a', 'r', 0 };
46 ASSERT (wmemcmp (input1, input2, 2) == 0);
47 ASSERT (wmemcmp (input1, input2, 3) == 0);
48 ASSERT (wmemcmp (input1, input2, 4) != 0);
51 static const wchar_t input1[] = { 'f', 'o', 'o', 0 };
52 static const wchar_t input2[] = { 'b', 'a', 'r', 0 };
53 ASSERT (wmemcmp (input1, input2, 1) != 0);
54 ASSERT (wmemcmp (input1, input2, 3) != 0);
57 /* Test less / equal / greater distinction. */
59 static const wchar_t input1[] = { 'f', 'o', 'o', 0 };
60 static const wchar_t input2[] = { 'm', 'o', 'o', 0 };
61 ASSERT (wmemcmp (input1, input2, 4) < 0);
62 ASSERT (wmemcmp (input2, input1, 4) > 0);
65 static const wchar_t input1[] = { 'o', 'o', 'm', 'p', 'h', 0 };
66 static const wchar_t input2[] = { 'o', 'o', 'p', 's', 0 };
67 ASSERT (wmemcmp (input1, input2, 3) < 0);
68 ASSERT (wmemcmp (input2, input1, 3) > 0);
71 static const wchar_t input1[] = { 'f', 'o', 'o', 0 };
72 static const wchar_t input2[] = { 'f', 'o', 'o', 'b', 'a', 'r', 0 };
73 ASSERT (wmemcmp (input1, input2, 4) < 0);
74 ASSERT (wmemcmp (input2, input1, 4) > 0);
77 /* ISO C requires wcscmp to work with all wchar_t values.
78 ISO C 17 ยง 7.29.4.4 says:
79 "Unless explicitly stated otherwise, the functions described in this
80 subclause order two wide characters the same way as two integers of
81 the underlying integer type designated by wchar_t." */
83 static const wchar_t input1[] = { (wchar_t) 0x76547654 };
84 static const wchar_t input2[] = { (wchar_t) 0x9abc9abc };
85 if ((wchar_t)-1 < 0)
87 /* wchar_t is signed. */
88 ASSERT (wmemcmp (input1, input2, 1) > 0);
89 ASSERT (wmemcmp (input2, input1, 1) < 0);
91 else
93 /* wchar_t is unsigned. */
94 ASSERT (wmemcmp (input1, input2, 1) < 0);
95 ASSERT (wmemcmp (input2, input1, 1) > 0);
99 static const wchar_t input1[] = { (wchar_t) 0x9abc9abc };
100 static const wchar_t input2[] = { (wchar_t) 0x9bdf9bdf };
101 ASSERT (wmemcmp (input1, input2, 1) < 0);
102 ASSERT (wmemcmp (input2, input1, 1) > 0);
105 /* Test zero-length operations on NULL pointers, allowed by
106 <https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3322.pdf>. */
107 ASSERT (wmemcmp (NULL, L"x", 0) == 0);
108 ASSERT (wmemcmp (L"x", NULL, 0) == 0);
109 ASSERT (wmemcmp (NULL, NULL, 0) == 0);
111 return test_exit_status;