Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (8.804 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_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
#include <aos_unittest.h>
30

    
31
#if (AMIROOS_CFG_TESTS_ENABLE == true) || defined(__DOXYGEN__)
32
#include <aos_debug.h>
33
#include <chprintf.h>
34
#include <string.h>
35
/******************************************************************************/
36
/* LOCAL DEFINITIONS                                                          */
37
/******************************************************************************/
38

    
39
/******************************************************************************/
40
/* EXPORTED VARIABLES                                                         */
41
/******************************************************************************/
42

    
43
/******************************************************************************/
44
/* LOCAL TYPES                                                                */
45
/******************************************************************************/
46

    
47
/******************************************************************************/
48
/* LOCAL VARIABLES                                                            */
49
/******************************************************************************/
50

    
51
/******************************************************************************/
52
/* LOCAL FUNCTIONS                                                            */
53
/******************************************************************************/
54

    
55
/******************************************************************************/
56
/* EXPORTED FUNCTIONS                                                         */
57
/******************************************************************************/
58

    
59
/**
60
 * @brief   Retrieve the total number of tests (passed and failed).
61
 *
62
 * @param[in] result    The result object to evaluate.
63
 *
64
 * @return  Number of total tests executed.
65
 */
66
inline uint32_t aosUtResultTotal(aos_utresult_t *result)
67
{
68
  aosDbgCheck(result != NULL);
69

    
70
  return result->passed + result->failed;
71
}
72

    
73
/**
74
 * @brief   Retrieve the ratio of passed tests.
75
 *
76
 * @param[in] result    The result object to evaluate.
77
 *
78
 * @return  Ratio of passed tests to total tests as float in range [0, 1].
79
 */
80
inline float aosUtResultRatio(aos_utresult_t *result)
81
{
82
  aosDbgCheck(result != NULL);
83

    
84
  if (aosUtResultTotal(result) > 0) {
85
    return (float)result->passed / (float)aosUtResultTotal(result);
86
  } else {
87
    return 1.0f;
88
  }
89
}
90

    
91
/**
92
 * @brief   Print the summary of a test.
93
 * @details The summary consists of:
94
 *          - total numer of tests executed
95
 *          - absolute number of passed tests
96
 *          - absolute number of failed tests
97
 *          - relative ratio of passed tests
98
 *
99
 * @param[in] stream    Stream to print the result to.
100
 * @param[in] result    Result to evaluate and print.
101
 * @param[in] heading   Optional heading (defaults to "summary").
102
 */
103
void aosUtResultPrintSummary(BaseSequentialStream *stream, aos_utresult_t *result, char* heading)
104
{
105
  aosDbgCheck(stream != NULL);
106
  aosDbgCheck(result != NULL);
107

    
108
  chprintf(stream, "%s:\n", (heading != NULL) ? heading : "summary");
109
  chprintf(stream, "\ttotal:  %3u\n", aosUtResultTotal(result));
110
  chprintf(stream, "\tpassed: %3u\n", result->passed);
111
  chprintf(stream, "\tfailed: %3u\n", result->failed);
112
  chprintf(stream, "\tratio:  %3u%%\n", (uint8_t)(aosUtResultRatio(result) * 100.0f));
113

    
114
  return;
115
}
116

    
117
/**
118
 * @brief   Initialize a unit test object.
119
 *
120
 * @param[in] ut          The unit test object to initialize.
121
 * @param[in] name        name of the unit test.
122
 * @param[in] info        Optional information string.
123
 * @param[in] func        Unit test calback function.
124
 * @param[in] data        Optional data for the unit test.
125
 * @param[in] shellname   Name of the shell command
126
 * @param[in] shellcb     Callback for the shell command.
127
 */
128
void aosUtObjectInit(aos_unittest_t* ut, char* name, char* info, aos_utfunction_t func, void* data, char* shellname, aos_shellcmdcb_t shellcb)
129
{
130
  aosDbgCheck(ut != NULL);
131
  aosDbgCheck(name != NULL && strlen(name) > 0);
132
  aosDbgCheck(func != NULL);
133
  aosDbgCheck(shellname != NULL && strlen(shellname) > 0);
134
  aosDbgCheck(shellcb != NULL);
135

    
136
  ut->name = name;
137
  ut->info = info;
138
  ut->testfunc = func;
139
  ut->shellcmd.name = shellname;
140
  ut->shellcmd.callback = shellcb;
141
  ut->shellcmd.next = NULL;
142
  ut->data = data;
143

    
144
  return;
145
}
146

    
147
/**
148
 * @brief   Run an unit test.
149
 *
150
 * @param[in] stream  A stream for printing messages.
151
 * @param[in] ut      Unit test to execute.
152
 * @param[in] note    Optional note string.
153
 *
154
 * @return    Result of the test.
155
 */
156
aos_utresult_t aosUtRun(BaseSequentialStream *stream, aos_unittest_t *ut, char *note)
157
{
158
  aosDbgCheck(stream != NULL);
159
  aosDbgCheck(ut != NULL);
160

    
161
  // print name heading
162
  {
163
    chprintf(stream, "\n");
164
    const int nchars = chprintf(stream, "%s unit test\n", ut->name);
165
    for (int c = 0; c < nchars-1; ++c) {
166
      chprintf(stream, "=");
167
    }
168
    chprintf(stream, "\n");
169
  }
170

    
171
  // print info (if any)
172
  if (ut->info != NULL) {
173
    chprintf(stream, "info: %s\n", ut->info);
174
  }
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
  aos_utresult_t result = ut->testfunc(stream, ut);
183

    
184
  // print summary
185
  aosUtResultPrintSummary(stream, &result, NULL);
186

    
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
void aosUtPassed(BaseSequentialStream *stream, aos_utresult_t* result)
198
{
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
void aosUtPassedMsg(BaseSequentialStream *stream, aos_utresult_t* result, const char *fmt, ...)
218
{
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
void aosUtFailed(BaseSequentialStream *stream, aos_utresult_t* result)
242
{
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
void aosUtFailedMsg(BaseSequentialStream *stream, aos_utresult_t* result, const char *fmt, ...)
262
{
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
void aosUtInfoMsg(BaseSequentialStream* stream, const char* fmt, ...)
285
{
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
#endif /* AMIROOS_CFG_TESTS_ENABLE == true */
298

    
299
/** @} */