brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.3 KiB · 93a61e6 Raw
92 lines · cpp
1//===- bind-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-exception6//7//===----------------------------------------------------------------------===//8//9// Tests for orc-rt's bind-test.h APIs.10//11//===----------------------------------------------------------------------===//12 13#include "CommonTestUtils.h"14#include "orc-rt/bind.h"15#include "orc-rt/move_only_function.h"16#include "gtest/gtest.h"17 18using namespace orc_rt;19 20static void voidVoid(void) {}21 22TEST(BindTest, VoidVoid) {23  auto B = bind_front(voidVoid);24  B();25}26 27static int addInts(int X, int Y) { return X + Y; }28 29TEST(BindTest, SimpleBind) {30  auto Add1 = bind_front(addInts, 1);31  EXPECT_EQ(Add1(2), 3);32}33 34TEST(BindTest, NoBoundArguments) {35  auto Add = bind_front(addInts);36  EXPECT_EQ(Add(1, 2), 3);37}38 39TEST(BindTest, NoFreeArguments) {40  auto Add1And2 = bind_front(addInts, 1, 2);41  EXPECT_EQ(Add1And2(), 3);42}43 44TEST(BindTest, LambdaCapture) {45  auto Add1 = bind_front([](int X, int Y) { return X + Y; }, 1);46  EXPECT_EQ(Add1(2), 3);47}48 49TEST(BindTest, MinimalMoves) {50  OpCounter<>::reset();51  {52    auto B = bind_front([](OpCounter<> &O, int) {}, OpCounter<>());53    B(0);54  }55  EXPECT_EQ(OpCounter<>::defaultConstructions(), 1U);56  EXPECT_EQ(OpCounter<>::copies(), 0U);57  EXPECT_EQ(OpCounter<>::moves(), 1U);58  EXPECT_EQ(OpCounter<>::destructions(), 2U);59}60 61TEST(BindTest, MinimalCopies) {62  OpCounter<>::reset();63  {64    OpCounter<> O;65    auto B = bind_front([](OpCounter<> &O, int) {}, O);66    B(0);67  }68  EXPECT_EQ(OpCounter<>::defaultConstructions(), 1U);69  EXPECT_EQ(OpCounter<>::copies(), 1U);70  EXPECT_EQ(OpCounter<>::moves(), 0U);71  EXPECT_EQ(OpCounter<>::destructions(), 2U);72}73 74TEST(BindTest, ForwardUnboundArgs) {75  auto B = bind_front([](int &) {});76  int N = 7;77  B(N);78}79 80static int increment(int N) { return N + 1; }81 82TEST(BindTest, BindFunction) {83  auto Op = bind_front([](int op(int), int arg) { return op(arg); }, increment);84  EXPECT_EQ(Op(1), 2);85}86 87TEST(BindTest, BindTo_move_only_function) {88  move_only_function<int(int, int)> Add = [](int X, int Y) { return X + Y; };89  auto Add1 = bind_front(std::move(Add), 1);90  EXPECT_EQ(Add1(2), 3);91}92