From c37df2efca391294500eca96f6af9e693dceebd7 Mon Sep 17 00:00:00 2001 From: Qinghao Shi Date: Wed, 5 Feb 2020 23:29:53 +0000 Subject: [PATCH 1/2] TEST: Refactor mbedmicro tests to use utest framework --- .../mbedmicro-mbed/call_before_main/main.cpp | 33 ++++++++-- TESTS/mbedmicro-mbed/cpp/main.cpp | 64 ++++++++++++------- TESTS/mbedmicro-mbed/div/main.cpp | 44 ++++++++----- 3 files changed, 100 insertions(+), 41 deletions(-) diff --git a/TESTS/mbedmicro-mbed/call_before_main/main.cpp b/TESTS/mbedmicro-mbed/call_before_main/main.cpp index c185c5959b..81a779d3d4 100644 --- a/TESTS/mbedmicro-mbed/call_before_main/main.cpp +++ b/TESTS/mbedmicro-mbed/call_before_main/main.cpp @@ -13,7 +13,14 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include "mbed.h" #include "greentea-client/test_env.h" +#include "utest/utest.h" +#include "unity/unity.h" + +using utest::v1::Case; + +static const int test_timeout = 5; namespace { bool mbed_main_called = false; @@ -21,13 +28,31 @@ bool mbed_main_called = false; extern "C" void mbed_main() { - printf("MBED: mbed_main() call before main()\r\n"); + utest_printf("MBED: mbed_main() call before main()\r\n"); mbed_main_called = true; } +void test_call_before_main(void) +{ + + utest_printf("MBED: main() starts now!\r\n"); + TEST_ASSERT_MESSAGE(mbed_main_called, "mbed_main didn't called before main"); +} + +// Test cases +Case cases[] = { + Case("Test mbed_main called before main ", test_call_before_main), +}; + +utest::v1::status_t greentea_test_setup(const size_t number_of_cases) +{ + GREENTEA_SETUP(test_timeout, "default_auto"); + return utest::v1::greentea_test_setup_handler(number_of_cases); +} + +utest::v1::Specification specification(greentea_test_setup, cases); + int main() { - GREENTEA_SETUP(5, "default_auto"); - printf("MBED: main() starts now!\r\n"); - GREENTEA_TESTSUITE_RESULT(mbed_main_called); + return !utest::v1::Harness::run(specification); } diff --git a/TESTS/mbedmicro-mbed/cpp/main.cpp b/TESTS/mbedmicro-mbed/cpp/main.cpp index 5597831f26..f4248d5a91 100644 --- a/TESTS/mbedmicro-mbed/cpp/main.cpp +++ b/TESTS/mbedmicro-mbed/cpp/main.cpp @@ -13,7 +13,14 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include "mbed.h" #include "greentea-client/test_env.h" +#include "utest/utest.h" +#include "unity/unity.h" + +using utest::v1::Case; + +static const int test_timeout = 10; #define PATTERN_CHECK_VALUE 0xF0F0ADAD @@ -76,30 +83,43 @@ Heap::init Heap::hello Heap::destroy *******************/ -int main(void) +void test_static(void) { - GREENTEA_SETUP(10, "default_auto"); - bool result = true; - for (;;) { - s.print("init"); - // Global stack object simple test - s.stack_test(); - if (s.check_init() == false) { - result = false; - break; - } - - // Heap test object simple test - Test *m = new Test("Heap"); - m->hello(); - - if (m->check_init() == false) { - result = false; - } - delete m; - break; + s.print("init"); + // Global stack object simple test + s.stack_test(); + if (s.check_init() == false) { + TEST_ASSERT_MESSAGE(false, "Global stack initialization check failed"); } - GREENTEA_TESTSUITE_RESULT(result); +} + +void test_heap(void) +{ + // Heap test object simple test + Test *m = new Test("Heap"); + m->hello(); + if (m->check_init() == false) { + TEST_ASSERT_MESSAGE(false, "Heap object initialization check failed"); + } +} + +// Test cases +Case cases[] = { + Case("Test stack object", test_static), + Case("Test heap object", test_heap), +}; + +utest::v1::status_t greentea_test_setup(const size_t number_of_cases) +{ + GREENTEA_SETUP(test_timeout, "default_auto"); + return utest::v1::greentea_test_setup_handler(number_of_cases); +} + +utest::v1::Specification specification(greentea_test_setup, cases); + +int main() +{ + return !utest::v1::Harness::run(specification); } diff --git a/TESTS/mbedmicro-mbed/div/main.cpp b/TESTS/mbedmicro-mbed/div/main.cpp index 05a018b59a..5431f7a3ed 100644 --- a/TESTS/mbedmicro-mbed/div/main.cpp +++ b/TESTS/mbedmicro-mbed/div/main.cpp @@ -16,6 +16,12 @@ #include // std::pair #include "mbed.h" #include "greentea-client/test_env.h" +#include "utest/utest.h" +#include "unity/unity.h" + +using utest::v1::Case; + +static const int test_timeout = 5; uint32_t test_64(uint64_t ticks) { @@ -28,34 +34,42 @@ uint32_t test_64(uint64_t ticks) return (uint32_t)(0xFFFFFFFF & ticks); } -const char *result_str(bool result) -{ - return result ? "[OK]" : "[FAIL]"; -} -int main() +void test_division(void) { - GREENTEA_SETUP(5, "default_auto"); - - bool result = true; { // 0xFFFFFFFF * 8 = 0x7fffffff8 std::pair values = std::make_pair(0x55555555, 0x7FFFFFFF8); uint32_t test_ret = test_64(values.second); - bool test_res = values.first == test_ret; - result = result && test_res; - printf("64bit: 0x7FFFFFFF8: expected 0x%lX got 0x%lX ... %s\r\n", values.first, test_ret, result_str(test_res)); + utest_printf("64bit: 0x7FFFFFFF8: expected 0x%lX got 0x%lX \r\n", values.first, test_ret); + TEST_ASSERT_EQUAL_UINT32(values.first, test_ret); } { // 0xFFFFFFFF * 24 = 0x17ffffffe8 std::pair values = std::make_pair(0xFFFFFFFF, 0x17FFFFFFE8); uint32_t test_ret = test_64(values.second); - bool test_res = values.first == test_ret; - result = result && test_res; - printf("64bit: 0x17FFFFFFE8: expected 0x%lX got 0x%lX ... %s\r\n", values.first, test_ret, result_str(test_res)); + utest_printf("64bit: 0x17FFFFFFE8: expected 0x%lX got 0x%lX \r\n", values.first, test_ret); + TEST_ASSERT_EQUAL_UINT32(values.first, test_ret); } - GREENTEA_TESTSUITE_RESULT(result); +} + +// Test cases +Case cases[] = { + Case("Test division", test_division), +}; + +utest::v1::status_t greentea_test_setup(const size_t number_of_cases) +{ + GREENTEA_SETUP(test_timeout, "default_auto"); + return utest::v1::greentea_test_setup_handler(number_of_cases); +} + +utest::v1::Specification specification(greentea_test_setup, cases); + +int main() +{ + return !utest::v1::Harness::run(specification); } From 4bd91ce89004c092fc3d395a170312a7ebfb8a8e Mon Sep 17 00:00:00 2001 From: Qinghao Shi Date: Thu, 6 Feb 2020 10:54:29 +0000 Subject: [PATCH 2/2] TEST: update license --- TESTS/mbedmicro-mbed/call_before_main/main.cpp | 4 +++- TESTS/mbedmicro-mbed/cpp/main.cpp | 4 +++- TESTS/mbedmicro-mbed/div/main.cpp | 4 +++- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/TESTS/mbedmicro-mbed/call_before_main/main.cpp b/TESTS/mbedmicro-mbed/call_before_main/main.cpp index 81a779d3d4..65dda45579 100644 --- a/TESTS/mbedmicro-mbed/call_before_main/main.cpp +++ b/TESTS/mbedmicro-mbed/call_before_main/main.cpp @@ -1,5 +1,7 @@ /* mbed Microcontroller Library - * Copyright (c) 2017 ARM Limited + * Copyright (c) 2017-2020 ARM Limited + * + * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/TESTS/mbedmicro-mbed/cpp/main.cpp b/TESTS/mbedmicro-mbed/cpp/main.cpp index f4248d5a91..af956a333a 100644 --- a/TESTS/mbedmicro-mbed/cpp/main.cpp +++ b/TESTS/mbedmicro-mbed/cpp/main.cpp @@ -1,5 +1,7 @@ /* mbed Microcontroller Library - * Copyright (c) 2017 ARM Limited + * Copyright (c) 2017-2020 ARM Limited + * + * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/TESTS/mbedmicro-mbed/div/main.cpp b/TESTS/mbedmicro-mbed/div/main.cpp index 5431f7a3ed..6cab8e5071 100644 --- a/TESTS/mbedmicro-mbed/div/main.cpp +++ b/TESTS/mbedmicro-mbed/div/main.cpp @@ -1,5 +1,7 @@ /* mbed Microcontroller Library - * Copyright (c) 2017 ARM Limited + * Copyright (c) 2017-2020 ARM Limited + * + * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License.