1 //===----- WrapperFunctionUtilsTest.cpp - Test Wrapper-Function utils -----===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "llvm/ExecutionEngine/Orc/Shared/WrapperFunctionUtils.h"
10 #include "llvm/ADT/FunctionExtras.h"
11 #include "llvm/Testing/Support/Error.h"
12 #include "gtest/gtest.h"
17 using namespace llvm::orc
;
18 using namespace llvm::orc::shared
;
21 constexpr const char *TestString
= "test string";
22 } // end anonymous namespace
24 TEST(WrapperFunctionUtilsTest
, DefaultWrapperFunctionResult
) {
25 WrapperFunctionResult R
;
26 EXPECT_TRUE(R
.empty());
27 EXPECT_EQ(R
.size(), 0U);
28 EXPECT_EQ(R
.getOutOfBandError(), nullptr);
31 TEST(WrapperFunctionUtilsTest
, WrapperFunctionResultFromRange
) {
32 auto R
= WrapperFunctionResult::copyFrom(TestString
, strlen(TestString
) + 1);
33 EXPECT_EQ(R
.size(), strlen(TestString
) + 1);
34 EXPECT_TRUE(strcmp(R
.data(), TestString
) == 0);
35 EXPECT_FALSE(R
.empty());
36 EXPECT_EQ(R
.getOutOfBandError(), nullptr);
39 TEST(WrapperFunctionUtilsTest
, WrapperFunctionResultFromCString
) {
40 auto R
= WrapperFunctionResult::copyFrom(TestString
);
41 EXPECT_EQ(R
.size(), strlen(TestString
) + 1);
42 EXPECT_TRUE(strcmp(R
.data(), TestString
) == 0);
43 EXPECT_FALSE(R
.empty());
44 EXPECT_EQ(R
.getOutOfBandError(), nullptr);
47 TEST(WrapperFunctionUtilsTest
, WrapperFunctionResultFromStdString
) {
48 auto R
= WrapperFunctionResult::copyFrom(std::string(TestString
));
49 EXPECT_EQ(R
.size(), strlen(TestString
) + 1);
50 EXPECT_TRUE(strcmp(R
.data(), TestString
) == 0);
51 EXPECT_FALSE(R
.empty());
52 EXPECT_EQ(R
.getOutOfBandError(), nullptr);
55 TEST(WrapperFunctionUtilsTest
, WrapperFunctionResultFromOutOfBandError
) {
56 auto R
= WrapperFunctionResult::createOutOfBandError(TestString
);
57 EXPECT_FALSE(R
.empty());
58 EXPECT_TRUE(strcmp(R
.getOutOfBandError(), TestString
) == 0);
61 TEST(WrapperFunctionUtilsTest
, WrapperFunctionCCallCreateEmpty
) {
63 WrapperFunctionCall::Create
<SPSArgList
<>>(ExecutorAddr()), Succeeded());
66 static void voidNoop() {}
70 AddClass(int32_t X
) : X(X
) {}
71 int32_t addMethod(int32_t Y
) { return X
+ Y
; }
76 static WrapperFunctionResult
voidNoopWrapper(const char *ArgData
,
78 return WrapperFunction
<void()>::handle(ArgData
, ArgSize
, voidNoop
);
81 static WrapperFunctionResult
addWrapper(const char *ArgData
, size_t ArgSize
) {
82 return WrapperFunction
<int32_t(int32_t, int32_t)>::handle(
83 ArgData
, ArgSize
, [](int32_t X
, int32_t Y
) -> int32_t { return X
+ Y
; });
86 static WrapperFunctionResult
addMethodWrapper(const char *ArgData
,
88 return WrapperFunction
<int32_t(SPSExecutorAddr
, int32_t)>::handle(
89 ArgData
, ArgSize
, makeMethodWrapperHandler(&AddClass::addMethod
));
92 TEST(WrapperFunctionUtilsTest
, WrapperFunctionCallAndHandleVoid
) {
93 EXPECT_FALSE(!!WrapperFunction
<void()>::call(voidNoopWrapper
));
96 TEST(WrapperFunctionUtilsTest
, WrapperFunctionCallAndHandleRet
) {
98 EXPECT_FALSE(!!WrapperFunction
<int32_t(int32_t, int32_t)>::call(
99 addWrapper
, Result
, 1, 2));
100 EXPECT_EQ(Result
, (int32_t)3);
103 TEST(WrapperFunctionUtilsTest
, WrapperFunctionMethodCallAndHandleRet
) {
106 EXPECT_FALSE(!!WrapperFunction
<int32_t(SPSExecutorAddr
, int32_t)>::call(
107 addMethodWrapper
, Result
, ExecutorAddr::fromPtr(&AddObj
), 2));
108 EXPECT_EQ(Result
, (int32_t)3);
111 static void voidNoopAsync(unique_function
<void(SPSEmpty
)> SendResult
) {
112 SendResult(SPSEmpty());
115 static WrapperFunctionResult
voidNoopAsyncWrapper(const char *ArgData
,
117 std::promise
<WrapperFunctionResult
> RP
;
118 auto RF
= RP
.get_future();
120 WrapperFunction
<void()>::handleAsync(
121 ArgData
, ArgSize
, voidNoopAsync
,
122 [&](WrapperFunctionResult R
) { RP
.set_value(std::move(R
)); });
127 static WrapperFunctionResult
addAsyncWrapper(const char *ArgData
,
129 std::promise
<WrapperFunctionResult
> RP
;
130 auto RF
= RP
.get_future();
132 WrapperFunction
<int32_t(int32_t, int32_t)>::handleAsync(
134 [](unique_function
<void(int32_t)> SendResult
, int32_t X
, int32_t Y
) {
137 [&](WrapperFunctionResult R
) { RP
.set_value(std::move(R
)); });
141 TEST(WrapperFunctionUtilsTest
, WrapperFunctionCallAndHandleAsyncVoid
) {
142 EXPECT_FALSE(!!WrapperFunction
<void()>::call(voidNoopAsyncWrapper
));
145 TEST(WrapperFunctionUtilsTest
, WrapperFunctionCallAndHandleAsyncRet
) {
147 EXPECT_FALSE(!!WrapperFunction
<int32_t(int32_t, int32_t)>::call(
148 addAsyncWrapper
, Result
, 1, 2));
149 EXPECT_EQ(Result
, (int32_t)3);
152 static WrapperFunctionResult
failingWrapper(const char *ArgData
,
154 return WrapperFunctionResult::createOutOfBandError("failed");
157 void asyncFailingWrapperCaller(unique_function
<void(WrapperFunctionResult
)> F
,
158 const char *ArgData
, size_t ArgSize
) {
159 F(failingWrapper(ArgData
, ArgSize
));
162 TEST(WrapperFunctionUtilsTest
, WrapperFunctionCallFailingAsync
) {
163 WrapperFunction
<void()>::callAsync(asyncFailingWrapperCaller
, [](Error Err
) {
164 EXPECT_THAT_ERROR(std::move(Err
), Failed());