Erki e554d30bf6
All checks were successful
continuous-integration/drone/push Build is passing
gitea/skullc-peripherals/pipeline/head This commit looks good
Utility: add the ability to generate static functions from IFunction
2022-06-26 17:53:40 +03:00

132 lines
2.6 KiB
C++

//
// Created by erki on 03.07.21.
//
#include <catch2/catch.hpp>
#include <utility_function.hpp>
#include <utility_tag.hpp>
TEST_CASE("Function calls function appropriately.", "[utility],[function]")
{
static bool func_called = false;
auto func_to_call = []() {
func_called = true;
};
Utility::Function<void()> function(func_to_call);
function();
REQUIRE(func_called == true);
}
TEST_CASE("Function passes arguments appropriately.", "[utility],[function]")
{
static int int_to_set = 0;
static bool bool_to_set = false;
auto func_to_call = [](const int i, const bool b) {
int_to_set = i;
bool_to_set = b;
};
Utility::Function<void(int, bool)> function(func_to_call);
function(10, true);
REQUIRE(int_to_set == 10);
REQUIRE(bool_to_set == true);
}
TEST_CASE("Function works with toStaticFunction.", "[utility],[function]")
{
static int int_to_set = 0;
static bool bool_to_set = false;
auto func_to_call = [](const int i, const bool b) {
int_to_set = i;
bool_to_set = b;
};
Utility::Function<void(int, bool)> function(func_to_call);
using signature = void (*)(int, bool);
signature func_pointer = function.toStaticFunction<SKULLC_TAG>();
func_pointer(10, true);
REQUIRE(int_to_set == 10);
REQUIRE(bool_to_set == true);
}
TEST_CASE("FunctionOwned calls function appropriately.", "[utility],[function]")
{
struct S
{
bool to_set = false;
void toCall()
{
to_set = true;
}
};
S subject;
Utility::FunctionOwned<S, void()> function(subject, &S::toCall);
function();
REQUIRE(subject.to_set == true);
}
TEST_CASE("FunctionOwned passes arguments appropriately.", "[utility],[function]")
{
struct S
{
int int_to_set = 0;
bool bool_to_set = true;
void toCall(const int i, const bool b)
{
int_to_set = i;
bool_to_set = b;
}
};
S subject;
Utility::FunctionOwned<S, void(int, bool)> function(subject, &S::toCall);
function(10, false);
REQUIRE(subject.int_to_set == 10);
REQUIRE(subject.bool_to_set == false);
}
TEST_CASE("FunctionOwned works with toStaticFunction.", "[utility],[function]")
{
struct S
{
int int_to_set = 0;
bool bool_to_set = true;
void toCall(const int i, const bool b)
{
int_to_set = i;
bool_to_set = b;
}
};
S subject;
Utility::FunctionOwned<S, void(int, bool)> function(subject, &S::toCall);
using signature = void (*)(int, bool);
signature func_pointer = function.toStaticFunction<SKULLC_TAG>();
func_pointer(10, true);
REQUIRE(subject.int_to_set == 10);
REQUIRE(subject.bool_to_set == true);
}