Skip to content

Commit a2a9bca

Browse files
committed
添加第15章部分单元测试(缺矩阵连乘)
1 parent 7de3dbd commit a2a9bca

13 files changed

+238
-87
lines changed

Makefile

Lines changed: 39 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,61 @@
1-
ALLFILES = $(subst ./,,$(wildcard ./chap*/*.[ch]) $(wildcard ./*.[ch]))
2-
ALLSRCS = utils.c $(subst ./,,$(wildcard ./chap*/*.c))
3-
TESTSRCS = $(subst ./,,$(wildcard ./chap*/test*.c))
4-
SRCS = $(filter-out $(TESTSRCS), $(ALLSRCS))
5-
ALLOBJS = $(patsubst %.c, %.o, $(ALLSRCS))
6-
OBJS = $(patsubst %.c, %.o, $(SRCS))
7-
8-
CC = gcc
9-
CFLAGS = -Wall -g -std=gnu99
10-
INCLUDEFLAGS =
11-
LDFLAGS = -lcunit -lm
1+
CC=gcc
2+
CFLAGS=-Wall -g -std=gnu99
3+
INCLUDEFLAGS=
4+
# 注意链接参数必须放在目标后面
5+
LDLIBS=-lcunit -lm
6+
7+
DLTDEP=rm -f *.d chap*/*.d
8+
129
TARGET = main
13-
TEST = test
10+
TESTTG = test_main
11+
12+
TARGETSRC = $(addsuffix .c, $(TARGET))
13+
TESTTGSRC = $(addsuffix .c, $(TESTTG))
14+
15+
IGNSRCS = play.c
16+
ALLFILES= $(subst ./, , $(wildcard ./*.[ch]) $(wildcard ./chap*/*.[ch]))
17+
ALLSRCS = $(filter-out $(IGNSRCS), $(subst ./, , $(wildcard ./*.c) $(wildcard ./chap*/*.c)))
18+
TSTSRCS = $(subst ./, , $(wildcard ./test*.c) $(wildcard ./chap*/test*.c))
19+
20+
TARGETDEPSRCS = $(filter-out $(TSTSRCS), $(ALLSRCS))
21+
TESTTGDEPSRCS = $(filter-out $(TARGETSRC), $(ALLSRCS))
22+
TARGETDEPOBJS = $(patsubst %.c, %.o, $(TARGETDEPSRCS))
23+
TESTTGDEPOBJS = $(patsubst %.c, %.o, $(TESTTGDEPSRCS))
1424

1525
.PHONY:all
16-
all: $(TARGET) $(TEST) tags
26+
all: $(TARGET) $(TESTTG) tags
1727

18-
$(TARGET): $(addsuffix .o, $(TARGET)) $(OBJS)
19-
@$(CC) -o $@ $^ $(LDFLAGS)
28+
$(TARGET): $(TARGETDEPOBJS)
29+
$(CC) -o $@ $^ $(LDLIBS)
30+
$(DLTDEP)
2031

21-
$(TEST): $(addsuffix .o, $(TEST)) $(ALLOBJS)
22-
@$(CC) -o $@ $^ $(LDFLAGS)
32+
$(TESTTG): $(TESTTGDEPOBJS)
33+
$(CC) -o $@ $^ $(LDLIBS)
34+
$(DLTDEP)
2335

2436
# 要么把所有源文件加到依赖;要么设为phony。
2537
# 否则首次生成过tags之后后续会因为已存在不再生成
2638
tags: $(ALLFILES)
27-
@ctags -R .
39+
ctags -R .
2840

29-
%.o:%.c
30-
@$(CC) -o $@ -c $< $(CFLAGS) $(INCLUDEFLAGS)
41+
# %.o: %.c
42+
# $(COMPILE) $< -o $@
3143

3244
# 使用gcc的-MM选项自动生成各源文件的依赖
33-
%.d:%.c
34-
@set -e; rm -f $@; $(CC) -MM $< $(INCLUDEFLAGS) > $@.$$$$; \
35-
sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
36-
rm -f $@.$$$$
45+
%.d: %c
46+
$(CC) -MM $< $(INCLUDEFLAGS) | awk '{if ($$1 ~ /o:$$/) $$1 = "$*.o:"; print $$0}' > $@;
3747

