头文件 include,
lib 库,对象文件的打包
- google 框架
自行网上搜索下载
/*************************************************************************
> File Name: simpletest.cpp
> Author:
> Mail:
> Created Time: Mon 26 Oct 2020 07:33:18 PM CST
************************************************************************/
#include <stdio.h>
#include <gtest/gtest.h>
int add(int a, int b) {
return a+b;
}
TEST(func, add) {
EXPECT_EQ(add(3, 4), 7);
EXPECT_EQ(add(3, 2), 7);
EXPECT_EQ(add(3, 5), 7);
}
int main(int argc, char *argv[]) {
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
g++ -std=c++11 -I./include simpletest.cpp -L./ lib -lgtest -lpthread
/*************************************************************************
> File Name: simpletest.cpp
> Author:
> Mail:
> Created Time: Mon 26 Oct 2020 07:33:18 PM CST
************************************************************************/
#include <stdio.h>
#include <test.h>
int add(int a, int b) {
return a+b;
}
TEST(func, add) {
EXPECT_EQ(add(3, 4), 7);
EXPECT_EQ(add(3, 2), 7);
EXPECT_EQ(add(3, 5), 7);
}
TEST(testfunc, add2) {
EXPECT_EQ(add(3, 4), 7);
EXPECT_EQ(add(3, 2), 7);
EXPECT_EQ(add(3, 5), 7);
}
int main(int argc, char *argv[]) {
return RUN_ALL_TESTS();
}
/*************************************************************************
> File Name: test.h
> Author:
> Mail:
> Created Time: Mon 26 Oct 2020 08:27:07 PM CST
************************************************************************/
#ifndef _TEST_H
#define _TEST_H
#define TEST(a, b)\
__attribute__((constructor))\
void a##__test__##b()
#define EXPECT_EQ(a, b) printf("%s == %s ? %s\n", #a, #b, (a) == (b) ? "True" : "False");
int RUN_ALL_TESTS();
#endif
test.cc
int RUN_ALL_TESTS() {
return 0;
}
makefile
.PHONY:clear
all: simpletest.o test.o test.h
g++ -I./ simpletest.o test.o -o ./bin/test
simpletest.o: simpletest.cpp test.h
g++ -I./ -c simpletest.cpp -o simpletest.o
test.o: test.cc test.h
g++ -I./ -c test.cc -o test.o
clean:
rm -rf bin/test simpletest.o test.o
- 优化
#include <test.h>
#include <string.h>
#include <stdio.h>
int func_cnt = 0;
Function func_arr[100];
int RUN_ALL_TESTS() {
for (int i = 0; i < func_cnt; i++) {
printf("RUN TESTS : %s\n", func_arr[i].str);
func_arr[i].func();
printf("RUN END\n");
}
return 0;
}
void add_function(TestFunc func, const char *str) {
func_arr[func_cnt].func = func;
func_arr[func_cnt].str = strdup(str);
func_cnt++;
return;
}
/*************************************************************************
> File Name: test.h
> Author:
> Mail:
> Created Time: Mon 26 Oct 2020 08:27:07 PM CST
************************************************************************/
#ifndef _TEST_H
#define _TEST_H
#define TEST(a, b)\
void a##_test_##b();\
__attribute__((constructor))\
void add##_test_##a##_test_##b() {\
add_function(a##_test_##b, #a "_test_"#b);\
}\
void a##_test_##b()
#define EXPECT_EQ(a, b) printf("%s == %s ? %s\n", #a, #b, (a) == (b) ? "True" : "False");
typedef void (*TestFunc)();
typedef struct Function {
TestFunc func;
const char *str;
} Function;
int RUN_ALL_TESTS();
void add_function(TestFunc, const char *);
#endif
attribute((constructor))先执行后面的
4.输出格式
/*************************************************************************
> File Name: test.h
> Author:
> Mail:
> Created Time: Mon 26 Oct 2020 08:27:07 PM CST
************************************************************************/
#ifndef _TEST_H
#define _TEST_H
#define TEST(a, b)\
void a##_test_##b();\
__attribute__((constructor))\
void add##_test_##a##_test_##b() {\
add_function(a##_test_##b, #a "."#b);\
}\
void a##_test_##b()
//修改
#define CORLOR(a, b) "\033[" #b "m" a "\033[0m"
#define CORLOR_HL(a, b) "\033[1;" #b "m" a "\033[0m"
#define GREEN(a) CORLOR(a, 32)
#define RED(a) CORLOR(a, 31)
#define BLUE(a) CORLOR(a, 34)
#define YELLOW(a) CORLOR(a, 33)
#define GREEN_HL(a) CORLOR_HL(a, 32)
#define RED_HL(a) CORLOR_HL(a, 31)
#define BLUE_HL(a) CORLOR_HL(a, 34)
#define YELLOW_HL(a) CORLOR_HL(a, 33)
#define EXPECT(a, b, comp) {\
printf(GREEN("[--------]") #a " " #comp " " #b);\
printf(" %s\n", (a) comp (b) ? GREEN_HL("TRUE") : RED_HL("FALSE"));\
}
#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, >=)//修改
typedef void (*TestFunc)();
typedef struct Function {
TestFunc func;
const char *str;
} Function;
int RUN_ALL_TESTS();
void add_function(TestFunc, const char *);
#endif
- 设计统计信息
/*************************************************************************
> File Name: test.h
> Author:
> Mail:
> Created Time: Mon 26 Oct 2020 08:27:07 PM CST
************************************************************************/
#ifndef _TEST_H
#define _TEST_H
#define TEST(a, b)\
void a##_test_##b();\
__attribute__((constructor))\
void add##_test_##a##_test_##b() {\
add_function(a##_test_##b, #a "."#b);\
}\
void a##_test_##b()
#define CORLOR(a, b) "\033[" #b "m" a "\033[0m"
#define CORLOR_HL(a, b) "\033[1;" #b "m" a "\033[0m"
#define GREEN(a) CORLOR(a, 32)
#define RED(a) CORLOR(a, 31)
#define BLUE(a) CORLOR(a, 34)
#define YELLOW(a) CORLOR(a, 33)
#define GREEN_HL(a) CORLOR_HL(a, 32)
#define RED_HL(a) CORLOR_HL(a, 31)
#define BLUE_HL(a) CORLOR_HL(a, 34)
#define YELLOW_HL(a) CORLOR_HL(a, 33)
#define EXPECT(a, b, comp) {\
printf(GREEN("[--------]") #a " " #comp " " #b);\
test_info.total += 1;\
if (a comp b) test_info.success += 1;\
printf(" %s\n", (a) comp (b) ? GREEN_HL("TRUE") : RED_HL("FALSE"));\
}
#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, >=)
typedef void (*TestFunc)();
typedef struct Function {
TestFunc func;
const char *str;
} Function;
struct FunctionInfo {
int total, success;
};
extern struct FunctionInfo test_info;//修改
int RUN_ALL_TESTS();
void add_function(TestFunc, const char *);
#endif
#include <test.h>
#include <string.h>
#include <stdio.h>
#include <math.h>
int func_cnt = 0;
Function func_arr[100];
struct FunctionInfo test_info;
int RUN_ALL_TESTS() {
for (int i = 0; i < func_cnt; i++) {
printf(GREEN("[====RUN====]") RED_HL(" %s\n"), func_arr[i].str);
func_arr[i].func();
double rate = 100.0 * test_info.success / test_info.total;//修改开始
printf(GREEN("[ "));
if (fabs(fabs(rate - 100.0) < 1e-6)) {
printf(BLUE_HL("%6.2lf%%"), rate);
} else {
printf(RED_HL("%6.2lf%%"), rate);
}
printf(GREEN(" ]") " total : %d success : %d\n",
test_info.total,
test_info.success
);
}
return 0;
}
void add_function(TestFunc func, const char *str) {
func_arr[func_cnt].func = func;
func_arr[func_cnt].str = strdup(str);
func_cnt++;
return;
}
- 错误信息
/*************************************************************************
> File Name: test.h
> Author:
> Mail:
> Created Time: Mon 26 Oct 2020 08:27:07 PM CST
************************************************************************/
#ifndef _TEST_H
#define _TEST_H
#define TEST(a, b)\
void a##_test_##b();\
__attribute__((constructor))\
void add##_test_##a##_test_##b() {\
add_function(a##_test_##b, #a "."#b);\
}\
void a##_test_##b()
#define CORLOR(a, b) "\033[" #b "m" a "\033[0m"
#define CORLOR_HL(a, b) "\033[1;" #b "m" a "\033[0m"
#define GREEN(a) CORLOR(a, 32)
#define RED(a) CORLOR(a, 31)
#define BLUE(a) CORLOR(a, 34)
#define YELLOW(a) CORLOR(a, 33)
#define GREEN_HL(a) CORLOR_HL(a, 32)
#define RED_HL(a) CORLOR_HL(a, 31)
#define BLUE_HL(a) CORLOR_HL(a, 34)
#define YELLOW_HL(a) CORLOR_HL(a, 33)
#define TYPE(a) _Generic((a), \
int : "%d", \
double : "%lf", \
float : "%f", \
long long : "%lld", \
const char * : "%s", \
char * : "%s"\
)
#define P(a, color) {\
char frm[1000];\
sprintf(frm, color("%s"), TYPE(a));\
printf(frm, a);\
}
#define EXPECT(a, b, comp) {\
test_info.total += 1;\
__typeof(a) _a = (a);\
__typeof(b) _b = (b);\
if (_a comp _b) test_info.success += 1;\
else {\
printf("\n");\
printf(YELLOW_HL("\t%s:%d: failure\n"), __FILE__, __LINE__);\
printf(YELLOW_HL("\t\texpect : " #a " " #comp " " #b "\n\t\t" "actual : " ));\
P(_a, YELLOW_HL);\
P(" vs ", YELLOW_HL);\
P(_b, YELLOW_HL);\
printf("\n\n");\
}\
printf(GREEN("[----------]") #a " " #comp " " #b);\
printf(" %s\n", _a comp _b ? GREEN_HL("TRUE") : RED_HL("FALSE"));\
}
#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, >=)
typedef void (*TestFunc)();
typedef struct Function {
TestFunc func;
const char *str;
} Function;
struct FunctionInfo {
int total, success;
};
extern struct FunctionInfo test_info;
int RUN_ALL_TESTS();
void add_function(TestFunc, const char *);
#endif
用到_Generic 需要用c ,故该g++为gcc ; test,cc 为test.c, simpletest.cpp为simpletest.c

7. 设置链表
#include <test.h>
#include <string.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
Function func_head, *func_tail = &func_head;
struct FunctionInfo test_info;
int RUN_ALL_TESTS() {
for (struct LinkNode *p = func_head.p.next; p; p = p->next) {
Function *func = Head(p, Function, p);
printf(GREEN("[====RUN====]") RED_HL(" %s\n"), func->str);
test_info.total = 0, test_info.success = 0;
func->func();
double rate = 100.0 * test_info.success / test_info.total;
printf(GREEN("[ "));
if (fabs(fabs(rate - 100.0) < 1e-6)) {
printf(BLUE_HL("%6.2lf%%"), rate);
} else {
printf(RED_HL("%6.2lf%%"), rate);
}
printf(GREEN(" ]") " total : %d success : %d\n",
test_info.total,
test_info.success
);
}
return 0;
}
void add_function(TestFunc func, const char *str) {
Function *temp = (Function *)calloc(1, sizeof(Function));
temp->func = func;
temp->str = strdup(str);
func_tail->p.next = &(temp->p);
func_tail = temp;
return;
}
typedef struct Function {
TestFunc func;
const char *str;
struct LinkNode p;
} Function;
整个过程, 最终代码可以贴出
/*************************************************************************
> File Name: test.h
> Author:
> Mail:
> Created Time: Mon 26 Oct 2020 08:27:07 PM CST
************************************************************************/
#ifndef _TEST_H
#define _TEST_H
#include <linklist.h>
#define TEST(a, b)\
void a##_test_##b();\
__attribute__((constructor))\
void add##_test_##a##_test_##b() {\
add_function(a##_test_##b, #a "."#b);\
}\
void a##_test_##b()
#define CORLOR(a, b) "\033[" #b "m" a "\033[0m"
#define CORLOR_HL(a, b) "\033[1;" #b "m" a "\033[0m"
#define GREEN(a) CORLOR(a, 32)
#define RED(a) CORLOR(a, 31)
#define BLUE(a) CORLOR(a, 34)
#define YELLOW(a) CORLOR(a, 33)
#define GREEN_HL(a) CORLOR_HL(a, 32)
#define RED_HL(a) CORLOR_HL(a, 31)
#define BLUE_HL(a) CORLOR_HL(a, 34)
#define YELLOW_HL(a) CORLOR_HL(a, 33)
#define TYPE(a) _Generic((a), \
int : "%d", \
double : "%lf", \
float : "%f", \
long long : "%lld", \
const char * : "%s", \
char * : "%s"\
)
#define P(a, color) {\
char frm[1000];\
sprintf(frm, color("%s"), TYPE(a));\
printf(frm, a);\
}
#define EXPECT(a, b, comp) {\
test_info.total += 1;\
__typeof(a) _a = (a);\
__typeof(b) _b = (b);\
if (_a comp _b) test_info.success += 1;\
else {\
printf("\n");\
printf(YELLOW_HL("\t%s:%d: failure\n"), __FILE__, __LINE__);\
printf(YELLOW_HL("\t\texpect : " #a " " #comp " " #b "\n\t\t" "actual : " ));\
P(_a, YELLOW_HL);\
P(" vs ", YELLOW_HL);\
P(_b, YELLOW_HL);\
printf("\n\n");\
}\
printf(GREEN("[----------]") #a " " #comp " " #b);\
printf(" %s\n", _a comp _b ? GREEN_HL("TRUE") : RED_HL("FALSE"));\
}
#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, >=)
typedef void (*TestFunc)();
typedef struct Function {
TestFunc func;
const char *str;
struct LinkNode p;
} Function;
struct FunctionInfo {
int total, success;
};
extern struct FunctionInfo test_info;
int RUN_ALL_TESTS();
void add_function(TestFunc, const char *);
#endif
#include <test.h>
#include <string.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
Function func_head, *func_tail = &func_head;
struct FunctionInfo test_info;
int RUN_ALL_TESTS() {
for (struct LinkNode *p = func_head.p.next; p; p = p->next) {
Function *func = Head(p, Function, p);
printf(GREEN("[====RUN====]") RED_HL(" %s\n"), func->str);
test_info.total = 0, test_info.success = 0;
func->func();
double rate = 100.0 * test_info.success / test_info.total;
printf(GREEN("[ "));
if (fabs(fabs(rate - 100.0) < 1e-6)) {
printf(BLUE_HL("%6.2lf%%"), rate);
} else {
printf(RED_HL("%6.2lf%%"), rate);
}
printf(GREEN(" ]") " total : %d success : %d\n",
test_info.total,
test_info.success
);
}
return 0;
}
void add_function(TestFunc func, const char *str) {
Function *temp = (Function *)calloc(1, sizeof(Function));
temp->func = func;
temp->str = strdup(str);
func_tail->p.next = &(temp->p);
func_tail = temp;
return;
}
/*************************************************************************
> File Name: simpletest.c
> Author:
> Mail:
> Created Time: Mon 26 Oct 2020 07:33:18 PM CST
************************************************************************/
#include <stdio.h>
#include <test.h>
int add(int a, int b) {
return a+b;
}
TEST(func, add) {
EXPECT_EQ(add(3, 4), 7);
EXPECT_NE(add(3, 2), 7);
EXPECT_EQ(add(3, 5), 7);
}
TEST(testfunc, add2) {
EXPECT_EQ(add(3, 4), 7);
EXPECT_LE(add(3, 2), 7);
EXPECT_EQ(add(3, 5), 7);
}
TEST(test, funcadd2) {
EXPECT_GT(add(3, 4), 7);
EXPECT_LT(add(3, 2), 7);
EXPECT_GE(add(3, 5), 7);
}
int main(int argc, char *argv[]) {
return RUN_ALL_TESTS();
}
.PHONY:clear
all: simpletest.o test.o test.h
gcc -I./ simpletest.o test.o -o ./bin/test
simpletest.o: simpletest.c test.h
gcc -I./ -c simpletest.c -o simpletest.o
test.o: test.c test.h
gcc -I./ -c test.c -o test.o
clean:
rm -rf bin/test simpletest.o test.o

被折叠的 条评论
为什么被折叠?



