Skip to content

Commit f502b41

Browse files
authored
Merge pull request #12378 from jamesbeyond/test_update
TEST: Refactor mbedmicro tests to use utest framework
2 parents 69a2803 + 4bd91ce commit f502b41

File tree

3 files changed

+108
-43
lines changed

3 files changed

+108
-43
lines changed

TESTS/mbedmicro-mbed/call_before_main/main.cpp

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
/* mbed Microcontroller Library
2-
* Copyright (c) 2017 ARM Limited
2+
* Copyright (c) 2017-2020 ARM Limited
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
35
*
46
* Licensed under the Apache License, Version 2.0 (the "License");
57
* you may not use this file except in compliance with the License.
@@ -13,21 +15,46 @@
1315
* See the License for the specific language governing permissions and
1416
* limitations under the License.
1517
*/
18+
#include "mbed.h"
1619
#include "greentea-client/test_env.h"
20+
#include "utest/utest.h"
21+
#include "unity/unity.h"
22+
23+
using utest::v1::Case;
24+
25+
static const int test_timeout = 5;
1726

1827
namespace {
1928
bool mbed_main_called = false;
2029
}
2130

2231
extern "C" void mbed_main()
2332
{
24-
printf("MBED: mbed_main() call before main()\r\n");
33+
utest_printf("MBED: mbed_main() call before main()\r\n");
2534
mbed_main_called = true;
2635
}
2736

37+
void test_call_before_main(void)
38+
{
39+
40+
utest_printf("MBED: main() starts now!\r\n");
41+
TEST_ASSERT_MESSAGE(mbed_main_called, "mbed_main didn't called before main");
42+
}
43+
44+
// Test cases
45+
Case cases[] = {
46+
Case("Test mbed_main called before main ", test_call_before_main),
47+
};
48+
49+
utest::v1::status_t greentea_test_setup(const size_t number_of_cases)
50+
{
51+
GREENTEA_SETUP(test_timeout, "default_auto");
52+
return utest::v1::greentea_test_setup_handler(number_of_cases);
53+
}
54+
55+
utest::v1::Specification specification(greentea_test_setup, cases);
56+
2857
int main()
2958
{
30-
GREENTEA_SETUP(5, "default_auto");
31-
printf("MBED: main() starts now!\r\n");
32-
GREENTEA_TESTSUITE_RESULT(mbed_main_called);
59+
return !utest::v1::Harness::run(specification);
3360
}

TESTS/mbedmicro-mbed/cpp/main.cpp

Lines changed: 44 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
/* mbed Microcontroller Library
2-
* Copyright (c) 2017 ARM Limited
2+
* Copyright (c) 2017-2020 ARM Limited
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
35
*
46
* Licensed under the Apache License, Version 2.0 (the "License");
57
* you may not use this file except in compliance with the License.
@@ -13,7 +15,14 @@
1315
* See the License for the specific language governing permissions and
1416
* limitations under the License.
1517
*/
18+
#include "mbed.h"
1619
#include "greentea-client/test_env.h"
20+
#include "utest/utest.h"
21+
#include "unity/unity.h"
22+
23+
using utest::v1::Case;
24+
25+
static const int test_timeout = 10;
1726

1827
#define PATTERN_CHECK_VALUE 0xF0F0ADAD
1928

