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 "gtest/gtest.h"
12 
13 #include <future>
14 
15 using namespace llvm;
16 using namespace llvm::orc::shared;
17 
18 namespace {
19 constexpr const char *TestString = "test string";
20 } // end anonymous namespace
21 
TEST(WrapperFunctionUtilsTest,DefaultWrapperFunctionResult)22 TEST(WrapperFunctionUtilsTest, DefaultWrapperFunctionResult) {
23   WrapperFunctionResult R;
24   EXPECT_TRUE(R.empty());
25   EXPECT_EQ(R.size(), 0U);
26   EXPECT_EQ(R.getOutOfBandError(), nullptr);
27 }
28 
TEST(WrapperFunctionUtilsTest,WrapperFunctionResultFromRange)29 TEST(WrapperFunctionUtilsTest, WrapperFunctionResultFromRange) {
30   auto R = WrapperFunctionResult::copyFrom(TestString, strlen(TestString) + 1);
31   EXPECT_EQ(R.size(), strlen(TestString) + 1);
32   EXPECT_TRUE(strcmp(R.data(), TestString) == 0);
33   EXPECT_FALSE(R.empty());
34   EXPECT_EQ(R.getOutOfBandError(), nullptr);
35 }
36 
TEST(WrapperFunctionUtilsTest,WrapperFunctionResultFromCString)37 TEST(WrapperFunctionUtilsTest, WrapperFunctionResultFromCString) {
38   auto R = WrapperFunctionResult::copyFrom(TestString);
39   EXPECT_EQ(R.size(), strlen(TestString) + 1);
40   EXPECT_TRUE(strcmp(R.data(), TestString) == 0);
41   EXPECT_FALSE(R.empty());
42   EXPECT_EQ(R.getOutOfBandError(), nullptr);
43 }
44 
TEST(WrapperFunctionUtilsTest,WrapperFunctionResultFromStdString)45 TEST(WrapperFunctionUtilsTest, WrapperFunctionResultFromStdString) {
46   auto R = WrapperFunctionResult::copyFrom(std::string(TestString));
47   EXPECT_EQ(R.size(), strlen(TestString) + 1);
48   EXPECT_TRUE(strcmp(R.data(), TestString) == 0);
49   EXPECT_FALSE(R.empty());
50   EXPECT_EQ(R.getOutOfBandError(), nullptr);
51 }
52 
TEST(WrapperFunctionUtilsTest,WrapperFunctionResultFromOutOfBandError)53 TEST(WrapperFunctionUtilsTest, WrapperFunctionResultFromOutOfBandError) {
54   auto R = WrapperFunctionResult::createOutOfBandError(TestString);
55   EXPECT_FALSE(R.empty());
56   EXPECT_TRUE(strcmp(R.getOutOfBandError(), TestString) == 0);
57 }
58 
voidNoop()59 static void voidNoop() {}
60 
voidNoopWrapper(const char * ArgData,size_t ArgSize)61 static WrapperFunctionResult voidNoopWrapper(const char *ArgData,
62                                              size_t ArgSize) {
63   return WrapperFunction<void()>::handle(ArgData, ArgSize, voidNoop);
64 }
65 
addWrapper(const char * ArgData,size_t ArgSize)66 static WrapperFunctionResult addWrapper(const char *ArgData, size_t ArgSize) {
67   return WrapperFunction<int32_t(int32_t, int32_t)>::handle(
68       ArgData, ArgSize, [](int32_t X, int32_t Y) -> int32_t { return X + Y; });
69 }
70 
TEST(WrapperFunctionUtilsTest,WrapperFunctionCallAndHandleVoid)71 TEST(WrapperFunctionUtilsTest, WrapperFunctionCallAndHandleVoid) {
72   EXPECT_FALSE(!!WrapperFunction<void()>::call(voidNoopWrapper));
73 }
74 
TEST(WrapperFunctionUtilsTest,WrapperFunctionCallAndHandleRet)75 TEST(WrapperFunctionUtilsTest, WrapperFunctionCallAndHandleRet) {
76   int32_t Result;
77   EXPECT_FALSE(!!WrapperFunction<int32_t(int32_t, int32_t)>::call(
78       addWrapper, Result, 1, 2));
79   EXPECT_EQ(Result, (int32_t)3);
80 }
81 
voidNoopAsync(unique_function<void (SPSEmpty)> SendResult)82 static void voidNoopAsync(unique_function<void(SPSEmpty)> SendResult) {
83   SendResult(SPSEmpty());
84 }
85 
voidNoopAsyncWrapper(const char * ArgData,size_t ArgSize)86 static WrapperFunctionResult voidNoopAsyncWrapper(const char *ArgData,
87                                                   size_t ArgSize) {
88   std::promise<WrapperFunctionResult> RP;
89   auto RF = RP.get_future();
90 
91   WrapperFunction<void()>::handleAsync(
92       ArgData, ArgSize, voidNoopAsync,
93       [&](WrapperFunctionResult R) { RP.set_value(std::move(R)); });
94 
95   return RF.get();
96 }
97 
addAsyncWrapper(const char * ArgData,size_t ArgSize)98 static WrapperFunctionResult addAsyncWrapper(const char *ArgData,
99                                              size_t ArgSize) {
100   std::promise<WrapperFunctionResult> RP;
101   auto RF = RP.get_future();
102 
103   WrapperFunction<int32_t(int32_t, int32_t)>::handleAsync(
104       ArgData, ArgSize,
105       [](unique_function<void(int32_t)> SendResult, int32_t X, int32_t Y) {
106         SendResult(X + Y);
107       },
108       [&](WrapperFunctionResult R) { RP.set_value(std::move(R)); });
109   return RF.get();
110 }
111 
TEST(WrapperFunctionUtilsTest,WrapperFunctionCallAndHandleAsyncVoid)112 TEST(WrapperFunctionUtilsTest, WrapperFunctionCallAndHandleAsyncVoid) {
113   EXPECT_FALSE(!!WrapperFunction<void()>::call(voidNoopAsyncWrapper));
114 }
115 
TEST(WrapperFunctionUtilsTest,WrapperFunctionCallAndHandleAsyncRet)116 TEST(WrapperFunctionUtilsTest, WrapperFunctionCallAndHandleAsyncRet) {
117   int32_t Result;
118   EXPECT_FALSE(!!WrapperFunction<int32_t(int32_t, int32_t)>::call(
119       addAsyncWrapper, Result, 1, 2));
120   EXPECT_EQ(Result, (int32_t)3);
121 }
122