Test.h
Go to the documentation of this file.
1 /* ----------------------------------------------------------------------------
2 
3  * GTSAM Copyright 2010, Georgia Tech Research Corporation,
4  * Atlanta, Georgia 30332-0415
5  * All Rights Reserved
6  * Authors: Frank Dellaert, et al. (see THANKS for the full author list)
7 
8  * See LICENSE for the license information
9 
10  * -------------------------------------------------------------------------- */
11 
13 //
14 // TEST.H
15 //
16 // This file contains the Test class along with the macros which make effective
17 // in the harness.
18 //
20 
21 #ifndef TEST_H
22 #define TEST_H
23 
24 
25 #include <cmath>
26 #include <boost/lexical_cast.hpp>
27 
28 class TestResult;
29 
30 class Test
31 {
32 public:
33  Test (const std::string& testName);
34  Test (const std::string& testName, const std::string& filename, long lineNumber, bool safeCheck);
35  virtual ~Test() {};
36 
37  virtual void run (TestResult& result) = 0;
38 
39 
40  void setNext(Test *test);
41  Test *getNext () const;
42  std::string getName() const {return name_;}
43  std::string getFilename() const {return filename_;}
44  long getLineNumber() const {return lineNumber_;}
45  bool safe() const {return safeCheck_;}
46 
47 protected:
48 
49  bool check (long expected, long actual, TestResult& result, const std::string& fileName, long lineNumber);
50  bool check (const std::string& expected, const std::string& actual, TestResult& result, const std::string& fileName, long lineNumber);
51 
52  std::string name_;
54  std::string filename_;
55  long lineNumber_;
56  bool safeCheck_;
57 
58 };
59 
63 #define TEST(testGroup, testName)\
64  class testGroup##testName##Test : public Test \
65  { public: testGroup##testName##Test () : Test (#testName "Test", __FILE__, __LINE__, true) {} \
66  virtual ~testGroup##testName##Test () {};\
67  void run (TestResult& result_) override;} \
68  testGroup##testName##Instance; \
69  void testGroup##testName##Test::run (TestResult& result_)
70 
74 #define FRIEND_TEST(testGroup, testName) \
75  friend class testGroup##testName##Test;
76 
81 #define TEST_UNSAFE(testGroup, testName)\
82  class testGroup##testName##Test : public Test \
83  { public: testGroup##testName##Test () : Test (#testName "Test", __FILE__, __LINE__, false) {} \
84  virtual ~testGroup##testName##Test () {};\
85  void run (TestResult& result_) override;} \
86  testGroup##testName##Instance; \
87  void testGroup##testName##Test::run (TestResult& result_)
88 
92 #define TEST_DISABLED(testGroup, testName)\
93  void testGroup##testName##Test(TestResult& result_, const std::string& name_)
94 
95 /*
96  * Convention for tests:
97  * - "EXPECT" is a test that will not end execution of the series of tests
98  * - Otherwise, upon a failure, the test will end
99  *
100  * Usage:
101  * EXPECT is useful when checking several different parts of an condition so
102  * that a failure of one check won't hide another failure.
103  *
104  * Note: Exception tests are not available in a EXPECT form, as exceptions rarely
105  * fit the criteria of an assertion that does not need to be true to continue
106  */
107 
108 /* True ASSERTs: tests end at first failure */
109 #define CHECK(condition)\
110 { if (!(condition)) \
111 { result_.addFailure (Failure (name_, __FILE__,__LINE__, #condition)); return; } }
112 
113 #define THROWS_EXCEPTION(condition)\
114 { try { condition; \
115  result_.addFailure (Failure (name_, __FILE__,__LINE__, std::string("Didn't throw: ") + boost::lexical_cast<std::string>(#condition))); \
116  return; } \
117  catch (...) {} }
118 
119 #define CHECK_EXCEPTION(condition, exception_name)\
120 { try { condition; \
121  result_.addFailure (Failure (name_, __FILE__,__LINE__, std::string("Didn't throw: ") + boost::lexical_cast<std::string>(#condition))); \
122  return; } \
123  catch (exception_name&) {} \
124  catch (...) { \
125  result_.addFailure (Failure (name_, __FILE__,__LINE__, std::string("Wrong exception: ") + boost::lexical_cast<std::string>(#condition) + boost::lexical_cast<std::string>(", expected: ") + boost::lexical_cast<std::string>(#exception_name))); \
126  return; } }
127 
128 #define EQUALITY(expected,actual)\
129  { if (!assert_equal(expected,actual)) \
130  result_.addFailure(Failure(name_, __FILE__, __LINE__, #expected, #actual)); }
131 
132 #define CHECK_EQUAL(expected,actual)\
133 { if ((expected) == (actual)) return; result_.addFailure(Failure(name_, __FILE__, __LINE__, boost::lexical_cast<std::string>(expected), boost::lexical_cast<std::string>(actual))); }
134 
135 #define LONGS_EQUAL(expected,actual)\
136 { long actualTemp = actual; \
137  long expectedTemp = expected; \
138  if ((expectedTemp) != (actualTemp)) \
139 { result_.addFailure (Failure (name_, __FILE__, __LINE__, boost::lexical_cast<std::string>(expectedTemp), \
140 boost::lexical_cast<std::string>(actualTemp))); return; } }
141 
142 #define DOUBLES_EQUAL(expected,actual,threshold)\
143 { double actualTemp = actual; \
144  double expectedTemp = expected; \
145  if (!std::isfinite(actualTemp) || !std::isfinite(expectedTemp) || fabs ((expectedTemp)-(actualTemp)) > threshold) \
146 { result_.addFailure (Failure (name_, __FILE__, __LINE__, \
147 boost::lexical_cast<std::string>((double)expectedTemp), boost::lexical_cast<std::string>((double)actualTemp))); return; } }
148 
149 
150 /* EXPECTs: tests will continue running after a failure */
151 #define EXPECT(condition)\
152 { if (!(condition)) \
153 { result_.addFailure (Failure (name_, __FILE__,__LINE__, #condition)); } }
154 
155 #define EXPECT_LONGS_EQUAL(expected,actual)\
156 { long actualTemp = actual; \
157  long expectedTemp = expected; \
158  if ((expectedTemp) != (actualTemp)) \
159 { result_.addFailure (Failure (name_, __FILE__, __LINE__, boost::lexical_cast<std::string>(expectedTemp), \
160 boost::lexical_cast<std::string>(actualTemp))); } }
161 
162 #define EXPECT_DOUBLES_EQUAL(expected,actual,threshold)\
163 { double actualTemp = actual; \
164  double expectedTemp = expected; \
165  if (!std::isfinite(actualTemp) || !std::isfinite(expectedTemp) || fabs ((expectedTemp)-(actualTemp)) > threshold) \
166 { result_.addFailure (Failure (name_, __FILE__, __LINE__, \
167 boost::lexical_cast<std::string>((double)expectedTemp), boost::lexical_cast<std::string>((double)actualTemp))); } }
168 
169 
170 #define FAIL(text) \
171 { result_.addFailure (Failure (name_, __FILE__, __LINE__,(text))); return; }
172 
173 
174 
175 #endif
std::string getName() const
Definition: Test.h:42
Matrix expected
Definition: testMatrix.cpp:974
bool check(long expected, long actual, TestResult &result, const std::string &fileName, long lineNumber)
Definition: Test.cpp:43
long getLineNumber() const
Definition: Test.h:44
bool safe() const
Definition: Test.h:45
Definition: test.py:1
Definition: Test.h:30
virtual void run(TestResult &result)=0
Test * next_
Definition: Test.h:53
std::string getFilename() const
Definition: Test.h:43
Values result
Test(const std::string &testName)
Definition: Test.cpp:20
virtual ~Test()
Definition: Test.h:35
long lineNumber_
Definition: Test.h:55
void setNext(Test *test)
Definition: Test.cpp:38
Test * getNext() const
Definition: Test.cpp:33
std::string name_
Definition: Test.h:52
std::string filename_
Definition: Test.h:54
bool safeCheck_
This is the line line number of the test, rather than the a single check.
Definition: Test.h:56


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:46:02