#include #include #include "test.h" /* required defs */ #define SPVEC_TYPE int #define SPVEC_NAME ints /* optional defs */ #define SPVEC_MALLOC mallocc #define SPVEC_CALLOC callocc #define SPVEC_REALLOC reallocc #define SPVEC_FREE free #include int main() { #if defined(COVERAGE) assert(!covsrv_init()); atexit(covsrv_destroy); #endif struct ints ints = ints_create(); foreach(ints, iter, &ints) { assert(false && "iterating empty spvec"); } /* ensure stability before we do anything else */ assert(ints_reserve(&ints, 1)); int *p = ints_at(&ints, 0); /* should allocate at least a few extra buckets */ assert(ints_reserve(&ints, 256)); assert(p == ints_at(&ints, 0)); /* test out resetting as well while we're at it*/ assert(ints_len(&ints) == 256); ints_reset(&ints); assert(ints_len(&ints) == 0); 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); } int i = 0; foreach(ints, iter, &ints) { assert(iter.v && *iter.v == i); i++; } /* TEN million !!1! */ 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); ints_destroy(&ints); }