]> git.ipfire.org Git - thirdparty/glibc.git/blob - support/tst-test_compare.c
Update copyright dates with scripts/update-copyrights.
[thirdparty/glibc.git] / support / tst-test_compare.c
1 /* Basic test for the TEST_COMPARE macro.
2 Copyright (C) 2017-2018 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 <http://www.gnu.org/licenses/>. */
18
19 #include <string.h>
20 #include <support/check.h>
21 #include <support/capture_subprocess.h>
22
23 static void
24 subprocess (void *closure)
25 {
26 char ch = 1;
27 /* These tests should fail. */
28 TEST_COMPARE (ch, -1); /* Line 28. */
29 TEST_COMPARE (2LL, -2LL); /* Line 29. */
30 TEST_COMPARE (3LL, (short) -3); /* Line 30. */
31 }
32
33 struct bitfield
34 {
35 int i2 : 2;
36 int i3 : 3;
37 unsigned int u2 : 2;
38 unsigned int u3 : 3;
39 int i31 : 31;
40 unsigned int u31 : 31 ;
41 long long int i63 : 63;
42 unsigned long long int u63 : 63;
43 };
44
45 static int
46 do_test (void)
47 {
48 /* This should succeed. */
49 TEST_COMPARE (1, 1);
50 TEST_COMPARE (2LL, 2U);
51 {
52 char i8 = 3;
53 unsigned short u16 = 3;
54 TEST_COMPARE (i8, u16);
55 }
56
57 struct bitfield bitfield = { 0 };
58 TEST_COMPARE (bitfield.i2, bitfield.i3);
59 TEST_COMPARE (bitfield.u2, bitfield.u3);
60 TEST_COMPARE (bitfield.u2, bitfield.i3);
61 TEST_COMPARE (bitfield.u3, bitfield.i3);
62 TEST_COMPARE (bitfield.i2, bitfield.u3);
63 TEST_COMPARE (bitfield.i3, bitfield.u2);
64 TEST_COMPARE (bitfield.i63, bitfield.i63);
65 TEST_COMPARE (bitfield.u63, bitfield.u63);
66 TEST_COMPARE (bitfield.i31, bitfield.i63);
67 TEST_COMPARE (bitfield.i63, bitfield.i31);
68
69 struct support_capture_subprocess proc = support_capture_subprocess
70 (&subprocess, NULL);
71
72 /* Discard the reported error. */
73 support_record_failure_reset ();
74
75 puts ("info: *** subprocess output starts ***");
76 fputs (proc.out.buffer, stdout);
77 puts ("info: *** subprocess output ends ***");
78
79 TEST_VERIFY
80 (strcmp (proc.out.buffer,
81 "tst-test_compare.c:28: numeric comparison failure\n"
82 " left: 1 (0x1); from: ch\n"
83 " right: -1 (0xffffffff); from: -1\n"
84 "tst-test_compare.c:29: numeric comparison failure\n"
85 " left: 2 (0x2); from: 2LL\n"
86 " right: -2 (0xfffffffffffffffe); from: -2LL\n"
87 "tst-test_compare.c:30: numeric comparison failure"
88 " (widths 64 and 32)\n"
89 " left: 3 (0x3); from: 3LL\n"
90 " right: -3 (0xfffffffd); from: (short) -3\n") == 0);
91
92 /* Check that there is no output on standard error. */
93 support_capture_subprocess_check (&proc, "TEST_COMPARE", 0, sc_allow_stdout);
94
95 return 0;
96 }
97
98 #include <support/test-driver.c>