Hasty Badger
Small UI library (a branch of Turbo Badger)
 All Classes Namespaces Functions Variables Enumerations Enumerator Friends Groups Pages
tb_test.h
1 // ================================================================================
2 // == This file is a part of Turbo Badger. (C) 2011-2014, Emil SegerÃ¥s ==
3 // == See tb_core.h for more information. ==
4 // ================================================================================
5 
6 #ifndef TB_TEST_H
7 #define TB_TEST_H
8 
85 #include "tb_types.h"
86 #include "tb_linklist.h"
87 #include "tb_str.h"
88 #include <math.h>
89 #include <iostream>
90 
91 namespace tb {
92 
94 #define TB_TEST_VERBOSE 1
95 
96 #ifdef TB_UNIT_TESTING
97 
99 int TBRunTests(uint32_t settings = TB_TEST_VERBOSE);
100 
102 #define TB_VERIFY(expr) { fail_line_nr = __LINE__; fail_file = __FILE__; if (!(expr)) { fail_text = (#expr); return; } }
103 
105 #define TB_VERIFY_FLOAT(val, ref_val) { TB_VERIFY(fabs(ref_val - val) < 1.0E-5); }
106 
108 #define TB_VERIFY_STR(str1, str2) do { \
109  if (strcmp((const char *)(str1), (const char *)(str2))) { \
110  std::cerr << "S1:" << (const char *)(str1) << "\n"; \
111  std::cerr << "S2:" << (const char *)(str2) << "\n"; \
112  } \
113  TB_VERIFY(strcmp((const char *)(str1), (const char *)(str2)) == 0); \
114  } while (0)
115 
117 #define TB_PASS() return;
118 
120 #define TB_FAIL(error) { fail_line_nr = __LINE__; fail_file = __FILE__; fail_text = error; return; }
121 
123 #define TB_TEST_FILE(filename) tb_get_test_file_name(__FILE__, filename)
124 
126 class TBCall : public TBLinkOf<TBCall>
127 {
128 public:
130  virtual const char *name() = 0;
132  virtual void exec() = 0;
133 };
134 
136 class TBTestGroup
137 {
138 public:
139  TBTestGroup(const char *name);
140  bool IsSpecialTest(TBCall *call) const { return !call->linklist; }
141 public:
142  const char *name;
143  TBCall *setup;
144  TBCall *cleanup;
145  TBCall *init;
146  TBCall *shutdown;
147  TBLinkListOf<TBCall> calls;
148  TBTestGroup *next_test_group;
149 };
150 
152 class TBRegisterCall
153 {
154 public:
155  TBRegisterCall(TBTestGroup *test, TBCall *call);
156  ~TBRegisterCall();
157 private:
158  TBCall *call;
159 };
160 
161 #define TB_TEST_GROUP(name) \
162  namespace testgroup_##name \
163  { \
164  class TheGroup : public TBTestGroup \
165  { \
166  public: \
167  TheGroup() : TBTestGroup(#name) {} \
168  }; \
169  TheGroup the_group_obj; \
170  int force_link = 0; \
171  } \
172  namespace testgroup_##name \
173 
174 #define TB_TEST(callname) \
175  class CallObj##callname : public TBCall \
176  { \
177  public: \
178  virtual const char *name(); \
179  virtual void exec(); \
180  }; \
181  CallObj##callname callname; \
182  TBRegisterCall callname##reg(&the_group_obj, &callname); \
183  const char *CallObj##callname::name() { return #callname; } \
184  void CallObj##callname::exec()
185 
190 #define TB_FORCE_LINK_TEST_GROUP(name) \
191  namespace testgroup_##name { void force_link_call() { extern int force_link; force_link = 1; }}
192 
193 TBStr tb_get_test_file_name(const char *testpath, const char *filename);
194 
195 // Internal globals
196 extern uint32_t test_settings;
197 extern int fail_line_nr;
198 extern const char *fail_file;
199 extern const char *fail_text;
200 
201 #else
202 
203 inline int TBRunTests(uint32_t settings = TB_TEST_VERBOSE) { return 0; }
204 
205 #endif
206 
207 } // namespace tb
208 
209 #endif // TB_TEST_H