| #include <function2/function2.hpp> |
| #include <stdplus/function_view.hpp> |
| |
| #include <functional> |
| |
| #include <gtest/gtest.h> |
| |
| namespace stdplus |
| { |
| |
| int add(int a, int b) |
| { |
| return a + b; |
| } |
| |
| int mul(int a, int b) noexcept |
| { |
| return a * b; |
| } |
| |
| struct Funcy |
| { |
| int a = 0; |
| |
| int operator()(float y) noexcept |
| { |
| return a += (int)y; |
| } |
| |
| int operator()(int b) noexcept |
| { |
| return a += b; |
| } |
| }; |
| |
| struct FuncyPriv : private Funcy |
| { |
| using Funcy::operator(); |
| }; |
| |
| int callFv(stdplus::function_view<int(int, int)> fv) |
| { |
| return fv(3, 5); |
| } |
| |
| TEST(FunctionView, Ptr) |
| { |
| function_view<int(int, int)> fv = add; |
| EXPECT_TRUE(fv); |
| EXPECT_EQ(3, fv(1, 2)); |
| EXPECT_EQ(14, fv(5, 9)); |
| fv = mul; |
| EXPECT_EQ(14, fv(7, 2)); |
| EXPECT_EQ(0, fv(0, 10)); |
| function_view fv2 = add; |
| EXPECT_EQ(1, fv2(1, 0)); |
| |
| fv = nullptr; |
| EXPECT_FALSE(fv); |
| |
| EXPECT_EQ(8, callFv(add)); |
| } |
| |
| TEST(FunctionView, Obj) |
| { |
| Funcy f; |
| function_view<int(int)> fv = f; |
| EXPECT_EQ(2, fv(2)); |
| EXPECT_EQ(5, fv(3)); |
| |
| FuncyPriv fp; |
| fv = fp; |
| EXPECT_EQ(2, fv(2)); |
| EXPECT_EQ(5, fv(3)); |
| } |
| |
| TEST(FunctionView, Lambda) |
| { |
| auto addl = [](int a, int b) { return a + b; }; |
| function_view<int(int, int)> fv = addl; |
| EXPECT_EQ(3, fv(1, 2)); |
| EXPECT_EQ(14, fv(5, 9)); |
| function_view fv2 = addl; |
| EXPECT_EQ(1, fv2(1, 0)); |
| fv = fv2; |
| EXPECT_EQ(1, fv(1, 0)); |
| function_view<int(int, int) const> fv3 = addl; |
| EXPECT_EQ(1, fv3(1, 0)); |
| |
| auto mull = [old = 1](int a) mutable { return old *= a; }; |
| function_view fvm = mull; |
| EXPECT_EQ(2, fvm(2)); |
| EXPECT_EQ(4, fvm(2)); |
| |
| auto mula = [](auto a, auto b) { return a + b; }; |
| fv = mula; |
| EXPECT_EQ(5, fv(3, 2)); |
| |
| EXPECT_EQ(8, callFv([](auto a, auto b) { return a + b; })); |
| } |
| |
| TEST(FunctionView, StdFunction) |
| { |
| std::function<int(int, int)> addf = add; |
| function_view<int(int, int)> fv = addf; |
| EXPECT_EQ(3, fv(1, 2)); |
| EXPECT_EQ(14, fv(5, 9)); |
| { |
| function_view fv2 = addf; |
| EXPECT_EQ(1, fv2(1, 0)); |
| fv = fv2; |
| } |
| EXPECT_EQ(6, fv(3, 3)); |
| |
| fu2::unique_function<int(int, int) const noexcept> mulf = mul; |
| function_view<int(int, int) const> fv2 = mulf; |
| EXPECT_EQ(2, fv2(1, 2)); |
| |
| fu2::unique_function<int(int)> mulfa = [old = 1](auto a) mutable { |
| return old *= a; |
| }; |
| function_view<int(int)> fva = mulfa; |
| EXPECT_EQ(3, fva(3)); |
| EXPECT_EQ(9, fva(3)); |
| } |
| |
| } // namespace stdplus |