1 /* Basic test for the TEST_COMPARE_STRING_WIDE macro.
2    Copyright (C) 2018-2022 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4 
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9 
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14 
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, see
17    <https://www.gnu.org/licenses/>.  */
18 
19 #include <string.h>
20 #include <support/check.h>
21 #include <support/capture_subprocess.h>
22 
23 static void
subprocess(void * closure)24 subprocess (void *closure)
25 {
26   /* These tests should fail.  They were chosen to cover differences
27      in length (with the same contents), single-bit mismatches, and
28      mismatching null pointers.  */
29   TEST_COMPARE_STRING_WIDE (L"", NULL);             /* Line 29.  */
30   TEST_COMPARE_STRING_WIDE (L"X", L"");              /* Line 30.  */
31   TEST_COMPARE_STRING_WIDE (NULL, L"X");            /* Line 31.  */
32   TEST_COMPARE_STRING_WIDE (L"abcd", L"abcD");       /* Line 32.  */
33   TEST_COMPARE_STRING_WIDE (L"abcd", NULL);         /* Line 33.  */
34   TEST_COMPARE_STRING_WIDE (NULL, L"abcd");         /* Line 34.  */
35 }
36 
37 /* Same contents, different addresses.  */
38 wchar_t buffer_abc_1[] = L"abc";
39 wchar_t buffer_abc_2[] = L"abc";
40 
41 static int
do_test(void)42 do_test (void)
43 {
44   /* This should succeed.  Even if the pointers and array contents are
45      different, zero-length inputs are not different.  */
46   TEST_COMPARE_STRING_WIDE (NULL, NULL);
47   TEST_COMPARE_STRING_WIDE (L"", L"");
48   TEST_COMPARE_STRING_WIDE (buffer_abc_1, buffer_abc_2);
49   TEST_COMPARE_STRING_WIDE (buffer_abc_1, L"abc");
50 
51   struct support_capture_subprocess proc = support_capture_subprocess
52     (&subprocess, NULL);
53 
54   /* Discard the reported error.  */
55   support_record_failure_reset ();
56 
57   puts ("info: *** subprocess output starts ***");
58   fputs (proc.out.buffer, stdout);
59   puts ("info: *** subprocess output ends ***");
60 
61   TEST_VERIFY
62     (strcmp (proc.out.buffer,
63 "tst-test_compare_string_wide.c:29: error: string comparison failed\n"
64 "  left string: 0 wide characters\n"
65 "  right string: NULL\n"
66 "tst-test_compare_string_wide.c:30: error: string comparison failed\n"
67 "  left string: 1 wide characters\n"
68 "  right string: 0 wide characters\n"
69 "  left (evaluated from L\"X\"):\n"
70 "      L\"X\"\n"
71 "      58\n"
72 "tst-test_compare_string_wide.c:31: error: string comparison failed\n"
73 "  left string: NULL\n"
74 "  right string: 1 wide characters\n"
75 "  right (evaluated from L\"X\"):\n"
76 "      L\"X\"\n"
77 "      58\n"
78 "tst-test_compare_string_wide.c:32: error: string comparison failed\n"
79 "  string length: 4 wide characters\n"
80 "  left (evaluated from L\"abcd\"):\n"
81 "      L\"abcd\"\n"
82 "      61 62 63 64\n"
83 "  right (evaluated from L\"abcD\"):\n"
84 "      L\"abcD\"\n"
85 "      61 62 63 44\n"
86 "tst-test_compare_string_wide.c:33: error: string comparison failed\n"
87 "  left string: 4 wide characters\n"
88 "  right string: NULL\n"
89 "  left (evaluated from L\"abcd\"):\n"
90 "      L\"abcd\"\n"
91 "      61 62 63 64\n"
92 "tst-test_compare_string_wide.c:34: error: string comparison failed\n"
93 "  left string: NULL\n"
94 "  right string: 4 wide characters\n"
95 "  right (evaluated from L\"abcd\"):\n"
96 "      L\"abcd\"\n"
97 "      61 62 63 64\n"
98              ) == 0);
99 
100   /* Check that there is no output on standard error.  */
101   support_capture_subprocess_check (&proc, "TEST_COMPARE_STRING_WIDE",
102                                     0, sc_allow_stdout);
103 
104   return 0;
105 }
106 
107 #include <support/test-driver.c>
108