Test Driver

Got a test driver for module tests. Gotta make sure the basic building blocks work first lol.
Will add salloc test soon.
This commit is contained in:
secXsQuared 2016-05-22 20:19:56 -07:00
parent dddcd487a2
commit 05695f2626
8 changed files with 316 additions and 209 deletions

View File

@ -13,7 +13,7 @@ boot_info_t *SAPI hal_init(multiboot_info_t *m_info)
// set up kernel heap;
hal_alloc_init();
boot_info_t *boot_info = (boot_info_t *) hal_alloc(sizeof(boot_info_t));
boot_info_t *boot_info = (boot_info_t *) halloc(sizeof(boot_info_t));
text_pos = get_pos(0, 0);
// get gdt ready
@ -53,12 +53,12 @@ boot_info_t *SAPI hal_init(multiboot_info_t *m_info)
// memory info
if (m_info->flags & (1 << 6))
{
boot_info->mem_info = (mem_info_t *) hal_alloc(sizeof(mem_info_t));
boot_info->mem_info = (mem_info_t *) halloc(sizeof(mem_info_t));
hal_assert(boot_info->mem_info != NULL, "Unable to allocate memory for mem_info.");
boot_info->mem_info->mem_available = 0;
boot_info->mem_info->mem_installed = 0;
boot_info->mem_info->free_page_list = (linked_list_t *) hal_alloc((sizeof(linked_list_t)));
boot_info->mem_info->occupied_page_list = (linked_list_t *) hal_alloc((sizeof(linked_list_t)));
boot_info->mem_info->free_page_list = (linked_list_t *) halloc((sizeof(linked_list_t)));
boot_info->mem_info->occupied_page_list = (linked_list_t *) halloc((sizeof(linked_list_t)));
hal_assert(boot_info->mem_info->free_page_list != NULL &&
boot_info->mem_info->occupied_page_list != NULL, "Unable to allocate memory for mem_info_lists.");
linked_list_init(boot_info->mem_info->free_page_list);
@ -112,7 +112,7 @@ boot_info_t *SAPI hal_init(multiboot_info_t *m_info)
// }
// }
memory_descriptor_node_t *each_desc = (memory_descriptor_node_t *) hal_alloc(
memory_descriptor_node_t *each_desc = (memory_descriptor_node_t *) halloc(
sizeof(memory_descriptor_node_t));
hal_assert(each_desc != NULL, "Unable to allocate memory for memory_descriptor.");
each_desc->page_count = page_count;
@ -133,22 +133,22 @@ boot_info_t *SAPI hal_init(multiboot_info_t *m_info)
// loaded kernel modules
if (m_info->flags & (1 << 3))
{
boot_info->module_info = (module_info_t *) hal_alloc(sizeof(module_info_t));
boot_info->module_info = (module_info_t *) halloc(sizeof(module_info_t));
hal_assert(boot_info->module_info != NULL, "Unable to allocate memory for module_info.");
boot_info->module_info->module_count = 0;
boot_info->module_info->module_list = (linked_list_t *) hal_alloc(sizeof(linked_list_t));
boot_info->module_info->module_list = (linked_list_t *) halloc(sizeof(linked_list_t));
hal_assert(boot_info->module_info->module_list != NULL, "Unable to allocate memory for module_list.");
linked_list_init(boot_info->module_info->module_list);
multiboot_module_t *mods_list = (multiboot_module_t *) (uint64_t) m_info->mods_addr;
boot_info->module_info->module_count = m_info->mods_count;
for (uint64_t i = 0; i < boot_info->module_info->module_count; i++)
{
module_descriptor_node_t *each_module = (module_descriptor_node_t *) hal_alloc(
module_descriptor_node_t *each_module = (module_descriptor_node_t *) halloc(
sizeof(module_descriptor_node_t));
hal_assert(each_module != NULL, "Unable to allocate memory for module_descriptor.");
each_module->base_addr = (mods_list + i)->mod_start;
each_module->size = (mods_list + i)->mod_end - (mods_list + i)->mod_start;
each_module->name = (char *) hal_alloc((size_t) str_len((char *) (uint64_t)(mods_list + i)->cmdline) + 1);
each_module->name = (char *) halloc((size_t) str_len((char *) (uint64_t) (mods_list + i)->cmdline) + 1);
hal_assert(each_module->name != NULL, "Unable to allocate memory for module name string.");
mem_cpy((void *) (uint64_t) (mods_list + i)->cmdline, each_module->name,
str_len((char *) (uint64_t) (mods_list + i)->cmdline) + 1);

View File

@ -72,7 +72,7 @@ void SAPI hal_write_pml4_entry(void *const base, uint64_t const pdpt_addr, uint6
}
void SAPI hal_write_segment_descriptor(void *const gdt, uint32_t const base, uint32_t const limit,
uint64_t const attr)
uint64_t const attr)
{
if (gdt == NULL)
return;
@ -90,12 +90,12 @@ void SAPI hal_write_segment_descriptor(void *const gdt, uint32_t const base, uin
return;
}
void* SAPI hal_alloc(uint32_t size)
void *SAPI halloc(uint32_t size)
{
return salloc(kernel_heap,size);
return salloc(kernel_heap, size);
}
void SAPI hal_free(void *ptr)
void SAPI hfree(void *ptr)
{
sfree(kernel_heap, ptr);
return;

View File

@ -93,9 +93,9 @@ typedef struct __attribute__((packed))
uint64_t edx;
} cpuid_t;
void* SAPI hal_alloc(uint32_t size);
void* SAPI halloc(uint32_t size);
void SAPI hal_free(void *ptr);
void SAPI hfree(void *ptr);
void SAPI hal_alloc_init();

View File

@ -1,8 +1,8 @@
#ifndef _LIB_TEST_H_
#define _LIB_TEST_H_
void linked_list_test(void);
void SAPI linked_list_test(void);
void avl_tree_test(void);
void SAPI avl_tree_test(void);
#endif

View File

@ -0,0 +1,15 @@
#ifndef _K_TEST_DRIVER_H_
#define _K_TEST_DRIVER_H_
#include "k_type.h"
#include "k_def.h"
void SAPI test_begin(char *name);
void SAPI test_end();
void *SAPI talloc(uint32_t size);
void SAPI run_case(char *name, bool result);
#endif

View File

@ -1,6 +1,5 @@
#include "hal_print.h"
#include "k_test_driver.h"
#include "avl_tree.h"
#include "hal_mem.h"
typedef struct
{
@ -8,38 +7,10 @@ typedef struct
int val;
} int_tree_node;
#define GLOBAL_ALLOC_TABLE_SIZE 150
static void* g_gat[GLOBAL_ALLOC_TABLE_SIZE];
static void push_gat(void* wow)
{
int i = 0;
for(i = 0; i < GLOBAL_ALLOC_TABLE_SIZE; i++)
{
if(g_gat[i] == NULL)
{
g_gat[i] = wow;
break;
}
}
}
static void clear_gat()
{
int i = 0;
for(i = 0; i < GLOBAL_ALLOC_TABLE_SIZE; i++)
{
if(g_gat[i] != NULL)
hal_free(g_gat[i]);
}
}
static int_tree_node *create_tree_node(int val)
{
int_tree_node *rs = hal_alloc(sizeof(int_tree_node));
int_tree_node *rs = talloc(sizeof(int_tree_node));
rs->val = val;
push_gat(rs);
return rs;
}
@ -524,12 +495,12 @@ static bool delete_complex_2()
avl_tree_insert(&tree, &create_tree_node(30)->tree_entry, compare);
avl_tree_insert(&tree, &create_tree_node(25)->tree_entry, compare);
avl_tree_insert(&tree, &create_tree_node(35)->tree_entry, compare);
int val1[] = {20,10,30,25,35};
int val1[] = {20, 10, 30, 25, 35};
result = result && pre_order_assert(&tree, val1, 5);
avl_tree_delete(&tree, &deleted->tree_entry, compare);
int val2[] = {25,10,30,35};
int val2[] = {25, 10, 30, 35};
result = result && pre_order_assert(&tree, val2, 4);
return result && avl_tree_validate(&tree, compare);
}
@ -564,12 +535,12 @@ static bool delete_complex_3()
avl_tree_insert(&tree, &create_tree_node(5)->tree_entry, compare);
avl_tree_insert(&tree, &create_tree_node(15)->tree_entry, compare);
avl_tree_insert(&tree, &create_tree_node(25)->tree_entry, compare);
int val1[] = {20,10,5,15,30,25};
int val1[] = {20, 10, 5, 15, 30, 25};
result = result && pre_order_assert(&tree, val1, 6);
avl_tree_delete(&tree, &deleted->tree_entry, compare);
int val2[] = {20,15,5,30,25};
int val2[] = {20, 15, 5, 30, 25};
result = result && pre_order_assert(&tree, val2, 5);
return result && avl_tree_validate(&tree, compare);
}
@ -609,7 +580,7 @@ static bool delete_complex_4()
avl_tree_insert(&tree, &delete15->tree_entry, compare);
avl_tree_insert(&tree, &delete25->tree_entry, compare);
int val1[] = {20,10,5,15,30,25};
int val1[] = {20, 10, 5, 15, 30, 25};
result = result && pre_order_assert(&tree, val1, 6);
avl_tree_delete(&tree, &delete5->tree_entry, compare);
@ -668,12 +639,12 @@ static bool delete_complex_single_rotation()
avl_tree_insert(&tree, &create_tree_node(35)->tree_entry, compare);
avl_tree_insert(&tree, &deleted->tree_entry, compare);
avl_tree_insert(&tree, &create_tree_node(31)->tree_entry, compare);
int val1[] = {20,10,5,15,12,30,25,22,40,35,31,50};
int val1[] = {20, 10, 5, 15, 12, 30, 25, 22, 40, 35, 31, 50};
result = result && pre_order_assert(&tree, val1, 12);
avl_tree_delete(&tree, &deleted->tree_entry, compare);
int val2[] = {20,10,5,15,12,30,25,22,35,31,40};
int val2[] = {20, 10, 5, 15, 12, 30, 25, 22, 35, 31, 40};
result = result && pre_order_assert(&tree, val2, 11);
return result && avl_tree_validate(&tree, compare);
}
@ -723,12 +694,12 @@ static bool delete_complex_double_rotation()
avl_tree_insert(&tree, &create_tree_node(35)->tree_entry, compare);
avl_tree_insert(&tree, &create_tree_node(50)->tree_entry, compare);
avl_tree_insert(&tree, &create_tree_node(31)->tree_entry, compare);
int val1[] = {20,10,5,15,12,30,25,22,40,35,31,50};
int val1[] = {20, 10, 5, 15, 12, 30, 25, 22, 40, 35, 31, 50};
result = result && pre_order_assert(&tree, val1, 12);
avl_tree_delete(&tree, &deleted->tree_entry, compare);
int val2[] = {20,10,5,15,12,35,30,25,31,40,50};
int val2[] = {20, 10, 5, 15, 12, 35, 30, 25, 31, 40, 50};
result = result && pre_order_assert(&tree, val2, 11);
return result && avl_tree_validate(&tree, compare);
}
@ -778,12 +749,12 @@ static bool delete_complex_multiple_rotation()
avl_tree_insert(&tree, &create_tree_node(35)->tree_entry, compare);
avl_tree_insert(&tree, &create_tree_node(50)->tree_entry, compare);
avl_tree_insert(&tree, &create_tree_node(31)->tree_entry, compare);
int val1[] = {20,10,5,15,12,30,25,22,40,35,31,50};
int val1[] = {20, 10, 5, 15, 12, 30, 25, 22, 40, 35, 31, 50};
result = result && pre_order_assert(&tree, val1, 12);
avl_tree_delete(&tree, &deleted->tree_entry, compare);
int val2[] = {30,20,12,10,15,25,22,40,35,31,50};
int val2[] = {30, 20, 12, 10, 15, 25, 22, 40, 35, 31, 50};
result = result && pre_order_assert(&tree, val2, 11);
return result && avl_tree_validate(&tree, compare);
}
@ -822,7 +793,7 @@ static bool delete_DNE()
avl_tree_insert(&tree, &create_tree_node(15)->tree_entry, compare);
avl_tree_insert(&tree, &create_tree_node(25)->tree_entry, compare);
int val1[] = {20,10,5,15,30,25};
int val1[] = {20, 10, 5, 15, 30, 25};
result = result && pre_order_assert(&tree, val1, 6);
avl_tree_delete(&tree, &delete24->tree_entry, compare);
@ -833,44 +804,41 @@ static bool delete_DNE()
void SAPI avl_tree_test(void)
{
int i = 0;
for (i = 0; i < GLOBAL_ALLOC_TABLE_SIZE; i++)
{
g_gat[i] = NULL;
}
test_begin("AVL tree test");
// simple tests
hal_printf("insert_simple_l %s\n", insert_simple_l() ? "PASS" : "FAIL");
hal_printf("insert_simple_r %s\n", insert_simple_r() ? "PASS" : "FAIL");
hal_printf("insert_simple_ll %s\n", insert_simple_ll() ? "PASS" : "FAIL");
hal_printf("insert_simple_rr %s\n", insert_simple_rr() ? "PASS" : "FAIL");
run_case("insert_simple_l", insert_simple_l());
run_case("insert_simple_r", insert_simple_r());
run_case("insert_simple_ll", insert_simple_ll());
run_case("insert_simple_rr", insert_simple_rr());
// complex ones
hal_printf("insert_complex_1 %s\n", insert_complex_1() ? "PASS" : "FAIL");
hal_printf("insert_complex_2 %s\n", insert_complex_2() ? "PASS" : "FAIL");
hal_printf("insert_complex_3 %s\n", insert_complex_3() ? "PASS" : "FAIL");
hal_printf("insert_complex_4 %s\n", insert_complex_4() ? "PASS" : "FAIL");
run_case("insert_complex_1", insert_complex_1());
run_case("insert_complex_2", insert_complex_2());
run_case("insert_complex_3", insert_complex_3());
run_case("insert_complex_4", insert_complex_4());
// insert duplicate
hal_printf("insert_duplicate %s\n", insert_duplicate() ? "PASS" : "FAIL");
run_case("insert_duplicate", insert_duplicate());
// simple tests
hal_printf("delete_simple_l %s\n", delete_simple_l() ? "PASS" : "FAIL");
hal_printf("delete_simple_r %s\n", delete_simple_r() ? "PASS" : "FAIL");
hal_printf("delete_simple_ll %s\n", delete_simple_ll() ? "PASS" : "FAIL");
hal_printf("delete_simple_rr %s\n", delete_simple_rr() ? "PASS" : "FAIL");
run_case("delete_simple_l", delete_simple_l());
run_case("delete_simple_r", delete_simple_r());
run_case("delete_simple_ll", delete_simple_ll());
run_case("delete_simple_rr", delete_simple_rr());
// complex tests
hal_printf("delete_complex_1 %s\n", delete_complex_1() ? "PASS" : "FAIL");
hal_printf("delete_complex_2 %s\n", delete_complex_2() ? "PASS" : "FAIL");
hal_printf("delete_complex_3 %s\n", delete_complex_3() ? "PASS" : "FAIL");
hal_printf("delete_complex_4 %s\n", delete_complex_4() ? "PASS" : "FAIL");
hal_printf("delete_complex_single_rotation %s\n", delete_complex_single_rotation() ? "PASS" : "FAIL");
hal_printf("delete_complex_double_rotation %s\n", delete_complex_double_rotation() ? "PASS" : "FAIL");
hal_printf("delete_complex_multiple_rotation %s\n", delete_complex_multiple_rotation() ? "PASS" : "FAIL");
run_case("delete_complex_1", delete_complex_1());
run_case("delete_complex_2", delete_complex_2());
run_case("delete_complex_3", delete_complex_3());
run_case("delete_complex_4", delete_complex_4());
run_case("delete_complex_single_rotation", delete_complex_single_rotation());
run_case("delete_complex_double_rotation", delete_complex_double_rotation());
run_case("delete_complex_multiple_rotation", delete_complex_multiple_rotation());
// delete non-existing
hal_printf("delete_DNE %s\n", delete_DNE() ? "PASS" : "FAIL");
run_case("delete_DNE", delete_DNE());
clear_gat();
test_end();
}

