diff options
author | Kimplul <kimi.h.kuparinen@gmail.com> | 2025-08-22 15:27:17 +0300 |
---|---|---|
committer | Kimplul <kimi.h.kuparinen@gmail.com> | 2025-08-22 15:27:17 +0300 |
commit | b0d619e2c9595f4ec05463e87be9d0d3423c0a70 (patch) | |
tree | dfcda5da356b660fadb8b133772c926c08908cbb /tests | |
parent | 7774ae2f8c2dca9ab2d93082856f031e78a1b5f0 (diff) | |
download | conts-b0d619e2c9595f4ec05463e87be9d0d3423c0a70.tar.gz conts-b0d619e2c9595f4ec05463e87be9d0d3423c0a70.zip |
use covsrv for coverage testing
Diffstat (limited to 'tests')
-rw-r--r-- | tests/map.c | 20 | ||||
-rw-r--r-- | tests/sptree.c | 20 | ||||
-rw-r--r-- | tests/test.h | 12 | ||||
-rw-r--r-- | tests/vec.c | 34 |
4 files changed, 83 insertions, 3 deletions
diff --git a/tests/map.c b/tests/map.c index 6c42787..a370413 100644 --- a/tests/map.c +++ b/tests/map.c @@ -1,20 +1,38 @@ #include <assert.h> #include <stdio.h> +#include "test.h" +/* required defs */ #define MAP_KEY int #define MAP_TYPE int #define MAP_HASH(a) CONTS_MAP_NO_HASH(a) #define MAP_CMP(a, b) ((a) - (b)) #define MAP_NAME ints + +/* optional defs */ +#define MAP_MALLOC mallocc +#define MAP_CALLOC callocc +#define MAP_REALLOC reallocc +#define MAP_FREE free + #include <conts/map.h> int main() { +#if defined(COVERAGE) + assert(!covsrv_init()); + atexit(covsrv_destroy); +#endif + /* heuristic, but if we know how many elements we'll need, we should * give it to the create function. */ struct ints ints = ints_create(0); for (int i = 0; i < 1000000; ++i) { - ints_insert(&ints, i, i); + if (!ints_insert(&ints, i, i)) { + fprintf(stderr, "failed inserting %d\n", i); + ints_destroy(&ints); + return -1; + } } assert(ints_len(&ints) == 1000000); diff --git a/tests/sptree.c b/tests/sptree.c index b8d1e5a..6ab3d6a 100644 --- a/tests/sptree.c +++ b/tests/sptree.c @@ -1,16 +1,34 @@ #include <assert.h> #include <stdio.h> +#include "test.h" +/* required defs */ #define SPTREE_TYPE int #define SPTREE_CMP(a, b) ((b) - (a)) #define SPTREE_NAME ints + +/* optional defs */ +#define SPTREE_MALLOC mallocc +#define SPTREE_CALLOC callocc +#define SPTREE_REALLOC reallocc +#define SPTREE_FREE free + #include <conts/sptree.h> int main() { +#if defined(COVERAGE) + assert(!covsrv_init()); + atexit(covsrv_destroy); +#endif + struct ints ints = ints_create(); for (int i = 0; i < 1000000; ++i) { - ints_insert(&ints, i); + if (!ints_insert(&ints, i)) { + fprintf(stderr, "failed inserting %d\n", i); + ints_destroy(&ints); + return -1; + } } assert(ints_len(&ints) == 1000000); diff --git a/tests/test.h b/tests/test.h new file mode 100644 index 0000000..2fa0879 --- /dev/null +++ b/tests/test.h @@ -0,0 +1,12 @@ +#ifndef TEST_H +#define TEST_H + +#include <covsrv/covsrv.h> + +#define cover_ptr(name, ...) ({covsrv_die() ? NULL : name (__VA_ARGS__);}) + +#define mallocc(...) cover_ptr(malloc, __VA_ARGS__) +#define callocc(...) cover_ptr(calloc, __VA_ARGS__) +#define reallocc(...) cover_ptr(realloc, __VA_ARGS__) + +#endif /* TEST_H */ diff --git a/tests/vec.c b/tests/vec.c index a84096c..8531b14 100644 --- a/tests/vec.c +++ b/tests/vec.c @@ -1,14 +1,33 @@ +#include <stdio.h> #include <assert.h> +#include "test.h" +/* required defs */ #define VEC_TYPE int #define VEC_NAME ints + +/* optional defs */ +#define VEC_MALLOC mallocc +#define VEC_CALLOC callocc +#define VEC_REALLOC reallocc +#define VEC_FREE free + #include <conts/vec.h> int main() { +#if defined(COVERAGE) + assert(!covsrv_init()); + atexit(covsrv_destroy); +#endif + struct ints ints = ints_create(0); for (int i = 0; i < 1000000; ++i) { - ints_append(&ints, i); + if (!ints_append(&ints, i)) { + fprintf(stderr, "failed appending %d to vec\n", i); + ints_destroy(&ints); + return -1; + } } assert(ints_len(&ints) == 1000000); @@ -23,6 +42,19 @@ int main() i++; } + /* TEN million !!1! */ + if (!ints_reserve(&ints, 10000000)) { + fprintf(stderr, "failed reserving vec\n"); + ints_destroy(&ints); + return -1; + } + + /* set size back to keep test runtime reasonable + * (is shrink necessary when we already have reserve? I + * guess it shows intention a bit better and just asserts instead of + * maybe fails?) */ + ints_shrink(&ints, 1000000); + for (int i = 1000000 - 1; i >= 0; --i) { ints_remove(&ints, i); } |