From c384464d25f127a482d8ffc706c3af79a626f62a Mon Sep 17 00:00:00 2001 From: Will Andrews Date: Tue, 6 Jun 2017 03:40:45 +0000 Subject: [PATCH] tsearch_test: Test twalk & add some determinism. --- lib/libc/tests/stdlib/tsearch_test.c | 30 +++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/lib/libc/tests/stdlib/tsearch_test.c b/lib/libc/tests/stdlib/tsearch_test.c index 88c62f580336..ee25743f10b0 100644 --- a/lib/libc/tests/stdlib/tsearch_test.c +++ b/lib/libc/tests/stdlib/tsearch_test.c @@ -31,6 +31,10 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include + +static int n_nodes = 0; +static int n_seen = 0; /* Validates the integrity of an AVL tree. */ static inline unsigned int @@ -57,6 +61,14 @@ compar(const void *a, const void *b) return *(int *)a - *(int *)b; } +static void +treewalk(const posix_tnode *node, VISIT v, int level) +{ + + if (v == postorder || v == leaf) + n_seen++; +} + ATF_TC_WITHOUT_HEAD(tsearch_test); ATF_TC_BODY(tsearch_test, tc) { @@ -83,11 +95,22 @@ ATF_TC_BODY(tsearch_test, tc) bool present[NKEYS] = {}; for (int i = 0; i < NKEYS * 10; ++i) { int key = nrand48(random_state) % NKEYS; - switch (nrand48(random_state) % 3) { + int sample = i; + + /* + * Ensure each case is tested at least 10 times, plus a + * random sampling. + */ + if ((sample % NKEYS) > 3) + sample = nrand48(random_state) % 3; + + switch (sample) { case 0: /* tdelete(). */ if (present[key]) { ATF_CHECK(tdelete(&key, &root, compar) != NULL); present[key] = false; + ATF_CHECK(n_nodes > 0); + n_nodes--; } else { ATF_CHECK_EQ(NULL, tdelete(&key, &root, compar)); @@ -109,12 +132,17 @@ ATF_TC_BODY(tsearch_test, tc) ATF_CHECK_EQ(&keys[key], *(int **)tsearch( &keys[key], &root, compar)); present[key] = true; + n_nodes++; } break; } tnode_assert(root); } + /* Walk the tree. */ + twalk(root, treewalk); + ATF_CHECK_EQ(n_nodes, n_seen); + /* Remove all entries from the tree. */ for (int key = 0; key < NKEYS; ++key) if (present[key])