APM:Libraries
AP_Test.h
Go to the documentation of this file.
1 /*
2  This program is free software: you can redistribute it and/or modify
3  it under the terms of the GNU General Public License as published by
4  the Free Software Foundation, either version 3 of the License, or
5  (at your option) any later version.
6 
7  This program is distributed in the hope that it will be useful,
8  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  GNU General Public License for more details.
11 
12  You should have received a copy of the GNU General Public License
13  along with this program. If not, see <http://www.gnu.org/licenses/>.
14  */
15 
16 //
39 
42 class Test
43 {
44 public:
51  Test(const char *name);
52 
55  ~Test();
56 
63  void require(bool expr, const char *source);
64 
67  static void report();
68 
69 private:
70  const char *_name;
71  bool _fail;
72  static int16_t _passed;
73  static int16_t _failed;
74 };
75 
78 Test::Test(const char *name) :
79  _name(name),
80  _fail(false)
81 {
82 }
83 
87 {
88  Serial.printf("%s: %s\n", _fail ? "FAILED" : "passed", _name);
89  if (_fail) {
90  _failed++;
91  } else {
92  _passed++;
93  }
94 }
95 
98 void
99 Test::require(bool expr, const char *source)
100 {
101  if (!expr) {
102  _fail = true;
103  Serial.printf("%s: fail: %s\n", _name, source);
104  }
105 }
106 
109 void
111 {
112  Serial.printf("\n%d passed %d failed\n", _passed, _failed);
113 }
114 
115 int16_t Test::_passed = 0;
116 int16_t Test::_failed = 0;
117 
126 #define TEST(name) Test _test(# name)
127 
134 #define REQUIRE(expr) _test.require(expr, # expr)
135 
const char * _name
name of the current test
Definition: AP_Test.h:70
Test(const char *name)
Definition: AP_Test.h:78
Definition: AP_Test.h:42
const char * name
Definition: BusTest.cpp:11
static int16_t _failed
global fail count
Definition: AP_Test.h:73
~Test()
Definition: AP_Test.h:86
bool _fail
set if any ::require calls indicate the test failed
Definition: AP_Test.h:71
static void report()
Definition: AP_Test.h:110
static int16_t _passed
global pass count
Definition: AP_Test.h:72
void require(bool expr, const char *source)
Definition: AP_Test.h:99