- Got rid of bogus callback_func

- Refactored linked list interface.
- Fixed all test cases and made them standalone instead of within the kernel.
This commit is contained in:
secXsQuared 2018-03-29 06:00:22 -04:00
parent 3fb0c6e5a6
commit 5a3f9e3419
25 changed files with 302 additions and 315 deletions

1
.gitignore vendored
View File

@ -1,4 +1,3 @@
.idea
cmake-build-debug
CMakeLists.txt
out/

16
CMakeLists.txt Normal file
View File

@ -0,0 +1,16 @@
cmake_minimum_required(VERSION 2.8.4)
project(secX)
include_directories(include)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c11")
file(GLOB_RECURSE SOURCE_FILES ./*.h ./*.c)
file(GLOB_RECURSE TestFiles ./test/*.c ./lib/*.c)
add_executable(Workspace ${SOURCE_FILES})
#include_directories(test/include)
#add_definitions(-DTDBG)
#add_executable(Test ${TestFiles})

View File

@ -10,8 +10,6 @@ dir := kernel
include $(dir)/Rules.mk
dir := lib
include $(dir)/Rules.mk
dir := test
include $(dir)/Rules.mk
dir := mk
include $(dir)/Rules.mk

View File

@ -7,10 +7,10 @@
* IRQL Definitions
*/
typedef uint32 k_irql;
#define IRQL_DISABLED_LEVEL (1 << 3)
#define IRQL_DPC_LEVEL (1 << 2)
#define IRQL_APC_LEVEL (1 << 1)
#define IRQL_PASSIVE_LEVEL (1 << 0)
#define IRQL_DISABLED_LEVEL (1ul << 3)
#define IRQL_DPC_LEVEL (1ul << 2)
#define IRQL_APC_LEVEL (1ul << 1)
#define IRQL_PASSIVE_LEVEL (1ul << 0)
k_irql SXAPI hal_set_irql(k_irql irql);

View File

@ -6,10 +6,12 @@
typedef uint32 handle_t;
typedef void (SXAPI *ref_free_func)(void*);
typedef struct
{
int32 ref_count;
callback_func free_routine;
ref_free_func free_routine;
} ref_node_t;
#define K_HANDLE_BASE (0x80000000ul)
@ -22,7 +24,7 @@ typedef struct
sx_status SXAPI rf_reference_setup(void);
sx_status SXAPI rf_reference_create(ref_node_t *ref,
callback_func free_func);
ref_free_func free_func);
sx_status SXAPI rf_reference_obj(ref_node_t *ref);

View File

@ -13,6 +13,8 @@ struct avl_tree_node
int32 height;
};
typedef int32 (SXAPI *avl_tree_compare_func)(struct avl_tree_node *left, struct avl_tree_node *right);
/*
* A comparison function between tree_node and your_node
* Returns:
@ -22,7 +24,7 @@ struct avl_tree_node
*/
struct avl_tree
{
callback_func compare;
avl_tree_compare_func compare;
struct avl_tree_node *root;
};
@ -32,7 +34,7 @@ void SXAPI lb_avl_tree_insert(struct avl_tree *tree, struct avl_tree_node *entry
struct avl_tree_node *SXAPI lb_avl_tree_delete(struct avl_tree *tree, struct avl_tree_node *entry);
void SXAPI lb_avl_tree_init(struct avl_tree *tree, callback_func compare);
void SXAPI lb_avl_tree_init(struct avl_tree *tree, avl_tree_compare_func compare);
struct avl_tree_node *SXAPI lb_avl_tree_largest(struct avl_tree *tree);

View File

@ -9,16 +9,22 @@ struct linked_list_node
struct linked_list_node *next;
};
/**
* @returns 0 if they are equal
* @param node each node in the list that will compare to arg
* @param arg supplied by user
*/
typedef int32 (SXAPI *linked_list_cmp_func)(struct linked_list_node *node, void *arg);
struct linked_list
{
struct linked_list_node *head;
struct linked_list_node *tail;
int32 size;
};
/*
* Returns true if current list node == your node
* false otherwise
*/
/* Linked list interfaces */
void SXAPI lb_linked_list_init(struct linked_list *list);
@ -32,16 +38,22 @@ struct linked_list_node *SXAPI lb_linked_list_pop_front(struct linked_list *list
struct linked_list_node *SXAPI lb_linked_list_pop_back(struct linked_list *list);
void SXAPI lb_linked_list_insert(struct linked_list *list, int32 index, struct linked_list_node *node);
void SXAPI lb_linked_list_insert_by_idx(struct linked_list *list, int32 index, struct linked_list_node *node);
void SXAPI lb_linked_list_insert_ref(struct linked_list *list, struct linked_list_node *prev_node, struct linked_list_node *node);
struct linked_list_node *SXAPI lb_linked_list_remove(struct linked_list *list, int32 index);
struct linked_list_node *SXAPI lb_linked_list_remove_ref(struct linked_list *list, struct linked_list_node *node);
struct linked_list_node *SXAPI lb_linked_list_remove_by_idx(struct linked_list *list, int32 index);
struct linked_list_node *SXAPI lb_linked_list_get(struct linked_list *list, int32 index);
struct linked_list_node *
SXAPI lb_linked_list_search(struct linked_list *list, void *obj, linked_list_cmp_func cmp_func);
/* Linked list node interfaces */
void SXAPI lb_linked_list_insert_by_ref(struct linked_list *list, struct linked_list_node *cur_node,
struct linked_list_node *new_node);
struct linked_list_node *SXAPI lb_linked_list_remove_by_ref(struct linked_list *list, struct linked_list_node *node);
struct linked_list_node *SXAPI lb_linked_list_next(struct linked_list_node *node);
struct linked_list_node *SXAPI lb_linked_list_prev(struct linked_list_node *node);
@ -50,7 +62,4 @@ struct linked_list_node *SXAPI lb_linked_list_first(struct linked_list *list);
struct linked_list_node *SXAPI lb_linked_list_last(struct linked_list *list);
int32 SXAPI lb_linked_list_search(struct linked_list *list, struct linked_list_node *target,
callback_func equals);
#endif

View File

@ -22,9 +22,6 @@ typedef _Bool bool;
#define TRUE (1)
#define FALSE (0)
typedef int32 (*callback_func)(void *kernel_args, void *user_args);
#define STRUCT_PACKED __attribute__((packed))
#define UNREFERENCED(x) {(x) = (x);}

View File

@ -6,11 +6,9 @@ void SXAPI ke_printf(const char *str, ...)
va_start(args, str);
ke_vprintf(str, args);
va_end(args);
return;
}
void SXAPI ke_vprintf(const char *str, va_list args)
{
hal_vprintf(str, args);
return;
}

