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 "gtest/gtest.h"
11 
12 using namespace llvm;
13 using namespace llvm::orc::shared;
14 
15 namespace {
16 constexpr const char *TestString = "test string";
17 } // end anonymous namespace
18 
19 TEST(WrapperFunctionUtilsTest, DefaultWrapperFunctionResult) {
20   WrapperFunctionResult R;
21   EXPECT_TRUE(R.empty());
22   EXPECT_EQ(R.size(), 0U);
23   EXPECT_EQ(R.getOutOfBandError(), nullptr);
24 }
25 
26 TEST(WrapperFunctionUtilsTest, WrapperFunctionResultFromRange) {
27   auto R = WrapperFunctionResult::copyFrom(TestString, strlen(TestString) + 1);
28   EXPECT_EQ(R.size(), strlen(TestString) + 1);
29   EXPECT_TRUE(strcmp(R.data(), TestString) == 0);
30   EXPECT_FALSE(R.empty());
31   EXPECT_EQ(R.getOutOfBandError(), nullptr);
32 }
33 
34 TEST(WrapperFunctionUtilsTest, WrapperFunctionResultFromCString) {
35   auto R = WrapperFunctionResult::copyFrom(TestString);
36   EXPECT_EQ(R.size(), strlen(TestString) + 1);
37   EXPECT_TRUE(strcmp(R.data(), TestString) == 0);
38   EXPECT_FALSE(R.empty());
39   EXPECT_EQ(R.getOutOfBandError(), nullptr);
40 }
41 
42 TEST(WrapperFunctionUtilsTest, WrapperFunctionResultFromStdString) {
43   auto R = WrapperFunctionResult::copyFrom(std::string(TestString));
44   EXPECT_EQ(R.size(), strlen(TestString) + 1);
45   EXPECT_TRUE(strcmp(R.data(), TestString) == 0);
46   EXPECT_FALSE(R.empty());
47   EXPECT_EQ(R.getOutOfBandError(), nullptr);
48 }
49 
50 TEST(WrapperFunctionUtilsTest, WrapperFunctionResultFromOutOfBandError) {
51   auto R = WrapperFunctionResult::createOutOfBandError(TestString);
52   EXPECT_FALSE(R.empty());
53   EXPECT_TRUE(strcmp(R.getOutOfBandError(), TestString) == 0);
54 }
55 
56 static WrapperFunctionResult voidNoopWrapper(const char *ArgData,
57                                              size_t ArgSize) {
58   return WrapperFunction<void()>::handle(ArgData, ArgSize, voidNoop);
59 }
60 
61 static WrapperFunctionResult addWrapper(const char *ArgData, size_t ArgSize) {
62   return WrapperFunction<int32_t(int32_t, int32_t)>::handle(
63       ArgData, ArgSize, [](int32_t X, int32_t Y) -> int32_t { return X + Y; });
64 }
65 
66 TEST(WrapperFunctionUtilsTest, WrapperFunctionCallVoidNoopAndHandle) {
67   EXPECT_FALSE(!!WrapperFunction<void()>::call((void *)&voidNoopWrapper));
68 }
69 
70 TEST(WrapperFunctionUtilsTest, WrapperFunctionCallAndHandle) {
71   int32_t Result;
72   EXPECT_FALSE(!!WrapperFunction<int32_t(int32_t, int32_t)>::call(
73       addWrapper, Result, 1, 2));
74   EXPECT_EQ(Result, (int32_t)3);
75 }
76