basedOpinionated utility library |
git clone git://git.dimitrijedobrota.com/based.git |
Log | Files | Refs | README | LICENSE | HACKING | CONTRIBUTING | CODE_OF_CONDUCT | BUILDING |
curry_test.cpp (2240B)
0 #include <catch2/catch_test_macros.hpp> 1 2 #include "based/functional.hpp" 3 4 // NOLINTBEGIN(*cognitive-complexity*, *magic*) 5 6 namespace 7 { 8 9 auto free_func(int a, double b, int c, double d) 10 { 11 return static_cast<int>(a + b + c + d); 12 } 13 14 } // namespace 15 16 TEST_CASE("free function", "[functional/curry]") 17 { 18 const based::curried curried = free_func; 19 20 REQUIRE(curried(1)(2.0)(3)(4.0) == 10); 21 REQUIRE(curried(1)(2.0, 3)(4.0) == 10); 22 REQUIRE(curried(1)(2.0, 3, 4.0) == 10); 23 REQUIRE(curried(1, 2.0)(3, 4.0) == 10); 24 REQUIRE(curried(1, 2.0, 3)(4.0) == 10); 25 REQUIRE(curried(1, 2.0, 3, 4.0) == 10); 26 } 27 28 TEST_CASE("lambda", "[functional/curry]") 29 { 30 const based::curried curried = [](int a, double b, int c, double d) 31 { 32 return static_cast<int>(a + b + c + d); 33 }; 34 35 REQUIRE(curried(1)(2.0)(3)(4.0) == 10); 36 REQUIRE(curried(1)(2.0)(3, 4.0) == 10); 37 REQUIRE(curried(1)(2.0, 3)(4.0) == 10); 38 REQUIRE(curried(1)(2.0, 3, 4.0) == 10); 39 REQUIRE(curried(1, 2.0)(3)(4.0) == 10); 40 REQUIRE(curried(1, 2.0)(3, 4.0) == 10); 41 REQUIRE(curried(1, 2.0, 3)(4.0) == 10); 42 REQUIRE(curried(1, 2.0, 3, 4.0) == 10); 43 } 44 45 TEST_CASE("member function", "[functional/curry]") 46 { 47 struct test 48 { 49 [[nodiscard]] auto func(int a, double b, int c, double d) const 50 { 51 return static_cast<int>(m_x + a + b + c + d); 52 } 53 54 int m_x = 0; 55 }; 56 57 const based::curried curried = &test::func; 58 test tmp; 59 60 REQUIRE(curried(std::ref(tmp))(1)(2.0)(3)(4.0) == 10); 61 REQUIRE(curried(std::ref(tmp))(1)(2.0)(3, 4.0) == 10); 62 REQUIRE(curried(std::ref(tmp))(1)(2.0, 3)(4.0) == 10); 63 REQUIRE(curried(std::ref(tmp))(1)(2.0, 3, 4.0) == 10); 64 REQUIRE(curried(std::ref(tmp))(1, 2.0)(3)(4.0) == 10); 65 REQUIRE(curried(std::ref(tmp))(1, 2.0)(3, 4.0) == 10); 66 REQUIRE(curried(std::ref(tmp))(1, 2.0, 3)(4.0) == 10); 67 REQUIRE(curried(std::ref(tmp))(1, 2.0, 3, 4.0) == 10); 68 REQUIRE(curried(std::ref(tmp), 1)(2.0)(3)(4.0) == 10); 69 REQUIRE(curried(std::ref(tmp), 1)(2.0, 3)(4.0) == 10); 70 REQUIRE(curried(std::ref(tmp), 1, 2.0)(3)(4.0) == 10); 71 REQUIRE(curried(std::ref(tmp), 1, 2.0, 3)(4.0) == 10); 72 REQUIRE(curried(std::ref(tmp), 1, 2.0, 3, 4.0) == 10); 73 REQUIRE(curried(std::ref(tmp), 1, 2.0, 3, 4.0) == 10); 74 } 75 76 // NOLINTEND(*cognitive-complexity*, *magic*)