View File

@ -12,7 +12,6 @@ void ke_rwwlock_init(k_rwwlock_t *lock)
lock->reader_ct = 0;
lock->writer_ct = 0;
}
return;
}
void ke_rwwlock_reader_lock(k_rwwlock_t *lock)
@ -29,7 +28,6 @@ void ke_rwwlock_reader_lock(k_rwwlock_t *lock)
ke_spin_unlock(&lock->r_mutex);
ke_spin_unlock(&lock->r_try);
}
return;
}
void ke_rwwlock_reader_unlock(k_rwwlock_t *lock)
@ -44,7 +42,6 @@ void ke_rwwlock_reader_unlock(k_rwwlock_t *lock)
}
ke_spin_unlock(&lock->r_mutex);
}
return;
}
void ke_rwwlock_writer_lock(k_rwwlock_t *lock)
@ -82,7 +79,6 @@ void ke_rwwlock_reader_unlock_lower_irql(k_rwwlock_t *lock, k_irql irql)
{
ke_rwwlock_reader_unlock(lock);
ke_lower_irql(irql);
return;
}
k_irql ke_rwwlock_writer_lock_raise_irql(k_rwwlock_t *lock, k_irql irql)
@ -96,5 +92,4 @@ void ke_rwwlock_writer_unlock_lower_irql(k_rwwlock_t *lock, k_irql irql)
{
ke_rwwlock_writer_unlock(lock);
ke_lower_irql(irql);
return;
}

View File

@ -16,7 +16,6 @@ void SXAPI ke_spin_lock(k_spin_lock_t *lock)
while (ke_interlocked_compare_exchange_32(&lock->val, 0, 1) != 0)
{}
}
return;
}
void SXAPI ke_spin_unlock(k_spin_lock_t *lock)
@ -25,7 +24,6 @@ void SXAPI ke_spin_unlock(k_spin_lock_t *lock)
{
lock->val = 0;
}
return;
}
k_irql SXAPI ke_spin_lock_raise_irql(k_spin_lock_t *lock, k_irql irql)
@ -46,5 +44,4 @@ void SXAPI ke_spin_unlock_lower_irql(k_spin_lock_t *lock, k_irql irql)
ke_spin_unlock(lock);
ke_lower_irql(irql);
}
return;
}

View File

