From 13d33be824a0f6a3952045df7b97b35fc69520a8 Mon Sep 17 00:00:00 2001 From: Kimplul Date: Fri, 22 Aug 2025 16:12:08 +0300 Subject: cover all functions + Not quite all lines due to sptree being dumb >:((((( --- include/conts/sptree.h | 1 + tests/map.c | 28 ++++++++++++++++++++++++++-- tests/sptree.c | 38 ++++++++++++++++++++++++++++++++++++++ tests/vec.c | 28 ++++++++++++++++++++++++++++ 4 files changed, 93 insertions(+), 2 deletions(-) diff --git a/include/conts/sptree.h b/include/conts/sptree.h index 1464d0f..53c3e73 100644 --- a/include/conts/sptree.h +++ b/include/conts/sptree.h @@ -119,6 +119,7 @@ static inline SPTREE_TYPE *SPTREE(next)(SPTREE_TYPE *prev) n = p; } + /* god damn it, stop ruining my coverage you bastard */ return NULL; } diff --git a/tests/map.c b/tests/map.c index a370413..505be58 100644 --- a/tests/map.c +++ b/tests/map.c @@ -5,7 +5,7 @@ /* required defs */ #define MAP_KEY int #define MAP_TYPE int -#define MAP_HASH(a) CONTS_MAP_NO_HASH(a) +#define MAP_HASH(a) ints_generic_hash(&(a)) #define MAP_CMP(a, b) ((a) - (b)) #define MAP_NAME ints @@ -26,7 +26,17 @@ int main() /* 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); + struct ints ints = ints_create(10); + + /* check that trying to search for something in an empty map doesn*t + * crash */ + assert(ints_find(&ints, 0) == NULL); + + /* similarly, iterating an empty map doesn't do anything */ + foreach(ints, iter, &ints) { + assert(false && "iterating empty map"); + } + for (int i = 0; i < 1000000; ++i) { if (!ints_insert(&ints, i, i)) { fprintf(stderr, "failed inserting %d\n", i); @@ -36,11 +46,25 @@ int main() } assert(ints_len(&ints) == 1000000); + for (int i = 0; i < 1000000; ++i) { int *v = ints_find(&ints, i); assert(v && *v == i); } + /* check that trying to find something that doesn't exist doesn't crash */ + assert(ints_find(&ints, 123456789) == NULL); + + /* check that trying to remove something that doesn't exist is a noop */ + size_t len = ints_len(&ints); + ints_remove(&ints, 123456789); + assert(ints_len(&ints) == len); + + /* check that trying to insert 0 again gets us the existing 0 */ + int *orig = ints_find(&ints, 0); + int *new = ints_insert(&ints, 0, 0); + assert(orig == new); + size_t count = 0; foreach(ints, iter, &ints) { assert(iter->key == iter->data); diff --git a/tests/sptree.c b/tests/sptree.c index 6ab3d6a..9eff356 100644 --- a/tests/sptree.c +++ b/tests/sptree.c @@ -1,4 +1,5 @@ #include +#include #include #include "test.h" @@ -23,6 +24,11 @@ int main() #endif struct ints ints = ints_create(); + /* check that iterating an empty tree doesn't do anything */ + foreach(ints, iter, &ints) { + assert(false && "iterating empty tree"); + } + for (int i = 0; i < 1000000; ++i) { if (!ints_insert(&ints, i)) { fprintf(stderr, "failed inserting %d\n", i); @@ -37,6 +43,11 @@ int main() assert(v && *v == i); } + /* check that inserting duplicate returns the original */ + int *orig = ints_find(&ints, 0); + ints_insert(&ints, 0); + assert(ints_find(&ints, 0) == orig); + int i = 0; foreach(ints, iter, &ints) { /* since my trees are ordered, this must hold, although you @@ -51,5 +62,32 @@ int main() } assert(ints_len(&ints) == 0); + + /* check that removing nonexistant item (or empty tree) doesn't crash */ + ints_remove(&ints, 0); + + /* insert random integers to hopefully exercise the code a bit more */ + srand(0); + + int inserted[1000]; + for (int i = 0; i < 1000; ++i) { + inserted[i] = rand(); + + /* covsrv shouldn't fail anymore */ + assert(ints_insert(&ints, inserted[i])); + } + + for (int i = 0; i < 1000; ++i) { + int *v = ints_find(&ints, inserted[i]); + assert(v && *v == inserted[i]); + } + + for (int i = 0; i < 1000; ++i) { + ints_remove(&ints, inserted[i]); + } + + assert(ints_len(&ints) == 0); + + ints_destroy(&ints); } diff --git a/tests/vec.c b/tests/vec.c index 8531b14..1ed5f72 100644 --- a/tests/vec.c +++ b/tests/vec.c @@ -14,6 +14,12 @@ #include +/* used for sorting testing */ +static int int_comp(int *a, int *b) +{ + return *a - *b; +} + int main() { #if defined(COVERAGE) @@ -55,10 +61,32 @@ int main() * maybe fails?) */ ints_shrink(&ints, 1000000); + /* so the above is equivalent to */ + assert(ints_reserve(&ints, 1000000)); + assert(ints_len(&ints) == 1000000); + for (int i = 1000000 - 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); } -- cgit v1.2.3