based

Opinionated utility library
git clone git://git.dimitrijedobrota.com/based.git
Log | Files | Refs | README | LICENSE | HACKING | CONTRIBUTING | CODE_OF_CONDUCT | BUILDING

curry_test.cpp (2246B)


0 #include "based/functional/curry.hpp"
2 #include <catch2/catch_test_macros.hpp>
4 // NOLINTBEGIN(*cognitive-complexity*, *magic*)
6 namespace
7 {
9 auto free_func(int a, double b, int c, double d)
10 {
11 return static_cast<int>(a + b + c + d);
12 }
14 } // namespace
16 TEST_CASE("free function", "[functional/curry]")
17 {
18 const based::curried curried = free_func;
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 }
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 };
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 }
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 }
54 int m_x = 0;
55 };
57 const based::curried curried = &test::func;
58 test tmp;
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 }
76 // NOLINTEND(*cognitive-complexity*, *magic*)