-
Notifications
You must be signed in to change notification settings - Fork 1
/
unique.cpp
254 lines (209 loc) · 8.81 KB
/
unique.cpp
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
//
// This file is part of dingo project <https://github.com/romanpauk/dingo>
//
// See LICENSE for license and copyright information
// SPDX-License-Identifier: MIT
//
#include <dingo/container.h>
#include <dingo/factory/constructor.h>
#include <dingo/storage/unique.h>
#include <gtest/gtest.h>
#include "assert.h"
#include "class.h"
#include "containers.h"
#include "test.h"
namespace dingo {
template <typename T> struct unique_test : public test<T> {};
TYPED_TEST_SUITE(unique_test, container_types);
TYPED_TEST(unique_test, value_resolve_rvalue) {
using container_type = TypeParam;
{
container_type container;
container.template register_type<scope<unique>, storage<Class>>();
{
AssertClass(container.template resolve<Class&&>());
ASSERT_EQ(Class::Constructor, 1);
ASSERT_EQ(Class::MoveConstructor, 2);
ASSERT_EQ(Class::CopyConstructor, 0);
}
ASSERT_EQ(Class::Destructor, Class::GetTotalInstances());
AssertTypeNotConvertible<Class, type_list<Class*>>(container);
}
ASSERT_EQ(Class::Destructor, Class::GetTotalInstances());
}
TYPED_TEST(unique_test, value_resolve_value) {
using container_type = TypeParam;
{
container_type container;
container.template register_type<scope<unique>, storage<Class>>();
{
AssertClass(container.template resolve<Class>());
ASSERT_EQ(Class::Constructor, 1);
ASSERT_EQ(Class::MoveConstructor, 1);
ASSERT_EQ(Class::CopyConstructor, 1);
}
AssertClass(container.template resolve<const Class>());
AssertClass(*container.template resolve<std::optional<Class>>());
AssertClass(*container.template resolve<const std::optional<Class>>());
AssertClass(*container.template resolve<std::optional<const Class>>());
AssertClass(
*container.template resolve<const std::optional<const Class>>());
ASSERT_EQ(Class::Destructor, Class::GetTotalInstances());
}
ASSERT_EQ(Class::Destructor, Class::GetTotalInstances());
}
TYPED_TEST(unique_test, ptr) {
using container_type = TypeParam;
{
container_type container;
container.template register_type<scope<unique>, storage<Class*>>();
auto c = container.template resolve<Class*>();
AssertClass(*c);
ASSERT_EQ(Class::Constructor, 1);
ASSERT_EQ(Class::CopyConstructor, 0);
ASSERT_EQ(Class::MoveConstructor, 0);
ASSERT_EQ(Class::Destructor, 0);
delete c;
auto c2 = container.template resolve<const Class*>();
AssertClass(*c2);
delete c2;
ASSERT_EQ(Class::Destructor, Class::GetTotalInstances());
}
ASSERT_EQ(Class::Destructor, Class::GetTotalInstances());
}
TYPED_TEST(unique_test, ptr_interface) {
using container_type = TypeParam;
container_type container;
container.template register_type<scope<unique>, storage<Class*>,
interface<IClass, IClass2>>();
AssertClass(std::unique_ptr<IClass>(container.template resolve<IClass*>()));
AssertClass(
std::unique_ptr<IClass2>(container.template resolve<IClass2*>()));
AssertClass(container.template resolve<std::unique_ptr<IClass>>());
AssertClass(container.template resolve<std::unique_ptr<IClass>&&>());
AssertClass(container.template resolve<std::unique_ptr<const IClass>&&>());
AssertClass(*container.template resolve<std::unique_ptr<const IClass>>());
AssertClass(container.template resolve<std::shared_ptr<IClass>>());
AssertClass(container.template resolve<std::shared_ptr<IClass>&&>());
AssertClass(container.template resolve<std::shared_ptr<const IClass>>());
AssertClass(*container.template resolve<const std::shared_ptr<IClass>>());
AssertClass(
*container.template resolve<const std::shared_ptr<const IClass>>());
}
TYPED_TEST(unique_test, unique_ptr) {
using container_type = TypeParam;
{
container_type container;
container.template register_type<scope<unique>,
storage<std::unique_ptr<Class>>,
interface<Class, IClass>>();
{
AssertClass(*container.template resolve<std::unique_ptr<Class>>());
ASSERT_EQ(Class::Constructor, 1);
ASSERT_EQ(Class::MoveConstructor, 0);
ASSERT_EQ(Class::CopyConstructor, 0);
AssertClass(
*container.template resolve<std::unique_ptr<const Class>>());
}
ASSERT_EQ(Class::Destructor, Class::GetTotalInstances());
AssertTypeNotConvertible<Class, type_list<Class&, Class&&, Class*>>(
container);
}
ASSERT_EQ(Class::Destructor, Class::GetTotalInstances());
}
TYPED_TEST(unique_test, unique_ptr_multiple_interface) {
using container_type = TypeParam;
container_type container;
container
.template register_type<scope<unique>, storage<std::unique_ptr<Class>>,
interface<IClass, IClass2>>();
AssertClass(container.template resolve<std::unique_ptr<IClass>>());
AssertClass(container.template resolve<std::unique_ptr<IClass2>>());
AssertClass(container.template resolve<std::shared_ptr<IClass>>());
}
// TODO: this excercises resolving code without creating temporaries,
// yet there is no way how to test it
TYPED_TEST(unique_test, unique_ptr_single_interface) {
using container_type = TypeParam;
container_type container;
container.template register_type<
scope<unique>, storage<std::unique_ptr<Class>>, interface<IClass>>();
AssertClass(container.template resolve<std::unique_ptr<IClass>>());
}
TYPED_TEST(unique_test, optional) {
using container_type = TypeParam;
{
container_type container;
container.template register_type<
scope<unique>, storage<std::optional<Class>>, interface<Class>>();
{
AssertClass(*container.template resolve<std::optional<Class>>());
ASSERT_EQ(Class::Constructor, 1);
ASSERT_EQ(Class::MoveConstructor, 1);
ASSERT_EQ(Class::CopyConstructor, 1); // TODO
AssertClass(
*container.template resolve<const std::optional<Class>>());
AssertClass(
*container.template resolve<std::optional<const Class>>());
AssertClass(
*container
.template resolve<const std::optional<const Class>>());
}
ASSERT_EQ(Class::Destructor, Class::GetTotalInstances());
AssertTypeNotConvertible<Class, type_list<Class&, Class&&, Class*>>(
container);
}
ASSERT_EQ(Class::Destructor, Class::GetTotalInstances());
}
// TODO: shared_ptr needs to be passed as &&
// TODO: creates multiple conversions. The temporaries should be tied to
// something smaller than top-level context (perhaps per-argument in
// constructor_detection_impl::construct?) as it is not possible to overwrite
// the conversion, all arguments need to exist at the same time
TYPED_TEST(unique_test, unique_multiple) {
using container_type = TypeParam;
struct C {
C(std::unique_ptr<IClass1>&& c1, std::unique_ptr<IClass1>&& c11,
std::unique_ptr<IClass2>&& c2, std::unique_ptr<IClass2>&& c22) {
AssertClass(*c1);
AssertClass(*c11);
assert(&c1 != &c11);
AssertClass(*c2);
AssertClass(*c22);
assert(&c2 != &c22);
}
};
container_type container;
container.template register_type<scope<unique>, storage<Class*>,
interface<IClass1, IClass2>>();
container.template construct<C>();
}
TYPED_TEST(unique_test, ambiguous_resolve) {
struct A {
A() : index(0) {}
A(int) : index(1) {}
A(float) : index(2) {}
A(int, float) : index(3) {}
A(float, int) : index(4) {}
int index;
};
using container_type = TypeParam;
container_type container;
container.template register_type<scope<unique>, storage<A>,
factory<constructor<A(float, int)>>>();
container.template register_type<scope<unique>, storage<float>>();
container.template register_type<scope<unique>, storage<int>>();
ASSERT_EQ((container.template construct<A, constructor<A>>().index), 0);
ASSERT_EQ((container.template construct<A, constructor<A()>>().index), 0);
ASSERT_EQ((container.template construct<A, constructor<A(int)>>().index),
1);
ASSERT_EQ((container.template construct<A, constructor<A(float)>>().index),
2);
ASSERT_EQ(
(container.template construct<A, constructor<A, int, float>>().index),
3);
ASSERT_EQ(
(container.template construct<A, constructor<A, float, int>>().index),
4);
}
} // namespace dingo