-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdyn_optional_test.cc
186 lines (147 loc) · 5.63 KB
/
dyn_optional_test.cc
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/* Copyright (c) 2024 The XYZ Authors. All Rights Reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
==============================================================================*/
#include "dyn_optional.h"
#include <memory>
#include <type_traits>
#include <utility>
#include "gtest/gtest.h"
namespace {
using xyz::dyn_optional;
template <typename T>
class PropagatingAllocator {
public:
using value_type = T;
using propagate_on_container_copy_assignment = std::true_type;
using propagate_on_container_move_assignment = std::true_type;
using propagate_on_container_swap = std::true_type;
inline static size_t default_construct_count = 0;
PropagatingAllocator() { ++default_construct_count; }
friend auto operator<=>(const PropagatingAllocator&,
const PropagatingAllocator&) = default;
T* allocate(std::size_t n) const {
std::allocator<T> default_allocator{};
return default_allocator.allocate(n);
}
void deallocate(T* p, std::size_t n) const {
std::allocator<T> default_allocator{};
return default_allocator.deallocate(p, n);
}
};
template <typename T>
class NonPropagatingAllocator {
public:
using value_type = T;
using propagate_on_container_copy_assignment = std::false_type;
using propagate_on_container_move_assignment = std::false_type;
using propagate_on_container_swap = std::false_type;
inline static size_t default_construct_count = 0;
NonPropagatingAllocator() { ++default_construct_count; }
friend auto operator<=>(const NonPropagatingAllocator&,
const NonPropagatingAllocator&) = default;
NonPropagatingAllocator select_on_container_copy_construction() const {
return NonPropagatingAllocator();
}
T* allocate(std::size_t n) const {
std::allocator<T> default_allocator{};
return default_allocator.allocate(n);
}
void deallocate(T* p, std::size_t n) const {
std::allocator<T> default_allocator{};
return default_allocator.deallocate(p, n);
}
};
TEST(TestUtilities, PropagatingAllocator) {
using allocator_traits = std::allocator_traits<PropagatingAllocator<int>>;
EXPECT_TRUE(allocator_traits::propagate_on_container_copy_assignment::value);
EXPECT_TRUE(allocator_traits::propagate_on_container_move_assignment::value);
EXPECT_TRUE(allocator_traits::propagate_on_container_swap::value);
auto a = PropagatingAllocator<int>{};
[[maybe_unused]] auto aa =
allocator_traits::select_on_container_copy_construction(a);
EXPECT_EQ(PropagatingAllocator<int>::default_construct_count, 1);
}
TEST(TestUtilities, NonPropagatingAllocator) {
using allocator_traits = std::allocator_traits<NonPropagatingAllocator<int>>;
EXPECT_FALSE(allocator_traits::propagate_on_container_copy_assignment::value);
EXPECT_FALSE(allocator_traits::propagate_on_container_move_assignment::value);
EXPECT_FALSE(allocator_traits::propagate_on_container_swap::value);
auto a = NonPropagatingAllocator<int>{};
[[maybe_unused]] auto aa =
allocator_traits::select_on_container_copy_construction(a);
EXPECT_EQ(NonPropagatingAllocator<int>::default_construct_count, 2);
}
//
// Default Allocator Tests.
//
//
// Default Allocator Tests.
//
TEST(DynOptionalDefaultAllocator, DefaultConstruct) {
dyn_optional<int> opt;
EXPECT_FALSE(opt);
}
TEST(DynOptionalDefaultAllocator, ValueConstruct) {
dyn_optional<int> opt(42);
EXPECT_TRUE(opt);
EXPECT_EQ(*opt, 42);
}
TEST(DynOptionalDefaultAllocator, Swap) {
dyn_optional<int> opt1(42);
dyn_optional<int> opt2(43);
opt1.swap(opt2);
EXPECT_EQ(*opt1, 43);
EXPECT_EQ(*opt2, 42);
}
//
// Propagating Allocator Tests.
//
TEST(DynOptionalPropagatingAllocator, DefaultConstruct) {
dyn_optional<int, PropagatingAllocator<int>> opt;
EXPECT_FALSE(opt);
}
TEST(DynOptionalPropagatingAllocator, ValueConstruct) {
dyn_optional<int, PropagatingAllocator<int>> opt(42);
EXPECT_TRUE(opt);
EXPECT_EQ(*opt, 42);
}
TEST(DynOptionalPropagatingAllocator, Swap) {
dyn_optional<int, PropagatingAllocator<int>> opt1(42);
dyn_optional<int, PropagatingAllocator<int>> opt2(43);
opt1.swap(opt2);
EXPECT_EQ(*opt1, 43);
EXPECT_EQ(*opt2, 42);
}
//
// NonPropagating Allocator Tests.
//
TEST(DynOptionalNonPropagatingAllocator, DefaultConstruct) {
dyn_optional<int, NonPropagatingAllocator<int>> opt;
EXPECT_FALSE(opt);
}
TEST(DynOptionalNonPropagatingAllocator, ValueConstruct) {
dyn_optional<int, NonPropagatingAllocator<int>> opt(42);
EXPECT_TRUE(opt);
EXPECT_EQ(*opt, 42);
}
TEST(DynOptionalNonPropagatingAllocator, Swap) {
dyn_optional<int, NonPropagatingAllocator<int>> opt1(42);
dyn_optional<int, NonPropagatingAllocator<int>> opt2(43);
opt1.swap(opt2);
EXPECT_EQ(*opt1, 43);
EXPECT_EQ(*opt2, 42);
}
} // namespace