Skip to content

Commit ebfa857

Browse files
committed
引入CUnit单元测试,完成第二章测试
1 parent b507b9c commit ebfa857

10 files changed

+175
-8
lines changed

Makefile

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,27 @@
1-
BIN = main
2-
3-
SRCS = $(BIN).c utils.c $(subst ./, ,$(wildcard ./chap*/*.c))
1+
ALLSRCS = utils.c $(subst ./,,$(wildcard ./chap*/*.c))
2+
TESTSRCS = $(subst ./,,$(wildcard ./chap*/test*.c))
3+
SRCS = $(filter-out $(TESTSRCS), $(ALLSRCS))
4+
ALLOBJS = $(patsubst %.c, %.o, $(ALLSRCS))
45
OBJS = $(patsubst %.c, %.o, $(SRCS))
56

67
CC = gcc
78
CFLAGS = -Wall -g
89
INCLUDEFLAGS =
9-
LDFLAGS =
10+
LDFLAGS = -lcunit
1011
TARGET = main
12+
TEST = test
1113

1214
.PHONY:run
1315
run: all
14-
./$(TARGET)
16+
./$(TEST)
1517

1618
.PHONY:all
17-
all: $(TARGET)
19+
all: $(TARGET) $(TEST)
20+
21+
$(TARGET): $(addsuffix .o, $(TARGET)) $(OBJS)
22+
$(CC) -o $@ $^ $(LDFLAGS)
1823

19-
$(TARGET): $(OBJS)
24+
$(TEST): $(addsuffix .o, $(TEST)) $(ALLOBJS)
2025
$(CC) -o $@ $^ $(LDFLAGS)
2126

2227
%.o:%.c
@@ -40,3 +45,10 @@ debug:
4045
.PHONY:dump
4146
dump:
4247
less $(TARGET).exe.stackdump
48+
.PHONY:var
49+
var:
50+
@echo ALLSRCS: $(ALLSRCS)
51+
@echo TESTSRCS: $(TESTSRCS)
52+
@echo SRCS: $(SRCS)
53+
@echo ALLOBJS: $(ALLOBJS)
54+
@echo OBJS: $(OBJS)

chap02/test_insertion_sort.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include "insertion_sort.h"
2+
#include "test_insertion_sort.h"
3+
#include "../utils.h"
4+
#include <CUnit/CUnit.h>
5+
#include <stdlib.h>
6+
7+
static void test_insertion_sort() {
8+
int len = 100;
9+
int nums[len];
10+
for (int i = 0; i < len; i++) {
11+
nums[i] = rand();
12+
}
13+
insertion_sort(nums, len);
14+
for (int i = 1; i < len; i++) {
15+
CU_ASSERT(nums[i-1] <= nums[i]);
16+
}
17+
}
18+
19+
CU_ErrorCode add_test_insertion_sort() {
20+
CU_pSuite pSuite = CU_add_suite("SuitInsertionSort", NULL, NULL);
21+
CHECK_CU_GLOBAL();
22+
CU_ADD_TEST(pSuite, test_insertion_sort);
23+
CHECK_CU_GLOBAL();
24+
return CUE_SUCCESS;
25+
}

chap02/test_insertion_sort.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#ifndef TEST_INSERTION_SORT_H
2+
#define TEST_INSERTION_SORT_H
3+
4+
#include <CUnit/CUnit.h>
5+
6+
CU_ErrorCode add_test_insertion_sort();
7+
8+
#endif

chap02/test_merge_sort.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#include "test_merge_sort.h"
2+
#include "../utils.h"
3+
#include <stdlib.h>
4+
5+
CU_ErrorCode add

chap02/test_merge_sort.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#ifndef TEST_MERGE_SORT_H
2+
#define TEST_MERGE_SORT_H
3+
4+
#include <CUnit/CUnit.h>
5+
6+
CU_ErrorCode add_test_merge_sort();
7+
8+
#endif

