rsys

Basic data structures and low-level features
git clone git://git.meso-star.fr/rsys.git
Log | Files | Refs | README | LICENSE

commit e74df4f6cbd2cfe496b70e24b92704ac5998d5d0
parent 07061827c4018b47979c366f02effde49f1231a6
Author: vaplv <vaplv@free.fr>
Date:   Fri, 15 Mar 2019 09:03:28 +0100

Test the allocation policy of the dynamic arrays

Diffstat:
Msrc/test_dynamic_array.c | 78++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 78 insertions(+), 0 deletions(-)

diff --git a/src/test_dynamic_array.c b/src/test_dynamic_array.c @@ -358,6 +358,83 @@ test_alignment(struct mem_allocator* allocator) darray_byte1K_release(&bytes1K); } +static void +test_allocation_policy(struct mem_allocator* allocator) +{ + struct darray_int integers; + const int* mem; + int i; + + darray_int_init(allocator, &integers); + CHK(darray_int_capacity(&integers) == 0); + + CHK(darray_int_reserve(&integers, 33) == RES_OK); + CHK(darray_int_capacity(&integers) == 33); + CHK(darray_int_size_get(&integers) == 0); + mem = darray_int_cdata_get(&integers); + + CHK(darray_int_resize(&integers, 14) == RES_OK); + CHK(darray_int_capacity(&integers) == 33); + CHK(darray_int_size_get(&integers) == 14); + CHK(darray_int_cdata_get(&integers) == mem); + + darray_int_clear(&integers); + CHK(darray_int_capacity(&integers) == 33); + CHK(darray_int_size_get(&integers) == 0); + CHK(darray_int_cdata_get(&integers) == mem); + + CHK(darray_int_resize(&integers, 35) == RES_OK); + CHK(darray_int_capacity(&integers) == 35); + CHK(darray_int_size_get(&integers) == 35); + CHK(darray_int_cdata_get(&integers) != mem); + + darray_int_purge(&integers); + CHK(darray_int_capacity(&integers) == 0); + CHK(darray_int_size_get(&integers) == 0); + + i = 0; + CHK(darray_int_push_back(&integers, &i) == RES_OK); + CHK(darray_int_capacity(&integers) == 1); + CHK(darray_int_size_get(&integers) == 1); + mem = darray_int_cdata_get(&integers); + + CHK(darray_int_push_back(&integers, &i) == RES_OK); + CHK(darray_int_capacity(&integers) == 2); + CHK(darray_int_size_get(&integers) == 2); + CHK(darray_int_cdata_get(&integers) != mem); + mem = darray_int_cdata_get(&integers); + + CHK(darray_int_push_back(&integers, &i) == RES_OK); + CHK(darray_int_capacity(&integers) == 4); + CHK(darray_int_size_get(&integers) == 3); + CHK(darray_int_cdata_get(&integers) != mem); + mem = darray_int_cdata_get(&integers); + + CHK(darray_int_push_back(&integers, &i) == RES_OK); + CHK(darray_int_capacity(&integers) == 4); + CHK(darray_int_size_get(&integers) == 4); + CHK(darray_int_cdata_get(&integers) == mem); + + CHK(darray_int_push_back(&integers, &i) == RES_OK); + CHK(darray_int_capacity(&integers) == 8); + CHK(darray_int_size_get(&integers) == 5); + CHK(darray_int_cdata_get(&integers) != mem); + mem = darray_int_cdata_get(&integers); + + darray_int_purge(&integers); + CHK(darray_int_capacity(&integers) == 0); + CHK(darray_int_size_get(&integers) == 0); + CHK(darray_int_cdata_get(&integers) == NULL); + + FOR_EACH(i, 0, 33) { + CHK(darray_int_push_back(&integers, &i) == RES_OK); + } + CHK(darray_int_capacity(&integers) == 64); + CHK(darray_int_size_get(&integers) == 33); + + darray_int_release(&integers); +} + int main(int argc, char** argv) { @@ -378,6 +455,7 @@ main(int argc, char** argv) test_swap_string(&allocator_proxy, &allocator_proxy); test_alignment(&mem_default_allocator); + test_allocation_policy(&mem_default_allocator); check_memory_allocator(&allocator_proxy); mem_shutdown_proxy_allocator(&allocator_proxy);