From e2a9f821ba1f6785f019da31c02092d9e4019a12 Mon Sep 17 00:00:00 2001 From: oktonion <0oktonion0@gmail.com> Date: Fri, 6 Sep 2024 12:07:21 +0300 Subject: [PATCH] + tests --- tests/delegate0_test.cpp | 16 +++++ tests/delegate1_test.cpp | 133 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 149 insertions(+) diff --git a/tests/delegate0_test.cpp b/tests/delegate0_test.cpp index 92bd02a..7894895 100644 --- a/tests/delegate0_test.cpp +++ b/tests/delegate0_test.cpp @@ -88,6 +88,22 @@ TEST_CASE("Testing cpp delegate 0") { CHECK(true == func_called); func_called = false; + + d0.bind(vptr, &void_func_void_p); + + func_called = false; + d0(); + CHECK(true == func_called); + + func_called = false; + + d0 = delegates::bind(vptr, &void_func_void_p); + + func_called = false; + d0(); + CHECK(true == func_called); + + func_called = false; } SUBCASE("Delegate 0 class member func invocation") diff --git a/tests/delegate1_test.cpp b/tests/delegate1_test.cpp index 41ef729..4f24e75 100644 --- a/tests/delegate1_test.cpp +++ b/tests/delegate1_test.cpp @@ -56,6 +56,22 @@ struct TestChild } }; +template +void tmpl_int_func(int) {} +template +void tmpl_func_p(T*, int) {} + +template +delegates::delegate tmpl1_int_func_delegate() { + void(*func)(int) = &tmpl_int_func; + return func; +} +template +delegates::delegate tmpl2_int_func_delegate() { + void(*func)(int) = &tmpl_int_func; + return func; +} + TEST_CASE("Testing cpp delegate 1") { using delegates::delegate; @@ -125,6 +141,20 @@ TEST_CASE("Testing cpp delegate 1") { dd1(42); CHECK(true == func_called); + d1.bind(&void_func_int); + + func_called = false; + d1(42); + CHECK(true == func_called); + + func_called = false; + + d1 = delegates::bind(&void_func_int); + + func_called = false; + d1(42); + CHECK(true == func_called); + func_called = false; } @@ -171,6 +201,30 @@ TEST_CASE("Testing cpp delegate 1") { func_called = false; dd1(42); + CHECK(true == func_called); + + d1.bind(&tt, &Test::call); + + func_called = false; + d1(42); + CHECK(true == func_called); + + d1 = delegates::bind(&tt, &Test::call); + + func_called = false; + d1(42); + CHECK(true == func_called); + + d1.bind(&tt, &Test::call_const); + + func_called = false; + d1(42); + CHECK(true == func_called); + + d1 = delegates::bind(&tt, &Test::call_const); + + func_called = false; + d1(42); CHECK(true == func_called); func_called = false; @@ -255,4 +309,83 @@ TEST_CASE("Testing cpp delegate 1") { } } + + SUBCASE("Delegate 1 comparison") + { + delegate d1; + REQUIRE(!d1); + void(*func)(int) = &tmpl_int_func; + d1 = delegate(func); + delegate d1_other = d1; + + CHECK(d1_other == d1); + CHECK_FALSE(d1_other != d1); + CHECK_FALSE(d1_other < d1); + + func = &tmpl_int_func; + d1_other = delegate(func); + + CHECK(d1_other == d1); + CHECK_FALSE(d1_other != d1); + CHECK_FALSE(d1_other < d1); + + d1_other = delegate(tmpl1_int_func_delegate()); + + CHECK(d1_other == d1); + CHECK_FALSE(d1_other != d1); + CHECK_FALSE(d1_other < d1); + + d1 = delegate(tmpl2_int_func_delegate()); + + CHECK(d1_other == d1); + CHECK_FALSE(d1_other != d1); + CHECK_FALSE(d1_other < d1); + + { + int a = 0; + void(*func)(int*, int) = &tmpl_func_p; + d1_other = delegate(&a, func); + + CHECK_FALSE(d1_other == d1); + CHECK(d1_other != d1); + + func = &tmpl_func_p; + d1 = delegate(&a, func); + + CHECK(d1_other == d1); + CHECK_FALSE(d1_other != d1); + CHECK_FALSE(d1_other < d1); + } + + { + Test t; + d1_other = delegate(&t, &Test::call); + + CHECK_FALSE(d1_other == d1); + CHECK(d1_other != d1); + + d1 = delegate(&t, &Test::call); + + CHECK(d1_other == d1); + CHECK_FALSE(d1_other != d1); + CHECK_FALSE(d1_other < d1); + + d1 = delegate(&t, &Test::call_const); + + CHECK_FALSE(d1_other == d1); + CHECK(d1_other != d1); + + d1_other = delegate(&t, &Test::call_const); + + CHECK(d1_other == d1); + CHECK_FALSE(d1_other != d1); + CHECK_FALSE(d1_other < d1); + } + + func = &tmpl_int_func; + d1_other = delegate(func); + + CHECK_FALSE(d1_other == d1); + CHECK(d1_other != d1); + } }