summaryrefslogtreecommitdiff
path: root/test/std/containers/unord/unord.set/merge.pass.cpp
blob: ebf03cd565ab840cb4a00d92a5adb4d1c42636df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
//===----------------------------------------------------------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

// UNSUPPORTED: c++98, c++03, c++11, c++14

// <unordered_set>

// class unordered_set

// template <class H2, class P2>
//   void merge(unordered_set<key_type, H2, P2, allocator_type>& source);
// template <class H2, class P2>
//   void merge(unordered_set<key_type, H2, P2, allocator_type>&& source);
// template <class H2, class P2>
//   void merge(unordered_multiset<key_type, H2, P2, allocator_type>& source);
// template <class H2, class P2>
//   void merge(unordered_multiset<key_type, H2, P2, allocator_type>&& source);

#include <unordered_set>
#include "test_macros.h"
#include "Counter.h"

template <class Set>
bool set_equal(const Set& set, Set other)
{
    return set == other;
}

#ifndef TEST_HAS_NO_EXCEPTIONS
template <class T>
struct throw_hasher
{
    bool& should_throw_;

    throw_hasher(bool& should_throw) : should_throw_(should_throw) {}

    typedef size_t result_type;
    typedef T argument_type;

    size_t operator()(const T& p) const
    {
        if (should_throw_)
            throw 0;
        return std::hash<T>()(p);
    }
};
#endif

int main()
{
    {
        std::unordered_set<int> src{1, 3, 5};
        std::unordered_set<int> dst{2, 4, 5};
        dst.merge(src);
        assert(set_equal(src, {5}));
        assert(set_equal(dst, {1, 2, 3, 4, 5}));
    }

#ifndef TEST_HAS_NO_EXCEPTIONS
    {
        bool do_throw = false;
        typedef std::unordered_set<Counter<int>, throw_hasher<Counter<int>>> set_type;
        set_type src({1, 3, 5}, 0, throw_hasher<Counter<int>>(do_throw));
        set_type dst({2, 4, 5}, 0, throw_hasher<Counter<int>>(do_throw));

        assert(Counter_base::gConstructed == 6);

        do_throw = true;
        try
        {
            dst.merge(src);
        }
        catch (int)
        {
            do_throw = false;
        }
        assert(!do_throw);
        assert(set_equal(src, set_type({1, 3, 5}, 0, throw_hasher<Counter<int>>(do_throw))));
        assert(set_equal(dst, set_type({2, 4, 5}, 0, throw_hasher<Counter<int>>(do_throw))));
    }
#endif
    assert(Counter_base::gConstructed == 0);
    struct equal
    {
        equal() = default;

        bool operator()(const Counter<int>& lhs, const Counter<int>& rhs) const
        {
            return lhs == rhs;
        }
    };
    struct hasher
    {
        hasher() = default;
        typedef Counter<int> argument_type;
        typedef size_t result_type;
        size_t operator()(const Counter<int>& p) const { return std::hash<Counter<int>>()(p); }
    };
    {
        typedef std::unordered_set<Counter<int>, std::hash<Counter<int>>, std::equal_to<Counter<int>>> first_set_type;
        typedef std::unordered_set<Counter<int>, hasher, equal> second_set_type;
        typedef std::unordered_multiset<Counter<int>, hasher, equal> third_set_type;

        {
            first_set_type first{1, 2, 3};
            second_set_type second{2, 3, 4};
            third_set_type third{1, 3};

            assert(Counter_base::gConstructed == 8);

            first.merge(second);
            first.merge(third);

            assert(set_equal(first, {1, 2, 3, 4}));
            assert(set_equal(second, {2, 3}));
            assert(set_equal(third, {1, 3}));

            assert(Counter_base::gConstructed == 8);
        }
        assert(Counter_base::gConstructed == 0);
        {
            first_set_type first{1, 2, 3};
            second_set_type second{2, 3, 4};
            third_set_type third{1, 3};

            assert(Counter_base::gConstructed == 8);

            first.merge(std::move(second));
            first.merge(std::move(third));

            assert(set_equal(first, {1, 2, 3, 4}));
            assert(set_equal(second, {2, 3}));
            assert(set_equal(third, {1, 3}));

            assert(Counter_base::gConstructed == 8);
        }
        assert(Counter_base::gConstructed == 0);
    }
    {
        std::unordered_set<int> first;
        {
            std::unordered_set<int> second;
            first.merge(second);
            first.merge(std::move(second));
        }
        {
            std::unordered_multiset<int> second;
            first.merge(second);
            first.merge(std::move(second));
        }
    }
}