| 1 | // Copyright (C) 2016-2025 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 | |
| 9 | // This library is distributed in the hope that it will be useful, |
| 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | // GNU General Public License for more details. |
| 13 | |
| 14 | // You should have received a copy of the GNU General Public License along |
| 15 | // with this library; see the file COPYING3. If not see |
| 16 | // <http://www.gnu.org/licenses/>. |
| 17 | |
| 18 | // { dg-do run { target c++17 } } |
| 19 | |
| 20 | #include <set> |
| 21 | #include <algorithm> |
| 22 | #include <testsuite_hooks.h> |
| 23 | |
| 24 | using test_type = std::set<int>; |
| 25 | |
| 26 | void |
| 27 | test01() |
| 28 | { |
| 29 | const test_type c0{ 1, 2, 3 }; |
| 30 | test_type c1 = c0, c2 = c0; |
| 31 | c1.merge(c2); |
| 32 | VERIFY( c1 == c0 ); |
| 33 | VERIFY( c2 == c0 ); |
| 34 | |
| 35 | c1.clear(); |
| 36 | c1.merge(c2); |
| 37 | VERIFY( c1 == c0 ); |
| 38 | VERIFY( c2.empty() ); |
| 39 | |
| 40 | c2.merge(std::move(c1)); |
| 41 | VERIFY( c1.empty() ); |
| 42 | VERIFY( c2 == c0 ); |
| 43 | } |
| 44 | |
| 45 | void |
| 46 | test02() |
| 47 | { |
| 48 | const test_type c0{ 1, 2, 3 }; |
| 49 | test_type c1 = c0; |
| 50 | std::set<int, std::less<>> c2( c0.begin(), c0.end() ); |
| 51 | c1.merge(c2); |
| 52 | VERIFY( c1 == c0 ); |
| 53 | VERIFY( std::equal(c2.begin(), c2.end(), c0.begin(), c0.end()) ); |
| 54 | |
| 55 | c1.clear(); |
| 56 | c1.merge(c2); |
| 57 | VERIFY( c1 == c0 ); |
| 58 | VERIFY( c2.empty() ); |
| 59 | |
| 60 | c2.merge(std::move(c1)); |
| 61 | VERIFY( c1.empty() ); |
| 62 | VERIFY( std::equal(c2.begin(), c2.end(), c0.begin(), c0.end()) ); |
| 63 | } |
| 64 | |
| 65 | void |
| 66 | test03() |
| 67 | { |
| 68 | const test_type c0{ 1, 2, 3 }; |
| 69 | test_type c1 = c0; |
| 70 | std::set<int, std::greater<>> c2( c0.begin(), c0.end() ); |
| 71 | c1.merge(c2); |
| 72 | VERIFY( c1 == c0 ); |
| 73 | VERIFY( std::equal(c2.rbegin(), c2.rend(), c0.begin(), c0.end()) ); |
| 74 | |
| 75 | c1.clear(); |
| 76 | c1.merge(c2); |
| 77 | VERIFY( c1 == c0 ); |
| 78 | VERIFY( c2.empty() ); |
| 79 | |
| 80 | c2.merge(c1); |
| 81 | VERIFY( c1.empty() ); |
| 82 | VERIFY( std::equal(c2.rbegin(), c2.rend(), c0.begin(), c0.end()) ); |
| 83 | |
| 84 | c1.merge(std::move(c2)); |
| 85 | VERIFY( c1 == c0 ); |
| 86 | VERIFY( c2.empty() ); |
| 87 | } |
| 88 | |
| 89 | void |
| 90 | test04() |
| 91 | { |
| 92 | const test_type c0{ 1, 2, 3 }; |
| 93 | test_type c1 = c0; |
| 94 | std::multiset<int, std::greater<>> c2( c0.begin(), c0.end() ); |
| 95 | c1.merge(c2); |
| 96 | VERIFY( c1 == c0 ); |
| 97 | VERIFY( std::equal(c2.rbegin(), c2.rend(), c0.begin(), c0.end()) ); |
| 98 | |
| 99 | c1.clear(); |
| 100 | c1.merge(c2); |
| 101 | VERIFY( c1 == c0 ); |
| 102 | VERIFY( c2.empty() ); |
| 103 | |
| 104 | c2.merge(c1); |
| 105 | VERIFY( c1.empty() ); |
| 106 | VERIFY( std::equal(c2.rbegin(), c2.rend(), c0.begin(), c0.end()) ); |
| 107 | |
| 108 | c1 = c0; |
| 109 | c2.merge(c1); |
| 110 | VERIFY( c1.empty() ); |
| 111 | VERIFY( c2.size() == (2 * c0.size()) ); |
| 112 | VERIFY( std::is_sorted(c2.begin(), c2.end(), c2.value_comp()) ); |
| 113 | |
| 114 | c1.merge(c2); |
| 115 | VERIFY( c1 == c0 ); |
| 116 | VERIFY( std::equal(c2.rbegin(), c2.rend(), c0.begin(), c0.end()) ); |
| 117 | |
| 118 | c1.merge(std::move(c2)); |
| 119 | VERIFY( c1 == c0 ); |
| 120 | VERIFY( std::equal(c2.rbegin(), c2.rend(), c0.begin(), c0.end()) ); |
| 121 | |
| 122 | c1.clear(); |
| 123 | c1.merge(std::move(c2)); |
| 124 | VERIFY( c1 == c0 ); |
| 125 | VERIFY( c2.empty() ); |
| 126 | } |
| 127 | |
| 128 | int |
| 129 | main() |
| 130 | { |
| 131 | test01(); |
| 132 | test02(); |
| 133 | test03(); |
| 134 | test04(); |
| 135 | } |