swap.pass.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. // <vector>
  9. // vector<bool>
  10. // void swap(vector& x);
  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> v1(100);
  20. std::vector<bool> v2(200);
  21. v1.swap(v2);
  22. assert(v1.size() == 200);
  23. assert(v1.capacity() >= 200);
  24. assert(v2.size() == 100);
  25. assert(v2.capacity() >= 100);
  26. }
  27. {
  28. typedef test_allocator<bool> A;
  29. std::vector<bool, A> v1(100, true, A(1, 1));
  30. std::vector<bool, A> v2(200, false, A(1, 2));
  31. swap(v1, v2);
  32. assert(v1.size() == 200);
  33. assert(v1.capacity() >= 200);
  34. assert(v2.size() == 100);
  35. assert(v2.capacity() >= 100);
  36. assert(v1.get_allocator().get_id() == 1);
  37. assert(v2.get_allocator().get_id() == 2);
  38. }
  39. {
  40. typedef other_allocator<bool> A;
  41. std::vector<bool, A> v1(100, true, A(1));
  42. std::vector<bool, A> v2(200, false, A(2));
  43. swap(v1, v2);
  44. assert(v1.size() == 200);
  45. assert(v1.capacity() >= 200);
  46. assert(v2.size() == 100);
  47. assert(v2.capacity() >= 100);
  48. assert(v1.get_allocator() == A(2));
  49. assert(v2.get_allocator() == A(1));
  50. }
  51. {
  52. std::vector<bool> v(2);
  53. std::vector<bool>::reference r1 = v[0];
  54. std::vector<bool>::reference r2 = v[1];
  55. r1 = true;
  56. using std::swap;
  57. swap(r1, r2);
  58. assert(v[0] == false);
  59. assert(v[1] == true);
  60. }
  61. #if TEST_STD_VER >= 11
  62. {
  63. std::vector<bool, min_allocator<bool>> v1(100);
  64. std::vector<bool, min_allocator<bool>> v2(200);
  65. v1.swap(v2);
  66. assert(v1.size() == 200);
  67. assert(v1.capacity() >= 200);
  68. assert(v2.size() == 100);
  69. assert(v2.capacity() >= 100);
  70. }
  71. {
  72. typedef min_allocator<bool> A;
  73. std::vector<bool, A> v1(100, true, A());
  74. std::vector<bool, A> v2(200, false, A());
  75. swap(v1, v2);
  76. assert(v1.size() == 200);
  77. assert(v1.capacity() >= 200);
  78. assert(v2.size() == 100);
  79. assert(v2.capacity() >= 100);
  80. assert(v1.get_allocator() == A());
  81. assert(v2.get_allocator() == A());
  82. }
  83. {
  84. std::vector<bool, min_allocator<bool>> v(2);
  85. std::vector<bool, min_allocator<bool>>::reference r1 = v[0];
  86. std::vector<bool, min_allocator<bool>>::reference r2 = v[1];
  87. r1 = true;
  88. using std::swap;
  89. swap(r1, r2);
  90. assert(v[0] == false);
  91. assert(v[1] == true);
  92. }
  93. #endif
  94. return 0;
  95. }