diff options
Diffstat (limited to 'orc-rt/unittests/SPSWrapperFunctionTest.cpp')
-rw-r--r-- | orc-rt/unittests/SPSWrapperFunctionTest.cpp | 89 |
1 files changed, 80 insertions, 9 deletions
diff --git a/orc-rt/unittests/SPSWrapperFunctionTest.cpp b/orc-rt/unittests/SPSWrapperFunctionTest.cpp index 32aaa61..7f88ce0 100644 --- a/orc-rt/unittests/SPSWrapperFunctionTest.cpp +++ b/orc-rt/unittests/SPSWrapperFunctionTest.cpp @@ -82,7 +82,7 @@ static void void_noop_sps_wrapper(orc_rt_SessionRef Session, void *CallCtx, [](move_only_function<void()> Return) { Return(); }); } -TEST(SPSWrapperFunctionUtilsTest, TestVoidNoop) { +TEST(SPSWrapperFunctionUtilsTest, VoidNoop) { bool Ran = false; SPSWrapperFunction<void()>::call(DirectCaller(nullptr, void_noop_sps_wrapper), [&](Error Err) { @@ -102,7 +102,7 @@ static void add_via_lambda_sps_wrapper(orc_rt_SessionRef Session, void *CallCtx, }); } -TEST(SPSWrapperFunctionUtilsTest, TestBinaryOpViaLambda) { +TEST(SPSWrapperFunctionUtilsTest, BinaryOpViaLambda) { int32_t Result = 0; SPSWrapperFunction<int32_t(int32_t, int32_t)>::call( DirectCaller(nullptr, add_via_lambda_sps_wrapper), @@ -123,7 +123,7 @@ add_via_function_sps_wrapper(orc_rt_SessionRef Session, void *CallCtx, Session, CallCtx, Return, ArgBytes, add_via_function); } -TEST(SPSWrapperFunctionUtilsTest, TestBinaryOpViaFunction) { +TEST(SPSWrapperFunctionUtilsTest, BinaryOpViaFunction) { int32_t Result = 0; SPSWrapperFunction<int32_t(int32_t, int32_t)>::call( DirectCaller(nullptr, add_via_function_sps_wrapper), @@ -139,7 +139,7 @@ add_via_function_pointer_sps_wrapper(orc_rt_SessionRef Session, void *CallCtx, Session, CallCtx, Return, ArgBytes, &add_via_function); } -TEST(SPSWrapperFunctionUtilsTest, TestBinaryOpViaFunctionPointer) { +TEST(SPSWrapperFunctionUtilsTest, BinaryOpViaFunctionPointer) { int32_t Result = 0; SPSWrapperFunction<int32_t(int32_t, int32_t)>::call( DirectCaller(nullptr, add_via_function_pointer_sps_wrapper), @@ -161,7 +161,7 @@ static void improbable_feat_sps_wrapper(orc_rt_SessionRef Session, }); } -TEST(SPSWrapperFunctionUtilsTest, TestFunctionReturningErrorSuccessCase) { +TEST(SPSWrapperFunctionUtilsTest, TransparentConversionErrorSuccessCase) { bool DidRun = false; SPSWrapperFunction<SPSError(bool)>::call( DirectCaller(nullptr, improbable_feat_sps_wrapper), @@ -174,7 +174,7 @@ TEST(SPSWrapperFunctionUtilsTest, TestFunctionReturningErrorSuccessCase) { EXPECT_TRUE(DidRun); } -TEST(SPSWrapperFunctionUtilsTest, TestFunctionReturningErrorFailureCase) { +TEST(SPSWrapperFunctionUtilsTest, TransparentConversionErrorFailureCase) { std::string ErrMsg; SPSWrapperFunction<SPSError(bool)>::call( DirectCaller(nullptr, improbable_feat_sps_wrapper), @@ -197,7 +197,7 @@ static void halve_number_sps_wrapper(orc_rt_SessionRef Session, void *CallCtx, }); } -TEST(SPSWrapperFunctionUtilsTest, TestFunctionReturningExpectedSuccessCase) { +TEST(SPSWrapperFunctionUtilsTest, TransparentConversionExpectedSuccessCase) { int32_t Result = 0; SPSWrapperFunction<SPSExpected<int32_t>(int32_t)>::call( DirectCaller(nullptr, halve_number_sps_wrapper), @@ -209,7 +209,7 @@ TEST(SPSWrapperFunctionUtilsTest, TestFunctionReturningExpectedSuccessCase) { EXPECT_EQ(Result, 1); } -TEST(SPSWrapperFunctionUtilsTest, TestFunctionReturningExpectedFailureCase) { +TEST(SPSWrapperFunctionUtilsTest, TransparentConversionExpectedFailureCase) { std::string ErrMsg; SPSWrapperFunction<SPSExpected<int32_t>(int32_t)>::call( DirectCaller(nullptr, halve_number_sps_wrapper), @@ -221,6 +221,27 @@ TEST(SPSWrapperFunctionUtilsTest, TestFunctionReturningExpectedFailureCase) { EXPECT_EQ(ErrMsg, "N is not a multiple of 2"); } +static void +round_trip_int_pointer_sps_wrapper(orc_rt_SessionRef Session, void *CallCtx, + orc_rt_WrapperFunctionReturn Return, + orc_rt_WrapperFunctionBuffer ArgBytes) { + SPSWrapperFunction<SPSExecutorAddr(SPSExecutorAddr)>::handle( + Session, CallCtx, Return, ArgBytes, + [](move_only_function<void(int32_t *)> Return, int32_t *P) { + Return(P); + }); +} + +TEST(SPSWrapperFunctionUtilsTest, TransparentSerializationPointers) { + int X = 42; + int *P = nullptr; + SPSWrapperFunction<SPSExecutorAddr(SPSExecutorAddr)>::call( + DirectCaller(nullptr, round_trip_int_pointer_sps_wrapper), + [&](Expected<int32_t *> R) { P = cantFail(std::move(R)); }, &X); + + EXPECT_EQ(P, &X); +} + template <size_t N> struct SPSOpCounter {}; namespace orc_rt { @@ -249,7 +270,7 @@ handle_with_reference_types_sps_wrapper(orc_rt_SessionRef Session, OpCounter<3> &&) { Return(); }); } -TEST(SPSWrapperFunctionUtilsTest, TestHandlerWithReferences) { +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(); @@ -297,3 +318,53 @@ TEST(SPSWrapperFunctionUtilsTest, TestHandlerWithReferences) { 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 Session, + void *CallCtx, + orc_rt_WrapperFunctionReturn Return, + orc_rt_WrapperFunctionBuffer ArgBytes) { + SPSWrapperFunction<int32_t(SPSExecutorAddr, int32_t, int32_t)>::handle( + Session, CallCtx, 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 Session, void *CallCtx, + orc_rt_WrapperFunctionReturn Return, + orc_rt_WrapperFunctionBuffer ArgBytes) { + SPSWrapperFunction<int32_t(SPSExecutorAddr, int32_t, int32_t)>::handle( + Session, CallCtx, 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); +} |