PX4 Firmware
PX4 Autopilot Software http://px4.io
tests_main.c
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (C) 2012-2019 PX4 Development Team. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in
13  * the documentation and/or other materials provided with the
14  * distribution.
15  * 3. Neither the name PX4 nor the names of its contributors may be
16  * used to endorse or promote products derived from this software
17  * without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  *
32  ****************************************************************************/
33 
34 /**
35  * @file tests_main.c
36  * Tests main file, loads individual tests.
37  *
38  * @author Lorenz Meier <lm@inf.ethz.ch>
39  */
40 
41 #include "tests_main.h"
42 
43 #include <px4_platform_common/px4_config.h>
44 
45 #include <stdio.h>
46 #include <string.h>
47 #include <fcntl.h>
48 #include <errno.h>
49 
50 
51 static int test_help(int argc, char *argv[]);
52 static int test_runner(unsigned option);
53 
54 static int test_all(int argc, char *argv[]);
55 static int test_jig(int argc, char *argv[]);
56 
57 const struct {
58  const char *name;
59  int (* fn)(int argc, char *argv[]);
60  unsigned options;
61 #define OPT_NOHELP (1<<0)
62 #define OPT_NOALLTEST (1<<1)
63 #define OPT_NOJIGTEST (1<<2)
64 } tests[] = {
68 
69 #ifdef __PX4_NUTTX
70  {"adc", test_adc, OPT_NOJIGTEST},
71  {"file", test_file, OPT_NOJIGTEST | OPT_NOALLTEST},
72  {"led", test_led, 0},
73  {"time", test_time, OPT_NOJIGTEST},
74  {"uart_baudchange", test_uart_baudchange, OPT_NOJIGTEST},
75  {"uart_break", test_uart_break, OPT_NOJIGTEST | OPT_NOALLTEST},
76  {"uart_console", test_uart_console, OPT_NOJIGTEST | OPT_NOALLTEST},
77 #else
78  {"rc", rc_tests_main, 0},
79 #endif /* __PX4_NUTTX */
80 
81 
82  {"autodeclination", test_autodeclination, 0},
83  {"bezier", test_bezierQuad, 0},
84  {"bson", test_bson, 0},
85  {"conv", test_conv, 0},
86  {"dataman", test_dataman, OPT_NOJIGTEST | OPT_NOALLTEST},
87  {"file2", test_file2, OPT_NOJIGTEST},
88  {"float", test_float, 0},
89  {"hott_telemetry", test_hott_telemetry, OPT_NOJIGTEST | OPT_NOALLTEST},
90  {"hrt", test_hrt, OPT_NOJIGTEST | OPT_NOALLTEST},
91  {"int", test_int, 0},
92  {"IntrusiveQueue", test_IntrusiveQueue, 0},
93  {"jig_voltages", test_jig_voltages, OPT_NOALLTEST},
94  {"List", test_List, 0},
95  {"mathlib", test_mathlib, 0},
96  {"matrix", test_matrix, 0},
97  {"microbench_hrt", test_microbench_hrt, 0},
98  {"microbench_math", test_microbench_math, 0},
99  {"microbench_matrix", test_microbench_matrix, 0},
100  {"microbench_uorb", test_microbench_uorb, 0},
101  {"mixer", test_mixer, OPT_NOJIGTEST},
102  {"mixer", test_mixer, OPT_NOJIGTEST},
103  {"mount", test_mount, OPT_NOJIGTEST | OPT_NOALLTEST},
104  {"param", test_param, 0},
105  {"parameters", test_parameters, 0},
106  {"perf", test_perf, OPT_NOJIGTEST},
107  {"ppm", test_ppm, OPT_NOJIGTEST | OPT_NOALLTEST},
108  {"ppm_loopback", test_ppm_loopback, OPT_NOALLTEST},
109  {"rc", test_rc, OPT_NOJIGTEST | OPT_NOALLTEST},
110  {"search_min", test_search_min, 0},
111  {"servo", test_servo, OPT_NOJIGTEST | OPT_NOALLTEST},
112  {"sleep", test_sleep, OPT_NOJIGTEST},
113  {"smoothz", test_smooth_z, 0},
114  {"tone", test_tone, 0},
115  {"uart_loopback", test_uart_loopback, OPT_NOJIGTEST | OPT_NOALLTEST},
116  {"uart_send", test_uart_send, OPT_NOJIGTEST | OPT_NOALLTEST},
117  {"versioning", test_versioning, 0},
118 
119 
120  /* external tests */
121  {"commander", commander_tests_main, 0},
122  {"controllib", controllib_test_main, 0},
123 #ifndef __PX4_NUTTX
124  {"mavlink", mavlink_tests_main, 0},
125 #else
126  {"sf0x", sf0x_tests_main, 0},
127 #endif
128  {"uorb", uorb_tests_main, 0},
129 
130  {NULL, NULL, 0}
131 };
132 
133 #define NTESTS (sizeof(tests) / sizeof(tests[0]))
134 
135 static int
136 test_help(int argc, char *argv[])
137 {
138  unsigned i;
139 
140  printf("Available tests:\n");
141 
142  for (i = 0; tests[i].name; i++) {
143  printf(" %s\n", tests[i].name);
144  }
145 
146  return 0;
147 }
148 
149 static int
150 test_all(int argc, char *argv[])
151 {
152  return test_runner(OPT_NOALLTEST);
153 }
154 
155 static int
156 test_jig(int argc, char *argv[])
157 {
158  return test_runner(OPT_NOJIGTEST);
159 }
160 
161 static int
162 test_runner(unsigned option)
163 {
164  size_t i;
165  char *args[2] = {"all", NULL};
166  unsigned int failcount = 0;
167  unsigned int testcount = 0;
168  unsigned int passed[NTESTS];
169 
170  printf("\nRunning all tests...\n\n");
171 
172  for (i = 0; tests[i].name; i++) {
173  // Only run tests that are not excluded.
174  if (!(tests[i].options & option)) {
175  for (int j = 0; j < 80; j++) {
176  printf("-");
177  }
178 
179  printf("\n [%s] \t\tSTARTING TEST\n", tests[i].name);
180  fflush(stdout);
181 
182  /* Execute test */
183  if (tests[i].fn(1, args) != 0) {
184  fprintf(stderr, " [%s] \t\tFAIL\n", tests[i].name);
185  fflush(stderr);
186  failcount++;
187  passed[i] = 0;
188 
189  } else {
190  printf(" [%s] \t\tPASS\n", tests[i].name);
191  fflush(stdout);
192  passed[i] = 1;
193  }
194 
195  for (int j = 0; j < 80; j++) {
196  printf("-");
197  }
198 
199  testcount++;
200  printf("\n\n");
201  }
202  }
203 
204  // Print summary.
205  printf("\n");
206 
207  for (size_t j = 0; j < 80; j++) {
208  printf("#");
209  }
210 
211  printf("\n\n T E S T S U M M A R Y\n\n");
212 
213  if (failcount == 0) {
214  printf(" ______ __ __ ______ __ __ \n");
215  printf(" /\\ __ \\ /\\ \\ /\\ \\ /\\ __ \\ /\\ \\/ / \n");
216  printf(" \\ \\ __ \\ \\ \\ \\____ \\ \\ \\____ \\ \\ \\/\\ \\ \\ \\ _\"-. \n");
217  printf(" \\ \\_\\ \\_\\ \\ \\_____\\ \\ \\_____\\ \\ \\_____\\ \\ \\_\\ \\_\\ \n");
218  printf(" \\/_/\\/_/ \\/_____/ \\/_____/ \\/_____/ \\/_/\\/_/ \n");
219  printf("\n");
220  printf(" All tests passed (%d of %d)\n\n", testcount, testcount);
221 
222  } else {
223  printf(" ______ ______ __ __ \n");
224  printf(" /\\ ___\\ /\\ __ \\ /\\ \\ /\\ \\ \n");
225  printf(" \\ \\ __\\ \\ \\ __ \\ \\ \\ \\ \\ \\ \\__\n");
226  printf(" \\ \\_\\ \\ \\_\\ \\_\\ \\ \\_\\ \\ \\_____\\ \n");
227  printf(" \\/_/ \\/_/\\/_/ \\/_/ \\/_____/ \n");
228  printf("\n");
229  printf(" Some tests failed (%d of %d)\n\n", failcount, testcount);
230  }
231 
232  // Print failed tests.
233  if (failcount > 0) {
234  printf(" Failed tests:\n\n");
235  }
236 
237  for (size_t k = 0; k < i; k++) {
238  if (!passed[k] && !(tests[k].options & option)) {
239  printf(" [%s] to obtain details, please re-run with\n\t nsh> tests %s\n\n", tests[k].name, tests[k].name);
240  }
241  }
242 
243  fflush(stdout);
244  return (failcount > 0);
245 }
246 
247 __EXPORT int tests_main(int argc, char *argv[]);
248 
249 /**
250  * Executes system tests.
251  */
252 int tests_main(int argc, char *argv[])
253 {
254  if (argc < 2) {
255  printf("tests: missing test name - 'tests help' for a list of tests\n");
256  return 1;
257  }
258 
259  int tone_test_index = -1;
260  char *tone_test = {"tone"};
261  char *tone_fail[2] = {NULL, "2"};
262  char *tone_pass[2] = {NULL, "14"};
263 
264  // Identify the tone test index for later use.
265  for (size_t i = 0; tests[i].name; i++) {
266  if (*tone_test == *tests[i].name) {
267  tone_test_index = i;
268  }
269  }
270 
271  for (size_t i = 0; tests[i].name; i++) {
272  if (!strcmp(tests[i].name, argv[1])) {
273  if (tests[i].fn(argc - 1, argv + 1) == 0) {
274  if (tone_test_index != -1) {
275  tests[tone_test_index].fn(2, tone_pass); // Play a notification.
276  }
277 
278  printf("%s PASSED\n", tests[i].name);
279  return 0;
280 
281  } else {
282  if (tone_test_index != -1) {
283  tests[tone_test_index].fn(2, tone_fail); // Play an error notification.
284  }
285 
286  printf("%s FAILED\n", tests[i].name);
287  return -1;
288  }
289  }
290  }
291 
292  printf("tests: no test called '%s' - 'tests help' for a list of tests\n", argv[1]);
293  return 1;
294 }
static int test_help(int argc, char *argv[])
Definition: tests_main.c:136
int test_time(int argc, char *argv[])
Definition: test_time.c:78
int test_IntrusiveQueue(int argc, char *argv[])
int test_microbench_hrt(int argc, char *argv[])
int test_ppm(int argc, char *argv[])
Definition: test_ppm.cpp:50
int test_mixer(int argc, char *argv[])
int test_bezierQuad(int argc, char *argv[])
unsigned options
Definition: tests_main.c:60
int test_tone(int argc, char *argv[])
Definition: test_tone.cpp:49
int test_microbench_math(int argc, char *argv[])
int test_smooth_z(int argc, char *argv[])
int test_dataman(int argc, char *argv[])
Definition: test_dataman.c:159
Definition: I2C.hpp:51
int test_conv(int argc, char *argv[])
Definition: test_conv.cpp:56
int test_autodeclination(int argc, char *argv[])
#define NTESTS
Definition: tests_main.c:133
int test_uart_loopback(int argc, char *argv[])
int test_sleep(int argc, char *argv[])
Definition: test_sleep.c:55
__EXPORT int uorb_tests_main(int argc, char *argv[])
#define OPT_NOHELP
Definition: tests_main.c:61
int test_matrix(int argc, char *argv[])
int test_ppm_loopback(int argc, char *argv[])
int test_versioning(int argc, char *argv[])
int test_rc(int argc, char *argv[])
Definition: test_rc.c:61
__EXPORT int sf0x_tests_main(int argc, char *argv[])
int test_List(int argc, char *argv[])
int test_mathlib(int argc, char *argv[])
int test_mount(int argc, char *argv[])
Definition: test_mount.c:60
int test_jig_voltages(int argc, char *argv[])
int test_perf(int argc, char *argv[])
Definition: test_perf.c:47
static int test_all(int argc, char *argv[])
Definition: tests_main.c:150
int test_uart_send(int argc, char *argv[])
int test_param(int argc, char *argv[])
Definition: test_param.c:49
const struct @146 tests[]
int test_bson(int argc, char *argv[])
Definition: test_bson.cpp:262
Tests declaration file.
int test_uart_console(int argc, char *argv[])
static int test_jig(int argc, char *argv[])
Definition: tests_main.c:156
int(* fn)(int argc, char *argv[])
Definition: tests_main.c:59
__EXPORT int controllib_test_main(int argc, char *argv[])
int test_float(int argc, char *argv[])
int test_file2(int argc, char *argv[])
Definition: test_file2.c:173
const char * name
Definition: tests_main.c:58
int test_servo(int argc, char *argv[])
Definition: test_servo.c:57
__EXPORT int commander_tests_main(int argc, char *argv[])
__EXPORT int rc_tests_main(int argc, char *argv[])
int test_hrt(int argc, char *argv[])
Definition: test_hrt.cpp:45
int test_microbench_uorb(int argc, char *argv[])
int test_uart_baudchange(int argc, char *argv[])
int test_hott_telemetry(int argc, char *argv[])
static int test_runner(unsigned option)
Definition: tests_main.c:162
int test_led(int argc, char *argv[])
Definition: test_led.c:58
#define OPT_NOJIGTEST
Definition: tests_main.c:63
int test_microbench_matrix(int argc, char *argv[])
int test_parameters(int argc, char *argv[])
int test_uart_break(int argc, char *argv[])
#define OPT_NOALLTEST
Definition: tests_main.c:62
int test_file(int argc, char *argv[])
Definition: test_file.c:91
int test_int(int argc, char *argv[])
__EXPORT int tests_main(int argc, char *argv[])
Executes system tests.
Definition: tests_main.c:252
int test_search_min(int argc, char *argv[])
int test_adc(int argc, char *argv[])
Definition: test_adc.c:56