yunit.c和yunit.h的代码实现平头哥使用版本
时间: 2024-03-20 19:40:44 浏览: 17
以下是我平头哥使用的 Yunit 的 yunit.c 和 yunit.h 的代码实现版本:
yunit.c:
```
#include "yunit.h"
static int num_tests = 0;
static int num_failed_tests = 0;
void yunit_run_test(yunit_test_func test_func, const char *test_name) {
int result = test_func();
num_tests++;
if (result == YUNIT_TEST_FAIL) {
num_failed_tests++;
printf("[FAILED] %s\n", test_name);
} else {
printf("[PASSED] %s\n", test_name);
}
}
int yunit_report(void) {
printf("\n==============================================\n");
if (num_failed_tests == 0) {
printf("All %d tests passed\n", num_tests);
} else {
printf("%d/%d tests failed\n", num_failed_tests, num_tests);
}
printf("==============================================\n");
return num_failed_tests;
}
```
yunit.h:
```
#ifndef YUNIT_H_
#define YUNIT_H_
#include <stdio.h>
#define YUNIT_TEST_PASS 0
#define YUNIT_TEST_FAIL 1
typedef int (*yunit_test_func)(void);
void yunit_run_test(yunit_test_func test_func, const char *test_name);
int yunit_report(void);
#endif /* YUNIT_H_ */
```
这个版本中,在输出测试结果时,我把测试通过和测试失败分别标注为[PASSED]和[FAILED],这样可以更加清晰地看到每个测试的结果。并且在 yunit_report 函数中,我增加了分隔线,让输出结果更加整齐美观。