libcopp  2.2.0
test_manager.h
Go to the documentation of this file.
1 /*
2  * test_manager.h
3  *
4  * Created on: 2014年3月11日
5  * Author: owent
6  *
7  * Released under the MIT license
8  */
9 
10 #ifndef TEST_MANAGER_H_
11 #define TEST_MANAGER_H_
12 
13 #pragma once
14 
15 #include <stdint.h>
16 #include <ctime>
17 #include <map>
18 #include <string>
19 #include <type_traits>
20 #include <vector>
21 
22 #ifdef __cpp_impl_three_way_comparison
23 # include <compare>
24 #endif
25 
26 #include "cli/shell_font.h"
27 
28 #include "test_case_base.h"
29 
30 #if (defined(__cplusplus) && __cplusplus >= 201103L) || (defined(_MSC_VER) && _MSC_VER >= 1600)
31 
32 # include <unordered_map>
33 # include <unordered_set>
34 # define UTILS_TEST_ENV_AUTO_MAP(...) std::unordered_map<__VA_ARGS__>
35 # define UTILS_TEST_ENV_AUTO_SET(...) std::unordered_set<__VA_ARGS__>
36 # define UTILS_TEST_ENV_AUTO_UNORDERED 1
37 #else
38 
39 # include <map>
40 # include <set>
41 # define UTILS_TEST_ENV_AUTO_MAP(...) std::map<__VA_ARGS__>
42 # define UTILS_TEST_ENV_AUTO_SET(...) std::set<__VA_ARGS__>
43 
44 #endif
45 
49 class test_manager {
50  public:
54  typedef std::vector<std::pair<std::string, case_ptr_type> > test_type;
55  typedef std::vector<std::pair<std::string, on_start_ptr_type> > event_on_start_type;
56  typedef std::vector<std::pair<std::string, on_exit_ptr_type> > event_on_exit_type;
57  typedef UTILS_TEST_ENV_AUTO_MAP(std::string, test_type) test_data_type;
58 
59  public:
60  test_manager();
61  virtual ~test_manager();
62 
63  void append_test_case(const std::string &test_name, const std::string &case_name, case_ptr_type);
64  void append_event_on_start(const std::string &event_name, on_start_ptr_type);
65  void append_event_on_exit(const std::string &event_name, on_exit_ptr_type);
66 
67  int run_event_on_start();
68  int run_event_on_exit();
69  int run();
70 
71  void set_cases(const std::vector<std::string> &case_names);
72 
73  static test_manager &me();
74 
75  static std::string get_expire_time(clock_t begin, clock_t end);
76 
77 #ifdef UTILS_TEST_MACRO_TEST_ENABLE_BOOST_TEST
78  static boost::unit_test::test_suite *&test_suit();
79 #endif
80 
82  const char *str_;
83  pick_param_str_t(const char *in);
84  pick_param_str_t(const std::string &in);
85 
86  bool operator==(const pick_param_str_t &other) const;
87 #ifdef __cpp_impl_three_way_comparison
88  std::strong_ordering operator<=>(const pick_param_str_t &other) const;
89 #else
90  bool operator!=(const pick_param_str_t &other) const;
91  bool operator>=(const pick_param_str_t &other) const;
92  bool operator>(const pick_param_str_t &other) const;
93  bool operator<=(const pick_param_str_t &other) const;
94  bool operator<(const pick_param_str_t &other) const;
95 #endif
96  };
97 
98  template <typename TL, typename TR, bool has_pointer, bool has_integer, bool all_integer>
99  struct pick_param;
100 
101  // compare pointer with integer
102  template <typename TL, typename TR>
103  struct pick_param<TL, TR, true, true, false> {
104  template <typename T>
105  uintptr_t operator()(const T &t) {
106  return (uintptr_t)(t);
107  }
108  };
109 
110  // compare integer with integer, all converted to int64_t or uint64_t
111  template <typename TL, typename TR>
112  struct pick_param<TL, TR, false, true, true> {
113  // uint64_t operator()(const uint64_t &t) { return static_cast<uint64_t>(t); }
114 
115  template <typename T>
116  int64_t operator()(const T &t) {
117  return static_cast<int64_t>(t);
118  }
119  };
120 
121  template <typename TL, typename TR, bool has_pointer, bool has_integer, bool all_integer>
122  struct pick_param {
123  pick_param_str_t operator()(const char *t) { return pick_param_str_t(t); }
124  pick_param_str_t operator()(const std::string &t) { return pick_param_str_t(t); }
125 
126  template <typename T>
127  const T &operator()(const T &t) {
128  return t;
129  }
130  };
131 
132  // expect functions
133  template <typename TL, typename TR>
134  bool expect_eq(const TL &l, const TR &r, const char *lexpr, const char *rexpr, const char *file, size_t line) {
135  pick_param<TL, TR, std::is_pointer<TL>::value || std::is_pointer<TR>::value,
136  std::is_integral<TL>::value || std::is_integral<TR>::value,
137  std::is_integral<TL>::value && std::is_integral<TR>::value>
138  pp;
139  if (pp(l) == pp(r)) {
141  return true;
142  } else {
144  util::cli::shell_stream ss(std::cout);
145  ss() << util::cli::shell_font_style::SHELL_FONT_COLOR_RED << "FAILED => " << file << ":" << line << std::endl
146  << "Expected: " << lexpr << " == " << rexpr << std::endl
147  << lexpr << ": " << l << std::endl
148  << rexpr << ": " << r << std::endl;
149 
150  return false;
151  }
152  }
153 
154  template <typename TL, typename TR>
155  bool expect_ne(const TL &l, const TR &r, const char *lexpr, const char *rexpr, const char *file, size_t line) {
156  pick_param<TL, TR, std::is_pointer<TL>::value || std::is_pointer<TR>::value,
157  std::is_integral<TL>::value || std::is_integral<TR>::value,
158  std::is_integral<TL>::value && std::is_integral<TR>::value>
159  pp;
160 
161  if (pp(l) != pp(r)) {
163  return true;
164  } else {
166  util::cli::shell_stream ss(std::cout);
167  ss() << util::cli::shell_font_style::SHELL_FONT_COLOR_RED << "FAILED => " << file << ":" << line << std::endl
168  << "Expected: " << lexpr << " != " << rexpr << std::endl
169  << lexpr << ": " << l << std::endl
170  << rexpr << ": " << r << std::endl;
171 
172  return false;
173  }
174  }
175 
176  template <typename TL, typename TR>
177  bool expect_lt(const TL &l, const TR &r, const char *lexpr, const char *rexpr, const char *file, size_t line) {
178  pick_param<TL, TR, std::is_pointer<TL>::value || std::is_pointer<TR>::value,
179  std::is_integral<TL>::value || std::is_integral<TR>::value,
180  std::is_integral<TL>::value && std::is_integral<TR>::value>
181  pp;
182 
183  if (pp(l) < pp(r)) {
185  return true;
186  } else {
188  util::cli::shell_stream ss(std::cout);
189  ss() << util::cli::shell_font_style::SHELL_FONT_COLOR_RED << "FAILED => " << file << ":" << line << std::endl
190  << "Expected: " << lexpr << " < " << rexpr << std::endl
191  << lexpr << ": " << l << std::endl
192  << rexpr << ": " << r << std::endl;
193 
194  return false;
195  }
196  }
197 
198  template <typename TL, typename TR>
199  bool expect_le(const TL &l, const TR &r, const char *lexpr, const char *rexpr, const char *file, size_t line) {
200  pick_param<TL, TR, std::is_pointer<TL>::value || std::is_pointer<TR>::value,
201  std::is_integral<TL>::value || std::is_integral<TR>::value,
202  std::is_integral<TL>::value && std::is_integral<TR>::value>
203  pp;
204 
205  if (pp(l) <= pp(r)) {
207  return true;
208  } else {
210  util::cli::shell_stream ss(std::cout);
211  ss() << util::cli::shell_font_style::SHELL_FONT_COLOR_RED << "FAILED => " << file << ":" << line << std::endl
212  << "Expected: " << lexpr << " <= " << rexpr << std::endl
213  << lexpr << ": " << l << std::endl
214  << rexpr << ": " << r << std::endl;
215 
216  return false;
217  }
218  }
219 
220  template <typename TL, typename TR>
221  bool expect_gt(const TL &l, const TR &r, const char *lexpr, const char *rexpr, const char *file, size_t line) {
222  pick_param<TL, TR, std::is_pointer<TL>::value || std::is_pointer<TR>::value,
223  std::is_integral<TL>::value || std::is_integral<TR>::value,
224  std::is_integral<TL>::value && std::is_integral<TR>::value>
225  pp;
226 
227  if (pp(l) > pp(r)) {
229  return true;
230  } else {
232  util::cli::shell_stream ss(std::cout);
233  ss() << util::cli::shell_font_style::SHELL_FONT_COLOR_RED << "FAILED => " << file << ":" << line << std::endl
234  << "Expected: " << lexpr << " > " << rexpr << std::endl
235  << lexpr << ": " << l << std::endl
236  << rexpr << ": " << r << std::endl;
237 
238  return false;
239  }
240  }
241 
242  template <typename TL, typename TR>
243  bool expect_ge(const TL &l, const TR &r, const char *lexpr, const char *rexpr, const char *file, size_t line) {
244  pick_param<TL, TR, std::is_pointer<TL>::value || std::is_pointer<TR>::value,
245  std::is_integral<TL>::value || std::is_integral<TR>::value,
246  std::is_integral<TL>::value && std::is_integral<TR>::value>
247  pp;
248 
249  if (pp(l) >= pp(r)) {
251  return true;
252  } else {
254  util::cli::shell_stream ss(std::cout);
255  ss() << util::cli::shell_font_style::SHELL_FONT_COLOR_RED << "FAILED => " << file << ":" << line << std::endl
256  << "Expected: " << lexpr << " >= " << rexpr << std::endl
257  << lexpr << ": " << l << std::endl
258  << rexpr << ": " << r << std::endl;
259 
260  return false;
261  }
262  }
263 
264  template <typename TL>
265  bool expect_true(const TL &l, const char *expr, const char *file, size_t line) {
266  if (!!(l)) {
268  return true;
269  } else {
271  util::cli::shell_stream ss(std::cout);
272  ss() << util::cli::shell_font_style::SHELL_FONT_COLOR_RED << "FAILED => " << file << ":" << line << std::endl
273  << "Expected true: " << expr << std::endl
274  << expr << ": " << l << std::endl;
275 
276  return false;
277  }
278  }
279 
280  template <typename TL>
281  bool expect_false(const TL &l, const char *expr, const char *file, size_t line) {
282  if (!(l)) {
284  return true;
285  } else {
287  util::cli::shell_stream ss(std::cout);
288  ss() << util::cli::shell_font_style::SHELL_FONT_COLOR_RED << "FAILED => " << file << ":" << line << std::endl
289  << "Expected false: " << expr << std::endl
290  << expr << ": " << l << std::endl;
291 
292  return false;
293  }
294  }
295 
296  static void set_counter_ptr(int *success_counter_ptr, int *failed_counter_ptr);
297  static void inc_success_counter();
298  static void inc_failed_counter();
299 
300  private:
301  test_data_type tests_;
304  int success_;
305  int failed_;
306  UTILS_TEST_ENV_AUTO_SET(std::string) run_cases_;
307  UTILS_TEST_ENV_AUTO_SET(std::string) run_groups_;
308 };
309 
310 int run_event_on_start();
311 int run_event_on_exit();
312 int run_tests(int argc, char *argv[]);
313 
314 #endif /* TEST_MANAGER_H_ */
UTILS_TEST_ENV_AUTO_SET(std::string) run_groups_
static std::string get_expire_time(clock_t begin, clock_t end)
bool expect_lt(const TL &l, const TR &r, const char *lexpr, const char *rexpr, const char *file, size_t line)
Definition: test_manager.h:177
test_on_exit_base * on_exit_ptr_type
Definition: test_manager.h:53
std::vector< std::pair< std::string, case_ptr_type > > test_type
Definition: test_manager.h:54
void set_cases(const std::vector< std::string > &case_names)
static test_manager & me()
test_on_start_base * on_start_ptr_type
Definition: test_manager.h:52
bool expect_false(const TL &l, const char *expr, const char *file, size_t line)
Definition: test_manager.h:281
void append_event_on_start(const std::string &event_name, on_start_ptr_type)
std::vector< std::pair< std::string, on_start_ptr_type > > event_on_start_type
Definition: test_manager.h:55
event_on_exit_type evt_on_exits_
Definition: test_manager.h:303
bool expect_ne(const TL &l, const TR &r, const char *lexpr, const char *rexpr, const char *file, size_t line)
Definition: test_manager.h:155
test_case_base * case_ptr_type
Definition: test_manager.h:51
std::vector< std::pair< std::string, on_exit_ptr_type > > event_on_exit_type
Definition: test_manager.h:56
static void set_counter_ptr(int *success_counter_ptr, int *failed_counter_ptr)
bool expect_gt(const TL &l, const TR &r, const char *lexpr, const char *rexpr, const char *file, size_t line)
Definition: test_manager.h:221
event_on_start_type evt_on_starts_
Definition: test_manager.h:302
test_data_type tests_
Definition: test_manager.h:301
void append_event_on_exit(const std::string &event_name, on_exit_ptr_type)
bool expect_true(const TL &l, const char *expr, const char *file, size_t line)
Definition: test_manager.h:265
virtual ~test_manager()
int run_event_on_start()
static void inc_failed_counter()
UTILS_TEST_ENV_AUTO_SET(std::string) run_cases_
static void inc_success_counter()
int run_event_on_exit()
bool expect_le(const TL &l, const TR &r, const char *lexpr, const char *rexpr, const char *file, size_t line)
Definition: test_manager.h:199
typedef UTILS_TEST_ENV_AUTO_MAP(std::string, test_type) test_data_type
bool expect_eq(const TL &l, const TR &r, const char *lexpr, const char *rexpr, const char *file, size_t line)
Definition: test_manager.h:134
void append_test_case(const std::string &test_name, const std::string &case_name, case_ptr_type)
bool expect_ge(const TL &l, const TR &r, const char *lexpr, const char *rexpr, const char *file, size_t line)
Definition: test_manager.h:243
bool operator>=(const pick_param_str_t &other) const
bool operator!=(const pick_param_str_t &other) const
bool operator==(const pick_param_str_t &other) const
bool operator>(const pick_param_str_t &other) const
bool operator<=(const pick_param_str_t &other) const
bool operator<(const pick_param_str_t &other) const
const T & operator()(const T &t)
Definition: test_manager.h:127
pick_param_str_t operator()(const std::string &t)
Definition: test_manager.h:124
pick_param_str_t operator()(const char *t)
Definition: test_manager.h:123
int run_event_on_exit()
int run_event_on_start()
int run_tests(int argc, char *argv[])