xref: /llvm-project/llvm/unittests/ExecutionEngine/Orc/WrapperFunctionUtilsTest.cpp (revision 0e43f3b04d527776458df7aa1d7ce1787ff0b32f)
1 //===----- WrapperFunctionUtilsTest.cpp - Test Wrapper-Function utils -----===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 
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"
13 
14 #include <future>
15 
16 using namespace llvm;
17 using namespace llvm::orc;
18 using namespace llvm::orc::shared;
19 
20 namespace {
21 constexpr const char *TestString = "test string";
22 } // end anonymous namespace
23 
TEST(WrapperFunctionUtilsTest,DefaultWrapperFunctionResult)24 TEST(WrapperFunctionUtilsTest, DefaultWrapperFunctionResult) {
25   WrapperFunctionResult R;
26   EXPECT_TRUE(R.empty());
27   EXPECT_EQ(R.size(), 0U);
28   EXPECT_EQ(R.getOutOfBandError(), nullptr);
29 }
30 
TEST(WrapperFunctionUtilsTest,WrapperFunctionResultFromRange)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);
37 }
38 
TEST(WrapperFunctionUtilsTest,WrapperFunctionResultFromCString)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);
45 }
46 
TEST(WrapperFunctionUtilsTest,WrapperFunctionResultFromStdString)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);
53 }
54 
TEST(WrapperFunctionUtilsTest,WrapperFunctionResultFromOutOfBandError)55 TEST(WrapperFunctionUtilsTest, WrapperFunctionResultFromOutOfBandError) {
56   auto R = WrapperFunctionResult::createOutOfBandError(TestString);
57   EXPECT_FALSE(R.empty());
58   EXPECT_TRUE(strcmp(R.getOutOfBandError(), TestString) == 0);
59 }
60 
TEST(WrapperFunctionUtilsTest,WrapperFunctionCCallCreateEmpty)61 TEST(WrapperFunctionUtilsTest, WrapperFunctionCCallCreateEmpty) {
62   EXPECT_THAT_EXPECTED(
63       WrapperFunctionCall::Create<SPSArgList<>>(ExecutorAddr()), Succeeded());
64 }
65 
voidNoop()66 static void voidNoop() {}
67 
68 class AddClass {
69 public:
AddClass(int32_t X)70   AddClass(int32_t X) : X(X) {}
addMethod(int32_t Y)71   int32_t addMethod(int32_t Y) { return X + Y; }
72 private:
73   int32_t X;
74 };
75 
voidNoopWrapper(const char * ArgData,size_t ArgSize)76 static WrapperFunctionResult voidNoopWrapper(const char *ArgData,
77                                              size_t ArgSize) {
78   return WrapperFunction<void()>::handle(ArgData, ArgSize, voidNoop);
79 }
80 
addWrapper(const char * ArgData,size_t ArgSize)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; });
84 }
85 
addMethodWrapper(const char * ArgData,size_t ArgSize)86 static WrapperFunctionResult addMethodWrapper(const char *ArgData,
87                                               size_t ArgSize) {
88   return WrapperFunction<int32_t(SPSExecutorAddr, int32_t)>::handle(
89       ArgData, ArgSize, makeMethodWrapperHandler(&AddClass::addMethod));
90 }
91 
TEST(WrapperFunctionUtilsTest,WrapperFunctionCallAndHandleVoid)92 TEST(WrapperFunctionUtilsTest, WrapperFunctionCallAndHandleVoid) {
93   EXPECT_FALSE(!!WrapperFunction<void()>::call(voidNoopWrapper));
94 }
95 
TEST(WrapperFunctionUtilsTest,WrapperFunctionCallAndHandleRet)96 TEST(WrapperFunctionUtilsTest, WrapperFunctionCallAndHandleRet) {
97   int32_t Result;
98   EXPECT_FALSE(!!WrapperFunction<int32_t(int32_t, int32_t)>::call(
99       addWrapper, Result, 1, 2));
100   EXPECT_EQ(Result, (int32_t)3);
101 }
102 
TEST(WrapperFunctionUtilsTest,WrapperFunctionMethodCallAndHandleRet)103 TEST(WrapperFunctionUtilsTest, WrapperFunctionMethodCallAndHandleRet) {
104   int32_t Result;
105   AddClass AddObj(1);
106   EXPECT_FALSE(!!WrapperFunction<int32_t(SPSExecutorAddr, int32_t)>::call(
107       addMethodWrapper, Result, ExecutorAddr::fromPtr(&AddObj), 2));
108   EXPECT_EQ(Result, (int32_t)3);
109 }
110 
voidNoopAsync(unique_function<void (SPSEmpty)> SendResult)111 static void voidNoopAsync(unique_function<void(SPSEmpty)> SendResult) {
112   SendResult(SPSEmpty());
113 }
114 
voidNoopAsyncWrapper(const char * ArgData,size_t ArgSize)115 static WrapperFunctionResult voidNoopAsyncWrapper(const char *ArgData,
116                                                   size_t ArgSize) {
117   std::promise<WrapperFunctionResult> RP;
118   auto RF = RP.get_future();
119 
120   WrapperFunction<void()>::handleAsync(
121       ArgData, ArgSize, voidNoopAsync,
122       [&](WrapperFunctionResult R) { RP.set_value(std::move(R)); });
123 
124   return RF.get();
125 }
126 
addAsyncWrapper(const char * ArgData,size_t ArgSize)127 static WrapperFunctionResult addAsyncWrapper(const char *ArgData,
128                                              size_t ArgSize) {
129   std::promise<WrapperFunctionResult> RP;
130   auto RF = RP.get_future();
131 
132   WrapperFunction<int32_t(int32_t, int32_t)>::handleAsync(
133       ArgData, ArgSize,
134       [](unique_function<void(int32_t)> SendResult, int32_t X, int32_t Y) {
135         SendResult(X + Y);
136       },
137       [&](WrapperFunctionResult R) { RP.set_value(std::move(R)); });
138   return RF.get();
139 }
140 
TEST(WrapperFunctionUtilsTest,WrapperFunctionCallAndHandleAsyncVoid)141 TEST(WrapperFunctionUtilsTest, WrapperFunctionCallAndHandleAsyncVoid) {
142   EXPECT_FALSE(!!WrapperFunction<void()>::call(voidNoopAsyncWrapper));
143 }
144 
TEST(WrapperFunctionUtilsTest,WrapperFunctionCallAndHandleAsyncRet)145 TEST(WrapperFunctionUtilsTest, WrapperFunctionCallAndHandleAsyncRet) {
146   int32_t Result;
147   EXPECT_FALSE(!!WrapperFunction<int32_t(int32_t, int32_t)>::call(
148       addAsyncWrapper, Result, 1, 2));
149   EXPECT_EQ(Result, (int32_t)3);
150 }
151 
failingWrapper(const char * ArgData,size_t ArgSize)152 static WrapperFunctionResult failingWrapper(const char *ArgData,
153                                             size_t ArgSize) {
154   return WrapperFunctionResult::createOutOfBandError("failed");
155 }
156 
asyncFailingWrapperCaller(unique_function<void (WrapperFunctionResult)> F,const char * ArgData,size_t ArgSize)157 void asyncFailingWrapperCaller(unique_function<void(WrapperFunctionResult)> F,
158                                const char *ArgData, size_t ArgSize) {
159   F(failingWrapper(ArgData, ArgSize));
160 }
161 
TEST(WrapperFunctionUtilsTest,WrapperFunctionCallFailingAsync)162 TEST(WrapperFunctionUtilsTest, WrapperFunctionCallFailingAsync) {
163   WrapperFunction<void()>::callAsync(asyncFailingWrapperCaller, [](Error Err) {
164     EXPECT_THAT_ERROR(std::move(Err), Failed());
165   });
166 }
167