]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - tests/sys/sys/rb_test.c
contrib/tzdata: import tzdata 2024a
[FreeBSD/FreeBSD.git] / tests / sys / sys / rb_test.c
1 /*      $OpenBSD: rb-test.c,v 1.4 2008/04/13 00:22:17 djm Exp $ */
2 /*
3  * Copyright 2002 Niels Provos <provos@citi.umich.edu>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
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.
16  *
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.
27  */
28 #include <sys/types.h>
29
30 #define _RB_DIAGNOSTIC 1
31 #include <sys/tree.h>
32 #include <stdlib.h>
33
34 #include <atf-c.h>
35
36 struct node {
37         RB_ENTRY(node) node;
38         int key;
39 };
40
41 static RB_HEAD(tree, node) root;
42
43 static int
44 compare(struct node *a, struct node *b)
45 {
46         if (a->key < b->key) return (-1);
47         else if (a->key > b->key) return (1);
48         return (0);
49 }
50
51 RB_PROTOTYPE(tree, node, node, compare);
52
53 RB_GENERATE(tree, node, node, compare);
54
55 #define ITER 150
56
57 ATF_TC_WITHOUT_HEAD(rb_test);
58 ATF_TC_BODY(rb_test, tc)
59 {
60         struct node *tmp, *ins, store[ITER];
61         int i, j, k, max, min;
62
63         min = ITER;
64         max = -1;
65
66         RB_INIT(&root);
67
68         /* Initialize keys */
69         for (i = 0; i < ITER; i++)
70                 store[i].key = i;
71
72         /* Randomly shuffle keys */
73         for (i = 0; i < ITER; i++) {
74                 j = i + arc4random_uniform(ITER - i);
75                 k = store[j].key;
76                 store[j].key = store[i].key;
77                 store[i].key = k;
78         }
79
80         for (i = 0; i < ITER; i++) {
81                 for (j = 0; j < i; ++j) {
82                         tmp = &store[j];
83                         ATF_REQUIRE_EQ(tmp, RB_FIND(tree, &root, tmp));
84                 }
85                 tmp = &store[i];
86                 if (tmp->key > max)
87                         max = tmp->key;
88                 if (tmp->key < min)
89                         min = tmp->key;
90                 ATF_REQUIRE_EQ(NULL, RB_INSERT(tree, &root, tmp));
91                 ins = RB_MIN(tree, &root);
92                 ATF_REQUIRE_MSG(ins != NULL, "RB_MIN error");
93                 ATF_CHECK_EQ(min, ins->key);
94                 ins = RB_MAX(tree, &root);
95                 ATF_REQUIRE_MSG(ins != NULL, "RB_MAX error");
96                 ATF_CHECK_EQ(max, ins->key);
97         }
98         tmp = RB_ROOT(&root);
99         ATF_REQUIRE_MSG(tree_RB_RANK(tmp) >= 0, "RB rank balance error");
100         for (i = 0; i < ITER; i++) {
101                 tmp = RB_ROOT(&root);
102                 ATF_REQUIRE_MSG(tmp != NULL, "RB_ROOT error");
103                 ATF_CHECK_EQ(tmp, RB_REMOVE(tree, &root, tmp));
104         }
105 }
106
107 ATF_TP_ADD_TCS(tp)
108 {
109
110         ATF_TP_ADD_TC(tp, rb_test);
111
112         return (atf_no_error());
113 }