amiro-os / core / src / aos_test.c @ f606e2bf
History | View | Annotate | Download (8.467 KB)
1 |
/*
|
---|---|
2 |
AMiRo-OS is an operating system designed for the Autonomous Mini Robot (AMiRo) platform.
|
3 |
Copyright (C) 2016..2019 Thomas Schöpping et al.
|
4 |
|
5 |
This program is free software: you can redistribute it and/or modify
|
6 |
it under the terms of the GNU General Public License as published by
|
7 |
the Free Software Foundation, either version 3 of the License, or
|
8 |
(at your option) any later version.
|
9 |
|
10 |
This program is distributed in the hope that it will be useful,
|
11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
GNU General Public License for more details.
|
14 |
|
15 |
You should have received a copy of the GNU General Public License
|
16 |
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
17 |
*/
|
18 |
|
19 |
/**
|
20 |
* @file aos_test.c
|
21 |
* @brief Test code.
|
22 |
* @details Functions to initialize and run tests,
|
23 |
* as well as utility functions to be used in tests.
|
24 |
*
|
25 |
* @addtogroup aos_tests
|
26 |
* @{
|
27 |
*/
|
28 |
|
29 |
#include <amiroos.h> |
30 |
|
31 |
#if (AMIROOS_CFG_TESTS_ENABLE == true) || defined(__DOXYGEN__) |
32 |
|
33 |
#include <string.h> |
34 |
#include <stdarg.h> |
35 |
|
36 |
/******************************************************************************/
|
37 |
/* LOCAL DEFINITIONS */
|
38 |
/******************************************************************************/
|
39 |
|
40 |
/******************************************************************************/
|
41 |
/* EXPORTED VARIABLES */
|
42 |
/******************************************************************************/
|
43 |
|
44 |
/******************************************************************************/
|
45 |
/* LOCAL TYPES */
|
46 |
/******************************************************************************/
|
47 |
|
48 |
/******************************************************************************/
|
49 |
/* LOCAL VARIABLES */
|
50 |
/******************************************************************************/
|
51 |
|
52 |
/******************************************************************************/
|
53 |
/* LOCAL FUNCTIONS */
|
54 |
/******************************************************************************/
|
55 |
|
56 |
/******************************************************************************/
|
57 |
/* EXPORTED FUNCTIONS */
|
58 |
/******************************************************************************/
|
59 |
|
60 |
/**
|
61 |
* @brief Initialize a result object.
|
62 |
*
|
63 |
* @param[in] result Pointer to the object to be nitialized.
|
64 |
*/
|
65 |
inline void aosTestResultInit(aos_testresult_t* result) |
66 |
{ |
67 |
result->passed = 0;
|
68 |
result->failed = 0;
|
69 |
|
70 |
return;
|
71 |
} |
72 |
|
73 |
/**
|
74 |
* @brief Adds two result objects.
|
75 |
*
|
76 |
* @param[in] a The first summand.
|
77 |
* @param[in] b The second summand.
|
78 |
*
|
79 |
* @return Resulting sum of both summands added.
|
80 |
*/
|
81 |
inline aos_testresult_t aosTestResultAdd(aos_testresult_t a, aos_testresult_t b)
|
82 |
{ |
83 |
a.passed += b.passed; |
84 |
a.failed += b.failed; |
85 |
|
86 |
return a;
|
87 |
} |
88 |
|
89 |
/**
|
90 |
* @brief Retrieve the total number of tests (passed and failed).
|
91 |
*
|
92 |
* @param[in] result The result object to evaluate.
|
93 |
*
|
94 |
* @return Number of total tests executed.
|
95 |
*/
|
96 |
inline uint32_t aosTestResultTotal(const aos_testresult_t* result) |
97 |
{ |
98 |
aosDbgCheck(result != NULL);
|
99 |
|
100 |
return result->passed + result->failed;
|
101 |
} |
102 |
|
103 |
/**
|
104 |
* @brief Retrieve the ratio of passed tests.
|
105 |
*
|
106 |
* @param[in] result The result object to evaluate.
|
107 |
*
|
108 |
* @return Ratio of passed tests to total tests as float in range [0, 1].
|
109 |
*/
|
110 |
inline float aosTestResultRatio(const aos_testresult_t *result) |
111 |
{ |
112 |
aosDbgCheck(result != NULL);
|
113 |
|
114 |
if (aosTestResultTotal(result) > 0) { |
115 |
return (float)result->passed / (float)(aosTestResultTotal(result)); |
116 |
} else {
|
117 |
return 1.0f; |
118 |
} |
119 |
} |
120 |
|
121 |
/**
|
122 |
* @brief Print the summary of a test.
|
123 |
* @details The summary consists of:
|
124 |
* - total numer of tests executed
|
125 |
* - absolute number of passed tests
|
126 |
* - absolute number of failed tests
|
127 |
* - relative ratio of passed tests
|
128 |
*
|
129 |
* @param[in] stream Stream to print the result to.
|
130 |
* @param[in] result Result to evaluate and print.
|
131 |
* @param[in] heading Optional heading (defaults to "summary").
|
132 |
*/
|
133 |
void aosTestResultPrintSummary(BaseSequentialStream *stream, const aos_testresult_t* result, const char* heading) |
134 |
{ |
135 |
aosDbgCheck(stream != NULL);
|
136 |
aosDbgCheck(result != NULL);
|
137 |
|
138 |
chprintf(stream, "%s:\n", (heading != NULL) ? heading : "summary"); |
139 |
chprintf(stream, "\ttotal: %3u\n", aosTestResultTotal(result));
|
140 |
chprintf(stream, "\tpassed: %3u\n", result->passed);
|
141 |
chprintf(stream, "\tfailed: %3u\n", result->failed);
|
142 |
chprintf(stream, "\tratio: %3u%%\n", (uint8_t)(aosTestResultRatio(result) * 100.0f)); // implicitly rounded off by cast to integer |
143 |
|
144 |
return;
|
145 |
} |
146 |
|
147 |
/**
|
148 |
* @brief Run a test.
|
149 |
*
|
150 |
* @param[in] stream A stream for printing messages.
|
151 |
* @param[in] test Test to execute.
|
152 |
* @param[in] note Optional note string.
|
153 |
*
|
154 |
* @return Result of the test.
|
155 |
*/
|
156 |
aos_testresult_t aosTestRun(BaseSequentialStream *stream, const aos_test_t *test, const char* note) |
157 |
{ |
158 |
aosDbgCheck(stream != NULL);
|
159 |
aosDbgCheck(test != NULL);
|
160 |
|
161 |
// print name heading
|
162 |
{ |
163 |
chprintf(stream, "\n");
|
164 |
const int nchars = chprintf(stream, "%s test\n", test->name); |
165 |
for (int c = 0; c < nchars-1; ++c) { |
166 |
chprintf(stream, "=");
|
167 |
} |
168 |
chprintf(stream, "\n");
|
169 |
} |
170 |
|
171 |
// print info (if any)
|
172 |
if (test->info != NULL) { |
173 |
chprintf(stream, "info: %s\n", test->info);
|
174 |
} |
175 |
// print note (if any)
|
176 |
if (note != NULL) { |
177 |
chprintf(stream, "note: %s\n", note);
|
178 |
} |
179 |
chprintf(stream, "\n");
|
180 |
|
181 |
// run test
|
182 |
aos_testresult_t result = test->testfunc(stream, test); |
183 |
|
184 |
// print summary
|
185 |
aosTestResultPrintSummary(stream, &result, NULL);
|
186 |
|
187 |
return result;
|
188 |
} |
189 |
|
190 |
/**
|
191 |
* @brief Helper function for passed tests.
|
192 |
* @details Prints a message that the test was passed and modifies the result accordigly.
|
193 |
*
|
194 |
* @param[in] stream Stream to print the message to.
|
195 |
* @param[in,out] result Result object to modify.
|
196 |
*/
|
197 |
void aosTestPassed(BaseSequentialStream *stream, aos_testresult_t* result)
|
198 |
{ |
199 |
aosDbgCheck(stream != NULL);
|
200 |
aosDbgCheck(result != NULL);
|
201 |
|
202 |
++result->passed; |
203 |
chprintf(stream, "\tPASSED\n");
|
204 |
chprintf(stream, "\n");
|
205 |
|
206 |
return;
|
207 |
} |
208 |
|
209 |
/**
|
210 |
* @brief Helper function for passed tests.
|
211 |
* @details Prints a message that the test was passed, an additional custom message, and modifies the result accordigly.
|
212 |
*
|
213 |
* @param[in] stream Stream to print the message to.
|
214 |
* @param[in,out] result Result object to modify.
|
215 |
* @param[in] fmt Formatted message string.
|
216 |
*/
|
217 |
void aosTestPassedMsg(BaseSequentialStream *stream, aos_testresult_t* result, const char *fmt, ...) |
218 |
{ |
219 |
aosDbgCheck(stream != NULL);
|
220 |
aosDbgCheck(result != NULL);
|
221 |
|
222 |
va_list ap; |
223 |
|
224 |
++result->passed; |
225 |
chprintf(stream, "\tPASSED\t");
|
226 |
va_start(ap, fmt); |
227 |
chvprintf(stream, fmt, ap); |
228 |
va_end(ap); |
229 |
chprintf(stream, "\n");
|
230 |
|
231 |
return;
|
232 |
} |
233 |
|
234 |
/**
|
235 |
* @brief Helper function for failed tests.
|
236 |
* @details Prints a message that the test was failed and modifies the result accordigly.
|
237 |
*
|
238 |
* @param[in] stream Stream to print the message to.
|
239 |
* @param[in,out] result Result object to modify.
|
240 |
*/
|
241 |
void aosTestFailed(BaseSequentialStream *stream, aos_testresult_t* result)
|
242 |
{ |
243 |
aosDbgCheck(stream != NULL);
|
244 |
aosDbgCheck(result != NULL);
|
245 |
|
246 |
++result->failed; |
247 |
chprintf(stream, "\tFAILED\n");
|
248 |
chprintf(stream, "\n");
|
249 |
|
250 |
return;
|
251 |
} |
252 |
|
253 |
/**
|
254 |
* @brief Helper function for failed tests.
|
255 |
* @details Prints a message that the test was failed, an additional custom message, and modifies the result accordigly.
|
256 |
*
|
257 |
* @param[in] stream Stream to print the message to.
|
258 |
* @param[in,out] result Result object to modify.
|
259 |
* @param[in] fmt Formatted message string.
|
260 |
*/
|
261 |
void aosTestFailedMsg(BaseSequentialStream *stream, aos_testresult_t* result, const char *fmt, ...) |
262 |
{ |
263 |
aosDbgCheck(stream != NULL);
|
264 |
aosDbgCheck(result != NULL);
|
265 |
|
266 |
va_list ap; |
267 |
|
268 |
++result->failed; |
269 |
chprintf(stream, "\tFAILED\t");
|
270 |
va_start(ap, fmt); |
271 |
chvprintf(stream, fmt, ap); |
272 |
va_end(ap); |
273 |
chprintf(stream, "\n");
|
274 |
|
275 |
return;
|
276 |
} |
277 |
|
278 |
/**
|
279 |
* @brief Helper function for information messages.
|
280 |
*
|
281 |
* @param[in] stream Strean to rpint the message to.
|
282 |
* @param[in] fmt Formatted message string.
|
283 |
*/
|
284 |
void aosTestInfoMsg(BaseSequentialStream* stream, const char* fmt, ...) |
285 |
{ |
286 |
aosDbgCheck(stream != NULL);
|
287 |
|
288 |
va_list ap; |
289 |
va_start(ap, fmt); |
290 |
chvprintf(stream, fmt, ap); |
291 |
va_end(ap); |
292 |
chprintf(stream, "\n");
|
293 |
|
294 |
return;
|
295 |
} |
296 |
|
297 |
#endif /* (AMIROOS_CFG_TESTS_ENABLE == true) */ |
298 |
|
299 |
/** @} */
|