xref: /freebsd/tests/sys/sys/arb_test.c (revision c697fb7f)
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  * $FreeBSD$
30  */
31 #include <sys/types.h>
32 
33 #include <sys/arb.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 
37 #include <atf-c.h>
38 
39 struct node {
40 	ARB32_ENTRY() next;
41 	int key;
42 };
43 
44 ARB32_HEAD(tree, node) *root;
45 
46 static int
47 compare(const struct node *a, const struct node *b)
48 {
49 	if (a->key < b->key) return (-1);
50 	else if (a->key > b->key) return (1);
51 	return (0);
52 }
53 
54 ARB_PROTOTYPE(tree, node, next, compare);
55 
56 ARB_GENERATE(tree, node, next, compare);
57 
58 #define ITER 150
59 #define MIN 5
60 #define MAX 5000
61 
62 ATF_TC_WITHOUT_HEAD(arb_test);
63 ATF_TC_BODY(arb_test, tc)
64 {
65 	struct node *tmp, *ins;
66 	int i, max, min;
67 
68 	max = min = 42; /* pacify gcc */
69 
70 	root = (struct tree *)calloc(1, ARB_ALLOCSIZE(root, ITER, tmp));
71 
72 	ARB_INIT(tmp, next, root, ITER);
73 
74 	for (i = 0; i < ITER; i++) {
75 		tmp = ARB_GETFREE(root, next);
76 		ATF_REQUIRE_MSG(tmp != NULL, "ARB_GETFREE failed");
77 		do {
78 			tmp->key = arc4random_uniform(MAX-MIN);
79 			tmp->key += MIN;
80 		} while (ARB_FIND(tree, root, tmp) != NULL);
81 		if (i == 0)
82 			max = min = tmp->key;
83 		else {
84 			if (tmp->key > max)
85 				max = tmp->key;
86 			if (tmp->key < min)
87 				min = tmp->key;
88 		}
89 		ATF_REQUIRE_EQ(NULL, ARB_INSERT(tree, root, tmp));
90 	}
91 
92 	ins = ARB_MIN(tree, root);
93 	ATF_REQUIRE_MSG(ins != NULL, "ARB_MIN error");
94 	ATF_CHECK_EQ(min, ins->key);
95 	tmp = ins;
96 	ins = ARB_MAX(tree, root);
97 	ATF_REQUIRE_MSG(ins != NULL, "ARB_MAX error");
98 	ATF_CHECK_EQ(max, ins->key);
99 
100 	ATF_CHECK_EQ(tmp, ARB_REMOVE(tree, root, tmp));
101 
102 	for (i = 0; i < ITER - 1; i++) {
103 		tmp = ARB_ROOT(root);
104 		ATF_REQUIRE_MSG(tmp != NULL, "ARB_ROOT error");
105 		ATF_CHECK_EQ(tmp, ARB_REMOVE(tree, root, tmp));
106 	}
107 }
108 
109 ATF_TP_ADD_TCS(tp)
110 {
111 
112 	ATF_TP_ADD_TC(tp, arb_test);
113 
114 	return (atf_no_error());
115 }
116