38-
-include $(OBJS:.o=.d)
48+
# 导入依赖
49+
-include $(ALLSRCS:.c=.d)
3950

4051
.PHONY:clean
4152
clean:
42-
@rm -f $(TARGET) $(OBJS) **/*.d **/*.d.*
43-
44-
.PHONY:debug
45-
debug: all
46-
@gdb $(TEST)
47-
48-
.PHONY:dump
49-
dump:
50-
@less $(TARGET).exe.stackdump
53+
rm -f $(TARGET) $(TESTTG) $(TARGETDEPOBJS) $(TESTTGDEPOBJS)
5154

5255
.PHONY:run
5356
run: all
54-
@./$(TEST)
57+
@./$(TESTTG)
5558

5659
.PHONY:var
5760
var:
58-
@echo ALLFILES: $(ALLFILES)
59-
@echo ALLSRCS: $(ALLSRCS)
60-
@echo TESTSRCS: $(TESTSRCS)
61-
@echo SRCS: $(SRCS)
62-
@echo ALLOBJS: $(ALLOBJS)
63-
@echo OBJS: $(OBJS)
61+
@echo TSTSRCS: $(TSTSRCS)

Makefile.bak

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
ALLFILES = $(subst ./,,$(wildcard ./chap*/*.[ch]) $(wildcard ./*.[ch]))
2+
ALLSRCS = utils.c $(subst ./,,$(wildcard ./chap*/*.c))
3+
TESTSRCS = $(subst ./,,$(wildcard ./chap*/test*.c))
4+
SRCS = $(filter-out $(TESTSRCS), $(ALLSRCS))
5+
ALLOBJS = $(patsubst %.c, %.o, $(ALLSRCS))
6+
OBJS = $(patsubst %.c, %.o, $(SRCS))
7+
8+
CC = gcc
9+
CFLAGS = -Wall -g -std=gnu99
10+
INCLUDEFLAGS =
11+
LDFLAGS = -lcunit -lm
12+
TARGET = main
13+
TEST = test
14+
15+
.PHONY:all
16+
all: $(TARGET) $(TEST) tags
17+
18+
$(TARGET): $(addsuffix .o, $(TARGET)) $(OBJS)
19+
@$(CC) -o $@ $^ $(LDFLAGS)
20+
21+
$(TEST): $(addsuffix .o, $(TEST)) $(ALLOBJS)
22+
@$(CC) -o $@ $^ $(LDFLAGS)
23+
24+
# 要么把所有源文件加到依赖;要么设为phony。
25+
# 否则首次生成过tags之后后续会因为已存在不再生成
26+
tags: $(ALLFILES)
27+
@ctags -R .
28+
29+
%.o:%.c
30+
@$(CC) -o $@ -c $< $(CFLAGS) $(INCLUDEFLAGS)
31+
32+
# 使用gcc的-MM选项自动生成各源文件的依赖
33+
%.d:%.c
34+
@set -e; rm -f $@; $(CC) -MM $< $(INCLUDEFLAGS) > $@.$$$$; \
35+
sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
36+
rm -f $@.$$$$
37+
38+
-include $(OBJS:.o=.d)
39+
40+
.PHONY:clean
41+
clean:
42+
@rm -f $(TARGET) $(OBJS) **/*.d **/*.d.*
43+
44+
.PHONY:debug
45+
debug: all
46+
@gdb $(TEST)
47+
48+
.PHONY:dump
49+
dump:
50+
@less $(TARGET).exe.stackdump
51+
52+
.PHONY:run
53+
run: all
54+
@./$(TEST)
55+
56+
.PHONY:var
57+
var:
58+
@echo ALLFILES: $(ALLFILES)
59+
@echo ALLSRCS: $(ALLSRCS)
60+
@echo TESTSRCS: $(TESTSRCS)
61+
@echo SRCS: $(SRCS)
62+
@echo ALLOBJS: $(ALLOBJS)
63+
@echo OBJS: $(OBJS)

