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 "gtest/gtest.h"
16 using namespace llvm::orc::shared
;
19 constexpr const char *TestString
= "test string";
20 } // end anonymous namespace
22 TEST(WrapperFunctionUtilsTest
, DefaultWrapperFunctionResult
) {
23 WrapperFunctionResult R
;
24 EXPECT_TRUE(R
.empty());
25 EXPECT_EQ(R
.size(), 0U);
26 EXPECT_EQ(R
.getOutOfBandError(), nullptr);
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);
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);
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);
53 TEST(WrapperFunctionUtilsTest
, WrapperFunctionResultFromOutOfBandError
) {
54 auto R
= WrapperFunctionResult::createOutOfBandError(TestString
);
55 EXPECT_FALSE(R
.empty());
56 EXPECT_TRUE(strcmp(R
.getOutOfBandError(), TestString
) == 0);
59 static void voidNoop() {}
61 static WrapperFunctionResult
voidNoopWrapper(const char *ArgData
,
63 return WrapperFunction
<void()>::handle(ArgData
, ArgSize
, voidNoop
);
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
; });
71 TEST(WrapperFunctionUtilsTest
, WrapperFunctionCallAndHandleVoid
) {
72 EXPECT_FALSE(!!WrapperFunction
<void()>::call(voidNoopWrapper
));
75 TEST(WrapperFunctionUtilsTest
, WrapperFunctionCallAndHandleRet
) {
77 EXPECT_FALSE(!!WrapperFunction
<int32_t(int32_t, int32_t)>::call(
78 addWrapper
, Result
, 1, 2));
79 EXPECT_EQ(Result
, (int32_t)3);
82 static void voidNoopAsync(unique_function
<void(SPSEmpty
)> SendResult
) {
83 SendResult(SPSEmpty());
86 static WrapperFunctionResult
voidNoopAsyncWrapper(const char *ArgData
,
88 std::promise
<WrapperFunctionResult
> RP
;
89 auto RF
= RP
.get_future();
91 WrapperFunction
<void()>::handleAsync(
92 ArgData
, ArgSize
, voidNoopAsync
,
93 [&](WrapperFunctionResult R
) { RP
.set_value(std::move(R
)); });
98 static WrapperFunctionResult
addAsyncWrapper(const char *ArgData
,
100 std::promise
<WrapperFunctionResult
> RP
;
101 auto RF
= RP
.get_future();
103 WrapperFunction
<int32_t(int32_t, int32_t)>::handleAsync(
105 [](unique_function
<void(int32_t)> SendResult
, int32_t X
, int32_t Y
) {
108 [&](WrapperFunctionResult R
) { RP
.set_value(std::move(R
)); });
112 TEST(WrapperFunctionUtilsTest
, WrapperFunctionCallAndHandleAsyncVoid
) {
113 EXPECT_FALSE(!!WrapperFunction
<void()>::call(voidNoopAsyncWrapper
));
116 TEST(WrapperFunctionUtilsTest
, WrapperFunctionCallAndHandleAsyncRet
) {
118 EXPECT_FALSE(!!WrapperFunction
<int32_t(int32_t, int32_t)>::call(
119 addAsyncWrapper
, Result
, 1, 2));
120 EXPECT_EQ(Result
, (int32_t)3);