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