chap15/optimal_binary_search_tree.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ obst_slt_t optimal_bst(double p[], double q[], int n) {
5454
}
5555
}
5656
}
57-
printf("%lf\n", e[1][n]);
5857
return slt;
5958
}
6059

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include "optimal_binary_search_tree.h"
2+
#include "../utils.h"
3+
#include <CUnit/CUnit.h>
4+
#include <CUnit/console.h>
5+
#include <stdio.h>
6+
7+
static int InitSuite() {
8+
return 0;
9+
}
10+
11+
static int EndSuite() {
12+
return 0;
13+
}
14+
15+
static void test_optimal_bst() {
16+
double p[] = {0, 0.15, 0.10, 0.05, 0.10, 0.20};
17+
double q[] = {0.05, 0.10, 0.05, 0.05, 0.05, 0.10};
18+
int n = 5;
19+
obst_slt_t slt = optimal_bst(p, q, n);
20+
CU_ASSERT(slt.e[1][n] == 2.75);
21+
}
22+
23+
CU_ErrorCode add_test_obst() {
24+
CU_pSuite pSuite = NULL;
25+
26+
/***************
27+
* 1. CU_add_suite 增加一个Suite
28+
* 2. Suite名字 : testSuite
29+
* 3. InitSuite EndSuite:分别是测试单元初始和释放函数,如不需要则NULL传递
30+
****************/
31+
pSuite = CU_add_suite("TestOBST", InitSuite, EndSuite);
32+
CHECK_CU_GLOBAL();
33+
34+
/***************
35+
* 1. 注册当前Suite下的测试用例 
36+
* 2. pSuite:用例指针
37+
* 3. "Test1": 测试单元名称
38+
* 4. Test1:测试函数
39+
***************/
40+
CU_ADD_TEST(pSuite, test_optimal_bst);
41+
CHECK_CU_GLOBAL();
42+
43+
return CUE_SUCCESS;
44+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#ifndef TEST_OPTIMAL_BINARY_SEARCH_TREE
2+
#define TEST_OPTIMAL_BINARY_SEARCH_TREE
3+
4+
#include <CUnit/CUnit.h>
5+
6+
CU_ErrorCode add_test_obst();
7+
8+
#endif

test.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include "chap02/test_insertion_sort.h"
2+
#include "chap15/test_optimal_binary_search_tree.h"
3+
#include "utils.h"
4+
#include <stdio.h>
5+
#include <stdlib.h>
6+
#include <time.h>
7+
#include <CUnit/console.h>
8+
9+
int main() {
10+
srand(time(NULL));
11+
//CU_initialize_registry 注册函数注册一个用例返回CUE_系列异常值
12+
int cu_res;
13+
cu_res = CU_initialize_registry();
14+
CHECK_CU_RETURN(cu_res);
15+
16+
//CU_get_registry 返回注册到用例指针
17+
//assert(NULL != CU_get_registry());
18+
19+
//检测是否在执行
20+
//assert(!CU_is_test_running());
21+
22+
//注册各个suite
23+
CHECK_CU_RETURN(add_test_obst());
24+
CHECK_CU_RETURN(add_test_insertion_sort());
25+
26+
//使用console控制交互界面的函数入口
27+
CU_console_run_tests();
28+
29+
/***使用自动产生XML文件的模式********
30+
CU_set_output_filename("TestMax");
31+
CU_list_tests_to_file();
32+
CU_automated_run_tests();
33+
***********************************/
34+
35+
//调用完毕清除注册信息
36+
CU_cleanup_registry();
37+
38+
return 0;
39+
}

utils.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,25 @@
11
#ifndef UTILS_H
22
#define UTILS_H
33

4+
#define CHECK_CU_GLOBAL() \
5+
do { \
6+
CU_ErrorCode c = CU_get_error(); \
7+
if (CUE_SUCCESS != c) { \
8+
printf("%s", CU_get_error_msg()); \
9+
return c; \
10+
} \
11+
} \
12+
while (0)
13+
14+
#define CHECK_CU_RETURN(res) \
15+
do { \
16+
if (CUE_SUCCESS != (res)) { \
17+
printf("%s", CU_get_error_msg()); \
18+
return (res); \
19+
} \
20+
} \
21+
while (0)
22+
423
void print_array(int nums[], int len);
524
void print_array2(int **nums, int rows, int cols);
625
void swap(int nums[], int a, int b);

0 commit comments

Comments
 (0)