From fa2dbcd6b622df3ab8c5fac4cd401a89a0f30a49 Mon Sep 17 00:00:00 2001 From: Aleksander Kaminski Date: Thu, 12 Sep 2024 11:55:41 +0200 Subject: [PATCH] test: Add binary heap test JIRA: RTOS-917 --- test/Makefile | 2 +- test/bheap.c | 171 ++++++++++++++++++++++++++++++++++++++++++++++++++ test/bheap.h | 24 +++++++ test/test.h | 7 ++- 4 files changed, 201 insertions(+), 3 deletions(-) create mode 100644 test/bheap.c create mode 100644 test/bheap.h diff --git a/test/Makefile b/test/Makefile index 56a2fb88f..9c833046b 100644 --- a/test/Makefile +++ b/test/Makefile @@ -5,5 +5,5 @@ # Author: Pawel Pisarczyk # -OBJS += $(addprefix $(PREFIX_O)test/, test.o vm.o rb.o proc.o msg.o) +OBJS += $(addprefix $(PREFIX_O)test/, test.o vm.o rb.o proc.o msg.o bheap.o) diff --git a/test/bheap.c b/test/bheap.c new file mode 100644 index 000000000..750490075 --- /dev/null +++ b/test/bheap.c @@ -0,0 +1,171 @@ +/* + * Phoenix-RTOS + * + * Operating system kernel + * + * Min/max binary heap test + * + * Copyright 2024 Phoenix Systems + * Author: Aleksander Kaminski + * + * This file is part of Phoenix-RTOS. + * + * %LICENSE% + */ + +#include "hal/hal.h" +#include "lib/lib.h" +#include "proc/proc.h" +#include "vm/vm.h" + +#define NODE_CNT 50 + + +typedef struct _test_data_t { + bhnode_t linkage; + struct _test_data_t *prev; + struct _test_data_t *next; + int key; +} test_data_t; + + +static struct { + bheap_t heap; + test_data_t *list; +} test_bhCommon; + + +static int test_bhAddNode(int key) +{ + test_data_t *data = vm_kmalloc(sizeof(*data)); + if (data == NULL) { + return -1; + } + + data->key = key; + lib_bhInsert(&test_bhCommon.heap, &data->linkage); + LIST_ADD(&test_bhCommon.list, data); + + return 0; +} + + +static int test_bhRemoveNode(int key) +{ + test_data_t *curr = test_bhCommon.list; + + if (curr == NULL) { + return -1; + } + + do { + if (curr->key == key) { + LIST_REMOVE(&test_bhCommon.list, curr); + lib_bhRemove(&test_bhCommon.heap, &curr->linkage); + vm_kfree(curr); + return 0; + } + curr = curr->next; + } while (curr != test_bhCommon.list); + + return -1; +} + + +static void test_bhDump(bhnode_t *node) +{ + test_data_t *data = lib_bhof(test_data_t, linkage, node); + + lib_printf("%d", data->key); +} + + +static int test_bhCompare(bhnode_t *n1, bhnode_t *n2) +{ + test_data_t *d1 = lib_bhof(test_data_t, linkage, n1); + test_data_t *d2 = lib_bhof(test_data_t, linkage, n2); + + return d2->key - d1->key; +} + + +void test_bh(void) +{ + int i; + unsigned int seed = 5318008; + + lib_bhInit(&test_bhCommon.heap, test_bhCompare); + + lib_printf("test bh: Adding nodes ascending order\n"); + for (i = 0; i < NODE_CNT; ++i) { + if (test_bhAddNode(i) < 0) { + lib_printf("test bh: node add fail\n"); + } + } + + lib_bhDump(test_bhCommon.heap.root, test_bhDump); + + lib_printf("test bh: Removing nodes ascending order\n"); + for (i = 0; i < NODE_CNT; ++i) { + if (test_bhRemoveNode(i) < 0) { + lib_printf("test bh: node remove fail\n"); + } + } + + lib_bhDump(test_bhCommon.heap.root, test_bhDump); + + lib_printf("test bh: Adding nodes decreasing order\n"); + for (i = NODE_CNT - 1; i >= 0; --i) { + if (test_bhAddNode(i) < 0) { + lib_printf("test bh: node add fail\n"); + } + } + + lib_bhDump(test_bhCommon.heap.root, test_bhDump); + + lib_printf("test bh: Removing nodes decreasing order\n"); + for (i = NODE_CNT - 1; i >= 0; --i) { + if (test_bhRemoveNode(i) < 0) { + lib_printf("test bh: node remove fail\n"); + } + } + + lib_bhDump(test_bhCommon.heap.root, test_bhDump); + + lib_printf("test bh: Adding pseudo-random keys\n"); + for (i = 0; i < NODE_CNT; ++i) { + if (test_bhAddNode(lib_rand(&seed) % 32768) < 0) { + lib_printf("test bh: node add fail\n"); + } + } + + lib_bhDump(test_bhCommon.heap.root, test_bhDump); + + lib_printf("test bh: Removing pseudo-random keys\n"); + while (test_bhCommon.list != NULL) { + if (test_bhRemoveNode(test_bhCommon.list->key) < 0) { + lib_printf("test bh: node remove fail\n"); + } + } + + lib_bhDump(test_bhCommon.heap.root, test_bhDump); + + lib_printf("test bh: Adding identical key\n"); + for (i = NODE_CNT - 1; i >= 0; --i) { + if (test_bhAddNode(420) < 0) { + lib_printf("test bh: node add fail\n"); + } + } + + lib_bhDump(test_bhCommon.heap.root, test_bhDump); + + lib_printf("test bh: cleanup\n"); + for (i = NODE_CNT - 1; i >= 0; --i) { + if (test_bhRemoveNode(420) < 0) { + lib_printf("test bh: node remove fail\n"); + } + } + + lib_bhDump(test_bhCommon.heap.root, test_bhDump); + +} diff --git a/test/bheap.h b/test/bheap.h new file mode 100644 index 000000000..c60c0b232 --- /dev/null +++ b/test/bheap.h @@ -0,0 +1,24 @@ +/* + * Phoenix-RTOS + * + * Operating system kernel + * + * Min/max binary heap test + * + * Copyright 2024 Phoenix Systems + * Author: Aleksander Kaminski + * + * This file is part of Phoenix-RTOS. + * + * %LICENSE% + */ + + +#ifndef _TEST_BH_H_ +#define _TEST_BH_H_ + + +extern void test_bh(void); + + +#endif diff --git a/test/test.h b/test/test.h index 154824de4..08209908b 100644 --- a/test/test.h +++ b/test/test.h @@ -17,7 +17,10 @@ #ifndef _TEST_TEST_H_ #define _TEST_TEST_H_ -#include "vm.h" -#include "proc.h" +#include "test/bheap.h" +#include "test/msg.h" +#include "test/proc.h" +#include "test/rb.h" +#include "test/vm.h" #endif