libcopp  1.1.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 <ctime>
16 #include <map>
17 #include <stdint.h>
18 #include <string>
19 #include <type_traits>
20 #include <vector>
21 
22 
23 #include "cli/shell_font.h"
24 
25 #include "test_case_base.h"
26 
27 
28 #if (defined(__cplusplus) && __cplusplus >= 201103L) || (defined(_MSC_VER) && _MSC_VER >= 1600)
29 
30 #include <unordered_map>
31 #include <unordered_set>
32 #define UTILS_TEST_ENV_AUTO_MAP(...) std::unordered_map<__VA_ARGS__>
33 #define UTILS_TEST_ENV_AUTO_SET(...) std::unordered_set<__VA_ARGS__>
34 #define UTILS_TEST_ENV_AUTO_UNORDERED 1
35 #else
36 
37 #include <map>
38 #include <set>
39 #define UTILS_TEST_ENV_AUTO_MAP(...) std::map<__VA_ARGS__>
40 #define UTILS_TEST_ENV_AUTO_SET(...) std::set<__VA_ARGS__>
41 
42 #endif
43 
47 class test_manager {
48 public:
50  typedef std::vector<std::pair<std::string, case_ptr_type> > test_type;
51  typedef UTILS_TEST_ENV_AUTO_MAP(std::string, test_type) test_data_type;
52 
53 public:
54  test_manager();
55  virtual ~test_manager();
56 
57  void append(const std::string &test_name, const std::string &case_name, case_ptr_type);
58 
59  int run();
60 
61  void set_cases(const std::vector<std::string> &case_names);
62 
63  static test_manager &me();
64 
67 
68  static std::string get_expire_time(clock_t begin, clock_t end);
69 
70 #ifdef UTILS_TEST_MACRO_TEST_ENABLE_BOOST_TEST
71  static boost::unit_test::test_suite *&test_suit();
72 #endif
73 
75  const char *str_;
76  pick_param_str_t(const char *in);
77  pick_param_str_t(const std::string &in);
78 
79  bool operator==(const pick_param_str_t &other) const;
80  bool operator!=(const pick_param_str_t &other) const;
81  bool operator>=(const pick_param_str_t &other) const;
82  bool operator>(const pick_param_str_t &other) const;
83  bool operator<=(const pick_param_str_t &other) const;
84  bool operator<(const pick_param_str_t &other) const;
85  };
86 
87  template <typename TL, typename TR, bool has_pointer, bool has_integer, bool all_integer>
88  struct pick_param;
89 
90  // compare pointer with integer
91  template <typename TL, typename TR>
92  struct pick_param<TL, TR, true, true, false> {
93  template <typename T>
94  uintptr_t operator()(const T &t) {
95  return (uintptr_t)(t);
96  }
97  };
98 
99  // compare integer with integer, all converted to int64_t or uint64_t
100  template <typename TL, typename TR>
101  struct pick_param<TL, TR, false, true, true> {
102  // uint64_t operator()(const uint64_t &t) { return static_cast<uint64_t>(t); }
103 
104  template <typename T>
105  int64_t operator()(const T &t) {
106  return static_cast<int64_t>(t);
107  }
108  };
109 
110  template <typename TL, typename TR, bool has_pointer, bool has_integer, bool all_integer>
111  struct pick_param {
112  pick_param_str_t operator()(const char *t) { return pick_param_str_t(t); }
113  pick_param_str_t operator()(const std::string &t) { return pick_param_str_t(t); }
114 
115  template <typename T>
116  const T &operator()(const T &t) {
117  return t;
118  }
119  };
120 
121  // expect functions
122  template <typename TL, typename TR>
123  bool expect_eq(const TL &l, const TR &r, const char *lexpr, const char *rexpr, const char *file, size_t line) {
124  pick_param<TL, TR, std::is_pointer<TL>::value || std::is_pointer<TR>::value,
125  std::is_integral<TL>::value || std::is_integral<TR>::value, std::is_integral<TL>::value && std::is_integral<TR>::value>
126  pp;
127  if (pp(l) == pp(r)) {
128  ++(*success_counter_ptr);
129  return true;
130  } else {
131  ++(*failed_counter_ptr);
132  util::cli::shell_stream ss(std::cout);
133  ss() << util::cli::shell_font_style::SHELL_FONT_COLOR_RED << "FAILED => " << file << ":" << line << std::endl
134  << "Expected: " << lexpr << " == " << rexpr << std::endl
135  << lexpr << ": " << l << std::endl
136  << rexpr << ": " << r << std::endl;
137 
138  return false;
139  }
140  }
141 
142  template <typename TL, typename TR>
143  bool expect_ne(const TL &l, const TR &r, const char *lexpr, const char *rexpr, const char *file, size_t line) {
144  pick_param<TL, TR, std::is_pointer<TL>::value || std::is_pointer<TR>::value,
145  std::is_integral<TL>::value || std::is_integral<TR>::value, std::is_integral<TL>::value && std::is_integral<TR>::value>
146  pp;
147 
148  if (pp(l) != pp(r)) {
149  ++(*success_counter_ptr);
150  return true;
151  } else {
152  ++(*failed_counter_ptr);
153  util::cli::shell_stream ss(std::cout);
154  ss() << util::cli::shell_font_style::SHELL_FONT_COLOR_RED << "FAILED => " << file << ":" << line << std::endl
155  << "Expected: " << lexpr << " != " << rexpr << std::endl
156  << lexpr << ": " << l << std::endl
157  << rexpr << ": " << r << std::endl;
158 
159  return false;
160  }
161  }
162 
163  template <typename TL, typename TR>
164  bool expect_lt(const TL &l, const TR &r, const char *lexpr, const char *rexpr, const char *file, size_t line) {
165  pick_param<TL, TR, std::is_pointer<TL>::value || std::is_pointer<TR>::value,
166  std::is_integral<TL>::value || std::is_integral<TR>::value, std::is_integral<TL>::value && std::is_integral<TR>::value>
167  pp;
168 
169  if (pp(l) < pp(r)) {
170  ++(*success_counter_ptr);
171  return true;
172  } else {
173  ++(*failed_counter_ptr);
174  util::cli::shell_stream ss(std::cout);
175  ss() << util::cli::shell_font_style::SHELL_FONT_COLOR_RED << "FAILED => " << file << ":" << line << std::endl
176  << "Expected: " << lexpr << " < " << rexpr << std::endl
177  << lexpr << ": " << l << std::endl
178  << rexpr << ": " << r << std::endl;
179 
180  return false;
181  }
182  }
183 
184  template <typename TL, typename TR>
185  bool expect_le(const TL &l, const TR &r, const char *lexpr, const char *rexpr, const char *file, size_t line) {
186  pick_param<TL, TR, std::is_pointer<TL>::value || std::is_pointer<TR>::value,
187  std::is_integral<TL>::value || std::is_integral<TR>::value, std::is_integral<TL>::value && std::is_integral<TR>::value>
188  pp;
189 
190  if (pp(l) <= pp(r)) {
191  ++(*success_counter_ptr);
192  return true;
193  } else {
194  ++(*failed_counter_ptr);
195  util::cli::shell_stream ss(std::cout);
196  ss() << util::cli::shell_font_style::SHELL_FONT_COLOR_RED << "FAILED => " << file << ":" << line << std::endl
197  << "Expected: " << lexpr << " <= " << rexpr << std::endl
198  << lexpr << ": " << l << std::endl
199  << rexpr << ": " << r << std::endl;
200 
201  return false;
202  }
203  }
204 
205  template <typename TL, typename TR>
206  bool expect_gt(const TL &l, const TR &r, const char *lexpr, const char *rexpr, const char *file, size_t line) {
207  pick_param<TL, TR, std::is_pointer<TL>::value || std::is_pointer<TR>::value,
208  std::is_integral<TL>::value || std::is_integral<TR>::value, std::is_integral<TL>::value && std::is_integral<TR>::value>
209  pp;
210 
211  if (pp(l) > pp(r)) {
212  ++(*success_counter_ptr);
213  return true;
214  } else {
215  ++(*failed_counter_ptr);
216  util::cli::shell_stream ss(std::cout);
217  ss() << util::cli::shell_font_style::SHELL_FONT_COLOR_RED << "FAILED => " << file << ":" << line << std::endl
218  << "Expected: " << lexpr << " > " << rexpr << std::endl
219  << lexpr << ": " << l << std::endl
220  << rexpr << ": " << r << std::endl;
221 
222  return false;
223  }
224  }
225 
226  template <typename TL, typename TR>
227  bool expect_ge(const TL &l, const TR &r, const char *lexpr, const char *rexpr, const char *file, size_t line) {
228  pick_param<TL, TR, std::is_pointer<TL>::value || std::is_pointer<TR>::value,
229  std::is_integral<TL>::value || std::is_integral<TR>::value, std::is_integral<TL>::value && std::is_integral<TR>::value>
230  pp;
231 
232  if (pp(l) >= pp(r)) {
233  ++(*success_counter_ptr);
234  return true;
235  } else {
236  ++(*failed_counter_ptr);
237  util::cli::shell_stream ss(std::cout);
238  ss() << util::cli::shell_font_style::SHELL_FONT_COLOR_RED << "FAILED => " << file << ":" << line << std::endl
239  << "Expected: " << lexpr << " >= " << rexpr << std::endl
240  << lexpr << ": " << l << std::endl
241  << rexpr << ": " << r << std::endl;
242 
243  return false;
244  }
245  }
246 
247  template <typename TL>
248  bool expect_true(const TL &l, const char *expr, const char *file, size_t line) {
249  if (!!(l)) {
250  ++(*success_counter_ptr);
251  return true;
252  } else {
253  ++(*failed_counter_ptr);
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 true: " << expr << std::endl
257  << expr << ": " << l << std::endl;
258 
259  return false;
260  }
261  }
262 
263  template <typename TL>
264  bool expect_false(const TL &l, const char *expr, const char *file, size_t line) {
265  if (!(l)) {
266  ++(*success_counter_ptr);
267  return true;
268  } else {
269  ++(*failed_counter_ptr);
270  util::cli::shell_stream ss(std::cout);
271  ss() << util::cli::shell_font_style::SHELL_FONT_COLOR_RED << "FAILED => " << file << ":" << line << std::endl
272  << "Expected false: " << expr << std::endl
273  << expr << ": " << l << std::endl;
274 
275  return false;
276  }
277  }
278 
279 private:
280  test_data_type tests_;
281  int success_;
282  int failed_;
283  UTILS_TEST_ENV_AUTO_SET(std::string) run_cases_;
284  UTILS_TEST_ENV_AUTO_SET(std::string) run_groups_;
285 };
286 
287 int run_tests(int argc, char *argv[]);
288 
289 #endif /* TEST_MANAGER_H_ */
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:206
bool expect_false(const TL &l, const char *expr, const char *file, size_t line)
Definition: test_manager.h:264
bool operator>(const pick_param_str_t &other) const
static test_manager & me()
bool operator!=(const pick_param_str_t &other) const
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:164
test_case_base * case_ptr_type
Definition: test_manager.h:49
bool operator<(const pick_param_str_t &other) const
const T & operator()(const T &t)
Definition: test_manager.h:116
bool operator==(const pick_param_str_t &other) const
STL namespace.
pick_param_str_t operator()(const char *t)
Definition: test_manager.h:112
void append(const std::string &test_name, const std::string &case_name, case_ptr_type)
bool operator<=(const pick_param_str_t &other) const
test_data_type tests_
Definition: test_manager.h:280
bool expect_true(const TL &l, const char *expr, const char *file, size_t line)
Definition: test_manager.h:248
int * failed_counter_ptr
Definition: test_manager.h:66
void set_cases(const std::vector< std::string > &case_names)
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:185
bool operator>=(const pick_param_str_t &other) const
UTILS_TEST_ENV_AUTO_SET(std::string) run_cases_
std::vector< std::pair< std::string, case_ptr_type > > test_type
Definition: test_manager.h:50
static std::string get_expire_time(clock_t begin, clock_t end)
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:143
int run_tests(int argc, char *argv[])
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:123
pick_param_str_t operator()(const std::string &t)
Definition: test_manager.h:113
typedef UTILS_TEST_ENV_AUTO_MAP(std::string, test_type) test_data_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:227
int * success_counter_ptr
Definition: test_manager.h:65