Statistics
| Branch: | Tag: | Revision:

amiro-os / core / src / aos_test.c @ 78b92b33

History | View | Annotate | Download (8.467 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 ce12e797 Thomas Schöpping
 * @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 4c72a54c Thomas Schöpping
 * @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 e545e620 Thomas Schöpping
 * @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 4c72a54c Thomas Schöpping
inline uint32_t aosTestResultTotal(const aos_testresult_t* result)
97 e545e620 Thomas Schöpping
{
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 4c72a54c Thomas Schöpping
inline float aosTestResultRatio(const aos_testresult_t *result)
111 e545e620 Thomas Schöpping
{
112
  aosDbgCheck(result != NULL);
113
114 4c72a54c Thomas Schöpping
  if (aosTestResultTotal(result) > 0) {
115
    return (float)result->passed / (float)(aosTestResultTotal(result));
116 e545e620 Thomas Schöpping
  } 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 4c72a54c Thomas Schöpping
void aosTestResultPrintSummary(BaseSequentialStream *stream, const aos_testresult_t* result, const char* heading)
134 e545e620 Thomas Schöpping
{
135
  aosDbgCheck(stream != NULL);
136
  aosDbgCheck(result != NULL);
137
138
  chprintf(stream, "%s:\n", (heading != NULL) ? heading : "summary");
139 4c72a54c Thomas Schöpping
  chprintf(stream, "\ttotal:  %3u\n", aosTestResultTotal(result));
140 e545e620 Thomas Schöpping
  chprintf(stream, "\tpassed: %3u\n", result->passed);
141
  chprintf(stream, "\tfailed: %3u\n", result->failed);
142 4c72a54c Thomas Schöpping
  chprintf(stream, "\tratio:  %3u%%\n", (uint8_t)(aosTestResultRatio(result) * 100.0f)); // implicitly rounded off by cast to integer
143 e545e620 Thomas Schöpping
144
  return;
145
}
146
147
/**
148 4c72a54c Thomas Schöpping
 * @brief   Run a test.
149 e545e620 Thomas Schöpping
 *
150
 * @param[in] stream  A stream for printing messages.
151 4c72a54c Thomas Schöpping
 * @param[in] test    Test to execute.
152 e545e620 Thomas Schöpping
 * @param[in] note    Optional note string.
153
 *
154
 * @return    Result of the test.
155
 */
156 4c72a54c Thomas Schöpping
aos_testresult_t aosTestRun(BaseSequentialStream *stream, const aos_test_t *test, const char* note)
157 e545e620 Thomas Schöpping
{
158
  aosDbgCheck(stream != NULL);
159 4c72a54c Thomas Schöpping
  aosDbgCheck(test != NULL);
160 e545e620 Thomas Schöpping
161
  // print name heading
162
  {
163
    chprintf(stream, "\n");
164 4c72a54c Thomas Schöpping
    const int nchars = chprintf(stream, "%s test\n", test->name);
165 e545e620 Thomas Schöpping
    for (int c = 0; c < nchars-1; ++c) {
166
      chprintf(stream, "=");
167
    }
168
    chprintf(stream, "\n");
169
  }
170
171
  // print info (if any)
172 4c72a54c Thomas Schöpping
  if (test->info != NULL) {
173
    chprintf(stream, "info: %s\n", test->info);
174 e545e620 Thomas Schöpping
  }
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 4c72a54c Thomas Schöpping
  aos_testresult_t result = test->testfunc(stream, test);
183 e545e620 Thomas Schöpping
184
  // print summary
185 4c72a54c Thomas Schöpping
  aosTestResultPrintSummary(stream, &result, NULL);
186 e545e620 Thomas Schöpping
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 4c72a54c Thomas Schöpping
void aosTestPassed(BaseSequentialStream *stream, aos_testresult_t* result)
198 e545e620 Thomas Schöpping
{
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 4c72a54c Thomas Schöpping
void aosTestPassedMsg(BaseSequentialStream *stream, aos_testresult_t* result, const char *fmt, ...)
218 e545e620 Thomas Schöpping
{
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 4c72a54c Thomas Schöpping
void aosTestFailed(BaseSequentialStream *stream, aos_testresult_t* result)
242 e545e620 Thomas Schöpping
{
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 4c72a54c Thomas Schöpping
void aosTestFailedMsg(BaseSequentialStream *stream, aos_testresult_t* result, const char *fmt, ...)
262 e545e620 Thomas Schöpping
{
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 4c72a54c Thomas Schöpping
void aosTestInfoMsg(BaseSequentialStream* stream, const char* fmt, ...)
285 e545e620 Thomas Schöpping
{
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 7de0cc90 Thomas Schöpping
#endif /* (AMIROOS_CFG_TESTS_ENABLE == true) */
298 53710ca3 Marc Rothmann
299
/** @} */