William A. Kennington III | ece1943 | 2023-07-15 03:17:24 -0700 | [diff] [blame] | 1 | #include <function2/function2.hpp> |
| 2 | #include <stdplus/function_view.hpp> |
| 3 | |
| 4 | #include <functional> |
| 5 | |
| 6 | #include <gtest/gtest.h> |
| 7 | |
| 8 | namespace stdplus |
| 9 | { |
| 10 | |
| 11 | int add(int a, int b) |
| 12 | { |
| 13 | return a + b; |
| 14 | } |
| 15 | |
| 16 | int mul(int a, int b) noexcept |
| 17 | { |
| 18 | return a * b; |
| 19 | } |
| 20 | |
| 21 | struct Funcy |
| 22 | { |
| 23 | int a = 0; |
| 24 | |
| 25 | int operator()(float y) noexcept |
| 26 | { |
| 27 | return a += (int)y; |
| 28 | } |
| 29 | |
| 30 | int operator()(int b) noexcept |
| 31 | { |
| 32 | return a += b; |
| 33 | } |
| 34 | }; |
| 35 | |
| 36 | struct FuncyPriv : private Funcy |
| 37 | { |
| 38 | using Funcy::operator(); |
| 39 | }; |
| 40 | |
| 41 | int callFv(stdplus::function_view<int(int, int)> fv) |
| 42 | { |
| 43 | return fv(3, 5); |
| 44 | } |
| 45 | |
| 46 | TEST(FunctionView, Ptr) |
| 47 | { |
| 48 | function_view<int(int, int)> fv = add; |
William A. Kennington III | 1c8d0ae | 2023-07-16 14:44:28 -0700 | [diff] [blame^] | 49 | EXPECT_TRUE(fv); |
William A. Kennington III | ece1943 | 2023-07-15 03:17:24 -0700 | [diff] [blame] | 50 | EXPECT_EQ(3, fv(1, 2)); |
| 51 | EXPECT_EQ(14, fv(5, 9)); |
| 52 | fv = mul; |
| 53 | EXPECT_EQ(14, fv(7, 2)); |
| 54 | EXPECT_EQ(0, fv(0, 10)); |
| 55 | function_view fv2 = add; |
| 56 | EXPECT_EQ(1, fv2(1, 0)); |
| 57 | |
William A. Kennington III | 1c8d0ae | 2023-07-16 14:44:28 -0700 | [diff] [blame^] | 58 | fv = nullptr; |
| 59 | EXPECT_FALSE(fv); |
| 60 | |
William A. Kennington III | ece1943 | 2023-07-15 03:17:24 -0700 | [diff] [blame] | 61 | EXPECT_EQ(8, callFv(add)); |
| 62 | } |
| 63 | |
| 64 | TEST(FunctionView, Obj) |
| 65 | { |
| 66 | Funcy f; |
| 67 | function_view<int(int)> fv = f; |
| 68 | EXPECT_EQ(2, fv(2)); |
| 69 | EXPECT_EQ(5, fv(3)); |
| 70 | |
| 71 | FuncyPriv fp; |
| 72 | fv = fp; |
| 73 | EXPECT_EQ(2, fv(2)); |
| 74 | EXPECT_EQ(5, fv(3)); |
| 75 | } |
| 76 | |
| 77 | TEST(FunctionView, Lambda) |
| 78 | { |
| 79 | auto addl = [](int a, int b) { return a + b; }; |
| 80 | function_view<int(int, int)> fv = addl; |
| 81 | EXPECT_EQ(3, fv(1, 2)); |
| 82 | EXPECT_EQ(14, fv(5, 9)); |
| 83 | function_view fv2 = addl; |
| 84 | EXPECT_EQ(1, fv2(1, 0)); |
| 85 | fv = fv2; |
| 86 | EXPECT_EQ(1, fv(1, 0)); |
| 87 | function_view<int(int, int) const> fv3 = addl; |
| 88 | EXPECT_EQ(1, fv3(1, 0)); |
| 89 | |
| 90 | auto mull = [old = 1](int a) mutable { return old *= a; }; |
| 91 | function_view fvm = mull; |
| 92 | EXPECT_EQ(2, fvm(2)); |
| 93 | EXPECT_EQ(4, fvm(2)); |
| 94 | |
| 95 | auto mula = [](auto a, auto b) { return a + b; }; |
| 96 | fv = mula; |
| 97 | EXPECT_EQ(5, fv(3, 2)); |
| 98 | |
| 99 | EXPECT_EQ(8, callFv([](auto a, auto b) { return a + b; })); |
| 100 | } |
| 101 | |
| 102 | TEST(FunctionView, StdFunction) |
| 103 | { |
| 104 | std::function<int(int, int)> addf = add; |
| 105 | function_view<int(int, int)> fv = addf; |
| 106 | EXPECT_EQ(3, fv(1, 2)); |
| 107 | EXPECT_EQ(14, fv(5, 9)); |
| 108 | { |
| 109 | function_view fv2 = addf; |
| 110 | EXPECT_EQ(1, fv2(1, 0)); |
| 111 | fv = fv2; |
| 112 | } |
| 113 | EXPECT_EQ(6, fv(3, 3)); |
| 114 | |
| 115 | fu2::unique_function<int(int, int) const noexcept> mulf = mul; |
| 116 | function_view<int(int, int) const> fv2 = mulf; |
| 117 | EXPECT_EQ(2, fv2(1, 2)); |
| 118 | |
| 119 | fu2::unique_function<int(int)> mulfa = [old = 1](auto a) mutable { |
| 120 | return old *= a; |
| 121 | }; |
| 122 | function_view<int(int)> fva = mulfa; |
| 123 | EXPECT_EQ(3, fva(3)); |
| 124 | EXPECT_EQ(9, fva(3)); |
| 125 | } |
| 126 | |
| 127 | } // namespace stdplus |