From 2f2ba142a3b03e30dfde4ccd00d5af446eaf6bc5 Mon Sep 17 00:00:00 2001 From: audiobird Date: Mon, 14 Apr 2025 08:31:19 -0700 Subject: [PATCH 1/3] Fix callable Function<> class --- util/callable.hh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/util/callable.hh b/util/callable.hh index 9d09893..91b4431 100644 --- a/util/callable.hh +++ b/util/callable.hh @@ -104,19 +104,19 @@ public: Ret call(Args... args) { if (m_callback) - return m_callback(&m_data[0], std::forward(args)...); + return m_callback(&m_data[0], std::forward(args)...); return Ret(); } Ret operator()(Args... args) { - return call(std::forward(args)...); + return call(std::forward(args)...); } private: template static Ret invoke(void *object, Args... args) { Callable &callable = *reinterpret_cast(object); - callable(std::forward(args)...); + return callable(std::forward(args)...); } template From 18fd4d4bee0118d0c6256b15e1223b66670451cc Mon Sep 17 00:00:00 2001 From: audiobird Date: Mon, 14 Apr 2025 08:31:23 -0700 Subject: [PATCH 2/3] Add callable test --- tests/callable_tests.cc | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 tests/callable_tests.cc diff --git a/tests/callable_tests.cc b/tests/callable_tests.cc new file mode 100644 index 0000000..2a9a96f --- /dev/null +++ b/tests/callable_tests.cc @@ -0,0 +1,15 @@ +#include "doctest.h" +#include "util/callable.hh" + +TEST_CASE("Callable Function") { + auto capture{0}; + auto func = Function{[&capture](int a, int b) { + capture += 10; + return a + b + capture; + }}; + + CHECK(func(10, 10) == 30); + CHECK(capture == 10); + CHECK(func(10, 10) == 40); + CHECK(capture == 20); +} From 07436e211c96428b92ef33b68e88037dd1071378 Mon Sep 17 00:00:00 2001 From: Dan Green Date: Mon, 14 Apr 2025 09:16:11 -0700 Subject: [PATCH 3/3] More callable tests --- tests/callable_tests.cc | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/tests/callable_tests.cc b/tests/callable_tests.cc index 2a9a96f..a726b0b 100644 --- a/tests/callable_tests.cc +++ b/tests/callable_tests.cc @@ -13,3 +13,38 @@ TEST_CASE("Callable Function") { CHECK(func(10, 10) == 40); CHECK(capture == 20); } + +TEST_CASE("Callable Function mixed params") { + auto capture{0}; + auto func = Function{[&capture](int a, float b) { + capture += 10; + return a + b + capture; + }}; + + CHECK(func(10, 10.f) == 30); + CHECK(capture == 10); + CHECK(func(10, 10.f) == 40); + CHECK(capture == 20); +} + +TEST_CASE("Callable Function lots of params") { + struct A { + int a; + char b; + float d; + double e; + }; + auto capture{0}; + auto func = Function{[&capture](int a, float b, char x, A aa, A bb) { + capture += 10; + if (x == 'a') + return aa.a + bb.b + capture * 2.f; + else + return a + b + capture; + }}; + + CHECK(func(10, 10.f, 'x', {}, {2, 'b', 1.2f, 1.4}) == 30); + CHECK(capture == 10); + CHECK(func(10, 10.f, 'a', {}, {2, 'a', 1.2f, 1.4}) == 137); + CHECK(capture == 20); +}