| 1 | /* Basic test for the TEST_COMPARE macro. |
| 2 | Copyright (C) 2017-2024 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 |
| 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 | /* Functions which return signed sizes are common, so test that these |
| 46 | results can readily checked using TEST_COMPARE. */ |
| 47 | |
| 48 | static int |
| 49 | return_ssize_t (void) |
| 50 | { |
| 51 | return 4; |
| 52 | } |
| 53 | |
| 54 | static int |
| 55 | return_int (void) |
| 56 | { |
| 57 | return 4; |
| 58 | } |
| 59 | |
| 60 | |
| 61 | static int |
| 62 | do_test (void) |
| 63 | { |
| 64 | /* This should succeed. */ |
| 65 | TEST_COMPARE (1, 1); |
| 66 | TEST_COMPARE (2LL, 2U); |
| 67 | { |
| 68 | char i8 = 3; |
| 69 | unsigned short u16 = 3; |
| 70 | TEST_COMPARE (i8, u16); |
| 71 | } |
| 72 | TEST_COMPARE (return_ssize_t (), sizeof (char[4])); |
| 73 | TEST_COMPARE (return_int (), sizeof (char[4])); |
| 74 | |
| 75 | struct bitfield bitfield = { 0 }; |
| 76 | TEST_COMPARE (bitfield.i2, bitfield.i3); |
| 77 | TEST_COMPARE (bitfield.u2, bitfield.u3); |
| 78 | TEST_COMPARE (bitfield.u2, bitfield.i3); |
| 79 | TEST_COMPARE (bitfield.u3, bitfield.i3); |
| 80 | TEST_COMPARE (bitfield.i2, bitfield.u3); |
| 81 | TEST_COMPARE (bitfield.i3, bitfield.u2); |
| 82 | TEST_COMPARE (bitfield.i63, bitfield.i63); |
| 83 | TEST_COMPARE (bitfield.u63, bitfield.u63); |
| 84 | TEST_COMPARE (bitfield.i31, bitfield.i63); |
| 85 | TEST_COMPARE (bitfield.i63, bitfield.i31); |
| 86 | |
| 87 | struct support_capture_subprocess proc = support_capture_subprocess |
| 88 | (callback: &subprocess, NULL); |
| 89 | |
| 90 | /* Discard the reported error. */ |
| 91 | support_record_failure_reset (); |
| 92 | |
| 93 | puts (s: "info: *** subprocess output starts ***" ); |
| 94 | fputs (s: proc.out.buffer, stdout); |
| 95 | puts (s: "info: *** subprocess output ends ***" ); |
| 96 | |
| 97 | TEST_VERIFY |
| 98 | (strcmp (proc.out.buffer, |
| 99 | "tst-test_compare.c:28: numeric comparison failure\n" |
| 100 | " left: 1 (0x1); from: ch\n" |
| 101 | " right: -1 (0xffffffff); from: -1\n" |
| 102 | "tst-test_compare.c:29: numeric comparison failure\n" |
| 103 | " left: 2 (0x2); from: 2LL\n" |
| 104 | " right: -2 (0xfffffffffffffffe); from: -2LL\n" |
| 105 | "tst-test_compare.c:30: numeric comparison failure" |
| 106 | " (widths 64 and 32)\n" |
| 107 | " left: 3 (0x3); from: 3LL\n" |
| 108 | " right: -3 (0xfffffffd); from: (short) -3\n" ) == 0); |
| 109 | |
| 110 | /* Check that there is no output on standard error. */ |
| 111 | support_capture_subprocess_check (&proc, context: "TEST_COMPARE" , status_or_signal: 0, allowed: sc_allow_stdout); |
| 112 | |
| 113 | return 0; |
| 114 | } |
| 115 | |
| 116 | #include <support/test-driver.c> |
| 117 | |