llvm-project
150 строк · 4.4 Кб
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 multimap
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_EXCEPTIONS36struct throw_comparator37{
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) const44{45if (should_throw_)46throw 0;47return lhs < rhs;48}49};50#endif51
52int main(int, char**)53{
54{55std::multimap<int, int> src{{1, 0}, {3, 0}, {5, 0}};56std::multimap<int, int> dst{{2, 0}, {4, 0}, {5, 0}};57dst.merge(src);58assert(map_equal(src, {}));59assert(map_equal(dst, {{1, 0}, {2, 0}, {3, 0}, {4, 0}, {5, 0}, {5, 0}}));60}61
62#ifndef TEST_HAS_NO_EXCEPTIONS63{64bool do_throw = false;65typedef std::multimap<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;72try73{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#endif85assert(Counter_base::gConstructed == 0);86struct comparator87{88comparator() = default;89
90bool operator()(const Counter<int>& lhs, const Counter<int>& rhs) const91{92return lhs < rhs;93}94};95{96typedef std::multimap<Counter<int>, int, std::less<Counter<int>>> first_map_type;97typedef std::multimap<Counter<int>, int, comparator> second_map_type;98typedef std::map<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}, {1, 0}, {2, 0}, {2, 0}, {3, 0}, {3, 0}, {3, 0}, {4, 0}}));111assert(map_equal(second, {}));112assert(map_equal(third, {}));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}, {1, 0}, {2, 0}, {2, 0}, {3, 0}, {3, 0}, {3, 0}, {4, 0}}));128assert(map_equal(second, {}));129assert(map_equal(third, {}));130
131assert(Counter_base::gConstructed == 8);132}133assert(Counter_base::gConstructed == 0);134}135assert(Counter_base::gConstructed == 0);136{137std::multimap<int, int> first;138{139std::multimap<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