| 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 <map> |
| 21 | #include <algorithm> |
| 22 | #include <testsuite_hooks.h> |
| 23 | |
| 24 | using test_type = std::map<int, int>; |
| 25 | |
| 26 | void |
| 27 | test01() |
| 28 | { |
| 29 | const test_type c0{ {1, 10}, {2, 20}, {3, 30} }; |
| 30 | test_type c1 = c0, c2 = c0; |
| 31 | |
| 32 | c1.merge(c2); |
| 33 | VERIFY( c1 == c0 ); |
| 34 | VERIFY( c2 == c0 ); |
| 35 | |
| 36 | c1.clear(); |
| 37 | c1.merge(c2); |
| 38 | VERIFY( c1 == c0 ); |
| 39 | VERIFY( c2.empty() ); |
| 40 | |
| 41 | c2.merge(std::move(c1)); |
| 42 | VERIFY( c1.empty() ); |
| 43 | VERIFY( c2 == c0 ); |
| 44 | } |
| 45 | |
| 46 | void |
| 47 | test02() |
| 48 | { |
| 49 | const test_type c0{ {1, 10}, {2, 20}, {3, 30} }; |
| 50 | test_type c1 = c0; |
| 51 | std::map<int, int, std::less<>> c2( c0.begin(), c0.end() ); |
| 52 | |
| 53 | c1.merge(c2); |
| 54 | VERIFY( c1 == c0 ); |
| 55 | VERIFY( std::equal(c2.begin(), c2.end(), c0.begin(), c0.end()) ); |
| 56 | |
| 57 | c1.clear(); |
| 58 | c1.merge(c2); |
| 59 | VERIFY( c1 == c0 ); |
| 60 | VERIFY( c2.empty() ); |
| 61 | |
| 62 | c2.merge(std::move(c1)); |
| 63 | VERIFY( c1.empty() ); |
| 64 | VERIFY( std::equal(c2.begin(), c2.end(), c0.begin(), c0.end()) ); |
| 65 | } |
| 66 | |
| 67 | void |
| 68 | test03() |
| 69 | { |
| 70 | const test_type c0{ {1, 10}, {2, 20}, {3, 30} }; |
| 71 | test_type c1 = c0; |
| 72 | std::map<int, int, std::greater<>> c2( c0.begin(), c0.end() ); |
| 73 | |
| 74 | c1.merge(c2); |
| 75 | VERIFY( c1 == c0 ); |
| 76 | VERIFY( std::equal(c2.rbegin(), c2.rend(), c0.begin(), c0.end()) ); |
| 77 | |
| 78 | c1.clear(); |
| 79 | c1.merge(c2); |
| 80 | VERIFY( c1 == c0 ); |
| 81 | VERIFY( c2.empty() ); |
| 82 | |
| 83 | c2.merge(c1); |
| 84 | VERIFY( c1.empty() ); |
| 85 | VERIFY( std::equal(c2.rbegin(), c2.rend(), c0.begin(), c0.end()) ); |
| 86 | |
| 87 | c1.merge(std::move(c2)); |
| 88 | VERIFY( c1 == c0 ); |
| 89 | VERIFY( c2.empty() ); |
| 90 | } |
| 91 | |
| 92 | void |
| 93 | test04() |
| 94 | { |
| 95 | const test_type c0{ {1, 10}, {2, 20}, {3, 30} }; |
| 96 | test_type c1 = c0; |
| 97 | std::multimap<int, int, std::greater<>> c2( c0.begin(), c0.end() ); |
| 98 | |
| 99 | c1.merge(c2); |
| 100 | VERIFY( c1 == c0 ); |
| 101 | VERIFY( std::equal(c2.rbegin(), c2.rend(), c0.begin(), c0.end()) ); |
| 102 | |
| 103 | c1.clear(); |
| 104 | c1.merge(c2); |
| 105 | VERIFY( c1 == c0 ); |
| 106 | VERIFY( c2.empty() ); |
| 107 | |
| 108 | c2.merge(c1); |
| 109 | VERIFY( c1.empty() ); |
| 110 | VERIFY( std::equal(c2.rbegin(), c2.rend(), c0.begin(), c0.end()) ); |
| 111 | |
| 112 | c1 = c0; |
| 113 | c2.merge(c1); |
| 114 | VERIFY( c1.empty() ); |
| 115 | VERIFY( c2.size() == (2 * c0.size()) ); |
| 116 | VERIFY( std::is_sorted(c2.begin(), c2.end(), c2.value_comp()) ); |
| 117 | |
| 118 | c1.merge(c2); |
| 119 | VERIFY( c1 == c0 ); |
| 120 | VERIFY( std::equal(c2.rbegin(), c2.rend(), c0.begin(), c0.end()) ); |
| 121 | |
| 122 | c1.merge(std::move(c2)); |
| 123 | VERIFY( c1 == c0 ); |
| 124 | VERIFY( std::equal(c2.rbegin(), c2.rend(), c0.begin(), c0.end()) ); |
| 125 | |
| 126 | c1.clear(); |
| 127 | c1.merge(std::move(c2)); |
| 128 | VERIFY( c1 == c0 ); |
| 129 | VERIFY( c2.empty() ); |
| 130 | } |
| 131 | |
| 132 | int |
| 133 | main() |
| 134 | { |
| 135 | test01(); |
| 136 | test02(); |
| 137 | test03(); |
| 138 | test04(); |
| 139 | } |