1*57718be8SEnji Cooper /* $NetBSD: t_tree.c,v 1.1 2011/05/05 13:36:05 jruoho Exp $ */
2*57718be8SEnji Cooper
3*57718be8SEnji Cooper /*-
4*57718be8SEnji Cooper * Copyright (c) 2010, 2011 The NetBSD Foundation, Inc.
5*57718be8SEnji Cooper * All rights reserved.
6*57718be8SEnji Cooper *
7*57718be8SEnji Cooper * Redistribution and use in source and binary forms, with or without
8*57718be8SEnji Cooper * modification, are permitted provided that the following conditions
9*57718be8SEnji Cooper * are met:
10*57718be8SEnji Cooper * 1. Redistributions of source code must retain the above copyright
11*57718be8SEnji Cooper * notice, this list of conditions and the following disclaimer.
12*57718be8SEnji Cooper * 2. Redistributions in binary form must reproduce the above copyright
13*57718be8SEnji Cooper * notice, this list of conditions and the following disclaimer in the
14*57718be8SEnji Cooper * documentation and/or other materials provided with the distribution.
15*57718be8SEnji Cooper *
16*57718be8SEnji Cooper * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17*57718be8SEnji Cooper * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18*57718be8SEnji Cooper * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19*57718be8SEnji Cooper * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20*57718be8SEnji Cooper * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21*57718be8SEnji Cooper * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22*57718be8SEnji Cooper * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23*57718be8SEnji Cooper * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24*57718be8SEnji Cooper * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25*57718be8SEnji Cooper * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26*57718be8SEnji Cooper * POSSIBILITY OF SUCH DAMAGE.
27*57718be8SEnji Cooper */
28*57718be8SEnji Cooper
29*57718be8SEnji Cooper #include <sys/cdefs.h>
30*57718be8SEnji Cooper #include <sys/tree.h>
31*57718be8SEnji Cooper
32*57718be8SEnji Cooper #include <atf-c.h>
33*57718be8SEnji Cooper #include <stdlib.h>
34*57718be8SEnji Cooper #include <stdio.h>
35*57718be8SEnji Cooper
36*57718be8SEnji Cooper struct mist {
37*57718be8SEnji Cooper RB_ENTRY(mist) rbentry;
38*57718be8SEnji Cooper int key;
39*57718be8SEnji Cooper };
40*57718be8SEnji Cooper RB_HEAD(head, mist) tt;
41*57718be8SEnji Cooper
42*57718be8SEnji Cooper static int
mistcmp(struct mist * a,struct mist * b)43*57718be8SEnji Cooper mistcmp(struct mist *a, struct mist *b)
44*57718be8SEnji Cooper {
45*57718be8SEnji Cooper #if 0
46*57718be8SEnji Cooper return (b->key - a->key); /* wrong, can overflow */
47*57718be8SEnji Cooper #else
48*57718be8SEnji Cooper if (b->key > a->key)
49*57718be8SEnji Cooper return 1;
50*57718be8SEnji Cooper else if (b->key < a->key)
51*57718be8SEnji Cooper return (-1);
52*57718be8SEnji Cooper else
53*57718be8SEnji Cooper return 0;
54*57718be8SEnji Cooper #endif
55*57718be8SEnji Cooper }
56*57718be8SEnji Cooper
RB_PROTOTYPE(head,mist,rbentry,mistcmp)57*57718be8SEnji Cooper RB_PROTOTYPE(head, mist, rbentry, mistcmp)
58*57718be8SEnji Cooper RB_GENERATE(head, mist, rbentry, mistcmp)
59*57718be8SEnji Cooper
60*57718be8SEnji Cooper static struct mist *
61*57718be8SEnji Cooper addmist(int key)
62*57718be8SEnji Cooper {
63*57718be8SEnji Cooper struct mist *m;
64*57718be8SEnji Cooper
65*57718be8SEnji Cooper m = malloc(sizeof(struct mist));
66*57718be8SEnji Cooper m->key = key;
67*57718be8SEnji Cooper RB_INSERT(head, &tt, m);
68*57718be8SEnji Cooper return m;
69*57718be8SEnji Cooper }
70*57718be8SEnji Cooper
71*57718be8SEnji Cooper static int
findmist(struct mist * m)72*57718be8SEnji Cooper findmist(struct mist *m)
73*57718be8SEnji Cooper {
74*57718be8SEnji Cooper
75*57718be8SEnji Cooper return (!!RB_FIND(head, &tt, m));
76*57718be8SEnji Cooper }
77*57718be8SEnji Cooper
78*57718be8SEnji Cooper #define N 1000
79*57718be8SEnji Cooper static int
test(void)80*57718be8SEnji Cooper test(void)
81*57718be8SEnji Cooper {
82*57718be8SEnji Cooper struct mist *m[N];
83*57718be8SEnji Cooper int fail, i, j;
84*57718be8SEnji Cooper
85*57718be8SEnji Cooper RB_INIT(&tt);
86*57718be8SEnji Cooper fail = 0;
87*57718be8SEnji Cooper for (i = 0; i < N; i++) {
88*57718be8SEnji Cooper m[i] = addmist(random() << 1); /* use all 32 bits */
89*57718be8SEnji Cooper for (j = 0; j <= i; j++)
90*57718be8SEnji Cooper if (!findmist(m[j]))
91*57718be8SEnji Cooper fail++;
92*57718be8SEnji Cooper }
93*57718be8SEnji Cooper return fail;
94*57718be8SEnji Cooper }
95*57718be8SEnji Cooper
96*57718be8SEnji Cooper ATF_TC(tree_rbstress);
ATF_TC_HEAD(tree_rbstress,tc)97*57718be8SEnji Cooper ATF_TC_HEAD(tree_rbstress, tc)
98*57718be8SEnji Cooper {
99*57718be8SEnji Cooper
100*57718be8SEnji Cooper atf_tc_set_md_var(tc, "descr", "rb-tree stress test");
101*57718be8SEnji Cooper }
102*57718be8SEnji Cooper
ATF_TC_BODY(tree_rbstress,tc)103*57718be8SEnji Cooper ATF_TC_BODY(tree_rbstress, tc)
104*57718be8SEnji Cooper {
105*57718be8SEnji Cooper int i, fail, f;
106*57718be8SEnji Cooper
107*57718be8SEnji Cooper srandom(4711);
108*57718be8SEnji Cooper fail = 0;
109*57718be8SEnji Cooper for (i = 0; i < 10; i++) {
110*57718be8SEnji Cooper f = test();
111*57718be8SEnji Cooper if (f) {
112*57718be8SEnji Cooper atf_tc_fail_nonfatal("loop %d: %d errors\n", i, f);
113*57718be8SEnji Cooper fail += f;
114*57718be8SEnji Cooper }
115*57718be8SEnji Cooper }
116*57718be8SEnji Cooper }
117*57718be8SEnji Cooper
ATF_TP_ADD_TCS(tp)118*57718be8SEnji Cooper ATF_TP_ADD_TCS(tp)
119*57718be8SEnji Cooper {
120*57718be8SEnji Cooper
121*57718be8SEnji Cooper ATF_TP_ADD_TC(tp, tree_rbstress);
122*57718be8SEnji Cooper
123*57718be8SEnji Cooper return atf_no_error();
124*57718be8SEnji Cooper }
125