]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - tests/sys/sys/arb_test.c
ice(4): Update copyright year to 2024
[FreeBSD/FreeBSD.git] / tests / sys / sys / arb_test.c
1 /*      $OpenBSD: rb-test.c,v 1.4 2008/04/13 00:22:17 djm Exp $ */
2 /*
3  * Copyright 2019 Edward Tomasz Napierala <trasz@FreeBSD.org>
4  * Copyright 2002 Niels Provos <provos@citi.umich.edu>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
17  *
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.
28  */
29 #include <sys/types.h>
30
31 #include <sys/arb.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34
35 #include <atf-c.h>
36
37 struct node {
38         ARB32_ENTRY() next;
39         int key;
40 };
41
42 static ARB32_HEAD(tree, node) *root;
43
44 static int
45 compare(const struct node *a, const struct node *b)
46 {
47         if (a->key < b->key) return (-1);
48         else if (a->key > b->key) return (1);
49         return (0);
50 }
51
52 ARB_PROTOTYPE(tree, node, next, compare);
53
54 ARB_GENERATE(tree, node, next, compare);
55
56 #define ITER 150
57 #define MIN 5
58 #define MAX 5000
59
60 ATF_TC_WITHOUT_HEAD(arb_test);
61 ATF_TC_BODY(arb_test, tc)
62 {
63         struct node *tmp, *ins;
64         int i, max, min;
65
66         max = min = 42; /* pacify gcc */
67
68         root = (struct tree *)calloc(1, ARB_ALLOCSIZE(root, ITER, tmp));
69
70         ARB_INIT(tmp, next, root, ITER);
71
72         for (i = 0; i < ITER; i++) {
73                 tmp = ARB_GETFREE(root, next);
74                 ATF_REQUIRE_MSG(tmp != NULL, "ARB_GETFREE failed");
75                 do {
76                         tmp->key = arc4random_uniform(MAX-MIN);
77                         tmp->key += MIN;
78                 } while (ARB_FIND(tree, root, tmp) != NULL);
79                 if (i == 0)
80                         max = min = tmp->key;
81                 else {
82                         if (tmp->key > max)
83                                 max = tmp->key;
84                         if (tmp->key < min)
85                                 min = tmp->key;
86                 }
87                 ATF_REQUIRE_EQ(NULL, ARB_INSERT(tree, root, tmp));
88         }
89
90         ins = ARB_MIN(tree, root);
91         ATF_REQUIRE_MSG(ins != NULL, "ARB_MIN error");
92         ATF_CHECK_EQ(min, ins->key);
93         tmp = ins;
94         ins = ARB_MAX(tree, root);
95         ATF_REQUIRE_MSG(ins != NULL, "ARB_MAX error");
96         ATF_CHECK_EQ(max, ins->key);
97
98         ATF_CHECK_EQ(tmp, ARB_REMOVE(tree, root, tmp));
99
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));
104         }
105 }
106
107 ATF_TP_ADD_TCS(tp)
108 {
109
110         ATF_TP_ADD_TC(tp, arb_test);
111
112         return (atf_no_error());
113 }