c的测试框架自实现

头文件 include,
lib 库,对象文件的打包

  1. 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

  1. 优化
#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

  1. 设计统计信息
/*************************************************************************
        > 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;
}

  1. 错误信息
/*************************************************************************
        > 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

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值