@ -24,7 +24,7 @@ static _Bool initialized;
* = 0 if tree_node == your_node
* > 0 if tree_node > your_node
*/
static int32 mmp_base_paddr_compare(void *tree_node, void *my_node)
static int32 SXAPI mmp_base_paddr_compare(struct avl_tree_node* tree_node, struct avl_tree_node *my_node)
{
uintptr tree_base = OBTAIN_STRUCT_ADDR(tree_node,
struct phys_page_desc,

View File

@ -11,14 +11,12 @@ typedef struct
struct avl_tree_node tree_node;
handle_t handle;
ref_node_t *ref;
callback_func free_routine;
ref_free_func free_routine;
} handle_node_t;
static int32 rfp_handle_node_free(void *node, void *up)
static void SXAPI rfp_handle_node_free(void *node)
{
UNREFERENCED(up);
ke_free(node);
return 0;
}
// ===========================
@ -30,7 +28,7 @@ static bool initialized;
static k_spin_lock_t handle_tree_lock;
static uint32 handle_base;
static int32 rfp_handle_compare(void *tree_node, void *my_node)
static int32 rfp_handle_compare(struct avl_tree_node *tree_node, struct avl_tree_node *my_node)
{
handle_node_t *tcb = OBTAIN_STRUCT_ADDR(tree_node, handle_node_t, tree_node);
handle_node_t *my_tcb = OBTAIN_STRUCT_ADDR(my_node, handle_node_t, tree_node);
@ -74,7 +72,7 @@ sx_status SXAPI rf_reference_setup(void)
}
sx_status SXAPI rf_reference_create(ref_node_t *ref,
callback_func free_func)
ref_free_func free_func)
{
ke_assert(ke_get_irql() <= IRQL_DPC_LEVEL);
@ -122,7 +120,7 @@ sx_status SXAPI rf_dereference_obj(ref_node_t *ref_node)
if (old_ref_count == 1)
{
ref_node->free_routine(ref_node, NULL);
ref_node->free_routine(ref_node);
}
return result;
@ -220,7 +218,7 @@ static sx_status SXAPI rf_create_handle(ref_node_t *ref,
}
else
{
node->free_routine(node, NULL);
node->free_routine(node);
}
return result;
@ -256,7 +254,7 @@ static sx_status SXAPI rf_close_handle(handle_t handle)
if (free)
{
handle_node->free_routine(handle_node, NULL);
handle_node->free_routine(handle_node);
}
if (sx_success(status))

View File

@ -102,8 +102,8 @@ static struct avl_tree_node *SXAPI lbp_avl_tree_node_balance(struct avl_tree_nod
}
static struct avl_tree_node *SXAPI lbp_avl_tree_node_insert(struct avl_tree_node *root, struct avl_tree_node *node,
callback_func compare,
struct avl_tree_node *parent)
avl_tree_compare_func compare,
struct avl_tree_node *parent)
{
if (node == NULL || compare == NULL)
{
@ -289,9 +289,9 @@ static void SXAPI lbp_avl_tree_swap_nodes(struct avl_tree_node *node1, struct av
}
static struct avl_tree_node *SXAPI lbp_avl_tree_node_delete(struct avl_tree_node *root,
struct avl_tree_node *node,
callback_func compare,
struct avl_tree_node **deleted_node)
struct avl_tree_node *node,
avl_tree_compare_func compare,
struct avl_tree_node **deleted_node)
{
if (root == NULL || node == NULL || compare == NULL || deleted_node == NULL)
{
@ -351,7 +351,7 @@ static struct avl_tree_node *SXAPI lbp_avl_tree_node_delete(struct avl_tree_node
}
static struct avl_tree_node *SXAPI lbp_avl_tree_node_search(struct avl_tree_node *root, struct avl_tree_node *node,
callback_func compare)
avl_tree_compare_func compare)
{
if (root == NULL || compare == NULL)
{
@ -530,7 +530,7 @@ int32 SXAPI lb_avl_tree_size(struct avl_tree *tree)
return size;
}
void SXAPI lb_avl_tree_init(struct avl_tree *tree, callback_func compare)
void SXAPI lb_avl_tree_init(struct avl_tree *tree, avl_tree_compare_func compare)
{
if (tree != NULL)
{
@ -553,7 +553,7 @@ static int32 SXAPI lbp_avl_tree_node_calculate_height(struct avl_tree_node *tree
1;
}
static bool SXAPI lbp_avl_tree_node_test(struct avl_tree_node *tree, callback_func compare)
static bool SXAPI lbp_avl_tree_node_test(struct avl_tree_node *tree, avl_tree_compare_func compare)
{
if (tree == NULL)
{

View File

@ -1,6 +1,6 @@
#include "lib/linked_list.h"
static void SXAPI lbp_init_linked_list_node(struct linked_list_node *node)
static inline void SXAPI lbp_init_linked_list_node(struct linked_list_node *node)
{
if (node != NULL)
{
@ -9,75 +9,13 @@ static void SXAPI lbp_init_linked_list_node(struct linked_list_node *node)
}
}
static void SXAPI lbp_append_node(struct linked_list_node *target, struct linked_list_node *node)
{
if (target == NULL || node == NULL)
{
return;
}
struct linked_list_node *next = target->next;
// update the next node
if (next != NULL)
{
next->prev = node;
}
// update the target node
target->next = node;
// update the node itself
node->prev = target;
node->next = next;
}
// link target with node, suppose target is in the current list
static void SXAPI lbp_prepend_node(struct linked_list_node *target, struct linked_list_node *node)
{
if (target == NULL || node == NULL)
{
return;
}
struct linked_list_node *prev = target->prev;
// update the prev node
if (prev != NULL)
{
prev->next = node;
}
// update the target node
target->prev = node;
// update the node itself
node->next = target;
node->prev = prev;
}
static void SXAPI lbp_unlink_node(struct linked_list_node *node)
{
if (node == NULL)
{
return;
}
if (node->prev != NULL)
{
node->prev->next = node->next;
}
if (node->next != NULL)
{
node->next->prev = node->prev;
}
}
void SXAPI lb_linked_list_init(struct linked_list *list)
{
if (list != NULL)
{
list->head = NULL;
list->tail = NULL;
list->size = 0;
}
}
@ -87,19 +25,8 @@ int32 SXAPI lb_linked_list_size(struct linked_list *list)
{
return -1;
}
if (list->head == NULL)
{
return 0;
}
int32 size = 1;
struct linked_list_node *cur_node = list->head;
struct linked_list_node *tail = list->tail;
while ((cur_node != tail) && ((cur_node = cur_node->next) != NULL))
{
size++;
}
return size;
return list->size;
}
void SXAPI lb_linked_list_push_front(struct linked_list *list, struct linked_list_node *node)
@ -111,7 +38,7 @@ void SXAPI lb_linked_list_push_front(struct linked_list *list, struct linked_lis
lbp_init_linked_list_node(node);
lb_linked_list_insert_ref(list, NULL, node);
lb_linked_list_insert_by_ref(list, NULL, node);
}
void SXAPI lb_linked_list_push_back(struct linked_list *list, struct linked_list_node *node)
@ -123,7 +50,7 @@ void SXAPI lb_linked_list_push_back(struct linked_list *list, struct linked_list
lbp_init_linked_list_node(node);
lb_linked_list_insert_ref(list, list->tail, node);
lb_linked_list_insert_by_ref(list, list->tail, node);
}
struct linked_list_node *SXAPI lb_linked_list_pop_front(struct linked_list *list)
@ -132,7 +59,7 @@ struct linked_list_node *SXAPI lb_linked_list_pop_front(struct linked_list *list
{
return NULL;
}
return lb_linked_list_remove_ref(list, list->head);
return lb_linked_list_remove_by_ref(list, list->head);
}
struct linked_list_node *SXAPI lb_linked_list_pop_back(struct linked_list *list)
@ -142,48 +69,76 @@ struct linked_list_node *SXAPI lb_linked_list_pop_back(struct linked_list *list)
return NULL;
}
return lb_linked_list_remove_ref(list, list->tail);
return lb_linked_list_remove_by_ref(list, list->tail);
}
void SXAPI lb_linked_list_insert_ref(struct linked_list *list, struct linked_list_node *prev_node, struct linked_list_node *node)
void SXAPI lb_linked_list_insert_by_ref(struct linked_list *list, struct linked_list_node *cur_node,
struct linked_list_node *new_node)
{
if (list == NULL || node == NULL)
struct linked_list_node *left_node = NULL;
struct linked_list_node *right_node = NULL;
if (list == NULL || new_node == NULL)
{
return;
}
lbp_init_linked_list_node(node);
if (prev_node == NULL)
lbp_init_linked_list_node(new_node);
/*
* adjust the current node
*/
if (cur_node == NULL)
{
// if prev_node is NULL, then we are inserting to the head
// linked node with list->head
lbp_prepend_node(list->head, node);
if (list->tail == NULL)
{
// if the list is empty, we assign list->tail to node too
list->tail = node;
}
list->head = node;
new_node->next = list->head;
new_node->prev = NULL;
}
else
{
// if prev_node is not NULL, we are inserting to the middle or the end
// linked node with the prev_node
lbp_append_node(prev_node, node);
if (node->next == NULL)
{
// if it's the end
list->tail = node;
}
new_node->prev = cur_node;
new_node->next = cur_node->next;
}
/*
* assign left and right node
*/
if (cur_node == NULL)
{
left_node = NULL;
right_node = list->head == NULL ? NULL : list->head;
}
else
{
left_node = cur_node;
right_node = cur_node->next;
}
/*
* adjust left and right node accordingly
*/
if (left_node != NULL)
{
left_node->next = new_node;
}
else
{
list->head = new_node;
}
if (right_node != NULL)
{
right_node->prev = new_node;
}
else
{
list->tail = new_node;
}
list->size++;
}
void SXAPI lb_linked_list_insert(struct linked_list *list, int32 index, struct linked_list_node *node)
void SXAPI lb_linked_list_insert_by_idx(struct linked_list *list, int32 index, struct linked_list_node *node)
{
if (list == NULL || index < 0 || node == NULL)
{
@ -196,21 +151,22 @@ void SXAPI lb_linked_list_insert(struct linked_list *list, int32 index, struct l
{
if (index == 0)
{
lb_linked_list_insert_ref(list, NULL, node);
lb_linked_list_insert_by_ref(list, NULL, node);
}
}
else
{
lb_linked_list_insert_ref(list, prev_node, node);
lb_linked_list_insert_by_ref(list, prev_node, node);
}
}
struct linked_list_node *SXAPI lb_linked_list_remove(struct linked_list *list, int32 index)
struct linked_list_node *SXAPI lb_linked_list_remove_by_idx(struct linked_list *list, int32 index)
{
if (list == NULL || index < 0)
{
return NULL;
}
struct linked_list_node *cur_node = lb_linked_list_get(list, index);
if (cur_node == NULL)
@ -218,31 +174,49 @@ struct linked_list_node *SXAPI lb_linked_list_remove(struct linked_list *list, i
return NULL;
}
return lb_linked_list_remove_ref(list, cur_node);
return lb_linked_list_remove_by_ref(list, cur_node);
}
struct linked_list_node *SXAPI lb_linked_list_remove_ref(struct linked_list *list, struct linked_list_node *node)
/*
* returns the next node
*/
struct linked_list_node *SXAPI lb_linked_list_remove_by_ref(struct linked_list *list, struct linked_list_node *node)
{
struct linked_list_node *ret = NULL;
if (list == NULL || node == NULL)
{
return NULL;
return ret;
}
lbp_unlink_node(node);
if (node->next == NULL)
/*
* Adjust the left and right node
*/
if (node->prev != NULL)
{
list->tail = node->prev;
node->prev->next = node->next;
}
if (node->prev == NULL)
else
{
list->head = node->next;
}
if (node->next != NULL)
{
node->next->prev = node->prev;
}
else
{
list->tail = node->prev;
}
ret = node->next;
lbp_init_linked_list_node(node);
return node;
list->size--;
return ret;
}
struct linked_list_node *SXAPI lb_linked_list_get(struct linked_list *list, int32 index)
@ -295,35 +269,27 @@ struct linked_list_node *SXAPI lb_linked_list_last(struct linked_list *list)
return result;
}
int32 SXAPI lb_linked_list_search(struct linked_list *list, struct linked_list_node *target,
callback_func equals)
struct linked_list_node *SXAPI lb_linked_list_search(struct linked_list *list, void *obj, linked_list_cmp_func cmp_func)
{
if (list == NULL || target == NULL)
{
return -1;
}
int32 result = 0;
struct linked_list_node *ret = NULL;
struct linked_list_node *node = lb_linked_list_first(list);
if (list == NULL)
{
return NULL;
}
while (node != NULL)
{
if (equals != NULL)
if (cmp_func(node, obj) == 0)
{
if (equals(node, target))
{
return result;
}
ret = node;
break;
}
else
{
if (target->next == node->next && target->prev == node->prev)
{
return result;
}
}
result++;
node = lb_linked_list_next(node);
}
return -1;
return ret;
}

2
qemu.bat Normal file
View File

@ -0,0 +1,2 @@
@echo off
qemu-system-x86_64 -bios sim/qemu/qemu_bios.bin -cdrom out/secxkrnl.iso

BIN
qemu_bios.bin Normal file

Binary file not shown.

3
qemugdb Normal file
View File

@ -0,0 +1,3 @@
target remote localhost:1234
symbol-file secxkrnl.elf
break *0x1003000

View File

@ -1,10 +0,0 @@
include $(MK)/prologue.mk
SRC_$(d) := $(d)/avl_tree_test.c \
$(d)/driver.c \
$(d)/linked_list_test.c \
$(d)/salloc_test.c
include $(MK)/stdrules.mk
include $(MK)/epilogue.mk

View File

@ -1,6 +1,7 @@
#include "test/driver.h"
#include "test/test_case.h"
#include "driver.h"
#include "test_case.h"
#include "lib/avl_tree.h"
#include <stdio.h>
typedef struct
{
@ -15,31 +16,31 @@ static int_tree_node *create_tree_node(int val)
return rs;
}
static int32 compare(void *root1, void *node1)
static int32 SXAPI compare(struct avl_tree_node *root, struct avl_tree_node *node)
{
struct avl_tree_node *root = (struct avl_tree_node *) node1;
struct avl_tree_node *node = (struct avl_tree_node *) root1;
int_tree_node *rooti = OBTAIN_STRUCT_ADDR(root, int_tree_node, tree_entry);
int_tree_node *nodei = OBTAIN_STRUCT_ADDR(node, int_tree_node, tree_entry);
return rooti->val - nodei->val;
}
//static void _pre_order(struct avl_tree_node *node, bool root)
//{
// if (node == NULL)
// return;
// int_tree_node *my_node = OBTAIN_STRUCT_ADDR(node, tree_entry, int_tree_node);
// printf("%d-", my_node->val);
// _pre_order(node->left, false);
// _pre_order(node->right, false);
// if (root)
// printf("\n");
//}
//
//static void pre_order(struct avl_tree_node *node)
//{
// _pre_order(node, TRUE);
//}
static void _pre_order(struct avl_tree_node *node, bool root)
{
if (node == NULL)
return;
int_tree_node *my_node = OBTAIN_STRUCT_ADDR(node, int_tree_node, tree_entry);
printf("%d-", my_node->val);
_pre_order(node->left, FALSE);
_pre_order(node->right, FALSE);
if (root)
printf("\n");
}
static void pre_order(struct avl_tree_node *node)
{
#ifdef TDBG
_pre_order(node, TRUE);
#endif
}
static int counter = 0;
@ -87,12 +88,14 @@ static bool insert_simple_l(void)
lb_avl_tree_init(&tree, compare);
lb_avl_tree_insert(&tree, &create_tree_node(1)->tree_entry);
pre_order(tree.root);
lb_avl_tree_insert(&tree, &create_tree_node(2)->tree_entry);
pre_order(tree.root);
int val1[] = {1, 2};
result = result && pre_order_assert(&tree, val1, 2);
lb_avl_tree_insert(&tree, &create_tree_node(3)->tree_entry);
pre_order(tree.root);
int val2[] = {2, 1, 3};
result = result && pre_order_assert(&tree, val2, 3);
return result && lb_avl_tree_validate(&tree);
@ -895,7 +898,7 @@ static bool test_apocalypse(void)
}
void avl_tree_test(void)
void SXAPI avl_tree_test(void)
{
test_begin("AVL tree test");

View File

@ -1,6 +1,7 @@
#include "kernel/ke/print.h"
#include "test/driver.h"
#include "kernel/ke/alloc.h"
#include "driver.h"
#include "test_case.h"
#include <stdio.h>
#include <stdlib.h>
#define GAT_SIZE 256
#define CASE_NUM 32
@ -18,17 +19,17 @@ static char *test_name;
static void test_info(void)
{
ke_printf("[TD-INFO][%s] - ", test_name);
printf("[TD-INFO][%s] - ", test_name);
}
static void test_warning(void)
{
ke_printf("[TD-WARN][%s] - ", test_name);
printf("[TD-WARN][%s] - ", test_name);
}
static void test_error(void)
{
ke_printf("[TD-ERR][%s] - ", test_name);
printf("[TD-ERR][%s] - ", test_name);
}
static void gat_push(void *ptr)
@ -61,7 +62,7 @@ static void gat_free(void)
{
if (gat[i] != NULL)
{
ke_free(gat[i]);
free(gat[i]);
gat[i] = NULL;
}
}
@ -81,7 +82,7 @@ static void ginfo_push(char *case_name, bool success)
}
}
test_warning();
ke_printf("GINFO full, [%s] result not recorded.\n", r_case_name);
printf("GINFO full, [%s] result not recorded.\n", r_case_name);
}
void SXAPI test_begin(char *name)
@ -117,15 +118,15 @@ void SXAPI test_end(void)
}
}
test_info();
ke_printf("%s\n", failed > 0 ? "FAIL" : "PASS");
ke_printf(" %d cases executed. S: %d. F: %d.\n", total, success, failed);
printf("%s\n", failed > 0 ? "FAIL" : "PASS");
printf(" %d cases executed. S: %d. F: %d.\n", total, success, failed);
if (failed > 0)
{
for (int i = 0; i < CASE_NUM; i++)
{
if (ginfo[i].used && !ginfo[i].success)
{
ke_printf(" %s FAILED\n", ginfo[i].case_name);
printf(" %s FAILED\n", ginfo[i].case_name);
}
}
}
@ -139,14 +140,14 @@ void SXAPI *talloc(uint32 size)
{
if (!gat_full())
{
void *result = ke_alloc(size);
void *result = malloc(size);
gat_push(result);
return result;
}
else
{
test_error();
ke_printf("GAT full, rejecting further allocations.\n");
printf("GAT full, rejecting further allocations.\n");
}
return NULL;
}
@ -156,5 +157,13 @@ void SXAPI run_case(char *name, bool result)
ginfo_push(name, result);
}
int main(void)
{
linked_list_test();
salloc_test();
avl_tree_test();
return 0;
}

