summaryrefslogtreecommitdiff
path: root/tests/sptree.c
diff options
context:
space:
mode:
Diffstat (limited to 'tests/sptree.c')
-rw-r--r--tests/sptree.c68
1 files changed, 63 insertions, 5 deletions
diff --git a/tests/sptree.c b/tests/sptree.c
index b8d1e5a..3499d61 100644
--- a/tests/sptree.c
+++ b/tests/sptree.c
@@ -1,24 +1,57 @@
#include <assert.h>
+#include <stdlib.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>
+#define RANDITER (ITER / 100)
+
+int inserted[RANDITER];
+
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);
+ /* check that iterating an empty tree doesn't do anything */
+ foreach(ints, iter, &ints) {
+ assert(false && "iterating empty tree");
}
- assert(ints_len(&ints) == 1000000);
- for (int i = 0; i < 1000000; ++i) {
+ for (int i = 0; i < ITER; ++i) {
+ if (!ints_insert(&ints, i)) {
+ fprintf(stderr, "failed inserting %d\n", i);
+ ints_destroy(&ints);
+ return -1;
+ }
+ }
+ assert(ints_len(&ints) == ITER);
+
+ for (int i = 0; i < ITER; ++i) {
int *v = ints_find(&ints, i);
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
@@ -28,10 +61,35 @@ int main()
i++;
}
- for (int i = 0; i < 1000000; ++i) {
+ for (int i = 0; i < ITER; ++i) {
ints_remove(&ints, i);
}
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);
+
+ for (int i = 0; i < RANDITER; ++i) {
+ inserted[i] = rand();
+
+ /* covsrv shouldn't fail anymore */
+ assert(ints_insert(&ints, inserted[i]));
+ }
+
+ for (int i = 0; i < RANDITER; ++i) {
+ int *v = ints_find(&ints, inserted[i]);
+ assert(v && *v == inserted[i]);
+ }
+
+ for (int i = 0; i < RANDITER; ++i) {
+ ints_remove(&ints, inserted[i]);
+ }
+
+ assert(ints_len(&ints) == 0);
+
ints_destroy(&ints);
}