/* mbed Microcontroller Library * Copyright (c) 2017 ARM Limited * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #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(&bound_func0, &thing); Verifier::verify0(&const_func0, (const Thing*)&thing); Verifier::verify0(&volatile_func0, (volatile Thing*)&thing); Verifier::verify0(&const_volatile_func0, (const volatile Thing*)&thing); Verifier::verify0(callback(static_func0)); Callback cb(static_func0); Verifier::verify0(cb); cb = static_func0; Verifier::verify0(cb); cb.attach(&bound_func0, &thing); Verifier::verify0(&cb, &Callback::call); Verifier::verify0(&Callback::thunk, (void*)&cb); } 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(&bound_func1, &thing); Verifier::verify1(&const_func1, (const Thing*)&thing); Verifier::verify1(&volatile_func1, (volatile Thing*)&thing); Verifier::verify1(&const_volatile_func1, (const volatile Thing*)&thing); Verifier::verify1(callback(static_func1)); Callback cb(static_func1); Verifier::verify1(cb); cb = static_func1; Verifier::verify1(cb); cb.attach(&bound_func1, &thing); Verifier::verify1(&cb, &Callback::call); Verifier::verify1(&Callback::thunk, (void*)&cb); } 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(&bound_func2, &thing); Verifier::verify2(&const_func2, (const Thing*)&thing); Verifier::verify2(&volatile_func2, (volatile Thing*)&thing); Verifier::verify2(&const_volatile_func2, (const volatile Thing*)&thing); Verifier::verify2(callback(static_func2)); Callback cb(static_func2); Verifier::verify2(cb); cb = static_func2; Verifier::verify2(cb); cb.attach(&bound_func2, &thing); Verifier::verify2(&cb, &Callback::call); Verifier::verify2(&Callback::thunk, (void*)&cb); } 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(&bound_func3, &thing); Verifier::verify3(&const_func3, (const Thing*)&thing); Verifier::verify3(&volatile_func3, (volatile Thing*)&thing); Verifier::verify3(&const_volatile_func3, (const volatile Thing*)&thing); Verifier::verify3(callback(static_func3)); Callback cb(static_func3); Verifier::verify3(cb); cb = static_func3; Verifier::verify3(cb); cb.attach(&bound_func3, &thing); Verifier::verify3(&cb, &Callback::call); Verifier::verify3(&Callback::thunk, (void*)&cb); } 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(&bound_func4, &thing); Verifier::verify4(&const_func4, (const Thing*)&thing); Verifier::verify4(&volatile_func4, (volatile Thing*)&thing); Verifier::verify4(&const_volatile_func4, (const volatile Thing*)&thing); Verifier::verify4(callback(static_func4)); Callback cb(static_func4); Verifier::verify4(cb); cb = static_func4; Verifier::verify4(cb); cb.attach(&bound_func4, &thing); Verifier::verify4(&cb, &Callback::call); Verifier::verify4(&Callback::thunk, (void*)&cb); } 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(&bound_func5, &thing); Verifier::verify5(&const_func5, (const Thing*)&thing); Verifier::verify5(&volatile_func5, (volatile Thing*)&thing); Verifier::verify5(&const_volatile_func5, (const volatile Thing*)&thing); Verifier::verify5(callback(static_func5)); Callback cb(static_func5); Verifier::verify5(cb); cb = static_func5; Verifier::verify5(cb); cb.attach(&bound_func5, &thing); Verifier::verify5(&cb, &Callback::call); Verifier::verify5(&Callback::thunk, (void*)&cb); } // 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); }