Skip to content

Commit 4cd34b1

Browse files
committed
Port utest unit tests from Yotta / Minar based environment to Morpheus /
us ticker based environment
1 parent 720e8e7 commit 4cd34b1

File tree

14 files changed

+1922
-0
lines changed

14 files changed

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

0 commit comments

Comments
 (0)