libcopp  1.1.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 <iostream>
16 #include <sstream>
17 #include <cstdio>
18 
19 #ifdef _MSC_VER
20 #include <Windows.h>
21 #endif
22 
23 #ifdef UTILS_TEST_MACRO_TEST_ENABLE_GTEST
24 #include "gtest/gtest.h"
25 
26 #define CASE_TEST(test_name, case_name) TEST(test_name, case_name)
27 
28 #define CASE_EXPECT_TRUE(c) EXPECT_TRUE(c)
29 #define CASE_EXPECT_FALSE(c) EXPECT_FALSE(c)
30 #define CASE_EXPECT_EQ(l, r) EXPECT_EQ(l, r)
31 #define CASE_EXPECT_NE(l, r) EXPECT_NE(l, r)
32 #define CASE_EXPECT_LT(l, r) EXPECT_LT(l, r)
33 #define CASE_EXPECT_LE(l, r) EXPECT_LE(l, r)
34 #define CASE_EXPECT_GT(l, r) EXPECT_GT(l, r)
35 #define CASE_EXPECT_GE(l, r) EXPECT_GE(l, r)
36 
37 #else
38 #include "test_manager.h"
39 
40 #define test_case_func_name(test_name, case_name) test_func_test_##test_name##_case_##case_name
41 #define test_case_obj_name(test_name, case_name) test_obj_test_##test_name##_case_##case_name##_obj
42 
43 #define CASE_TEST(test_name, case_name) \
44 static void test_case_func_name(test_name, case_name) (); \
45 static test_case_base test_case_obj_name(test_name, case_name) (#test_name, #case_name, test_case_func_name(test_name, case_name)); \
46 void test_case_func_name(test_name, case_name) ()
47 
48 
49 #ifdef UTILS_TEST_MACRO_TEST_ENABLE_BOOST_TEST
50  #define CASE_EXPECT_TRUE(c) BOOST_CHECK(c)
51  #define CASE_EXPECT_FALSE(c) BOOST_CHECK(!(c))
52  #define CASE_EXPECT_EQ(l, r) BOOST_CHECK_EQUAL(l, r)
53  #define CASE_EXPECT_NE(l, r) BOOST_CHECK_NE(l, r)
54  #define CASE_EXPECT_LT(l, r) BOOST_CHECK_LT(l, r)
55  #define CASE_EXPECT_LE(l, r) BOOST_CHECK_LE(l, r)
56  #define CASE_EXPECT_GT(l, r) BOOST_CHECK_GT(l, r)
57  #define CASE_EXPECT_GE(l, r) BOOST_CHECK_GE(l, r)
58 
59 #else
60  #define CASE_EXPECT_TRUE(c) test_manager::me().expect_true((c), #c, __FILE__, __LINE__)
61  #define CASE_EXPECT_FALSE(c) test_manager::me().expect_false((c), #c, __FILE__, __LINE__)
62  #define CASE_EXPECT_EQ(l, r) test_manager::me().expect_eq((l), (r), #l, #r, __FILE__, __LINE__)
63  #define CASE_EXPECT_NE(l, r) test_manager::me().expect_ne((l), (r), #l, #r, __FILE__, __LINE__)
64  #define CASE_EXPECT_LT(l, r) test_manager::me().expect_lt((l), (r), #l, #r, __FILE__, __LINE__)
65  #define CASE_EXPECT_LE(l, r) test_manager::me().expect_le((l), (r), #l, #r, __FILE__, __LINE__)
66  #define CASE_EXPECT_GT(l, r) test_manager::me().expect_gt((l), (r), #l, #r, __FILE__, __LINE__)
67  #define CASE_EXPECT_GE(l, r) test_manager::me().expect_ge((l), (r), #l, #r, __FILE__, __LINE__)
68 
69 #endif
70 
71 #endif
72 
73 // 前景色: BLACK,RED,GREEN,YELLOW,BLUE,MAGENTA,CYAN,WHITE
74 #define CASE_MSG_FCOLOR(x) util::cli::shell_font_style::SHELL_FONT_COLOR_##x
75 // 背景色: BLACK,RED,GREEN,YELLOW,BLUE,MAGENTA,CYAN,WHITE
76 #define CASE_MSG_BCOLOR(x) util::cli::shell_font_style::SHELL_FONT_BACKGROUND_COLOR_##x
77 // 字体格式: BOLD,UNDERLINE,FLASH,DARK
78 #define CASE_MSG_STYLE(x) util::cli::shell_font_style::SHELL_FONT_SPEC_##x
79 
80 #define CASE_MSG_INFO() util::cli::shell_stream(std::cout)()<< "[ RUNNING ] "
81 #define CASE_MSG_ERROR() util::cli::shell_stream(std::cerr)()<< "[ RUNNING ] "
82 
83 // 测试中休眠
84 #if (defined(__cplusplus) && __cplusplus >= 201103L) || (defined(_MSC_VER) && _MSC_VER >= 1800)
85  #include <thread>
86  #define CASE_THREAD_SLEEP_MS(x) std::this_thread::sleep_for(std::chrono::milliseconds(x))
87  #define CASE_THREAD_YIELD() std::this_thread::yield()
88 
89 #elif defined(_MSC_VER)
90  #include <Windows.h>
91  #define CASE_THREAD_SLEEP_MS(x) Sleep(x)
92  #define CASE_THREAD_YIELD() YieldProcessor()
93 
94 #else
95  #include <unistd.h>
96 
97  #define CASE_THREAD_SLEEP_MS(x) ((x > 1000)? sleep(x / 1000): usleep(0)); usleep((x % 1000) * 1000)
98  #if defined(__linux__) || defined(__unix__)
99  #include <sched.h>
100  #define CASE_THREAD_YIELD() sched_yield()
101  #elif defined(__GNUC__) || defined(__clang__)
102  #if defined(__i386__) || defined(__x86_64__)
103 
108  #define CASE_THREAD_YIELD() __asm__ __volatile__("pause")
109  #elif defined(__ia64__) || defined(__ia64)
110 
115  #define CASE_THREAD_YIELD() __asm__ __volatile__ ("hint @pause")
116  #elif defined(__arm__) && !defined(__ANDROID__)
117 
121  #define CASE_THREAD_YIELD() __asm__ __volatile__ ("yield")
122  #else
123  #define CASE_THREAD_YIELD()
124  #endif
125  #else
126  #define CASE_THREAD_YIELD()
127  #endif
128 
129 #endif
130 
131 #endif /* TEST_MACROS_H_ */