Skip to content

Commit 0b90132

Browse files
committed
完成第七章测试
1 parent 186bfbf commit 0b90132

File tree

4 files changed

+37
-2
lines changed

4 files changed

+37
-2
lines changed

chap06/test_heap_sort.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include <CUnit/CUnit.h>
55
#include <stdlib.h>
66

7-
static void test_insertion_sort() {
7+
static void test_heap_sort() {
88
int len = 100;
99
int nums[len];
1010
for (int i = 0; i < len; i++) {
@@ -19,7 +19,7 @@ static void test_insertion_sort() {
1919
CU_ErrorCode add_test_heap_sort() {
2020
CU_pSuite pSuite = CU_add_suite("heap_sort", NULL, NULL);
2121
CHECK_CU_GLOBAL();
22-
CU_ADD_TEST(pSuite, test_insertion_sort);
22+
CU_ADD_TEST(pSuite, test_heap_sort);
2323
CHECK_CU_GLOBAL();
2424
return CUE_SUCCESS;
2525
}

chap07/test_quick_sort.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include "quick_sort.h"
2+
#include "test_quick_sort.h"
3+
#include "../utils.h"
4+
#include <CUnit/CUnit.h>
5+
#include <stdlib.h>
6+
7+
static void test_quick_sort() {
8+
int len = 100;
9+
int nums[len];
10+
for (int i = 0; i < len; i++) {
11+
nums[i] = rand();
12+
}
13+
quick_sort(nums, 0, 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_quick_sort() {
20+
CU_pSuite pSuite = CU_add_suite("quick_sort", NULL, NULL);
21+
CHECK_CU_GLOBAL();
22+
CU_ADD_TEST(pSuite, test_quick_sort);
23+
CHECK_CU_GLOBAL();
24+
return CUE_SUCCESS;
25+
}

chap07/test_quick_sort.h

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

test.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "chap02/test_merge_sort.h"
33
#include "chap04/test_maximum_subarray.h"
44
#include "chap06/test_heap_sort.h"
5+
#include "chap07/test_quick_sort.h"
56
#include "chap15/test_optimal_binary_search_tree.h"
67
#include "utils.h"
78
#include <stdio.h>
@@ -27,6 +28,7 @@ int main() {
2728
CHECK_CU_RETURN(add_test_merge_sort());
2829
CHECK_CU_RETURN(add_test_maximum_subarray());
2930
CHECK_CU_RETURN(add_test_heap_sort());
31+
CHECK_CU_RETURN(add_test_quick_sort());
3032
CHECK_CU_RETURN(add_test_obst());
3133

3234
//使用console控制交互界面的函数入口

0 commit comments

Comments
 (0)