Revert "[libc] Use best-fit binary trie to make malloc logarithmic" (#117065)
[llvm-project.git] / libcxx / test / std / containers / associative / map / map.modifiers / merge.pass.cpp
blobd760658d54f5fc202d8e527be5c9140934998c94
1 //===----------------------------------------------------------------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: c++03, c++11, c++14
11 // <map>
13 // class map
15 // template <class C2>
16 // void merge(map<key_type, value_type, C2, allocator_type>& source);
17 // template <class C2>
18 // void merge(map<key_type, value_type, C2, allocator_type>&& source);
19 // template <class C2>
20 // void merge(multimap<key_type, value_type, C2, allocator_type>& source);
21 // template <class C2>
22 // void merge(multimap<key_type, value_type, C2, allocator_type>&& source);
24 #include <map>
25 #include <cassert>
26 #include "test_macros.h"
27 #include "Counter.h"
29 template <class Map>
30 bool map_equal(const Map& map, Map other)
32 return map == other;
35 #ifndef TEST_HAS_NO_EXCEPTIONS
36 struct throw_comparator
38 bool& should_throw_;
40 throw_comparator(bool& should_throw) : should_throw_(should_throw) {}
42 template <class T>
43 bool operator()(const T& lhs, const T& rhs) const
45 if (should_throw_)
46 throw 0;
47 return lhs < rhs;
50 #endif
52 int main(int, char**)
55 std::map<int, int> src{{1, 0}, {3, 0}, {5, 0}};
56 std::map<int, int> dst{{2, 0}, {4, 0}, {5, 0}};
57 dst.merge(src);
58 assert(map_equal(src, {{5,0}}));
59 assert(map_equal(dst, {{1, 0}, {2, 0}, {3, 0}, {4, 0}, {5, 0}}));
62 #ifndef TEST_HAS_NO_EXCEPTIONS
64 bool do_throw = false;
65 typedef std::map<Counter<int>, int, throw_comparator> map_type;
66 map_type src({{1, 0}, {3, 0}, {5, 0}}, throw_comparator(do_throw));
67 map_type dst({{2, 0}, {4, 0}, {5, 0}}, throw_comparator(do_throw));
69 assert(Counter_base::gConstructed == 6);
71 do_throw = true;
72 try
74 dst.merge(src);
76 catch (int)
78 do_throw = false;
80 assert(!do_throw);
81 assert(map_equal(src, map_type({{1, 0}, {3, 0}, {5, 0}}, throw_comparator(do_throw))));
82 assert(map_equal(dst, map_type({{2, 0}, {4, 0}, {5, 0}}, throw_comparator(do_throw))));
84 #endif
85 assert(Counter_base::gConstructed == 0);
86 struct comparator
88 comparator() = default;
90 bool operator()(const Counter<int>& lhs, const Counter<int>& rhs) const
92 return lhs < rhs;
96 typedef std::map<Counter<int>, int, std::less<Counter<int>>> first_map_type;
97 typedef std::map<Counter<int>, int, comparator> second_map_type;
98 typedef std::multimap<Counter<int>, int, comparator> third_map_type;
101 first_map_type first{{1, 0}, {2, 0}, {3, 0}};
102 second_map_type second{{2, 0}, {3, 0}, {4, 0}};
103 third_map_type third{{1, 0}, {3, 0}};
105 assert(Counter_base::gConstructed == 8);
107 first.merge(second);
108 first.merge(third);
110 assert(map_equal(first, {{1, 0}, {2, 0}, {3, 0}, {4, 0}}));
111 assert(map_equal(second, {{2, 0}, {3, 0}}));
112 assert(map_equal(third, {{1, 0}, {3, 0}}));
114 assert(Counter_base::gConstructed == 8);
116 assert(Counter_base::gConstructed == 0);
118 first_map_type first{{1, 0}, {2, 0}, {3, 0}};
119 second_map_type second{{2, 0}, {3, 0}, {4, 0}};
120 third_map_type third{{1, 0}, {3, 0}};
122 assert(Counter_base::gConstructed == 8);
124 first.merge(std::move(second));
125 first.merge(std::move(third));
127 assert(map_equal(first, {{1, 0}, {2, 0}, {3, 0}, {4, 0}}));
128 assert(map_equal(second, {{2, 0}, {3, 0}}));
129 assert(map_equal(third, {{1, 0}, {3, 0}}));
131 assert(Counter_base::gConstructed == 8);
133 assert(Counter_base::gConstructed == 0);
135 assert(Counter_base::gConstructed == 0);
137 std::map<int, int> first;
139 std::map<int, int> second;
140 first.merge(second);
141 first.merge(std::move(second));
144 std::multimap<int, int> second;
145 first.merge(second);
146 first.merge(std::move(second));
149 return 0;