原网址: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