llvm-project
149 строк · 3.9 Кб
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// <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>
25#include <cassert>
26#include "test_macros.h"
27#include "Counter.h"
28
29template <class Set>
30bool set_equal(const Set& set, Set other)
31{
32return set == 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::set<int> src{1, 3, 5};
56std::set<int> dst{2, 4, 5};
57dst.merge(src);
58assert(set_equal(src, {5}));
59assert(set_equal(dst, {1, 2, 3, 4, 5}));
60}
61
62#ifndef TEST_HAS_NO_EXCEPTIONS
63{
64bool do_throw = false;
65typedef std::set<Counter<int>, throw_comparator> set_type;
66set_type src({1, 3, 5}, throw_comparator(do_throw));
67set_type dst({2, 4, 5}, 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(set_equal(src, set_type({1, 3, 5}, throw_comparator(do_throw))));
82assert(set_equal(dst, set_type({2, 4, 5}, 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::set<Counter<int>, std::less<Counter<int>>> first_set_type;
97typedef std::set<Counter<int>, comparator> second_set_type;
98typedef std::multiset<Counter<int>, comparator> third_set_type;
99
100{
101first_set_type first{1, 2, 3};
102second_set_type second{2, 3, 4};
103third_set_type third{1, 3};
104
105assert(Counter_base::gConstructed == 8);
106
107first.merge(second);
108first.merge(third);
109
110assert(set_equal(first, {1, 2, 3, 4}));
111assert(set_equal(second, {2, 3}));
112assert(set_equal(third, {1, 3}));
113
114assert(Counter_base::gConstructed == 8);
115}
116assert(Counter_base::gConstructed == 0);
117{
118first_set_type first{1, 2, 3};
119second_set_type second{2, 3, 4};
120third_set_type third{1, 3};
121
122assert(Counter_base::gConstructed == 8);
123
124first.merge(std::move(second));
125first.merge(std::move(third));
126
127assert(set_equal(first, {1, 2, 3, 4}));
128assert(set_equal(second, {2, 3}));
129assert(set_equal(third, {1, 3}));
130
131assert(Counter_base::gConstructed == 8);
132}
133assert(Counter_base::gConstructed == 0);
134}
135{
136std::set<int> first;
137{
138std::set<int> second;
139first.merge(second);
140first.merge(std::move(second));
141}
142{
143std::multiset<int> second;
144first.merge(second);
145first.merge(std::move(second));
146}
147}
148return 0;
149}
150