|
| 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-exception |
| 6 | +// |
| 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 | + |
| 18 | +using namespace orc_rt; |
| 19 | + |
| 20 | +static void voidVoid(void) {} |
| 21 | + |
| 22 | +TEST(BindTest, VoidVoid) { |
| 23 | + auto B = bind_front(voidVoid); |
| 24 | + B(); |
| 25 | +} |
| 26 | + |
| 27 | +static int addInts(int X, int Y) { return X + Y; } |
| 28 | + |
| 29 | +TEST(BindTest, SimpleBind) { |
| 30 | + auto Add1 = bind_front(addInts, 1); |
| 31 | + EXPECT_EQ(Add1(2), 3); |
| 32 | +} |
| 33 | + |
| 34 | +TEST(BindTest, NoBoundArguments) { |
| 35 | + auto Add = bind_front(addInts); |
| 36 | + EXPECT_EQ(Add(1, 2), 3); |
| 37 | +} |
| 38 | + |
| 39 | +TEST(BindTest, NoFreeArguments) { |
| 40 | + auto Add1And2 = bind_front(addInts, 1, 2); |
| 41 | + EXPECT_EQ(Add1And2(), 3); |
| 42 | +} |
| 43 | + |
| 44 | +TEST(BindTest, LambdaCapture) { |
| 45 | + auto Add1 = bind_front([](int X, int Y) { return X + Y; }, 1); |
| 46 | + EXPECT_EQ(Add1(2), 3); |
| 47 | +} |
| 48 | + |
| 49 | +TEST(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 | + |
| 61 | +TEST(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 | + |
| 74 | +static int increment(int N) { return N + 1; } |
| 75 | + |
| 76 | +TEST(BindTest, BindFunction) { |
| 77 | + auto Op = bind_front([](int op(int), int arg) { return op(arg); }, increment); |
| 78 | + EXPECT_EQ(Op(1), 2); |
| 79 | +} |
| 80 | + |
| 81 | +TEST(BindTest, BindTo_move_only_function) { |
| 82 | + move_only_function<int(int, int)> Add = [](int X, int Y) { return X + Y; }; |
| 83 | + auto Add1 = bind_front(std::move(Add), 1); |
| 84 | + EXPECT_EQ(Add1(2), 3); |
| 85 | +} |
0 commit comments