diff --git a/TESTS/mbed_drivers/callback/main.cpp b/TESTS/mbed_drivers/callback/main.cpp new file mode 100644 index 0000000000..90fdd1af1c --- /dev/null +++ b/TESTS/mbed_drivers/callback/main.cpp @@ -0,0 +1,267 @@ +#include "mbed.h" +#include "test_env.h" +#include "unity.h" +#include "utest.h" + +using namespace utest::v1; + + +// static functions +template +T static_func5(T a0, T a1, T a2, T a3, T a4) { return a0 | a1 | a2 | a3 | a4; } +template +T static_func4(T a0, T a1, T a2, T a3) { return a0 | a1 | a2 | a3; } +template +T static_func3(T a0, T a1, T a2) { return a0 | a1 | a2; } +template +T static_func2(T a0, T a1) { return a0 | a1; } +template +T static_func1(T a0) { return a0; } +template +T static_func0() { return 0; } + +// class functions +template +struct Thing { + T t; + Thing() : t(0x80) {} + + T member_func5(T a0, T a1, T a2, T a3, T a4) { return t | a0 | a1 | a2 | a3 | a4; } + T member_func4(T a0, T a1, T a2, T a3) { return t | a0 | a1 | a2 | a3; } + T member_func3(T a0, T a1, T a2) { return t | a0 | a1 | a2; } + T member_func2(T a0, T a1) { return t | a0 | a1; } + T member_func1(T a0) { return t | a0; } + T member_func0() { return t; } +}; + +// bound functions +template +T bound_func5(Thing *t, T a0, T a1, T a2, T a3, T a4) { return t->t | a0 | a1 | a2 | a3 | a4; } +template +T bound_func4(Thing *t, T a0, T a1, T a2, T a3) { return t->t | a0 | a1 | a2 | a3; } +template +T bound_func3(Thing *t, T a0, T a1, T a2) { return t->t | a0 | a1 | a2; } +template +T bound_func2(Thing *t, T a0, T a1) { return t->t | a0 | a1; } +template +T bound_func1(Thing *t, T a0) { return t->t | a0; } +template +T bound_func0(Thing *t) { return t->t; } + + +// function call and result verification +template +struct Verifier { + static void verify5(Callback func) { + T result = func(0x01, 0x02, 0x04, 0x08, 0x10); + TEST_ASSERT_EQUAL(result, 0x1f); + } + + template + static void verify5(O *obj, M method) { + Callback func(obj, method); + T result = func(0x01, 0x02, 0x04, 0x08, 0x10); + TEST_ASSERT_EQUAL(result, 0x9f); + } + + static void verify4(Callback func) { + T result = func(0x01, 0x02, 0x04, 0x08); + TEST_ASSERT_EQUAL(result, 0x0f); + } + + template + static void verify4(O *obj, M method) { + Callback func(obj, method); + T result = func(0x01, 0x02, 0x04, 0x08); + TEST_ASSERT_EQUAL(result, 0x8f); + } + + static void verify3(Callback func) { + T result = func(0x01, 0x02, 0x04); + TEST_ASSERT_EQUAL(result, 0x07); + } + + template + static void verify3(O *obj, M method) { + Callback func(obj, method); + T result = func(0x01, 0x02, 0x04); + TEST_ASSERT_EQUAL(result, 0x87); + } + + static void verify2(Callback func) { + T result = func(0x01, 0x02); + TEST_ASSERT_EQUAL(result, 0x03); + } + + template + static void verify2(O *obj, M method) { + Callback func(obj, method); + T result = func(0x01, 0x02); + TEST_ASSERT_EQUAL(result, 0x83); + } + + static void verify1(Callback func) { + T result = func(0x01); + TEST_ASSERT_EQUAL(result, 0x01); + } + + template + static void verify1(O *obj, M method) { + Callback func(obj, method); + T result = func(0x01); + TEST_ASSERT_EQUAL(result, 0x81); + } + + static void verify0(Callback func) { + T result = func(); + TEST_ASSERT_EQUAL(result, 0x00); + } + + template + static void verify0(O *obj, M method) { + Callback func(obj, method); + T result = func(); + TEST_ASSERT_EQUAL(result, 0x80); + } +}; + + +// test dispatch +template +void test_dispatch5() { + Thing thing; + Verifier::verify5(static_func5); + Verifier::verify5(&thing, &Thing::member_func5); + Verifier::verify5(&thing, &bound_func5); + + Callback callback(static_func5); + Verifier::verify5(callback); + callback.attach(&thing, &bound_func5); + Verifier::verify5(&callback, &Callback::call); + Verifier::verify5((void*)&callback, &Callback::thunk); +} + +template +void test_dispatch4() { + Thing thing; + Verifier::verify4(static_func4); + Verifier::verify4(&thing, &Thing::member_func4); + Verifier::verify4(&thing, &bound_func4); + + Callback callback(static_func4); + Verifier::verify4(callback); + callback.attach(&thing, &bound_func4); + Verifier::verify4(&callback, &Callback::call); + Verifier::verify4((void*)&callback, &Callback::thunk); +} + +template +void test_dispatch3() { + Thing thing; + Verifier::verify3(static_func3); + Verifier::verify3(&thing, &Thing::member_func3); + Verifier::verify3(&thing, &bound_func3); + + Callback callback(static_func3); + Verifier::verify3(callback); + callback.attach(&thing, &bound_func3); + Verifier::verify3(&callback, &Callback::call); + Verifier::verify3((void*)&callback, &Callback::thunk); +} + +template +void test_dispatch2() { + Thing thing; + Verifier::verify2(static_func2); + Verifier::verify2(&thing, &Thing::member_func2); + Verifier::verify2(&thing, &bound_func2); + + Callback callback(static_func2); + Verifier::verify2(callback); + callback.attach(&thing, &bound_func2); + Verifier::verify2(&callback, &Callback::call); + Verifier::verify2((void*)&callback, &Callback::thunk); +} + +template +void test_dispatch1() { + Thing thing; + Verifier::verify1(static_func1); + Verifier::verify1(&thing, &Thing::member_func1); + Verifier::verify1(&thing, &bound_func1); + + Callback callback(static_func1); + Verifier::verify1(callback); + callback.attach(&thing, &bound_func1); + Verifier::verify1(&callback, &Callback::call); + Verifier::verify1((void*)&callback, &Callback::thunk); +} + +template +void test_dispatch0() { + Thing thing; + Verifier::verify0(static_func0); + Verifier::verify0(&thing, &Thing::member_func0); + Verifier::verify0(&thing, &bound_func0); + + Callback callback(static_func0); + Verifier::verify0(callback); + callback.attach(&thing, &bound_func0); + Verifier::verify0(&callback, &Callback::call); + Verifier::verify0((void*)&callback, &Callback::thunk); +} + +template +void test_fparg1() { + Thing thing; + FunctionPointerArg1 fp(static_func1); + Verifier::verify1(fp); + Verifier::verify1(fp.get_function()); +} + +template +void test_fparg0() { + Thing thing; + FunctionPointerArg1 fp(static_func0); + Verifier::verify0(fp); + Verifier::verify0(fp.get_function()); +} + + +// Test setup +utest::v1::status_t test_setup(const size_t number_of_cases) { + GREENTEA_SETUP(40, "default_auto"); + return verbose_test_setup_handler(number_of_cases); +} + +Case cases[] = { + Case("Testing callbacks with 5 ints", test_dispatch5), + Case("Testing callbacks with 4 ints", test_dispatch4), + Case("Testing callbacks with 3 ints", test_dispatch3), + Case("Testing callbacks with 2 ints", test_dispatch2), + Case("Testing callbacks with 1 ints", test_dispatch1), + Case("Testing callbacks with 0 ints", test_dispatch0), + + Case("Testing callbacks with 5 uchars", test_dispatch5), + Case("Testing callbacks with 4 uchars", test_dispatch4), + Case("Testing callbacks with 3 uchars", test_dispatch3), + Case("Testing callbacks with 2 uchars", test_dispatch2), + Case("Testing callbacks with 1 uchars", test_dispatch1), + Case("Testing callbacks with 0 uchars", test_dispatch0), + + Case("Testing callbacks with 5 uint64s", test_dispatch5), + Case("Testing callbacks with 4 uint64s", test_dispatch4), + Case("Testing callbacks with 3 uint64s", test_dispatch3), + Case("Testing callbacks with 2 uint64s", test_dispatch2), + Case("Testing callbacks with 1 uint64s", test_dispatch1), + Case("Testing callbacks with 0 uint64s", test_dispatch0), + + Case("Testing FunctionPointerArg1 compatibility", test_fparg1), + Case("Testing FunctionPointer compatibility", test_fparg0), +}; + +Specification specification(test_setup, cases); + +int main() { + return !Harness::run(specification); +}