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
|
#include <wil/wistd_functional.h>
#include "common.h"
#include "test_objects.h"
// Test methods/objects
int GetValue()
{
return 42;
}
int GetOtherValue()
{
return 8;
}
int Negate(int value)
{
return -value;
}
int Add(int lhs, int rhs)
{
return lhs + rhs;
}
TEST_CASE("WistdFunctionTests::CallOperatorTest", "[wistd]")
{
wistd::function<int()> getValue = GetValue;
REQUIRE(GetValue() == getValue());
wistd::function<int(int)> negate = Negate;
REQUIRE(Negate(42) == negate(42));
wistd::function<int(int, int)> add = Add;
REQUIRE(Add(42, 8) == add(42, 8));
}
TEST_CASE("WistdFunctionTests::AssignmentOperatorTest", "[wistd]")
{
wistd::function<int()> fn = GetValue;
REQUIRE(GetValue() == fn());
fn = GetOtherValue;
REQUIRE(GetOtherValue() == fn());
}
#ifdef WIL_ENABLE_EXCEPTIONS
TEST_CASE("WistdFunctionTests::StdFunctionConstructionTest", "[wistd]")
{
// We should be able to capture a std::function in a wistd::function
wistd::function<int()> fn;
{
value_holder holder{ 42 };
std::function<int()> stdFn = [holder]()
{
return holder.value;
};
fn = stdFn;
}
REQUIRE(42 == fn());
}
#endif
TEST_CASE("WistdFunctionTests::CopyConstructionTest", "[wistd]")
{
object_counter_state state;
{
wistd::function<int()> copyFrom = [counter = object_counter{ state }]()
{
return counter.state->copy_count;
};
REQUIRE(0 == copyFrom());
auto copyTo = copyFrom;
REQUIRE(1 == copyTo());
}
REQUIRE(0 == state.instance_count());
}
TEST_CASE("WistdFunctionTests::CopyAssignmentTest", "[wistd]")
{
object_counter_state state;
{
wistd::function<int()> copyTo;
{
wistd::function<int()> copyFrom = [counter = object_counter{ state }]()
{
return counter.state->copy_count;
};
REQUIRE(0 == copyFrom());
copyTo = copyFrom;
}
REQUIRE(1 == copyTo());
}
REQUIRE(0 == state.instance_count());
}
TEST_CASE("WistdFunctionTests::MoveConstructionTest", "[wistd]")
{
object_counter_state state;
{
wistd::function<int()> moveFrom = [counter = object_counter{ state }]()
{
return counter.state->copy_count;
};
REQUIRE(0 == moveFrom());
auto moveTo = std::move(moveFrom);
REQUIRE(0 == moveTo());
// Because we move the underlying function object, we _must_ invalidate the moved from function
REQUIRE_FALSE(moveFrom != nullptr);
}
REQUIRE(0 == state.instance_count());
}
TEST_CASE("WistdFunctionTests::MoveAssignmentTest", "[wistd]")
{
object_counter_state state;
{
wistd::function<int()> moveTo;
{
wistd::function<int()> moveFrom = [counter = object_counter{ state }]()
{
return counter.state->copy_count;
};
REQUIRE(0 == moveFrom());
moveTo = std::move(moveFrom);
}
REQUIRE(0 == moveTo());
}
REQUIRE(0 == state.instance_count());
}
TEST_CASE("WistdFunctionTests::SwapTest", "[wistd]")
{
object_counter_state state;
{
wistd::function<int()> first;
wistd::function<int()> second;
first.swap(second);
REQUIRE_FALSE(first != nullptr);
REQUIRE_FALSE(second != nullptr);
first = [counter = object_counter{ state }]()
{
return counter.state->copy_count;
};
first.swap(second);
REQUIRE_FALSE(first != nullptr);
REQUIRE(second != nullptr);
REQUIRE(0 == second());
first.swap(second);
REQUIRE(first != nullptr);
REQUIRE_FALSE(second != nullptr);
REQUIRE(0 == first());
second = [counter = object_counter{ state }]()
{
return counter.state->copy_count;
};
first.swap(second);
REQUIRE(first != nullptr);
REQUIRE(second != nullptr);
REQUIRE(0 == first());
}
REQUIRE(0 == state.instance_count());
}
// MSVC's optimizer has had issues with wistd::function in the past when forwarding wistd::function objects to a
// function that accepts the arguments by value. This test exercises the workaround that we have in place. Note
// that this of course requires building with optimizations enabled
void ForwardingTest(wistd::function<int()> getValue, wistd::function<int(int)> negate, wistd::function<int(int, int)> add)
{
// Previously, this would cause a runtime crash
REQUIRE(Add(GetValue(), Negate(8)) == add(getValue(), negate(8)));
}
template <typename... Args>
void CallForwardingTest(Args&&... args)
{
ForwardingTest(wistd::forward<Args>(args)...);
}
TEST_CASE("WistdFunctionTests::OptimizationRegressionTest", "[wistd]")
{
CallForwardingTest(
wistd::function<int()>(GetValue),
wistd::function<int(int)>(Negate),
wistd::function<int(int, int)>(Add));
}
|