-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtests.lisp
355 lines (318 loc) · 11.2 KB
/
tests.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
;; Copyright (c) 2011 Phil Hargett
;; Permission is hereby granted, free of charge, to any person obtaining a copy
;; of this software and associated documentation files (the "Software"), to deal
;; in the Software without restriction, including without limitation the rights
;; to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
;; copies of the Software, and to permit persons to whom the Software is
;; furnished to do so, subject to the following conditions:
;; The above copyright notice and this permission notice shall be included in
;; all copies or substantial portions of the Software.
;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
;; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
;; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
;; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
;; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
;; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
;; THE SOFTWARE.
(defpackage :hh-redblack-tests
(:use :cl :lisp-unit :hh-redblack))
(in-package :hh-redblack-tests)
(remove-tests :all)
(define-test create-rb-rtree-tests
(let ((tree (make-red-black-tree)))
(assert-true tree)
(assert-eq :black (hh-redblack::color (hh-redblack::root tree)))
(assert-false (hh-redblack::rb-first tree))
(assert-false (hh-redblack::rb-last tree))))
(define-test create-rb-node-tests
(let ((node (make-instance 'hh-redblack::memory-red-black-node)))
(assert-true node)))
(define-test put-tests
(let ((tree (make-red-black-tree)))
(rb-put tree 1 "one")
(assert-eq :black (hh-redblack::color (hh-redblack::root tree)))
(assert-true t)))
(define-test put-get-tests
(let ((tree (make-red-black-tree)))
;; Check that something we didn't put in the tree isn't there.
(assert-false (rb-get tree 1))
(rb-put tree 1 "one")
(assert-true (string= "one" (rb-get tree 1)))
(rb-put tree 2 "two")
(assert-true (string= "one" (rb-get tree 1)))
(assert-true (string= "two" (rb-get tree 2)))))
;; Test that we can store nil as a value.
(define-test nil-test
(let ((tree (make-red-black-tree)))
(multiple-value-bind (val found) (rb-get tree 1)
(assert-false val)
(assert-false found))
(rb-put tree 1 nil)
(multiple-value-bind (val found) (rb-get tree 1)
(assert-false val)
(assert-true found))))
(define-test remove-test
(let ((tree (make-red-black-tree)))
(rb-put tree 1 "one")
(assert-true (string= "one" (rb-get tree 1)))
(rb-remove tree 1)
(assert-false (rb-get tree 1))))
;; A regression test for issue #3 on Github. Previously this would
;; cause on infinite loop. As long as this code finishes were okay.
(define-test regression-test-issue3
(let ((tree (make-red-black-tree)))
(loop for x in '(-1 -7 1 0 -4) do (rb-put tree x "data"))
(loop for x in '(1 -1 -7) do (rb-remove tree x))))
;; Previously the code would allow for multiple nodes with the same
;; key.
(define-test regression-test-duplication
(let ((tree (make-red-black-tree)))
(rb-put tree 1 "data")
(rb-put tree 1 "new data")
(assert-true (string= (rb-get tree 1) "new data"))
(rb-remove tree 1)
(assert-false (rb-get tree 1))))
(define-test iteration-tests
(assert-equal `(1 2 3 4 5)
(let ((tree (make-red-black-tree))
(keys ()))
(rb-put tree 4 "four")
(rb-put tree 1 "one")
(rb-put tree 5 "five")
(rb-put tree 3 "three")
(rb-put tree 2 "two")
(with-rb-keys-and-data (key data :first) tree
(setf keys (append keys (list key))))
keys))
(assert-equal `(5 4 3 2 1)
(let ((tree (make-red-black-tree))
(keys ()))
(rb-put tree 4 "four")
(rb-put tree 1 "one")
(rb-put tree 5 "five")
(rb-put tree 3 "three")
(rb-put tree 2 "two")
(with-rb-keys-and-data (key data :last) tree
(setf keys (append keys (list key))))
keys))
(assert-equal `("one" "two" "three" "four" "five")
(let ((tree (make-red-black-tree))
(all-data ()))
(rb-put tree 4 "four")
(rb-put tree 1 "one")
(rb-put tree 5 "five")
(rb-put tree 3 "three")
(rb-put tree 2 "two")
(with-rb-keys-and-data (key data :first) tree
(setf all-data (append all-data (list data))))
all-data)))
(define-test remove-tests
(assert-equal `(1 2 4 5)
(let ((tree (make-red-black-tree))
(keys ()))
(rb-put tree 4 "four")
(rb-put tree 1 "one")
(rb-put tree 5 "five")
(rb-put tree 3 "three")
(rb-put tree 2 "two")
(rb-remove tree 3)
(with-rb-keys-and-data (key data :first) tree
(setf keys (append keys (list key))))
keys))
(let ((tree (make-red-black-tree)))
(rb-put tree 4 "four")
(rb-put tree 1 "one")
(rb-put tree 5 "five")
(rb-put tree 3 "three")
(rb-put tree 2 "two")
(assert-equal `(1 2 3 4 5)
(let ((keys ()))
(with-rb-keys-and-data (key data :first) tree
(setf keys (append keys (list key))))
keys))
(rb-remove tree 3)
(rb-remove tree 4)
(assert-equal `(1 2 5)
(let ((keys ()))
(with-rb-keys-and-data (key data :first) tree
(setf keys (append keys (list key))))
keys))
(rb-put tree 3 "three")
(rb-put tree 4 "four")
(assert-equal `(1 2 3 4 5)
(let ((keys ()))
(with-rb-keys-and-data (key data :first) tree
(setf keys (append keys (list key))))
keys))))
(defmacro with-temporary-tree ((var) &rest body)
`(let ((temp-file-name (asdf:system-relative-pathname (asdf:find-system "hh-redblack")
(format nil "text-~s.tree" (random (expt 2 32))))))
(unwind-protect
(let ((,var (make-text-file-red-black-tree temp-file-name)))
,@body)
(delete-file temp-file-name))))
(define-test peristent-red-black-tree-tests
(with-temporary-tree (tree)
(assert-true tree))
(with-temporary-tree (tree)
(assert-error 'requires-red-black-transaction
(rb-put tree 1 "one")))
(with-temporary-tree (tree)
(with-rb-transaction (tree)
(rb-put tree 1 "one"))
(assert-error 'requires-red-black-transaction
(rb-get tree 1)))
(with-temporary-tree (tree)
(with-rb-transaction (tree)
(rb-put tree 1 "one"))
(with-rb-transaction (tree)
(rb-put tree 2 "two"))
(with-rb-transaction (tree)
(assert-equal `(1 2) (rb-keys tree))))
(with-temporary-tree (tree)
(with-rb-transaction (tree)
(rb-put tree 1 "one"))
(with-rb-transaction (tree)
(rb-put tree 2 "two"))
(with-rb-transaction (tree)
(rb-put tree 3 "two"))
(with-rb-transaction (tree)
(rb-remove tree 2))
(with-rb-transaction (tree)
(assert-equal `(1 3) (rb-keys tree))))
(with-temporary-tree (tree)
(with-rb-transaction (tree)
(rb-put tree 4 "four")
(rb-put tree 1 "one")
(rb-put tree 5 "five")
(rb-put tree 3 "three")
(rb-put tree 2 "two")
(assert-equal `(1 2 3 4 5)
(let ((keys ()))
(with-rb-keys-and-data (key data :first) tree
(setf keys (append keys (list key))))
keys))))
(with-temporary-tree (tree)
(with-rb-transaction (tree)
(rb-put tree 4 "four")
(rb-put tree 1 "one")
(rb-put tree 5 "five")
(rb-put tree 3 "three")
(rb-put tree 2 "two")
(assert-equal `(1 2 3 4 5)
(let ((keys ()))
(with-rb-keys-and-data (key data :first) tree
(setf keys (append keys (list key))))
keys)))
(with-rb-transaction (tree)
(rb-remove tree 3)
(assert-equal `(1 2 4 5)
(let ((keys ()))
(with-rb-keys-and-data (key data :first) tree
(setf keys (append keys (list key))))
keys)))
(with-rb-transaction (tree)
(rb-remove tree 4)
(assert-equal `(1 2 5)
(let ((keys ()))
(with-rb-keys-and-data (key data :first) tree
(setf keys (append keys (list key))))
keys)))
(assert-equal `("one" "two" "three" "four" "five")
(with-temporary-tree (tree)
(with-rb-transaction (tree)
(let ((all-data ()))
(rb-put tree 4 "four")
(rb-put tree 1 "one")
(rb-put tree 5 "five")
(rb-put tree 3 "three")
(rb-put tree 2 "two")
(with-rb-keys-and-data (key data :first) tree
(setf all-data (append all-data (list (hh-redblack::contents data)))))
all-data))))
(assert-equal `("one" "two" "three" "four" "five")
(with-temporary-tree (tree)
(with-rb-transaction (tree)
(rb-put tree 4 "four")
(rb-put tree 1 "one")
(rb-put tree 5 "five")
(rb-put tree 3 "three")
(rb-put tree 2 "two")
(loop for key in (rb-keys tree)
collect (rb-get tree key)))))
(let ((temp-file-name (asdf:system-relative-pathname (asdf:find-system "hh-redblack")
(format nil "text-~s.tree" (random (expt 2 32))))))
(unwind-protect
(progn
(let ((tree (make-text-file-red-black-tree temp-file-name)))
(with-rb-transaction (tree)
(rb-put tree 1 "one"))
(with-rb-transaction (tree)
(assert-equal `(1) (rb-keys tree))))
(let ((tree (make-text-file-red-black-tree temp-file-name)))
(with-rb-transaction (tree)
(rb-put tree 2 "two"))
(with-rb-transaction (tree)
(assert-equal `(1 2) (rb-keys tree))))
(let ((tree (make-text-file-red-black-tree temp-file-name)))
(with-rb-transaction (tree)
(assert-equal `(1 2) (rb-keys tree)))))
(delete-file temp-file-name)))
(with-rb-transaction (tree)
(rb-put tree 3 "three")
(rb-put tree 4 "four")
(assert-equal `(1 2 3 4 5)
(let ((keys ()))
(with-rb-keys-and-data (key data :first) tree
(setf keys (append keys (list key))))
keys)))))
(define-test larger-tree-tests
(let ((tree (make-red-black-tree)))
(rb-put tree 5 "five")
(rb-put tree 3 "three")
(rb-put tree 6 "six")
(rb-put tree 1 "one")
(rb-put tree 8 "eight")
(rb-put tree 2 "two")
(rb-put tree 9 "nine")
(rb-put tree 7 "seven")
(rb-put tree 10 "ten")
(rb-put tree 4 "four")
(rb-remove tree 3)
(assert-equal `(1 2 4 5 6 7 8 9 10)
(rb-keys tree)))
(let ((tree (make-memory-persistent-red-black-tree)))
(with-rb-transaction (tree)
(rb-put tree 5 "five")
(rb-put tree 3 "three")
(rb-put tree 6 "six")
(rb-put tree 1 "one")
(rb-put tree 8 "eight")
(rb-put tree 2 "two")
(rb-put tree 9 "nine")
(rb-put tree 7 "seven")
(rb-put tree 10 "ten")
(rb-put tree 4 "four"))
(with-rb-transaction (tree)
(rb-remove tree 3))
(assert-equal `(1 2 4 5 6 7 8 9 10)
(with-rb-transaction (tree)
(rb-keys tree))))
(with-temporary-tree (tree)
(with-rb-transaction (tree)
(rb-put tree 5 "five")
(rb-put tree 3 "three")
(rb-put tree 6 "six")
(rb-put tree 1 "one")
(rb-put tree 8 "eight")
(rb-put tree 2 "two")
(rb-put tree 9 "nine")
(rb-put tree 7 "seven")
(rb-put tree 10 "ten")
(rb-put tree 4 "four"))
(with-rb-transaction (tree)
(rb-remove tree 3))
(assert-equal `(1 2 4 5 6 7 8 9 10)
(with-rb-transaction (tree)
(rb-keys tree)))))
(run-tests :all)