Revert "[libc] Use best-fit binary trie to make malloc logarithmic" (#117065)
[llvm-project.git] / libcxx / test / std / utilities / any / any.class / any.assign / value.pass.cpp
blob939c766777c0bf699962dcf2393812138b18d49c
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 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: c++03, c++11, c++14
11 // <any>
13 // template <class ValueType>
14 // any& operator=(ValueType&&);
16 // Test value copy and move assignment.
18 #include <any>
19 #include <cassert>
21 #include "any_helpers.h"
22 #include "count_new.h"
23 #include "test_macros.h"
25 template <class LHS, class RHS>
26 void test_assign_value() {
27 assert(LHS::count == 0);
28 assert(RHS::count == 0);
29 LHS::reset();
30 RHS::reset();
32 std::any lhs = LHS(1);
33 const std::any rhs = RHS(2);
35 assert(LHS::count == 1);
36 assert(RHS::count == 1);
37 assert(RHS::copied == 0);
39 lhs = rhs;
41 assert(RHS::copied == 1);
42 assert(LHS::count == 0);
43 assert(RHS::count == 2);
45 assertContains<RHS>(lhs, 2);
46 assertContains<RHS>(rhs, 2);
48 assert(LHS::count == 0);
49 assert(RHS::count == 0);
50 LHS::reset();
51 RHS::reset();
53 std::any lhs = LHS(1);
54 std::any rhs = RHS(2);
56 assert(LHS::count == 1);
57 assert(RHS::count == 1);
58 assert(RHS::moved == 1);
60 lhs = std::move(rhs);
62 assert(RHS::moved >= 1);
63 assert(RHS::copied == 0);
64 assert(LHS::count == 0);
65 assert(RHS::count == 1 + rhs.has_value());
66 LIBCPP_ASSERT(!rhs.has_value());
68 assertContains<RHS>(lhs, 2);
69 if (rhs.has_value())
70 assertContains<RHS>(rhs, 0);
72 assert(LHS::count == 0);
73 assert(RHS::count == 0);
76 template <class RHS>
77 void test_assign_value_empty() {
78 assert(RHS::count == 0);
79 RHS::reset();
81 std::any lhs;
82 RHS rhs(42);
83 assert(RHS::count == 1);
84 assert(RHS::copied == 0);
86 lhs = rhs;
88 assert(RHS::count == 2);
89 assert(RHS::copied == 1);
90 assert(RHS::moved >= 0);
91 assertContains<RHS>(lhs, 42);
93 assert(RHS::count == 0);
94 RHS::reset();
96 std::any lhs;
97 RHS rhs(42);
98 assert(RHS::count == 1);
99 assert(RHS::moved == 0);
101 lhs = std::move(rhs);
103 assert(RHS::count == 2);
104 assert(RHS::copied == 0);
105 assert(RHS::moved >= 1);
106 assertContains<RHS>(lhs, 42);
108 assert(RHS::count == 0);
109 RHS::reset();
113 template <class Tp, bool Move = false>
114 void test_assign_throws() {
115 #if !defined(TEST_HAS_NO_EXCEPTIONS)
116 auto try_throw =
117 [](std::any& lhs, Tp& rhs) {
118 try {
119 Move ? lhs = std::move(rhs)
120 : lhs = rhs;
121 assert(false);
122 } catch (const my_any_exception&) {
123 // do nothing
124 } catch (...) {
125 assert(false);
128 // const lvalue to empty
130 std::any lhs;
131 Tp rhs(1);
132 assert(Tp::count == 1);
134 try_throw(lhs, rhs);
136 assert(Tp::count == 1);
137 assertEmpty<Tp>(lhs);
140 std::any lhs = small(2);
141 Tp rhs(1);
142 assert(small::count == 1);
143 assert(Tp::count == 1);
145 try_throw(lhs, rhs);
147 assert(small::count == 1);
148 assert(Tp::count == 1);
149 assertContains<small>(lhs, 2);
152 std::any lhs = large(2);
153 Tp rhs(1);
154 assert(large::count == 1);
155 assert(Tp::count == 1);
157 try_throw(lhs, rhs);
159 assert(large::count == 1);
160 assert(Tp::count == 1);
161 assertContains<large>(lhs, 2);
163 #endif
167 // Test that any& operator=(ValueType&&) is *never* selected for:
168 // * std::in_place type.
169 // * Non-copyable types
170 void test_sfinae_constraints() {
171 { // Only the constructors are required to SFINAE on in_place_t
172 using Tag = std::in_place_type_t<int>;
173 using RawTag = std::remove_reference_t<Tag>;
174 static_assert(std::is_assignable<std::any, RawTag&&>::value, "");
177 struct Dummy { Dummy() = delete; };
178 using T = std::in_place_type_t<Dummy>;
179 static_assert(std::is_assignable<std::any, T>::value, "");
182 // Test that the ValueType&& constructor SFINAE's away when the
183 // argument is non-copyable
184 struct NoCopy {
185 NoCopy() = default;
186 NoCopy(NoCopy const&) = delete;
187 NoCopy(NoCopy&&) = default;
189 static_assert(!std::is_assignable<std::any, NoCopy>::value, "");
190 static_assert(!std::is_assignable<std::any, NoCopy&>::value, "");
194 int main(int, char**) {
195 test_assign_value<small1, small2>();
196 test_assign_value<large1, large2>();
197 test_assign_value<small, large>();
198 test_assign_value<large, small>();
199 test_assign_value_empty<small>();
200 test_assign_value_empty<large>();
201 test_assign_throws<small_throws_on_copy>();
202 test_assign_throws<large_throws_on_copy>();
203 test_assign_throws<throws_on_move, /* Move = */ true>();
204 test_sfinae_constraints();
206 return 0;