From 5c0f39f190dace90c1428c1b64d758db6561939c Mon Sep 17 00:00:00 2001 From: Christopher Haster Date: Thu, 25 Aug 2016 10:36:29 -0500 Subject: [PATCH] Split callback test into multiple tests based on types As the templated tests grew, the resulting binary exceeded a flash size of 64K. This caused the test to incorrectly fail on small devices. Moved and split into the following: TESTS/mbed_functional/callback TESTS/mbed_functional/callback_small TESTS/mbed_functional/callback_big TESTS/mbed_functional/functionpointer --- TESTS/mbed_functional/callback/main.cpp | 351 ++++++++++++++++++ TESTS/mbed_functional/callback_big/main.cpp | 351 ++++++++++++++++++ .../callback_small}/main.cpp | 33 -- .../mbed_functional/functionpointer/main.cpp | 225 +++++++++++ 4 files changed, 927 insertions(+), 33 deletions(-) create mode 100644 TESTS/mbed_functional/callback/main.cpp create mode 100644 TESTS/mbed_functional/callback_big/main.cpp rename TESTS/{mbed_drivers/callback => mbed_functional/callback_small}/main.cpp (91%) create mode 100644 TESTS/mbed_functional/functionpointer/main.cpp diff --git a/TESTS/mbed_functional/callback/main.cpp b/TESTS/mbed_functional/callback/main.cpp new file mode 100644 index 0000000000..8442448e53 --- /dev/null +++ b/TESTS/mbed_functional/callback/main.cpp @@ -0,0 +1,351 @@ +#include "mbed.h" +#include "greentea-client/test_env.h" +#include "unity.h" +#include "utest.h" + +using namespace utest::v1; + + +// static functions +template +T static_func0() { return 0; } +template +T static_func1(T a0) { return 0 | a0; } +template +T static_func2(T a0, T a1) { return 0 | a0 | a1; } +template +T static_func3(T a0, T a1, T a2) { return 0 | a0 | a1 | a2; } +template +T static_func4(T a0, T a1, T a2, T a3) { return 0 | a0 | a1 | a2 | a3; } +template +T static_func5(T a0, T a1, T a2, T a3, T a4) { return 0 | a0 | a1 | a2 | a3 | a4; } + +// class functions +template +struct Thing { + T t; + Thing() : t(0x80) {} + + T member_func0() { return t; } + T member_func1(T a0) { return t | a0; } + T member_func2(T a0, T a1) { return t | a0 | a1; } + T member_func3(T a0, T a1, T a2) { return t | a0 | a1 | a2; } + T member_func4(T a0, T a1, T a2, T a3) { return t | a0 | a1 | a2 | a3; } + T member_func5(T a0, T a1, T a2, T a3, T a4) { return t | a0 | a1 | a2 | a3 | a4; } + + T const_member_func0() const { return t; } + T const_member_func1(T a0) const { return t | a0; } + T const_member_func2(T a0, T a1) const { return t | a0 | a1; } + T const_member_func3(T a0, T a1, T a2) const { return t | a0 | a1 | a2; } + T const_member_func4(T a0, T a1, T a2, T a3) const { return t | a0 | a1 | a2 | a3; } + T const_member_func5(T a0, T a1, T a2, T a3, T a4) const { return t | a0 | a1 | a2 | a3 | a4; } + + T volatile_member_func0() volatile { return t; } + T volatile_member_func1(T a0) volatile { return t | a0; } + T volatile_member_func2(T a0, T a1) volatile { return t | a0 | a1; } + T volatile_member_func3(T a0, T a1, T a2) volatile { return t | a0 | a1 | a2; } + T volatile_member_func4(T a0, T a1, T a2, T a3) volatile { return t | a0 | a1 | a2 | a3; } + T volatile_member_func5(T a0, T a1, T a2, T a3, T a4) volatile { return t | a0 | a1 | a2 | a3 | a4; } + + T const_volatile_member_func0() const volatile { return t; } + T const_volatile_member_func1(T a0) const volatile { return t | a0; } + T const_volatile_member_func2(T a0, T a1) const volatile { return t | a0 | a1; } + T const_volatile_member_func3(T a0, T a1, T a2) const volatile { return t | a0 | a1 | a2; } + T const_volatile_member_func4(T a0, T a1, T a2, T a3) const volatile { return t | a0 | a1 | a2 | a3; } + T const_volatile_member_func5(T a0, T a1, T a2, T a3, T a4) const volatile { return t | a0 | a1 | a2 | a3 | a4; } +}; + +// bound functions +template +T bound_func0(Thing *t) { return t->t; } +template +T bound_func1(Thing *t, T a0) { return t->t | a0; } +template +T bound_func2(Thing *t, T a0, T a1) { return t->t | a0 | a1; } +template +T bound_func3(Thing *t, T a0, T a1, T a2) { return t->t | a0 | a1 | a2; } +template +T bound_func4(Thing *t, T a0, T a1, T a2, T a3) { return t->t | a0 | a1 | a2 | a3; } +template +T bound_func5(Thing *t, T a0, T a1, T a2, T a3, T a4) { return t->t | a0 | a1 | a2 | a3 | a4; } + +// const bound functions +template +T const_func0(const Thing *t) { return t->t; } +template +T const_func1(const Thing *t, T a0) { return t->t | a0; } +template +T const_func2(const Thing *t, T a0, T a1) { return t->t | a0 | a1; } +template +T const_func3(const Thing *t, T a0, T a1, T a2) { return t->t | a0 | a1 | a2; } +template +T const_func4(const Thing *t, T a0, T a1, T a2, T a3) { return t->t | a0 | a1 | a2 | a3; } +template +T const_func5(const Thing *t, T a0, T a1, T a2, T a3, T a4) { return t->t | a0 | a1 | a2 | a3 | a4; } + +// volatile bound functions +template +T volatile_func0(volatile Thing *t) { return t->t; } +template +T volatile_func1(volatile Thing *t, T a0) { return t->t | a0; } +template +T volatile_func2(volatile Thing *t, T a0, T a1) { return t->t | a0 | a1; } +template +T volatile_func3(volatile Thing *t, T a0, T a1, T a2) { return t->t | a0 | a1 | a2; } +template +T volatile_func4(volatile Thing *t, T a0, T a1, T a2, T a3) { return t->t | a0 | a1 | a2 | a3; } +template +T volatile_func5(volatile Thing *t, T a0, T a1, T a2, T a3, T a4) { return t->t | a0 | a1 | a2 | a3 | a4; } + +// const volatile bound functions +template +T const_volatile_func0(const volatile Thing *t) { return t->t; } +template +T const_volatile_func1(const volatile Thing *t, T a0) { return t->t | a0; } +template +T const_volatile_func2(const volatile Thing *t, T a0, T a1) { return t->t | a0 | a1; } +template +T const_volatile_func3(const volatile Thing *t, T a0, T a1, T a2) { return t->t | a0 | a1 | a2; } +template +T const_volatile_func4(const volatile Thing *t, T a0, T a1, T a2, T a3) { return t->t | a0 | a1 | a2 | a3; } +template +T const_volatile_func5(const volatile Thing *t, T a0, T a1, T a2, T a3, T a4) { return t->t | a0 | a1 | a2 | a3 | a4; } + + +// function call and result verification +template +struct Verifier { + 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); + } + + static void verify1(Callback func) { + T result = func((1 << 0)); + TEST_ASSERT_EQUAL(result, 0x00 | (1 << 0)); + } + + template + static void verify1(O *obj, M method) { + Callback func(obj, method); + T result = func((1 << 0)); + TEST_ASSERT_EQUAL(result, 0x80 | (1 << 0)); + } + + static void verify2(Callback func) { + T result = func((1 << 0), (1 << 1)); + TEST_ASSERT_EQUAL(result, 0x00 | (1 << 0) | (1 << 1)); + } + + template + static void verify2(O *obj, M method) { + Callback func(obj, method); + T result = func((1 << 0), (1 << 1)); + TEST_ASSERT_EQUAL(result, 0x80 | (1 << 0) | (1 << 1)); + } + + static void verify3(Callback func) { + T result = func((1 << 0), (1 << 1), (1 << 2)); + TEST_ASSERT_EQUAL(result, 0x00 | (1 << 0) | (1 << 1) | (1 << 2)); + } + + template + static void verify3(O *obj, M method) { + Callback func(obj, method); + T result = func((1 << 0), (1 << 1), (1 << 2)); + TEST_ASSERT_EQUAL(result, 0x80 | (1 << 0) | (1 << 1) | (1 << 2)); + } + + static void verify4(Callback func) { + T result = func((1 << 0), (1 << 1), (1 << 2), (1 << 3)); + TEST_ASSERT_EQUAL(result, 0x00 | (1 << 0) | (1 << 1) | (1 << 2) | (1 << 3)); + } + + template + static void verify4(O *obj, M method) { + Callback func(obj, method); + T result = func((1 << 0), (1 << 1), (1 << 2), (1 << 3)); + TEST_ASSERT_EQUAL(result, 0x80 | (1 << 0) | (1 << 1) | (1 << 2) | (1 << 3)); + } + + static void verify5(Callback func) { + T result = func((1 << 0), (1 << 1), (1 << 2), (1 << 3), (1 << 4)); + TEST_ASSERT_EQUAL(result, 0x00 | (1 << 0) | (1 << 1) | (1 << 2) | (1 << 3) | (1 << 4)); + } + + template + static void verify5(O *obj, M method) { + Callback func(obj, method); + T result = func((1 << 0), (1 << 1), (1 << 2), (1 << 3), (1 << 4)); + TEST_ASSERT_EQUAL(result, 0x80 | (1 << 0) | (1 << 1) | (1 << 2) | (1 << 3) | (1 << 4)); + } +}; + + +// test dispatch +template +void test_dispatch0() { + Thing thing; + Verifier::verify0(static_func0); + Verifier::verify0(&thing, &Thing::member_func0); + Verifier::verify0((const Thing*)&thing, &Thing::const_member_func0); + Verifier::verify0((volatile Thing*)&thing, &Thing::volatile_member_func0); + Verifier::verify0((const volatile Thing*)&thing, &Thing::const_volatile_member_func0); + Verifier::verify0(&thing, &bound_func0); + Verifier::verify0((const Thing*)&thing, &const_func0); + Verifier::verify0((volatile Thing*)&thing, &volatile_func0); + Verifier::verify0((const volatile Thing*)&thing, &const_volatile_func0); + Verifier::verify0(callback(static_func0)); + + Callback cb(static_func0); + Verifier::verify0(cb); + cb = static_func0; + Verifier::verify0(cb); + cb.attach(&thing, &bound_func0); + Verifier::verify0(&cb, &Callback::call); + Verifier::verify0((void*)&cb, &Callback::thunk); +} + +template +void test_dispatch1() { + Thing thing; + Verifier::verify1(static_func1); + Verifier::verify1(&thing, &Thing::member_func1); + Verifier::verify1((const Thing*)&thing, &Thing::const_member_func1); + Verifier::verify1((volatile Thing*)&thing, &Thing::volatile_member_func1); + Verifier::verify1((const volatile Thing*)&thing, &Thing::const_volatile_member_func1); + Verifier::verify1(&thing, &bound_func1); + Verifier::verify1((const Thing*)&thing, &const_func1); + Verifier::verify1((volatile Thing*)&thing, &volatile_func1); + Verifier::verify1((const volatile Thing*)&thing, &const_volatile_func1); + Verifier::verify1(callback(static_func1)); + + Callback cb(static_func1); + Verifier::verify1(cb); + cb = static_func1; + Verifier::verify1(cb); + cb.attach(&thing, &bound_func1); + Verifier::verify1(&cb, &Callback::call); + Verifier::verify1((void*)&cb, &Callback::thunk); +} + +template +void test_dispatch2() { + Thing thing; + Verifier::verify2(static_func2); + Verifier::verify2(&thing, &Thing::member_func2); + Verifier::verify2((const Thing*)&thing, &Thing::const_member_func2); + Verifier::verify2((volatile Thing*)&thing, &Thing::volatile_member_func2); + Verifier::verify2((const volatile Thing*)&thing, &Thing::const_volatile_member_func2); + Verifier::verify2(&thing, &bound_func2); + Verifier::verify2((const Thing*)&thing, &const_func2); + Verifier::verify2((volatile Thing*)&thing, &volatile_func2); + Verifier::verify2((const volatile Thing*)&thing, &const_volatile_func2); + Verifier::verify2(callback(static_func2)); + + Callback cb(static_func2); + Verifier::verify2(cb); + cb = static_func2; + Verifier::verify2(cb); + cb.attach(&thing, &bound_func2); + Verifier::verify2(&cb, &Callback::call); + Verifier::verify2((void*)&cb, &Callback::thunk); +} + +template +void test_dispatch3() { + Thing thing; + Verifier::verify3(static_func3); + Verifier::verify3(&thing, &Thing::member_func3); + Verifier::verify3((const Thing*)&thing, &Thing::const_member_func3); + Verifier::verify3((volatile Thing*)&thing, &Thing::volatile_member_func3); + Verifier::verify3((const volatile Thing*)&thing, &Thing::const_volatile_member_func3); + Verifier::verify3(&thing, &bound_func3); + Verifier::verify3((const Thing*)&thing, &const_func3); + Verifier::verify3((volatile Thing*)&thing, &volatile_func3); + Verifier::verify3((const volatile Thing*)&thing, &const_volatile_func3); + Verifier::verify3(callback(static_func3)); + + Callback cb(static_func3); + Verifier::verify3(cb); + cb = static_func3; + Verifier::verify3(cb); + cb.attach(&thing, &bound_func3); + Verifier::verify3(&cb, &Callback::call); + Verifier::verify3((void*)&cb, &Callback::thunk); +} + +template +void test_dispatch4() { + Thing thing; + Verifier::verify4(static_func4); + Verifier::verify4(&thing, &Thing::member_func4); + Verifier::verify4((const Thing*)&thing, &Thing::const_member_func4); + Verifier::verify4((volatile Thing*)&thing, &Thing::volatile_member_func4); + Verifier::verify4((const volatile Thing*)&thing, &Thing::const_volatile_member_func4); + Verifier::verify4(&thing, &bound_func4); + Verifier::verify4((const Thing*)&thing, &const_func4); + Verifier::verify4((volatile Thing*)&thing, &volatile_func4); + Verifier::verify4((const volatile Thing*)&thing, &const_volatile_func4); + Verifier::verify4(callback(static_func4)); + + Callback cb(static_func4); + Verifier::verify4(cb); + cb = static_func4; + Verifier::verify4(cb); + cb.attach(&thing, &bound_func4); + Verifier::verify4(&cb, &Callback::call); + Verifier::verify4((void*)&cb, &Callback::thunk); +} + +template +void test_dispatch5() { + Thing thing; + Verifier::verify5(static_func5); + Verifier::verify5(&thing, &Thing::member_func5); + Verifier::verify5((const Thing*)&thing, &Thing::const_member_func5); + Verifier::verify5((volatile Thing*)&thing, &Thing::volatile_member_func5); + Verifier::verify5((const volatile Thing*)&thing, &Thing::const_volatile_member_func5); + Verifier::verify5(&thing, &bound_func5); + Verifier::verify5((const Thing*)&thing, &const_func5); + Verifier::verify5((volatile Thing*)&thing, &volatile_func5); + Verifier::verify5((const volatile Thing*)&thing, &const_volatile_func5); + Verifier::verify5(callback(static_func5)); + + Callback cb(static_func5); + Verifier::verify5(cb); + cb = static_func5; + Verifier::verify5(cb); + cb.attach(&thing, &bound_func5); + Verifier::verify5(&cb, &Callback::call); + Verifier::verify5((void*)&cb, &Callback::thunk); +} + + +// Test setup +utest::v1::status_t test_setup(const size_t number_of_cases) { + GREENTEA_SETUP(10, "default_auto"); + return verbose_test_setup_handler(number_of_cases); +} + +Case cases[] = { + Case("Testing callbacks with 0 ints", test_dispatch0), + Case("Testing callbacks with 1 ints", test_dispatch1), + Case("Testing callbacks with 2 ints", test_dispatch2), + Case("Testing callbacks with 3 ints", test_dispatch3), + Case("Testing callbacks with 4 ints", test_dispatch4), + Case("Testing callbacks with 5 ints", test_dispatch5), +}; + +Specification specification(test_setup, cases); + +int main() { + return !Harness::run(specification); +} diff --git a/TESTS/mbed_functional/callback_big/main.cpp b/TESTS/mbed_functional/callback_big/main.cpp new file mode 100644 index 0000000000..47630a3f21 --- /dev/null +++ b/TESTS/mbed_functional/callback_big/main.cpp @@ -0,0 +1,351 @@ +#include "mbed.h" +#include "greentea-client/test_env.h" +#include "unity.h" +#include "utest.h" + +using namespace utest::v1; + + +// static functions +template +T static_func0() { return 0; } +template +T static_func1(T a0) { return 0 | a0; } +template +T static_func2(T a0, T a1) { return 0 | a0 | a1; } +template +T static_func3(T a0, T a1, T a2) { return 0 | a0 | a1 | a2; } +template +T static_func4(T a0, T a1, T a2, T a3) { return 0 | a0 | a1 | a2 | a3; } +template +T static_func5(T a0, T a1, T a2, T a3, T a4) { return 0 | a0 | a1 | a2 | a3 | a4; } + +// class functions +template +struct Thing { + T t; + Thing() : t(0x80) {} + + T member_func0() { return t; } + T member_func1(T a0) { return t | a0; } + T member_func2(T a0, T a1) { return t | a0 | a1; } + T member_func3(T a0, T a1, T a2) { return t | a0 | a1 | a2; } + T member_func4(T a0, T a1, T a2, T a3) { return t | a0 | a1 | a2 | a3; } + T member_func5(T a0, T a1, T a2, T a3, T a4) { return t | a0 | a1 | a2 | a3 | a4; } + + T const_member_func0() const { return t; } + T const_member_func1(T a0) const { return t | a0; } + T const_member_func2(T a0, T a1) const { return t | a0 | a1; } + T const_member_func3(T a0, T a1, T a2) const { return t | a0 | a1 | a2; } + T const_member_func4(T a0, T a1, T a2, T a3) const { return t | a0 | a1 | a2 | a3; } + T const_member_func5(T a0, T a1, T a2, T a3, T a4) const { return t | a0 | a1 | a2 | a3 | a4; } + + T volatile_member_func0() volatile { return t; } + T volatile_member_func1(T a0) volatile { return t | a0; } + T volatile_member_func2(T a0, T a1) volatile { return t | a0 | a1; } + T volatile_member_func3(T a0, T a1, T a2) volatile { return t | a0 | a1 | a2; } + T volatile_member_func4(T a0, T a1, T a2, T a3) volatile { return t | a0 | a1 | a2 | a3; } + T volatile_member_func5(T a0, T a1, T a2, T a3, T a4) volatile { return t | a0 | a1 | a2 | a3 | a4; } + + T const_volatile_member_func0() const volatile { return t; } + T const_volatile_member_func1(T a0) const volatile { return t | a0; } + T const_volatile_member_func2(T a0, T a1) const volatile { return t | a0 | a1; } + T const_volatile_member_func3(T a0, T a1, T a2) const volatile { return t | a0 | a1 | a2; } + T const_volatile_member_func4(T a0, T a1, T a2, T a3) const volatile { return t | a0 | a1 | a2 | a3; } + T const_volatile_member_func5(T a0, T a1, T a2, T a3, T a4) const volatile { return t | a0 | a1 | a2 | a3 | a4; } +}; + +// bound functions +template +T bound_func0(Thing *t) { return t->t; } +template +T bound_func1(Thing *t, T a0) { return t->t | a0; } +template +T bound_func2(Thing *t, T a0, T a1) { return t->t | a0 | a1; } +template +T bound_func3(Thing *t, T a0, T a1, T a2) { return t->t | a0 | a1 | a2; } +template +T bound_func4(Thing *t, T a0, T a1, T a2, T a3) { return t->t | a0 | a1 | a2 | a3; } +template +T bound_func5(Thing *t, T a0, T a1, T a2, T a3, T a4) { return t->t | a0 | a1 | a2 | a3 | a4; } + +// const bound functions +template +T const_func0(const Thing *t) { return t->t; } +template +T const_func1(const Thing *t, T a0) { return t->t | a0; } +template +T const_func2(const Thing *t, T a0, T a1) { return t->t | a0 | a1; } +template +T const_func3(const Thing *t, T a0, T a1, T a2) { return t->t | a0 | a1 | a2; } +template +T const_func4(const Thing *t, T a0, T a1, T a2, T a3) { return t->t | a0 | a1 | a2 | a3; } +template +T const_func5(const Thing *t, T a0, T a1, T a2, T a3, T a4) { return t->t | a0 | a1 | a2 | a3 | a4; } + +// volatile bound functions +template +T volatile_func0(volatile Thing *t) { return t->t; } +template +T volatile_func1(volatile Thing *t, T a0) { return t->t | a0; } +template +T volatile_func2(volatile Thing *t, T a0, T a1) { return t->t | a0 | a1; } +template +T volatile_func3(volatile Thing *t, T a0, T a1, T a2) { return t->t | a0 | a1 | a2; } +template +T volatile_func4(volatile Thing *t, T a0, T a1, T a2, T a3) { return t->t | a0 | a1 | a2 | a3; } +template +T volatile_func5(volatile Thing *t, T a0, T a1, T a2, T a3, T a4) { return t->t | a0 | a1 | a2 | a3 | a4; } + +// const volatile bound functions +template +T const_volatile_func0(const volatile Thing *t) { return t->t; } +template +T const_volatile_func1(const volatile Thing *t, T a0) { return t->t | a0; } +template +T const_volatile_func2(const volatile Thing *t, T a0, T a1) { return t->t | a0 | a1; } +template +T const_volatile_func3(const volatile Thing *t, T a0, T a1, T a2) { return t->t | a0 | a1 | a2; } +template +T const_volatile_func4(const volatile Thing *t, T a0, T a1, T a2, T a3) { return t->t | a0 | a1 | a2 | a3; } +template +T const_volatile_func5(const volatile Thing *t, T a0, T a1, T a2, T a3, T a4) { return t->t | a0 | a1 | a2 | a3 | a4; } + + +// function call and result verification +template +struct Verifier { + 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); + } + + static void verify1(Callback func) { + T result = func((1 << 0)); + TEST_ASSERT_EQUAL(result, 0x00 | (1 << 0)); + } + + template + static void verify1(O *obj, M method) { + Callback func(obj, method); + T result = func((1 << 0)); + TEST_ASSERT_EQUAL(result, 0x80 | (1 << 0)); + } + + static void verify2(Callback func) { + T result = func((1 << 0), (1 << 1)); + TEST_ASSERT_EQUAL(result, 0x00 | (1 << 0) | (1 << 1)); + } + + template + static void verify2(O *obj, M method) { + Callback func(obj, method); + T result = func((1 << 0), (1 << 1)); + TEST_ASSERT_EQUAL(result, 0x80 | (1 << 0) | (1 << 1)); + } + + static void verify3(Callback func) { + T result = func((1 << 0), (1 << 1), (1 << 2)); + TEST_ASSERT_EQUAL(result, 0x00 | (1 << 0) | (1 << 1) | (1 << 2)); + } + + template + static void verify3(O *obj, M method) { + Callback func(obj, method); + T result = func((1 << 0), (1 << 1), (1 << 2)); + TEST_ASSERT_EQUAL(result, 0x80 | (1 << 0) | (1 << 1) | (1 << 2)); + } + + static void verify4(Callback func) { + T result = func((1 << 0), (1 << 1), (1 << 2), (1 << 3)); + TEST_ASSERT_EQUAL(result, 0x00 | (1 << 0) | (1 << 1) | (1 << 2) | (1 << 3)); + } + + template + static void verify4(O *obj, M method) { + Callback func(obj, method); + T result = func((1 << 0), (1 << 1), (1 << 2), (1 << 3)); + TEST_ASSERT_EQUAL(result, 0x80 | (1 << 0) | (1 << 1) | (1 << 2) | (1 << 3)); + } + + static void verify5(Callback func) { + T result = func((1 << 0), (1 << 1), (1 << 2), (1 << 3), (1 << 4)); + TEST_ASSERT_EQUAL(result, 0x00 | (1 << 0) | (1 << 1) | (1 << 2) | (1 << 3) | (1 << 4)); + } + + template + static void verify5(O *obj, M method) { + Callback func(obj, method); + T result = func((1 << 0), (1 << 1), (1 << 2), (1 << 3), (1 << 4)); + TEST_ASSERT_EQUAL(result, 0x80 | (1 << 0) | (1 << 1) | (1 << 2) | (1 << 3) | (1 << 4)); + } +}; + + +// test dispatch +template +void test_dispatch0() { + Thing thing; + Verifier::verify0(static_func0); + Verifier::verify0(&thing, &Thing::member_func0); + Verifier::verify0((const Thing*)&thing, &Thing::const_member_func0); + Verifier::verify0((volatile Thing*)&thing, &Thing::volatile_member_func0); + Verifier::verify0((const volatile Thing*)&thing, &Thing::const_volatile_member_func0); + Verifier::verify0(&thing, &bound_func0); + Verifier::verify0((const Thing*)&thing, &const_func0); + Verifier::verify0((volatile Thing*)&thing, &volatile_func0); + Verifier::verify0((const volatile Thing*)&thing, &const_volatile_func0); + Verifier::verify0(callback(static_func0)); + + Callback cb(static_func0); + Verifier::verify0(cb); + cb = static_func0; + Verifier::verify0(cb); + cb.attach(&thing, &bound_func0); + Verifier::verify0(&cb, &Callback::call); + Verifier::verify0((void*)&cb, &Callback::thunk); +} + +template +void test_dispatch1() { + Thing thing; + Verifier::verify1(static_func1); + Verifier::verify1(&thing, &Thing::member_func1); + Verifier::verify1((const Thing*)&thing, &Thing::const_member_func1); + Verifier::verify1((volatile Thing*)&thing, &Thing::volatile_member_func1); + Verifier::verify1((const volatile Thing*)&thing, &Thing::const_volatile_member_func1); + Verifier::verify1(&thing, &bound_func1); + Verifier::verify1((const Thing*)&thing, &const_func1); + Verifier::verify1((volatile Thing*)&thing, &volatile_func1); + Verifier::verify1((const volatile Thing*)&thing, &const_volatile_func1); + Verifier::verify1(callback(static_func1)); + + Callback cb(static_func1); + Verifier::verify1(cb); + cb = static_func1; + Verifier::verify1(cb); + cb.attach(&thing, &bound_func1); + Verifier::verify1(&cb, &Callback::call); + Verifier::verify1((void*)&cb, &Callback::thunk); +} + +template +void test_dispatch2() { + Thing thing; + Verifier::verify2(static_func2); + Verifier::verify2(&thing, &Thing::member_func2); + Verifier::verify2((const Thing*)&thing, &Thing::const_member_func2); + Verifier::verify2((volatile Thing*)&thing, &Thing::volatile_member_func2); + Verifier::verify2((const volatile Thing*)&thing, &Thing::const_volatile_member_func2); + Verifier::verify2(&thing, &bound_func2); + Verifier::verify2((const Thing*)&thing, &const_func2); + Verifier::verify2((volatile Thing*)&thing, &volatile_func2); + Verifier::verify2((const volatile Thing*)&thing, &const_volatile_func2); + Verifier::verify2(callback(static_func2)); + + Callback cb(static_func2); + Verifier::verify2(cb); + cb = static_func2; + Verifier::verify2(cb); + cb.attach(&thing, &bound_func2); + Verifier::verify2(&cb, &Callback::call); + Verifier::verify2((void*)&cb, &Callback::thunk); +} + +template +void test_dispatch3() { + Thing thing; + Verifier::verify3(static_func3); + Verifier::verify3(&thing, &Thing::member_func3); + Verifier::verify3((const Thing*)&thing, &Thing::const_member_func3); + Verifier::verify3((volatile Thing*)&thing, &Thing::volatile_member_func3); + Verifier::verify3((const volatile Thing*)&thing, &Thing::const_volatile_member_func3); + Verifier::verify3(&thing, &bound_func3); + Verifier::verify3((const Thing*)&thing, &const_func3); + Verifier::verify3((volatile Thing*)&thing, &volatile_func3); + Verifier::verify3((const volatile Thing*)&thing, &const_volatile_func3); + Verifier::verify3(callback(static_func3)); + + Callback cb(static_func3); + Verifier::verify3(cb); + cb = static_func3; + Verifier::verify3(cb); + cb.attach(&thing, &bound_func3); + Verifier::verify3(&cb, &Callback::call); + Verifier::verify3((void*)&cb, &Callback::thunk); +} + +template +void test_dispatch4() { + Thing thing; + Verifier::verify4(static_func4); + Verifier::verify4(&thing, &Thing::member_func4); + Verifier::verify4((const Thing*)&thing, &Thing::const_member_func4); + Verifier::verify4((volatile Thing*)&thing, &Thing::volatile_member_func4); + Verifier::verify4((const volatile Thing*)&thing, &Thing::const_volatile_member_func4); + Verifier::verify4(&thing, &bound_func4); + Verifier::verify4((const Thing*)&thing, &const_func4); + Verifier::verify4((volatile Thing*)&thing, &volatile_func4); + Verifier::verify4((const volatile Thing*)&thing, &const_volatile_func4); + Verifier::verify4(callback(static_func4)); + + Callback cb(static_func4); + Verifier::verify4(cb); + cb = static_func4; + Verifier::verify4(cb); + cb.attach(&thing, &bound_func4); + Verifier::verify4(&cb, &Callback::call); + Verifier::verify4((void*)&cb, &Callback::thunk); +} + +template +void test_dispatch5() { + Thing thing; + Verifier::verify5(static_func5); + Verifier::verify5(&thing, &Thing::member_func5); + Verifier::verify5((const Thing*)&thing, &Thing::const_member_func5); + Verifier::verify5((volatile Thing*)&thing, &Thing::volatile_member_func5); + Verifier::verify5((const volatile Thing*)&thing, &Thing::const_volatile_member_func5); + Verifier::verify5(&thing, &bound_func5); + Verifier::verify5((const Thing*)&thing, &const_func5); + Verifier::verify5((volatile Thing*)&thing, &volatile_func5); + Verifier::verify5((const volatile Thing*)&thing, &const_volatile_func5); + Verifier::verify5(callback(static_func5)); + + Callback cb(static_func5); + Verifier::verify5(cb); + cb = static_func5; + Verifier::verify5(cb); + cb.attach(&thing, &bound_func5); + Verifier::verify5(&cb, &Callback::call); + Verifier::verify5((void*)&cb, &Callback::thunk); +} + + +// Test setup +utest::v1::status_t test_setup(const size_t number_of_cases) { + GREENTEA_SETUP(10, "default_auto"); + return verbose_test_setup_handler(number_of_cases); +} + +Case cases[] = { + Case("Testing callbacks with 0 uint64s", test_dispatch0), + Case("Testing callbacks with 1 uint64s", test_dispatch1), + Case("Testing callbacks with 2 uint64s", test_dispatch2), + Case("Testing callbacks with 3 uint64s", test_dispatch3), + Case("Testing callbacks with 4 uint64s", test_dispatch4), + Case("Testing callbacks with 5 uint64s", test_dispatch5), +}; + +Specification specification(test_setup, cases); + +int main() { + return !Harness::run(specification); +} diff --git a/TESTS/mbed_drivers/callback/main.cpp b/TESTS/mbed_functional/callback_small/main.cpp similarity index 91% rename from TESTS/mbed_drivers/callback/main.cpp rename to TESTS/mbed_functional/callback_small/main.cpp index 403968a039..e5fa15b4b6 100644 --- a/TESTS/mbed_drivers/callback/main.cpp +++ b/TESTS/mbed_functional/callback_small/main.cpp @@ -328,22 +328,6 @@ void test_dispatch5() { Verifier::verify5((void*)&cb, &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) { @@ -352,29 +336,12 @@ utest::v1::status_t test_setup(const size_t number_of_cases) { } Case cases[] = { - Case("Testing callbacks with 0 ints", test_dispatch0), - Case("Testing callbacks with 1 ints", test_dispatch1), - Case("Testing callbacks with 2 ints", test_dispatch2), - Case("Testing callbacks with 3 ints", test_dispatch3), - Case("Testing callbacks with 4 ints", test_dispatch4), - Case("Testing callbacks with 5 ints", test_dispatch5), - Case("Testing callbacks with 0 uchars", test_dispatch0), Case("Testing callbacks with 1 uchars", test_dispatch1), Case("Testing callbacks with 2 uchars", test_dispatch2), Case("Testing callbacks with 3 uchars", test_dispatch3), Case("Testing callbacks with 4 uchars", test_dispatch4), Case("Testing callbacks with 5 uchars", test_dispatch5), - - Case("Testing callbacks with 0 uint64s", test_dispatch0), - Case("Testing callbacks with 1 uint64s", test_dispatch1), - Case("Testing callbacks with 2 uint64s", test_dispatch2), - Case("Testing callbacks with 3 uint64s", test_dispatch3), - Case("Testing callbacks with 4 uint64s", test_dispatch4), - Case("Testing callbacks with 5 uint64s", test_dispatch5), - - Case("Testing FunctionPointerArg1 compatibility", test_fparg1), - Case("Testing FunctionPointer compatibility", test_fparg0), }; Specification specification(test_setup, cases); diff --git a/TESTS/mbed_functional/functionpointer/main.cpp b/TESTS/mbed_functional/functionpointer/main.cpp new file mode 100644 index 0000000000..7a61dc0abd --- /dev/null +++ b/TESTS/mbed_functional/functionpointer/main.cpp @@ -0,0 +1,225 @@ +#include "mbed.h" +#include "greentea-client/test_env.h" +#include "unity.h" +#include "utest.h" + +using namespace utest::v1; + + +// static functions +template +T static_func0() { return 0; } +template +T static_func1(T a0) { return 0 | a0; } +template +T static_func2(T a0, T a1) { return 0 | a0 | a1; } +template +T static_func3(T a0, T a1, T a2) { return 0 | a0 | a1 | a2; } +template +T static_func4(T a0, T a1, T a2, T a3) { return 0 | a0 | a1 | a2 | a3; } +template +T static_func5(T a0, T a1, T a2, T a3, T a4) { return 0 | a0 | a1 | a2 | a3 | a4; } + +// class functions +template +struct Thing { + T t; + Thing() : t(0x80) {} + + T member_func0() { return t; } + T member_func1(T a0) { return t | a0; } + T member_func2(T a0, T a1) { return t | a0 | a1; } + T member_func3(T a0, T a1, T a2) { return t | a0 | a1 | a2; } + T member_func4(T a0, T a1, T a2, T a3) { return t | a0 | a1 | a2 | a3; } + T member_func5(T a0, T a1, T a2, T a3, T a4) { return t | a0 | a1 | a2 | a3 | a4; } + + T const_member_func0() const { return t; } + T const_member_func1(T a0) const { return t | a0; } + T const_member_func2(T a0, T a1) const { return t | a0 | a1; } + T const_member_func3(T a0, T a1, T a2) const { return t | a0 | a1 | a2; } + T const_member_func4(T a0, T a1, T a2, T a3) const { return t | a0 | a1 | a2 | a3; } + T const_member_func5(T a0, T a1, T a2, T a3, T a4) const { return t | a0 | a1 | a2 | a3 | a4; } + + T volatile_member_func0() volatile { return t; } + T volatile_member_func1(T a0) volatile { return t | a0; } + T volatile_member_func2(T a0, T a1) volatile { return t | a0 | a1; } + T volatile_member_func3(T a0, T a1, T a2) volatile { return t | a0 | a1 | a2; } + T volatile_member_func4(T a0, T a1, T a2, T a3) volatile { return t | a0 | a1 | a2 | a3; } + T volatile_member_func5(T a0, T a1, T a2, T a3, T a4) volatile { return t | a0 | a1 | a2 | a3 | a4; } + + T const_volatile_member_func0() const volatile { return t; } + T const_volatile_member_func1(T a0) const volatile { return t | a0; } + T const_volatile_member_func2(T a0, T a1) const volatile { return t | a0 | a1; } + T const_volatile_member_func3(T a0, T a1, T a2) const volatile { return t | a0 | a1 | a2; } + T const_volatile_member_func4(T a0, T a1, T a2, T a3) const volatile { return t | a0 | a1 | a2 | a3; } + T const_volatile_member_func5(T a0, T a1, T a2, T a3, T a4) const volatile { return t | a0 | a1 | a2 | a3 | a4; } +}; + +// bound functions +template +T bound_func0(Thing *t) { return t->t; } +template +T bound_func1(Thing *t, T a0) { return t->t | a0; } +template +T bound_func2(Thing *t, T a0, T a1) { return t->t | a0 | a1; } +template +T bound_func3(Thing *t, T a0, T a1, T a2) { return t->t | a0 | a1 | a2; } +template +T bound_func4(Thing *t, T a0, T a1, T a2, T a3) { return t->t | a0 | a1 | a2 | a3; } +template +T bound_func5(Thing *t, T a0, T a1, T a2, T a3, T a4) { return t->t | a0 | a1 | a2 | a3 | a4; } + +// const bound functions +template +T const_func0(const Thing *t) { return t->t; } +template +T const_func1(const Thing *t, T a0) { return t->t | a0; } +template +T const_func2(const Thing *t, T a0, T a1) { return t->t | a0 | a1; } +template +T const_func3(const Thing *t, T a0, T a1, T a2) { return t->t | a0 | a1 | a2; } +template +T const_func4(const Thing *t, T a0, T a1, T a2, T a3) { return t->t | a0 | a1 | a2 | a3; } +template +T const_func5(const Thing *t, T a0, T a1, T a2, T a3, T a4) { return t->t | a0 | a1 | a2 | a3 | a4; } + +// volatile bound functions +template +T volatile_func0(volatile Thing *t) { return t->t; } +template +T volatile_func1(volatile Thing *t, T a0) { return t->t | a0; } +template +T volatile_func2(volatile Thing *t, T a0, T a1) { return t->t | a0 | a1; } +template +T volatile_func3(volatile Thing *t, T a0, T a1, T a2) { return t->t | a0 | a1 | a2; } +template +T volatile_func4(volatile Thing *t, T a0, T a1, T a2, T a3) { return t->t | a0 | a1 | a2 | a3; } +template +T volatile_func5(volatile Thing *t, T a0, T a1, T a2, T a3, T a4) { return t->t | a0 | a1 | a2 | a3 | a4; } + +// const volatile bound functions +template +T const_volatile_func0(const volatile Thing *t) { return t->t; } +template +T const_volatile_func1(const volatile Thing *t, T a0) { return t->t | a0; } +template +T const_volatile_func2(const volatile Thing *t, T a0, T a1) { return t->t | a0 | a1; } +template +T const_volatile_func3(const volatile Thing *t, T a0, T a1, T a2) { return t->t | a0 | a1 | a2; } +template +T const_volatile_func4(const volatile Thing *t, T a0, T a1, T a2, T a3) { return t->t | a0 | a1 | a2 | a3; } +template +T const_volatile_func5(const volatile Thing *t, T a0, T a1, T a2, T a3, T a4) { return t->t | a0 | a1 | a2 | a3 | a4; } + + +// function call and result verification +template +struct Verifier { + 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); + } + + static void verify1(Callback func) { + T result = func((1 << 0)); + TEST_ASSERT_EQUAL(result, 0x00 | (1 << 0)); + } + + template + static void verify1(O *obj, M method) { + Callback func(obj, method); + T result = func((1 << 0)); + TEST_ASSERT_EQUAL(result, 0x80 | (1 << 0)); + } + + static void verify2(Callback func) { + T result = func((1 << 0), (1 << 1)); + TEST_ASSERT_EQUAL(result, 0x00 | (1 << 0) | (1 << 1)); + } + + template + static void verify2(O *obj, M method) { + Callback func(obj, method); + T result = func((1 << 0), (1 << 1)); + TEST_ASSERT_EQUAL(result, 0x80 | (1 << 0) | (1 << 1)); + } + + static void verify3(Callback func) { + T result = func((1 << 0), (1 << 1), (1 << 2)); + TEST_ASSERT_EQUAL(result, 0x00 | (1 << 0) | (1 << 1) | (1 << 2)); + } + + template + static void verify3(O *obj, M method) { + Callback func(obj, method); + T result = func((1 << 0), (1 << 1), (1 << 2)); + TEST_ASSERT_EQUAL(result, 0x80 | (1 << 0) | (1 << 1) | (1 << 2)); + } + + static void verify4(Callback func) { + T result = func((1 << 0), (1 << 1), (1 << 2), (1 << 3)); + TEST_ASSERT_EQUAL(result, 0x00 | (1 << 0) | (1 << 1) | (1 << 2) | (1 << 3)); + } + + template + static void verify4(O *obj, M method) { + Callback func(obj, method); + T result = func((1 << 0), (1 << 1), (1 << 2), (1 << 3)); + TEST_ASSERT_EQUAL(result, 0x80 | (1 << 0) | (1 << 1) | (1 << 2) | (1 << 3)); + } + + static void verify5(Callback func) { + T result = func((1 << 0), (1 << 1), (1 << 2), (1 << 3), (1 << 4)); + TEST_ASSERT_EQUAL(result, 0x00 | (1 << 0) | (1 << 1) | (1 << 2) | (1 << 3) | (1 << 4)); + } + + template + static void verify5(O *obj, M method) { + Callback func(obj, method); + T result = func((1 << 0), (1 << 1), (1 << 2), (1 << 3), (1 << 4)); + TEST_ASSERT_EQUAL(result, 0x80 | (1 << 0) | (1 << 1) | (1 << 2) | (1 << 3) | (1 << 4)); + } +}; + + +// test dispatch +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(10, "default_auto"); + return verbose_test_setup_handler(number_of_cases); +} + +Case cases[] = { + Case("Testing FunctionPointerArg1 compatibility", test_fparg1), + Case("Testing FunctionPointer compatibility", test_fparg0), +}; + +Specification specification(test_setup, cases); + +int main() { + return !Harness::run(specification); +}