AVL tree apocalypse test

This commit is contained in:
secXsQuared 2016-05-23 12:36:41 -07:00
parent 05695f2626
commit 6133063fb0
7 changed files with 134 additions and 14 deletions

View File

@ -38,4 +38,6 @@ avl_tree_entry_t *SAPI avl_tree_smaller(avl_tree_entry_t *entry);
bool SAPI avl_tree_validate(avl_tree_t *tree, int32_t (*compare)(avl_tree_entry_t *, avl_tree_entry_t *));
int32_t SAPI avl_tree_size(avl_tree_t *tree);
#endif

View File

@ -3,22 +3,22 @@
#include "k_type.h"
static inline uint64_t bit_mask_64(uint32_t bit)
static inline uint64_t SAPI bit_mask_64(uint32_t bit)
{
return (uint64_t)1 << bit;
}
static inline uint32_t bit_mask_32(uint32_t bit)
static inline uint32_t SAPI bit_mask_32(uint32_t bit)
{
return (uint32_t)1 << bit;
}
static inline uint64_t bit_field_mask_64(uint32_t low, uint32_t high)
static inline uint64_t SAPI bit_field_mask_64(uint32_t low, uint32_t high)
{
return ~(~(uint64_t)0 << high << 1) << low;
}
static inline uint32_t bit_field_mask_32(uint32_t low, uint32_t high)
static inline uint32_t SAPI bit_field_mask_32(uint32_t low, uint32_t high)
{
return ~(~(uint32_t)0 << high << 1) << low;
}

View File

@ -4,6 +4,12 @@
#include "k_def.h"
#include "k_type.h"
uint32_t SAPI rand( void );
void SAPI srand(uint32_t _seed );
void SAPI mrand(uint32_t max);
uint64_t SAPI str_len(char const *str);
uint64_t SAPI str_cmp(char const *str1, char const *str2);
@ -14,37 +20,37 @@ void SAPI mem_move(void *src, void *dst, uint64_t size);
void SAPI mem_set(void *src, int8_t const val, uint64_t size);
static inline uint64_t align_down(uint64_t val, uint64_t alignment)
static inline uint64_t SAPI align_down(uint64_t val, uint64_t alignment)
{
return (val / alignment) * alignment;
}
static inline uint64_t align_up(uint64_t val, uint64_t alignment)
static inline uint64_t SAPI align_up(uint64_t val, uint64_t alignment)
{
return ((((val) % (alignment)) == 0) ? (((val) / (alignment)) * (alignment)) : ((((val) / (alignment)) * (alignment)) + 1));
}
static inline uint64_t is_overlap(uint64_t x1, uint64_t x2, uint64_t y1, uint64_t y2)
static inline uint64_t SAPI is_overlap(uint64_t x1, uint64_t x2, uint64_t y1, uint64_t y2)
{
return ((x1 <= y2) && (y1 <= x2)) ? 1 : 0;
}
static inline int64_t max_64(int64_t a, int64_t b)
static inline int64_t SAPI max_64(int64_t a, int64_t b)
{
return (a) > (b) ? a : b;
}
static inline int64_t min_64(int64_t a, int64_t b)
static inline int64_t SAPI min_64(int64_t a, int64_t b)
{
return (a) < (b) ? a : b;
}
static inline int32_t max_32(int32_t a, int32_t b)
static inline int32_t SAPI max_32(int32_t a, int32_t b)
{
return (a) > (b) ? a : b;
}
static inline int32_t min_32(int32_t a, int32_t b)
static inline int32_t SAPI min_32(int32_t a, int32_t b)
{
return (a) < (b) ? a : b;
}

View File

@ -0,0 +1,20 @@
#include "std_lib.h"
static uint32_t seed = 1;
static uint32_t max = 16777215;
uint32_t SAPI rand( void )
{
seed = seed * 1103512986 + 29865;
return (unsigned int)(seed / 65536) % (max+1);
}
void SAPI srand( uint32_t _seed )
{
seed = _seed;
}
void SAPI mrand(uint32_t _max)
{
max = _max;
}

View File

@ -6,7 +6,7 @@
#include "linked_list.h"
static inline uint32_t seg_selector(uint32_t index, uint32_t rpl)
static inline uint32_t SAPI seg_selector(uint32_t index, uint32_t rpl)
{
return (index << 3) + rpl;
}

View File

@ -802,7 +802,93 @@ static bool delete_DNE()
return result && avl_tree_validate(&tree, compare);
}
void SAPI avl_tree_test(void)
#define AVL_APOCALYPSE_NUM 500
#define AVL_APOCALYPSE_ITER 2
static int_tree_node apocalypse[AVL_APOCALYPSE_NUM];
static bool test_apocalypse()
{
bool result = true;
avl_tree_t tree;
avl_tree_init(&tree);
// insert test
for(int i = 0; i < AVL_APOCALYPSE_NUM; i++)
{
apocalypse[i].val = rand();
while(avl_tree_search(&tree, &apocalypse[i].tree_entry,compare) != NULL)
{
apocalypse[i].val += rand() % 32765;
}
avl_tree_insert(&tree, &apocalypse[i].tree_entry, compare);
}
// integrity test
result = result && avl_tree_validate(&tree, compare);
result = result && avl_tree_size(&tree) == AVL_APOCALYPSE_NUM;
// smaller and bigger test
avl_tree_entry_t* entry = avl_tree_smallest(&tree);
uint32_t size = 0;
int32_t prev = -1;
int32_t cur = OBTAIN_STRUCT_ADDR(entry, tree_entry,int_tree_node)->val;
while(entry != NULL)
{
if(cur < prev)
{
result = false;
break;
}
size++;
entry = avl_tree_larger(entry);
prev = cur;
if(entry != NULL)
{
cur = OBTAIN_STRUCT_ADDR(entry, tree_entry, int_tree_node)->val;
}
}
result = result && size == AVL_APOCALYPSE_NUM;
// larger test
entry = avl_tree_largest(&tree);
size = 0;
cur = OBTAIN_STRUCT_ADDR(entry, tree_entry,int_tree_node)->val;
prev = cur;
while(entry != NULL)
{
if(cur > prev)
{
result = false;
break;
}
size++;
entry = avl_tree_smaller(entry);
prev = cur;
if(entry != NULL)
{
cur = OBTAIN_STRUCT_ADDR(entry, tree_entry, int_tree_node)->val;
}
}
result = result && size == AVL_APOCALYPSE_NUM;
// delete and search test
for(int i = 0; i < AVL_APOCALYPSE_NUM; i++)
{
result = result && (avl_tree_search(&tree,&apocalypse[i].tree_entry, compare) != NULL);
avl_tree_delete(&tree,&apocalypse[i].tree_entry, compare);
result = result && (avl_tree_search(&tree,&apocalypse[i].tree_entry, compare) == NULL);
result = result && avl_tree_validate(&tree, compare);
}
result = result && (avl_tree_size(&tree) == 0);
return result;
}
void avl_tree_test(void)
{
test_begin("AVL tree test");
@ -839,6 +925,13 @@ void SAPI avl_tree_test(void)
// delete non-existing
run_case("delete_DNE", delete_DNE());
srand(2986);
// ultimate apocalypse
for(int i = 0; i < AVL_APOCALYPSE_ITER; i++)
{
run_case("test_apocalypse", test_apocalypse());
}
test_end();
}

View File

@ -1,5 +1,4 @@
#include "k_test_driver.h"
#include "k_type.h"
#include "linked_list.h"
#include "k_lib_test.h"
#include "std_lib.h"