merge.cc 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // Copyright (C) 2016-2022 Free Software Foundation, Inc.
  2. //
  3. // This file is part of the GNU ISO C++ Library. This library is free
  4. // software; you can redistribute it and/or modify it under the
  5. // terms of the GNU General Public License as published by the
  6. // Free Software Foundation; either version 3, or (at your option)
  7. // any later version.
  8. // This library is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. // You should have received a copy of the GNU General Public License along
  13. // with this library; see the file COPYING3. If not see
  14. // <http://www.gnu.org/licenses/>.
  15. // { dg-do run { target c++17 } }
  16. #include <set>
  17. #include <algorithm>
  18. #include <testsuite_hooks.h>
  19. using test_type = std::set<int>;
  20. void
  21. test01()
  22. {
  23. const test_type c0{ 1, 2, 3 };
  24. test_type c1 = c0, c2 = c0;
  25. c1.merge(c2);
  26. VERIFY( c1 == c0 );
  27. VERIFY( c2 == c0 );
  28. c1.clear();
  29. c1.merge(c2);
  30. VERIFY( c1 == c0 );
  31. VERIFY( c2.empty() );
  32. c2.merge(std::move(c1));
  33. VERIFY( c1.empty() );
  34. VERIFY( c2 == c0 );
  35. }
  36. void
  37. test02()
  38. {
  39. const test_type c0{ 1, 2, 3 };
  40. test_type c1 = c0;
  41. std::set<int, std::less<>> c2( c0.begin(), c0.end() );
  42. c1.merge(c2);
  43. VERIFY( c1 == c0 );
  44. VERIFY( std::equal(c2.begin(), c2.end(), c0.begin(), c0.end()) );
  45. c1.clear();
  46. c1.merge(c2);
  47. VERIFY( c1 == c0 );
  48. VERIFY( c2.empty() );
  49. c2.merge(std::move(c1));
  50. VERIFY( c1.empty() );
  51. VERIFY( std::equal(c2.begin(), c2.end(), c0.begin(), c0.end()) );
  52. }
  53. void
  54. test03()
  55. {
  56. const test_type c0{ 1, 2, 3 };
  57. test_type c1 = c0;
  58. std::set<int, std::greater<>> c2( c0.begin(), c0.end() );
  59. c1.merge(c2);
  60. VERIFY( c1 == c0 );
  61. VERIFY( std::equal(c2.rbegin(), c2.rend(), c0.begin(), c0.end()) );
  62. c1.clear();
  63. c1.merge(c2);
  64. VERIFY( c1 == c0 );
  65. VERIFY( c2.empty() );
  66. c2.merge(c1);
  67. VERIFY( c1.empty() );
  68. VERIFY( std::equal(c2.rbegin(), c2.rend(), c0.begin(), c0.end()) );
  69. c1.merge(std::move(c2));
  70. VERIFY( c1 == c0 );
  71. VERIFY( c2.empty() );
  72. }
  73. void
  74. test04()
  75. {
  76. const test_type c0{ 1, 2, 3 };
  77. test_type c1 = c0;
  78. std::multiset<int, std::greater<>> c2( c0.begin(), c0.end() );
  79. c1.merge(c2);
  80. VERIFY( c1 == c0 );
  81. VERIFY( std::equal(c2.rbegin(), c2.rend(), c0.begin(), c0.end()) );
  82. c1.clear();
  83. c1.merge(c2);
  84. VERIFY( c1 == c0 );
  85. VERIFY( c2.empty() );
  86. c2.merge(c1);
  87. VERIFY( c1.empty() );
  88. VERIFY( std::equal(c2.rbegin(), c2.rend(), c0.begin(), c0.end()) );
  89. c1 = c0;
  90. c2.merge(c1);
  91. VERIFY( c1.empty() );
  92. VERIFY( c2.size() == (2 * c0.size()) );
  93. VERIFY( std::is_sorted(c2.begin(), c2.end(), c2.value_comp()) );
  94. c1.merge(c2);
  95. VERIFY( c1 == c0 );
  96. VERIFY( std::equal(c2.rbegin(), c2.rend(), c0.begin(), c0.end()) );
  97. c1.merge(std::move(c2));
  98. VERIFY( c1 == c0 );
  99. VERIFY( std::equal(c2.rbegin(), c2.rend(), c0.begin(), c0.end()) );
  100. c1.clear();
  101. c1.merge(std::move(c2));
  102. VERIFY( c1 == c0 );
  103. VERIFY( c2.empty() );
  104. }
  105. int
  106. main()
  107. {
  108. test01();
  109. test02();
  110. test03();
  111. test04();
  112. }