swap.pass.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is dual licensed under the MIT and the University of Illinois Open
  6. // Source Licenses. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. // UNSUPPORTED: c++98, c++03
  10. // <experimental/filesystem>
  11. // class path
  12. // void swap(path& rhs) noexcept;
  13. #include <experimental/filesystem>
  14. #include <type_traits>
  15. #include <cassert>
  16. #include "test_macros.h"
  17. #include "test_iterators.h"
  18. #include "count_new.hpp"
  19. #include "filesystem_test_helper.hpp"
  20. namespace fs = std::experimental::filesystem;
  21. struct SwapTestcase {
  22. const char* value1;
  23. const char* value2;
  24. };
  25. #define LONG_STR1 "_THIS_IS_LONG_THIS_IS_LONG_THIS_IS_LONG_THIS_IS_LONG_THIS_IS_LONG_THIS_IS_LONG_THIS_IS_LONG"
  26. #define LONG_STR2 "_THIS_IS_LONG2_THIS_IS_LONG2_THIS_IS_LONG2_THIS_IS_LONG2_THIS_IS_LONG2_THIS_IS_LONG2_THIS_IS_LONG2"
  27. const SwapTestcase TestCases[] =
  28. {
  29. {"", ""}
  30. , {"shortstr", LONG_STR1}
  31. , {LONG_STR1, "shortstr"}
  32. , {LONG_STR1, LONG_STR2}
  33. };
  34. #undef LONG_STR1
  35. #undef LONG_STR2
  36. int main()
  37. {
  38. using namespace fs;
  39. {
  40. path p;
  41. ASSERT_NOEXCEPT(p.swap(p));
  42. ASSERT_SAME_TYPE(void, decltype(p.swap(p)));
  43. }
  44. for (auto const & TC : TestCases) {
  45. path p1(TC.value1);
  46. path p2(TC.value2);
  47. {
  48. DisableAllocationGuard g;
  49. p1.swap(p2);
  50. }
  51. assert(p1 == TC.value2);
  52. assert(p2 == TC.value1);
  53. {
  54. DisableAllocationGuard g;
  55. p1.swap(p2);
  56. }
  57. assert(p1 == TC.value1);
  58. assert(p2 == TC.value2);
  59. }
  60. // self-swap
  61. {
  62. const char* Val = "aoeuaoeuaoeuaoeuaoeuaoeuaoeuaoeuaoeu";
  63. path p1(Val);
  64. assert(p1 == Val);
  65. {
  66. DisableAllocationGuard g;
  67. p1.swap(p1);
  68. }
  69. assert(p1 == Val);
  70. }
  71. }