-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconstructors.cpp
More file actions
125 lines (111 loc) · 4.63 KB
/
constructors.cpp
File metadata and controls
125 lines (111 loc) · 4.63 KB
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
// Copyright - 2020 - Jan Christoph Uhde <[email protected]>
// Please see LICENSE.md for license or visit https://github.com/extcpp/basics
#include "expected.hpp"
#include <string>
#include <type_traits>
#include <vector>
struct takes_init_and_variadic {
std::vector<int> v;
std::tuple<int, int> t;
template<class... Args>
takes_init_and_variadic(std::initializer_list<int> l, Args&&... args) : v(l), t(std::forward<Args>(args)...) {}
};
TEST(expected_constructors, constructors) {
{
eu::expected<int, int> e;
EXPECT_TRUE(e);
EXPECT_TRUE(e == 0);
}
{
eu::expected<int, int> e = eu::make_unexpected(0);
EXPECT_TRUE(!e);
EXPECT_TRUE(e.error() == 0);
}
{
eu::expected<int, int> e(eu::unexpect, 0);
EXPECT_TRUE(!e);
EXPECT_TRUE(e.error() == 0);
}
{
eu::expected<int, int> e(std::in_place, 42);
EXPECT_TRUE(e);
EXPECT_TRUE(e == 42);
}
{
eu::expected<std::vector<int>, int> e(std::in_place, {0, 1});
EXPECT_TRUE(e);
EXPECT_TRUE((*e)[0] == 0);
EXPECT_TRUE((*e)[1] == 1);
}
{
eu::expected<std::tuple<int, int>, int> e(std::in_place, 0, 1);
EXPECT_TRUE(e);
EXPECT_TRUE(std::get<0>(*e) == 0);
EXPECT_TRUE(std::get<1>(*e) == 1);
}
{
eu::expected<takes_init_and_variadic, int> e(std::in_place, {0, 1}, 2, 3);
EXPECT_TRUE(e);
EXPECT_TRUE(e->v[0] == 0);
EXPECT_TRUE(e->v[1] == 1);
EXPECT_TRUE(std::get<0>(e->t) == 2);
EXPECT_TRUE(std::get<1>(e->t) == 3);
}
{
eu::expected<int, int> e;
EXPECT_TRUE((std::is_default_constructible<decltype(e)>::value));
EXPECT_TRUE((std::is_copy_constructible<decltype(e)>::value));
EXPECT_TRUE((std::is_move_constructible<decltype(e)>::value));
EXPECT_TRUE((std::is_copy_assignable<decltype(e)>::value));
EXPECT_TRUE((std::is_move_assignable<decltype(e)>::value));
EXPECT_TRUE((std::is_trivially_copy_constructible<decltype(e)>::value));
EXPECT_TRUE((std::is_trivially_copy_assignable<decltype(e)>::value));
EXPECT_TRUE((std::is_trivially_move_constructible<decltype(e)>::value));
EXPECT_TRUE((std::is_trivially_move_assignable<decltype(e)>::value));
}
{
eu::expected<int, std::string> e;
EXPECT_TRUE((std::is_default_constructible<decltype(e)>::value));
EXPECT_TRUE((std::is_copy_constructible<decltype(e)>::value));
EXPECT_TRUE((std::is_move_constructible<decltype(e)>::value));
EXPECT_TRUE((std::is_copy_assignable<decltype(e)>::value));
EXPECT_TRUE((std::is_move_assignable<decltype(e)>::value));
EXPECT_TRUE((!std::is_trivially_copy_constructible<decltype(e)>::value));
EXPECT_TRUE((!std::is_trivially_copy_assignable<decltype(e)>::value));
EXPECT_TRUE((!std::is_trivially_move_constructible<decltype(e)>::value));
EXPECT_TRUE((!std::is_trivially_move_assignable<decltype(e)>::value));
}
{
eu::expected<std::string, int> e;
EXPECT_TRUE((std::is_default_constructible<decltype(e)>::value));
EXPECT_TRUE((std::is_copy_constructible<decltype(e)>::value));
EXPECT_TRUE((std::is_move_constructible<decltype(e)>::value));
EXPECT_TRUE((std::is_copy_assignable<decltype(e)>::value));
EXPECT_TRUE((std::is_move_assignable<decltype(e)>::value));
EXPECT_TRUE((!std::is_trivially_copy_constructible<decltype(e)>::value));
EXPECT_TRUE((!std::is_trivially_copy_assignable<decltype(e)>::value));
EXPECT_TRUE((!std::is_trivially_move_constructible<decltype(e)>::value));
EXPECT_TRUE((!std::is_trivially_move_assignable<decltype(e)>::value));
}
{
eu::expected<std::string, std::string> e;
EXPECT_TRUE((std::is_default_constructible<decltype(e)>::value));
EXPECT_TRUE((std::is_copy_constructible<decltype(e)>::value));
EXPECT_TRUE((std::is_move_constructible<decltype(e)>::value));
EXPECT_TRUE((std::is_copy_assignable<decltype(e)>::value));
EXPECT_TRUE((std::is_move_assignable<decltype(e)>::value));
EXPECT_TRUE((!std::is_trivially_copy_constructible<decltype(e)>::value));
EXPECT_TRUE((!std::is_trivially_copy_assignable<decltype(e)>::value));
EXPECT_TRUE((!std::is_trivially_move_constructible<decltype(e)>::value));
EXPECT_TRUE((!std::is_trivially_move_assignable<decltype(e)>::value));
}
{
eu::expected<void, int> e;
EXPECT_TRUE(e);
}
{
eu::expected<void, int> e(eu::unexpect, 42);
EXPECT_TRUE(!e);
EXPECT_TRUE(e.error() == 42);
}
}