]>
Commit | Line | Data |
---|---|---|
b67e2c8c | 1 | /* |
4c50505b | 2 | * $Id: splay.cc,v 1.4 2003/06/24 12:31:00 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 | ||
23 | typedef struct { | |
24 | int i; | |
25 | } intnode; | |
26 | ||
27 | int | |
29b17d63 | 28 | compareintvoid(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 | 35 | int |
36 | compareint(intnode * const &a, intnode * const &b) | |
37 | { | |
38 | return a->i - b->i; | |
39 | } | |
40 | ||
23087cb8 | 41 | class SplayCheck |
42 | { | |
43 | public: | |
44 | static void BeginWalk(); | |
45 | static int LastValue; | |
46 | static bool ExpectedFail; | |
47 | static void WalkVoid(void *const &, void *); | |
48 | static void WalkNode(intnode *const &, void *); | |
49 | static void WalkNodeRef(intnode const &, void *); | |
50 | static void CheckNode(intnode const &); | |
51 | }; | |
52 | ||
53 | int SplayCheck::LastValue (0); | |
54 | bool SplayCheck::ExpectedFail (false); | |
55 | ||
b67e2c8c | 56 | void |
23087cb8 | 57 | SplayCheck::BeginWalk() |
b67e2c8c | 58 | { |
23087cb8 | 59 | LastValue = 0; |
b67e2c8c | 60 | } |
61 | ||
29b17d63 | 62 | void |
23087cb8 | 63 | SplayCheck::WalkVoid(void *const &node, void *state) |
29b17d63 | 64 | { |
23087cb8 | 65 | intnode *A = (intnode *)node; |
66 | CheckNode(*A); | |
67 | } | |
68 | ||
69 | void | |
70 | SplayCheck::CheckNode(intnode const &A) | |
71 | { | |
72 | if (LastValue > A.i) { | |
73 | /* failure */ | |
74 | if (!ExpectedFail) | |
75 | exit (1); | |
76 | } else | |
77 | /* success */ | |
78 | if (ExpectedFail) | |
79 | exit (1); | |
80 | LastValue = A.i; | |
81 | } | |
82 | ||
83 | void | |
84 | SplayCheck::WalkNode (intnode *const &a, void *state) | |
85 | { | |
86 | CheckNode (*a); | |
87 | } | |
88 | ||
89 | void | |
90 | SplayCheck::WalkNodeRef (intnode const &a, void *state) | |
91 | { | |
92 | CheckNode (a); | |
29b17d63 | 93 | } |
94 | ||
95 | void | |
96 | destintvoid(void * &data) | |
97 | { | |
98 | intnode *i = (intnode *)data; | |
99 | xfree (i); | |
100 | } | |
101 | ||
102 | void | |
103 | destint(intnode * &data) | |
104 | { | |
105 | delete data; | |
106 | } | |
107 | ||
108 | int | |
109 | compareintref(intnode const &a, intnode const &b) | |
110 | { | |
111 | return a.i - b.i; | |
112 | } | |
113 | ||
29b17d63 | 114 | void |
115 | destintref (intnode &) | |
116 | { | |
117 | } | |
118 | ||
b67e2c8c | 119 | int |
120 | main(int argc, char *argv[]) | |
121 | { | |
23087cb8 | 122 | { |
b67e2c8c | 123 | int i; |
124 | intnode *I; | |
29b17d63 | 125 | /* test void * splay containers */ |
b67e2c8c | 126 | splayNode *top = NULL; |
127 | srandom(time(NULL)); | |
128 | for (i = 0; i < 100; i++) { | |
129 | I = (intnode *)xcalloc(sizeof(intnode), 1); | |
130 | I->i = random(); | |
29b17d63 | 131 | top = splay_insert(I, top, compareintvoid); |
132 | } | |
23087cb8 | 133 | SplayCheck::BeginWalk(); |
134 | splay_walk(top, SplayCheck::WalkVoid, NULL); | |
29b17d63 | 135 | |
23087cb8 | 136 | SplayCheck::BeginWalk(); |
137 | top->walk(SplayCheck::WalkVoid, NULL); | |
29b17d63 | 138 | top->destroy(destintvoid); |
139 | /* check we don't segfault on NULL splay calls */ | |
140 | top = NULL; | |
141 | top->splay(NULL, compareintvoid); | |
23087cb8 | 142 | } |
29b17d63 | 143 | /* test typesafe splay containers */ |
144 | { | |
145 | /* intnode* */ | |
146 | SplayNode<intnode *> *safeTop = NULL; | |
23087cb8 | 147 | for ( int i = 0; i < 100; i++) { |
148 | intnode *I; | |
29b17d63 | 149 | I = new intnode; |
150 | I->i = random(); | |
151 | safeTop = safeTop->insert(I, compareint); | |
152 | } | |
23087cb8 | 153 | SplayCheck::BeginWalk(); |
154 | safeTop->walk(SplayCheck::WalkNode, NULL); | |
29b17d63 | 155 | |
156 | safeTop->destroy(destint); | |
157 | /* check we don't segfault on NULL splay calls */ | |
158 | safeTop = NULL; | |
159 | safeTop->splay(NULL, compareint); | |
160 | } | |
161 | { | |
162 | /* intnode */ | |
163 | SplayNode<intnode> *safeTop = NULL; | |
23087cb8 | 164 | for (int i = 0; i < 100; i++) { |
29b17d63 | 165 | intnode I; |
166 | I.i = random(); | |
167 | safeTop = safeTop->insert(I, compareintref); | |
b67e2c8c | 168 | } |
23087cb8 | 169 | SplayCheck::BeginWalk(); |
170 | safeTop->walk(SplayCheck::WalkNodeRef, NULL); | |
29b17d63 | 171 | |
172 | safeTop->destroy(destintref); | |
173 | /* check we don't segfault on NULL splay calls */ | |
174 | safeTop = NULL; | |
175 | safeTop->splay(intnode(), compareintref); | |
23087cb8 | 176 | SplayCheck::BeginWalk(); |
177 | safeTop->walk(SplayCheck::WalkNodeRef, NULL); | |
29b17d63 | 178 | } |
23087cb8 | 179 | /* check the check routine */ |
180 | SplayCheck::BeginWalk(); | |
181 | intnode I; | |
182 | I.i = 1; | |
4c50505b | 183 | /* check we don't segfault on NULL splay calls */ |
23087cb8 | 184 | SplayCheck::WalkNodeRef(I, NULL); |
185 | I.i = 0; | |
186 | SplayCheck::ExpectedFail = true; | |
187 | SplayCheck::WalkNodeRef(I, NULL); | |
4c50505b | 188 | |
189 | { | |
190 | /* check for begin() */ | |
191 | SplayNode<intnode> *safeTop = NULL; | |
192 | if (safeTop->start() != NULL) | |
193 | exit (1); | |
194 | for (int i = 0; i < 100; i++) { | |
195 | intnode I; | |
196 | I.i = random(); | |
197 | if (i > 50) | |
198 | safeTop = safeTop->insert(I, compareintref); | |
199 | } | |
200 | { | |
201 | intnode I; | |
202 | I.i = 50; | |
203 | safeTop = safeTop->insert (I, compareintref); | |
204 | } | |
205 | if (!safeTop->start()) | |
206 | exit (1); | |
207 | if (safeTop->start()->data.i != 50) | |
208 | exit (1); | |
209 | safeTop->destroy(destintref); | |
210 | } | |
211 | { | |
212 | Splay<intnode *> aSplay; | |
213 | if (aSplay.start() != NULL) | |
214 | exit (1); | |
215 | } | |
b67e2c8c | 216 | return 0; |
217 | } |