-
Notifications
You must be signed in to change notification settings - Fork 1
/
construct.cpp
129 lines (111 loc) · 3.17 KB
/
construct.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
//
// 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/storage/external.h>
#include <dingo/storage/shared.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 construct_test : public test<T> {};
TYPED_TEST_SUITE(construct_test, container_types);
TEST(construct_test, class_traits) {
struct A {};
struct B {
B(std::shared_ptr<A>) {}
};
class_traits<A>::construct();
class_traits<B>::construct(nullptr);
delete class_traits<B*>::construct(nullptr);
class_traits<std::unique_ptr<B>>::construct(nullptr);
class_traits<std::shared_ptr<B>>::construct(nullptr);
class_traits<std::optional<B>>::construct(nullptr);
}
TYPED_TEST(construct_test, plain) {
using container_type = TypeParam;
container_type container;
container.template construct<int>();
container.template construct<std::unique_ptr<int>>();
container.template construct<std::shared_ptr<int>>();
container.template construct<std::optional<int>>();
}
#if 0
// TODO: can ambiguous construction be detected?
TYPED_TEST(construct_test, ambiguous) {
using container_type = TypeParam;
struct A {
A() {}
A(int) {}
};
container_type container;
container.template construct<A>();
}
#endif
TYPED_TEST(construct_test, aggregate1) {
using container_type = TypeParam;
struct A {
int x;
};
struct B {
B(A&&) {}
};
container_type container;
container.template register_type<scope<unique>, storage<A>>();
container.template register_type<scope<unique>, storage<int>>();
container.template construct<B>();
}
TYPED_TEST(construct_test, aggregate2) {
using container_type = TypeParam;
struct A {
int a = 1;
};
struct B {
A a0;
};
struct C {
A a0;
A a1;
};
struct D {
int a;
int b;
};
container_type container;
container.template register_type<scope<external>, storage<int>>(2);
container.template register_type<scope<unique>, storage<A>>();
auto b = container.template construct<B>();
auto c = container.template construct<C>();
auto d = container.template construct<D>();
ASSERT_EQ(b.a0.a, 2);
ASSERT_EQ(c.a0.a, 2);
ASSERT_EQ(c.a1.a, 2);
ASSERT_EQ(d.a, 2);
ASSERT_EQ(d.b, 2);
}
TYPED_TEST(construct_test, resolve) {
using container_type = TypeParam;
struct A {};
struct B {
B(A&&) {}
};
struct C {
C(B&) {}
};
container_type container;
container.template register_type<scope<unique>, storage<A>>();
container
.template register_type<scope<shared>, storage<std::shared_ptr<B>>>();
container.template construct<B>();
container.template construct<std::unique_ptr<B>>();
container.template construct<std::shared_ptr<B>>();
container.template construct<std::optional<B>>();
container.template construct<C>();
}
} // namespace dingo