IR: de-duplicate two CmpInst routines (NFC) (#116866)
[llvm-project.git] / llvm / unittests / ExecutionEngine / Orc / WrapperFunctionUtilsTest.cpp
blob1ed690ae9b986c893f0dee8db1e0334bb715a89d
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 //===----------------------------------------------------------------------===//
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"
14 #include <future>
16 using namespace llvm;
17 using namespace llvm::orc;
18 using namespace llvm::orc::shared;
20 namespace {
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) {
62 EXPECT_THAT_EXPECTED(
63 WrapperFunctionCall::Create<SPSArgList<>>(ExecutorAddr()), Succeeded());
66 static void voidNoop() {}
68 class AddClass {
69 public:
70 AddClass(int32_t X) : X(X) {}
71 int32_t addMethod(int32_t Y) { return X + Y; }
72 private:
73 int32_t X;
76 static WrapperFunctionResult voidNoopWrapper(const char *ArgData,
77 size_t ArgSize) {
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,
87 size_t ArgSize) {
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) {
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);
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);
111 static void voidNoopAsync(unique_function<void(SPSEmpty)> SendResult) {
112 SendResult(SPSEmpty());
115 static WrapperFunctionResult voidNoopAsyncWrapper(const char *ArgData,
116 size_t ArgSize) {
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)); });
124 return RF.get();
127 static WrapperFunctionResult addAsyncWrapper(const char *ArgData,
128 size_t ArgSize) {
129 std::promise<WrapperFunctionResult> RP;
130 auto RF = RP.get_future();
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);
137 [&](WrapperFunctionResult R) { RP.set_value(std::move(R)); });
138 return RF.get();
141 TEST(WrapperFunctionUtilsTest, WrapperFunctionCallAndHandleAsyncVoid) {
142 EXPECT_FALSE(!!WrapperFunction<void()>::call(voidNoopAsyncWrapper));
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);
152 static WrapperFunctionResult failingWrapper(const char *ArgData,
153 size_t ArgSize) {
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());