commit bdf283f2af6bf4b852c06969598df43417172962
parent 7f3a85372fd15944e178ffa18fb2319e530cc123
Author: vaplv <vaplv@free.fr>
Date: Mon, 24 Feb 2014 22:02:37 +0100
Add the <BEGIN|END>_DECLS rsys macro
Fix an issue in the dynamic array reserve function
Diffstat:
3 files changed, 140 insertions(+), 21 deletions(-)
diff --git a/src/dynamic_array.h b/src/dynamic_array.h
@@ -135,7 +135,9 @@ DARRAY_FUNC__(reserve)(struct DARRAY_TYPE__* darray, const size_t sz)
if(darray->size) {
size_t i = 0;
FOR_EACH(i, 0, darray->size) {
- const int err = DARRAY_FUNCTOR_COPY_AND_RELEASE(data+i, darray->data+i);
+ int err = 0;
+ DARRAY_FUNCTOR_INIT(darray->allocator, data+i);
+ err = DARRAY_FUNCTOR_COPY_AND_RELEASE(data+i, darray->data+i);
if(err) {
MEM_FREE(darray->allocator, data);
return err;
diff --git a/src/rsys.h b/src/rsys.h
@@ -206,5 +206,13 @@
#define OFFSET_PTR(Ptr, Offset) (void*)((uintptr_t)(Ptr) + (Offset))
+#ifdef __cplusplus
+ #define BEGIN_DECLS extern "C" {
+ #define END_DECLS }
+#else
+ #define BEGIN_DECLS
+ #define END_DECLS
+#endif
+
#endif /* RSYS_H */
diff --git a/src/test_dynamic_array.c b/src/test_dynamic_array.c
@@ -4,27 +4,42 @@
#define DARRAY_DATA const char*
#include "dynamic_array.h"
-int
-main(int argc, char** argv)
+const char* strs[] = {
+ "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!",
+
+ "Rcvfbqr 2, GUR FUBERF BS URYY:\n\
+ ------------------------------\n\
+ \n\
+ LBH'IR QBAR VG! GUR UVQRBHF PLORE- QRZBA YBEQ GUNG EHYRQ GUR YBFG QRVZBF ZBBA\n\
+ ONFR UNF ORRA FYNVA NAQ LBH NER GEVHZCUNAG! OHG ... JURER NER LBH? LBH\n\
+ PYNZORE GB GUR RQTR BS GUR ZBBA NAQ YBBX QBJA GB FRR GUR NJSHY GEHGU.\n\
+ \n",
+
+ "QRVZBF SYBNGF NOBIR URYY VGFRYS! LBH'IR ARIRE URNEQ BS NALBAR RFPNCVAT SEBZ\n\
+ URYY, OHG LBH'YY ZNXR GUR ONFGNEQF FBEEL GURL RIRE URNEQ BS LBH! DHVPXYL, LBH\n\
+ ENCCRY QBJA GB GUR FHESNPR BS URYY.\n\
+ \n\
+ ABJ, VG'F BA GB GUR SVANY PUNCGRE BS QBBZ! -- VASREAB."
+
+};
+const size_t nstrs = sizeof(strs)/sizeof(const char*);
+
+static void
+test_primitive_type(void)
{
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);
@@ -34,7 +49,7 @@ main(int argc, char** argv)
CHECK(darray_str_size_get(&darray), 0);
FOR_EACH(i, 0, 4) {
- darray_str_push_back(&darray, str + i);
+ darray_str_push_back(&darray, strs + i);
}
CHECK(darray_str_size_get(&darray), 4);
darray_str_clear(&darray);
@@ -43,10 +58,10 @@ main(int argc, char** argv)
darray_str_init(&allocator_proxy, &darray);
FOR_EACH(i, 0, nstrs) {
- darray_str_push_back(&darray, str + i);
+ darray_str_push_back(&darray, strs + i);
}
FOR_EACH(i, 0, nstrs) {
- CHECK(strcmp(darray_str_cdata_get(&darray)[i], str[i]), 0);
+ CHECK(strcmp(darray_str_cdata_get(&darray)[i], strs[i]), 0);
}
darray_str_clear(&darray);
CHECK(darray_str_size_get(&darray), 0);
@@ -69,6 +84,100 @@ main(int argc, char** argv)
FATAL("Memory leaks\n");
}
mem_shutdown_proxy_allocator(&allocator_proxy);
+}
+
+#include "str.h"
+#define DARRAY_NAME struct_str
+#define DARRAY_DATA struct str
+#define DARRAY_FUNCTOR_INIT str_init
+#define DARRAY_FUNCTOR_COPY str_copy
+#define DARRAY_FUNCTOR_RELEASE str_release
+#define DARRAY_FUNCTOR_COPY_AND_RELEASE str_copy_and_release
+#include "dynamic_array.h"
+
+static void
+test_struct(void)
+{
+ struct mem_allocator allocator_proxy;
+ struct darray_struct_str darray, darray2;
+ const size_t nstrs = sizeof(strs)/sizeof(const char*);
+ size_t i = 0;
+
+ mem_init_proxy_allocator(&allocator_proxy, &mem_default_allocator);
+
+ darray_struct_str_init(&allocator_proxy, &darray);
+ darray_struct_str_init(&allocator_proxy, &darray2);
+ CHECK(darray_struct_str_size_get(&darray), 0);
+ CHECK(darray_struct_str_size_get(&darray2), 0);
+
+ darray_struct_str_resize(&darray, nstrs);
+ FOR_EACH(i, 0, nstrs) {
+ str_set(darray_struct_str_data_get(&darray) + i, strs[i]);
+ }
+ CHECK(darray_struct_str_size_get(&darray), nstrs);
+ FOR_EACH(i, 0, nstrs) {
+ const struct str* str = darray_struct_str_cdata_get(&darray) + i;
+ CHECK(strcmp(str_cget(str), strs[i]), 0);
+ }
+
+ darray_struct_str_copy(&darray, &darray2);
+ CHECK(darray_struct_str_size_get(&darray), 0);
+ FOR_EACH(i, 0, nstrs) {
+ struct str str;
+ str_init(&allocator_proxy, &str);
+ str_set(&str, strs[i]);
+ darray_struct_str_push_back(&darray, &str);
+ str_release(&str);
+ }
+
+ CHECK(darray_struct_str_size_get(&darray), nstrs);
+ FOR_EACH(i, 0, nstrs) {
+ const struct str* str = darray_struct_str_cdata_get(&darray) + i;
+ CHECK(strcmp(str_cget(str), strs[i]), 0);
+ }
+
+ darray_struct_str_copy(&darray2, &darray);
+ CHECK(darray_struct_str_size_get(&darray2), nstrs);
+ FOR_EACH(i, 0, nstrs) {
+ const struct str* str = darray_struct_str_cdata_get(&darray) + i;
+ const struct str* str2 = darray_struct_str_cdata_get(&darray2) + i;
+ CHECK(strcmp(str_cget(str), strs[i]), 0);
+ CHECK(strcmp(str_cget(str2), strs[i]), 0);
+ }
+ darray_struct_str_clear(&darray2);
+ CHECK(darray_struct_str_size_get(&darray2), 0);
+
+ darray_struct_str_copy_and_clear(&darray2, &darray);
+ CHECK(darray_struct_str_size_get(&darray2), nstrs);
+ CHECK(darray_struct_str_size_get(&darray), 0);
+ FOR_EACH(i, 0, nstrs) {
+ const struct str* str = darray_struct_str_cdata_get(&darray2) + i;
+ CHECK(strcmp(str_cget(str), strs[i]), 0);
+ }
+
+ darray_struct_str_copy_and_release(&darray, &darray2);
+ FOR_EACH(i, 0, nstrs) {
+ const struct str* str = darray_struct_str_cdata_get(&darray) + i;
+ CHECK(strcmp(str_cget(str), strs[i]), 0);
+ }
+
+ darray_struct_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);
+}
+
+int
+main(int argc, char** argv)
+{
+ (void)argc, (void)argv;
+ test_primitive_type();
+ test_struct();
CHECK(mem_allocated_size(), 0);
return 0;
}