Skip to content

Commit 186bfbf

Browse files
committed
完成第六章测试
1 parent 0b6a493 commit 186bfbf

File tree

4 files changed

+35
-1
lines changed

4 files changed

+35
-1
lines changed

chap04/test_maximum_subarray.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ static void test_maximum_subarray() {
1616
CU_ASSERT_EQUAL(slt.sum, 43);
1717
int nums2[] = {1, 2, -3, 1};
1818
slt = maximum_subarray(nums2, 0, sizeof(nums2)/sizeof(int), MSA_PLC_LINEAR);
19-
printf("%d, %d, %d\n", slt.left, slt.right, slt.sum);
2019
CU_ASSERT_EQUAL(slt.left, 0);
2120
CU_ASSERT_EQUAL(slt.right, 1);
2221
CU_ASSERT_EQUAL(slt.sum, 3);

chap06/test_heap_sort.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include "heap_sort.h"
2+
#include "test_heap_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+
heap_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_heap_sort() {
20+
CU_pSuite pSuite = CU_add_suite("heap_sort", NULL, NULL);
21+
CHECK_CU_GLOBAL();
22+
CU_ADD_TEST(pSuite, test_insertion_sort);
23+
CHECK_CU_GLOBAL();
24+
return CUE_SUCCESS;
25+
}

chap06/test_heap_sort.h

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

test.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "chap02/test_insertion_sort.h"
22
#include "chap02/test_merge_sort.h"
33
#include "chap04/test_maximum_subarray.h"
4+
#include "chap06/test_heap_sort.h"
45
#include "chap15/test_optimal_binary_search_tree.h"
56
#include "utils.h"
67
#include <stdio.h>
@@ -25,6 +26,7 @@ int main() {
2526
CHECK_CU_RETURN(add_test_insertion_sort());
2627
CHECK_CU_RETURN(add_test_merge_sort());
2728
CHECK_CU_RETURN(add_test_maximum_subarray());
29+
CHECK_CU_RETURN(add_test_heap_sort());
2830
CHECK_CU_RETURN(add_test_obst());
2931

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

0 commit comments

Comments
 (0)