1/* Basic test for the TEST_COMPARE_STRING 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
23static void
24subprocess (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 ("", NULL); /* Line 29. */
30 TEST_COMPARE_STRING ("X", ""); /* Line 30. */
31 TEST_COMPARE_STRING (NULL, "X"); /* Line 31. */
32 TEST_COMPARE_STRING ("abcd", "abcD"); /* Line 32. */
33 TEST_COMPARE_STRING ("abcd", NULL); /* Line 33. */
34 TEST_COMPARE_STRING (NULL, "abcd"); /* Line 34. */
35}
36
37/* Same contents, different addresses. */
38char buffer_abc_1[] = "abc";
39char buffer_abc_2[] = "abc";
40
41static int
42do_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 (NULL, NULL);
47 TEST_COMPARE_STRING ("", "");
48 TEST_COMPARE_STRING (buffer_abc_1, buffer_abc_2);
49 TEST_COMPARE_STRING (buffer_abc_1, "abc");
50
51 struct support_capture_subprocess proc = support_capture_subprocess
52 (callback: &subprocess, NULL);
53
54 /* Discard the reported error. */
55 support_record_failure_reset ();
56
57 puts (s: "info: *** subprocess output starts ***");
58 fputs (s: proc.out.buffer, stdout);
59 puts (s: "info: *** subprocess output ends ***");
60
61 TEST_VERIFY
62 (strcmp (proc.out.buffer,
63"tst-test_compare_string.c:29: error: string comparison failed\n"
64" left string: 0 bytes\n"
65" right string: NULL\n"
66"tst-test_compare_string.c:30: error: string comparison failed\n"
67" left string: 1 bytes\n"
68" right string: 0 bytes\n"
69" left (evaluated from \"X\"):\n"
70" \"X\"\n"
71" 58\n"
72"tst-test_compare_string.c:31: error: string comparison failed\n"
73" left string: NULL\n"
74" right string: 1 bytes\n"
75" right (evaluated from \"X\"):\n"
76" \"X\"\n"
77" 58\n"
78"tst-test_compare_string.c:32: error: string comparison failed\n"
79" string length: 4 bytes\n"
80" left (evaluated from \"abcd\"):\n"
81" \"abcd\"\n"
82" 61 62 63 64\n"
83" right (evaluated from \"abcD\"):\n"
84" \"abcD\"\n"
85" 61 62 63 44\n"
86"tst-test_compare_string.c:33: error: string comparison failed\n"
87" left string: 4 bytes\n"
88" right string: NULL\n"
89" left (evaluated from \"abcd\"):\n"
90" \"abcd\"\n"
91" 61 62 63 64\n"
92"tst-test_compare_string.c:34: error: string comparison failed\n"
93" left string: NULL\n"
94" right string: 4 bytes\n"
95" right (evaluated from \"abcd\"):\n"
96" \"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, context: "TEST_COMPARE_STRING",
102 status_or_signal: 0, allowed: sc_allow_stdout);
103
104 return 0;
105}
106
107#include <support/test-driver.c>
108

source code of glibc/support/tst-test_compare_string.c