str_verscmp.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. lib/strutil - tests for lib/strutil/str_verscmp function.
  3. Testcases are taken from Gnulib.
  4. Copyright (C) 2019-2021
  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. /* --------------------------------------------------------------------------------------------- */
  24. /* From glibc bug 9913 */
  25. static char const a[] = "B0075022800016.gbp.corp.com";
  26. static char const b[] = "B007502280067.gbp.corp.com";
  27. static char const c[] = "B007502357019.GBP.CORP.COM";
  28. /* --------------------------------------------------------------------------------------------- */
  29. /* @Before */
  30. static void
  31. setup (void)
  32. {
  33. }
  34. /* --------------------------------------------------------------------------------------------- */
  35. /* @After */
  36. static void
  37. teardown (void)
  38. {
  39. }
  40. /* --------------------------------------------------------------------------------------------- */
  41. static int
  42. sign (int n)
  43. {
  44. return ((n < 0) ? -1 : (n == 0) ? 0 : 1);
  45. }
  46. /* --------------------------------------------------------------------------------------------- */
  47. /* @DataSource("str_verscmp_test_ds") */
  48. /* *INDENT-OFF* */
  49. static const struct str_verscmp_test_struct
  50. {
  51. const char *s1;
  52. const char *s2;
  53. int expected_result;
  54. } str_verscmp_test_ds[] =
  55. {
  56. { "", "", 0 },
  57. { "a", "a", 0 },
  58. { "a", "b", -1 },
  59. { "b", "a", 1 },
  60. { "000", "00", -1 },
  61. { "00", "000", 1 },
  62. { "a0", "a", 1 },
  63. { "00", "01", -1 },
  64. { "01", "010", -1 },
  65. { "010", "09", -1 },
  66. { "09", "0", -1 },
  67. { "9", "10", -1 },
  68. { "0a", "0", 1 },
  69. /* From glibc bug 9913 */
  70. { a, b, -1 },
  71. { b, c, -1 },
  72. { a, c, -1 },
  73. { b, a, 1 },
  74. { c, b, 1 },
  75. { c, a, 1 }
  76. };
  77. /* *INDENT-ON* */
  78. /* @Test(dataSource = "str_verscmp_test_ds") */
  79. /* *INDENT-OFF* */
  80. START_TEST (str_verscmp_test)
  81. /* *INDENT-ON* */
  82. {
  83. /* given */
  84. int actual_result;
  85. const struct str_verscmp_test_struct *data = &str_verscmp_test_ds[_i];
  86. /* when */
  87. actual_result = str_verscmp (data->s1, data->s2);
  88. /* then */
  89. mctest_assert_int_eq (sign (actual_result), sign (data->expected_result));
  90. }
  91. /* *INDENT-OFF* */
  92. END_TEST
  93. /* *INDENT-ON* */
  94. /* --------------------------------------------------------------------------------------------- */
  95. int
  96. main (void)
  97. {
  98. TCase *tc_core;
  99. tc_core = tcase_create ("Core");
  100. tcase_add_checked_fixture (tc_core, setup, teardown);
  101. /* Add new tests here: *************** */
  102. mctest_add_parameterized_test (tc_core, str_verscmp_test, str_verscmp_test_ds);
  103. /* *********************************** */
  104. return mctest_run_all (tc_core);
  105. }
  106. /* --------------------------------------------------------------------------------------------- */