Statistics
| Branch: | Tag: | Revision:

amiro-os / core / src / aos_unittest.c @ b49ed442

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