cemu
载入中...
搜索中...
未找到
unittest.h 文件参考

单元测试头文件 更多...

#include <stdio.h>
#include "color.h"

浏览源代码.

struct  unittest_t
 

宏定义

#define VA_NARG(args...)   VA_NARG_(0, ##args, VA_RSEQ_N())
 
#define VA_NARG_(args...)   VA_ARG_N(args)
 
#define VA_ARG_N(_0, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, N, ...)   N
 
#define VA_RSEQ_N()   16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0
 
#define VA_FIRST(args...)   VA_FIRST_(args)
 
#define VA_FIRST_(F, ...)   F
 
#define VA_FIRST_STR(args...)   VA_FIRST_STR_(args)
 
#define VA_FIRST_STR_(F, ...)   #F
 
#define VA_REST_(F, args...)   args
 
#define VA_REST(args...)   VA_REST_(args)
 
#define UT_FAIL   "FAIL"
 
#define UT_PASS   "PASS"
 
#define ut_set_quiet(num)    ut.quiet = num;
 
#define ut_assert(test...)
 
#define ut_dec_test(testname)    int testname##_test()
 
#define ut_def_test(testname, ...)
 
#define ut_run_test(testname)
 
#define ut_print_test()
 

变量

static struct unittest_t ut
 

详细描述

单元测试头文件

作者
lancer (lance.nosp@m.rsta.nosp@m.dium@.nosp@m.163..nosp@m.com)
版本
0.1
日期
2023-12-28
注解
参考项目:Gitee | MiniUnit

A small unit test framework for C/C++.

Origin author: Bo Zhuang sdzhu.nosp@m.angb.nosp@m.o@hot.nosp@m.mail.nosp@m..com Features:

  • ut_assert(expr) assertion fail if expr false
  • ut_assert(expr, message) assertion with message
  • ut_assert(expr, message, args...) assertion with message and args
  • ut_run_test(test) to run a test function in form int f() return 0 if passed
  • ut_test_results() to display the test results
  • #define MU_NOCOLOR if ANSI escape code not supported

For example,

#include "unittest.h"
int test_one() {
ut_assert(2 + 2 == 4);
return 0; // 0 means test passed
}
int test_two() {
int a = 3, b = 5;
ut_assert(a == 3);
ut_assert(b == 5, "b is 5");
ut_assert(a + b == 7, "should be %d", a + b); // fail
return 0;
}
int main()
{
ut_run_test(test_one);
ut_run_test(test_two);
ut_test_results();
return 0;
}
int main(int argc, char *argv[], char *envp[])
主函数
Definition cemu.c:30
单元测试头文件

宏定义说明

◆ ut_def_test

#define ut_def_test ( testname,
... )
值:
int testname##_test() \
{ \
ut.flag = 0; \
ut.u_t = 0, ut.u_p = 0, ut.u_f = 0; \
__VA_ARGS__ \
if (ut.quiet < 3) \
{ \
printf("\n" \
"|-" _bgreen(" %d " UT_PASS) " - ", \
ut.u_p); \
printf(_bred("%d " UT_FAIL), ut.u_f); \
printf(" - " _bold("%d all") " \n|", ut.u_t); \
} \
return ut.flag; \
}

◆ ut_print_test

#define ut_print_test ( )
值:
do \
{ \
if (ut.quiet < 6) \
{ \
printf("\n" \
"|============== Test Result =============="); \
printf("\n" \
"| " _bgreen("%d " UT_PASS) " and ", \
ut.n_pass); \
printf(_bred("%d " UT_FAIL), ut.n_fail); \
printf(" in " _bold("%d TEST(S)"), ut.n_test); \
if (ut.n_pass == ut.n_test) \
printf("\n" \
"|============" _green(" ALL TESTS PASSED ") "==========="); \
else \
printf("\n" \
"|===========" _red(" %d TEST(S) FAILED ") "============" \
"\n", \
ut.n_fail); \
} \
} while (0)

变量说明

◆ ut

struct unittest_t ut
static
初始值:
= {
.n_fail = 0,
.n_pass = 0,
.n_test = 0,
.u_f = 0,
.u_p = 0,
.u_t = 0,
.flag = 0,
.quiet = 0
}