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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
|
//===-- SPSWrapperFunctionTest.cpp ----------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// Test SPSWrapperFunction and associated utilities.
//
//===----------------------------------------------------------------------===//
#include "CommonTestUtils.h"
#include "orc-rt/SPSWrapperFunction.h"
#include "orc-rt/WrapperFunction.h"
#include "orc-rt/move_only_function.h"
#include "DirectCaller.h"
#include "gtest/gtest.h"
using namespace orc_rt;
static void void_noop_sps_wrapper(orc_rt_SessionRef S, uint64_t CallId,
orc_rt_WrapperFunctionReturn Return,
orc_rt_WrapperFunctionBuffer ArgBytes) {
SPSWrapperFunction<void()>::handle(
S, CallId, Return, ArgBytes,
[](move_only_function<void()> Return) { Return(); });
}
TEST(SPSWrapperFunctionUtilsTest, VoidNoop) {
bool Ran = false;
SPSWrapperFunction<void()>::call(DirectCaller(nullptr, void_noop_sps_wrapper),
[&](Error Err) {
cantFail(std::move(Err));
Ran = true;
});
EXPECT_TRUE(Ran);
}
static void add_via_lambda_sps_wrapper(orc_rt_SessionRef S, uint64_t CallId,
orc_rt_WrapperFunctionReturn Return,
orc_rt_WrapperFunctionBuffer ArgBytes) {
SPSWrapperFunction<int32_t(int32_t, int32_t)>::handle(
S, CallId, Return, ArgBytes,
[](move_only_function<void(int32_t)> Return, int32_t X, int32_t Y) {
Return(X + Y);
});
}
TEST(SPSWrapperFunctionUtilsTest, BinaryOpViaLambda) {
int32_t Result = 0;
SPSWrapperFunction<int32_t(int32_t, int32_t)>::call(
DirectCaller(nullptr, add_via_lambda_sps_wrapper),
[&](Expected<int32_t> R) { Result = cantFail(std::move(R)); }, 41, 1);
EXPECT_EQ(Result, 42);
}
static void add_via_function(move_only_function<void(int32_t)> Return,
int32_t X, int32_t Y) {
Return(X + Y);
}
static void
add_via_function_sps_wrapper(orc_rt_SessionRef S, uint64_t CallId,
orc_rt_WrapperFunctionReturn Return,
orc_rt_WrapperFunctionBuffer ArgBytes) {
SPSWrapperFunction<int32_t(int32_t, int32_t)>::handle(
S, CallId, Return, ArgBytes, add_via_function);
}
TEST(SPSWrapperFunctionUtilsTest, BinaryOpViaFunction) {
int32_t Result = 0;
SPSWrapperFunction<int32_t(int32_t, int32_t)>::call(
DirectCaller(nullptr, add_via_function_sps_wrapper),
[&](Expected<int32_t> R) { Result = cantFail(std::move(R)); }, 41, 1);
EXPECT_EQ(Result, 42);
}
static void
add_via_function_pointer_sps_wrapper(orc_rt_SessionRef S, uint64_t CallId,
orc_rt_WrapperFunctionReturn Return,
orc_rt_WrapperFunctionBuffer ArgBytes) {
SPSWrapperFunction<int32_t(int32_t, int32_t)>::handle(
S, CallId, Return, ArgBytes, &add_via_function);
}
TEST(SPSWrapperFunctionUtilsTest, BinaryOpViaFunctionPointer) {
int32_t Result = 0;
SPSWrapperFunction<int32_t(int32_t, int32_t)>::call(
DirectCaller(nullptr, add_via_function_pointer_sps_wrapper),
[&](Expected<int32_t> R) { Result = cantFail(std::move(R)); }, 41, 1);
EXPECT_EQ(Result, 42);
}
static void
round_trip_string_via_span_sps_wrapper(orc_rt_SessionRef S, uint64_t CallId,
orc_rt_WrapperFunctionReturn Return,
orc_rt_WrapperFunctionBuffer ArgBytes) {
SPSWrapperFunction<SPSString(SPSString)>::handle(
S, CallId, Return, ArgBytes,
[](move_only_function<void(std::string)> Return, span<const char> S) {
Return({S.data(), S.size()});
});
}
TEST(SPSWrapperFunctionUtilsTest, RoundTripStringViaSpan) {
/// Test that the SPSWrapperFunction<...>::handle call in
/// round_trip_string_via_span_sps_wrapper can deserialize into a usable
/// span<const char>.
std::string Result;
SPSWrapperFunction<SPSString(SPSString)>::call(
DirectCaller(nullptr, round_trip_string_via_span_sps_wrapper),
[&](Expected<std::string> R) { Result = cantFail(std::move(R)); },
std::string_view("hello, world!"));
EXPECT_EQ(Result, "hello, world!");
}
static void improbable_feat_sps_wrapper(orc_rt_SessionRef S, uint64_t CallId,
orc_rt_WrapperFunctionReturn Return,
orc_rt_WrapperFunctionBuffer ArgBytes) {
SPSWrapperFunction<SPSError(bool)>::handle(
S, CallId, Return, ArgBytes,
[](move_only_function<void(Error)> Return, bool LuckyHat) {
if (LuckyHat)
Return(Error::success());
else
Return(make_error<StringError>("crushed by boulder"));
});
}
TEST(SPSWrapperFunctionUtilsTest, TransparentConversionErrorSuccessCase) {
bool DidRun = false;
SPSWrapperFunction<SPSError(bool)>::call(
DirectCaller(nullptr, improbable_feat_sps_wrapper),
[&](Expected<Error> E) {
DidRun = true;
cantFail(cantFail(std::move(E)));
},
true);
EXPECT_TRUE(DidRun);
}
TEST(SPSWrapperFunctionUtilsTest, TransparentConversionErrorFailureCase) {
std::string ErrMsg;
SPSWrapperFunction<SPSError(bool)>::call(
DirectCaller(nullptr, improbable_feat_sps_wrapper),
[&](Expected<Error> E) { ErrMsg = toString(cantFail(std::move(E))); },
false);
EXPECT_EQ(ErrMsg, "crushed by boulder");
}
static void halve_number_sps_wrapper(orc_rt_SessionRef S, uint64_t CallId,
orc_rt_WrapperFunctionReturn Return,
orc_rt_WrapperFunctionBuffer ArgBytes) {
SPSWrapperFunction<SPSExpected<int32_t>(int32_t)>::handle(
S, CallId, Return, ArgBytes,
[](move_only_function<void(Expected<int32_t>)> Return, int N) {
if (N % 2 == 0)
Return(N >> 1);
else
Return(make_error<StringError>("N is not a multiple of 2"));
});
}
TEST(SPSWrapperFunctionUtilsTest, TransparentConversionExpectedSuccessCase) {
int32_t Result = 0;
SPSWrapperFunction<SPSExpected<int32_t>(int32_t)>::call(
DirectCaller(nullptr, halve_number_sps_wrapper),
[&](Expected<Expected<int32_t>> R) {
Result = cantFail(cantFail(std::move(R)));
},
2);
EXPECT_EQ(Result, 1);
}
TEST(SPSWrapperFunctionUtilsTest, TransparentConversionExpectedFailureCase) {
std::string ErrMsg;
SPSWrapperFunction<SPSExpected<int32_t>(int32_t)>::call(
DirectCaller(nullptr, halve_number_sps_wrapper),
[&](Expected<Expected<int32_t>> R) {
ErrMsg = toString(cantFail(std::move(R)).takeError());
},
3);
EXPECT_EQ(ErrMsg, "N is not a multiple of 2");
}
template <size_t N> struct SPSOpCounter {};
namespace orc_rt {
template <size_t N>
class SPSSerializationTraits<SPSOpCounter<N>, OpCounter<N>> {
public:
static size_t size(const OpCounter<N> &O) { return 0; }
static bool serialize(SPSOutputBuffer &OB, const OpCounter<N> &O) {
return true;
}
static bool deserialize(SPSInputBuffer &OB, OpCounter<N> &O) { return true; }
};
} // namespace orc_rt
static void
handle_with_reference_types_sps_wrapper(orc_rt_SessionRef S, uint64_t CallId,
orc_rt_WrapperFunctionReturn Return,
orc_rt_WrapperFunctionBuffer ArgBytes) {
SPSWrapperFunction<void(
SPSOpCounter<0>, SPSOpCounter<1>, SPSOpCounter<2>,
SPSOpCounter<3>)>::handle(S, CallId, Return, ArgBytes,
[](move_only_function<void()> Return,
OpCounter<0>, OpCounter<1> &,
const OpCounter<2> &,
OpCounter<3> &&) { Return(); });
}
TEST(SPSWrapperFunctionUtilsTest, HandlerWithReferences) {
// Test that we can handle by-value, by-ref, by-const-ref, and by-rvalue-ref
// arguments, and that we generate the expected number of moves.
OpCounter<0>::reset();
OpCounter<1>::reset();
OpCounter<2>::reset();
OpCounter<3>::reset();
bool DidRun = false;
SPSWrapperFunction<void(SPSOpCounter<0>, SPSOpCounter<1>, SPSOpCounter<2>,
SPSOpCounter<3>)>::
call(
DirectCaller(nullptr, handle_with_reference_types_sps_wrapper),
[&](Error R) {
cantFail(std::move(R));
DidRun = true;
},
OpCounter<0>(), OpCounter<1>(), OpCounter<2>(), OpCounter<3>());
EXPECT_TRUE(DidRun);
// We expect two default constructions for each parameter: one for the
// argument to call, and one for the object to deserialize into.
EXPECT_EQ(OpCounter<0>::defaultConstructions(), 2U);
EXPECT_EQ(OpCounter<1>::defaultConstructions(), 2U);
EXPECT_EQ(OpCounter<2>::defaultConstructions(), 2U);
EXPECT_EQ(OpCounter<3>::defaultConstructions(), 2U);
// Pass-by-value: we expect two moves (one for SPS transparent conversion,
// one to copy the value to the parameter), and no copies.
EXPECT_EQ(OpCounter<0>::moves(), 2U);
EXPECT_EQ(OpCounter<0>::copies(), 0U);
// Pass-by-lvalue-reference: we expect one move (for SPS transparent
// conversion), no copies.
EXPECT_EQ(OpCounter<1>::moves(), 1U);
EXPECT_EQ(OpCounter<1>::copies(), 0U);
// Pass-by-const-lvalue-reference: we expect one move (for SPS transparent
// conversion), no copies.
EXPECT_EQ(OpCounter<2>::moves(), 1U);
EXPECT_EQ(OpCounter<2>::copies(), 0U);
// Pass-by-rvalue-reference: we expect one move (for SPS transparent
// conversion), no copies.
EXPECT_EQ(OpCounter<3>::moves(), 1U);
EXPECT_EQ(OpCounter<3>::copies(), 0U);
}
namespace {
class Adder {
public:
int32_t addSync(int32_t X, int32_t Y) { return X + Y; }
void addAsync(move_only_function<void(int32_t)> Return, int32_t X,
int32_t Y) {
Return(addSync(X, Y));
}
};
} // anonymous namespace
static void adder_add_async_sps_wrapper(orc_rt_SessionRef S, uint64_t CallId,
orc_rt_WrapperFunctionReturn Return,
orc_rt_WrapperFunctionBuffer ArgBytes) {
SPSWrapperFunction<int32_t(SPSExecutorAddr, int32_t, int32_t)>::handle(
S, CallId, Return, ArgBytes,
WrapperFunction::handleWithAsyncMethod(&Adder::addAsync));
}
TEST(SPSWrapperFunctionUtilsTest, HandleWtihAsyncMethod) {
auto A = std::make_unique<Adder>();
int32_t Result = 0;
SPSWrapperFunction<int32_t(SPSExecutorAddr, int32_t, int32_t)>::call(
DirectCaller(nullptr, adder_add_async_sps_wrapper),
[&](Expected<int32_t> R) { Result = cantFail(std::move(R)); },
ExecutorAddr::fromPtr(A.get()), 41, 1);
EXPECT_EQ(Result, 42);
}
static void adder_add_sync_sps_wrapper(orc_rt_SessionRef S, uint64_t CallId,
orc_rt_WrapperFunctionReturn Return,
orc_rt_WrapperFunctionBuffer ArgBytes) {
SPSWrapperFunction<int32_t(SPSExecutorAddr, int32_t, int32_t)>::handle(
S, CallId, Return, ArgBytes,
WrapperFunction::handleWithSyncMethod(&Adder::addSync));
}
TEST(SPSWrapperFunctionUtilsTest, HandleWithSyncMethod) {
auto A = std::make_unique<Adder>();
int32_t Result = 0;
SPSWrapperFunction<int32_t(SPSExecutorAddr, int32_t, int32_t)>::call(
DirectCaller(nullptr, adder_add_sync_sps_wrapper),
[&](Expected<int32_t> R) { Result = cantFail(std::move(R)); },
ExecutorAddr::fromPtr(A.get()), 41, 1);
EXPECT_EQ(Result, 42);
}
|