1、利用color封装实现个性化颜色输出功能
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <queue>
#include <stack>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <vector>
using namespace std;
#define COLOR(msg, code) "\033[0;" #code "m" msg "\033[0m"
#define RED(msg) COLOR(msg, 31)
#define GREEN(msg) COLOR(msg, 32)
#define YELLOW(msg) COLOR(msg, 33)
#define BLUE(msg) COLOR(msg, 34)
int main() {
//printf("\033[0;" "31" "m" "hello kaikeba" "\033[0m\n");
printf(RED("hello\n"));
printf(GREEN("hello\n"));
printf(YELLOW("hello\n"));
printf(BLUE("hello\n"));
printf("hello\n");
return 0;
}
结果:
1、ktest.h
#ifndef _KTEST_H
#define _KTEST_H
#define LOG(frm, args...) { \
printf("[%s : %s : %d] ", __FILE__, __func__, __LINE__); \
printf(frm, ##args); \
printf("\n"); \
}
//实现color系列封装
#define COLOR(msg, code) "\033[0;" #code "m" msg "\033[0m"
#define RED(msg) COLOR(msg, 31)
#define GREEN(msg) COLOR(msg, 32)
#define YELLOW(msg) COLOR(msg, 33)
#define BLUE(msg) COLOR(msg, 34)
int test_flag=0;
//二次封装宏(实现EXPECT系列的封装)
#define EXPECT(a, comp, b) { \
__typeof(a) __a=(a),__b=(b);\
if (!(__a comp __b)) { \
test_flag=0;\
printf(YELLOW( "%s:%d:Failure\n"),__FILE__,__LINE__); \
printf(YELLOW( "Expected:((%s)%s (%s)),actual:%d vs %d\n"),#a,#comp,#b,__a,__b);\
} \
}
#define EXPECT_EQ(a, b) EXPECT(a, ==, b)
#define EXPECT_NE(a, b) EXPECT(a, !=, b)
#define EXPECT_LT(a, b) EXPECT(a, <, b)
#define EXPECT_LE(a, b) EXPECT(a, <=, b)
#define EXPECT_GT(a, b) EXPECT(a, >, b)
#define EXPECT_GE(a, b) EXPECT(a, >=, b)
//4
#define TEST_FUNC_NAME(a,b) kaikeba_##a##_##b
#define TEST(a,b) \
void TEST_FUNC_NAME(a,b)();\ //声明测试用例函数
__attribute__((constructor))\ //声明并定义注册函数
void reg_##a##_##b() {\
add_test_function(TEST_FUNC_NAME(a,b),#a"."#b);\
return ;\
}\
void TEST_FUNC_NAME(a,b)()
//2定义一片存储区,存放不同的测试用例
struct FuncData{
void (*func)();//函数指针,用来指向测试用例函数地址
const char *func_name;//存放相关测试用例的名字,注意区分和函数名字的差别
}func_arr[100];
int func_cnt=0;//表示当前的系统中注册了多少个测试用例
//3添加测试用例的函数
void add_test_function(void (*func)(),const char* func_name){
func_arr[func_cnt].func=func;
func_arr[func_cnt].func_name=func_name;
func_cnt++;
return;
}
//5
const char*RUN = GREEN("[ RUN ]");
const char *OK = GREEN("[ OK ]");
const char*FAILED=RED("[FAILED]");
//1实现RUN_ALL_TEST,功能是遍历所有的测试用例的
int RUN_ALL_TESTS(){
//foreach test case
for(int i=0;i<func_cnt;i++){
printf("%s %s\n",RUN,func_arr[i].func_name);
test_flag=1;
long long b=clock();
func_arr[i].func();
long long e=clock();
if(test_flag){
printf("%s %s(%lld ms)\n",OK,func_arr[i].func_name,100*(e-b)/CLOCKS_PER_SEC);
}else{
printf("%s %s(%lld ms)\n",FAILED,func_arr[i].func_name,100*(e-b)/CLOCKS_PER_SEC);
}
}
return 0;
}
#endif
2、test.cpp
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <queue>
#include <stack>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <vector>
#include <ktest/ktest.h>
using namespace std;
int add(int a, int b) {
return a + b;
}
TEST(test1, add) {
EXPECT_EQ(add(3, 4), 7); // ==
EXPECT_NE(add(3, 4), 6); // !=
EXPECT_LT(add(3, 4), 8); // <
EXPECT_LE(add(3, 4), 7); // <=
EXPECT_GT(add(3, 4), 6); // >
EXPECT_GE(add(3, 4), 7); // >=
}
TEST(test2, add) {
EXPECT_EQ(add(3, 4), 7); // ==
EXPECT_NE(add(3, 4), 7); // !=
EXPECT_LT(add(3, 4), 8); // <
EXPECT_LE(add(3, 4), 7); // <=
EXPECT_GT(add(3, 4), 6); // >
EXPECT_GE(add(3, 4), 7); // >=
}
int main() {
return RUN_ALL_TESTS();
}
编译指令g++ -I./include test.cpp
输出结果
改进版的放在了github上test_framework