// g++ --std=c++11 test.cpp #include "test.h" #include "fl/function.h" #include "fl/function_list.h" using namespace fl; // Free function for testing static int add(int a, int b) { return a + b; } struct Foo { int value = 0; void set(int v) { value = v; } int get() const { return value; } }; struct Mult { int operator()(int a, int b) const { return a * b; } }; TEST_CASE("function is empty by default and bool-convertible") { function f; REQUIRE(!f); } TEST_CASE("Test function with lambda") { function f = [](int a, int b) { return a + b; }; REQUIRE(f); REQUIRE(f(2, 3) == 5); } TEST_CASE("Test function with free function pointer") { function f(add); REQUIRE(f); REQUIRE(f(4, 6) == 10); } TEST_CASE("Test function with functor object") { Mult m; function f(m); REQUIRE(f); REQUIRE(f(3, 7) == 21); } TEST_CASE("Test function with non-const member function") { Foo foo; function fset(&Foo::set, &foo); REQUIRE(fset); fset(42); REQUIRE(foo.value == 42); } TEST_CASE("Test function with const member function") { Foo foo; foo.value = 99; function fget(&Foo::get, &foo); REQUIRE(fget); REQUIRE(fget() == 99); } TEST_CASE("Void free function test") { function f = [](float) { /* do nothing */ }; REQUIRE(f); f(1); } TEST_CASE("Copy and move semantics") { function orig = [](int a, int b) { return a - b; }; REQUIRE(orig(10, 4) == 6); // Copy function copy = orig; REQUIRE(copy); REQUIRE(copy(8, 3) == 5); // Move function moved = std::move(orig); REQUIRE(moved); REQUIRE(moved(7, 2) == 5); REQUIRE(!orig); } TEST_CASE("Function list void float") { FunctionList fl; fl.add([](float) { /* do nothing */ }); fl.invoke(1); }