rbtree 0.3.0 → 0.4.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE +1 -1
- data/MANIFEST +0 -1
- data/README +38 -71
- data/dict.c +364 -83
- data/dict.h +30 -6
- data/extconf.rb +13 -2
- data/rbtree.c +772 -482
- data/test.rb +309 -182
- metadata +42 -45
- data/ChangeLog +0 -505
data/test.rb
CHANGED
@@ -1,4 +1,9 @@
|
|
1
|
-
|
1
|
+
begin
|
2
|
+
require "./rbtree"
|
3
|
+
rescue LoadError
|
4
|
+
require "rubygems"
|
5
|
+
require "rbtree"
|
6
|
+
end
|
2
7
|
require "test/unit.rb"
|
3
8
|
|
4
9
|
class RBTreeTest < Test::Unit::TestCase
|
@@ -6,6 +11,10 @@ class RBTreeTest < Test::Unit::TestCase
|
|
6
11
|
@rbtree = RBTree[*%w(b B d D a A c C)]
|
7
12
|
end
|
8
13
|
|
14
|
+
def have_enumerator?
|
15
|
+
defined?(Enumerable::Enumerator) or defined?(Enumerator)
|
16
|
+
end
|
17
|
+
|
9
18
|
def test_new
|
10
19
|
assert_nothing_raised {
|
11
20
|
RBTree.new
|
@@ -14,6 +23,18 @@ class RBTreeTest < Test::Unit::TestCase
|
|
14
23
|
}
|
15
24
|
assert_raises(ArgumentError) { RBTree.new("a") {} }
|
16
25
|
assert_raises(ArgumentError) { RBTree.new("a", "a") }
|
26
|
+
|
27
|
+
if RUBY_VERSION >= "1.9.2"
|
28
|
+
assert_nothing_raised {
|
29
|
+
RBTree.new(&lambda {|a, b|})
|
30
|
+
RBTree.new(&lambda {|*a|})
|
31
|
+
RBTree.new(&lambda {|a, *b|})
|
32
|
+
RBTree.new(&lambda {|a, b, *c|})
|
33
|
+
}
|
34
|
+
assert_raises(TypeError) { RBTree.new(&lambda {|a|}) }
|
35
|
+
assert_raises(TypeError) { RBTree.new(&lambda {|a, b, c|}) }
|
36
|
+
assert_raises(TypeError) { RBTree.new(&lambda {|a, b, c, *d|}) }
|
37
|
+
end
|
17
38
|
end
|
18
39
|
|
19
40
|
def test_aref
|
@@ -65,9 +86,13 @@ class RBTreeTest < Test::Unit::TestCase
|
|
65
86
|
assert_equal("C", rbtree["c"])
|
66
87
|
assert_equal("D", rbtree["d"])
|
67
88
|
|
89
|
+
# assert_raises(ArgumentError) { RBTree[["a"]] }
|
90
|
+
|
68
91
|
rbtree = RBTree[[["a"]]]
|
69
92
|
assert_equal(1, rbtree.size)
|
70
93
|
assert_equal(nil, rbtree["a"])
|
94
|
+
|
95
|
+
# assert_raises(ArgumentError) { RBTree[[["a", "A", "b", "B"]]] }
|
71
96
|
end
|
72
97
|
|
73
98
|
def test_clear
|
@@ -128,14 +153,13 @@ class RBTreeTest < Test::Unit::TestCase
|
|
128
153
|
end
|
129
154
|
|
130
155
|
def test_default
|
131
|
-
assert_equal(nil, @rbtree.default)
|
132
|
-
|
133
156
|
rbtree = RBTree.new("e")
|
134
157
|
assert_equal("e", rbtree.default)
|
135
158
|
assert_equal("e", rbtree.default("f"))
|
136
159
|
assert_raises(ArgumentError) { rbtree.default("e", "f") }
|
137
160
|
|
138
161
|
rbtree = RBTree.new {|tree, key| @rbtree[key || "c"] }
|
162
|
+
assert_equal(nil, rbtree.default)
|
139
163
|
assert_equal("C", rbtree.default(nil))
|
140
164
|
assert_equal("B", rbtree.default("b"))
|
141
165
|
end
|
@@ -144,14 +168,51 @@ class RBTreeTest < Test::Unit::TestCase
|
|
144
168
|
rbtree = RBTree.new { "e" }
|
145
169
|
rbtree.default = "f"
|
146
170
|
assert_equal("f", rbtree.default)
|
171
|
+
assert_equal(nil, rbtree.default_proc)
|
172
|
+
|
173
|
+
rbtree = RBTree.new { "e" }
|
174
|
+
rbtree.default = nil
|
175
|
+
assert_equal(nil, rbtree.default)
|
176
|
+
assert_equal(nil, rbtree.default_proc)
|
147
177
|
end
|
148
178
|
|
149
179
|
def test_default_proc
|
150
180
|
rbtree = RBTree.new("e")
|
151
181
|
assert_equal(nil, rbtree.default_proc)
|
152
182
|
|
153
|
-
rbtree = RBTree.new { "
|
154
|
-
assert_equal("
|
183
|
+
rbtree = RBTree.new { "f" }
|
184
|
+
assert_equal("f", rbtree.default_proc.call)
|
185
|
+
end
|
186
|
+
|
187
|
+
def test_set_default_proc
|
188
|
+
rbtree = RBTree.new("e")
|
189
|
+
rbtree.default_proc = Proc.new { "f" }
|
190
|
+
assert_equal(nil, rbtree.default)
|
191
|
+
assert_equal("f", rbtree.default_proc.call)
|
192
|
+
|
193
|
+
rbtree = RBTree.new("e")
|
194
|
+
rbtree.default_proc = nil
|
195
|
+
assert_equal(nil, rbtree.default)
|
196
|
+
assert_equal(nil, rbtree.default_proc)
|
197
|
+
|
198
|
+
if Symbol.method_defined?(:to_proc)
|
199
|
+
@rbtree.default_proc = :upper_bound
|
200
|
+
assert_equal(%w(d D), @rbtree["e"])
|
201
|
+
end
|
202
|
+
|
203
|
+
assert_raises(TypeError) { rbtree.default_proc = "f" }
|
204
|
+
|
205
|
+
if RUBY_VERSION >= "1.9.2"
|
206
|
+
assert_nothing_raised {
|
207
|
+
@rbtree.default_proc = lambda {|a, b|}
|
208
|
+
@rbtree.default_proc = lambda {|*a|}
|
209
|
+
@rbtree.default_proc = lambda {|a, *b|}
|
210
|
+
@rbtree.default_proc = lambda {|a, b, *c|}
|
211
|
+
}
|
212
|
+
assert_raises(TypeError) { @rbtree.default_proc = lambda {|a|} }
|
213
|
+
assert_raises(TypeError) { @rbtree.default_proc = lambda {|a, b, c|} }
|
214
|
+
assert_raises(TypeError) { @rbtree.default_proc = lambda {|a, b, c, *d|} }
|
215
|
+
end
|
155
216
|
end
|
156
217
|
|
157
218
|
def test_equal
|
@@ -185,6 +246,14 @@ class RBTreeTest < Test::Unit::TestCase
|
|
185
246
|
assert_not_equal(a, b)
|
186
247
|
b.readjust(a.cmp_proc)
|
187
248
|
assert_equal(a, b)
|
249
|
+
|
250
|
+
if RUBY_VERSION >= "1.8.7"
|
251
|
+
a = RBTree.new
|
252
|
+
a[1] = a
|
253
|
+
b = RBTree.new
|
254
|
+
b[1] = b
|
255
|
+
assert_equal(a, b)
|
256
|
+
end
|
188
257
|
end
|
189
258
|
|
190
259
|
def test_fetch
|
@@ -197,25 +266,15 @@ class RBTreeTest < Test::Unit::TestCase
|
|
197
266
|
|
198
267
|
assert_equal("E", @rbtree.fetch("e", "E"))
|
199
268
|
assert_equal("E", @rbtree.fetch("e") { "E" })
|
200
|
-
|
201
|
-
class << (stderr = "")
|
202
|
-
alias write <<
|
203
|
-
end
|
204
|
-
$stderr, stderr, $VERBOSE, verbose = stderr, $stderr, false, $VERBOSE
|
205
|
-
begin
|
206
|
-
assert_equal("E", @rbtree.fetch("e", "F") { "E" })
|
207
|
-
ensure
|
208
|
-
$stderr, stderr, $VERBOSE, verbose = stderr, $stderr, false, $VERBOSE
|
209
|
-
end
|
210
|
-
assert_match(/warning: block supersedes default value argument/, stderr)
|
269
|
+
# assert_equal("E", @rbtree.fetch("e", "F") { "E" })
|
211
270
|
|
212
271
|
assert_raises(ArgumentError) { @rbtree.fetch }
|
213
272
|
assert_raises(ArgumentError) { @rbtree.fetch("e", "E", "E") }
|
214
273
|
end
|
215
274
|
|
216
|
-
def
|
217
|
-
assert_equal("a", @rbtree.
|
218
|
-
assert_equal(nil, @rbtree.
|
275
|
+
def test_key
|
276
|
+
assert_equal("a", @rbtree.key("A"))
|
277
|
+
assert_equal(nil, @rbtree.key("E"))
|
219
278
|
end
|
220
279
|
|
221
280
|
def test_empty_p
|
@@ -225,9 +284,9 @@ class RBTreeTest < Test::Unit::TestCase
|
|
225
284
|
end
|
226
285
|
|
227
286
|
def test_each
|
228
|
-
|
229
|
-
@rbtree.each {|key, val|
|
230
|
-
assert_equal(%w(a A b B c C d D),
|
287
|
+
result = []
|
288
|
+
@rbtree.each {|key, val| result << key << val }
|
289
|
+
assert_equal(%w(a A b B c C d D), result)
|
231
290
|
|
232
291
|
assert_raises(TypeError) {
|
233
292
|
@rbtree.each { @rbtree["e"] = "E" }
|
@@ -243,41 +302,16 @@ class RBTreeTest < Test::Unit::TestCase
|
|
243
302
|
}
|
244
303
|
assert_equal(4, @rbtree.size)
|
245
304
|
|
246
|
-
if
|
305
|
+
if have_enumerator?
|
247
306
|
enumerator = @rbtree.each
|
248
|
-
assert_equal(%w(a A b B c C d D), enumerator.
|
249
|
-
end
|
250
|
-
end
|
251
|
-
|
252
|
-
def test_each_pair
|
253
|
-
ret = []
|
254
|
-
@rbtree.each_pair {|key, val| ret << key << val }
|
255
|
-
assert_equal(%w(a A b B c C d D), ret)
|
256
|
-
|
257
|
-
assert_raises(TypeError) {
|
258
|
-
@rbtree.each_pair { @rbtree["e"] = "E" }
|
259
|
-
}
|
260
|
-
assert_equal(4, @rbtree.size)
|
261
|
-
|
262
|
-
@rbtree.each_pair {
|
263
|
-
@rbtree.each_pair {}
|
264
|
-
assert_raises(TypeError) {
|
265
|
-
@rbtree["e"] = "E"
|
266
|
-
}
|
267
|
-
break
|
268
|
-
}
|
269
|
-
assert_equal(4, @rbtree.size)
|
270
|
-
|
271
|
-
if defined?(Enumerable::Enumerator)
|
272
|
-
enumerator = @rbtree.each_pair
|
273
|
-
assert_equal(%w(a A b B c C d D), enumerator.map.flatten)
|
307
|
+
assert_equal(%w(a A b B c C d D), enumerator.to_a.flatten)
|
274
308
|
end
|
275
309
|
end
|
276
310
|
|
277
311
|
def test_each_key
|
278
|
-
|
279
|
-
@rbtree.each_key {|key|
|
280
|
-
assert_equal(%w(a b c d),
|
312
|
+
result = []
|
313
|
+
@rbtree.each_key {|key| result.push(key) }
|
314
|
+
assert_equal(%w(a b c d), result)
|
281
315
|
|
282
316
|
assert_raises(TypeError) {
|
283
317
|
@rbtree.each_key { @rbtree["e"] = "E" }
|
@@ -293,16 +327,16 @@ class RBTreeTest < Test::Unit::TestCase
|
|
293
327
|
}
|
294
328
|
assert_equal(4, @rbtree.size)
|
295
329
|
|
296
|
-
if
|
330
|
+
if have_enumerator?
|
297
331
|
enumerator = @rbtree.each_key
|
298
|
-
assert_equal(%w(a b c d), enumerator.
|
332
|
+
assert_equal(%w(a b c d), enumerator.to_a.flatten)
|
299
333
|
end
|
300
334
|
end
|
301
335
|
|
302
336
|
def test_each_value
|
303
|
-
|
304
|
-
@rbtree.each_value {|val|
|
305
|
-
assert_equal(%w(A B C D),
|
337
|
+
result = []
|
338
|
+
@rbtree.each_value {|val| result.push(val) }
|
339
|
+
assert_equal(%w(A B C D), result)
|
306
340
|
|
307
341
|
assert_raises(TypeError) {
|
308
342
|
@rbtree.each_value { @rbtree["e"] = "E" }
|
@@ -318,16 +352,16 @@ class RBTreeTest < Test::Unit::TestCase
|
|
318
352
|
}
|
319
353
|
assert_equal(4, @rbtree.size)
|
320
354
|
|
321
|
-
if
|
355
|
+
if have_enumerator?
|
322
356
|
enumerator = @rbtree.each_value
|
323
|
-
assert_equal(%w(A B C D), enumerator.
|
357
|
+
assert_equal(%w(A B C D), enumerator.to_a.flatten)
|
324
358
|
end
|
325
359
|
end
|
326
360
|
|
327
361
|
def test_shift
|
328
|
-
|
362
|
+
result = @rbtree.shift
|
329
363
|
assert_equal(3, @rbtree.size)
|
330
|
-
assert_equal(
|
364
|
+
assert_equal(%w(a A), result)
|
331
365
|
assert_equal(nil, @rbtree["a"])
|
332
366
|
|
333
367
|
3.times { @rbtree.shift }
|
@@ -341,9 +375,9 @@ class RBTreeTest < Test::Unit::TestCase
|
|
341
375
|
end
|
342
376
|
|
343
377
|
def test_pop
|
344
|
-
|
378
|
+
result = @rbtree.pop
|
345
379
|
assert_equal(3, @rbtree.size)
|
346
|
-
assert_equal(
|
380
|
+
assert_equal(%w(d D), result)
|
347
381
|
assert_equal(nil, @rbtree["d"])
|
348
382
|
|
349
383
|
3.times { @rbtree.pop }
|
@@ -357,8 +391,8 @@ class RBTreeTest < Test::Unit::TestCase
|
|
357
391
|
end
|
358
392
|
|
359
393
|
def test_delete
|
360
|
-
|
361
|
-
assert_equal("C",
|
394
|
+
result = @rbtree.delete("c")
|
395
|
+
assert_equal("C", result)
|
362
396
|
assert_equal(3, @rbtree.size)
|
363
397
|
assert_equal(nil, @rbtree["c"])
|
364
398
|
|
@@ -367,7 +401,8 @@ class RBTreeTest < Test::Unit::TestCase
|
|
367
401
|
end
|
368
402
|
|
369
403
|
def test_delete_if
|
370
|
-
@rbtree.delete_if {|key, val| val == "A" || val == "B" }
|
404
|
+
result = @rbtree.delete_if {|key, val| val == "A" || val == "B" }
|
405
|
+
assert_same(@rbtree, result)
|
371
406
|
assert_equal(RBTree[*%w(c C d D)], @rbtree)
|
372
407
|
|
373
408
|
assert_raises(ArgumentError) {
|
@@ -391,58 +426,90 @@ class RBTreeTest < Test::Unit::TestCase
|
|
391
426
|
}
|
392
427
|
assert_equal(0, @rbtree.size)
|
393
428
|
|
394
|
-
if
|
429
|
+
if have_enumerator?
|
430
|
+
rbtree = RBTree[*%w(b B d D a A c C)]
|
431
|
+
rbtree.delete_if.with_index {|(key, val), i| i < 2 }
|
432
|
+
assert_equal(RBTree[*%w(c C d D)], rbtree)
|
433
|
+
end
|
434
|
+
end
|
435
|
+
|
436
|
+
def test_keep_if
|
437
|
+
result = @rbtree.keep_if {|key, val| val == "A" || val == "B" }
|
438
|
+
assert_same(@rbtree, result)
|
439
|
+
assert_equal(RBTree[*%w(a A b B)], @rbtree)
|
440
|
+
|
441
|
+
if have_enumerator?
|
395
442
|
rbtree = RBTree[*%w(b B d D a A c C)]
|
396
|
-
|
397
|
-
assert_equal([
|
443
|
+
rbtree.keep_if.with_index {|(key, val), i| i < 2 }
|
444
|
+
assert_equal(RBTree[*%w(a A b B)], rbtree)
|
398
445
|
end
|
399
446
|
end
|
400
447
|
|
401
448
|
def test_reject_bang
|
402
|
-
|
403
|
-
assert_equal(nil,
|
449
|
+
result = @rbtree.reject! { false }
|
450
|
+
assert_equal(nil, result)
|
404
451
|
assert_equal(4, @rbtree.size)
|
405
452
|
|
406
|
-
|
407
|
-
assert_same(@rbtree,
|
408
|
-
assert_equal(RBTree[*%w(c C d D)],
|
453
|
+
result = @rbtree.reject! {|key, val| val == "A" || val == "B" }
|
454
|
+
assert_same(@rbtree, result)
|
455
|
+
assert_equal(RBTree[*%w(c C d D)], result)
|
409
456
|
|
410
|
-
if
|
457
|
+
if have_enumerator?
|
411
458
|
rbtree = RBTree[*%w(b B d D a A c C)]
|
412
|
-
|
413
|
-
assert_equal([
|
459
|
+
rbtree.reject!.with_index {|(key, val), i| i < 2 }
|
460
|
+
assert_equal(RBTree[*%w(c C d D)], rbtree)
|
414
461
|
end
|
415
462
|
end
|
416
463
|
|
417
464
|
def test_reject
|
418
|
-
|
419
|
-
assert_equal(
|
465
|
+
result = @rbtree.reject { false }
|
466
|
+
assert_equal(RBTree[*%w(a A b B c C d D)], result)
|
420
467
|
assert_equal(4, @rbtree.size)
|
421
468
|
|
422
|
-
|
423
|
-
assert_equal(RBTree[*%w(c C d D)],
|
469
|
+
result = @rbtree.reject {|key, val| val == "A" || val == "B" }
|
470
|
+
assert_equal(RBTree[*%w(c C d D)], result)
|
424
471
|
assert_equal(4, @rbtree.size)
|
425
472
|
|
426
|
-
if
|
427
|
-
|
428
|
-
assert_equal([
|
473
|
+
if have_enumerator?
|
474
|
+
result = @rbtree.reject.with_index {|(key, val), i| i < 2 }
|
475
|
+
assert_equal(RBTree[*%w(c C d D)], result)
|
429
476
|
end
|
430
477
|
end
|
431
|
-
|
478
|
+
|
479
|
+
def test_select_bang
|
480
|
+
result = @rbtree.select! { true }
|
481
|
+
assert_equal(nil, result)
|
482
|
+
assert_equal(4, @rbtree.size)
|
483
|
+
|
484
|
+
result = @rbtree.select! {|key, val| val == "A" || val == "B" }
|
485
|
+
assert_same(@rbtree, result)
|
486
|
+
assert_equal(RBTree[*%w(a A b B)], result)
|
487
|
+
|
488
|
+
if have_enumerator?
|
489
|
+
rbtree = RBTree[*%w(b B d D a A c C)]
|
490
|
+
rbtree.select!.with_index {|(key, val), i| i < 2 }
|
491
|
+
assert_equal(RBTree[*%w(a A b B)], rbtree)
|
492
|
+
end
|
493
|
+
end
|
494
|
+
|
432
495
|
def test_select
|
433
|
-
|
434
|
-
assert_equal(
|
496
|
+
result = @rbtree.select { true }
|
497
|
+
assert_equal(RBTree[*%w(a A b B c C d D)], result)
|
498
|
+
assert_equal(4, @rbtree.size)
|
499
|
+
|
500
|
+
result = @rbtree.select {|key, val| val == "A" || val == "B" }
|
501
|
+
assert_equal(RBTree[*%w(a A b B)], result)
|
435
502
|
assert_raises(ArgumentError) { @rbtree.select("c") }
|
436
503
|
|
437
|
-
if
|
438
|
-
|
439
|
-
assert_equal([
|
504
|
+
if have_enumerator?
|
505
|
+
result = @rbtree.select.with_index {|(key, val), i| i < 2 }
|
506
|
+
assert_equal(RBTree[*%w(a A b B)], result)
|
440
507
|
end
|
441
508
|
end
|
442
509
|
|
443
510
|
def test_values_at
|
444
|
-
|
445
|
-
assert_equal(["D", "A", nil],
|
511
|
+
result = @rbtree.values_at("d", "a", "e")
|
512
|
+
assert_equal(["D", "A", nil], result)
|
446
513
|
end
|
447
514
|
|
448
515
|
def test_invert
|
@@ -472,12 +539,29 @@ class RBTreeTest < Test::Unit::TestCase
|
|
472
539
|
rbtree = RBTree.new
|
473
540
|
rbtree["e"] = "E"
|
474
541
|
|
475
|
-
|
476
|
-
assert_equal(RBTree[*%w(a A b B c C d D e E)],
|
542
|
+
result = @rbtree.merge(rbtree)
|
543
|
+
assert_equal(RBTree[*%w(a A b B c C d D e E)], result)
|
477
544
|
|
478
545
|
assert_equal(4, @rbtree.size)
|
479
546
|
end
|
480
|
-
|
547
|
+
|
548
|
+
if MultiRBTree.method_defined?(:flatten)
|
549
|
+
def test_flatten
|
550
|
+
rbtree = RBTree.new
|
551
|
+
rbtree.readjust {|a, b| a.flatten <=> b.flatten }
|
552
|
+
rbtree[["a"]] = ["A"]
|
553
|
+
rbtree[[["b"]]] = [["B"]]
|
554
|
+
assert_equal([["a"], ["A"], [["b"]], [["B"]]], rbtree.flatten)
|
555
|
+
assert_equal([["a"], ["A"], [["b"]], [["B"]]], rbtree.flatten(0))
|
556
|
+
assert_equal([["a"], ["A"], [["b"]], [["B"]]], rbtree.flatten(1))
|
557
|
+
assert_equal(["a", "A", ["b"], ["B"]], rbtree.flatten(2))
|
558
|
+
assert_equal(["a", "A", "b", "B"], rbtree.flatten(3))
|
559
|
+
|
560
|
+
assert_raises(TypeError) { @rbtree.flatten("e") }
|
561
|
+
assert_raises(ArgumentError) { @rbtree.flatten(1, 2) }
|
562
|
+
end
|
563
|
+
end
|
564
|
+
|
481
565
|
def test_has_key
|
482
566
|
assert_equal(true, @rbtree.has_key?("a"))
|
483
567
|
assert_equal(true, @rbtree.has_key?("b"))
|
@@ -506,25 +590,10 @@ class RBTreeTest < Test::Unit::TestCase
|
|
506
590
|
assert_equal([%w(a A), %w(b B), %w(c C), %w(d D)], @rbtree.to_a)
|
507
591
|
end
|
508
592
|
|
509
|
-
def test_to_s
|
510
|
-
if RUBY_VERSION < "1.9"
|
511
|
-
assert_equal("aAbBcCdD", @rbtree.to_s)
|
512
|
-
else
|
513
|
-
expected = "[[\"a\", \"A\"], [\"b\", \"B\"], [\"c\", \"C\"], [\"d\", \"D\"]]"
|
514
|
-
assert_equal(expected, @rbtree.to_s)
|
515
|
-
|
516
|
-
rbtree = RBTree.new
|
517
|
-
rbtree[rbtree] = rbtree
|
518
|
-
rbtree.default = rbtree
|
519
|
-
expected = "[[#<RBTree: {#<RBTree: ...>=>#<RBTree: ...>}, default=#<RBTree: ...>, cmp_proc=nil>, #<RBTree: {#<RBTree: ...>=>#<RBTree: ...>}, default=#<RBTree: ...>, cmp_proc=nil>]]"
|
520
|
-
assert_equal(expected, rbtree.to_s)
|
521
|
-
end
|
522
|
-
end
|
523
|
-
|
524
593
|
def test_to_hash
|
525
594
|
@rbtree.default = "e"
|
526
595
|
hash = @rbtree.to_hash
|
527
|
-
assert_equal(@rbtree.to_a.flatten, hash.
|
596
|
+
assert_equal(@rbtree.to_a.flatten, hash.sort_by {|key, val| key}.flatten)
|
528
597
|
assert_equal("e", hash.default)
|
529
598
|
|
530
599
|
rbtree = RBTree.new { "e" }
|
@@ -541,67 +610,82 @@ class RBTreeTest < Test::Unit::TestCase
|
|
541
610
|
end
|
542
611
|
|
543
612
|
def test_inspect
|
544
|
-
|
545
|
-
|
546
|
-
|
547
|
-
|
548
|
-
|
549
|
-
|
550
|
-
|
551
|
-
|
552
|
-
|
553
|
-
|
554
|
-
|
555
|
-
|
556
|
-
|
557
|
-
|
558
|
-
|
559
|
-
|
560
|
-
|
561
|
-
|
562
|
-
|
563
|
-
|
564
|
-
|
565
|
-
|
566
|
-
|
567
|
-
|
568
|
-
|
569
|
-
|
570
|
-
|
613
|
+
[:to_s, :inspect].each do |method|
|
614
|
+
@rbtree.default = "e"
|
615
|
+
@rbtree.readjust {|a, b| a <=> b}
|
616
|
+
re = /#<RBTree: (\{.*\}), default=(.*), cmp_proc=(.*)>/
|
617
|
+
|
618
|
+
assert_match(re, @rbtree.send(method))
|
619
|
+
match = re.match(@rbtree.send(method))
|
620
|
+
tree, default, cmp_proc = match.to_a[1..-1]
|
621
|
+
assert_equal(%({"a"=>"A", "b"=>"B", "c"=>"C", "d"=>"D"}), tree)
|
622
|
+
assert_equal(%("e"), default)
|
623
|
+
assert_match(/#<Proc:\w+([@ ]#{__FILE__}:\d+)?>/o, cmp_proc)
|
624
|
+
|
625
|
+
rbtree = RBTree.new
|
626
|
+
assert_match(re, rbtree.send(method))
|
627
|
+
match = re.match(rbtree.send(method))
|
628
|
+
tree, default, cmp_proc = match.to_a[1..-1]
|
629
|
+
assert_equal("{}", tree)
|
630
|
+
assert_equal("nil", default)
|
631
|
+
assert_equal("nil", cmp_proc)
|
632
|
+
|
633
|
+
next if method == :to_s and RUBY_VERSION < "1.9"
|
634
|
+
|
635
|
+
rbtree = RBTree.new
|
636
|
+
rbtree[rbtree] = rbtree
|
637
|
+
rbtree.default = rbtree
|
638
|
+
match = re.match(rbtree.send(method))
|
639
|
+
tree, default, cmp_proc = match.to_a[1..-1]
|
640
|
+
assert_equal("{#<RBTree: ...>=>#<RBTree: ...>}", tree)
|
641
|
+
assert_equal("#<RBTree: ...>", default)
|
642
|
+
assert_equal("nil", cmp_proc)
|
643
|
+
end
|
571
644
|
end
|
572
645
|
|
573
646
|
def test_lower_bound
|
574
647
|
rbtree = RBTree[*%w(a A c C e E)]
|
575
|
-
assert_equal(
|
576
|
-
assert_equal(
|
648
|
+
assert_equal(%w(c C), rbtree.lower_bound("c"))
|
649
|
+
assert_equal(%w(c C), rbtree.lower_bound("b"))
|
577
650
|
assert_equal(nil, rbtree.lower_bound("f"))
|
578
651
|
end
|
579
652
|
|
580
653
|
def test_upper_bound
|
581
654
|
rbtree = RBTree[*%w(a A c C e E)]
|
582
|
-
assert_equal(
|
583
|
-
assert_equal(
|
655
|
+
assert_equal(%w(c C), rbtree.upper_bound("c"))
|
656
|
+
assert_equal(%w(c C), rbtree.upper_bound("d"))
|
584
657
|
assert_equal(nil, rbtree.upper_bound("Z"))
|
585
658
|
end
|
586
659
|
|
587
660
|
def test_bound
|
588
661
|
rbtree = RBTree[*%w(a A c C e E)]
|
589
|
-
assert_equal(%w(a A c C), rbtree.bound("a", "c").flatten)
|
590
|
-
assert_equal(%w(a A), rbtree.bound("a").flatten)
|
591
|
-
assert_equal(%w(c C e E), rbtree.bound("b", "f").flatten)
|
662
|
+
assert_equal(%w(a A c C), rbtree.bound("a", "c").to_a.flatten)
|
663
|
+
assert_equal(%w(a A), rbtree.bound("a").to_a.flatten)
|
664
|
+
assert_equal(%w(c C e E), rbtree.bound("b", "f").to_a.flatten)
|
592
665
|
|
593
|
-
assert_equal([], rbtree.bound("b", "b"))
|
594
|
-
assert_equal([], rbtree.bound("Y", "Z"))
|
595
|
-
assert_equal([], rbtree.bound("f", "g"))
|
596
|
-
assert_equal([], rbtree.bound("f", "Z"))
|
666
|
+
assert_equal([], rbtree.bound("b", "b").to_a)
|
667
|
+
assert_equal([], rbtree.bound("Y", "Z").to_a)
|
668
|
+
assert_equal([], rbtree.bound("f", "g").to_a)
|
669
|
+
assert_equal([], rbtree.bound("f", "Z").to_a)
|
670
|
+
|
671
|
+
if defined?(Enumerator) and Enumerator.method_defined?(:size)
|
672
|
+
assert_equal(2, rbtree.bound("a", "c").size)
|
673
|
+
assert_equal(1, rbtree.bound("a").size)
|
674
|
+
assert_equal(2, rbtree.bound("b", "f").size)
|
675
|
+
|
676
|
+
assert_equal(0, rbtree.bound("b", "b").size)
|
677
|
+
assert_equal(0, rbtree.bound("Y", "Z").size)
|
678
|
+
assert_equal(0, rbtree.bound("f", "g").size)
|
679
|
+
assert_equal(0, rbtree.bound("f", "Z").size)
|
680
|
+
end
|
597
681
|
end
|
598
682
|
|
599
683
|
def test_bound_block
|
600
|
-
|
684
|
+
result = []
|
601
685
|
@rbtree.bound("b", "c") {|key, val|
|
602
|
-
|
686
|
+
result.push(key)
|
603
687
|
}
|
604
|
-
assert_equal(%w(b c),
|
688
|
+
assert_equal(%w(b c), result)
|
605
689
|
|
606
690
|
assert_raises(TypeError) {
|
607
691
|
@rbtree.bound("a", "d") {
|
@@ -621,7 +705,7 @@ class RBTreeTest < Test::Unit::TestCase
|
|
621
705
|
end
|
622
706
|
|
623
707
|
def test_first
|
624
|
-
assert_equal(
|
708
|
+
assert_equal(%w(a A), @rbtree.first)
|
625
709
|
|
626
710
|
rbtree = RBTree.new("e")
|
627
711
|
assert_equal("e", rbtree.first)
|
@@ -631,7 +715,7 @@ class RBTreeTest < Test::Unit::TestCase
|
|
631
715
|
end
|
632
716
|
|
633
717
|
def test_last
|
634
|
-
assert_equal(
|
718
|
+
assert_equal(%w(d D), @rbtree.last)
|
635
719
|
|
636
720
|
rbtree = RBTree.new("e")
|
637
721
|
assert_equal("e", rbtree.last)
|
@@ -661,7 +745,33 @@ class RBTreeTest < Test::Unit::TestCase
|
|
661
745
|
@rbtree.readjust(nil)
|
662
746
|
assert_raises(ArgumentError) { @rbtree[0] = nil }
|
663
747
|
|
748
|
+
if Symbol.method_defined?(:to_proc)
|
749
|
+
rbtree = RBTree[*%w(a A B b)]
|
750
|
+
assert_equal(%w(B b a A), rbtree.to_a.flatten)
|
751
|
+
rbtree.readjust(:casecmp)
|
752
|
+
assert_equal(%w(a A B b), rbtree.to_a.flatten)
|
753
|
+
end
|
664
754
|
|
755
|
+
if RUBY_VERSION >= "1.9.2"
|
756
|
+
assert_nothing_raised {
|
757
|
+
@rbtree.readjust(lambda {|a, b| a <=> b })
|
758
|
+
@rbtree.readjust(lambda {|*a| a[0] <=> a[1] })
|
759
|
+
@rbtree.readjust(lambda {|a, *b| a <=> b[0] })
|
760
|
+
@rbtree.readjust(lambda {|a, b, *c| a <=> b })
|
761
|
+
@rbtree.readjust(&lambda {|a, b| a <=> b })
|
762
|
+
@rbtree.readjust(&lambda {|*a| a[0] <=> a[1] })
|
763
|
+
@rbtree.readjust(&lambda {|a, *b| a <=> b[0] })
|
764
|
+
@rbtree.readjust(&lambda {|a, b, *c| a <=> b })
|
765
|
+
}
|
766
|
+
assert_raises(TypeError) { @rbtree.readjust(lambda {|a| 1 }) }
|
767
|
+
assert_raises(TypeError) { @rbtree.readjust(lambda {|a, b, c| 1 }) }
|
768
|
+
assert_raises(TypeError) { @rbtree.readjust(lambda {|a, b, c, *d| 1 }) }
|
769
|
+
assert_raises(TypeError) { @rbtree.readjust(&lambda {|a| 1 }) }
|
770
|
+
assert_raises(TypeError) { @rbtree.readjust(&lambda {|a, b, c| 1 }) }
|
771
|
+
assert_raises(TypeError) { @rbtree.readjust(&lambda {|a, b, c, *d| 1 }) }
|
772
|
+
end
|
773
|
+
|
774
|
+
|
665
775
|
rbtree = RBTree.new
|
666
776
|
key = ["a"]
|
667
777
|
rbtree[key] = nil
|
@@ -672,12 +782,22 @@ class RBTreeTest < Test::Unit::TestCase
|
|
672
782
|
rbtree.readjust
|
673
783
|
assert_equal([["e"], ["f"]], rbtree.keys)
|
674
784
|
|
675
|
-
assert_raises(ArgumentError) { @rbtree.readjust { "e" } }
|
676
785
|
assert_raises(TypeError) { @rbtree.readjust("e") }
|
677
786
|
assert_raises(ArgumentError) {
|
678
787
|
@rbtree.readjust(proc) {|a,b| a <=> b }
|
679
788
|
}
|
680
789
|
assert_raises(ArgumentError) { @rbtree.readjust(proc, proc) }
|
790
|
+
|
791
|
+
|
792
|
+
rbtree = RBTree[("a".."z").to_a.zip(("A".."Z").to_a)]
|
793
|
+
assert_nothing_raised do
|
794
|
+
rbtree.readjust do |a, b|
|
795
|
+
ObjectSpace.each_object(RBTree) do |temp|
|
796
|
+
temp.clear if temp.size == rbtree.size - 1
|
797
|
+
end
|
798
|
+
a <=> b
|
799
|
+
end
|
800
|
+
end
|
681
801
|
end
|
682
802
|
|
683
803
|
def test_replace
|
@@ -695,13 +815,13 @@ class RBTreeTest < Test::Unit::TestCase
|
|
695
815
|
end
|
696
816
|
|
697
817
|
def test_reverse_each
|
698
|
-
|
699
|
-
@rbtree.reverse_each { |key, val|
|
700
|
-
assert_equal(%w(d D c C b B a A),
|
818
|
+
result = []
|
819
|
+
@rbtree.reverse_each { |key, val| result.push([key, val]) }
|
820
|
+
assert_equal(%w(d D c C b B a A), result.flatten)
|
701
821
|
|
702
|
-
if
|
822
|
+
if have_enumerator?
|
703
823
|
enumerator = @rbtree.reverse_each
|
704
|
-
assert_equal(%w(d D c C b B a A), enumerator.
|
824
|
+
assert_equal(%w(d D c C b B a A), enumerator.to_a.flatten)
|
705
825
|
end
|
706
826
|
end
|
707
827
|
|
@@ -720,6 +840,16 @@ class RBTreeTest < Test::Unit::TestCase
|
|
720
840
|
Marshal.dump(@rbtree)
|
721
841
|
}
|
722
842
|
end
|
843
|
+
|
844
|
+
def test_modify_in_cmp_proc
|
845
|
+
can_clear = false
|
846
|
+
@rbtree.readjust do |a, b|
|
847
|
+
@rbtree.clear if can_clear
|
848
|
+
a <=> b
|
849
|
+
end
|
850
|
+
can_clear = true
|
851
|
+
assert_raises(TypeError) { @rbtree["e"] }
|
852
|
+
end
|
723
853
|
|
724
854
|
begin
|
725
855
|
require "pp"
|
@@ -800,15 +930,6 @@ class MultiRBTreeTest < Test::Unit::TestCase
|
|
800
930
|
assert_equal([%w(a A), %w(b B), %w(b C), %w(b D), %w(c C)],
|
801
931
|
@rbtree.to_a)
|
802
932
|
end
|
803
|
-
|
804
|
-
def test_to_s
|
805
|
-
if RUBY_VERSION < "1.9"
|
806
|
-
assert_equal("aAbBbCbDcC", @rbtree.to_s)
|
807
|
-
else
|
808
|
-
expected = "[[\"a\", \"A\"], [\"b\", \"B\"], [\"b\", \"C\"], \[\"b\", \"D\"], [\"c\", \"C\"]]"
|
809
|
-
assert_equal(expected, @rbtree.to_s)
|
810
|
-
end
|
811
|
-
end
|
812
933
|
|
813
934
|
def test_to_hash
|
814
935
|
assert_raises(TypeError) {
|
@@ -857,11 +978,11 @@ class MultiRBTreeTest < Test::Unit::TestCase
|
|
857
978
|
end
|
858
979
|
|
859
980
|
def test_each
|
860
|
-
|
981
|
+
result = []
|
861
982
|
@rbtree.each {|k, v|
|
862
|
-
|
983
|
+
result << k << v
|
863
984
|
}
|
864
|
-
assert_equal(%w(a A b B b C b D c C),
|
985
|
+
assert_equal(%w(a A b B b C b D c C), result)
|
865
986
|
end
|
866
987
|
|
867
988
|
def test_delete
|
@@ -883,6 +1004,17 @@ class MultiRBTreeTest < Test::Unit::TestCase
|
|
883
1004
|
assert_equal(%w(a A c C), @rbtree.to_a.flatten)
|
884
1005
|
end
|
885
1006
|
|
1007
|
+
def test_keep_if
|
1008
|
+
@rbtree.keep_if {|k, v| k != "b" }
|
1009
|
+
assert_equal(%w(a A c C), @rbtree.to_a.flatten)
|
1010
|
+
end
|
1011
|
+
|
1012
|
+
if MultiRBTree.method_defined?(:flatten)
|
1013
|
+
def test_flatten
|
1014
|
+
assert_equal(%w(a A b B b C b D c C), @rbtree.flatten)
|
1015
|
+
end
|
1016
|
+
end
|
1017
|
+
|
886
1018
|
def test_inspect
|
887
1019
|
assert_equal(%(#<MultiRBTree: {"a"=>"A", "b"=>"B", "b"=>"C", "b"=>"D", "c"=>"C"}, default=nil, cmp_proc=nil>),
|
888
1020
|
@rbtree.inspect)
|
@@ -906,7 +1038,7 @@ class MultiRBTreeTest < Test::Unit::TestCase
|
|
906
1038
|
end
|
907
1039
|
|
908
1040
|
def test_bound
|
909
|
-
assert_equal(%w(b B b C b D), @rbtree.bound("b").flatten)
|
1041
|
+
assert_equal(%w(b B b C b D), @rbtree.bound("b").to_a.flatten)
|
910
1042
|
end
|
911
1043
|
|
912
1044
|
def test_first
|
@@ -939,11 +1071,6 @@ class MultiRBTreeTest < Test::Unit::TestCase
|
|
939
1071
|
assert_equal(true, @rbtree.has_value?("D"))
|
940
1072
|
end
|
941
1073
|
|
942
|
-
def test_select
|
943
|
-
assert_equal(%w(b B b C b D), @rbtree.select {|k, v| k == "b"}.flatten)
|
944
|
-
assert_equal(%w(b C c C), @rbtree.select {|k, v| v == "C"}.flatten)
|
945
|
-
end
|
946
|
-
|
947
1074
|
def test_values_at
|
948
1075
|
assert_equal(%w(A B), @rbtree.values_at("a", "b"))
|
949
1076
|
end
|
@@ -960,9 +1087,9 @@ class MultiRBTreeTest < Test::Unit::TestCase
|
|
960
1087
|
assert_equal(%w(A B C D C), @rbtree.values)
|
961
1088
|
end
|
962
1089
|
|
963
|
-
def
|
964
|
-
assert_equal("b", @rbtree.
|
965
|
-
assert_equal("b", @rbtree.
|
966
|
-
assert_equal("b", @rbtree.
|
1090
|
+
def test_key
|
1091
|
+
assert_equal("b", @rbtree.key("B"))
|
1092
|
+
assert_equal("b", @rbtree.key("C"))
|
1093
|
+
assert_equal("b", @rbtree.key("D"))
|
967
1094
|
end
|
968
1095
|
end
|