Skip to content

Commit

Permalink
test: Add binary heap test
Browse files Browse the repository at this point in the history
JIRA: RTOS-917
  • Loading branch information
agkaminski committed Sep 12, 2024
1 parent e3bbae5 commit fa2dbcd
Show file tree
Hide file tree
Showing 4 changed files with 201 additions and 3 deletions.
2 changes: 1 addition & 1 deletion test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)

171 changes: 171 additions & 0 deletions test/bheap.c
Original file line number Diff line number Diff line change
@@ -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);

}
24 changes: 24 additions & 0 deletions test/bheap.h
Original file line number Diff line number Diff line change
@@ -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
7 changes: 5 additions & 2 deletions test/test.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit fa2dbcd

Please sign in to comment.