From 29db0dae43285bbc6fa7a947735907d6bdbfcf85 Mon Sep 17 00:00:00 2001 From: wjf-hs Date: Thu, 24 Apr 2025 17:26:10 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9A=82=E4=B8=8D=E6=98=AF=E7=94=A8=E5=87=BD?= =?UTF-8?q?=E6=95=B0=E6=B3=A8=E5=86=8C=E7=9A=84=E6=96=B9=E5=BC=8F=EF=BC=8C?= =?UTF-8?q?=E6=AF=95=E7=AB=9F=E5=A2=9E=E5=8A=A0=E4=BA=86=E9=A2=9D=E5=A4=96?= =?UTF-8?q?=E5=86=85=E5=AD=98=E6=88=90=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/common.h | 2 +- include/heap.h | 14 +++++-------- src/heap.c | 15 ++++++++++++++ test/test_heap.c | 53 ++++++++++++++++++++++++++++++++++++++++++++---- 4 files changed, 70 insertions(+), 14 deletions(-) diff --git a/include/common.h b/include/common.h index 796ff4f..f95c658 100644 --- a/include/common.h +++ b/include/common.h @@ -37,6 +37,6 @@ * obj == obj2 return 0 * obj > obj2 return 1 */ -typedef int (*cmp_fun_t)(void* obj, void* obj2); +typedef int (*compare_fun_t)(void* obj, void* obj2); #endif // _COMMON_H_ diff --git a/include/heap.h b/include/heap.h index 0e0a84b..457b2f9 100644 --- a/include/heap.h +++ b/include/heap.h @@ -44,15 +44,11 @@ struct _heap uint32_t(*size)(struct _heap* self); bool (*clear)(struct _heap* self); - /** - * @brief obj compare with obj2 - * - * @return - * obj < obj2 return -1 - * obj == obj2 return 0 - * obj > obj2 return 1 - */ - int (*compare)(void* obj, void* obj2); + // config + // !!! you have to implement this function + compare_fun_t compare; + // register function + // void (*register_compare)(struct _heap* self, compare_fun_t cmp_fun); // -------------------- debug -------------------- void (*print)(struct _heap* self); diff --git a/src/heap.c b/src/heap.c index 1f1ebee..bf26110 100644 --- a/src/heap.c +++ b/src/heap.c @@ -70,7 +70,14 @@ static void heap_swap(struct _heap* self, int i, int j) static void heap_fixed_up(struct _heap* self, int i) { assert(self != NULL); + assert(self->compare != NULL); int p = 0; + + if(self->compare == NULL) + { + return ; + } + if(self->_type == HEAP_MAX) { while(1) @@ -122,6 +129,11 @@ static void heap_fixed_down(struct _heap* self, int i) int l = 0,r = 0; int max = 0, min = 0; + if(self->compare == NULL) + { + return; + } + if(self->_type == HEAP_MAX) { while(1) @@ -265,6 +277,9 @@ static bool heap_init2(struct _heap* self, uint32_t obj_size, uint32_t capacity) // base self->size = heap_size; self->clear = heap_clear; + + // config + self->compare = NULL; // -------------------- debug -------------------- self->print = heap_print; diff --git a/test/test_heap.c b/test/test_heap.c index ec80a9e..cf322cb 100644 --- a/test/test_heap.c +++ b/test/test_heap.c @@ -36,7 +36,7 @@ static void* get_min(struct _heap* heap, void *array, int start, int end) return min; } -static void test_heap_num(void) +static void test_heap_min_num(void) { uint32_t i = 0; // int data[] = { 2,1,3,4}; @@ -79,7 +79,7 @@ static void test_heap_num(void) TEST_ASSERT_NULL(heap); } -static void test_heap_struct(void) +static void test_heap_min_struct(void) { uint32_t i = 0; struct _student data[] = { @@ -126,10 +126,55 @@ static void test_heap_struct(void) TEST_ASSERT_NULL(heap); } +static void test_heap_max_num(void) +{ + uint32_t i = 0; + // int data[] = { 2,1,3,4}; + // int data[] = { 1,2,3,4,5,6}; + // int data[] = { 5,2,3,1,7,8,6 }; + int data[] = { 5,2,3,1,7,8,6,4,9,10,12,11,15,14,13 }; + int temp = 0; + uint32_t len = sizeof(data) / sizeof(data[0]); + + heap_t heap = heap_max_new2(sizeof(int), 64); + TEST_ASSERT_NOT_NULL(heap); + heap->print_obj = print_num; + heap->compare = compare_num; + + for (i = 0; i < len; i++) + { + temp = data[i]; + TEST_ASSERT_TRUE(heap->push(heap, &temp)); + TEST_ASSERT_EQUAL_INT(i + 1, heap->size(heap)); + } + TEST_ASSERT_TRUE(heap->peek(heap, &temp)); + TEST_ASSERT_EQUAL_INT(*(int *)get_max(heap, data, 0, heap->size(heap)), temp); + + TEST_ASSERT_TRUE(heap->clear(heap)); + TEST_ASSERT_TRUE(heap->empty(heap)); + for (i = 0; i < len; i++) + { + temp = data[i]; + TEST_ASSERT_TRUE(heap->push(heap, &temp)); + } + + for (i = 0; i < len; i++) + { + temp = data[i]; + TEST_ASSERT_TRUE(heap->pop(heap, &temp)); + } + TEST_ASSERT_TRUE(heap->empty(heap)); + + heap_free(&heap); + TEST_ASSERT_NULL(heap); +} + void test_heap(void) { UnitySetTestFile(__FILE__); - RUN_TEST(test_heap_num); - RUN_TEST(test_heap_struct); + RUN_TEST(test_heap_min_num); + RUN_TEST(test_heap_min_struct); + + RUN_TEST(test_heap_max_num); }