From 5a9400459e12ace2e13a92934ce82bc9af963aaf Mon Sep 17 00:00:00 2001 From: jf-home Date: Mon, 2 Sep 2024 00:50:11 +0800 Subject: [PATCH] =?UTF-8?q?test=5Fheap=E6=96=B0=E5=A2=9E=E5=8D=95=E6=B5=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/test.c | 1 + test/test_heap.c | 140 +++++++++++++++++++++++++++++++++++++++++++++++ test/test_tree.c | 3 + 3 files changed, 144 insertions(+) create mode 100644 test/test_heap.c create mode 100644 test/test_tree.c diff --git a/test/test.c b/test/test.c index 025023e..fb2c3b1 100644 --- a/test/test.c +++ b/test/test.c @@ -82,6 +82,7 @@ int main(int argc, char const *argv[]) TEST_ADD(test_stack); TEST_ADD(test_list); TEST_ADD(test_deque); + TEST_ADD(test_heap); return UNITY_END(); } diff --git a/test/test_heap.c b/test/test_heap.c new file mode 100644 index 0000000..2b88ba0 --- /dev/null +++ b/test/test_heap.c @@ -0,0 +1,140 @@ +/** + * @file test_heap.c + * @author wenjf (Orig5826@163.com) + * @brief + * @version 0.1 + * @date 2024-09-02 + * + * @copyright Copyright (c) 2024 + * + */ +#include "test.h" + +static void* get_max(struct _heap* heap, void *array, int start, int end) +{ + void* max = array; + for (int i = start; i < end; i++) + { + if (heap->compare((char*)array + heap->_obj_size * i, max) > 0) + { + max = (char*)array + heap->_obj_size * i; + } + } + return max; +} + +static void* get_min(struct _heap* heap, void *array, int start, int end) +{ + void* min = array; + for (int i = start; i < end; i++) + { + if (heap->compare((char*)array + heap->_obj_size * i, min) < 0) + { + min = (char*)array + heap->_obj_size * i; + } + } + return min; +} + +static void test_heap_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_new(); + TEST_ASSERT_NOT_NULL(heap); + + heap_init2(heap, sizeof(int), 64); + heap->print_obj = print_num; + heap->compare = compare_num; + + // default: maxheap + // maxheap or minheap + heap->setmin(heap, true); + + 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_min(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); +} + +static void test_heap_struct(void) +{ + uint32_t i = 0; + struct _student data[] = { + {"zhao", 1001},{"qian", 1002}, {"sun", 1003}, {"li", 1004}, + "zhou", 1005, "wu", 1006, "zheng", 1007, "wang", 1008, + }; + struct _student temp = {0}; + uint32_t len = sizeof(data) / sizeof(data[0]); + + heap_t heap = heap_new(); + TEST_ASSERT_NOT_NULL(heap); + + heap_init2(heap, sizeof(struct _student), 64); + heap->print_obj = print_struct; + heap->compare = compare_struct; + + 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_TRUE(heap->peek(heap, &temp)); + TEST_ASSERT_EQUAL_INT(((struct _student*)get_min(heap, data, 0, heap->size(heap)))->id, temp.id); + TEST_ASSERT_EQUAL_STRING(((struct _student*)get_min(heap, data, 0, heap->size(heap)))->name, temp.name); + + 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) +{ + RUN_TEST(test_heap_num); + RUN_TEST(test_heap_struct); +} diff --git a/test/test_tree.c b/test/test_tree.c new file mode 100644 index 0000000..2a37cad --- /dev/null +++ b/test/test_tree.c @@ -0,0 +1,3 @@ + +#include "test.h" +