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
10
11// <unordered_set>
12
13// template <class Value, class Hash = hash<Value>, class Pred = equal_to<Value>,
14// class Alloc = allocator<Value>>
15// class unordered_set
16
17// unordered_set(unordered_set&& u);
18
19#include <unordered_set>
20#include <cassert>
21#include <cfloat>
22#include <cmath>
23#include <cstddef>
24
25#include "test_macros.h"
26#include "../../../test_compare.h"
27#include "../../../test_hash.h"
28#include "test_allocator.h"
29#include "min_allocator.h"
30
31int main(int, char**) {
32 {
33 typedef std::unordered_set<int, test_hash<int>, test_equal_to<int>, test_allocator<int> > C;
34 C c0(7, test_hash<int>(8), test_equal_to<int>(9), test_allocator<int>(10));
35 C c = std::move(c0);
36 LIBCPP_ASSERT(c.bucket_count() == 7);
37 assert(c.size() == 0);
38 assert(c.hash_function() == test_hash<int>(8));
39 assert(c.key_eq() == test_equal_to<int>(9));
40 assert(c.get_allocator() == test_allocator<int>(10));
41 assert(c.empty());
42 assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
43 assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
44 assert(c.load_factor() == 0);
45 assert(c.max_load_factor() == 1);
46
47 assert(c0.empty());
48 }
49 {
50 typedef std::unordered_set<int, test_hash<int>, test_equal_to<int>, test_allocator<int> > C;
51 typedef int P;
52 P a[] = {P(1), P(2), P(3), P(4), P(1), P(2)};
53 C c0(a, a + sizeof(a) / sizeof(a[0]), 7, test_hash<int>(8), test_equal_to<int>(9), test_allocator<int>(10));
54 C::iterator it0 = c0.begin();
55 C c = std::move(c0);
56 assert(it0 == c.begin()); // Iterators remain valid
57 LIBCPP_ASSERT(c.bucket_count() == 7);
58 assert(c.size() == 4);
59 assert(c.count(1) == 1);
60 assert(c.count(2) == 1);
61 assert(c.count(3) == 1);
62 assert(c.count(4) == 1);
63 assert(c.hash_function() == test_hash<int>(8));
64 assert(c.key_eq() == test_equal_to<int>(9));
65 assert(c.get_allocator() == test_allocator<int>(10));
66 assert(!c.empty());
67 assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
68 assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
69 assert(std::fabs(c.load_factor() - (float)c.size() / c.bucket_count()) < FLT_EPSILON);
70 assert(c.max_load_factor() == 1);
71
72 assert(c0.empty());
73 }
74 {
75 typedef std::unordered_set<int, test_hash<int>, test_equal_to<int>, min_allocator<int> > C;
76 C c0(7, test_hash<int>(8), test_equal_to<int>(9), min_allocator<int>());
77 C c = std::move(c0);
78 LIBCPP_ASSERT(c.bucket_count() == 7);
79 assert(c.size() == 0);
80 assert(c.hash_function() == test_hash<int>(8));
81 assert(c.key_eq() == test_equal_to<int>(9));
82 assert(c.get_allocator() == min_allocator<int>());
83 assert(c.empty());
84 assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
85 assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
86 assert(c.load_factor() == 0);
87 assert(c.max_load_factor() == 1);
88
89 assert(c0.empty());
90 }
91 {
92 typedef std::unordered_set<int, test_hash<int>, test_equal_to<int>, min_allocator<int> > C;
93 typedef int P;
94 P a[] = {P(1), P(2), P(3), P(4), P(1), P(2)};
95 C c0(a, a + sizeof(a) / sizeof(a[0]), 7, test_hash<int>(8), test_equal_to<int>(9), min_allocator<int>());
96 C::iterator it0 = c0.begin();
97 C c = std::move(c0);
98 assert(it0 == c.begin()); // Iterators remain valid
99 LIBCPP_ASSERT(c.bucket_count() == 7);
100 assert(c.size() == 4);
101 assert(c.count(1) == 1);
102 assert(c.count(2) == 1);
103 assert(c.count(3) == 1);
104 assert(c.count(4) == 1);
105 assert(c.hash_function() == test_hash<int>(8));
106 assert(c.key_eq() == test_equal_to<int>(9));
107 assert(c.get_allocator() == min_allocator<int>());
108 assert(!c.empty());
109 assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
110 assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
111 assert(std::fabs(c.load_factor() - (float)c.size() / c.bucket_count()) < FLT_EPSILON);
112 assert(c.max_load_factor() == 1);
113
114 assert(c0.empty());
115 }
116
117 return 0;
118}
119

source code of libcxx/test/std/containers/unord/unord.set/unord.set.cnstr/move.pass.cpp