]> git.ipfire.org Git - thirdparty/squid.git/blame - test-suite/splay.cc
Summary: Update TODO.
[thirdparty/squid.git] / test-suite / splay.cc
CommitLineData
b67e2c8c 1/*
29b17d63 2 * $Id: splay.cc,v 1.2 2003/02/08 01:45:51 robertc Exp $
b67e2c8c 3 *
4 * based on ftp://ftp.cs.cmu.edu/user/sleator/splaying/top-down-splay.c
5 * http://bobo.link.cs.cmu.edu/cgi-bin/splay/splay-cgi.pl
6 */
7
8#include "config.h"
9
10#if HAVE_STDIO_H
11#include <stdio.h>
12#endif
13#if HAVE_STDLIB_H
14#include <stdlib.h>
15#endif
16#if HAVE_UNISTD_H
17#include <unistd.h>
18#endif
19
20#include "splay.h"
21#include "util.h"
22
23typedef struct {
24 int i;
25} intnode;
26
27int
29b17d63 28compareintvoid(void * const &a, void * const &n)
b67e2c8c 29{
30 intnode *A = (intnode *)a;
31 intnode *B = (intnode *)n;
b67e2c8c 32 return A->i - B->i;
33}
34
29b17d63 35int
36compareint(intnode * const &a, intnode * const &b)
37{
38 return a->i - b->i;
39}
40
b67e2c8c 41void
29b17d63 42printintvoid(void * const &a, void *state)
b67e2c8c 43{
44 intnode *A = (intnode *)a;
45 printf("%d\n", A->i);
46}
47
29b17d63 48void
49printint (intnode * const &a, void *state)
50{
51 printf("%d\n",a->i);
52}
53
54void
55destintvoid(void * &data)
56{
57 intnode *i = (intnode *)data;
58 xfree (i);
59}
60
61void
62destint(intnode * &data)
63{
64 delete data;
65}
66
67int
68compareintref(intnode const &a, intnode const &b)
69{
70 return a.i - b.i;
71}
72
73void
74printintref (intnode const &a, void *unused)
75{
76 printf("%d\n",a.i);
77}
78
79void
80destintref (intnode &)
81{
82}
83
b67e2c8c 84int
85main(int argc, char *argv[])
86{
87 int i;
88 intnode *I;
29b17d63 89 /* test void * splay containers */
b67e2c8c 90 splayNode *top = NULL;
91 srandom(time(NULL));
92 for (i = 0; i < 100; i++) {
93 I = (intnode *)xcalloc(sizeof(intnode), 1);
94 I->i = random();
29b17d63 95 top = splay_insert(I, top, compareintvoid);
96 }
97 splay_walk(top, printintvoid, NULL);
98
99 top->walk(printintvoid, NULL);
100 top->destroy(destintvoid);
101 /* check we don't segfault on NULL splay calls */
102 top = NULL;
103 top->splay(NULL, compareintvoid);
104
105 /* test typesafe splay containers */
106 {
107 /* intnode* */
108 SplayNode<intnode *> *safeTop = NULL;
109 for (i = 0; i < 100; i++) {
110 I = new intnode;
111 I->i = random();
112 safeTop = safeTop->insert(I, compareint);
113 }
114 safeTop->walk(printint, NULL);
115
116 safeTop->destroy(destint);
117 /* check we don't segfault on NULL splay calls */
118 safeTop = NULL;
119 safeTop->splay(NULL, compareint);
120 }
121 {
122 /* intnode */
123 SplayNode<intnode> *safeTop = NULL;
124 for (i = 0; i < 100; i++) {
125 intnode I;
126 I.i = random();
127 safeTop = safeTop->insert(I, compareintref);
b67e2c8c 128 }
29b17d63 129 safeTop->walk(printintref, NULL);
130
131 safeTop->destroy(destintref);
132 /* check we don't segfault on NULL splay calls */
133 safeTop = NULL;
134 safeTop->splay(intnode(), compareintref);
135 safeTop->walk(printintref, NULL);
136}
b67e2c8c 137 return 0;
138}