tst-test_compare_string.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /* Basic test for the TEST_COMPARE_STRING macro.
  2. Copyright (C) 2018-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. /* These tests should fail. They were chosen to cover differences
  22. in length (with the same contents), single-bit mismatches, and
  23. mismatching null pointers. */
  24. TEST_COMPARE_STRING ("", NULL); /* Line 29. */
  25. TEST_COMPARE_STRING ("X", ""); /* Line 30. */
  26. TEST_COMPARE_STRING (NULL, "X"); /* Line 31. */
  27. TEST_COMPARE_STRING ("abcd", "abcD"); /* Line 32. */
  28. TEST_COMPARE_STRING ("abcd", NULL); /* Line 33. */
  29. TEST_COMPARE_STRING (NULL, "abcd"); /* Line 34. */
  30. }
  31. /* Same contents, different addresses. */
  32. char buffer_abc_1[] = "abc";
  33. char buffer_abc_2[] = "abc";
  34. static int
  35. do_test (void)
  36. {
  37. /* This should succeed. Even if the pointers and array contents are
  38. different, zero-length inputs are not different. */
  39. TEST_COMPARE_STRING (NULL, NULL);
  40. TEST_COMPARE_STRING ("", "");
  41. TEST_COMPARE_STRING (buffer_abc_1, buffer_abc_2);
  42. TEST_COMPARE_STRING (buffer_abc_1, "abc");
  43. struct support_capture_subprocess proc = support_capture_subprocess
  44. (&subprocess, NULL);
  45. /* Discard the reported error. */
  46. support_record_failure_reset ();
  47. puts ("info: *** subprocess output starts ***");
  48. fputs (proc.out.buffer, stdout);
  49. puts ("info: *** subprocess output ends ***");
  50. TEST_VERIFY
  51. (strcmp (proc.out.buffer,
  52. "tst-test_compare_string.c:29: error: blob comparison failed\n"
  53. " left string: 0 bytes\n"
  54. " right string: NULL\n"
  55. "tst-test_compare_string.c:30: error: blob comparison failed\n"
  56. " left string: 1 bytes\n"
  57. " right string: 0 bytes\n"
  58. " left (evaluated from \"X\"):\n"
  59. " \"X\"\n"
  60. " 58\n"
  61. "tst-test_compare_string.c:31: error: blob comparison failed\n"
  62. " left string: NULL\n"
  63. " right string: 1 bytes\n"
  64. " right (evaluated from \"X\"):\n"
  65. " \"X\"\n"
  66. " 58\n"
  67. "tst-test_compare_string.c:32: error: blob comparison failed\n"
  68. " string length: 4 bytes\n"
  69. " left (evaluated from \"abcd\"):\n"
  70. " \"abcd\"\n"
  71. " 61 62 63 64\n"
  72. " right (evaluated from \"abcD\"):\n"
  73. " \"abcD\"\n"
  74. " 61 62 63 44\n"
  75. "tst-test_compare_string.c:33: error: blob comparison failed\n"
  76. " left string: 4 bytes\n"
  77. " right string: NULL\n"
  78. " left (evaluated from \"abcd\"):\n"
  79. " \"abcd\"\n"
  80. " 61 62 63 64\n"
  81. "tst-test_compare_string.c:34: error: blob comparison failed\n"
  82. " left string: NULL\n"
  83. " right string: 4 bytes\n"
  84. " right (evaluated from \"abcd\"):\n"
  85. " \"abcd\"\n"
  86. " 61 62 63 64\n"
  87. ) == 0);
  88. /* Check that there is no output on standard error. */
  89. support_capture_subprocess_check (&proc, "TEST_COMPARE_STRING",
  90. 0, sc_allow_stdout);
  91. return 0;
  92. }
  93. #include <support/test-driver.c>