Google testing framework 2

Write your own main file

/*************************************************************************
	> File Name: main.cpp
	> Author: 
	> Mail: 
	> Created Time: 2020年01月12日 星期日 15时28分00秒
 ************************************************************************/
#include"unit.h"
#include"haizei_test.h"
#include<cstdio>
#include<iostream>
using namespace std;

int haizei_init(){
    printf("hello wolrd\n");
    return 0;
}

TEST(test1, add_function){
    EXPECT_EQ(add(3, 4), 7);

}

TEST(test2, add_function){
    EXPECT_EQ(add(3, 3), 7);
}
TEST(test3, add_function){
    EXPECT_EQ(add(3, 4), 8);
}

int main(int argc, char *argv[]){
    return RUN_ALL_TESTS();
}

Own test header files haizei_test.h

/*************************************************************************
	> File Name: haizei_test.h
	> Author: 
	> Mail: 
	> Created Time: 2020年01月12日 星期日 18时26分14秒
 ************************************************************************/

#ifndef _HAIZEI_TEST_H
#define _HAIZEI_TEST_H
#define maxn 1000

typedef void (*Func_T)();
extern Func_T func_arr[maxn];
extern int func_cnt;

#define TEST(a, b)\
    void a##b();\
    __attribute__((constructor))\
    void add_##a##b(){\
    func_arr[func_cnt++] = a##b;\
    }\
    void a##b()
#define EXPECT_EQ(a, b) {\
    if(a == b){\
        printf("YES\n");\
    }else{\
        printf("NO\n");\
    }\
}

int RUN_ALL_TESTS();

#endif

Their implementation code haizei_test.cc

#include"haizei_test.h"
#include<cstdio>
Func_T func_arr[maxn];
int func_cnt = 0;

int RUN_ALL_TESTS(){
    for(int i = 0; i < func_cnt; i++){
        printf("run %d\n", i);
        func_arr[i]();
    }
    return 0;
}

Published 48 original articles · won praise 5 · Views 760

Guess you like

Origin blog.csdn.net/weixin_43899266/article/details/103948911