llvm-project
150 строк · 4.3 Кб
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//===----------------------------------------------------------------------===//
8
9// UNSUPPORTED: c++03, c++11, c++14
10
11// <map>
12
13// class map
14
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);
23
24#include <map>
25#include <cassert>
26#include "test_macros.h"
27#include "Counter.h"
28
29template <class Map>
30bool map_equal(const Map& map, Map other)
31{
32return map == other;
33}
34
35#ifndef TEST_HAS_NO_EXCEPTIONS
36struct throw_comparator
37{
38bool& should_throw_;
39
40throw_comparator(bool& should_throw) : should_throw_(should_throw) {}
41
42template <class T>
43bool operator()(const T& lhs, const T& rhs) const
44{
45if (should_throw_)
46throw 0;
47return lhs < rhs;
48}
49};
50#endif
51
52int main(int, char**)
53{
54{
55std::map<int, int> src{{1, 0}, {3, 0}, {5, 0}};
56std::map<int, int> dst{{2, 0}, {4, 0}, {5, 0}};
57dst.merge(src);
58assert(map_equal(src, {{5,0}}));
59assert(map_equal(dst, {{1, 0}, {2, 0}, {3, 0}, {4, 0}, {5, 0}}));
60}
61
62#ifndef TEST_HAS_NO_EXCEPTIONS
63{
64bool do_throw = false;
65typedef std::map<Counter<int>, int, throw_comparator> map_type;
66map_type src({{1, 0}, {3, 0}, {5, 0}}, throw_comparator(do_throw));
67map_type dst({{2, 0}, {4, 0}, {5, 0}}, throw_comparator(do_throw));
68
69assert(Counter_base::gConstructed == 6);
70
71do_throw = true;
72try
73{
74dst.merge(src);
75}
76catch (int)
77{
78do_throw = false;
79}
80assert(!do_throw);
81assert(map_equal(src, map_type({{1, 0}, {3, 0}, {5, 0}}, throw_comparator(do_throw))));
82assert(map_equal(dst, map_type({{2, 0}, {4, 0}, {5, 0}}, throw_comparator(do_throw))));
83}
84#endif
85assert(Counter_base::gConstructed == 0);
86struct comparator
87{
88comparator() = default;
89
90bool operator()(const Counter<int>& lhs, const Counter<int>& rhs) const
91{
92return lhs < rhs;
93}
94};
95{
96typedef std::map<Counter<int>, int, std::less<Counter<int>>> first_map_type;
97typedef std::map<Counter<int>, int, comparator> second_map_type;
98typedef std::multimap<Counter<int>, int, comparator> third_map_type;
99
100{
101first_map_type first{{1, 0}, {2, 0}, {3, 0}};
102second_map_type second{{2, 0}, {3, 0}, {4, 0}};
103third_map_type third{{1, 0}, {3, 0}};
104
105assert(Counter_base::gConstructed == 8);
106
107first.merge(second);
108first.merge(third);
109
110assert(map_equal(first, {{1, 0}, {2, 0}, {3, 0}, {4, 0}}));
111assert(map_equal(second, {{2, 0}, {3, 0}}));
112assert(map_equal(third, {{1, 0}, {3, 0}}));
113
114assert(Counter_base::gConstructed == 8);
115}
116assert(Counter_base::gConstructed == 0);
117{
118first_map_type first{{1, 0}, {2, 0}, {3, 0}};
119second_map_type second{{2, 0}, {3, 0}, {4, 0}};
120third_map_type third{{1, 0}, {3, 0}};
121
122assert(Counter_base::gConstructed == 8);
123
124first.merge(std::move(second));
125first.merge(std::move(third));
126
127assert(map_equal(first, {{1, 0}, {2, 0}, {3, 0}, {4, 0}}));
128assert(map_equal(second, {{2, 0}, {3, 0}}));
129assert(map_equal(third, {{1, 0}, {3, 0}}));
130
131assert(Counter_base::gConstructed == 8);
132}
133assert(Counter_base::gConstructed == 0);
134}
135assert(Counter_base::gConstructed == 0);
136{
137std::map<int, int> first;
138{
139std::map<int, int> second;
140first.merge(second);
141first.merge(std::move(second));
142}
143{
144std::multimap<int, int> second;
145first.merge(second);
146first.merge(std::move(second));
147}
148}
149return 0;
150}
151