util.c 925 B

12345678910111213141516171819202122232425262728293031
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include "tests.h"
  3. #include "util/debug.h"
  4. #include <linux/compiler.h>
  5. #include <stdlib.h>
  6. #include <string2.h>
  7. static int test_strreplace(char needle, const char *haystack,
  8. const char *replace, const char *expected)
  9. {
  10. char *new = strreplace_chars(needle, haystack, replace);
  11. int ret = strcmp(new, expected);
  12. free(new);
  13. return ret == 0;
  14. }
  15. static int test__util(struct test_suite *t __maybe_unused, int subtest __maybe_unused)
  16. {
  17. TEST_ASSERT_VAL("empty string", test_strreplace(' ', "", "123", ""));
  18. TEST_ASSERT_VAL("no match", test_strreplace('5', "123", "4", "123"));
  19. TEST_ASSERT_VAL("replace 1", test_strreplace('3', "123", "4", "124"));
  20. TEST_ASSERT_VAL("replace 2", test_strreplace('a', "abcabc", "ef", "efbcefbc"));
  21. TEST_ASSERT_VAL("replace long", test_strreplace('a', "abcabc", "longlong",
  22. "longlongbclonglongbc"));
  23. return 0;
  24. }
  25. DEFINE_SUITE("util", util);