PX4 Firmware
PX4 Autopilot Software http://px4.io
unit_test.h
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (C) 2013 PX4 Development Team. All rights reserved.
4  * Author: Simon Wilks <sjwilks@gmail.com>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in
14  * the documentation and/or other materials provided with the
15  * distribution.
16  * 3. Neither the name PX4 nor the names of its contributors may be
17  * used to endorse or promote products derived from this software
18  * without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  *
33  ****************************************************************************/
34 
35 #ifndef UNIT_TEST_H_
36 #define UNIT_TEST_H_
37 
38 #include <px4_platform_common/log.h>
39 
40 #define ut_declare_test_c(test_function, test_class) \
41  extern "C" { \
42  int test_function(int argc, char *argv[]); \
43  int test_function(int argc, char *argv[]) \
44  { \
45  test_class* test = new test_class(); \
46  bool success = test->run_tests(); \
47  test->print_results(); \
48  delete test; \
49  return success ? 0 : -1; \
50  } \
51  }
52 
53 /// @brief Base class to be used for unit tests.
55 {
56 public:
57 
58  UnitTest() = default;
59  virtual ~UnitTest() = default;
60 
61  /// @brief Override to run your unit tests. Unit tests should be called using ut_run_test macro.
62  /// @return true: all unit tests succeeded, false: one or more unit tests failed
63  virtual bool run_tests(void) = 0;
64 
65  /// @brief Prints results from running of unit tests.
67  {
68  if (_tests_failed) {
69  PX4_ERR("SOME TESTS FAILED");
70 
71  } else {
72  PX4_INFO("ALL TESTS PASSED");
73  }
74 
75  PX4_INFO(" Tests passed : %d", _tests_passed);
76  PX4_INFO(" Tests failed : %d", _tests_failed);
77  PX4_INFO(" Tested assertions : %d", _assertions);
78  }
79 
80 
81 /// @brief Macro to create a function which will run a unit test class and print results.
82 #define ut_declare_test(test_function, test_class) \
83  bool test_function(void) \
84  { \
85  test_class* test = new test_class(); \
86  bool success = test->run_tests(); \
87  test->print_results(); \
88  delete test; \
89  return success; \
90  }
91 
92 protected:
93 
94 /// @brief Runs a single unit test. Unit tests must have the function signature of bool test(void). The unit
95 /// test should return true if it succeeded, false for fail.
96 #define ut_run_test(test) \
97  do { \
98  PX4_INFO("RUNNING TEST: %s", #test); \
99  _tests_run++; \
100  _init(); \
101  if (!test()) { \
102  PX4_ERR("TEST FAILED: %s", #test); \
103  _tests_failed++; \
104  } else { \
105  PX4_INFO("TEST PASSED: %s", #test); \
106  _tests_passed++; \
107  } \
108  _cleanup(); \
109  printf("\n"); \
110  } while (0)
111 
112 /// @brief Used to assert a value within a unit test.
113 #define ut_assert(message, test) \
114  do { \
115  if (!(test)) { \
116  _print_assert(message, #test, __FILE__, __LINE__); \
117  return false; \
118  } else { \
119  _assertions++; \
120  } \
121  } while (0)
122 
123 /// @brief Used to assert a value within a unit test.
124 #define ut_test(test) ut_assert("test", test)
125 
126 /// @brief To assert specifically to true.
127 #define ut_assert_true(test) \
128  do { \
129  if ((test) != true) { \
130  _print_assert("result not true", #test, __FILE__, __LINE__); \
131  return false; \
132  } else { \
133  _assertions++; \
134  } \
135  } while (0)
136 
137 /// @brief To assert specifically to true.
138 #define ut_assert_false(test) \
139  do { \
140  if ((test) != false) { \
141  _print_assert("result not false", #test, __FILE__, __LINE__); \
142  return false; \
143  } else { \
144  _assertions++; \
145  } \
146  } while (0)
147 
148 /// @brief Used to compare two integer values within a unit test. If possible use ut_compare instead of ut_assert
149 /// since it will give you better error reporting of the actual values being compared.
150 #define ut_compare(message, v1, v2) \
151  do { \
152  int _v1 = v1; \
153  int _v2 = v2; \
154  if (_v1 != _v2) { \
155  _print_compare(message, #v1, _v1, #v2, _v2, __FILE__, __LINE__); \
156  return false; \
157  } else { \
158  _assertions++; \
159  } \
160  } while (0)
161 
162 /// @brief Used to compare two float values within a unit test. If possible use ut_compare_float instead of ut_assert
163 /// since it will give you better error reporting of the actual values being compared.
164 #define ut_compare_float(message, v1, v2, precision) \
165  do { \
166  int _p = powf(10.0f, precision); \
167  int _v1 = (int)(v1 * _p + 0.5f); \
168  int _v2 = (int)(v2 * _p + 0.5f); \
169  if (_v1 != _v2) { \
170  _print_compare(message, #v1, _v1, #v2, _v2, __FILE__, __LINE__); \
171  return false; \
172  } else { \
173  _assertions++; \
174  } \
175  } while (0)
176 
177 /// @brief Used to compare two integer values within a unit test. If possible use ut_less_than instead of ut_assert
178 /// since it will give you better error reporting of the actual values being compared.
179 #define ut_less_than(message, v1_smaller, v2_bigger) \
180  do { \
181  int _v1 = v1_smaller; \
182  int _v2 = v2_bigger; \
183  if (!(_v1 < _v2)) { \
184  _print_compare(message, #v1_smaller, _v1, #v2_bigger, _v2, __FILE__, __LINE__); \
185  return false; \
186  } else { \
187  _assertions++; \
188  } \
189  } while (0)
190 
191  virtual void _init(void) {} ///< Run before each unit test. Override to provide custom behavior.
192  virtual void _cleanup(void) {} ///< Run after each unit test. Override to provide custom behavior.
193 
194  void _print_assert(const char *msg, const char *test, const char *file, int line)
195  {
196  PX4_ERR("Assertion failed: %s - %s (%s:%d)", msg, test, file, line);
197  }
198 
199  void _print_compare(const char *msg, const char *v1_text, int v1, const char *v2_text, int v2, const char *file,
200  int line)
201  {
202  PX4_ERR("Compare failed: %s - (%s:%d) (%s:%d) (%s:%d)", msg, v1_text, v1, v2_text, v2, file, line);
203  }
204 
205  int _tests_run{0}; ///< The number of individual unit tests run
206  int _tests_failed{0}; ///< The number of unit tests which failed
207  int _tests_passed{0}; ///< The number of unit tests which passed
208  int _assertions{0}; ///< Total number of assertions tested by all unit tests
209 };
210 
211 #endif /* UNIT_TEST_H_ */
Definition: I2C.hpp:51
virtual void _init(void)
Run before each unit test. Override to provide custom behavior.
Definition: unit_test.h:191
void _print_assert(const char *msg, const char *test, const char *file, int line)
Definition: unit_test.h:194
Base class to be used for unit tests.
Definition: unit_test.h:54
static char msg[NUM_MSG][CONFIG_USART1_TXBUFSIZE]
Definition: px4io.c:89
virtual void _cleanup(void)
Run after each unit test. Override to provide custom behavior.
Definition: unit_test.h:192
void _print_compare(const char *msg, const char *v1_text, int v1, const char *v2_text, int v2, const char *file, int line)
Definition: unit_test.h:199
void test(enum LPS25H_BUS busid)
Perform some basic functional tests on the driver; make sure we can collect data from the sensor in p...
Definition: lps25h.cpp:792
void print_results()
Prints results from running of unit tests.
Definition: unit_test.h:66
struct @83::@85::@87 file