#include "test_main.h" #include "lb/salloc.h" #include "test_case.h" typedef union { uint32 size; uint32 flags; } salloc_header; static const uint32 salloc_header_size = sizeof(salloc_header); static char buffer[1024]; static bool salloc_init_test(void) { lb_salloc_init(buffer, 1024); uint32 blk_size[] = {1024}; bool blk_free[] = {TRUE}; return lb_salloc_assert(buffer, blk_size, blk_free, 1); } static bool salloc_basic_alloc(void) { bool result = TRUE; lb_salloc_init(buffer, 1024); result = result && (lb_salloc(buffer, 10) != NULL); uint32 blk_size[] = {10 + salloc_header_size, 1024 - 10 - salloc_header_size}; bool blk_free[] = {FALSE, TRUE}; result = result && lb_salloc_assert(buffer, blk_size, blk_free, 2); return result; } static bool salloc_full_alloc(void) { bool result = TRUE; lb_salloc_init(buffer, 1024); result = result && (lb_salloc(buffer, 1024 - salloc_header_size) != NULL); uint32 blk_size[] = {1024}; bool blk_free[] = {FALSE}; result = result && lb_salloc_assert(buffer, blk_size, blk_free, 1); return result; } static bool salloc_overflow_alloc(void) { bool result = TRUE; lb_salloc_init(buffer, 1024); result = result && (lb_salloc(buffer, 1024 - salloc_header_size + 1) == NULL); uint32 blk_size[] = {1024}; bool blk_free[] = {TRUE}; result = result && lb_salloc_assert(buffer, blk_size, blk_free, 1); return result; } static bool salloc_multiple_alloc(void) { bool result = TRUE; lb_salloc_init(buffer, 1024); result = result && (lb_salloc(buffer, 10) != NULL); result = result && (lb_salloc(buffer, 10) != NULL); result = result && (lb_salloc(buffer, 10) != NULL); uint32 blk_size[] = {10 + salloc_header_size, 10 + salloc_header_size, 10 + salloc_header_size, 1024 - 3 * (10 + salloc_header_size)}; bool blk_free[] = {FALSE, FALSE, FALSE, TRUE}; result = result && lb_salloc_assert(buffer, blk_size, blk_free, 4); return result; } static bool salloc_alloc_not_enough(void) { void *ptr; bool result = TRUE; lb_salloc_init(buffer, salloc_header_size + salloc_header_size + salloc_header_size - 1); ptr = lb_salloc(buffer, salloc_header_size); result = result && (ptr != NULL); uint32 blk_size[] = {salloc_header_size + salloc_header_size + salloc_header_size - 1}; bool blk_free[] = {FALSE}; result = result && lb_salloc_assert(buffer, blk_size, blk_free, 1); return result; } static bool salloc_basic_free(void) { void *ptr; bool result = TRUE; lb_salloc_init(buffer, 1024); ptr = lb_salloc(buffer, 10); result = result && (ptr != NULL); lb_sfree(buffer, ptr); uint32 blk_size[] = {1024}; bool blk_free[] = {TRUE}; result = result && lb_salloc_assert(buffer, blk_size, blk_free, 1); return result; } static bool salloc_full_free(void) { void *ptr; bool result = TRUE; lb_salloc_init(buffer, 1024); ptr = lb_salloc(buffer, 1024 - salloc_header_size); result = result && (ptr != NULL); lb_sfree(buffer, ptr); uint32 blk_size[] = {1024}; bool blk_free[] = {TRUE}; result = result && lb_salloc_assert(buffer, blk_size, blk_free, 1); return result; } static bool salloc_multiple_free(void) { void *ptr1, *ptr2, *ptr3, *ptr4; bool result = TRUE; lb_salloc_init(buffer, 1024); ptr1 = lb_salloc(buffer, 10); ptr2 = lb_salloc(buffer, 10); ptr3 = lb_salloc(buffer, 10); ptr4 = lb_salloc(buffer, 10); result = result && (ptr1 != NULL) && (ptr2 != NULL) && (ptr3 != NULL) && (ptr4 != NULL); lb_sfree(buffer, ptr1); lb_sfree(buffer, ptr3); uint32 blk_size[] = {10 + salloc_header_size, 10 + salloc_header_size, 10 + salloc_header_size, 10 + salloc_header_size, 1024 - 4 * (10 + salloc_header_size)}; bool blk_free[] = {TRUE, FALSE, TRUE, FALSE, TRUE}; result = result && lb_salloc_assert(buffer, blk_size, blk_free, 5); return result; } static bool salloc_free_join_tail(void) { void *ptr1, *ptr2, *ptr3, *ptr4; bool result = TRUE; lb_salloc_init(buffer, 1024); ptr1 = lb_salloc(buffer, 10); ptr2 = lb_salloc(buffer, 10); ptr3 = lb_salloc(buffer, 10); ptr4 = lb_salloc(buffer, 10); result = result && (ptr1 != NULL) && (ptr2 != NULL) && (ptr3 != NULL) && (ptr4 != NULL); lb_sfree(buffer, ptr4); uint32 blk_size[] = {10 + salloc_header_size, 10 + salloc_header_size, 10 + salloc_header_size, 1024 - 3 * (10 + salloc_header_size)}; bool blk_free[] = {FALSE, FALSE, FALSE, TRUE}; result = result && lb_salloc_assert(buffer, blk_size, blk_free, 4); return result; } static bool salloc_free_join_head(void) { void *ptr1, *ptr2, *ptr3, *ptr4; bool result = TRUE; lb_salloc_init(buffer, 1024); ptr1 = lb_salloc(buffer, 10); ptr2 = lb_salloc(buffer, 10); ptr3 = lb_salloc(buffer, 10); ptr4 = lb_salloc(buffer, 10); result = result && (ptr1 != NULL) && (ptr2 != NULL) && (ptr3 != NULL) && (ptr4 != NULL); lb_sfree(buffer, ptr1); lb_sfree(buffer, ptr2); uint32 blk_size[] = {2 * (10 + salloc_header_size), 10 + salloc_header_size, 10 + salloc_header_size, 1024 - 4 * (10 + salloc_header_size)}; bool blk_free[] = {TRUE, FALSE, FALSE, TRUE}; result = result && lb_salloc_assert(buffer, blk_size, blk_free, 4); return result; } static bool salloc_free_join_mid(void) { void *ptr1, *ptr2, *ptr3, *ptr4; bool result = TRUE; lb_salloc_init(buffer, 1024); ptr1 = lb_salloc(buffer, 10); ptr2 = lb_salloc(buffer, 10); ptr3 = lb_salloc(buffer, 10); ptr4 = lb_salloc(buffer, 10); result = result && (ptr1 != NULL) && (ptr2 != NULL) && (ptr3 != NULL) && (ptr4 != NULL); lb_sfree(buffer, ptr2); lb_sfree(buffer, ptr3); uint32 blk_size[] = {10 + salloc_header_size, 2 * (10 + salloc_header_size), 10 + salloc_header_size, 1024 - 4 * (10 + salloc_header_size)}; bool blk_free[] = {FALSE, TRUE, FALSE, TRUE}; result = result && lb_salloc_assert(buffer, blk_size, blk_free, 4); return result; } static bool salloc_free_join_consecutive(void) { void *ptr1, *ptr2, *ptr3, *ptr4, *ptr5; bool result = TRUE; lb_salloc_init(buffer, 1024); ptr1 = lb_salloc(buffer, 10); ptr2 = lb_salloc(buffer, 10); ptr3 = lb_salloc(buffer, 10); ptr4 = lb_salloc(buffer, 10); ptr5 = lb_salloc(buffer, 10); result = result && (ptr1 != NULL) && (ptr2 != NULL) && (ptr3 != NULL) && (ptr4 != NULL) && (ptr5 != NULL); lb_sfree(buffer, ptr2); lb_sfree(buffer, ptr4); uint32 blk_size[] = {10 + salloc_header_size, 10 + salloc_header_size, 10 + salloc_header_size, 10 + salloc_header_size, 10 + salloc_header_size, 1024 - 5 * (10 + salloc_header_size)}; bool blk_free[] = {FALSE, TRUE, FALSE, TRUE, FALSE, TRUE}; result = result && lb_salloc_assert(buffer, blk_size, blk_free, 6); lb_sfree(buffer, ptr3); uint32 blk_size2[] = {10 + salloc_header_size, 3 * (10 + salloc_header_size), 10 + salloc_header_size, 1024 - 5 * (10 + salloc_header_size)}; bool blk_free2[] = {FALSE, TRUE, FALSE, TRUE}; result = result && lb_salloc_assert(buffer, blk_size2, blk_free2, 4); return result; } static bool salloc_free_all(void) { void *ptr1, *ptr2, *ptr3, *ptr4; bool result = TRUE; lb_salloc_init(buffer, 1024); ptr1 = lb_salloc(buffer, 10); ptr2 = lb_salloc(buffer, 10); ptr3 = lb_salloc(buffer, 10); ptr4 = lb_salloc(buffer, 10); result = result && (ptr1 != NULL) && (ptr2 != NULL) && (ptr3 != NULL) && (ptr4 != NULL); lb_sfree(buffer, ptr1); lb_sfree(buffer, ptr2); lb_sfree(buffer, ptr3); lb_sfree(buffer, ptr4); uint32 blk_size[] = {1024}; bool blk_free[] = {TRUE}; result = result && lb_salloc_assert(buffer, blk_size, blk_free, 1); return result; } void salloc_test(void) { test_begin("salloc test"); run_case("salloc_init_test", salloc_init_test()); run_case("salloc_basic_alloc", salloc_basic_alloc()); run_case("salloc_full_alloc", salloc_full_alloc()); run_case("salloc_overflow_alloc", salloc_overflow_alloc()); run_case("salloc_multiple_alloc", salloc_multiple_alloc()); run_case("salloc_alloc_not_enough", salloc_alloc_not_enough()); run_case("salloc_basic_free", salloc_basic_free()); run_case("salloc_full_free", salloc_full_free()); run_case("salloc_multiple_free", salloc_multiple_free()); run_case("salloc_free_join_tail", salloc_free_join_tail()); run_case("salloc_free_join_head", salloc_free_join_head()); run_case("salloc_free_join_mid", salloc_free_join_mid()); run_case("salloc_free_join_consecutive", salloc_free_join_consecutive()); run_case("salloc_free_all", salloc_free_all()); test_end(); }