@@ -76,30 +85,43 @@ Heap::init
7685
Heap::hello
7786
Heap::destroy
7887
*******************/
79-
int main(void)
88+
void test_static(void)
8089
{
81-
GREENTEA_SETUP(10, "default_auto");
82-
83-
bool result = true;
84-
for (;;) {
85-
s.print("init");
86-
// Global stack object simple test
87-
s.stack_test();
88-
if (s.check_init() == false) {
89-
result = false;
90-
break;
91-
}
9290

93-
// Heap test object simple test
94-
Test *m = new Test("Heap");
95-
m->hello();
91+
s.print("init");
92+
// Global stack object simple test
93+
s.stack_test();
94+
if (s.check_init() == false) {
95+
TEST_ASSERT_MESSAGE(false, "Global stack initialization check failed");
96+
}
9697

97-
if (m->check_init() == false) {
98-
result = false;
99-
}
100-
delete m;
101-
break;
98+
}
99+
100+
void test_heap(void)
101+
{
102+
// Heap test object simple test
103+
Test *m = new Test("Heap");
104+
m->hello();
105+
if (m->check_init() == false) {
106+
TEST_ASSERT_MESSAGE(false, "Heap object initialization check failed");
102107
}
108+
}
109+
110+
// Test cases
111+
Case cases[] = {
112+
Case("Test stack object", test_static),
113+
Case("Test heap object", test_heap),
114+
};
115+
116+
utest::v1::status_t greentea_test_setup(const size_t number_of_cases)
117+
{
118+
GREENTEA_SETUP(test_timeout, "default_auto");
119+
return utest::v1::greentea_test_setup_handler(number_of_cases);
120+
}
103121

104-
GREENTEA_TESTSUITE_RESULT(result);
122+
utest::v1::Specification specification(greentea_test_setup, cases);
123+
124+
int main()
125+
{
126+
return !utest::v1::Harness::run(specification);
105127
}

TESTS/mbedmicro-mbed/div/main.cpp

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
/* mbed Microcontroller Library
2-
* Copyright (c) 2017 ARM Limited
2+
* Copyright (c) 2017-2020 ARM Limited
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
35
*
46
* Licensed under the Apache License, Version 2.0 (the "License");
57
* you may not use this file except in compliance with the License.
@@ -16,6 +18,12 @@
1618
#include <utility> // std::pair
1719
#include "mbed.h"
1820
#include "greentea-client/test_env.h"
21+
#include "utest/utest.h"
22+
#include "unity/unity.h"
23+
24+
using utest::v1::Case;
25+
26+
static const int test_timeout = 5;
1927

2028
uint32_t test_64(uint64_t ticks)
2129
{
@@ -28,34 +36,42 @@ uint32_t test_64(uint64_t ticks)
2836
return (uint32_t)(0xFFFFFFFF & ticks);
2937
}
3038

31-
const char *result_str(bool result)
32-
{
33-
return result ? "[OK]" : "[FAIL]";
34-
}
3539

36-
int main()
40+
void test_division(void)
3741
{
38-
GREENTEA_SETUP(5, "default_auto");
39-
40-
bool result = true;
4142

4243
{
4344
// 0xFFFFFFFF * 8 = 0x7fffffff8
4445
std::pair<uint32_t, uint64_t> values = std::make_pair(0x55555555, 0x7FFFFFFF8);
4546
uint32_t test_ret = test_64(values.second);
46-
bool test_res = values.first == test_ret;
47-
result = result && test_res;
48-
printf("64bit: 0x7FFFFFFF8: expected 0x%lX got 0x%lX ... %s\r\n", values.first, test_ret, result_str(test_res));
47+
utest_printf("64bit: 0x7FFFFFFF8: expected 0x%lX got 0x%lX \r\n", values.first, test_ret);
48+
TEST_ASSERT_EQUAL_UINT32(values.first, test_ret);
4949
}
5050

5151
{
5252
// 0xFFFFFFFF * 24 = 0x17ffffffe8
5353
std::pair<uint32_t, uint64_t> values = std::make_pair(0xFFFFFFFF, 0x17FFFFFFE8);
5454
uint32_t test_ret = test_64(values.second);
55-
bool test_res = values.first == test_ret;
56-
result = result && test_res;
57-
printf("64bit: 0x17FFFFFFE8: expected 0x%lX got 0x%lX ... %s\r\n", values.first, test_ret, result_str(test_res));
55+
utest_printf("64bit: 0x17FFFFFFE8: expected 0x%lX got 0x%lX \r\n", values.first, test_ret);
56+
TEST_ASSERT_EQUAL_UINT32(values.first, test_ret);
5857
}
5958

60-
GREENTEA_TESTSUITE_RESULT(result);
59+
}
60+
61+
// Test cases
62+
Case cases[] = {
63+
Case("Test division", test_division),
64+
};
65+
66+
utest::v1::status_t greentea_test_setup(const size_t number_of_cases)
67+
{
68+
GREENTEA_SETUP(test_timeout, "default_auto");
69+
return utest::v1::greentea_test_setup_handler(number_of_cases);
70+
}
71+
72+
utest::v1::Specification specification(greentea_test_setup, cases);
73+
74+
int main()
75+
{
76+
return !utest::v1::Harness::run(specification);
6177
}

0 commit comments

Comments
 (0)