str_verscmp.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. lib/strutil - tests for lib/strutil/str_verscmp function.
  3. Testcases are taken from Gnulib.
  4. Copyright (C) 2019-2024
  5. Free Software Foundation, Inc.
  6. Written by:
  7. Andrew Borodin <aborodin@vmail.ru>, 2019
  8. This file is part of the Midnight Commander.
  9. The Midnight Commander is free software: you can redistribute it
  10. and/or modify it under the terms of the GNU General Public License as
  11. published by the Free Software Foundation, either version 3 of the License,
  12. or (at your option) any later version.
  13. The Midnight Commander is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. GNU General Public License for more details.
  17. You should have received a copy of the GNU General Public License
  18. along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #define TEST_SUITE_NAME "/lib/strutil"
  21. #include "tests/mctest.h"
  22. #include "lib/strutil.h"
  23. #include "lib/util.h" /* _GL_CMP() */
  24. /* --------------------------------------------------------------------------------------------- */
  25. /* From glibc bug 9913 */
  26. static char const a[] = "B0075022800016.gbp.corp.com";
  27. static char const b[] = "B007502280067.gbp.corp.com";
  28. static char const c[] = "B007502357019.GBP.CORP.COM";
  29. /* --------------------------------------------------------------------------------------------- */
  30. /* @Before */
  31. static void
  32. setup (void)
  33. {
  34. }
  35. /* --------------------------------------------------------------------------------------------- */
  36. /* @After */
  37. static void
  38. teardown (void)
  39. {
  40. }
  41. /* --------------------------------------------------------------------------------------------- */
  42. static int
  43. sign (int n)
  44. {
  45. return _GL_CMP (n, 0);
  46. }
  47. /* --------------------------------------------------------------------------------------------- */
  48. /* @DataSource("str_verscmp_test_ds") */
  49. /* *INDENT-OFF* */
  50. static const struct str_verscmp_test_struct
  51. {
  52. const char *s1;
  53. const char *s2;
  54. int expected_result;
  55. } str_verscmp_test_ds[] =
  56. {
  57. { "", "", 0 },
  58. { "a", "a", 0 },
  59. { "a", "b", -1 },
  60. { "b", "a", 1 },
  61. { "000", "00", -1 },
  62. { "00", "000", 1 },
  63. { "a0", "a", 1 },
  64. { "00", "01", -1 },
  65. { "01", "010", -1 },
  66. { "010", "09", -1 },
  67. { "09", "0", -1 },
  68. { "9", "10", -1 },
  69. { "0a", "0", 1 },
  70. /* From glibc bug 9913 */
  71. { a, b, -1 },
  72. { b, c, -1 },
  73. { a, c, -1 },
  74. { b, a, 1 },
  75. { c, b, 1 },
  76. { c, a, 1 }
  77. };
  78. /* *INDENT-ON* */
  79. /* @Test(dataSource = "str_verscmp_test_ds") */
  80. /* *INDENT-OFF* */
  81. START_TEST (str_verscmp_test)
  82. /* *INDENT-ON* */
  83. {
  84. /* given */
  85. int actual_result;
  86. const struct str_verscmp_test_struct *data = &str_verscmp_test_ds[_i];
  87. /* when */
  88. actual_result = str_verscmp (data->s1, data->s2);
  89. /* then */
  90. ck_assert_int_eq (sign (actual_result), sign (data->expected_result));
  91. }
  92. /* *INDENT-OFF* */
  93. END_TEST
  94. /* *INDENT-ON* */
  95. /* --------------------------------------------------------------------------------------------- */
  96. int
  97. main (void)
  98. {
  99. TCase *tc_core;
  100. tc_core = tcase_create ("Core");
  101. tcase_add_checked_fixture (tc_core, setup, teardown);
  102. /* Add new tests here: *************** */
  103. mctest_add_parameterized_test (tc_core, str_verscmp_test, str_verscmp_test_ds);
  104. /* *********************************** */
  105. return mctest_run_all (tc_core);
  106. }
  107. /* --------------------------------------------------------------------------------------------- */