libcopp  2.2.0
test_macros.h
Go to the documentation of this file.
1 /*
2  * test_macros.h
3  *
4  * Created on: 2014年3月11日
5  * Author: owent
6  *
7  * Released under the MIT license
8  */
9 
10 #ifndef TEST_MACROS_H_
11 #define TEST_MACROS_H_
12 
13 #pragma once
14 
15 #include <cstdio>
16 #include <iostream>
17 #include <sstream>
18 
19 #ifdef _MSC_VER
20 # ifndef WIN32_LEAN_AND_MEAN
21 # define WIN32_LEAN_AND_MEAN
22 # endif
23 # include <Windows.h>
24 #endif
25 
26 #ifdef UTILS_TEST_MACRO_TEST_ENABLE_GTEST
27 # include "gtest/gtest.h"
28 
29 # define CASE_TEST(test_name, case_name) TEST(test_name, case_name)
30 
31 # define CASE_EXPECT_TRUE(c) EXPECT_TRUE(c)
32 # define CASE_EXPECT_FALSE(c) EXPECT_FALSE(c)
33 # define CASE_EXPECT_EQ(l, r) EXPECT_EQ(l, r)
34 # define CASE_EXPECT_NE(l, r) EXPECT_NE(l, r)
35 # define CASE_EXPECT_LT(l, r) EXPECT_LT(l, r)
36 # define CASE_EXPECT_LE(l, r) EXPECT_LE(l, r)
37 # define CASE_EXPECT_GT(l, r) EXPECT_GT(l, r)
38 # define CASE_EXPECT_GE(l, r) EXPECT_GE(l, r)
39 
40 #else
41 # include "test_manager.h"
42 
43 # define test_case_func_name(test_name, case_name) test_func_test_##test_name##_case_##case_name##_
44 # define test_case_obj_name(test_name, case_name) test_obj_test_##test_name##_case_##case_name##_
45 
46 # define CASE_TEST(test_name, case_name) \
47  static void test_case_func_name(test_name, case_name)(); \
48  static test_case_base test_case_obj_name(test_name, case_name)(#test_name, #case_name, \
49  test_case_func_name(test_name, case_name)); \
50  void test_case_func_name(test_name, case_name)()
51 
52 # define test_event_on_start_func_name(event_name) test_func_event_on_start_##event_name##_
53 # define test_event_on_start_obj_name(event_name) test_obj_event_on_start_##event_name##_
54 # define test_event_on_exit_func_name(event_name) test_func_event_on_exit_##event_name##_
55 # define test_event_on_exit_obj_name(event_name) test_obj_event_on_exit_##event_name##_
56 
57 # ifdef _MSC_VER
58 # define CASE_TEST_EVENT_ON_START(event_name, ...) \
59  static void test_event_on_start_func_name(event_name)(); \
60  static test_on_start_base test_event_on_start_obj_name(event_name)( \
61  #event_name, test_event_on_start_func_name(event_name), __VA_ARGS__); \
62  void test_event_on_start_func_name(event_name)()
63 
64 # define CASE_TEST_EVENT_ON_EXIT(event_name, ...) \
65  static void test_event_on_exit_func_name(event_name)(); \
66  static test_on_exit_base test_event_on_exit_obj_name(event_name)( \
67  #event_name, test_event_on_exit_func_name(event_name), __VA_ARGS__); \
68  void test_event_on_exit_func_name(event_name)()
69 # else
70 # define CASE_TEST_EVENT_ON_START(event_name, args...) \
71  static void test_event_on_start_func_name(event_name)(); \
72  static test_on_start_base test_event_on_start_obj_name(event_name)( \
73  #event_name, test_event_on_start_func_name(event_name), ##args); \
74  void test_event_on_start_func_name(event_name)()
75 
76 # define CASE_TEST_EVENT_ON_EXIT(event_name, args...) \
77  static void test_event_on_exit_func_name(event_name)(); \
78  static test_on_exit_base test_event_on_exit_obj_name(event_name)( \
79  #event_name, test_event_on_exit_func_name(event_name), ##args); \
80  void test_event_on_exit_func_name(event_name)()
81 # endif
82 
83 # ifdef UTILS_TEST_MACRO_TEST_ENABLE_BOOST_TEST
84 # define CASE_EXPECT_TRUE(c) BOOST_CHECK(c)
85 # define CASE_EXPECT_FALSE(c) BOOST_CHECK(!(c))
86 # define CASE_EXPECT_EQ(l, r) BOOST_CHECK_EQUAL(l, r)
87 # define CASE_EXPECT_NE(l, r) BOOST_CHECK_NE(l, r)
88 # define CASE_EXPECT_LT(l, r) BOOST_CHECK_LT(l, r)
89 # define CASE_EXPECT_LE(l, r) BOOST_CHECK_LE(l, r)
90 # define CASE_EXPECT_GT(l, r) BOOST_CHECK_GT(l, r)
91 # define CASE_EXPECT_GE(l, r) BOOST_CHECK_GE(l, r)
92 
93 # else
94 # define CASE_EXPECT_TRUE(c) test_manager::me().expect_true((c), # c, __FILE__, __LINE__)
95 # define CASE_EXPECT_FALSE(c) test_manager::me().expect_false((c), # c, __FILE__, __LINE__)
96 # define CASE_EXPECT_EQ(l, r) test_manager::me().expect_eq((l), (r), # l, # r, __FILE__, __LINE__)
97 # define CASE_EXPECT_NE(l, r) test_manager::me().expect_ne((l), (r), # l, # r, __FILE__, __LINE__)
98 # define CASE_EXPECT_LT(l, r) test_manager::me().expect_lt((l), (r), # l, # r, __FILE__, __LINE__)
99 # define CASE_EXPECT_LE(l, r) test_manager::me().expect_le((l), (r), # l, # r, __FILE__, __LINE__)
100 # define CASE_EXPECT_GT(l, r) test_manager::me().expect_gt((l), (r), # l, # r, __FILE__, __LINE__)
101 # define CASE_EXPECT_GE(l, r) test_manager::me().expect_ge((l), (r), # l, # r, __FILE__, __LINE__)
102 
103 # endif
104 
105 #endif
106 
107 // 前景色: BLACK,RED,GREEN,YELLOW,BLUE,MAGENTA,CYAN,WHITE
108 #define CASE_MSG_FCOLOR(x) util::cli::shell_font_style::SHELL_FONT_COLOR_##x
109 // 背景色: BLACK,RED,GREEN,YELLOW,BLUE,MAGENTA,CYAN,WHITE
110 #define CASE_MSG_BCOLOR(x) util::cli::shell_font_style::SHELL_FONT_BACKGROUND_COLOR_##x
111 // 字体格式: BOLD,UNDERLINE,FLASH,DARK
112 #define CASE_MSG_STYLE(x) util::cli::shell_font_style::SHELL_FONT_SPEC_##x
113 
114 #define CASE_MSG_INFO() util::cli::shell_stream(std::cout)() << "[ RUNNING ] "
115 #define CASE_MSG_ERROR() util::cli::shell_stream(std::cerr)() << "[ RUNNING ] "
116 
117 // 测试中休眠
118 #if (defined(__cplusplus) && __cplusplus >= 201103L) || (defined(_MSC_VER) && _MSC_VER >= 1800)
119 # include <thread>
120 # define CASE_THREAD_SLEEP_MS(x) std::this_thread::sleep_for(std::chrono::milliseconds(x))
121 # define CASE_THREAD_YIELD() std::this_thread::yield()
122 
123 #elif defined(_MSC_VER)
124 # ifndef WIN32_LEAN_AND_MEAN
125 # define WIN32_LEAN_AND_MEAN
126 # endif
127 # include <Windows.h>
128 # define CASE_THREAD_SLEEP_MS(x) Sleep(x)
129 # define CASE_THREAD_YIELD() YieldProcessor()
130 
131 #else
132 # include <unistd.h>
133 
134 # define CASE_THREAD_SLEEP_MS(x) \
135  ((x > 1000) ? sleep(x / 1000) : usleep(0)); \
136  usleep((x % 1000) * 1000)
137 # if defined(__linux__) || defined(__unix__)
138 # include <sched.h>
139 # define CASE_THREAD_YIELD() sched_yield()
140 # elif defined(__GNUC__) || defined(__clang__)
141 # if defined(__i386__) || defined(__x86_64__)
147 # define CASE_THREAD_YIELD() __asm__ __volatile__("pause")
148 # elif defined(__ia64__) || defined(__ia64)
154 # define CASE_THREAD_YIELD() __asm__ __volatile__("hint @pause")
155 # elif defined(__arm__) && !defined(__ANDROID__)
160 # define CASE_THREAD_YIELD() __asm__ __volatile__("yield")
161 # else
162 # define CASE_THREAD_YIELD()
163 # endif
164 # else
165 # define CASE_THREAD_YIELD()
166 # endif
167 
168 #endif
169 
170 #endif /* TEST_MACROS_H_ */