Erik Pilkington | 5c4e07a | 2018-10-31 17:31:35 +0000 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
Chandler Carruth | 57b08b0 | 2019-01-19 10:56:40 +0000 | [diff] [blame] | 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 |
Erik Pilkington | 5c4e07a | 2018-10-31 17:31:35 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
Louis Dionne | 31cbe0f | 2020-06-01 10:38:23 -0400 | [diff] [blame^] | 9 | // UNSUPPORTED: c++03, c++11, c++14 |
Erik Pilkington | 5c4e07a | 2018-10-31 17:31:35 +0000 | [diff] [blame] | 10 | |
| 11 | // <set> |
| 12 | |
| 13 | // class set |
| 14 | |
| 15 | // template <class C2> |
| 16 | // void merge(set<key_type, C2, allocator_type>& source); |
| 17 | // template <class C2> |
| 18 | // void merge(set<key_type, C2, allocator_type>&& source); |
| 19 | // template <class C2> |
| 20 | // void merge(multiset<key_type, C2, allocator_type>& source); |
| 21 | // template <class C2> |
| 22 | // void merge(multiset<key_type, C2, allocator_type>&& source); |
| 23 | |
| 24 | #include <set> |
Stephan T. Lavavej | 174072c | 2018-11-14 23:23:51 +0000 | [diff] [blame] | 25 | #include <cassert> |
Erik Pilkington | 5c4e07a | 2018-10-31 17:31:35 +0000 | [diff] [blame] | 26 | #include "test_macros.h" |
| 27 | #include "Counter.h" |
| 28 | |
| 29 | template <class Set> |
| 30 | bool set_equal(const Set& set, Set other) |
| 31 | { |
| 32 | return set == other; |
| 33 | } |
| 34 | |
| 35 | #ifndef TEST_HAS_NO_EXCEPTIONS |
| 36 | struct throw_comparator |
| 37 | { |
| 38 | bool& should_throw_; |
| 39 | |
| 40 | throw_comparator(bool& should_throw) : should_throw_(should_throw) {} |
| 41 | |
| 42 | template <class T> |
| 43 | bool operator()(const T& lhs, const T& rhs) const |
| 44 | { |
| 45 | if (should_throw_) |
| 46 | throw 0; |
| 47 | return lhs < rhs; |
| 48 | } |
| 49 | }; |
| 50 | #endif |
| 51 | |
JF Bastien | 2df59c5 | 2019-02-04 20:31:13 +0000 | [diff] [blame] | 52 | int main(int, char**) |
Erik Pilkington | 5c4e07a | 2018-10-31 17:31:35 +0000 | [diff] [blame] | 53 | { |
| 54 | { |
| 55 | std::set<int> src{1, 3, 5}; |
| 56 | std::set<int> dst{2, 4, 5}; |
| 57 | dst.merge(src); |
| 58 | assert(set_equal(src, {5})); |
| 59 | assert(set_equal(dst, {1, 2, 3, 4, 5})); |
| 60 | } |
| 61 | |
| 62 | #ifndef TEST_HAS_NO_EXCEPTIONS |
| 63 | { |
| 64 | bool do_throw = false; |
| 65 | typedef std::set<Counter<int>, throw_comparator> set_type; |
| 66 | set_type src({1, 3, 5}, throw_comparator(do_throw)); |
| 67 | set_type dst({2, 4, 5}, throw_comparator(do_throw)); |
| 68 | |
| 69 | assert(Counter_base::gConstructed == 6); |
| 70 | |
| 71 | do_throw = true; |
| 72 | try |
| 73 | { |
| 74 | dst.merge(src); |
| 75 | } |
| 76 | catch (int) |
| 77 | { |
| 78 | do_throw = false; |
| 79 | } |
| 80 | assert(!do_throw); |
| 81 | assert(set_equal(src, set_type({1, 3, 5}, throw_comparator(do_throw)))); |
| 82 | assert(set_equal(dst, set_type({2, 4, 5}, throw_comparator(do_throw)))); |
| 83 | } |
| 84 | #endif |
| 85 | assert(Counter_base::gConstructed == 0); |
| 86 | struct comparator |
| 87 | { |
| 88 | comparator() = default; |
| 89 | |
| 90 | bool operator()(const Counter<int>& lhs, const Counter<int>& rhs) const |
| 91 | { |
| 92 | return lhs < rhs; |
| 93 | } |
| 94 | }; |
| 95 | { |
| 96 | typedef std::set<Counter<int>, std::less<Counter<int>>> first_set_type; |
| 97 | typedef std::set<Counter<int>, comparator> second_set_type; |
| 98 | typedef std::multiset<Counter<int>, comparator> third_set_type; |
| 99 | |
| 100 | { |
| 101 | first_set_type first{1, 2, 3}; |
| 102 | second_set_type second{2, 3, 4}; |
| 103 | third_set_type third{1, 3}; |
| 104 | |
| 105 | assert(Counter_base::gConstructed == 8); |
| 106 | |
| 107 | first.merge(second); |
| 108 | first.merge(third); |
| 109 | |
| 110 | assert(set_equal(first, {1, 2, 3, 4})); |
| 111 | assert(set_equal(second, {2, 3})); |
| 112 | assert(set_equal(third, {1, 3})); |
| 113 | |
| 114 | assert(Counter_base::gConstructed == 8); |
| 115 | } |
| 116 | assert(Counter_base::gConstructed == 0); |
| 117 | { |
| 118 | first_set_type first{1, 2, 3}; |
| 119 | second_set_type second{2, 3, 4}; |
| 120 | third_set_type third{1, 3}; |
| 121 | |
| 122 | assert(Counter_base::gConstructed == 8); |
| 123 | |
| 124 | first.merge(std::move(second)); |
| 125 | first.merge(std::move(third)); |
| 126 | |
| 127 | assert(set_equal(first, {1, 2, 3, 4})); |
| 128 | assert(set_equal(second, {2, 3})); |
| 129 | assert(set_equal(third, {1, 3})); |
| 130 | |
| 131 | assert(Counter_base::gConstructed == 8); |
| 132 | } |
| 133 | assert(Counter_base::gConstructed == 0); |
| 134 | } |
| 135 | { |
| 136 | std::set<int> first; |
| 137 | { |
| 138 | std::set<int> second; |
| 139 | first.merge(second); |
| 140 | first.merge(std::move(second)); |
| 141 | } |
| 142 | { |
| 143 | std::multiset<int> second; |
| 144 | first.merge(second); |
| 145 | first.merge(std::move(second)); |
| 146 | } |
| 147 | } |
JF Bastien | 2df59c5 | 2019-02-04 20:31:13 +0000 | [diff] [blame] | 148 | return 0; |
Erik Pilkington | 5c4e07a | 2018-10-31 17:31:35 +0000 | [diff] [blame] | 149 | } |