swap.pass.cpp 2.2 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. // <array>
  9. // void swap(array& a);
  10. // namespace std { void swap(array<T, N> &x, array<T, N> &y);
  11. #include <cassert>
  12. #include <array>
  13. #include "test_macros.h"
  14. // std::array is explicitly allowed to be initialized with A a = { init-list };.
  15. // Disable the missing braces warning for this reason.
  16. #include "disable_missing_braces_warning.h"
  17. struct NonSwappable {
  18. NonSwappable() {}
  19. private:
  20. NonSwappable(NonSwappable const&);
  21. NonSwappable& operator=(NonSwappable const&);
  22. };
  23. int main(int, char**)
  24. {
  25. {
  26. typedef double T;
  27. typedef std::array<T, 3> C;
  28. C c1 = {1, 2, 3.5};
  29. C c2 = {4, 5, 6.5};
  30. c1.swap(c2);
  31. assert(c1.size() == 3);
  32. assert(c1[0] == 4);
  33. assert(c1[1] == 5);
  34. assert(c1[2] == 6.5);
  35. assert(c2.size() == 3);
  36. assert(c2[0] == 1);
  37. assert(c2[1] == 2);
  38. assert(c2[2] == 3.5);
  39. }
  40. {
  41. typedef double T;
  42. typedef std::array<T, 3> C;
  43. C c1 = {1, 2, 3.5};
  44. C c2 = {4, 5, 6.5};
  45. std::swap(c1, c2);
  46. assert(c1.size() == 3);
  47. assert(c1[0] == 4);
  48. assert(c1[1] == 5);
  49. assert(c1[2] == 6.5);
  50. assert(c2.size() == 3);
  51. assert(c2[0] == 1);
  52. assert(c2[1] == 2);
  53. assert(c2[2] == 3.5);
  54. }
  55. {
  56. typedef double T;
  57. typedef std::array<T, 0> C;
  58. C c1 = {};
  59. C c2 = {};
  60. c1.swap(c2);
  61. assert(c1.size() == 0);
  62. assert(c2.size() == 0);
  63. }
  64. {
  65. typedef double T;
  66. typedef std::array<T, 0> C;
  67. C c1 = {};
  68. C c2 = {};
  69. std::swap(c1, c2);
  70. assert(c1.size() == 0);
  71. assert(c2.size() == 0);
  72. }
  73. {
  74. typedef NonSwappable T;
  75. typedef std::array<T, 0> C0;
  76. C0 l = {};
  77. C0 r = {};
  78. l.swap(r);
  79. #if TEST_STD_VER >= 11
  80. static_assert(noexcept(l.swap(r)), "");
  81. #endif
  82. }
  83. return 0;
  84. }