chap15/longest_common_subsequence.c

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ void free_lcs_slt(lcs_slt_t *slt) {
3939
lcs_slt_t lcs_length(const char *strx, const char *stry) {
4040
int m = strlen(strx);
4141
int n = strlen(stry);
42-
lcs_slt_t slt;
42+
lcs_slt_t slt = {NULL, NULL, 0, 0};
4343
init_lcs_slt(&slt, m, n);
4444
int **c = slt.c;
4545
lcs_slt_dirc_t **b = slt.b;
@@ -60,26 +60,34 @@ lcs_slt_t lcs_length(const char *strx, const char *stry) {
6060
return slt;
6161
}
6262

63-
void print_lcs_aux(const char *strx, int i, int j, lcs_slt_dirc_t **b);
63+
void get_lcs_aux(const char *strx, int i, int j, lcs_slt_dirc_t **b, char *buf, int *idx);
6464

65-
void print_lcs(const char *strx, const char *stry, int i, int j) {
65+
void get_lcs(const char *strx, const char *stry, int i, int j, char *buf) {
6666
lcs_slt_t slt = lcs_length(strx, stry);
6767
lcs_slt_dirc_t **b = slt.b;
68-
print_lcs_aux(strx, i, j, b);
69-
printf("\n");
68+
int idx = 0;
69+
get_lcs_aux(strx, i, j, b, buf, &idx);
70+
buf[idx] = '\0';
71+
for (int i = 0; i <= (idx-1)/2; i++) {
72+
char tmp = buf[i];
73+
buf[i] = buf[idx-i-1];
74+
buf[idx-i-1] = tmp;
75+
}
7076
free_lcs_slt(&slt);
7177
}
7278

73-
void print_lcs_aux(const char *strx, int i, int j, lcs_slt_dirc_t **b) {
79+
void get_lcs_aux(const char *strx, int i, int j, lcs_slt_dirc_t **b, char *buf, int *idx) {
7480
if (i == 0 || j == 0) {
7581
return;
7682
}
7783
if (b[i][j] == UP_LEFT) {
78-
print_lcs_aux(strx, i-1, j-1, b);
84+
buf[*idx] = strx[i-1];
85+
(*idx)++;
86+
get_lcs_aux(strx, i-1, j-1, b, buf, idx);
7987
printf("%c", strx[i-1]);
8088
} else if (b[i][j] == UP) {
81-
print_lcs_aux(strx, i-1, j, b);
89+
get_lcs_aux(strx, i-1, j, b, buf, idx);
8290
} else {
83-
print_lcs_aux(strx, i, j-1, b);
91+
get_lcs_aux(strx, i, j-1, b, buf, idx);
8492
}
8593
}

chap15/longest_common_subsequence.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,6 @@ typedef struct lcs_slt_s lcs_slt_t;
2121
void init_lcs_slt(lcs_slt_t *slt, int m, int n);
2222
void free_lcs_slt(lcs_slt_t *slt);
2323
lcs_slt_t lcs_length(const char *strx, const char *stry);
24+
void get_lcs(const char *strx, const char *stry, int i, int j, char *buf);
2425

2526
#endif

chap15/test_cut_rod.c

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

chap15/test_cut_rod.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#ifndef TEST_CUT_ROD
2+
#define TEST_CUT_ROD
3+
4+
#include <CUnit/CUnit.h>
5+
6+
CU_ErrorCode add_test_cut_rod();
7+
8+
#endif
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#include "longest_common_subsequence.h"
2+
#include "test_longest_common_subsequence.h"
3+
#include "../utils.h"
4+
#include <CUnit/CUnit.h>
5+
#include <CUnit/Console.h>
6+
#include <stdio.h>
7+
#include <stdlib.h>
8+
9+
static int InitSuite() {
10+
return 0;
11+
}
12+
13+
static int EndSuite() {
14+
return 0;
15+
}
16+
17+
static void test_lcs() {
18+
const char *str1 = "ABCBDAB";
19+
const char *str2 = "BDCABA";
20+
const char *lcs = "BCBA";
21+
char buf[512];
22+
get_lcs(str1, str2, strlen(str1), strlen(str2), buf);
23+
CU_ASSERT(strlen(buf) == strlen(lcs));
24+
CU_ASSERT(strncmp(buf, lcs, 4) == 0);
25+
}
26+
27+
CU_ErrorCode add_test_lcs() {
28+
CU_pSuite pSuite = NULL;
29+
30+
/***************
31+
* 1. CU_add_suite 增加一个Suite
32+
* 2. Suite名字 : testSuite
33+
* 3. InitSuite EndSuite:分别是测试单元初始和释放函数,如不需要则NULL传递
34+
****************/
35+
pSuite = CU_add_suite("lcs", InitSuite, EndSuite);
36+
CHECK_CU_GLOBAL();
37+
38+
/***************
39+
* 1. 注册当前Suite下的测试用例 
40+
* 2. pSuite:用例指针
41+
* 3. "Test1": 测试单元名称
42+
* 4. Test1:测试函数
43+
***************/
44+
CU_ADD_TEST(pSuite, test_lcs);
45+
CHECK_CU_GLOBAL();
46+
47+
return CUE_SUCCESS;
48+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#ifndef TEST_LONGEST_COMMON_SUBSEQ
2+
#define TEST_LONGEST_COMMON_SUBSEQ
3+
4+
#include <CUnit/CUnit.h>
5+
6+
CU_ErrorCode add_test_lcs();
7+
8+
#endif

main.c

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -16,42 +16,5 @@
1616
#include <time.h>
1717

1818
int main() {
19-
srand(time(NULL));
20-
//int nums[] = {13,-3,-25,20,-3,-16,-23,18,20,-7,12,-5,-22,15,-4,7};
21-
//int nums[10] = {1, 0, 4, 3, 7, 6, 5, 2, 9, 8};
22-
int nums[] = {
23-
71, 57, 79, 0, 60, 56, 89, 92, 87, 18,
24-
43, 53, 86, 26, 15, 51, 73, 65, 41, 42,
25-
25, 62, 49, 28, 70, 78, 5, 85, 22, 69,
26-
6, 93, 37, 4, 40, 38, 77, 64, 76, 63,
27-
94, 33, 21, 29, 35, 11, 12, 97, 48, 7,
28-
17, 91, 99, 8, 44, 24, 39, 83, 3, 52,
29-
90, 95, 2, 10, 58, 88, 27, 14, 75, 98,
30-
66, 9, 68, 67, 55, 50, 16, 45, 59, 20,
31-
54, 72, 32, 19, 84, 47, 1, 80, 13, 36,
32-
34, 96, 81, 31, 46, 30, 61, 23, 74, 82
33-
};
34-
// int nums[] = {49, 26, 64, 97, 59, 62, 76, 12, 76, 20, 5, 66, 60, 0, 28, 59, 9, 43, 5, 44, 27, 80, 80, 27, 10, 10, 14, 86, 7, 37, 39, 12, 27, 40, 46, 96, 93, 38, 11, 27, 56, 6, 75, 85, 12, 50, 49, 75, 63, 12, 11, 71, 28, 38, 0, 53, 96, 2, 99, 58, 85, 81, 88, 60, 73, 15, 86, 72, 63, 58, 47, 28, 59, 96, 62, 86, 73, 92, 50, 46, 60, 2, 23, 8, 42, 5, 11, 63, 60, 14, 50, 72, 28, 48, 82, 17, 15, 22, 68, 29};
35-
//int p[] = {30,35,15,5,10,20,25};
36-
//const char *strx = "ABCBDAB";
37-
//const char *stry = "BDCABA";
38-
//print_lcs(strx, stry, 7, 6);
39-
//print_optimal_parens(p, 6, 2, 5);
40-
//int price_table[] = {0,1,5,8,9,10,17,17,20,24,30};
41-
//print_array(nums, 100);
42-
//int s = linear_select(nums, 0, 100, 10);
43-
//radix_sort(nums, 100);
44-
// subarr sa = maximum_subarray(nums, 0, 4);
45-
//print_array(nums, 100);
46-
//for (int i = 1; i <= 10; i++) {
47-
// int s = memoized_cut_rod(price_table, i);
48-
// int t = bottom_up_cut_rod(price_table, i);
49-
// printf("%d %d\n", s, t);
50-
// print_cut_rod_solution(price_table, i);
51-
//}
52-
//printf("left: %d, right: %d, sum: %d\n", sa.left, sa.right, sa.sum);
53-
double p[] = {0, 0.15, 0.10, 0.05, 0.10, 0.20};
54-
double q[] = {0.05, 0.10, 0.05, 0.05, 0.05, 0.10};
55-
optimal_bst(p, q, 5);
5619
return 0;
5720
}

make

Whitespace-only changes.

test_main

73.3 KB
Binary file not shown.

0 commit comments

Comments
 (0)