Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (7.147 KB)

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