Pothos  0.2.2-g1bdc7547
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 struct name : Pothos::TestingBase \
40 { \
41  void runTestsImpl(void); \
42  void runTestsImpl_(void); \
43 }; \
44 pothos_static_block(name) \
45 { \
46  std::shared_ptr<Pothos::TestingBase> testObj(new name()); \
47  Pothos::PluginRegistry::add(Pothos::Plugin( \
48  Pothos::PluginPath(path).join(#name), Pothos::Object(testObj))); \
49 } \
50 void name::runTestsImpl(void) \
51 { \
52  POTHOS_TEST_CHECKPOINT(); \
53  this->runTestsImpl_(); \
54 } \
55 void name::runTestsImpl_(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(#statement, statement) \
82 }
83 
85 #define POTHOS_TEST_EQUAL(lhs, rhs) \
86 { \
87  __POTHOS_TEST_ASSERT(Pothos::TestingBase::current().toString(lhs) + \
88  " == " + Pothos::TestingBase::current().toString(rhs), (lhs) == (rhs)); \
89 }
90 
92 #define POTHOS_TEST_EQUALV(lhs, rhs) \
93 { \
94  POTHOS_TEST_EQUAL((lhs).size(), (rhs).size()); \
95  for (size_t i = 0; i < (lhs).size(); i++) \
96  { \
97  __POTHOS_TEST_ASSERT( \
98  "index " + Pothos::TestingBase::current().toString(i) + \
99  " asserts " + Pothos::TestingBase::current().toString((lhs)[i]) + \
100  " == " + Pothos::TestingBase::current().toString((rhs)[i]), (lhs)[i] == (rhs)[i]); \
101  } \
102 }
103 
105 #define POTHOS_TEST_EQUALA(lhs, rhs, size) \
106 { \
107  for (size_t i = 0; i < (size); i++) \
108  { \
109  __POTHOS_TEST_ASSERT( \
110  "index " + Pothos::TestingBase::current().toString(i) + \
111  " asserts " + Pothos::TestingBase::current().toString((lhs)[i]) + \
112  " == " + Pothos::TestingBase::current().toString((rhs)[i]), (lhs)[i] == (rhs)[i]); \
113  } \
114 }
115 
117 #define POTHOS_TEST_THROWS(statement, expectedException) \
118 { \
119  Pothos::TestingBase::current().report(#statement, #statement, "", __LINE__, __FILE__); \
120  __POTHOS_TEST_STATEMENT(#statement " must throw " #expectedException, \
121  try{statement;throw std::string("statement \"" #statement "\" did not throw");} \
122  catch(const expectedException &){}); \
123 }
124 
125 namespace Pothos {
126 
128 {
129  TestingBase(void);
130  ~TestingBase(void);
131  static TestingBase &current(void);
132  void runTests();
133  virtual void runTestsImpl() = 0;
134  void report(
135  const std::string &message,
136  const std::string &statement,
137  const std::string &error,
138  const int line,
139  const std::string &file);
140  template <typename T>
141  std::string toString(const T &v)
142  {
143  return Pothos::Object(v).toString();
144  }
145 };
146 
147 } //namespace Pothos
#define POTHOS_API
Definition: Config.hpp:41
std::string toString(void) const
Definition: Object.hpp:55
Definition: Testing.hpp:127
std::string toString(const T &v)
Definition: Testing.hpp:141