diff options
Diffstat (limited to 'tests/vec.c')
| -rw-r--r-- | tests/vec.c | 73 |
1 files changed, 68 insertions, 5 deletions
diff --git a/tests/vec.c b/tests/vec.c index a84096c..09a8deb 100644 --- a/tests/vec.c +++ b/tests/vec.c @@ -1,18 +1,47 @@ +#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> +/* used for sorting testing */ +static int int_comp(int *a, int *b) +{ + return *a - *b; +} + 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); + foreach(ints, iter, &ints) { + assert(false && "iterating empty vec"); } - assert(ints_len(&ints) == 1000000); - for (int i = 0; i < 1000000; ++i) { + for (int i = 0; i < ITER; ++i) { + if (!ints_append(&ints, i)) { + fprintf(stderr, "failed appending %d to vec\n", i); + ints_destroy(&ints); + return -1; + } + } + assert(ints_len(&ints) == ITER); + + for (int i = 0; i < ITER; ++i) { int *v = ints_at(&ints, i); assert(v && *v == i); } @@ -23,10 +52,44 @@ int main() i++; } - for (int i = 1000000 - 1; i >= 0; --i) { + if (!ints_reserve(&ints, 10 * ITER)) { + 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, ITER); + + /* so the above is equivalent to */ + assert(ints_reserve(&ints, ITER)); + assert(ints_len(&ints) == ITER); + + for (int i = ITER - 1; i >= 0; --i) { ints_remove(&ints, i); } assert(ints_len(&ints) == 0); + /* test out resetting as well */ + assert(ints_reserve(&ints, 10)); + assert(ints_len(&ints) == 10); + + ints_reset(&ints); + assert(ints_len(&ints) == 0); + + /* try out sorting and special accesses */ + ints_append(&ints, 3); + ints_append(&ints, 2); + ints_append(&ints, 1); + + ints_sort(&ints, int_comp); + + assert(*ints_back(&ints) == 3); + assert(*ints_pop(&ints) == 3); + assert(*ints_back(&ints) == 2); + ints_destroy(&ints); } |
