amiro-os / core / src / aos_unittest.c @ f1f56ad4
History | View | Annotate | Download (7.375 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 | * @file aos_unittest.c
|
||
21 | * @brief Unittest code.
|
||
22 | * @details Functions to initialize and run unittests,
|
||
23 | * as well as utility functions to be used in unittests.
|
||
24 | *
|
||
25 | * @addtogroup aos_unittests
|
||
26 | * @{
|
||
27 | */
|
||
28 | |||
29 | e545e620 | Thomas Schöpping | #include <aos_unittest.h> |
30 | |||
31 | bc5cdb79 | Thomas Schöpping | #if (AMIROOS_CFG_TESTS_ENABLE == true) || defined(__DOXYGEN__) |
32 | e545e620 | Thomas Schöpping | #include <aos_debug.h> |
33 | #include <chprintf.h> |
||
34 | #include <string.h> |
||
35 | |||
36 | /**
|
||
37 | * @brief Retrieve the total number of tests (passed and failed).
|
||
38 | *
|
||
39 | * @param[in] result The result object to evaluate.
|
||
40 | *
|
||
41 | * @return Number of total tests executed.
|
||
42 | */
|
||
43 | inline uint32_t aosUtResultTotal(aos_utresult_t *result)
|
||
44 | { |
||
45 | aosDbgCheck(result != NULL);
|
||
46 | |||
47 | return result->passed + result->failed;
|
||
48 | } |
||
49 | |||
50 | /**
|
||
51 | * @brief Retrieve the ratio of passed tests.
|
||
52 | *
|
||
53 | * @param[in] result The result object to evaluate.
|
||
54 | *
|
||
55 | * @return Ratio of passed tests to total tests as float in range [0, 1].
|
||
56 | */
|
||
57 | inline float aosUtResultRatio(aos_utresult_t *result) |
||
58 | { |
||
59 | aosDbgCheck(result != NULL);
|
||
60 | |||
61 | if (aosUtResultTotal(result) > 0) { |
||
62 | return (float)result->passed / (float)aosUtResultTotal(result); |
||
63 | } else {
|
||
64 | return 1.0f; |
||
65 | } |
||
66 | } |
||
67 | |||
68 | /**
|
||
69 | * @brief Print the summary of a test.
|
||
70 | * @details The summary consists of:
|
||
71 | * - total numer of tests executed
|
||
72 | * - absolute number of passed tests
|
||
73 | * - absolute number of failed tests
|
||
74 | * - relative ratio of passed tests
|
||
75 | *
|
||
76 | * @param[in] stream Stream to print the result to.
|
||
77 | * @param[in] result Result to evaluate and print.
|
||
78 | * @param[in] heading Optional heading (defaults to "summary").
|
||
79 | */
|
||
80 | void aosUtResultPrintSummary(BaseSequentialStream *stream, aos_utresult_t *result, char* heading) |
||
81 | { |
||
82 | aosDbgCheck(stream != NULL);
|
||
83 | aosDbgCheck(result != NULL);
|
||
84 | |||
85 | chprintf(stream, "%s:\n", (heading != NULL) ? heading : "summary"); |
||
86 | chprintf(stream, "\ttotal: %3u\n", aosUtResultTotal(result));
|
||
87 | chprintf(stream, "\tpassed: %3u\n", result->passed);
|
||
88 | chprintf(stream, "\tfailed: %3u\n", result->failed);
|
||
89 | chprintf(stream, "\tratio: %3u%%\n", (uint8_t)(aosUtResultRatio(result) * 100.0f)); |
||
90 | |||
91 | return;
|
||
92 | } |
||
93 | |||
94 | /**
|
||
95 | * @brief Initialize a unit test object.
|
||
96 | *
|
||
97 | * @param[in] ut The unit test object to initialize.
|
||
98 | * @param[in] name name of the unit test.
|
||
99 | * @param[in] info Optional information string.
|
||
100 | * @param[in] func Unit test calback function.
|
||
101 | * @param[in] data Optional data for the unit test.
|
||
102 | * @param[in] shellname Name of the shell command
|
||
103 | * @param[in] shellcb Callback for the shell command.
|
||
104 | */
|
||
105 | void aosUtObjectInit(aos_unittest_t* ut, char* name, char* info, aos_utfunction_t func, void* data, char* shellname, aos_shellcmdcb_t shellcb) |
||
106 | { |
||
107 | aosDbgCheck(ut != NULL);
|
||
108 | aosDbgCheck(name != NULL && strlen(name) > 0); |
||
109 | aosDbgCheck(func != NULL);
|
||
110 | aosDbgCheck(shellname != NULL && strlen(shellname) > 0); |
||
111 | aosDbgCheck(shellcb != NULL);
|
||
112 | |||
113 | ut->name = name; |
||
114 | ut->info = info; |
||
115 | ut->testfunc = func; |
||
116 | ut->shellcmd.name = shellname; |
||
117 | ut->shellcmd.callback = shellcb; |
||
118 | ut->shellcmd.next = NULL;
|
||
119 | ut->data = data; |
||
120 | |||
121 | return;
|
||
122 | } |
||
123 | |||
124 | /**
|
||
125 | * @brief Run an unit test.
|
||
126 | *
|
||
127 | * @param[in] stream A stream for printing messages.
|
||
128 | * @param[in] ut Unit test to execute.
|
||
129 | * @param[in] note Optional note string.
|
||
130 | *
|
||
131 | * @return Result of the test.
|
||
132 | */
|
||
133 | aos_utresult_t aosUtRun(BaseSequentialStream *stream, aos_unittest_t *ut, char *note)
|
||
134 | { |
||
135 | aosDbgCheck(stream != NULL);
|
||
136 | aosDbgCheck(ut != NULL);
|
||
137 | |||
138 | // print name heading
|
||
139 | { |
||
140 | chprintf(stream, "\n");
|
||
141 | const int nchars = chprintf(stream, "%s unit test\n", ut->name); |
||
142 | for (int c = 0; c < nchars-1; ++c) { |
||
143 | chprintf(stream, "=");
|
||
144 | } |
||
145 | chprintf(stream, "\n");
|
||
146 | } |
||
147 | |||
148 | // print info (if any)
|
||
149 | if (ut->info != NULL) { |
||
150 | chprintf(stream, "info: %s\n", ut->info);
|
||
151 | } |
||
152 | // print note (if any)
|
||
153 | if (note != NULL) { |
||
154 | chprintf(stream, "note: %s\n", note);
|
||
155 | } |
||
156 | chprintf(stream, "\n");
|
||
157 | |||
158 | // run test
|
||
159 | aos_utresult_t result = ut->testfunc(stream, ut); |
||
160 | |||
161 | // print summary
|
||
162 | aosUtResultPrintSummary(stream, &result, NULL);
|
||
163 | |||
164 | return result;
|
||
165 | } |
||
166 | |||
167 | /**
|
||
168 | * @brief Helper function for passed tests.
|
||
169 | * @details Prints a message that the test was passed and modifies the result accordigly.
|
||
170 | *
|
||
171 | * @param[in] stream Stream to print the message to.
|
||
172 | * @param[in,out] result Result object to modify.
|
||
173 | */
|
||
174 | void aosUtPassed(BaseSequentialStream *stream, aos_utresult_t* result)
|
||
175 | { |
||
176 | aosDbgCheck(stream != NULL);
|
||
177 | aosDbgCheck(result != NULL);
|
||
178 | |||
179 | ++result->passed; |
||
180 | chprintf(stream, "\tPASSED\n");
|
||
181 | chprintf(stream, "\n");
|
||
182 | |||
183 | return;
|
||
184 | } |
||
185 | |||
186 | /**
|
||
187 | * @brief Helper function for passed tests.
|
||
188 | * @details Prints a message that the test was passed, an additional custom message, and modifies the result accordigly.
|
||
189 | *
|
||
190 | * @param[in] stream Stream to print the message to.
|
||
191 | * @param[in,out] result Result object to modify.
|
||
192 | * @param[in] fmt Formatted message string.
|
||
193 | */
|
||
194 | void aosUtPassedMsg(BaseSequentialStream *stream, aos_utresult_t* result, const char *fmt, ...) |
||
195 | { |
||
196 | aosDbgCheck(stream != NULL);
|
||
197 | aosDbgCheck(result != NULL);
|
||
198 | |||
199 | va_list ap; |
||
200 | |||
201 | ++result->passed; |
||
202 | chprintf(stream, "\tPASSED\t");
|
||
203 | va_start(ap, fmt); |
||
204 | chvprintf(stream, fmt, ap); |
||
205 | va_end(ap); |
||
206 | chprintf(stream, "\n");
|
||
207 | |||
208 | return;
|
||
209 | } |
||
210 | |||
211 | /**
|
||
212 | * @brief Helper function for failed tests.
|
||
213 | * @details Prints a message that the test was failed and modifies the result accordigly.
|
||
214 | *
|
||
215 | * @param[in] stream Stream to print the message to.
|
||
216 | * @param[in,out] result Result object to modify.
|
||
217 | */
|
||
218 | void aosUtFailed(BaseSequentialStream *stream, aos_utresult_t* result)
|
||
219 | { |
||
220 | aosDbgCheck(stream != NULL);
|
||
221 | aosDbgCheck(result != NULL);
|
||
222 | |||
223 | ++result->failed; |
||
224 | chprintf(stream, "\tFAILED\n");
|
||
225 | chprintf(stream, "\n");
|
||
226 | |||
227 | return;
|
||
228 | } |
||
229 | |||
230 | /**
|
||
231 | * @brief Helper function for failed tests.
|
||
232 | * @details Prints a message that the test was failed, an additional custom message, and modifies the result accordigly.
|
||
233 | *
|
||
234 | * @param[in] stream Stream to print the message to.
|
||
235 | * @param[in,out] result Result object to modify.
|
||
236 | * @param[in] fmt Formatted message string.
|
||
237 | */
|
||
238 | void aosUtFailedMsg(BaseSequentialStream *stream, aos_utresult_t* result, const char *fmt, ...) |
||
239 | { |
||
240 | aosDbgCheck(stream != NULL);
|
||
241 | aosDbgCheck(result != NULL);
|
||
242 | |||
243 | va_list ap; |
||
244 | |||
245 | ++result->failed; |
||
246 | chprintf(stream, "\tFAILED\t");
|
||
247 | va_start(ap, fmt); |
||
248 | chvprintf(stream, fmt, ap); |
||
249 | va_end(ap); |
||
250 | chprintf(stream, "\n");
|
||
251 | |||
252 | return;
|
||
253 | } |
||
254 | |||
255 | /**
|
||
256 | * @brief Helper function for information messages.
|
||
257 | *
|
||
258 | * @param[in] stream Strean to rpint the message to.
|
||
259 | * @param[in] fmt Formatted message string.
|
||
260 | */
|
||
261 | void aosUtInfoMsg(BaseSequentialStream* stream, const char* fmt, ...) |
||
262 | { |
||
263 | aosDbgCheck(stream != NULL);
|
||
264 | |||
265 | va_list ap; |
||
266 | va_start(ap, fmt); |
||
267 | chvprintf(stream, fmt, ap); |
||
268 | va_end(ap); |
||
269 | chprintf(stream, "\n");
|
||
270 | |||
271 | return;
|
||
272 | } |
||
273 | |||
274 | #endif /* AMIROOS_CFG_TESTS_ENABLE == true */ |
||
275 | 53710ca3 | Marc Rothmann | |
276 | /** @} */ |