Firmware
unit_test.h
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_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 
55 {
56 public:
57 
58  UnitTest() = default;
59  virtual ~UnitTest() = default;
60 
63  virtual bool run_tests(void) = 0;
64 
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 
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 
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 
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 
124 #define ut_test(test) ut_assert("test", test)
125 
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 
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 
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 
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 
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) {}
192  virtual void _cleanup(void) {}
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};
206  int _tests_failed{0};
207  int _tests_passed{0};
208  int _assertions{0};
209 };
210 
211 #endif /* UNIT_TEST_H_ */
Definition: run_tests.py:1
Definition: I2C.hpp:51
Platform dependant logging/debug implementation.
Base class to be used for unit tests.
Definition: unit_test.h:54
virtual void _cleanup(void)
Run after each unit test. Override to provide custom behavior.
Definition: unit_test.h:192
void print_results()
Prints results from running of unit tests.
Definition: unit_test.h:66
virtual void _init(void)
Run before each unit test. Override to provide custom behavior.
Definition: unit_test.h:191