View File

@ -1,14 +1,20 @@
#include "test/driver.h"
#include "driver.h"
#include "test_case.h"
#include "lib/linked_list.h"
#include "test/test_case.h"
#include "lib/sxtdlib.h"
#include <stdio.h>
typedef struct
{
struct linked_list_node lnode;
int val;
int32 val;
} my_list_node;
static int32 SXAPI equals(struct linked_list_node *node, void *obj)
{
return ((int32)(uintptr) obj) - OBTAIN_STRUCT_ADDR(node, my_list_node, lnode)->val;
}
static bool validate_list(struct linked_list *list)
{
bool result = TRUE;
@ -34,6 +40,21 @@ static bool validate_list(struct linked_list *list)
return result;
}
static void print_list(struct linked_list *list)
{
#ifdef TDBG
struct linked_list_node *node = lb_linked_list_first(list);
while (node != NULL)
{
my_list_node *enode = OBTAIN_STRUCT_ADDR(node, my_list_node, lnode);
printf("%d->",enode->val);
node = lb_linked_list_next(node);
}
printf("[END]\n");
#endif
}
static bool assert_list(struct linked_list *list, int val[], int size)
{
@ -42,7 +63,7 @@ static bool assert_list(struct linked_list *list, int val[], int size)
if (!validate_list(list))
{
return TRUE;
return FALSE;
}
while (node != NULL && i < size)
@ -76,33 +97,11 @@ static bool assert_list(struct linked_list *list, int val[], int size)
return i == 0;
}
//void print_validate(struct linked_list *list)
//{
// my_list_node* node = (my_list_node*) linked_list_first(list);
// while(node != NULL)
// {
// hal_printf("%d", node->val);
// node = (my_list_node*) linked_list_next((struct linked_list_node *) node);
// }
//
// hal_printf("======");
// node = (my_list_node*) linked_list_last(list);
// while(node != NULL)
// {
// hal_printf("%d", node->val);
// node = (my_list_node*) linked_list_prev((struct linked_list_node *) node);
// }
//
// validate_list(list);
// hal_printf("\n");
// return;
//}
static void insert_val(struct linked_list *list, int index, int val)
{
my_list_node *a = (my_list_node *) talloc(sizeof(my_list_node));
a->val = val;
lb_linked_list_insert(list, index, &a->lnode);
lb_linked_list_insert_by_idx(list, index, &a->lnode);
}
static void push_back_val(struct linked_list *list, int val)
@ -128,6 +127,7 @@ static bool insert_test_beginning(void)
insert_val(&list, 0, 1);
insert_val(&list, 0, 2);
insert_val(&list, 0, 3);
print_list(&list);
// 3210==0123
int val[4] = {3, 2, 1, 0};
@ -160,6 +160,7 @@ static bool insert_test_end(void)
insert_val(&list, 1, 1);
insert_val(&list, 2, 2);
insert_val(&list, 3, 3);
print_list(&list);
int val[] = {0, 1, 2, 3};
return assert_list(&list, val, 4);
@ -187,8 +188,8 @@ static bool insert_test_invalid(void)
// NULL
insert_val(NULL, 1, 4);
lb_linked_list_insert_ref(NULL, list.head, list.tail);
lb_linked_list_insert_ref(&list, list.head, NULL);
lb_linked_list_insert_by_ref(NULL, list.head, list.tail);
lb_linked_list_insert_by_ref(&list, list.head, NULL);
int val[] = {0, 1, 2, 3};
return assert_list(&list, val, 4);
@ -204,8 +205,10 @@ static bool remove_test_beginning(void)
insert_val(&list, 0, 2);
insert_val(&list, 0, 3);
lb_linked_list_remove(&list, 0);
lb_linked_list_remove(&list, 0);
lb_linked_list_remove_by_idx(&list, 0);
//print_list(&list);
lb_linked_list_remove_by_idx(&list, 0);
//print_list(&list);
// 10==01
int val[] = {1, 0};
@ -225,8 +228,11 @@ static bool remove_test_middle(void)
insert_val(&list, 0, 4);
insert_val(&list, 0, 5);
lb_linked_list_remove(&list, 1);
lb_linked_list_remove(&list, 2);
print_list(&list);
lb_linked_list_remove_by_idx(&list, 1);
print_list(&list);
lb_linked_list_remove_by_idx(&list, 2);
print_list(&list);
// 5310=====0135
int val[] = {5, 3, 1, 0};
@ -239,12 +245,18 @@ static bool remove_test_end(void)
lb_linked_list_init(&list);
insert_val(&list, 0, 0);
print_list(&list);
insert_val(&list, 1, 1);
print_list(&list);
insert_val(&list, 2, 2);
print_list(&list);
insert_val(&list, 3, 3);
lb_linked_list_remove(&list, 3);
lb_linked_list_remove(&list, 2);
print_list(&list);
lb_linked_list_remove_by_idx(&list, 3);
print_list(&list);
lb_linked_list_remove_by_idx(&list, 2);
print_list(&list);
int val[] = {0, 1};
return assert_list(&list, val, 2);
@ -261,10 +273,10 @@ static bool remove_test_all(void)
insert_val(&list, 2, 2);
insert_val(&list, 3, 3);
lb_linked_list_remove(&list, 0);
lb_linked_list_remove(&list, 0);
lb_linked_list_remove(&list, 0);
lb_linked_list_remove(&list, 0);
lb_linked_list_remove_by_idx(&list, 0);
lb_linked_list_remove_by_idx(&list, 0);
lb_linked_list_remove_by_idx(&list, 0);
lb_linked_list_remove_by_idx(&list, 0);
result = result && assert_list(&list, NULL, 0);
@ -273,10 +285,10 @@ static bool remove_test_all(void)
insert_val(&list, 2, 2);
insert_val(&list, 3, 3);
lb_linked_list_remove(&list, 3);
lb_linked_list_remove(&list, 2);
lb_linked_list_remove(&list, 1);
lb_linked_list_remove(&list, 0);
lb_linked_list_remove_by_idx(&list, 3);
lb_linked_list_remove_by_idx(&list, 2);
lb_linked_list_remove_by_idx(&list, 1);
lb_linked_list_remove_by_idx(&list, 0);
result = result && assert_list(&list, NULL, 0);
@ -285,10 +297,10 @@ static bool remove_test_all(void)
insert_val(&list, 2, 2);
insert_val(&list, 3, 3);
lb_linked_list_remove(&list, 1);
lb_linked_list_remove(&list, 1);
lb_linked_list_remove(&list, 1);
lb_linked_list_remove(&list, 0);
lb_linked_list_remove_by_idx(&list, 1);
lb_linked_list_remove_by_idx(&list, 1);
lb_linked_list_remove_by_idx(&list, 1);
lb_linked_list_remove_by_idx(&list, 0);
result = result && assert_list(&list, NULL, 0);
@ -306,19 +318,19 @@ static bool remove_test_invalid(void)
insert_val(&list, 0, 0);
// large index
lb_linked_list_remove(&list, 5);
lb_linked_list_remove(&list, 6);
lb_linked_list_remove(&list, 999);
lb_linked_list_remove_by_idx(&list, 5);
lb_linked_list_remove_by_idx(&list, 6);
lb_linked_list_remove_by_idx(&list, 999);
// small index
lb_linked_list_remove(&list, -1);
lb_linked_list_remove(&list, -2);
lb_linked_list_remove(&list, -999);
lb_linked_list_remove_by_idx(&list, -1);
lb_linked_list_remove_by_idx(&list, -2);
lb_linked_list_remove_by_idx(&list, -999);
// NULL
lb_linked_list_remove(NULL, 1);
lb_linked_list_remove_ref(NULL, list.head);
lb_linked_list_remove_ref(&list, NULL);
lb_linked_list_remove_by_idx(NULL, 1);
lb_linked_list_remove_by_ref(NULL, list.head);
lb_linked_list_remove_by_ref(&list, NULL);
// 0123=====3210
int val[] = {0, 1, 2, 3};
@ -401,11 +413,6 @@ static bool push_pop_back_test(void)
return result;
}
static int32 equals(void *a, void *b)
{
return ((int64) (struct linked_list_node *) b) ==
OBTAIN_STRUCT_ADDR((struct linked_list_node *) a, my_list_node, lnode)->val;
}
static bool search_test(void)
{
@ -421,17 +428,13 @@ static bool search_test(void)
int val1[] = {1, 2, 3, 4};
result = result && assert_list(&list, val1, 4);
result = result && (lb_linked_list_search(&list, (void *) 4, equals) == 3);
result = result && (lb_linked_list_search(&list, (struct linked_list_node *) 3, equals) == 2);
result = result && (lb_linked_list_search(&list, (struct linked_list_node *) 2, equals) == 1);
result = result && (lb_linked_list_search(&list, (struct linked_list_node *) 1, equals) == 0);
result = result && (lb_linked_list_search(&list, NULL, equals) == -1);
result = result && (lb_linked_list_search(NULL, (struct linked_list_node *) 1, equals) == -1);
struct linked_list_node *node = lb_linked_list_get(&list, 1);
result = result && (lb_linked_list_search(&list, node, NULL) == 1);
result = result && (lb_linked_list_search(&list, (void *) 4, equals) != NULL);
result = result && (lb_linked_list_search(&list, (void *) 3, equals) != NULL);
result = result && (lb_linked_list_search(&list, (void *) 2, equals) != NULL);
result = result && (lb_linked_list_search(&list, (void *) 1, equals) != NULL);
result = result && (lb_linked_list_search(&list, NULL, equals) == NULL);
result = result && (lb_linked_list_search(NULL, (void *) 1, equals) == NULL);
result = result && assert_list(&list, val1, 4);

View File

@ -1,6 +1,6 @@
#include "test/driver.h"
#include "driver.h"
#include "lib/salloc.h"
#include "test/test_case.h"
#include "test_case.h"
typedef union
{