amiro-os / core / src / aos_test.c @ 3cee92c7
History | View | Annotate | Download (8.243 KB)
1 | e545e620 | Thomas Schöpping | /*
|
---|---|---|---|
2 | AMiRo-OS is an operating system designed for the Autonomous Mini Robot (AMiRo) platform.
|
||
3 | 84f0ce9e | Thomas Schöpping | Copyright (C) 2016..2019 Thomas Schöpping et al.
|
4 | e545e620 | Thomas Schöpping | |
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 | 53710ca3 | Marc Rothmann | /**
|
20 | 4c72a54c | Thomas Schöpping | * @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 | 53710ca3 | Marc Rothmann | *
|
25 | 4c72a54c | Thomas Schöpping | * @addtogroup aos_tests
|
26 | 53710ca3 | Marc Rothmann | * @{
|
27 | */
|
||
28 | |||
29 | 3940ba8a | Thomas Schöpping | #include <amiroos.h> |
30 | e545e620 | Thomas Schöpping | |
31 | bc5cdb79 | Thomas Schöpping | #if (AMIROOS_CFG_TESTS_ENABLE == true) || defined(__DOXYGEN__) |
32 | 3940ba8a | Thomas Schöpping | |
33 | 4c72a54c | Thomas Schöpping | #include <string.h> |
34 | #include <stdarg.h> |
||
35 | |||
36 | f3ac1c96 | Thomas Schöpping | /******************************************************************************/
|
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 | e545e620 | Thomas Schöpping | |
60 | /**
|
||
61 | 4c72a54c | Thomas Schöpping | * @brief Adds two result objects.
|
62 | *
|
||
63 | * @param[in] a The first summand.
|
||
64 | * @param[in] b The second summand.
|
||
65 | *
|
||
66 | * @return Resulting sum of both summands added.
|
||
67 | */
|
||
68 | inline aos_testresult_t aosTestResultAdd(aos_testresult_t a, aos_testresult_t b)
|
||
69 | { |
||
70 | a.passed += b.passed; |
||
71 | a.failed += b.failed; |
||
72 | |||
73 | return a;
|
||
74 | } |
||
75 | |||
76 | /**
|
||
77 | e545e620 | Thomas Schöpping | * @brief Retrieve the total number of tests (passed and failed).
|
78 | *
|
||
79 | * @param[in] result The result object to evaluate.
|
||
80 | *
|
||
81 | * @return Number of total tests executed.
|
||
82 | */
|
||
83 | 4c72a54c | Thomas Schöpping | inline uint32_t aosTestResultTotal(const aos_testresult_t* result) |
84 | e545e620 | Thomas Schöpping | { |
85 | aosDbgCheck(result != NULL);
|
||
86 | |||
87 | return result->passed + result->failed;
|
||
88 | } |
||
89 | |||
90 | /**
|
||
91 | * @brief Retrieve the ratio of passed tests.
|
||
92 | *
|
||
93 | * @param[in] result The result object to evaluate.
|
||
94 | *
|
||
95 | * @return Ratio of passed tests to total tests as float in range [0, 1].
|
||
96 | */
|
||
97 | 4c72a54c | Thomas Schöpping | inline float aosTestResultRatio(const aos_testresult_t *result) |
98 | e545e620 | Thomas Schöpping | { |
99 | aosDbgCheck(result != NULL);
|
||
100 | |||
101 | 4c72a54c | Thomas Schöpping | if (aosTestResultTotal(result) > 0) { |
102 | return (float)result->passed / (float)(aosTestResultTotal(result)); |
||
103 | e545e620 | Thomas Schöpping | } else {
|
104 | return 1.0f; |
||
105 | } |
||
106 | } |
||
107 | |||
108 | /**
|
||
109 | * @brief Print the summary of a test.
|
||
110 | * @details The summary consists of:
|
||
111 | * - total numer of tests executed
|
||
112 | * - absolute number of passed tests
|
||
113 | * - absolute number of failed tests
|
||
114 | * - relative ratio of passed tests
|
||
115 | *
|
||
116 | * @param[in] stream Stream to print the result to.
|
||
117 | * @param[in] result Result to evaluate and print.
|
||
118 | * @param[in] heading Optional heading (defaults to "summary").
|
||
119 | */
|
||
120 | 4c72a54c | Thomas Schöpping | void aosTestResultPrintSummary(BaseSequentialStream *stream, const aos_testresult_t* result, const char* heading) |
121 | e545e620 | Thomas Schöpping | { |
122 | aosDbgCheck(stream != NULL);
|
||
123 | aosDbgCheck(result != NULL);
|
||
124 | |||
125 | chprintf(stream, "%s:\n", (heading != NULL) ? heading : "summary"); |
||
126 | 4c72a54c | Thomas Schöpping | chprintf(stream, "\ttotal: %3u\n", aosTestResultTotal(result));
|
127 | e545e620 | Thomas Schöpping | chprintf(stream, "\tpassed: %3u\n", result->passed);
|
128 | chprintf(stream, "\tfailed: %3u\n", result->failed);
|
||
129 | 4c72a54c | Thomas Schöpping | chprintf(stream, "\tratio: %3u%%\n", (uint8_t)(aosTestResultRatio(result) * 100.0f)); // implicitly rounded off by cast to integer |
130 | e545e620 | Thomas Schöpping | |
131 | return;
|
||
132 | } |
||
133 | |||
134 | /**
|
||
135 | 4c72a54c | Thomas Schöpping | * @brief Run a test.
|
136 | e545e620 | Thomas Schöpping | *
|
137 | * @param[in] stream A stream for printing messages.
|
||
138 | 4c72a54c | Thomas Schöpping | * @param[in] test Test to execute.
|
139 | e545e620 | Thomas Schöpping | * @param[in] note Optional note string.
|
140 | *
|
||
141 | * @return Result of the test.
|
||
142 | */
|
||
143 | 4c72a54c | Thomas Schöpping | aos_testresult_t aosTestRun(BaseSequentialStream *stream, const aos_test_t *test, const char* note) |
144 | e545e620 | Thomas Schöpping | { |
145 | aosDbgCheck(stream != NULL);
|
||
146 | 4c72a54c | Thomas Schöpping | aosDbgCheck(test != NULL);
|
147 | e545e620 | Thomas Schöpping | |
148 | // print name heading
|
||
149 | { |
||
150 | chprintf(stream, "\n");
|
||
151 | 4c72a54c | Thomas Schöpping | const int nchars = chprintf(stream, "%s test\n", test->name); |
152 | e545e620 | Thomas Schöpping | for (int c = 0; c < nchars-1; ++c) { |
153 | chprintf(stream, "=");
|
||
154 | } |
||
155 | chprintf(stream, "\n");
|
||
156 | } |
||
157 | |||
158 | // print info (if any)
|
||
159 | 4c72a54c | Thomas Schöpping | if (test->info != NULL) { |
160 | chprintf(stream, "info: %s\n", test->info);
|
||
161 | e545e620 | Thomas Schöpping | } |
162 | // print note (if any)
|
||
163 | if (note != NULL) { |
||
164 | chprintf(stream, "note: %s\n", note);
|
||
165 | } |
||
166 | chprintf(stream, "\n");
|
||
167 | |||
168 | // run test
|
||
169 | 4c72a54c | Thomas Schöpping | aos_testresult_t result = test->testfunc(stream, test); |
170 | e545e620 | Thomas Schöpping | |
171 | // print summary
|
||
172 | 4c72a54c | Thomas Schöpping | aosTestResultPrintSummary(stream, &result, NULL);
|
173 | e545e620 | Thomas Schöpping | |
174 | return result;
|
||
175 | } |
||
176 | |||
177 | /**
|
||
178 | * @brief Helper function for passed tests.
|
||
179 | * @details Prints a message that the test was passed and modifies the result accordigly.
|
||
180 | *
|
||
181 | * @param[in] stream Stream to print the message to.
|
||
182 | * @param[in,out] result Result object to modify.
|
||
183 | */
|
||
184 | 4c72a54c | Thomas Schöpping | void aosTestPassed(BaseSequentialStream *stream, aos_testresult_t* result)
|
185 | e545e620 | Thomas Schöpping | { |
186 | aosDbgCheck(stream != NULL);
|
||
187 | aosDbgCheck(result != NULL);
|
||
188 | |||
189 | ++result->passed; |
||
190 | chprintf(stream, "\tPASSED\n");
|
||
191 | chprintf(stream, "\n");
|
||
192 | |||
193 | return;
|
||
194 | } |
||
195 | |||
196 | /**
|
||
197 | * @brief Helper function for passed tests.
|
||
198 | * @details Prints a message that the test was passed, an additional custom message, and modifies the result accordigly.
|
||
199 | *
|
||
200 | * @param[in] stream Stream to print the message to.
|
||
201 | * @param[in,out] result Result object to modify.
|
||
202 | * @param[in] fmt Formatted message string.
|
||
203 | */
|
||
204 | 4c72a54c | Thomas Schöpping | void aosTestPassedMsg(BaseSequentialStream *stream, aos_testresult_t* result, const char *fmt, ...) |
205 | e545e620 | Thomas Schöpping | { |
206 | aosDbgCheck(stream != NULL);
|
||
207 | aosDbgCheck(result != NULL);
|
||
208 | |||
209 | va_list ap; |
||
210 | |||
211 | ++result->passed; |
||
212 | chprintf(stream, "\tPASSED\t");
|
||
213 | va_start(ap, fmt); |
||
214 | chvprintf(stream, fmt, ap); |
||
215 | va_end(ap); |
||
216 | chprintf(stream, "\n");
|
||
217 | |||
218 | return;
|
||
219 | } |
||
220 | |||
221 | /**
|
||
222 | * @brief Helper function for failed tests.
|
||
223 | * @details Prints a message that the test was failed and modifies the result accordigly.
|
||
224 | *
|
||
225 | * @param[in] stream Stream to print the message to.
|
||
226 | * @param[in,out] result Result object to modify.
|
||
227 | */
|
||
228 | 4c72a54c | Thomas Schöpping | void aosTestFailed(BaseSequentialStream *stream, aos_testresult_t* result)
|
229 | e545e620 | Thomas Schöpping | { |
230 | aosDbgCheck(stream != NULL);
|
||
231 | aosDbgCheck(result != NULL);
|
||
232 | |||
233 | ++result->failed; |
||
234 | chprintf(stream, "\tFAILED\n");
|
||
235 | chprintf(stream, "\n");
|
||
236 | |||
237 | return;
|
||
238 | } |
||
239 | |||
240 | /**
|
||
241 | * @brief Helper function for failed tests.
|
||
242 | * @details Prints a message that the test was failed, an additional custom message, and modifies the result accordigly.
|
||
243 | *
|
||
244 | * @param[in] stream Stream to print the message to.
|
||
245 | * @param[in,out] result Result object to modify.
|
||
246 | * @param[in] fmt Formatted message string.
|
||
247 | */
|
||
248 | 4c72a54c | Thomas Schöpping | void aosTestFailedMsg(BaseSequentialStream *stream, aos_testresult_t* result, const char *fmt, ...) |
249 | e545e620 | Thomas Schöpping | { |
250 | aosDbgCheck(stream != NULL);
|
||
251 | aosDbgCheck(result != NULL);
|
||
252 | |||
253 | va_list ap; |
||
254 | |||
255 | ++result->failed; |
||
256 | chprintf(stream, "\tFAILED\t");
|
||
257 | va_start(ap, fmt); |
||
258 | chvprintf(stream, fmt, ap); |
||
259 | va_end(ap); |
||
260 | chprintf(stream, "\n");
|
||
261 | |||
262 | return;
|
||
263 | } |
||
264 | |||
265 | /**
|
||
266 | * @brief Helper function for information messages.
|
||
267 | *
|
||
268 | * @param[in] stream Strean to rpint the message to.
|
||
269 | * @param[in] fmt Formatted message string.
|
||
270 | */
|
||
271 | 4c72a54c | Thomas Schöpping | void aosTestInfoMsg(BaseSequentialStream* stream, const char* fmt, ...) |
272 | e545e620 | Thomas Schöpping | { |
273 | aosDbgCheck(stream != NULL);
|
||
274 | |||
275 | va_list ap; |
||
276 | va_start(ap, fmt); |
||
277 | chvprintf(stream, fmt, ap); |
||
278 | va_end(ap); |
||
279 | chprintf(stream, "\n");
|
||
280 | |||
281 | return;
|
||
282 | } |
||
283 | |||
284 | 7de0cc90 | Thomas Schöpping | #endif /* (AMIROOS_CFG_TESTS_ENABLE == true) */ |
285 | 53710ca3 | Marc Rothmann | |
286 | /** @} */ |