| 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::multiset<int>; |
| 25 | |
| 26 | void |
| 27 | test01() |
| 28 | { |
| 29 | test_type c0{ 1, 1, 2, 2, 3, 3 }; |
| 30 | test_type c1 = c0, c2 = c0; |
| 31 | |
| 32 | c1.merge(c2); |
| 33 | for (auto i : c1) |
| 34 | VERIFY( c1.count(i) == (2 * c0.count(i)) ); |
| 35 | VERIFY( c2.empty() ); |
| 36 | |
| 37 | c1.clear(); |
| 38 | c2 = c0; |
| 39 | c1.merge(std::move(c2)); |
| 40 | VERIFY( c1 == c0 ); |
| 41 | VERIFY( c2.empty() ); |
| 42 | } |
| 43 | |
| 44 | void |
| 45 | test02() |
| 46 | { |
| 47 | test_type c0{ 1, 1, 2, 2, 3, 3 }; |
| 48 | test_type c1 = c0; |
| 49 | std::multiset<int, std::less<>> c2( c0.begin(), c0.end() ); |
| 50 | |
| 51 | c1.merge(c2); |
| 52 | VERIFY( c1.size() == (2 * c0.size()) ); |
| 53 | for (auto i : c1) |
| 54 | VERIFY( c1.count(i) == (2 * c0.count(i)) ); |
| 55 | VERIFY( c2.empty() ); |
| 56 | |
| 57 | c1.clear(); |
| 58 | c2.insert( c0.begin(), c0.end() ); |
| 59 | c1.merge(std::move(c2)); |
| 60 | VERIFY( c1 == c0 ); |
| 61 | VERIFY( c2.empty() ); |
| 62 | } |
| 63 | |
| 64 | void |
| 65 | test03() |
| 66 | { |
| 67 | const test_type c0{ 1, 1, 2, 2, 3, 3 }; |
| 68 | test_type c1 = c0; |
| 69 | std::multiset<int, std::greater<>> c2( c0.begin(), c0.end() ); |
| 70 | |
| 71 | c1.merge(c2); |
| 72 | VERIFY( c1.size() == (2 * c0.size()) ); |
| 73 | for (auto i : c1) |
| 74 | VERIFY( c1.count(i) == (2 * c0.count(i)) ); |
| 75 | VERIFY( c2.empty() ); |
| 76 | |
| 77 | c1.clear(); |
| 78 | c2.insert( c0.begin(), c0.end() ); |
| 79 | c1.merge(std::move(c2)); |
| 80 | VERIFY( c1 == c0 ); |
| 81 | VERIFY( c2.empty() ); |
| 82 | } |
| 83 | |
| 84 | void |
| 85 | test04() |
| 86 | { |
| 87 | const test_type c0{ 1, 1, 2, 2, 3, 3 }; |
| 88 | test_type c1 = c0; |
| 89 | std::set<int, std::greater<>> c2( c0.begin(), c0.end() ); |
| 90 | |
| 91 | c1.merge(c2); |
| 92 | VERIFY( c1.size() == (1.5 * c0.size()) ); |
| 93 | for (auto& i : c1) |
| 94 | VERIFY( c1.count(i) == (1.5 * c0.count(i)) ); |
| 95 | VERIFY( c2.empty() ); |
| 96 | |
| 97 | c1.clear(); |
| 98 | c2.insert( c0.begin(), c0.end() ); |
| 99 | c1.merge(std::move(c2)); |
| 100 | VERIFY( c1.size() == (0.5 * c0.size()) ); |
| 101 | VERIFY( c2.empty() ); |
| 102 | } |
| 103 | |
| 104 | int |
| 105 | main() |
| 106 | { |
| 107 | test01(); |
| 108 | test02(); |
| 109 | test03(); |
| 110 | test04(); |
| 111 | } |