1 /* $OpenBSD: rb-test.c,v 1.4 2008/04/13 00:22:17 djm Exp $ */
3 * Copyright 2019 Edward Tomasz Napierala <trasz@FreeBSD.org>
4 * Copyright 2002 Niels Provos <provos@citi.umich.edu>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/types.h>
42 static ARB32_HEAD(tree, node) *root;
45 compare(const struct node *a, const struct node *b)
47 if (a->key < b->key) return (-1);
48 else if (a->key > b->key) return (1);
52 ARB_PROTOTYPE(tree, node, next, compare);
54 ARB_GENERATE(tree, node, next, compare);
60 ATF_TC_WITHOUT_HEAD(arb_test);
61 ATF_TC_BODY(arb_test, tc)
63 struct node *tmp, *ins;
66 max = min = 42; /* pacify gcc */
68 root = (struct tree *)calloc(1, ARB_ALLOCSIZE(root, ITER, tmp));
70 ARB_INIT(tmp, next, root, ITER);
72 for (i = 0; i < ITER; i++) {
73 tmp = ARB_GETFREE(root, next);
74 ATF_REQUIRE_MSG(tmp != NULL, "ARB_GETFREE failed");
76 tmp->key = arc4random_uniform(MAX-MIN);
78 } while (ARB_FIND(tree, root, tmp) != NULL);
87 ATF_REQUIRE_EQ(NULL, ARB_INSERT(tree, root, tmp));
90 ins = ARB_MIN(tree, root);
91 ATF_REQUIRE_MSG(ins != NULL, "ARB_MIN error");
92 ATF_CHECK_EQ(min, ins->key);
94 ins = ARB_MAX(tree, root);
95 ATF_REQUIRE_MSG(ins != NULL, "ARB_MAX error");
96 ATF_CHECK_EQ(max, ins->key);
98 ATF_CHECK_EQ(tmp, ARB_REMOVE(tree, root, tmp));
100 for (i = 0; i < ITER - 1; i++) {
101 tmp = ARB_ROOT(root);
102 ATF_REQUIRE_MSG(tmp != NULL, "ARB_ROOT error");
103 ATF_CHECK_EQ(tmp, ARB_REMOVE(tree, root, tmp));
110 ATF_TP_ADD_TC(tp, arb_test);
112 return (atf_no_error());