Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
5793f6a178
|
|||
|
57929be1af
|
|||
|
3e3ea41dc8
|
|||
|
e4597aef7d
|
|||
|
06b2c8ad8a
|
28
.gitea/workflows/ci.yaml
Normal file
28
.gitea/workflows/ci.yaml
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
name: libakstdlib CI Build
|
||||||
|
run-name: ${{ gitea.actor }} libakstdlib test
|
||||||
|
on: [push]
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
cmake_build:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- run: echo "Triggered by ${{ gitea.event_name }} from ${{ gitea.repository }}@${{ gitea.ref }}. Building on ${{ runner.os }}."
|
||||||
|
- name: Check out repository code
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
- name: dependencies
|
||||||
|
run: |
|
||||||
|
sudo apt-get update -y
|
||||||
|
sudo apt-get install -y cmake gcc moreutils
|
||||||
|
# Depends on libakerror@main
|
||||||
|
git clone https://source.starfort.tech/andrew/libakerror.git
|
||||||
|
cd libakerror
|
||||||
|
cmake -S . -B build
|
||||||
|
cmake --build build
|
||||||
|
cmake --install build
|
||||||
|
- name: build and test
|
||||||
|
run: |
|
||||||
|
cmake -S . -B build
|
||||||
|
cmake --build build
|
||||||
|
sudo cmake --install build
|
||||||
|
cmake --build build --target test
|
||||||
|
- run: echo "🍏 This job's status is ${{ job.status }}."
|
||||||
3
.gitmodules
vendored
Normal file
3
.gitmodules
vendored
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
[submodule "deps/libakerror"]
|
||||||
|
path = deps/libakerror
|
||||||
|
url = https://source.starfort.tech/andrew/libakerror.git
|
||||||
@@ -1,6 +1,10 @@
|
|||||||
cmake_minimum_required(VERSION 3.10)
|
cmake_minimum_required(VERSION 3.10)
|
||||||
project(akstdlib LANGUAGES C)
|
project(akstdlib LANGUAGES C)
|
||||||
|
|
||||||
|
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -ggdb -pg")
|
||||||
|
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -g -ggdb -pg")
|
||||||
|
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -g -ggdb -pg")
|
||||||
|
|
||||||
if(TARGET akerror::akerror)
|
if(TARGET akerror::akerror)
|
||||||
message(STATUS "FOUND akerror::akerror")
|
message(STATUS "FOUND akerror::akerror")
|
||||||
else()
|
else()
|
||||||
@@ -11,10 +15,14 @@ include(CTest)
|
|||||||
include(GNUInstallDirs)
|
include(GNUInstallDirs)
|
||||||
include(CMakePackageConfigHelpers)
|
include(CMakePackageConfigHelpers)
|
||||||
|
|
||||||
|
if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
|
||||||
|
add_subdirectory(deps/libakerror EXCLUDE_FROM_ALL)
|
||||||
|
else()
|
||||||
if(NOT TARGET akerror::akerror)
|
if(NOT TARGET akerror::akerror)
|
||||||
find_package(PkgConfig REQUIRED)
|
find_package(PkgConfig REQUIRED)
|
||||||
find_package(akerror REQUIRED)
|
find_package(akerror REQUIRED)
|
||||||
endif()
|
endif()
|
||||||
|
endif()
|
||||||
|
|
||||||
set(akstdlib_install_cmakedir "${CMAKE_INSTALL_LIBDIR}/cmake/akstdlib")
|
set(akstdlib_install_cmakedir "${CMAKE_INSTALL_LIBDIR}/cmake/akstdlib")
|
||||||
set(prefix ${CMAKE_INSTALL_PREFIX})
|
set(prefix ${CMAKE_INSTALL_PREFIX})
|
||||||
@@ -67,4 +75,13 @@ install(FILES
|
|||||||
DESTINATION ${akstdlib_install_cmakedir}
|
DESTINATION ${akstdlib_install_cmakedir}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
add_executable(test_linkedlist tests/test_linkedlist.c)
|
||||||
|
target_link_libraries(test_linkedlist PRIVATE akstdlib)
|
||||||
|
add_test(NAME linkedlist COMMAND test_linkedlist)
|
||||||
|
|
||||||
|
add_executable(test_tree tests/test_tree.c)
|
||||||
|
target_link_libraries(test_tree PRIVATE akstdlib)
|
||||||
|
add_test(NAME tree COMMAND test_tree)
|
||||||
|
|
||||||
|
|
||||||
# pkgconfig
|
# pkgconfig
|
||||||
|
|||||||
3
README.md
Normal file
3
README.md
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
# README
|
||||||
|
|
||||||
|

|
||||||
1
deps/libakerror
vendored
Submodule
1
deps/libakerror
vendored
Submodule
Submodule deps/libakerror added at 4fad0cec59
@@ -43,7 +43,6 @@ akerr_ErrorContext AKERR_NOIGNORE *aksl_memset(void *s, int c, size_t n);
|
|||||||
akerr_ErrorContext AKERR_NOIGNORE *aksl_memcpy(void *d, void *s, size_t n);
|
akerr_ErrorContext AKERR_NOIGNORE *aksl_memcpy(void *d, void *s, size_t n);
|
||||||
akerr_ErrorContext AKERR_NOIGNORE *aksl_free(void *ptr);
|
akerr_ErrorContext AKERR_NOIGNORE *aksl_free(void *ptr);
|
||||||
|
|
||||||
|
|
||||||
akerr_ErrorContext AKERR_NOIGNORE *aksl_printf(int *count, const char *restrict format, ...);
|
akerr_ErrorContext AKERR_NOIGNORE *aksl_printf(int *count, const char *restrict format, ...);
|
||||||
akerr_ErrorContext AKERR_NOIGNORE *aksl_fprintf(int *count, FILE *restrict stream, const char *restrict format, ...);
|
akerr_ErrorContext AKERR_NOIGNORE *aksl_fprintf(int *count, FILE *restrict stream, const char *restrict format, ...);
|
||||||
akerr_ErrorContext AKERR_NOIGNORE *aksl_sprintf(int *count, char *restrict str, const char *restrict format, ...);
|
akerr_ErrorContext AKERR_NOIGNORE *aksl_sprintf(int *count, char *restrict str, const char *restrict format, ...);
|
||||||
@@ -55,8 +54,10 @@ akerr_ErrorContext AKERR_NOIGNORE *aksl_atof(const char *nptr, double *dest);
|
|||||||
|
|
||||||
akerr_ErrorContext AKERR_NOIGNORE *aksl_realpath(const char *restrict path, char *restrict resolved_path);
|
akerr_ErrorContext AKERR_NOIGNORE *aksl_realpath(const char *restrict path, char *restrict resolved_path);
|
||||||
|
|
||||||
|
akerr_ErrorContext AKERR_NOIGNORE *aksl_strhash_djb2(char *str, size_t len, uint32_t *hashval);
|
||||||
|
|
||||||
// Linked list functions
|
// Linked list functions
|
||||||
akerr_ErrorContext AKERR_NOIGNORE *aksl_list_push(aksl_ListNode *list, aksl_ListNode *obj);
|
akerr_ErrorContext AKERR_NOIGNORE *aksl_list_append(aksl_ListNode *list, aksl_ListNode *obj);
|
||||||
akerr_ErrorContext AKERR_NOIGNORE *aksl_list_pop(aksl_ListNode *node);
|
akerr_ErrorContext AKERR_NOIGNORE *aksl_list_pop(aksl_ListNode *node);
|
||||||
akerr_ErrorContext AKERR_NOIGNORE *aksl_list_iterate(aksl_ListNode *list, aksl_ListNodeIterator iter, void *data);
|
akerr_ErrorContext AKERR_NOIGNORE *aksl_list_iterate(aksl_ListNode *list, aksl_ListNodeIterator iter, void *data);
|
||||||
|
|
||||||
|
|||||||
71
src/stdlib.c
71
src/stdlib.c
@@ -4,6 +4,7 @@
|
|||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
akerr_ErrorContext AKERR_NOIGNORE *aksl_malloc(size_t size, void **dst)
|
akerr_ErrorContext AKERR_NOIGNORE *aksl_malloc(size_t size, void **dst)
|
||||||
{
|
{
|
||||||
@@ -177,7 +178,20 @@ akerr_ErrorContext AKERR_NOIGNORE *aksl_realpath(const char *restrict path, char
|
|||||||
SUCCEED_RETURN(e);
|
SUCCEED_RETURN(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
akerr_ErrorContext AKERR_NOIGNORE *aksl_list_push(aksl_ListNode *list, aksl_ListNode *obj)
|
akerr_ErrorContext AKERR_NOIGNORE *aksl_strhash_djb2(char *str, size_t len, uint32_t *hashval)
|
||||||
|
{
|
||||||
|
PREPARE_ERROR(e);
|
||||||
|
FAIL_ZERO_RETURN(e, str, AKERR_NULLPOINTER, "str");
|
||||||
|
FAIL_ZERO_RETURN(e, hashval, AKERR_NULLPOINTER, "hashval");
|
||||||
|
uint32_t h = 5381;
|
||||||
|
while (len--) {
|
||||||
|
h = ((h << 5) + h) + *str++;
|
||||||
|
}
|
||||||
|
*hashval = h;
|
||||||
|
SUCCEED_RETURN(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
akerr_ErrorContext AKERR_NOIGNORE *aksl_list_append(aksl_ListNode *list, aksl_ListNode *obj)
|
||||||
{
|
{
|
||||||
PREPARE_ERROR(e);
|
PREPARE_ERROR(e);
|
||||||
FAIL_ZERO_RETURN(e, list, AKERR_NULLPOINTER, "list");
|
FAIL_ZERO_RETURN(e, list, AKERR_NULLPOINTER, "list");
|
||||||
@@ -185,19 +199,20 @@ akerr_ErrorContext AKERR_NOIGNORE *aksl_list_push(aksl_ListNode *list, aksl_List
|
|||||||
aksl_ListNode *slow = list;
|
aksl_ListNode *slow = list;
|
||||||
aksl_ListNode *fast = list;
|
aksl_ListNode *fast = list;
|
||||||
aksl_ListNode *tail = list;
|
aksl_ListNode *tail = list;
|
||||||
do {
|
while ( fast != NULL && fast->next != NULL ) {
|
||||||
if ( fast != NULL && fast->next != NULL ) {
|
|
||||||
fast = fast->next->next;
|
|
||||||
}
|
|
||||||
tail = slow;
|
tail = slow;
|
||||||
slow = slow->next;
|
slow = slow->next;
|
||||||
if ( fast != NULL && fast == slow) {
|
fast = fast->next->next;
|
||||||
FAIL(e, AKERR_CIRCULAR_REFERENCE, "%p", list);
|
if ( fast == slow) {
|
||||||
|
FAIL_RETURN(e, AKERR_CIRCULAR_REFERENCE, "%p", list);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if ( fast != NULL ) {
|
||||||
|
tail = fast;
|
||||||
}
|
}
|
||||||
} while ( slow != NULL || (fast != NULL && fast->next != NULL) );
|
|
||||||
tail->next = obj;
|
tail->next = obj;
|
||||||
obj->next = NULL;
|
obj->next = NULL;
|
||||||
obj->prev = slow;
|
obj->prev = tail;
|
||||||
SUCCEED_RETURN(e);
|
SUCCEED_RETURN(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -251,9 +266,10 @@ akerr_ErrorContext AKERR_NOIGNORE *aksl_tree_iterate(
|
|||||||
if ( lfree == NULL ) {
|
if ( lfree == NULL ) {
|
||||||
lfree = &aksl_free;
|
lfree = &aksl_free;
|
||||||
}
|
}
|
||||||
|
ATTEMPT {
|
||||||
switch ( searchmode ) {
|
switch ( searchmode ) {
|
||||||
case AKSL_TREE_SEARCH_DFS_PREORDER:
|
case AKSL_TREE_SEARCH_DFS_PREORDER:
|
||||||
PASS(e, iter(root, data));
|
CATCH(e, iter(root, data));
|
||||||
if ( root->left != NULL ) {
|
if ( root->left != NULL ) {
|
||||||
PASS(e, aksl_tree_iterate(root->left, iter, lalloc, lfree, searchmode, data, NULL));
|
PASS(e, aksl_tree_iterate(root->left, iter, lalloc, lfree, searchmode, data, NULL));
|
||||||
}
|
}
|
||||||
@@ -268,13 +284,13 @@ akerr_ErrorContext AKERR_NOIGNORE *aksl_tree_iterate(
|
|||||||
if ( root-> right != NULL ) {
|
if ( root-> right != NULL ) {
|
||||||
PASS(e, aksl_tree_iterate(root->right, iter, lalloc, lfree, searchmode, data, NULL));
|
PASS(e, aksl_tree_iterate(root->right, iter, lalloc, lfree, searchmode, data, NULL));
|
||||||
}
|
}
|
||||||
PASS(e, iter(root, data));
|
CATCH(e, iter(root, data));
|
||||||
break;
|
break;
|
||||||
case AKSL_TREE_SEARCH_DFS_INORDER:
|
case AKSL_TREE_SEARCH_DFS_INORDER:
|
||||||
if ( root->left != NULL ) {
|
if ( root->left != NULL ) {
|
||||||
PASS(e, aksl_tree_iterate(root->left, iter, lalloc, lfree, searchmode, data, NULL));
|
PASS(e, aksl_tree_iterate(root->left, iter, lalloc, lfree, searchmode, data, NULL));
|
||||||
}
|
}
|
||||||
PASS(e, iter(root, data));
|
CATCH(e, iter(root, data));
|
||||||
if ( root-> right != NULL ) {
|
if ( root-> right != NULL ) {
|
||||||
PASS(e, aksl_tree_iterate(root->right, iter, lalloc, lfree, searchmode, data, NULL));
|
PASS(e, aksl_tree_iterate(root->right, iter, lalloc, lfree, searchmode, data, NULL));
|
||||||
}
|
}
|
||||||
@@ -284,6 +300,12 @@ akerr_ErrorContext AKERR_NOIGNORE *aksl_tree_iterate(
|
|||||||
FAIL_RETURN(e, AKERR_NOT_IMPLEMENTED, "Searchmode %d", searchmode);
|
FAIL_RETURN(e, AKERR_NOT_IMPLEMENTED, "Searchmode %d", searchmode);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
} CLEANUP {
|
||||||
|
} PROCESS(e) {
|
||||||
|
} HANDLE(e, AKERR_ITERATOR_BREAK) {
|
||||||
|
// This is not an error condition, it's just telling us to stop early
|
||||||
|
SUCCEED_RETURN(e);
|
||||||
|
} FINISH(e, true);
|
||||||
SUCCEED_RETURN(e);
|
SUCCEED_RETURN(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -306,16 +328,23 @@ akerr_ErrorContext AKERR_NOIGNORE *aksl_list_iterate(aksl_ListNode *list, aksl_L
|
|||||||
FAIL_ZERO_RETURN(e, iter, AKERR_NULLPOINTER, "iter");
|
FAIL_ZERO_RETURN(e, iter, AKERR_NULLPOINTER, "iter");
|
||||||
aksl_ListNode *slow = list;
|
aksl_ListNode *slow = list;
|
||||||
aksl_ListNode *fast = list;
|
aksl_ListNode *fast = list;
|
||||||
aksl_ListNode *tail = list;
|
while ( fast != NULL && fast->next != NULL ) {
|
||||||
do {
|
|
||||||
if ( fast != NULL && fast->next != NULL ) {
|
|
||||||
fast = fast->next->next;
|
|
||||||
}
|
|
||||||
PASS(e, iter(slow, data));
|
|
||||||
slow = slow->next;
|
slow = slow->next;
|
||||||
if ( fast != NULL && fast == slow) {
|
fast = fast->next->next;
|
||||||
FAIL(e, AKERR_CIRCULAR_REFERENCE, "%p", list);
|
if ( fast == slow) {
|
||||||
|
FAIL_RETURN(e, AKERR_CIRCULAR_REFERENCE, "%p", list);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
while ( slow != NULL ) {
|
||||||
|
ATTEMPT {
|
||||||
|
CATCH(e, iter(slow, data));
|
||||||
|
slow = slow->next;
|
||||||
|
} CLEANUP {
|
||||||
|
} PROCESS(e) {
|
||||||
|
} HANDLE(e, AKERR_ITERATOR_BREAK) {
|
||||||
|
// This is not an error condition, it's just telling us to stop early
|
||||||
|
SUCCEED_RETURN(e);
|
||||||
|
} FINISH(e, true);
|
||||||
}
|
}
|
||||||
} while ( slow != NULL || (fast != NULL && fast->next != NULL) );
|
|
||||||
SUCCEED_RETURN(e);
|
SUCCEED_RETURN(e);
|
||||||
}
|
}
|
||||||
|
|||||||
75
tests/test_linkedlist.c
Normal file
75
tests/test_linkedlist.c
Normal file
@@ -0,0 +1,75 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <akstdlib.h>
|
||||||
|
|
||||||
|
|
||||||
|
// This iterator does nothing but print the node names it is visiting
|
||||||
|
akerr_ErrorContext AKERR_NOIGNORE *myiter(aksl_ListNode *node, void *data)
|
||||||
|
{
|
||||||
|
int count = 0;
|
||||||
|
PREPARE_ERROR(e);
|
||||||
|
FAIL_ZERO_RETURN(e, node, AKERR_NULLPOINTER, "node");
|
||||||
|
FAIL_ZERO_RETURN(e, node->data, AKERR_NULLPOINTER, "node->data");
|
||||||
|
PASS(e, aksl_fprintf(&count, stderr, "Visiting node : %s\n", node->data));
|
||||||
|
SUCCEED_RETURN(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
// This iterator function exits early once the index in `(int *)data` is reached
|
||||||
|
akerr_ErrorContext AKERR_NOIGNORE *myiter_earlyhalt(aksl_ListNode *node, void *data)
|
||||||
|
{
|
||||||
|
int *idx;
|
||||||
|
int count = 0;
|
||||||
|
PREPARE_ERROR(e);
|
||||||
|
FAIL_ZERO_RETURN(e, node, AKERR_NULLPOINTER, "node");
|
||||||
|
FAIL_ZERO_RETURN(e, node->data, AKERR_NULLPOINTER, "node->data");
|
||||||
|
FAIL_ZERO_RETURN(e, data, AKERR_NULLPOINTER, "data");
|
||||||
|
idx = (int *)data;
|
||||||
|
if ( *idx == 1 ) {
|
||||||
|
// This exception is eaten by the iterator, we will never see it
|
||||||
|
FAIL_RETURN(e, AKERR_ITERATOR_BREAK, "stop");
|
||||||
|
}
|
||||||
|
*idx += 1;
|
||||||
|
SUCCEED_RETURN(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
PREPARE_ERROR(e);
|
||||||
|
int idx = 0;
|
||||||
|
int count = 0;
|
||||||
|
aksl_ListNode mylist;
|
||||||
|
aksl_ListNode node1;
|
||||||
|
aksl_ListNode node2;
|
||||||
|
|
||||||
|
ATTEMPT {
|
||||||
|
memset((void *)&mylist, 0x00, sizeof(aksl_ListNode));
|
||||||
|
memset((void *)&node1, 0x00, sizeof(aksl_ListNode));
|
||||||
|
memset((void *)&node2, 0x00, sizeof(aksl_ListNode));
|
||||||
|
|
||||||
|
mylist.data = "Root";
|
||||||
|
|
||||||
|
node1.data = "Node 1";
|
||||||
|
CATCH(e, aksl_list_append(&mylist, &node1));
|
||||||
|
|
||||||
|
node2.data = "Node 2";
|
||||||
|
CATCH(e, aksl_list_append(&mylist, &node2));
|
||||||
|
|
||||||
|
// Iterate over all nodes in the list using the myiter() function
|
||||||
|
CATCH(e, aksl_list_iterate(&mylist, &myiter, NULL));
|
||||||
|
|
||||||
|
// Iterate over up to the first 2 nodes in the list and then exit early
|
||||||
|
idx = 0;
|
||||||
|
CATCH(e, aksl_list_iterate(&mylist, &myiter_earlyhalt, &idx));
|
||||||
|
CATCH(e, aksl_fprintf(&count, stderr, "Iterator exited early at index %d\n", idx));
|
||||||
|
|
||||||
|
// Break the list with a circular reference, and iterate it again
|
||||||
|
node2.next = &mylist;
|
||||||
|
CATCH(e, aksl_list_iterate(&mylist, &myiter, NULL));
|
||||||
|
|
||||||
|
} CLEANUP {
|
||||||
|
} PROCESS(e) {
|
||||||
|
} HANDLE(e, AKERR_CIRCULAR_REFERENCE) {
|
||||||
|
fprintf(stderr, "Circular reference error caught\n");
|
||||||
|
} FINISH_NORETURN(e);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
96
tests/test_tree.c
Normal file
96
tests/test_tree.c
Normal file
@@ -0,0 +1,96 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <akstdlib.h>
|
||||||
|
|
||||||
|
#define MAX_LEAVES 7
|
||||||
|
|
||||||
|
typedef struct TreeSearchParams
|
||||||
|
{
|
||||||
|
void *value;
|
||||||
|
int steps;
|
||||||
|
aksl_TreeNode *node;
|
||||||
|
} TreeSearchParams;
|
||||||
|
|
||||||
|
// This iterator does nothing but print the node names it is visiting
|
||||||
|
akerr_ErrorContext AKERR_NOIGNORE *myiter(aksl_TreeNode *node, void *data)
|
||||||
|
{
|
||||||
|
int count = 0;
|
||||||
|
TreeSearchParams *parms = NULL;
|
||||||
|
PREPARE_ERROR(e);
|
||||||
|
FAIL_ZERO_RETURN(e, node, AKERR_NULLPOINTER, "node");
|
||||||
|
FAIL_ZERO_RETURN(e, data, AKERR_NULLPOINTER, "data");
|
||||||
|
parms = (TreeSearchParams *)data;
|
||||||
|
parms->steps += 1;
|
||||||
|
if ( node->leaf == parms->value ) {
|
||||||
|
parms->node = node;
|
||||||
|
FAIL_RETURN(e, AKERR_ITERATOR_BREAK, "stop");
|
||||||
|
}
|
||||||
|
SUCCEED_RETURN(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
PREPARE_ERROR(e);
|
||||||
|
aksl_TreeNode tree[MAX_LEAVES];
|
||||||
|
TreeSearchParams parms = {
|
||||||
|
.value = (void *)17336,
|
||||||
|
.steps = 0,
|
||||||
|
.node = NULL
|
||||||
|
};
|
||||||
|
|
||||||
|
ATTEMPT {
|
||||||
|
memset((void *)&tree, 0x00, sizeof(aksl_TreeNode) * MAX_LEAVES);
|
||||||
|
/*
|
||||||
|
* Here we build a 3 level tree
|
||||||
|
*
|
||||||
|
* LEFT RIGHT
|
||||||
|
* TREE[0]
|
||||||
|
* +--------^^---------+
|
||||||
|
* | |
|
||||||
|
* TREE[1] TREE[2]
|
||||||
|
* +---^^---+ +---^^---+
|
||||||
|
* | | | |
|
||||||
|
*TREE[3] TREE[4] TREE[5] TREE[6]
|
||||||
|
*/
|
||||||
|
tree[0].left = &tree[1];
|
||||||
|
tree[0].right = &tree[2];
|
||||||
|
tree[1].left = &tree[3];
|
||||||
|
tree[1].right = &tree[4];
|
||||||
|
tree[2].left = &tree[5];
|
||||||
|
tree[2].right = &tree[6];
|
||||||
|
|
||||||
|
// Hide a value in tree[6]
|
||||||
|
tree[6].leaf = (void *)17336;
|
||||||
|
|
||||||
|
// Search for the value 17336 using DFS_PREORDER
|
||||||
|
CATCH(e, aksl_tree_iterate(&tree[0], &myiter, NULL, NULL, AKSL_TREE_SEARCH_DFS_PREORDER, &parms, NULL));
|
||||||
|
if ( parms.node != &tree[6] ) {
|
||||||
|
FAIL_BREAK(e, AKERR_API, "DFS_PREORDER_SEARCH didn't find the node");
|
||||||
|
}
|
||||||
|
if ( parms.steps != 7 ) {
|
||||||
|
FAIL_BREAK(e, AKERR_API, "DFS_PREORDER_SEARCH should've found the node in 7 steps, instead took %d", parms.steps);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Search for the value 17336 using DFS_INORDER
|
||||||
|
CATCH(e, aksl_tree_iterate(&tree[0], &myiter, NULL, NULL, AKSL_TREE_SEARCH_DFS_INORDER, &parms, NULL));
|
||||||
|
if ( parms.node != &tree[6] ) {
|
||||||
|
FAIL_BREAK(e, AKERR_API, "DFS_INORDER_SEARCH didn't find the node");
|
||||||
|
}
|
||||||
|
if ( parms.steps != 7 ) {
|
||||||
|
FAIL_BREAK(e, AKERR_API, "DFS_INORDER_SEARCH should've found the node in 7 steps, instead took %d", parms.steps);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Search for the value 17336 using DFS_PREORDER
|
||||||
|
CATCH(e, aksl_tree_iterate(&tree[0], &myiter, NULL, NULL, AKSL_TREE_SEARCH_DFS_POSTORDER, &parms, NULL));
|
||||||
|
if ( parms.node != &tree[6] ) {
|
||||||
|
FAIL_BREAK(e, AKERR_API, "DFS_POSTORDER_SEARCH didn't find the node");
|
||||||
|
}
|
||||||
|
if ( parms.steps != 7 ) {
|
||||||
|
FAIL_BREAK(e, AKERR_API, "DFS_POSTORDER_SEARCH should've found the node in 7 steps, instead took %d", parms.steps);
|
||||||
|
}
|
||||||
|
|
||||||
|
} CLEANUP {
|
||||||
|
} PROCESS(e) {
|
||||||
|
} FINISH_NORETURN(e);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user