From 3b3745e00833929c1d5a943a3d4e8caf532b90d0 Mon Sep 17 00:00:00 2001 From: Przemek Wirkus Date: Thu, 2 Jun 2016 17:11:27 +0100 Subject: [PATCH] Add few non-peripheral tests from mbedmicro/mbed/libraries/tests/mbed Tested on K64F: ``` $ mbedgt -VS -n TESTS-mbedmicro-mbed* ``` ``` mbedgt: test suite report: +--------------+---------------+---------------------------------------+--------+--------------------+-------------+ | target | platform_name | test suite | result | elapsed_time (sec) | copy_method | +--------------+---------------+---------------------------------------+--------+--------------------+-------------+ | K64F-GCC_ARM | K64F | tests-mbedmicro-mbed-call_before_main | OK | 10.53 | shell | | K64F-GCC_ARM | K64F | tests-mbedmicro-mbed-cpp | OK | 10.64 | shell | | K64F-GCC_ARM | K64F | tests-mbedmicro-mbed-div | OK | 10.64 | shell | | K64F-GCC_ARM | K64F | tests-mbedmicro-mbed-heap_and_stack | OK | 30.32 | shell | +--------------+---------------+---------------------------------------+--------+--------------------+-------------+ mbedgt: test suite results: 4 OK mbedgt: test case report: +--------------+---------------+---------------------------------------+---------------------------------------+--------+--------+--------+--------------------+ | target | platform_name | test suite | test case | passed | failed | result | elapsed_time (sec) | +--------------+---------------+---------------------------------------+---------------------------------------+--------+--------+--------+--------------------+ | K64F-GCC_ARM | K64F | tests-mbedmicro-mbed-call_before_main | tests-mbedmicro-mbed-call_before_main | 1 | 0 | OK | 10.53 | | K64F-GCC_ARM | K64F | tests-mbedmicro-mbed-cpp | tests-mbedmicro-mbed-cpp | 1 | 0 | OK | 10.64 | | K64F-GCC_ARM | K64F | tests-mbedmicro-mbed-div | tests-mbedmicro-mbed-div | 1 | 0 | OK | 10.64 | | K64F-GCC_ARM | K64F | tests-mbedmicro-mbed-heap_and_stack | tests-mbedmicro-mbed-heap_and_stack | 1 | 0 | OK | 30.32 | +--------------+---------------+---------------------------------------+---------------------------------------+--------+--------+--------+--------------------+ mbedgt: test case results: 4 OK mbedgt: completed in 62.25 sec ``` --- .../mbedmicro-mbed/call_before_main/main.cpp | 16 ++++ TESTS/mbedmicro-mbed/cpp/main.cpp | 83 +++++++++++++++++++ TESTS/mbedmicro-mbed/div/main.cpp | 41 +++++++++ TESTS/mbedmicro-mbed/heap_and_stack/main.cpp | 80 ++++++++++++++++++ 4 files changed, 220 insertions(+) create mode 100644 TESTS/mbedmicro-mbed/call_before_main/main.cpp create mode 100644 TESTS/mbedmicro-mbed/cpp/main.cpp create mode 100644 TESTS/mbedmicro-mbed/div/main.cpp create mode 100644 TESTS/mbedmicro-mbed/heap_and_stack/main.cpp diff --git a/TESTS/mbedmicro-mbed/call_before_main/main.cpp b/TESTS/mbedmicro-mbed/call_before_main/main.cpp new file mode 100644 index 0000000000..9a713d3f30 --- /dev/null +++ b/TESTS/mbedmicro-mbed/call_before_main/main.cpp @@ -0,0 +1,16 @@ +#include "greentea-client/test_env.h" + +namespace { + bool mbed_main_called = false; +} + +extern "C" void mbed_main() { + printf("MBED: mbed_main() call before main()\r\n"); + mbed_main_called = true; +} + +int main() { + GREENTEA_SETUP(5, "default_auto"); + printf("MBED: main() starts now!\r\n"); + GREENTEA_TESTSUITE_RESULT(mbed_main_called); +} diff --git a/TESTS/mbedmicro-mbed/cpp/main.cpp b/TESTS/mbedmicro-mbed/cpp/main.cpp new file mode 100644 index 0000000000..bfa02200ae --- /dev/null +++ b/TESTS/mbedmicro-mbed/cpp/main.cpp @@ -0,0 +1,83 @@ +#include "greentea-client/test_env.h" + +#define PATTERN_CHECK_VALUE 0xF0F0ADAD + +class Test { + +private: + const char* name; + const int pattern; + +public: + Test(const char* _name) : name(_name), pattern(PATTERN_CHECK_VALUE) { + print("init"); + } + + void print(const char *message) { + printf("%s::%s\n", name, message); + } + + bool check_init(void) { + bool result = (pattern == PATTERN_CHECK_VALUE); + print(result ? "check_init: OK" : "check_init: ERROR"); + return result; + } + + void stack_test(void) { + print("stack_test"); + Test t("Stack"); + t.hello(); + } + + void hello(void) { + print("hello"); + } + + ~Test() { + print("destroy"); + } +}; + +/* Check C++ startup initialisation */ +Test s("Static"); + +/* EXPECTED OUTPUT: +******************* +Static::init +Static::stack_test +Stack::init +Stack::hello +Stack::destroy +Static::check_init: OK +Heap::init +Heap::hello +Heap::destroy +*******************/ +int main (void) { + GREENTEA_SETUP(10, "default_auto"); + + bool result = true; + for (;;) + { + // 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; + } + + GREENTEA_TESTSUITE_RESULT(result); +} diff --git a/TESTS/mbedmicro-mbed/div/main.cpp b/TESTS/mbedmicro-mbed/div/main.cpp new file mode 100644 index 0000000000..6658d6db15 --- /dev/null +++ b/TESTS/mbedmicro-mbed/div/main.cpp @@ -0,0 +1,41 @@ +#include // std::pair +#include "mbed.h" +#include "greentea-client/test_env.h" + +uint32_t test_64(uint64_t ticks) { + ticks >>= 3; // divide by 8 + if (ticks > 0xFFFFFFFF) { + ticks /= 3; + } else { + ticks = (ticks * 0x55555556) >> 32; // divide by 3 + } + return (uint32_t)(0xFFFFFFFF & ticks); +} + +const char *result_str(bool result) { + return result ? "[OK]" : "[FAIL]"; +} + +int main() { + 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)); + } + + { // 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)); + } + + GREENTEA_TESTSUITE_RESULT(result); +} diff --git a/TESTS/mbedmicro-mbed/heap_and_stack/main.cpp b/TESTS/mbedmicro-mbed/heap_and_stack/main.cpp new file mode 100644 index 0000000000..b27f7f1f90 --- /dev/null +++ b/TESTS/mbedmicro-mbed/heap_and_stack/main.cpp @@ -0,0 +1,80 @@ +#include +#include +#include "greentea-client/test_env.h" + +static char *initial_stack_p; +static char *initial_heap_p; + +static char line[256]; +static unsigned int iterations = 0; + +void report_iterations(void) { + unsigned int tot = (0x100 * iterations)*2; + printf("\nAllocated (%d)Kb in (%u) iterations\n", tot/1024, iterations); +#if !defined(TOOLCHAIN_GCC_CR) + // EA: This causes a crash when compiling with GCC_CR??? + printf("%.2f\n", ((float)(tot)/(float)(initial_stack_p - initial_heap_p))*100.); +#endif +#ifdef TOOLCHAIN_ARM +#ifndef __MICROLIB + __heapvalid((__heapprt) fprintf, stdout, 1); +#endif +#endif +} + +void stack_test(char *latest_heap_pointer) { + char stack_line[256]; + iterations++; + + sprintf(stack_line, "\nstack pointer: %p", &stack_line[255]); + puts(stack_line); + + char *heap_pointer = (char*)malloc(0x100); + + if (heap_pointer == NULL) { + int diff = (&stack_line[255] - latest_heap_pointer); + if (diff > 0x200) { + sprintf(stack_line, "\n[WARNING] Malloc failed to allocate memory too soon. There are (0x%x) free bytes", diff); + report_iterations(); + puts(stack_line); + } else { + puts("\n[SUCCESS] Stack/Heap collision detected"); + report_iterations(); + } + return; + } else { + heap_pointer += 0x100; + sprintf(line, "heap pointer: %p", heap_pointer); + puts(line); + } + + if ((&stack_line[255]) > heap_pointer) { + stack_test(heap_pointer); + } else { + puts("\n[WARNING] The Stack/Heap collision was not detected"); + report_iterations(); + } +} + + +int main (void) { + GREENTEA_SETUP(30, "default_auto"); + + char c; + initial_stack_p = &c; + + initial_heap_p = (char*)malloc(1); + if (initial_heap_p == NULL) { + printf("Unable to malloc a single byte\n"); + GREENTEA_TESTSUITE_RESULT(false); + } + + printf("Initial stack/heap geometry:\n"); + printf(" stack pointer:V %p\n", initial_stack_p); + printf(" heap pointer :^ %p\n", initial_heap_p); + + initial_heap_p++; + stack_test(initial_heap_p); + + GREENTEA_TESTSUITE_RESULT(true); +}