swap.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Range v3 library
  2. //
  3. // Copyright Eric Niebler 2013-present
  4. //
  5. // Use, modification and distribution is subject to the
  6. // Boost Software License, Version 1.0. (See accompanying
  7. // file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. // Acknowledgements: Thanks for Paul Fultz for the suggestions that
  11. // concepts can be ordinary Boolean metafunctions.
  12. //
  13. // Project home: https://github.com/ericniebler/range-v3
  14. //
  15. #include <iostream>
  16. #include <tuple>
  17. #include <memory>
  18. #include <vector>
  19. #include <complex>
  20. #include <concepts/concepts.hpp>
  21. #include <range/v3/utility/swap.hpp>
  22. #include <range/v3/view/zip.hpp>
  23. #include <range/v3/range/conversion.hpp>
  24. #include "../simple_test.hpp"
  25. #include "../test_utils.hpp"
  26. template<typename T>
  27. struct S
  28. {
  29. T t;
  30. };
  31. int main()
  32. {
  33. int a=0,b=42;
  34. ranges::swap(a,b);
  35. CHECK(a == 42);
  36. CHECK(b == 0);
  37. CPP_assert(!ranges::swappable_with<std::pair<int,int>&&,std::pair<int,int>&&>);
  38. CPP_assert(ranges::swappable_with<std::pair<int&,int&>&&,std::pair<int&,int&>&&>);
  39. int c=24,d=82;
  40. ranges::swap(std::tie(a,b),std::tie(c,d));
  41. CHECK(a == 24);
  42. CHECK(b == 82);
  43. CHECK(c == 42);
  44. CHECK(d == 0);
  45. // Swap pairs of tuple proxies
  46. int e=1,f=2,g=3,h=4;
  47. ranges::swap(std::make_pair(std::tie(a,b), std::tie(c,d)), std::make_pair(std::tie(e,f), std::tie(g,h)));
  48. CHECK(a == 1);
  49. CHECK(b == 2);
  50. CHECK(c == 3);
  51. CHECK(d == 4);
  52. CHECK(e == 24);
  53. CHECK(f == 82);
  54. CHECK(g == 42);
  55. CHECK(h == 0);
  56. #ifndef _LIBCPP_VERSION
  57. // Swap tuples of pair proxies
  58. ranges::swap(std::make_tuple(std::make_pair(std::ref(a),std::ref(b)), std::make_pair(std::ref(c),std::ref(d))),
  59. std::make_tuple(std::make_pair(std::ref(e),std::ref(f)), std::make_pair(std::ref(g),std::ref(h))));
  60. CHECK(a == 24);
  61. CHECK(b == 82);
  62. CHECK(c == 42);
  63. CHECK(d == 0);
  64. CHECK(e == 1);
  65. CHECK(f == 2);
  66. CHECK(g == 3);
  67. CHECK(h == 4);
  68. #endif
  69. int aa=24,bb=82;
  70. ranges::iter_swap(&aa, &bb);
  71. CHECK(aa == 82);
  72. CHECK(bb == 24);
  73. std::unique_ptr<int> u0{new int{1}};
  74. std::unique_ptr<int> u1{new int{2}};
  75. int *p0 = u0.get();
  76. int *p1 = u1.get();
  77. ranges::iter_swap(&u0, &u1);
  78. CHECK(u0.get() == p1);
  79. CHECK(u1.get() == p0);
  80. {
  81. using namespace ranges;
  82. auto v0 = to<std::vector<MoveOnlyString>>({"a","b","c"});
  83. auto v1 = to<std::vector<MoveOnlyString>>({"x","y","z"});
  84. auto rng = views::zip(v0, v1);
  85. ranges::iter_swap(rng.begin(), rng.begin()+2);
  86. ::check_equal(v0, {"c","b","a"});
  87. ::check_equal(v1, {"z","y","x"});
  88. }
  89. {
  90. using T = std::complex<float>;
  91. T s,t;
  92. ranges::swap(s,t);
  93. }
  94. {
  95. using T = S<std::complex<float>>;
  96. T s,t;
  97. ranges::swap(s,t);
  98. }
  99. return ::test_result();
  100. }