]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - tests/sys/sys/splay_test.c
MFV: less v632.
[FreeBSD/FreeBSD.git] / tests / sys / sys / splay_test.c
1 /*      $OpenBSD: splay-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  * $FreeBSD$
29  */
30 #include <sys/types.h>
31
32 #include <sys/tree.h>
33 #include <stdlib.h>
34
35 #include <atf-c.h>
36
37 struct node {
38         SPLAY_ENTRY(node) node;
39         int key;
40 };
41
42 static SPLAY_HEAD(tree, node) root;
43
44 static int
45 compare(struct node *a, 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 SPLAY_PROTOTYPE(tree, node, node, compare);
53
54 SPLAY_GENERATE(tree, node, node, compare);
55
56 #define ITER 150
57 #define MIN 5
58 #define MAX 5000
59
60 ATF_TC_WITHOUT_HEAD(splay_test);
61 ATF_TC_BODY(splay_test, tc)
62 {
63         struct node *tmp, *ins;
64         int i, max, min;
65
66         SPLAY_INIT(&root);
67
68         max = min = 42; /* pacify gcc */
69
70         for (i = 0; i < ITER; i++) {
71                 tmp = malloc(sizeof(struct node));
72                 ATF_REQUIRE_MSG(tmp != NULL, "malloc failed");
73                 do {
74                         tmp->key = arc4random_uniform(MAX-MIN);
75                         tmp->key += MIN;
76                 } while (SPLAY_FIND(tree, &root, tmp) != NULL);
77                 if (i == 0)
78                         max = min = tmp->key;
79                 else {
80                         if (tmp->key > max)
81                                 max = tmp->key;
82                         if (tmp->key < min)
83                                 min = tmp->key;
84                 }
85                 ATF_REQUIRE_EQ(NULL, SPLAY_INSERT(tree, &root, tmp));
86         }
87
88         ins = SPLAY_MIN(tree, &root);
89         ATF_REQUIRE_MSG(ins != NULL, "SPLAY_MIN error");
90         ATF_CHECK_EQ(min, ins->key);
91         tmp = ins;
92         ins = SPLAY_MAX(tree, &root);
93         ATF_REQUIRE_MSG(ins != NULL, "SPLAY_MAX error");
94         ATF_CHECK_EQ(max, ins->key);
95
96         ATF_CHECK_EQ(tmp, SPLAY_REMOVE(tree, &root, tmp));
97
98         for (i = 0; i < ITER - 1; i++) {
99                 tmp = SPLAY_ROOT(&root);
100                 ATF_REQUIRE_MSG(tmp != NULL, "SPLAY_ROOT error");
101                 ATF_CHECK_EQ(tmp, SPLAY_REMOVE(tree, &root, tmp));
102                 free(tmp);
103         }
104 }
105
106 ATF_TP_ADD_TCS(tp)
107 {
108
109         ATF_TP_ADD_TC(tp, splay_test);
110
111         return (atf_no_error());
112 }