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#include <sys/types.h>
29#include <sys/tree.h>
30#include <unistd.h>
31#include <stdio.h>
32#include <err.h>
33#include <stdlib.h>
34
35struct node {
36	SPLAY_ENTRY(node) node;
37	int key;
38};
39
40SPLAY_HEAD(tree, node) root;
41
42static int
43compare(struct node *a, struct node *b)
44{
45	if (a->key < b->key) return (-1);
46	else if (a->key > b->key) return (1);
47	return (0);
48}
49
50SPLAY_PROTOTYPE(tree, node, node, compare);
51
52SPLAY_GENERATE(tree, node, node, compare);
53
54#define ITER 150
55#define MIN 5
56#define MAX 5000
57
58int
59main(int argc, char **argv)
60{
61	struct node *tmp, *ins;
62	int i, max, min;
63
64	SPLAY_INIT(&root);
65
66	for (i = 0; i < ITER; i++) {
67		tmp = malloc(sizeof(struct node));
68		if (tmp == NULL) err(1, "malloc");
69		do {
70			tmp->key = arc4random_uniform(MAX-MIN);
71			tmp->key += MIN;
72		} while (SPLAY_FIND(tree, &root, tmp) != NULL);
73		if (i == 0)
74			max = min = tmp->key;
75		else {
76			if (tmp->key > max)
77				max = tmp->key;
78			if (tmp->key < min)
79				min = tmp->key;
80		}
81		if (SPLAY_INSERT(tree, &root, tmp) != NULL)
82			errx(1, "SPLAY_INSERT failed");
83	}
84
85	ins = SPLAY_MIN(tree, &root);
86	if (ins->key != min)
87		errx(1, "min does not match");
88	tmp = ins;
89	ins = SPLAY_MAX(tree, &root);
90	if (ins->key != max)
91		errx(1, "max does not match");
92
93	if (SPLAY_REMOVE(tree, &root, tmp) != tmp)
94		errx(1, "SPLAY_REMOVE failed");
95
96	for (i = 0; i < ITER - 1; i++) {
97		tmp = SPLAY_ROOT(&root);
98		if (tmp == NULL)
99			errx(1, "SPLAY_ROOT error");
100		if (SPLAY_REMOVE(tree, &root, tmp) != tmp)
101			errx(1, "SPLAY_REMOVE error");
102		free(tmp);
103	}
104
105	exit(0);
106}
107