rsys

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

commit 329b872bef8fa8997d024fab6f5699061d711035
parent 983317be660205aa45870a223b957b7a042ae9b5
Author: vaplv <vaplv@free.fr>
Date:   Tue, 18 Feb 2014 16:11:50 +0100

Begin to test the dynamic array

Diffstat:
Mcmake/CMakeLists.txt | 2+-
Msrc/dynamic_array.h | 16++++++++++++----
Msrc/test_dynamic_array.c | 39++++++++++++++++++++++++++++++++++++++-
3 files changed, 51 insertions(+), 6 deletions(-)

diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt @@ -101,7 +101,7 @@ macro(new_test _name) endmacro(new_test) new_test(test_atomic) -new_test(test_dynamic_array) +new_test(test_dynamic_array rsys) new_test(test_free_list rsys) new_test(test_library rsys) new_test(test_list rsys) diff --git a/src/dynamic_array.h b/src/dynamic_array.h @@ -8,7 +8,11 @@ #endif /* DYNAMIC_ARRAY_H */ #else - +/* + * Dynamic array API generated with respect the DARRAY_<NAME|DATA_TYPE> macros + * The name of the generated type is darray_<DARRAY_NAME> and the naming + * convention of its functions is darray_<DARRAY_NAME>_<Function> + */ #ifndef DARRAY_NAME #error "Missing the DARRAY_NAME macro defining the structure name" #endif @@ -17,9 +21,6 @@ #error "Missing the DARRAY_DATA_TYPE macro defining the array data type" #endif -/* - * Dynamic array API generated with respect the DARRAY_<NAME|DATA_TYPE> macros. - */ #define DARRAY_FUNC__(Func) CONCAT(CONCAT(CONCAT(darray_, DARRAY_NAME),_), Func) #define DARRAY_TYPE__ CONCAT(darray_, DARRAY_NAME) @@ -57,6 +58,13 @@ DARRAY_FUNC__(clear)(struct DARRAY_TYPE__* darray) } static FINLINE char +DARRAY_FUNC__(is_empty)(struct DARRAY_TYPE__* darray) +{ + ASSERT(darray); + return darray->size == 0; +} + +static FINLINE char DARRAY_FUNC__(reserve)(struct DARRAY_TYPE__* darray, const size_t sz) { DARRAY_DATA_TYPE* data = NULL; diff --git a/src/test_dynamic_array.c b/src/test_dynamic_array.c @@ -1,13 +1,50 @@ #include "dynamic_array.h" -#define DARRAY_NAME Str +#define DARRAY_NAME str #define DARRAY_DATA_TYPE const char* #include "dynamic_array.h" int main(int argc, char** argv) { + struct mem_allocator allocator_proxy; + struct darray_str darray; + size_t i = 0; + const char* str[] = { + "Rcvfbqr", "1,", "XARR-QRRC", "VA", "GUR", "QRNQ:\n", + "---------------------------------", "BAPR", "LBH", "ORNG", "GUR", "OVT", + "ONQNFFRF", "NAQ", "PYRNA", "BHG", "GUR", "ZBBA", "ONFR", "LBH'ER", + "FHCCBFRQ", "GB\n", "JVA", "NERA'G", "LBH?", "NERA'G", "LBH?", "JURER'F", + "LBHE", "SNG", "ERJNEQ", "NAQ", "GVPXRG", "UBZR?", "JUNG\n", "GUR", "URYY", + "VF", "GUVF?", "VG'F", "ABG", "FHCCBFRQ", "GB", "RAQ", "GUVF", "JNL!", "VG", + "FGVAXF", "YVXR", "EBGGRA", "ZRNG,", "OHG", "YBBXF", "YVXR", "GUR", "YBFG", + "QRVZBF", "ONFR.", "YBBXF", "YVXR\n", "LBH'ER", "FGHPX", "BA", "GUR", + "FUBERF", "BS", "URYY.", "GUR", "BAYL", "JNL", "BHG", "VF", "GUEBHTU.", "GB", + "PBAGVAHR", "GUR", "QBBZ", "RKCREVRAPR,", "CYNL", "GUR", "FUBERF", "BS", + "URYY", "NAQ", "VGF", "NZNMVAT\n", "FRDHRY,", "VASREAB!" + }; + const size_t nstrs = sizeof(str)/sizeof(const char*); (void)argc, (void)argv; + + mem_init_proxy_allocator(&allocator_proxy, &mem_default_allocator); + + darray_str_init(&mem_default_allocator, &darray); + CHECK(darray_str_is_empty(&darray), 1); + + FOR_EACH(i, 0, nstrs) { + darray_str_push_back(&darray, str + i); + } + + darray_str_release(&darray); + + if(MEM_ALLOCATED_SIZE(&allocator_proxy)) { + char dump[512]; + MEM_DUMP(&allocator_proxy, dump, sizeof(dump)/sizeof(char)); + fprintf(stderr, "%s\n", dump); + FATAL("Memory leaks\n"); + } + mem_shutdown_proxy_allocator(&allocator_proxy); + CHECK(mem_allocated_size(), 0); return 0; }