1 /* $OpenBSD: rb-test.c,v 1.4 2008/04/13 00:22:17 djm Exp $ */
3 * Copyright 2002 Niels Provos <provos@citi.umich.edu>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include <sys/types.h>
42 RB_HEAD(tree, node) root;
45 compare(struct node *a, struct node *b)
47 if (a->key < b->key) return (-1);
48 else if (a->key > b->key) return (1);
52 RB_PROTOTYPE(tree, node, node, compare);
54 RB_GENERATE(tree, node, node, compare);
60 ATF_TC_WITHOUT_HEAD(rb_test);
61 ATF_TC_BODY(rb_test, tc)
63 struct node *tmp, *ins;
66 max = min = 42; /* pacify gcc */
70 for (i = 0; i < ITER; i++) {
71 tmp = malloc(sizeof(struct node));
72 ATF_CHECK_MSG(tmp != NULL, "malloc failed");
74 tmp->key = arc4random_uniform(MAX-MIN);
76 } while (RB_FIND(tree, &root, tmp) != NULL);
85 ATF_CHECK_EQ(NULL, RB_INSERT(tree, &root, tmp));
88 ins = RB_MIN(tree, &root);
89 ATF_CHECK_EQ(min, ins->key);
91 ins = RB_MAX(tree, &root);
92 ATF_CHECK_EQ(max, ins->key);
94 ATF_CHECK_EQ(tmp, RB_REMOVE(tree, &root, tmp));
96 for (i = 0; i < ITER - 1; i++) {
98 ATF_CHECK_MSG(tmp != NULL, "RB_ROOT error");
99 ATF_CHECK_EQ(tmp, RB_REMOVE(tree, &root, tmp));
107 ATF_TP_ADD_TC(tp, rb_test);
109 return (atf_no_error());