utils-selftests.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /* Unit tests for the utils.c file.
  2. Copyright (C) 2018-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program 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
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #include "defs.h"
  15. #include "utils.h"
  16. #include "gdbsupport/selftest.h"
  17. namespace selftests {
  18. namespace utils {
  19. static void
  20. test_substitute_path_component ()
  21. {
  22. auto test = [] (std::string s, const char *from, const char *to,
  23. const char *expected)
  24. {
  25. char *temp = xstrdup (s.c_str ());
  26. substitute_path_component (&temp, from, to);
  27. SELF_CHECK (strcmp (temp, expected) == 0);
  28. xfree (temp);
  29. };
  30. test ("/abc/$def/g", "abc", "xyz", "/xyz/$def/g");
  31. test ("abc/$def/g", "abc", "xyz", "xyz/$def/g");
  32. test ("/abc/$def/g", "$def", "xyz", "/abc/xyz/g");
  33. test ("/abc/$def/g", "g", "xyz", "/abc/$def/xyz");
  34. test ("/abc/$def/g", "ab", "xyz", "/abc/$def/g");
  35. test ("/abc/$def/g", "def", "xyz", "/abc/$def/g");
  36. test ("/abc/$def/g", "abc", "abc", "/abc/$def/g");
  37. test ("/abc/$def/g", "abc", "", "//$def/g");
  38. test ("/abc/$def/g", "abc/$def", "xyz", "/xyz/g");
  39. test ("/abc/$def/abc", "abc", "xyz", "/xyz/$def/xyz");
  40. }
  41. }
  42. }
  43. void _initialize_utils_selftests ();
  44. void
  45. _initialize_utils_selftests ()
  46. {
  47. selftests::register_test ("substitute_path_component",
  48. selftests::utils::test_substitute_path_component);
  49. }