Skip to content

Commit c37df2e

Browse files
Qinghao ShiQinghao Shi
authored andcommitted
TEST: Refactor mbedmicro tests to use utest framework
1 parent 0b7c78b commit c37df2e

File tree

3 files changed

+99
-40
lines changed

3 files changed

+99
-40
lines changed

TESTS/mbedmicro-mbed/call_before_main/main.cpp

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,46 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
#include "mbed.h"
1617
#include "greentea-client/test_env.h"
18+
#include "utest/utest.h"
19+
#include "unity/unity.h"
20+
21+
using utest::v1::Case;
22+
23+
static const int test_timeout = 5;
1724

1825
namespace {
1926
bool mbed_main_called = false;
2027
}
2128

2229
extern "C" void mbed_main()
2330
{
24-
printf("MBED: mbed_main() call before main()\r\n");
31+
utest_printf("MBED: mbed_main() call before main()\r\n");
2532
mbed_main_called = true;
2633
}
2734

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

TESTS/mbedmicro-mbed/cpp/main.cpp

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,14 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
#include "mbed.h"
1617
#include "greentea-client/test_env.h"
18+
#include "utest/utest.h"
19+
#include "unity/unity.h"
20+
21+
using utest::v1::Case;
22+
23+
static const int test_timeout = 10;
1724

1825
#define PATTERN_CHECK_VALUE 0xF0F0ADAD
1926

@@ -76,30 +83,43 @@ Heap::init
7683
Heap::hello
7784
Heap::destroy
7885
*******************/
79-
int main(void)
86+
void test_static(void)
8087
{
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-
}
9288

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

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

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

TESTS/mbedmicro-mbed/div/main.cpp

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@
1616
#include <utility> // std::pair
1717
#include "mbed.h"
1818
#include "greentea-client/test_env.h"
19+
#include "utest/utest.h"
20+
#include "unity/unity.h"
21+
22+
using utest::v1::Case;
23+
24+
static const int test_timeout = 5;
1925

2026
uint32_t test_64(uint64_t ticks)
2127
{
@@ -28,34 +34,42 @@ uint32_t test_64(uint64_t ticks)
2834
return (uint32_t)(0xFFFFFFFF & ticks);
2935
}
3036

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

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

4241
{
4342
// 0xFFFFFFFF * 8 = 0x7fffffff8
4443
std::pair<uint32_t, uint64_t> values = std::make_pair(0x55555555, 0x7FFFFFFF8);
4544
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));
45+
utest_printf("64bit: 0x7FFFFFFF8: expected 0x%lX got 0x%lX \r\n", values.first, test_ret);
46+
TEST_ASSERT_EQUAL_UINT32(values.first, test_ret);
4947
}
5048

5149
{
5250
// 0xFFFFFFFF * 24 = 0x17ffffffe8
5351
std::pair<uint32_t, uint64_t> values = std::make_pair(0xFFFFFFFF, 0x17FFFFFFE8);
5452
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));
53+
utest_printf("64bit: 0x17FFFFFFE8: expected 0x%lX got 0x%lX \r\n", values.first, test_ret);
54+
TEST_ASSERT_EQUAL_UINT32(values.first, test_ret);
5855
}
5956

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

0 commit comments

Comments
 (0)