brintos

brintos / llvm-project-archived public Read only

0
0
Text · 5.6 KiB · 8de2412 Raw
168 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 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"13 14#include <future>15 16using namespace llvm;17using namespace llvm::orc;18using namespace llvm::orc::shared;19 20namespace {21constexpr const char *TestString = "test string";22} // end anonymous namespace23 24TEST(WrapperFunctionUtilsTest, DefaultWrapperFunctionResult) {25  WrapperFunctionResult R;26  EXPECT_TRUE(R.empty());27  EXPECT_EQ(R.size(), 0U);28  EXPECT_EQ(R.getOutOfBandError(), nullptr);29}30 31TEST(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);37}38 39TEST(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);45}46 47TEST(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);53}54 55TEST(WrapperFunctionUtilsTest, WrapperFunctionResultFromOutOfBandError) {56  auto R = WrapperFunctionResult::createOutOfBandError(TestString);57  EXPECT_FALSE(R.empty());58  EXPECT_TRUE(strcmp(R.getOutOfBandError(), TestString) == 0);59}60 61TEST(WrapperFunctionUtilsTest, WrapperFunctionCCallCreateEmpty) {62  EXPECT_THAT_EXPECTED(63      WrapperFunctionCall::Create<SPSArgList<>>(ExecutorAddr()), Succeeded());64}65 66static void voidNoop() {}67 68class AddClass {69public:70  AddClass(int32_t X) : X(X) {}71  int32_t addMethod(int32_t Y) { return X + Y; }72private:73  int32_t X;74};75 76static WrapperFunctionResult voidNoopWrapper(const char *ArgData,77                                             size_t ArgSize) {78  return WrapperFunction<void()>::handle(ArgData, ArgSize, voidNoop);79}80 81static 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; });84}85 86static WrapperFunctionResult addMethodWrapper(const char *ArgData,87                                              size_t ArgSize) {88  return WrapperFunction<int32_t(SPSExecutorAddr, int32_t)>::handle(89      ArgData, ArgSize, makeMethodWrapperHandler(&AddClass::addMethod));90}91 92TEST(WrapperFunctionUtilsTest, WrapperFunctionCallAndHandleVoid) {93  EXPECT_FALSE(!!WrapperFunction<void()>::call(voidNoopWrapper));94}95 96TEST(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);101}102 103TEST(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);109}110 111static void voidNoopAsync(unique_function<void(SPSEmpty)> SendResult) {112  SendResult(SPSEmpty());113}114 115static WrapperFunctionResult voidNoopAsyncWrapper(const char *ArgData,116                                                  size_t ArgSize) {117  std::promise<WrapperFunctionResult> RP;118  auto RF = RP.get_future();119 120  WrapperFunction<void()>::handleAsync(121      ArgData, ArgSize,122      [&](WrapperFunctionResult R) { RP.set_value(std::move(R)); },123      voidNoopAsync);124 125  return RF.get();126}127 128static WrapperFunctionResult addAsyncWrapper(const char *ArgData,129                                             size_t ArgSize) {130  std::promise<WrapperFunctionResult> RP;131  auto RF = RP.get_future();132 133  WrapperFunction<int32_t(int32_t, int32_t)>::handleAsync(134      ArgData, ArgSize,135      [&](WrapperFunctionResult R) { RP.set_value(std::move(R)); },136      [](unique_function<void(int32_t)> SendResult, int32_t X, int32_t Y) {137        SendResult(X + Y);138      });139  return RF.get();140}141 142TEST(WrapperFunctionUtilsTest, WrapperFunctionCallAndHandleAsyncVoid) {143  EXPECT_FALSE(!!WrapperFunction<void()>::call(voidNoopAsyncWrapper));144}145 146TEST(WrapperFunctionUtilsTest, WrapperFunctionCallAndHandleAsyncRet) {147  int32_t Result;148  EXPECT_FALSE(!!WrapperFunction<int32_t(int32_t, int32_t)>::call(149      addAsyncWrapper, Result, 1, 2));150  EXPECT_EQ(Result, (int32_t)3);151}152 153static WrapperFunctionResult failingWrapper(const char *ArgData,154                                            size_t ArgSize) {155  return WrapperFunctionResult::createOutOfBandError("failed");156}157 158void asyncFailingWrapperCaller(unique_function<void(WrapperFunctionResult)> F,159                               const char *ArgData, size_t ArgSize) {160  F(failingWrapper(ArgData, ArgSize));161}162 163TEST(WrapperFunctionUtilsTest, WrapperFunctionCallFailingAsync) {164  WrapperFunction<void()>::callAsync(asyncFailingWrapperCaller, [](Error Err) {165    EXPECT_THAT_ERROR(std::move(Err), Failed());166  });167}168