从零开始的 JSON 库教程(一):启程 _学习笔记

原网址:https://zhuanlan.zhihu.com/json-tutorial

个人gitHub: https://github.com/mutiantong/json-tutorial/


单元测试

在单元测试中,老师主要讲了do ...while(0)在宏中的用法。


下面的代码,我自己按照老师的代码,写了一个单元测试的简单代码。

主要理解

1. 宏的编写技巧

2.关键字__LINE__

3.fpirntf

#include <stdio.h>

static int main_ret = 0;
static int test_count = 0;
static int test_pass = 0;

#define EXPECT_EQ_BASE_EQUAL(equality, expect, actual, format) \
    do {\
    test_count++; \
    if(equality)\
        {\
        test_pass++;\
        } \
        else \
        {\
        fprintf(stderr, "%s: line:%d  except: "format"  actual: "format"", __FILE__, __LINE__, expect, actual );\
        main_ret = 1;\
            }\
    }while (0)

// __LINE__ 表示的是调用的行
// "format" 需要加双引号,这样才能表示format是一个变量
// 宏,一行写不下,要用, '\' 分隔开
// fprint 是文件输出
// stderr 在这里代表 输出屏幕
// when in the macro, the '\' is the end of the line. We can not add any letter or backspace following the '\'
#define EXPECT_EQ(expect, actual) EXPECT_EQ_BASE_EQUAL((expect) == (actual), expect, actual, "%d")


int main() {


    EXPECT_EQ(5,4);

    return main_ret;
}


参考文档:

1、关于stdin, stdout, stderr,      https://my.oschina.net/qihh/blog/55308

2、 fprintf     http://www.cplusplus.com/reference/cstdio/fprintf/?kw=fprintf

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值