1459d04a5SEd Schouten /*- 2459d04a5SEd Schouten * Copyright (c) 2015 Nuxi, https://nuxi.nl/ 3459d04a5SEd Schouten * 4459d04a5SEd Schouten * Redistribution and use in source and binary forms, with or without 5459d04a5SEd Schouten * modification, are permitted provided that the following conditions 6459d04a5SEd Schouten * are met: 7459d04a5SEd Schouten * 1. Redistributions of source code must retain the above copyright 8459d04a5SEd Schouten * notice, this list of conditions and the following disclaimer. 9459d04a5SEd Schouten * 2. Redistributions in binary form must reproduce the above copyright 10459d04a5SEd Schouten * notice, this list of conditions and the following disclaimer in the 11459d04a5SEd Schouten * documentation and/or other materials provided with the distribution. 12459d04a5SEd Schouten * 13459d04a5SEd Schouten * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 14459d04a5SEd Schouten * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15459d04a5SEd Schouten * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16459d04a5SEd Schouten * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 17459d04a5SEd Schouten * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18459d04a5SEd Schouten * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 19459d04a5SEd Schouten * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 20459d04a5SEd Schouten * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 21459d04a5SEd Schouten * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22459d04a5SEd Schouten * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23459d04a5SEd Schouten * SUCH DAMAGE. 24459d04a5SEd Schouten */ 25459d04a5SEd Schouten 26459d04a5SEd Schouten #include <sys/cdefs.h> 27459d04a5SEd Schouten __FBSDID("$FreeBSD$"); 28459d04a5SEd Schouten 29459d04a5SEd Schouten #include <atf-c.h> 30459d04a5SEd Schouten #define _SEARCH_PRIVATE 31459d04a5SEd Schouten #include <search.h> 32459d04a5SEd Schouten #include <stdbool.h> 33459d04a5SEd Schouten #include <stdlib.h> 34459d04a5SEd Schouten 35459d04a5SEd Schouten /* Validates the integrity of an AVL tree. */ 36459d04a5SEd Schouten static inline unsigned int 37*4ef9bd22SEd Schouten tnode_assert(const posix_tnode *n) 38459d04a5SEd Schouten { 39459d04a5SEd Schouten unsigned int height_left, height_right; 40459d04a5SEd Schouten int balance; 41459d04a5SEd Schouten 42459d04a5SEd Schouten if (n == NULL) 43459d04a5SEd Schouten return 0; 44459d04a5SEd Schouten height_left = tnode_assert(n->llink); 45459d04a5SEd Schouten height_right = tnode_assert(n->rlink); 46459d04a5SEd Schouten balance = (int)height_left - (int)height_right; 47459d04a5SEd Schouten ATF_CHECK(balance >= -1); 48459d04a5SEd Schouten ATF_CHECK(balance <= 1); 49459d04a5SEd Schouten ATF_CHECK_EQ(balance, n->balance); 50459d04a5SEd Schouten return (height_left > height_right ? height_left : height_right) + 1; 51459d04a5SEd Schouten } 52459d04a5SEd Schouten 53459d04a5SEd Schouten static int 54459d04a5SEd Schouten compar(const void *a, const void *b) 55459d04a5SEd Schouten { 56459d04a5SEd Schouten 57459d04a5SEd Schouten return *(int *)a - *(int *)b; 58459d04a5SEd Schouten } 59459d04a5SEd Schouten 60459d04a5SEd Schouten ATF_TC_WITHOUT_HEAD(tsearch_test); 61459d04a5SEd Schouten ATF_TC_BODY(tsearch_test, tc) 62459d04a5SEd Schouten { 63459d04a5SEd Schouten /* 64459d04a5SEd Schouten * Run the test below in a deterministic fashion to prevent this 65459d04a5SEd Schouten * test from potentially flapping. We assume that this provides 66459d04a5SEd Schouten * enough coverage. 67459d04a5SEd Schouten */ 68459d04a5SEd Schouten #if 0 69459d04a5SEd Schouten unsigned short random_state[3]; 70459d04a5SEd Schouten arc4random_buf(random_state, sizeof(random_state)); 71459d04a5SEd Schouten #else 72459d04a5SEd Schouten unsigned short random_state[3] = { 26554, 13330, 3246 }; 73459d04a5SEd Schouten #endif 74459d04a5SEd Schouten 75459d04a5SEd Schouten #define NKEYS 1000 76459d04a5SEd Schouten /* Create 1000 possible keys. */ 77459d04a5SEd Schouten int keys[NKEYS]; 78459d04a5SEd Schouten for (int i = 0; i < NKEYS; ++i) 79459d04a5SEd Schouten keys[i] = i; 80459d04a5SEd Schouten 81459d04a5SEd Schouten /* Apply random operations on a binary tree and check the results. */ 82*4ef9bd22SEd Schouten posix_tnode *root = NULL; 83459d04a5SEd Schouten bool present[NKEYS] = {}; 84459d04a5SEd Schouten for (int i = 0; i < NKEYS * 10; ++i) { 85459d04a5SEd Schouten int key = nrand48(random_state) % NKEYS; 86459d04a5SEd Schouten switch (nrand48(random_state) % 3) { 87459d04a5SEd Schouten case 0: /* tdelete(). */ 88459d04a5SEd Schouten if (present[key]) { 89459d04a5SEd Schouten ATF_CHECK(tdelete(&key, &root, compar) != NULL); 90459d04a5SEd Schouten present[key] = false; 91459d04a5SEd Schouten } else { 92459d04a5SEd Schouten ATF_CHECK_EQ(NULL, 93459d04a5SEd Schouten tdelete(&key, &root, compar)); 94459d04a5SEd Schouten } 95459d04a5SEd Schouten break; 96459d04a5SEd Schouten case 1: /* tfind(). */ 97459d04a5SEd Schouten if (present[key]) { 98459d04a5SEd Schouten ATF_CHECK_EQ(&keys[key], 99459d04a5SEd Schouten *(int **)tfind(&key, &root, compar)); 100459d04a5SEd Schouten } else { 101459d04a5SEd Schouten ATF_CHECK_EQ(NULL, tfind(&key, &root, compar)); 102459d04a5SEd Schouten } 103459d04a5SEd Schouten break; 104459d04a5SEd Schouten case 2: /* tsearch(). */ 105459d04a5SEd Schouten if (present[key]) { 106459d04a5SEd Schouten ATF_CHECK_EQ(&keys[key], 107459d04a5SEd Schouten *(int **)tsearch(&key, &root, compar)); 108459d04a5SEd Schouten } else { 109459d04a5SEd Schouten ATF_CHECK_EQ(&keys[key], *(int **)tsearch( 110459d04a5SEd Schouten &keys[key], &root, compar)); 111459d04a5SEd Schouten present[key] = true; 112459d04a5SEd Schouten } 113459d04a5SEd Schouten break; 114459d04a5SEd Schouten } 115459d04a5SEd Schouten tnode_assert(root); 116459d04a5SEd Schouten } 117459d04a5SEd Schouten 118459d04a5SEd Schouten /* Remove all entries from the tree. */ 119459d04a5SEd Schouten for (int key = 0; key < NKEYS; ++key) 120459d04a5SEd Schouten if (present[key]) 121459d04a5SEd Schouten ATF_CHECK(tdelete(&key, &root, compar) != NULL); 122459d04a5SEd Schouten ATF_CHECK_EQ(NULL, root); 123459d04a5SEd Schouten } 124459d04a5SEd Schouten 125459d04a5SEd Schouten ATF_TP_ADD_TCS(tp) 126459d04a5SEd Schouten { 127459d04a5SEd Schouten 128459d04a5SEd Schouten ATF_TP_ADD_TC(tp, tsearch_test); 129459d04a5SEd Schouten 130459d04a5SEd Schouten return (atf_no_error()); 131459d04a5SEd Schouten } 132