Pothos  0.3.3-g32d3017c
The Pothos dataflow programming software suite
 All Classes Namespaces Files Functions Variables Typedefs Friends Macros
Testing.hpp
Go to the documentation of this file.
1 
13 #pragma once
14 #include <Pothos/Config.hpp>
15 #include <Pothos/Plugin.hpp>
16 #include <Pothos/Callable.hpp>
17 #include <Pothos/Exception.hpp>
18 #include <memory>
19 #include <string>
20 
38 #define POTHOS_TEST_BLOCK(path, name) \
39  POTHOS_STATIC_FIXTURE_DECL void name ## Runner(void); \
40  template <Pothos::Detail::InitFcn runner> \
41  struct name : Pothos::TestingBase \
42  { \
43  void runTestsImpl(void) \
44  { \
45  POTHOS_TEST_CHECKPOINT(); \
46  runner(); \
47  } \
48  }; \
49  pothos_static_block(name) \
50  { \
51  std::shared_ptr<Pothos::TestingBase> testObj(new name<name ## Runner>()); \
52  Pothos::PluginRegistry::add(Pothos::Plugin( \
53  Pothos::PluginPath(path).join(#name), Pothos::Object(testObj))); \
54  } \
55  POTHOS_STATIC_FIXTURE_DECL void name ## Runner(void)
56 
58 #define POTHOS_TEST_CHECKPOINT() \
59  Pothos::TestingBase::current().report("checkpoint", "", "", __LINE__, __FILE__)
60 
62 #define __POTHOS_TEST_STATEMENT(message, statement) \
63 { \
64  Pothos::TestingBase::current().report(message, #statement, "", __LINE__, __FILE__); \
65  try{statement;} \
66  catch(const std::string &ex){Pothos::TestingBase::current().report(message, #statement, ex, __LINE__, __FILE__);} \
67  catch(const Pothos::Exception &ex){Pothos::TestingBase::current().report(message, #statement, ex.displayText(), __LINE__, __FILE__);} \
68  catch(const std::exception &ex){Pothos::TestingBase::current().report(message, #statement, ex.what(), __LINE__, __FILE__);} \
69  catch(...){Pothos::TestingBase::current().report(message, #statement, "unknown", __LINE__, __FILE__);} \
70 }
71 
73 #define __POTHOS_TEST_ASSERT(message, statement) \
74 { \
75  __POTHOS_TEST_STATEMENT(message, if (statement) {} else throw std::string("statement \"" #statement "\" evaluated false");); \
76 }
77 
79 #define POTHOS_TEST_TRUE(statement) \
80 { \
81  __POTHOS_TEST_ASSERT("assert true " #statement, statement); \
82 }
83 
85 #define POTHOS_TEST_EQUAL(lhs, rhs) \
86 { \
87  __POTHOS_TEST_ASSERT( \
88  "assert equal " + Pothos::TestingBase::current().toString(lhs) + \
89  " == " + Pothos::TestingBase::current().toString(rhs), (lhs) == (rhs)); \
90 }
91 
93 #define POTHOS_TEST_CLOSE(lhs, rhs, tol) \
94 { \
95  __POTHOS_TEST_ASSERT( \
96  "assert close " + Pothos::TestingBase::current().toString(lhs) + \
97  " ~= " + Pothos::TestingBase::current().toString(rhs), (std::abs((lhs) - (rhs)) <= (tol))); \
98 }
99 
101 #define POTHOS_TEST_EQUALV(lhs, rhs) \
102 { \
103  POTHOS_TEST_EQUAL((lhs).size(), (rhs).size()); \
104  for (size_t i = 0; i < (lhs).size(); i++) \
105  { \
106  __POTHOS_TEST_ASSERT( \
107  "index " + Pothos::TestingBase::current().toString(i) + \
108  " asserts " + Pothos::TestingBase::current().toString((lhs)[i]) + \
109  " == " + Pothos::TestingBase::current().toString((rhs)[i]), (lhs)[i] == (rhs)[i]); \
110  } \
111 }
112 
114 #define POTHOS_TEST_EQUALA(lhs, rhs, size) \
115 { \
116  for (size_t i = 0; i < (size); i++) \
117  { \
118  __POTHOS_TEST_ASSERT( \
119  "index " + Pothos::TestingBase::current().toString(i) + \
120  " asserts " + Pothos::TestingBase::current().toString((lhs)[i]) + \
121  " == " + Pothos::TestingBase::current().toString((rhs)[i]), (lhs)[i] == (rhs)[i]); \
122  } \
123 }
124 
126 #define POTHOS_TEST_THROWS(statement, expectedException) \
127 { \
128  Pothos::TestingBase::current().report(#statement, #statement, "", __LINE__, __FILE__); \
129  __POTHOS_TEST_STATEMENT(#statement " must throw " #expectedException, \
130  try{statement;throw std::string("statement \"" #statement "\" did not throw");} \
131  catch(const expectedException &){}); \
132 }
133 
134 namespace Pothos {
135 
137 {
138  TestingBase(void);
139  ~TestingBase(void);
140  static TestingBase &current(void);
141  void runTests();
142  virtual void runTestsImpl() = 0;
143  void report(
144  const std::string &message,
145  const std::string &statement,
146  const std::string &error,
147  const int line,
148  const std::string &file);
149  template <typename T>
150  std::string toString(const T &v)
151  {
152  return Pothos::Object(v).toString();
153  }
154 };
155 
156 } //namespace Pothos
#define POTHOS_API
Definition: Config.hpp:41
std::string toString(void) const
Definition: Object.hpp:55
Definition: Testing.hpp:136
std::string toString(const T &v)
Definition: Testing.hpp:150