tst-test_compare.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /* Basic test for the TEST_COMPARE macro.
  2. Copyright (C) 2017-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <string.h>
  16. #include <support/check.h>
  17. #include <support/capture_subprocess.h>
  18. static void
  19. subprocess (void *closure)
  20. {
  21. char ch = 1;
  22. /* These tests should fail. */
  23. TEST_COMPARE (ch, -1); /* Line 28. */
  24. TEST_COMPARE (2LL, -2LL); /* Line 29. */
  25. TEST_COMPARE (3LL, (short) -3); /* Line 30. */
  26. }
  27. struct bitfield
  28. {
  29. int i2 : 2;
  30. int i3 : 3;
  31. unsigned int u2 : 2;
  32. unsigned int u3 : 3;
  33. int i31 : 31;
  34. unsigned int u31 : 31 ;
  35. long long int i63 : 63;
  36. unsigned long long int u63 : 63;
  37. };
  38. /* Functions which return signed sizes are common, so test that these
  39. results can readily checked using TEST_COMPARE. */
  40. static int
  41. return_ssize_t (void)
  42. {
  43. return 4;
  44. }
  45. static int
  46. return_int (void)
  47. {
  48. return 4;
  49. }
  50. static int
  51. do_test (void)
  52. {
  53. /* This should succeed. */
  54. TEST_COMPARE (1, 1);
  55. TEST_COMPARE (2LL, 2U);
  56. {
  57. char i8 = 3;
  58. unsigned short u16 = 3;
  59. TEST_COMPARE (i8, u16);
  60. }
  61. TEST_COMPARE (return_ssize_t (), sizeof (char[4]));
  62. TEST_COMPARE (return_int (), sizeof (char[4]));
  63. struct bitfield bitfield = { 0 };
  64. TEST_COMPARE (bitfield.i2, bitfield.i3);
  65. TEST_COMPARE (bitfield.u2, bitfield.u3);
  66. TEST_COMPARE (bitfield.u2, bitfield.i3);
  67. TEST_COMPARE (bitfield.u3, bitfield.i3);
  68. TEST_COMPARE (bitfield.i2, bitfield.u3);
  69. TEST_COMPARE (bitfield.i3, bitfield.u2);
  70. TEST_COMPARE (bitfield.i63, bitfield.i63);
  71. TEST_COMPARE (bitfield.u63, bitfield.u63);
  72. TEST_COMPARE (bitfield.i31, bitfield.i63);
  73. TEST_COMPARE (bitfield.i63, bitfield.i31);
  74. struct support_capture_subprocess proc = support_capture_subprocess
  75. (&subprocess, NULL);
  76. /* Discard the reported error. */
  77. support_record_failure_reset ();
  78. puts ("info: *** subprocess output starts ***");
  79. fputs (proc.out.buffer, stdout);
  80. puts ("info: *** subprocess output ends ***");
  81. TEST_VERIFY
  82. (strcmp (proc.out.buffer,
  83. "tst-test_compare.c:28: numeric comparison failure\n"
  84. " left: 1 (0x1); from: ch\n"
  85. " right: -1 (0xffffffff); from: -1\n"
  86. "tst-test_compare.c:29: numeric comparison failure\n"
  87. " left: 2 (0x2); from: 2LL\n"
  88. " right: -2 (0xfffffffffffffffe); from: -2LL\n"
  89. "tst-test_compare.c:30: numeric comparison failure"
  90. " (widths 64 and 32)\n"
  91. " left: 3 (0x3); from: 3LL\n"
  92. " right: -3 (0xfffffffd); from: (short) -3\n") == 0);
  93. /* Check that there is no output on standard error. */
  94. support_capture_subprocess_check (&proc, "TEST_COMPARE", 0, sc_allow_stdout);
  95. return 0;
  96. }
  97. #include <support/test-driver.c>