zoukankan      html  css  js  c++  java
  • haizei c++ 试听课程知识点 day2 --第2讲

    海贼班 c++ 试听课程知识点 day2 --第2讲

    制作自己的测试开发环境

     1.EXPECT封装  宏

    #ifndef   _HTEST_H
    #define   _HTEST_H
    
    #define EXPECT(a,comp,b){            //定义基础宏
      if(!((a) comp (b)))  {        
      printf("error
    ");              
        }                                        
    
    
    #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)
    
    #endif                    
    

    2.实现COLOR系列封装   存储在haizei/htest.h

    #ifndef   _HTEST_H
    #define   _HTEST_H
    
    #define COLOR(msg,code)  "33[0;1;    #code    "m"    msg    "33[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)            
    
    #endif

     #code   代表字符串化      相当于将传入的数值加上引号

    源代码中调用 

    #include <haizei/htest.h>
    using namespace std;
    int main()
    {
        printf(RED("hello world
    "));
        printf(GREEN("hello world
    "));
        printf(YELLOW("hello world
    "));
        printf(BLUE("hello world
    "));
        return 0;  
    }
    

    3.使用__attribute__完成函数的注册功能

    所有被声明为constructor的函数  都会先于main主函数去执行,不用调用。

    #include <iostream>
    using namespace std;
    __attribute__((constructor))  //注册test函数
    void test(){
        printf("test:hello world
    ");
        return;
    }
    int main()
    {
        printf("main:hello world
    ");
        return 0;
    }
    

    4.完善测试框架    简单功能

      

    /*************************************************************************
        > File Name: mytest.h
        > Author: huguang
        > Mail: hug@haizeix.com
        > Created Time: 涓? 8/12 20:40:20 2020
     ************************************************************************/

    #ifndef _MYTEST_H
    #define _MYTEST_H

    #define COLOR(msg, code) "33[0;1;" #code "m" msg "33[0m"     //基础宏定义   #code  用来字符串化 输入的数值   msg为传入的信息
    #define RED(msg) COLOR(msg, 31)            //简化宏
    #define GREEN(msg) COLOR(msg, 32)
    #define YELLOW(msg) COLOR(msg, 33)
    #define BLUE(msg) COLOR(msg, 34)

    #define EXPECT(a, comp, b) {
        __typeof(a) __a=(a),__b=(b);    //重新定义局部变量 ()为初始化
        if (!((__a) comp (__b))) {     //宏判断为假时 执行下面的输出
            printf("%s:%d:Failure "), __FILE__, __LINE__);
            printf(YELLOW("Expected:(%s) %s (%s),actual:%d vs %d "),#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)

    #define TEST(a, b)            //定义函数名中用到的字符串部分
    void a##_##b();
    __attribute__((constructor))       //注册函数
    void reg_##a##_##b() {           //实际注册函数名
        add_test(a##_##b, #a "." #b);
        return ;
    }
    void a##_##b()          //名称为a##_##b()函数的声明 注意这里的##为连接运算符

    struct {
        void (*func)();
        const char *name;
    } func_arr[100];
    int func_cnt = 0;

    void add_test(void (*func)(), const char *name) {
        func_arr[func_cnt].func = func;
        func_arr[func_cnt].name = name;
        func_cnt++;
        return ;
    }

    int RUN_ALL_TESTS() {
        for (int i = 0; i < func_cnt; i++) {
            printf(GREEN("[ RUN ]") " %s ", func_arr[i].name);
            func_arr[i].func();
        }
        return 0;
    }

    #endif

    5.结束

  • 相关阅读:
    IntelliJ IDEA 常用快捷键和设置
    Code Project精彩系列(2)
    Code Project精彩系列(2)
    Code Project精彩系列(2)
    Apache和Tomcat区别
    Apache和Tomcat区别
    Apache和Tomcat区别
    如何在 GitHub 建立个人主页和项目演示页面
    如何在 GitHub 建立个人主页和项目演示页面
    如何在 GitHub 建立个人主页和项目演示页面
  • 原文地址:https://www.cnblogs.com/dongguolei/p/13862577.html
Copyright © 2011-2022 走看看