1 //===-- wrapper_function_utils_test.cpp -----------------------------------===//
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 // This file is a part of the ORC runtime.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "wrapper_function_utils.h"
14 #include "gtest/gtest.h"
15 
16 #include <stdio.h>
17 
18 using namespace __orc_rt;
19 
20 namespace {
21 constexpr const char *TestString = "test string";
22 } // end anonymous namespace
23 
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 
31 TEST(WrapperFunctionUtilsTest, WrapperFunctionResultFromCStruct) {
32   __orc_rt_CWrapperFunctionResult CR =
33       __orc_rt_CreateCWrapperFunctionResultFromString(TestString);
34   WrapperFunctionResult R(CR);
35   EXPECT_EQ(R.size(), strlen(TestString) + 1);
36   EXPECT_TRUE(strcmp(R.data(), TestString) == 0);
37   EXPECT_FALSE(R.empty());
38   EXPECT_EQ(R.getOutOfBandError(), nullptr);
39 }
40 
41 TEST(WrapperFunctionUtilsTest, WrapperFunctionResultFromRange) {
42   auto R = WrapperFunctionResult::copyFrom(TestString, strlen(TestString) + 1);
43   EXPECT_EQ(R.size(), strlen(TestString) + 1);
44   EXPECT_TRUE(strcmp(R.data(), TestString) == 0);
45   EXPECT_FALSE(R.empty());
46   EXPECT_EQ(R.getOutOfBandError(), nullptr);
47 }
48 
49 TEST(WrapperFunctionUtilsTest, WrapperFunctionResultFromCString) {
50   auto R = WrapperFunctionResult::copyFrom(TestString);
51   EXPECT_EQ(R.size(), strlen(TestString) + 1);
52   EXPECT_TRUE(strcmp(R.data(), TestString) == 0);
53   EXPECT_FALSE(R.empty());
54   EXPECT_EQ(R.getOutOfBandError(), nullptr);
55 }
56 
57 TEST(WrapperFunctionUtilsTest, WrapperFunctionResultFromStdString) {
58   auto R = WrapperFunctionResult::copyFrom(std::string(TestString));
59   EXPECT_EQ(R.size(), strlen(TestString) + 1);
60   EXPECT_TRUE(strcmp(R.data(), TestString) == 0);
61   EXPECT_FALSE(R.empty());
62   EXPECT_EQ(R.getOutOfBandError(), nullptr);
63 }
64 
65 TEST(WrapperFunctionUtilsTest, WrapperFunctionResultFromOutOfBandError) {
66   auto R = WrapperFunctionResult::createOutOfBandError(TestString);
67   EXPECT_FALSE(R.empty());
68   EXPECT_TRUE(strcmp(R.getOutOfBandError(), TestString) == 0);
69 }
70 
71 static __orc_rt_CWrapperFunctionResult addWrapper(const char *ArgData,
72                                                   size_t ArgSize) {
73   return WrapperFunction<int32_t(int32_t, int32_t)>::handle(
74              ArgData, ArgSize,
75              [](int32_t X, int32_t Y) -> int32_t { return X + Y; })
76       .release();
77 }
78 
79 extern "C" __orc_rt_Opaque __orc_rt_jit_dispatch_ctx{};
80 
81 extern "C" __orc_rt_CWrapperFunctionResult
82 __orc_rt_jit_dispatch(__orc_rt_Opaque *Ctx, const void *FnTag,
83                       const char *ArgData, size_t ArgSize) {
84   using WrapperFunctionType =
85       __orc_rt_CWrapperFunctionResult (*)(const char *, size_t);
86 
87   return reinterpret_cast<WrapperFunctionType>(const_cast<void *>(FnTag))(
88       ArgData, ArgSize);
89 }
90 
91 TEST(WrapperFunctionUtilsTest, WrapperFunctionCallAndHandle) {
92   int32_t Result;
93   EXPECT_FALSE(!!WrapperFunction<int32_t(int32_t, int32_t)>::call(
94       (void *)&addWrapper, Result, 1, 2));
95   EXPECT_EQ(Result, (int32_t)3);
96 }
97