View File

@ -1,10 +1,8 @@
#include "k_test_driver.h"
#include "k_type.h"
#include "linked_list.h"
#include "k_lib_test.h"
#include "hal_print.h"
#include "std_lib.h"
#include "hal_mem.h"
typedef struct
{
@ -12,37 +10,11 @@ typedef struct
int val;
} my_list_node;
#define GLOBAL_ALLOC_TABLE_SIZE 128
static my_list_node* g_gat[GLOBAL_ALLOC_TABLE_SIZE];
static void push_gat(my_list_node* wow)
{
int i = 0;
for(i = 0; i < GLOBAL_ALLOC_TABLE_SIZE; i++)
{
if(g_gat[i] == NULL)
{
g_gat[i] = wow;
break;
}
}
}
static void clear_gat()
{
int i = 0;
for(i = 0; i < GLOBAL_ALLOC_TABLE_SIZE; i++)
{
if(g_gat[i] != NULL)
hal_free(g_gat[i]);
}
}
static bool validate_list(linked_list_t* list)
static bool validate_list(linked_list_t *list)
{
bool result = true;
// list_head_test
if(list->head != NULL)
if (list->head != NULL)
{
result = result && (list->head->prev == NULL);
}
@ -51,7 +23,7 @@ static bool validate_list(linked_list_t* list)
result = result && (list->tail == NULL);
}
if(list->tail != NULL)
if (list->tail != NULL)
{
result = result && (list->tail->next == NULL);
}
@ -64,18 +36,18 @@ static bool validate_list(linked_list_t* list)
}
static bool assert_list(linked_list_t* list, int val[], int size)
static bool assert_list(linked_list_t *list, int val[], int size)
{
linked_list_entry_t * node = linked_list_first(list);
linked_list_entry_t *node = linked_list_first(list);
int i = 0;
if(!validate_list(list))
if (!validate_list(list))
return false;
while(node != NULL && i < size)
while (node != NULL && i < size)
{
my_list_node* enode = OBTAIN_STRUCT_ADDR(node, lnode, my_list_node);
if(enode->val != val[i])
my_list_node *enode = OBTAIN_STRUCT_ADDR(node, lnode, my_list_node);
if (enode->val != val[i])
{
return false;
}
@ -83,16 +55,16 @@ static bool assert_list(linked_list_t* list, int val[], int size)
node = linked_list_next(node);
}
if(i != size)
if (i != size)
{
return false;
}
node = linked_list_last(list);
while(node != NULL && i >= 0)
while (node != NULL && i >= 0)
{
my_list_node* enode = OBTAIN_STRUCT_ADDR(node, lnode, my_list_node);
if(enode->val != val[i-1])
my_list_node *enode = OBTAIN_STRUCT_ADDR(node, lnode, my_list_node);
if (enode->val != val[i - 1])
{
return false;
}
@ -125,32 +97,29 @@ static bool assert_list(linked_list_t* list, int val[], int size)
// return;
//}
static void insert_val(linked_list_t* list, int index, int val)
static void insert_val(linked_list_t *list, int index, int val)
{
my_list_node *a = (my_list_node*)hal_alloc(sizeof(my_list_node));
my_list_node *a = (my_list_node *) talloc(sizeof(my_list_node));
a->val = val;
linked_list_insert_idx(list, index, &a->lnode);
push_gat(a);
}
static void push_back_val(linked_list_t* list, int val)
static void push_back_val(linked_list_t *list, int val)
{
my_list_node *a = (my_list_node*)hal_alloc(sizeof(my_list_node));
my_list_node *a = (my_list_node *) talloc(sizeof(my_list_node));
a->val = val;
linked_list_push_back(list, &a->lnode);
push_gat(a);
}
static void push_front_val(linked_list_t* list, int val)
static void push_front_val(linked_list_t *list, int val)
{
my_list_node *a = (my_list_node*)hal_alloc(sizeof(my_list_node));
my_list_node *a = (my_list_node *) talloc(sizeof(my_list_node));
a->val = val;
linked_list_push_front(list, &a->lnode);
push_gat(a);
}
static void insert_test_beginning()
static bool insert_test_beginning()
{
linked_list_t list;
linked_list_init(&list);
@ -160,11 +129,11 @@ static void insert_test_beginning()
insert_val(&list, 0, 3);
// 3210==0123
int val[4] = {3,2,1,0};
hal_printf("insert_test_beginning %s\n",assert_list(&list, val, 4) ? "PASS" : "FAIL");
int val[4] = {3, 2, 1, 0};
return assert_list(&list, val, 4);
}
static void insert_test_middle()
static bool insert_test_middle()
{
linked_list_t list;
linked_list_init(&list);
@ -177,11 +146,11 @@ static void insert_test_middle()
insert_val(&list, 1, 5);
insert_val(&list, 2, 6);
int val[] = {2,5,6,4,1,0};
hal_printf("insert_test_middle %s\n",assert_list(&list, val, 6) ? "PASS" : "FAIL");
int val[] = {2, 5, 6, 4, 1, 0};
return assert_list(&list, val, 6);
}
static void insert_test_end()
static bool insert_test_end()
{
linked_list_t list;
linked_list_init(&list);
@ -191,11 +160,11 @@ static void insert_test_end()
insert_val(&list, 2, 2);
insert_val(&list, 3, 3);
int val[] = {0,1,2,3};
hal_printf("insert_test_end %s\n",assert_list(&list, val, 4) ? "PASS" : "FAIL");
int val[] = {0, 1, 2, 3};
return assert_list(&list, val, 4);
}
static void insert_test_invalid()
static bool insert_test_invalid()
{
linked_list_t list;
linked_list_init(&list);
@ -220,12 +189,12 @@ static void insert_test_invalid()
linked_list_insert_ref(NULL, list.head, list.tail);
linked_list_insert_ref(&list, list.head, NULL);
int val[] = {0,1,2,3};
hal_printf("insert_test_invalid %s\n",assert_list(&list, val, 4) ? "PASS" : "FAIL");
int val[] = {0, 1, 2, 3};
return assert_list(&list, val, 4);
}
static void remove_test_beginning()
static bool remove_test_beginning()
{
linked_list_t list;
linked_list_init(&list);
@ -238,11 +207,11 @@ static void remove_test_beginning()
linked_list_remove_idx(&list, 0);
// 10==01
int val[] = {1,0};
hal_printf("remove_test_beginning %s\n",assert_list(&list, val, 2) ? "PASS" : "FAIL");
int val[] = {1, 0};
return assert_list(&list, val, 2);
}
static void remove_test_middle()
static bool remove_test_middle()
{
linked_list_t list;
linked_list_init(&list);
@ -259,11 +228,11 @@ static void remove_test_middle()
linked_list_remove_idx(&list, 2);
// 5310=====0135
int val[] = {5,3,1,0};
hal_printf("remove_test_middle %s\n",assert_list(&list, val, 4) ? "PASS" : "FAIL");
int val[] = {5, 3, 1, 0};
return assert_list(&list, val, 4);
}
static void remove_test_end()
static bool remove_test_end()
{
linked_list_t list;
linked_list_init(&list);
@ -276,11 +245,11 @@ static void remove_test_end()
linked_list_remove_idx(&list, 3);
linked_list_remove_idx(&list, 2);
int val[] = {0,1};
hal_printf("remove_test_all %s\n",assert_list(&list, val, 2) ? "PASS" : "FAIL");
int val[] = {0, 1};
return assert_list(&list, val, 2);
}
static void remove_test_all()
static bool remove_test_all()
{
bool result = true;
linked_list_t list;
@ -322,10 +291,10 @@ static void remove_test_all()
result = result && assert_list(&list, NULL, 0);
hal_printf("remove_test_end %s\n",result ? "PASS" : "FAIL");
return result;
}
static void remove_test_invalid()
static bool remove_test_invalid()
{
linked_list_t list;
linked_list_init(&list);
@ -351,11 +320,11 @@ static void remove_test_invalid()
linked_list_remove_ref(&list, NULL);
// 0123=====3210
int val[] = {0,1,2,3};
hal_printf("remove_test_invalid %s\n",assert_list(&list, val, 4) ? "PASS" : "FAIL");
int val[] = {0, 1, 2, 3};
return assert_list(&list, val, 4);
}
static void size_test()
static bool size_test()
{
bool result = true;
linked_list_t list;
@ -369,12 +338,12 @@ static void size_test()
insert_val(&list, 3, 3);
result = result && (linked_list_size(&list) == 4 && linked_list_size(&list2) == 0 && linked_list_size(NULL) == -1);
int val[] = {0,1,2,3};
int val[] = {0, 1, 2, 3};
result = result && assert_list(&list, val, 4);
hal_printf("size_test %s\n", result ? "PASS" : "FAIL");
return result;
}
static void push_pop_front_test()
static bool push_pop_front_test()
{
bool result = true;
linked_list_t list;
@ -386,12 +355,12 @@ static void push_pop_front_test()
push_front_val(&list, 4);
//4321==1234
int val1[] = {4,3,2,1};
int val1[] = {4, 3, 2, 1};
result = result && assert_list(&list, val1, 4);
linked_list_pop_front(&list);
//321==123
int val2[] = {3,2,1};
int val2[] = {3, 2, 1};
result = result && assert_list(&list, val2, 3);
linked_list_pop_front(&list);
@ -399,10 +368,10 @@ static void push_pop_front_test()
linked_list_pop_front(&list);
result = result && assert_list(&list, NULL, 0);
hal_printf("push_pop_front_test %s\n", result ? "PASS" : "FAIL");
return result;
}
static void push_pop_back_test()
static bool push_pop_back_test()
{
bool result = true;
linked_list_t list;
@ -414,12 +383,12 @@ static void push_pop_back_test()
push_back_val(&list, 4);
//1234==4321
int val1[] = {1,2,3,4};
int val1[] = {1, 2, 3, 4};
result = result && assert_list(&list, val1, 4);
linked_list_pop_back(&list);
//123==321
int val2[] = {1,2,3};
int val2[] = {1, 2, 3};
result = result && assert_list(&list, val2, 3);
linked_list_pop_back(&list);
@ -427,15 +396,15 @@ static void push_pop_back_test()
linked_list_pop_back(&list);
result = result && assert_list(&list, NULL, 0);
hal_printf("push_pop_back_test %s\n", result ? "PASS" : "FAIL");
return result;
}
static bool equals(linked_list_entry_t* a, linked_list_entry_t* b)
static bool equals(linked_list_entry_t *a, linked_list_entry_t *b)
{
return (int64_t)a == OBTAIN_STRUCT_ADDR(b, lnode, my_list_node)->val;
return (int64_t) a == OBTAIN_STRUCT_ADDR(b, lnode, my_list_node)->val;
}
static void search_test()
static bool search_test()
{
bool result = true;
linked_list_t list;
@ -446,54 +415,48 @@ static void search_test()
push_back_val(&list, 3);
push_back_val(&list, 4);
int val1[] = {1,2,3,4};
int val1[] = {1, 2, 3, 4};
result = result && assert_list(&list, val1, 4);
result = result && (linked_list_search(&list, (linked_list_entry_t *) 4, equals) == 3);
result = result && (linked_list_search(&list, (linked_list_entry_t *)3 ,equals) == 2);
result = result && (linked_list_search(&list, (linked_list_entry_t *)2 ,equals) == 1);
result = result && (linked_list_search(&list, (linked_list_entry_t *)1 ,equals) == 0);
result = result && (linked_list_search(&list, (linked_list_entry_t *) 3, equals) == 2);
result = result && (linked_list_search(&list, (linked_list_entry_t *) 2, equals) == 1);
result = result && (linked_list_search(&list, (linked_list_entry_t *) 1, equals) == 0);
result = result && (linked_list_search(&list, NULL ,equals) == -1);
result = result && (linked_list_search(NULL, (linked_list_entry_t *)1 ,equals) == -1);
result = result && (linked_list_search(&list, NULL, equals) == -1);
result = result && (linked_list_search(NULL, (linked_list_entry_t *) 1, equals) == -1);
linked_list_entry_t* node = linked_list_get(&list, 1);
result = result && (linked_list_search(&list, node , NULL) == 1);
linked_list_entry_t *node = linked_list_get(&list, 1);
result = result && (linked_list_search(&list, node, NULL) == 1);
result = result && assert_list(&list, val1, 4);
hal_printf("search_test %s\n", result ? "PASS" : "FAIL");
return result;
}
void linked_list_test(void)
void SAPI linked_list_test(void)
{
int i = 0;
for(i = 0; i < GLOBAL_ALLOC_TABLE_SIZE; i++)
{
g_gat[i] = NULL;
}
test_begin("Linked list test");
run_case("insert_test_beginning", insert_test_beginning());
run_case("insert_test_middle", insert_test_middle());
run_case("insert_test_end", insert_test_end());
run_case("insert_test_invalid", insert_test_invalid());
insert_test_beginning();
insert_test_middle();
insert_test_end();
insert_test_invalid();
run_case("remove_test_beginning", remove_test_beginning());
run_case("remove_test_middle", remove_test_middle());
run_case("remove_test_end", remove_test_end());
run_case("remove_test_invalid", remove_test_invalid());
remove_test_beginning();
remove_test_middle();
remove_test_end();
remove_test_invalid();
run_case("size_test", size_test());
size_test();
run_case("remove_test_all", remove_test_all());
remove_test_all();
run_case("push_pop_front_test", push_pop_front_test());
run_case("push_pop_back_test", push_pop_back_test());
push_pop_front_test();
push_pop_back_test();
search_test();
clear_gat();
run_case("search_test", search_test());
test_end();
}

View File

@ -0,0 +1,161 @@
#include "hal_print.h"
#include "k_test_driver.h"
#include "k_type.h"
#include "hal_mem.h"
#define GAT_SIZE 256
#define CASE_NUM 32
typedef struct
{
char *case_name;
bool success;
bool used;
} case_info;
static case_info ginfo[CASE_NUM];
static void *gat[GAT_SIZE];
static char *test_name;
static void test_info()
{
hal_printf("[TD-INFO][%s] - ", test_name);
}
static void test_warning()
{
hal_printf("[TD-WARN][%s] - ", test_name);
}
static void test_error()
{
hal_printf("[TD-ERR][%s] - ", test_name);
}
static void gat_push(void *ptr)
{
for (int i = 0; i < GAT_SIZE; i++)
{
if (gat[i] == NULL)
{
gat[i] = ptr;
return;
}
}
}
static bool gat_full()
{
for (int i = 0; i < GAT_SIZE; i++)
{
if (gat[i] == NULL)
{
return false;
}
}
return true;
}
static void gat_free()
{
for (int i = 0; i < GAT_SIZE; i++)
{
if (gat[i] != NULL)
{
hfree(gat[i]);
gat[i] = NULL;
}
}
}
static void ginfo_push(char *case_name, bool success)
{
char *r_case_name = (case_name == NULL ? "Anonymous Case" : case_name);
for (int i = 0; i < CASE_NUM; i++)
{
if (!ginfo[i].used)
{
ginfo[i].case_name = r_case_name;
ginfo[i].success = success;
ginfo[i].used = true;
return;
}
}
test_warning();
hal_printf("GINFO full, [%s] result not recorded.\n", r_case_name);
}
void SAPI test_begin(char *name)
{
test_name = (name == NULL ? "Anonymous Test" : name);
for (int i = 0; i < GAT_SIZE; i++)
{
gat[i] = NULL;
}
for (int i = 0; i < CASE_NUM; i++)
{
ginfo[i].used = false;
}
}
void SAPI test_end()
{
gat_free();
int32_t total = 0, failed = 0, success = 0;
for (int i = 0; i < CASE_NUM; i++)
{
if (ginfo[i].used)
{
total++;
if (ginfo[i].success)
{
success++;
}
else
{
failed++;
}
}
}
test_info();
hal_printf("%s\n", failed > 0 ? "FAIL" : "PASS");
hal_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)
{
hal_printf(" %s FAILED\n", ginfo[i].case_name);
}
}
}
for (int i = 0; i < CASE_NUM; i++)
{
ginfo[i].used = false;
}
}
void SAPI *talloc(uint32_t size)
{
if (!gat_full())
{
void *result = halloc(size);
gat_push(result);
return result;
}
else
{
test_error();
hal_printf("GAT full, rejecting further allocations.\n");
}
return NULL;
}
void SAPI run_case(char *name, bool result)
{
ginfo_push(name, result);
}