Skip to content

Commit 20a80cd

Browse files
committed
Merge pull request #109 from adbridge/utest_tests
Port utest unit tests from Yotta / Minar based environment to Morpheus /
2 parents 59f5751 + 15e372f commit 20a80cd

File tree

21 files changed

+1960
-43
lines changed

21 files changed

+1960
-43
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2013-2015 ARM Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* This file describes how to use some of the basic utest features to write your
17+
* unit test.
18+
*
19+
*/
20+
#include "mbed.h"
21+
#include "greentea-client/test_env.h"
22+
#include "utest.h"
23+
#include "unity.h"
24+
25+
using namespace utest::v1;
26+
27+
static Timeout utest_to;
28+
29+
void test_simple() {
30+
TEST_ASSERT_EQUAL(0, 0);
31+
printf("Simple test called\n");
32+
}
33+
34+
status_t test_repeats_setup(const Case *const source, const size_t index_of_case) {
35+
// Call the default handler for proper reporting
36+
status_t status = greentea_case_setup_handler(source, index_of_case);
37+
printf("Setting up for '%s'\n", source->get_description());
38+
return status;
39+
}
40+
control_t test_repeats(const size_t call_count) {
41+
printf("Called for the %u. time\n", call_count);
42+
TEST_ASSERT_NOT_EQUAL(3, call_count);
43+
// Specify how often this test is repeated ie. n total calls
44+
return (call_count < 2) ? CaseRepeatAll : CaseNext;
45+
}
46+
47+
void test_callback_validate() {
48+
// You may also use assertions here!
49+
TEST_ASSERT_EQUAL_PTR(0, 0);
50+
// Validate the callback
51+
Harness::validate_callback();
52+
}
53+
control_t test_asynchronous() {
54+
TEST_ASSERT_TRUE_MESSAGE(true, "(true == false) o_O");
55+
// Set up a callback in the future. This may also be an interrupt!
56+
utest_to.attach_us(test_callback_validate, (100*1000)); // Fire after 100 ms
57+
58+
// Set a 200ms timeout starting from now
59+
return CaseTimeout(200);
60+
}
61+
62+
control_t test_asynchronous_timeout(const size_t call_count) {
63+
TEST_ASSERT_TRUE_MESSAGE(true, "(true == false) o_O");
64+
// Set a 200ms timeout starting from now,
65+
// but automatically repeat only this handler on timeout.
66+
if (call_count >= 5) {
67+
// but after the 5th call, the callback finally gets validated
68+
utest_to.attach_us(test_callback_validate, (100*1000)); // Fire after 100 ms
69+
}
70+
return CaseRepeatHandlerOnTimeout(200);
71+
}
72+
73+
// Custom setup handler required for proper Greentea support
74+
status_t greentea_setup(const size_t number_of_cases) {
75+
GREENTEA_SETUP(20, "default_auto");
76+
77+
// Call the default reporting function
78+
return greentea_test_setup_handler(number_of_cases);
79+
}
80+
81+
// Specify all your test cases here
82+
Case cases[] = {
83+
Case("Simple Test", test_simple),
84+
Case("Repeating Test", test_repeats_setup, test_repeats),
85+
Case("Asynchronous Test (200ms timeout)", test_asynchronous),
86+
Case("Asynchronous Timeout Repeat", test_asynchronous_timeout)
87+
};
88+
89+
// Declare your test specification with a custom setup handler
90+
Specification specification(greentea_setup, cases);
91+
92+
int main()
93+
{
94+
// Run the specification only AFTER setting the custom scheduler.
95+
Harness::run(specification);
96+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#include "mbed-drivers/mbed.h"
2+
#include "greentea-client/test_env.h"
3+
#include "utest/utest.h"
4+
#include "unity/unity.h"
5+
#include "stack_trace.h"
6+
7+
using namespace utest::v1;
8+
9+
void test_simple() {
10+
UTEST_LOG_FUNCTION();
11+
TEST_ASSERT_EQUAL(0, 0);
12+
printf("Simple test called\n");
13+
}
14+
15+
status_t test_repeats_setup(const Case *const source, const size_t index_of_case) {
16+
UTEST_LOG_FUNCTION();
17+
// Call the default handler for proper reporting
18+
status_t status = greentea_case_setup_handler(source, index_of_case);
19+
printf("Setting up for '%s'\n", source->get_description());
20+
return status;
21+
}
22+
control_t test_repeats(const size_t call_count) {
23+
UTEST_LOG_FUNCTION();
24+
printf("Called for the %u. time\n", call_count);
25+
TEST_ASSERT_NOT_EQUAL(3, call_count);
26+
// Specify how often this test is repeated ie. n total calls
27+
return (call_count < 2) ? CaseRepeatAll : CaseNext;
28+
}
29+
30+
// Custom setup handler required for proper Greentea support
31+
status_t greentea_setup(const size_t number_of_cases) {
32+
UTEST_LOG_FUNCTION();
33+
GREENTEA_SETUP(20, "default_auto");
34+
// Call the default reporting function
35+
return greentea_test_setup_handler(number_of_cases);
36+
}
37+
38+
// Specify all your test cases here
39+
Case cases[] = {
40+
Case("Simple Test", test_simple),
41+
Case("Repeating Test", test_repeats_setup, test_repeats)
42+
};
43+
44+
// Declare your test specification with a custom setup handler
45+
Specification specification(greentea_setup, cases);
46+
47+
extern void utest_run(const Specification& specification);
48+
49+
int main()
50+
{
51+
UTEST_LOG_FUNCTION();
52+
// Run the specification only AFTER setting the custom scheduler.
53+
Harness::run(specification);
54+
}
Lines changed: 233 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,233 @@
1+
2+
/* mbed Microcontroller Library
3+
* Copyright (c) 2013-2015 ARM Limited
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
#include "mbed-drivers/mbed.h"
18+
#include "greentea-client/test_env.h"
19+
#include "utest/utest.h"
20+
#include "unity/unity.h"
21+
22+
#include <stdio.h>
23+
24+
using namespace utest::v1;
25+
26+
static int call_counter(0);
27+
28+
static Timeout utest_to;
29+
30+
// Validate: Simple Validation ----------------------------------------------------------------------------------------
31+
void simple_validation()
32+
{
33+
TEST_ASSERT_EQUAL(1, call_counter++);
34+
printf("Simple validation callback executed.\n");
35+
Harness::validate_callback();
36+
}
37+
38+
control_t simple_validation_case()
39+
{
40+
printf("Simple validation, posting callback\n");
41+
TEST_ASSERT_EQUAL(0, call_counter++);
42+
utest_to.attach_us(simple_validation, 100); // Fire after 100 us
43+
44+
return CaseAwait;
45+
}
46+
47+
// Validate: Multiple Validation --------------------------------------------------------------------------------------
48+
void multiple_validation()
49+
{
50+
printf("Multiple validation callback executed.\n");
51+
52+
// make sure validation is side-effect free
53+
TEST_ASSERT_EQUAL(3, call_counter++);
54+
Harness::validate_callback();
55+
TEST_ASSERT_EQUAL(4, call_counter++);
56+
Harness::validate_callback();
57+
TEST_ASSERT_EQUAL(5, call_counter++);
58+
Harness::validate_callback();
59+
TEST_ASSERT_EQUAL(6, call_counter++);
60+
Harness::validate_callback();
61+
TEST_ASSERT_EQUAL(7, call_counter++);
62+
Harness::validate_callback();
63+
TEST_ASSERT_EQUAL(8, call_counter++);
64+
65+
}
66+
67+
control_t multiple_validation_case()
68+
{
69+
TEST_ASSERT_EQUAL(2, call_counter++);
70+
printf("Multiple validation callback posted.\n");
71+
utest_to.attach_us(multiple_validation, 100000); // Fire after 100 ms
72+
return CaseAwait;
73+
}
74+
75+
status_t multiple_validation_case_teardown(const Case *const source, const size_t passed, const size_t failed, const failure_t failure)
76+
{
77+
TEST_ASSERT_EQUAL(1, passed);
78+
TEST_ASSERT_EQUAL(0, failed);
79+
TEST_ASSERT_EQUAL(REASON_NONE, failure.reason);
80+
TEST_ASSERT_EQUAL(LOCATION_NONE, failure.location);
81+
TEST_ASSERT_EQUAL(9, call_counter++);
82+
return greentea_case_teardown_handler(source, passed, failed, failure);
83+
}
84+
85+
// Validate: Premature Validation -------------------------------------------------------------------------------------
86+
control_t premature_validation_case()
87+
{
88+
TEST_ASSERT_EQUAL(10, call_counter++);
89+
/* Prematurely validate the callback.
90+
* This can happen, when you set up a callback that occurs in an interrupt
91+
* and it fires and validates the callback before this function completes.
92+
* The harness only knows whether to expect a callback after the case Handler
93+
* completes (obviously), so the callback validations are logged.
94+
*/
95+
Harness::validate_callback();
96+
return CaseAwait;
97+
}
98+
99+
status_t premature_validation_case_teardown(const Case *const source, const size_t passed, const size_t failed, const failure_t failure)
100+
{
101+
TEST_ASSERT_EQUAL(1, passed);
102+
TEST_ASSERT_EQUAL(0, failed);
103+
TEST_ASSERT_EQUAL(REASON_NONE, failure.reason);
104+
TEST_ASSERT_EQUAL(LOCATION_NONE, failure.location);
105+
TEST_ASSERT_EQUAL(11, call_counter++);
106+
return greentea_case_teardown_handler(source, passed, failed, failure);
107+
}
108+
109+
// Validate: Multiple Premature Validation ----------------------------------------------------------------------------
110+
control_t multiple_premature_validation_case()
111+
{
112+
TEST_ASSERT_EQUAL(12, call_counter++);
113+
Harness::validate_callback();
114+
TEST_ASSERT_EQUAL(13, call_counter++);
115+
Harness::validate_callback();
116+
TEST_ASSERT_EQUAL(14, call_counter++);
117+
Harness::validate_callback();
118+
TEST_ASSERT_EQUAL(15, call_counter++);
119+
Harness::validate_callback();
120+
TEST_ASSERT_EQUAL(16, call_counter++);
121+
return CaseAwait;
122+
}
123+
124+
status_t multiple_premature_validation_case_teardown(const Case *const source, const size_t passed, const size_t failed, const failure_t failure)
125+
{
126+
TEST_ASSERT_EQUAL(1, passed);
127+
TEST_ASSERT_EQUAL(0, failed);
128+
TEST_ASSERT_EQUAL(REASON_NONE, failure.reason);
129+
TEST_ASSERT_EQUAL(LOCATION_NONE, failure.location);
130+
TEST_ASSERT_EQUAL(17, call_counter++);
131+
return greentea_case_teardown_handler(source, passed, failed, failure);
132+
}
133+
134+
// Validate: Attributed Validation: Cancel Repeat ---------------------------------------------------------------------
135+
void attributed_validation_cancel_repeat()
136+
{
137+
TEST_ASSERT_EQUAL(19, call_counter++);
138+
printf("Validation cancel repeat callback executed.\n");
139+
// cancel all repeats
140+
Harness::validate_callback(CaseNoRepeat);
141+
}
142+
143+
control_t attributed_validation_cancel_repeat_case()
144+
{
145+
TEST_ASSERT_EQUAL(18, call_counter++);
146+
printf("Validation cancel repeat callback posted.\n");
147+
148+
utest_to.attach_us(attributed_validation_cancel_repeat, 100000); // Fire after 100 ms
149+
// the RepeatAll will be cancelled during callback validation
150+
return CaseRepeatAll + CaseAwait;
151+
}
152+
153+
status_t attributed_validation_cancel_repeat_case_teardown(const Case *const source, const size_t passed, const size_t failed, const failure_t failure)
154+
{
155+
TEST_ASSERT_EQUAL(1, passed);
156+
TEST_ASSERT_EQUAL(0, failed);
157+
TEST_ASSERT_EQUAL(REASON_NONE, failure.reason);
158+
TEST_ASSERT_EQUAL(LOCATION_NONE, failure.location);
159+
TEST_ASSERT_EQUAL(20, call_counter++);
160+
return greentea_case_teardown_handler(source, passed, failed, failure);
161+
}
162+
163+
// Validate: Attributed Validation: Enable Repeat Handler -------------------------------------------------------------
164+
void attributed_validation_enable_repeat()
165+
{
166+
printf("Validation enable repeat callback executed.\n");
167+
TEST_ASSERT_EQUAL(22, call_counter++);
168+
// cancel all repeats
169+
Harness::validate_callback(CaseRepeatHandler);
170+
TEST_ASSERT_EQUAL(23, call_counter++);
171+
// only the first validation counts
172+
Harness::validate_callback(CaseNoRepeat);
173+
TEST_ASSERT_EQUAL(24, call_counter++);
174+
}
175+
176+
control_t attributed_validation_enable_repeat_case(const size_t call_count)
177+
{
178+
if (call_count == 1) {
179+
TEST_ASSERT_EQUAL(21, call_counter++);
180+
printf("Validation enable repeat callback posted.\n");
181+
utest_to.attach_us(attributed_validation_enable_repeat, 100000); // Fire after 100 ms
182+
// the RepeatAll will be cancelled during callback validation
183+
return CaseAwait;
184+
}
185+
TEST_ASSERT_EQUAL(25, call_counter++);
186+
return CaseNext;
187+
}
188+
189+
status_t attributed_validation_enable_repeat_case_teardown(const Case *const source, const size_t passed, const size_t failed, const failure_t failure)
190+
{
191+
TEST_ASSERT_EQUAL(2, passed);
192+
TEST_ASSERT_EQUAL(0, failed);
193+
TEST_ASSERT_EQUAL(REASON_NONE, failure.reason);
194+
TEST_ASSERT_EQUAL(LOCATION_NONE, failure.location);
195+
TEST_ASSERT_EQUAL(26, call_counter++);
196+
return greentea_case_teardown_handler(source, passed, failed, failure);
197+
}
198+
199+
// Cases --------------------------------------------------------------------------------------------------------------
200+
Case cases[] = {
201+
Case("Validate: Simple Validation", simple_validation_case),
202+
Case("Validate: Multiple Validation", multiple_validation_case, multiple_validation_case_teardown),
203+
Case("Validate: Premature Validation", premature_validation_case, premature_validation_case_teardown),
204+
Case("Validate: Multiple Premature Validation", multiple_premature_validation_case, multiple_premature_validation_case_teardown),
205+
Case("Validate: Attributed Validation: Cancel Repeat", attributed_validation_cancel_repeat_case, attributed_validation_cancel_repeat_case_teardown),
206+
Case("Validate: Attributed Validation: Enable Repeat Handler", attributed_validation_enable_repeat_case, attributed_validation_enable_repeat_case_teardown)
207+
};
208+
209+
status_t greentea_setup(const size_t number_of_cases)
210+
{
211+
GREENTEA_SETUP(15, "default_auto");
212+
213+
return greentea_test_setup_handler(number_of_cases);
214+
}
215+
216+
void greentea_teardown(const size_t passed, const size_t failed, const failure_t failure)
217+
{
218+
TEST_ASSERT_EQUAL(27, call_counter++);
219+
TEST_ASSERT_EQUAL(6, passed);
220+
TEST_ASSERT_EQUAL(0, failed);
221+
TEST_ASSERT_EQUAL(REASON_NONE, failure.reason);
222+
TEST_ASSERT_EQUAL(LOCATION_NONE, failure.location);
223+
greentea_test_teardown_handler(passed, failed, failure);
224+
}
225+
226+
Specification specification(greentea_setup, cases, greentea_teardown, selftest_handlers);
227+
extern void utest_run(const Specification& specification);
228+
229+
int main()
230+
{
231+
// Run the specification only AFTER setting the custom scheduler.
232+
Harness::run(specification);
233+
}

0 commit comments

Comments
 (0)