move.pass.cpp 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. // UNSUPPORTED: c++98, c++03
  9. // <vector>
  10. // vector(vector&& c);
  11. #include <vector>
  12. #include <cassert>
  13. #include "test_macros.h"
  14. #include "test_allocator.h"
  15. #include "min_allocator.h"
  16. int main(int, char**)
  17. {
  18. {
  19. std::vector<bool, test_allocator<bool> > l(test_allocator<bool>(5));
  20. std::vector<bool, test_allocator<bool> > lo(test_allocator<bool>(5));
  21. for (int i = 1; i <= 3; ++i)
  22. {
  23. l.push_back(true);
  24. lo.push_back(true);
  25. }
  26. std::vector<bool, test_allocator<bool> > l2 = std::move(l);
  27. assert(l2 == lo);
  28. assert(l.empty());
  29. assert(l2.get_allocator() == lo.get_allocator());
  30. }
  31. {
  32. std::vector<bool, other_allocator<bool> > l(other_allocator<bool>(5));
  33. std::vector<bool, other_allocator<bool> > lo(other_allocator<bool>(5));
  34. for (int i = 1; i <= 3; ++i)
  35. {
  36. l.push_back(true);
  37. lo.push_back(true);
  38. }
  39. std::vector<bool, other_allocator<bool> > l2 = std::move(l);
  40. assert(l2 == lo);
  41. assert(l.empty());
  42. assert(l2.get_allocator() == lo.get_allocator());
  43. }
  44. {
  45. std::vector<bool, min_allocator<bool> > l(min_allocator<bool>{});
  46. std::vector<bool, min_allocator<bool> > lo(min_allocator<bool>{});
  47. for (int i = 1; i <= 3; ++i)
  48. {
  49. l.push_back(true);
  50. lo.push_back(true);
  51. }
  52. std::vector<bool, min_allocator<bool> > l2 = std::move(l);
  53. assert(l2 == lo);
  54. assert(l.empty());
  55. assert(l2.get_allocator() == lo.get_allocator());
  56. }
  57. {
  58. test_alloc_base::clear();
  59. using Vect = std::vector<bool, test_allocator<bool> >;
  60. using AllocT = Vect::allocator_type;
  61. Vect v(test_allocator<bool>(42, 101));
  62. assert(test_alloc_base::count == 1);
  63. {
  64. const AllocT& a = v.get_allocator();
  65. assert(test_alloc_base::count == 2);
  66. assert(a.get_data() == 42);
  67. assert(a.get_id() == 101);
  68. }
  69. assert(test_alloc_base::count == 1);
  70. test_alloc_base::clear_ctor_counters();
  71. Vect v2 = std::move(v);
  72. assert(test_alloc_base::count == 2);
  73. assert(test_alloc_base::copied == 0);
  74. assert(test_alloc_base::moved == 1);
  75. {
  76. const AllocT& a = v.get_allocator();
  77. assert(a.get_id() == test_alloc_base::moved_value);
  78. assert(a.get_data() == test_alloc_base::moved_value);
  79. }
  80. {
  81. const AllocT& a = v2.get_allocator();
  82. assert(a.get_id() == 101);
  83. assert(a.get_data() == 42);
  84. }
  85. }
  86. return 0;
  87. }