Statistics
| Branch: | Tag: | Revision:

amiro-os / core / src / aos_test.c @ 61460fde

History | View | Annotate | Download (8.243 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   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
 * @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
inline uint32_t aosTestResultTotal(const aos_testresult_t* result)
84
{
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
inline float aosTestResultRatio(const aos_testresult_t *result)
98
{
99
  aosDbgCheck(result != NULL);
100

    
101
  if (aosTestResultTotal(result) > 0) {
102
    return (float)result->passed / (float)(aosTestResultTotal(result));
103
  } 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
void aosTestResultPrintSummary(BaseSequentialStream *stream, const aos_testresult_t* result, const char* heading)
121
{
122
  aosDbgCheck(stream != NULL);
123
  aosDbgCheck(result != NULL);
124

    
125
  chprintf(stream, "%s:\n", (heading != NULL) ? heading : "summary");
126
  chprintf(stream, "\ttotal:  %3u\n", aosTestResultTotal(result));
127
  chprintf(stream, "\tpassed: %3u\n", result->passed);
128
  chprintf(stream, "\tfailed: %3u\n", result->failed);
129
  chprintf(stream, "\tratio:  %3u%%\n", (uint8_t)(aosTestResultRatio(result) * 100.0f)); // implicitly rounded off by cast to integer
130

    
131
  return;
132
}
133

    
134
/**
135
 * @brief   Run a test.
136
 *
137
 * @param[in] stream  A stream for printing messages.
138
 * @param[in] test    Test to execute.
139
 * @param[in] note    Optional note string.
140
 *
141
 * @return    Result of the test.
142
 */
143
aos_testresult_t aosTestRun(BaseSequentialStream *stream, const aos_test_t *test, const char* note)
144
{
145
  aosDbgCheck(stream != NULL);
146
  aosDbgCheck(test != NULL);
147

    
148
  // print name heading
149
  {
150
    chprintf(stream, "\n");
151
    const int nchars = chprintf(stream, "%s test\n", test->name);
152
    for (int c = 0; c < nchars-1; ++c) {
153
      chprintf(stream, "=");
154
    }
155
    chprintf(stream, "\n");
156
  }
157

    
158
  // print info (if any)
159
  if (test->info != NULL) {
160
    chprintf(stream, "info: %s\n", test->info);
161
  }
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
  aos_testresult_t result = test->testfunc(stream, test);
170

    
171
  // print summary
172
  aosTestResultPrintSummary(stream, &result, NULL);
173

    
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
void aosTestPassed(BaseSequentialStream *stream, aos_testresult_t* result)
185
{
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
void aosTestPassedMsg(BaseSequentialStream *stream, aos_testresult_t* result, const char *fmt, ...)
205
{
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
void aosTestFailed(BaseSequentialStream *stream, aos_testresult_t* result)
229
{
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
void aosTestFailedMsg(BaseSequentialStream *stream, aos_testresult_t* result, const char *fmt, ...)
249
{
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
void aosTestInfoMsg(BaseSequentialStream* stream, const char* fmt, ...)
272
{
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
#endif /* (AMIROOS_CFG_TESTS_ENABLE == true) */
285

    
286
/** @} */