rbtree 0.2.1 → 0.4.3
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 +36 -72
- data/dict.c +364 -83
- data/dict.h +30 -6
- data/extconf.rb +11 -5
- data/rbtree.c +796 -470
- data/test.rb +313 -157
- metadata +42 -45
- data/ChangeLog +0 -483
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
|
-
rbtree = RBTree.new {|
|
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
|
@@ -181,10 +242,18 @@ class RBTreeTest < Test::Unit::TestCase
|
|
181
242
|
|
182
243
|
a = RBTree.new
|
183
244
|
b = RBTree.new
|
184
|
-
a.readjust {|
|
245
|
+
a.readjust {|x, y| x <=> y }
|
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,15 +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
|
-
assert_equal("E", @rbtree.fetch("e", "F") { "E" })
|
269
|
+
# assert_equal("E", @rbtree.fetch("e", "F") { "E" })
|
201
270
|
|
202
271
|
assert_raises(ArgumentError) { @rbtree.fetch }
|
203
272
|
assert_raises(ArgumentError) { @rbtree.fetch("e", "E", "E") }
|
204
273
|
end
|
205
274
|
|
206
|
-
def
|
207
|
-
assert_equal("a", @rbtree.
|
208
|
-
assert_equal(nil, @rbtree.
|
275
|
+
def test_key
|
276
|
+
assert_equal("a", @rbtree.key("A"))
|
277
|
+
assert_equal(nil, @rbtree.key("E"))
|
209
278
|
end
|
210
279
|
|
211
280
|
def test_empty_p
|
@@ -215,9 +284,9 @@ class RBTreeTest < Test::Unit::TestCase
|
|
215
284
|
end
|
216
285
|
|
217
286
|
def test_each
|
218
|
-
|
219
|
-
@rbtree.each {|key, val|
|
220
|
-
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)
|
221
290
|
|
222
291
|
assert_raises(TypeError) {
|
223
292
|
@rbtree.each { @rbtree["e"] = "E" }
|
@@ -233,41 +302,16 @@ class RBTreeTest < Test::Unit::TestCase
|
|
233
302
|
}
|
234
303
|
assert_equal(4, @rbtree.size)
|
235
304
|
|
236
|
-
if
|
305
|
+
if have_enumerator?
|
237
306
|
enumerator = @rbtree.each
|
238
|
-
assert_equal(%w(a A b B c C d D), enumerator.
|
239
|
-
end
|
240
|
-
end
|
241
|
-
|
242
|
-
def test_each_pair
|
243
|
-
ret = []
|
244
|
-
@rbtree.each_pair {|key, val| ret << key << val }
|
245
|
-
assert_equal(%w(a A b B c C d D), ret)
|
246
|
-
|
247
|
-
assert_raises(TypeError) {
|
248
|
-
@rbtree.each_pair { @rbtree["e"] = "E" }
|
249
|
-
}
|
250
|
-
assert_equal(4, @rbtree.size)
|
251
|
-
|
252
|
-
@rbtree.each_pair {
|
253
|
-
@rbtree.each_pair {}
|
254
|
-
assert_raises(TypeError) {
|
255
|
-
@rbtree["e"] = "E"
|
256
|
-
}
|
257
|
-
break
|
258
|
-
}
|
259
|
-
assert_equal(4, @rbtree.size)
|
260
|
-
|
261
|
-
if defined?(Enumerable::Enumerator)
|
262
|
-
enumerator = @rbtree.each_pair
|
263
|
-
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)
|
264
308
|
end
|
265
309
|
end
|
266
310
|
|
267
311
|
def test_each_key
|
268
|
-
|
269
|
-
@rbtree.each_key {|key|
|
270
|
-
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)
|
271
315
|
|
272
316
|
assert_raises(TypeError) {
|
273
317
|
@rbtree.each_key { @rbtree["e"] = "E" }
|
@@ -283,16 +327,16 @@ class RBTreeTest < Test::Unit::TestCase
|
|
283
327
|
}
|
284
328
|
assert_equal(4, @rbtree.size)
|
285
329
|
|
286
|
-
if
|
330
|
+
if have_enumerator?
|
287
331
|
enumerator = @rbtree.each_key
|
288
|
-
assert_equal(%w(a b c d), enumerator.
|
332
|
+
assert_equal(%w(a b c d), enumerator.to_a.flatten)
|
289
333
|
end
|
290
334
|
end
|
291
335
|
|
292
336
|
def test_each_value
|
293
|
-
|
294
|
-
@rbtree.each_value {|val|
|
295
|
-
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)
|
296
340
|
|
297
341
|
assert_raises(TypeError) {
|
298
342
|
@rbtree.each_value { @rbtree["e"] = "E" }
|
@@ -308,16 +352,16 @@ class RBTreeTest < Test::Unit::TestCase
|
|
308
352
|
}
|
309
353
|
assert_equal(4, @rbtree.size)
|
310
354
|
|
311
|
-
if
|
355
|
+
if have_enumerator?
|
312
356
|
enumerator = @rbtree.each_value
|
313
|
-
assert_equal(%w(A B C D), enumerator.
|
357
|
+
assert_equal(%w(A B C D), enumerator.to_a.flatten)
|
314
358
|
end
|
315
359
|
end
|
316
360
|
|
317
361
|
def test_shift
|
318
|
-
|
362
|
+
result = @rbtree.shift
|
319
363
|
assert_equal(3, @rbtree.size)
|
320
|
-
assert_equal(
|
364
|
+
assert_equal(%w(a A), result)
|
321
365
|
assert_equal(nil, @rbtree["a"])
|
322
366
|
|
323
367
|
3.times { @rbtree.shift }
|
@@ -331,9 +375,9 @@ class RBTreeTest < Test::Unit::TestCase
|
|
331
375
|
end
|
332
376
|
|
333
377
|
def test_pop
|
334
|
-
|
378
|
+
result = @rbtree.pop
|
335
379
|
assert_equal(3, @rbtree.size)
|
336
|
-
assert_equal(
|
380
|
+
assert_equal(%w(d D), result)
|
337
381
|
assert_equal(nil, @rbtree["d"])
|
338
382
|
|
339
383
|
3.times { @rbtree.pop }
|
@@ -347,8 +391,8 @@ class RBTreeTest < Test::Unit::TestCase
|
|
347
391
|
end
|
348
392
|
|
349
393
|
def test_delete
|
350
|
-
|
351
|
-
assert_equal("C",
|
394
|
+
result = @rbtree.delete("c")
|
395
|
+
assert_equal("C", result)
|
352
396
|
assert_equal(3, @rbtree.size)
|
353
397
|
assert_equal(nil, @rbtree["c"])
|
354
398
|
|
@@ -357,7 +401,8 @@ class RBTreeTest < Test::Unit::TestCase
|
|
357
401
|
end
|
358
402
|
|
359
403
|
def test_delete_if
|
360
|
-
@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)
|
361
406
|
assert_equal(RBTree[*%w(c C d D)], @rbtree)
|
362
407
|
|
363
408
|
assert_raises(ArgumentError) {
|
@@ -381,58 +426,90 @@ class RBTreeTest < Test::Unit::TestCase
|
|
381
426
|
}
|
382
427
|
assert_equal(0, @rbtree.size)
|
383
428
|
|
384
|
-
if
|
429
|
+
if have_enumerator?
|
385
430
|
rbtree = RBTree[*%w(b B d D a A c C)]
|
386
|
-
|
387
|
-
assert_equal([
|
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?
|
442
|
+
rbtree = RBTree[*%w(b B d D a A c C)]
|
443
|
+
rbtree.keep_if.with_index {|(key, val), i| i < 2 }
|
444
|
+
assert_equal(RBTree[*%w(a A b B)], rbtree)
|
388
445
|
end
|
389
446
|
end
|
390
447
|
|
391
448
|
def test_reject_bang
|
392
|
-
|
393
|
-
assert_equal(nil,
|
449
|
+
result = @rbtree.reject! { false }
|
450
|
+
assert_equal(nil, result)
|
394
451
|
assert_equal(4, @rbtree.size)
|
395
452
|
|
396
|
-
|
397
|
-
assert_same(@rbtree,
|
398
|
-
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)
|
399
456
|
|
400
|
-
if
|
457
|
+
if have_enumerator?
|
401
458
|
rbtree = RBTree[*%w(b B d D a A c C)]
|
402
|
-
|
403
|
-
assert_equal([
|
459
|
+
rbtree.reject!.with_index {|(key, val), i| i < 2 }
|
460
|
+
assert_equal(RBTree[*%w(c C d D)], rbtree)
|
404
461
|
end
|
405
462
|
end
|
406
463
|
|
407
464
|
def test_reject
|
408
|
-
|
409
|
-
assert_equal(
|
465
|
+
result = @rbtree.reject { false }
|
466
|
+
assert_equal(RBTree[*%w(a A b B c C d D)], result)
|
410
467
|
assert_equal(4, @rbtree.size)
|
411
468
|
|
412
|
-
|
413
|
-
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)
|
414
471
|
assert_equal(4, @rbtree.size)
|
415
472
|
|
416
|
-
if
|
417
|
-
|
418
|
-
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)
|
419
476
|
end
|
420
477
|
end
|
421
|
-
|
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
|
+
|
422
495
|
def test_select
|
423
|
-
|
424
|
-
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)
|
425
502
|
assert_raises(ArgumentError) { @rbtree.select("c") }
|
426
503
|
|
427
|
-
if
|
428
|
-
|
429
|
-
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)
|
430
507
|
end
|
431
508
|
end
|
432
509
|
|
433
510
|
def test_values_at
|
434
|
-
|
435
|
-
assert_equal(["D", "A", nil],
|
511
|
+
result = @rbtree.values_at("d", "a", "e")
|
512
|
+
assert_equal(["D", "A", nil], result)
|
436
513
|
end
|
437
514
|
|
438
515
|
def test_invert
|
@@ -462,12 +539,29 @@ class RBTreeTest < Test::Unit::TestCase
|
|
462
539
|
rbtree = RBTree.new
|
463
540
|
rbtree["e"] = "E"
|
464
541
|
|
465
|
-
|
466
|
-
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)
|
467
544
|
|
468
545
|
assert_equal(4, @rbtree.size)
|
469
546
|
end
|
470
|
-
|
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
|
+
|
471
565
|
def test_has_key
|
472
566
|
assert_equal(true, @rbtree.has_key?("a"))
|
473
567
|
assert_equal(true, @rbtree.has_key?("b"))
|
@@ -496,14 +590,10 @@ class RBTreeTest < Test::Unit::TestCase
|
|
496
590
|
assert_equal([%w(a A), %w(b B), %w(c C), %w(d D)], @rbtree.to_a)
|
497
591
|
end
|
498
592
|
|
499
|
-
def test_to_s
|
500
|
-
assert_equal("aAbBcCdD", @rbtree.to_s)
|
501
|
-
end
|
502
|
-
|
503
593
|
def test_to_hash
|
504
594
|
@rbtree.default = "e"
|
505
595
|
hash = @rbtree.to_hash
|
506
|
-
assert_equal(@rbtree.to_a.flatten, hash.
|
596
|
+
assert_equal(@rbtree.to_a.flatten, hash.sort_by {|key, val| key}.flatten)
|
507
597
|
assert_equal("e", hash.default)
|
508
598
|
|
509
599
|
rbtree = RBTree.new { "e" }
|
@@ -520,64 +610,82 @@ class RBTreeTest < Test::Unit::TestCase
|
|
520
610
|
end
|
521
611
|
|
522
612
|
def test_inspect
|
523
|
-
|
524
|
-
|
525
|
-
|
526
|
-
|
527
|
-
|
528
|
-
|
529
|
-
|
530
|
-
|
531
|
-
|
532
|
-
|
533
|
-
|
534
|
-
|
535
|
-
|
536
|
-
|
537
|
-
|
538
|
-
|
539
|
-
|
540
|
-
|
541
|
-
|
542
|
-
|
543
|
-
|
544
|
-
|
545
|
-
|
546
|
-
|
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
|
547
644
|
end
|
548
645
|
|
549
646
|
def test_lower_bound
|
550
647
|
rbtree = RBTree[*%w(a A c C e E)]
|
551
|
-
assert_equal(
|
552
|
-
assert_equal(
|
648
|
+
assert_equal(%w(c C), rbtree.lower_bound("c"))
|
649
|
+
assert_equal(%w(c C), rbtree.lower_bound("b"))
|
553
650
|
assert_equal(nil, rbtree.lower_bound("f"))
|
554
651
|
end
|
555
652
|
|
556
653
|
def test_upper_bound
|
557
654
|
rbtree = RBTree[*%w(a A c C e E)]
|
558
|
-
assert_equal(
|
559
|
-
assert_equal(
|
655
|
+
assert_equal(%w(c C), rbtree.upper_bound("c"))
|
656
|
+
assert_equal(%w(c C), rbtree.upper_bound("d"))
|
560
657
|
assert_equal(nil, rbtree.upper_bound("Z"))
|
561
658
|
end
|
562
659
|
|
563
660
|
def test_bound
|
564
661
|
rbtree = RBTree[*%w(a A c C e E)]
|
565
|
-
assert_equal(%w(a A c C), rbtree.bound("a", "c").flatten)
|
566
|
-
assert_equal(%w(a A), rbtree.bound("a").flatten)
|
567
|
-
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)
|
568
665
|
|
569
|
-
assert_equal([], rbtree.bound("b", "b"))
|
570
|
-
assert_equal([], rbtree.bound("Y", "Z"))
|
571
|
-
assert_equal([], rbtree.bound("f", "g"))
|
572
|
-
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
|
573
681
|
end
|
574
682
|
|
575
683
|
def test_bound_block
|
576
|
-
|
684
|
+
result = []
|
577
685
|
@rbtree.bound("b", "c") {|key, val|
|
578
|
-
|
686
|
+
result.push(key)
|
579
687
|
}
|
580
|
-
assert_equal(%w(b c),
|
688
|
+
assert_equal(%w(b c), result)
|
581
689
|
|
582
690
|
assert_raises(TypeError) {
|
583
691
|
@rbtree.bound("a", "d") {
|
@@ -597,7 +705,7 @@ class RBTreeTest < Test::Unit::TestCase
|
|
597
705
|
end
|
598
706
|
|
599
707
|
def test_first
|
600
|
-
assert_equal(
|
708
|
+
assert_equal(%w(a A), @rbtree.first)
|
601
709
|
|
602
710
|
rbtree = RBTree.new("e")
|
603
711
|
assert_equal("e", rbtree.first)
|
@@ -607,7 +715,7 @@ class RBTreeTest < Test::Unit::TestCase
|
|
607
715
|
end
|
608
716
|
|
609
717
|
def test_last
|
610
|
-
assert_equal(
|
718
|
+
assert_equal(%w(d D), @rbtree.last)
|
611
719
|
|
612
720
|
rbtree = RBTree.new("e")
|
613
721
|
assert_equal("e", rbtree.last)
|
@@ -637,7 +745,33 @@ class RBTreeTest < Test::Unit::TestCase
|
|
637
745
|
@rbtree.readjust(nil)
|
638
746
|
assert_raises(ArgumentError) { @rbtree[0] = nil }
|
639
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
|
640
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
|
+
|
641
775
|
rbtree = RBTree.new
|
642
776
|
key = ["a"]
|
643
777
|
rbtree[key] = nil
|
@@ -648,12 +782,22 @@ class RBTreeTest < Test::Unit::TestCase
|
|
648
782
|
rbtree.readjust
|
649
783
|
assert_equal([["e"], ["f"]], rbtree.keys)
|
650
784
|
|
651
|
-
assert_raises(ArgumentError) { @rbtree.readjust { "e" } }
|
652
785
|
assert_raises(TypeError) { @rbtree.readjust("e") }
|
653
786
|
assert_raises(ArgumentError) {
|
654
787
|
@rbtree.readjust(proc) {|a,b| a <=> b }
|
655
788
|
}
|
656
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
|
657
801
|
end
|
658
802
|
|
659
803
|
def test_replace
|
@@ -671,13 +815,13 @@ class RBTreeTest < Test::Unit::TestCase
|
|
671
815
|
end
|
672
816
|
|
673
817
|
def test_reverse_each
|
674
|
-
|
675
|
-
@rbtree.reverse_each { |key, val|
|
676
|
-
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)
|
677
821
|
|
678
|
-
if
|
822
|
+
if have_enumerator?
|
679
823
|
enumerator = @rbtree.reverse_each
|
680
|
-
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)
|
681
825
|
end
|
682
826
|
end
|
683
827
|
|
@@ -696,13 +840,23 @@ class RBTreeTest < Test::Unit::TestCase
|
|
696
840
|
Marshal.dump(@rbtree)
|
697
841
|
}
|
698
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
|
699
853
|
|
700
854
|
begin
|
701
855
|
require "pp"
|
702
856
|
|
703
857
|
def test_pp
|
704
858
|
assert_equal(%(#<RBTree: {}, default=nil, cmp_proc=nil>\n),
|
705
|
-
PP.pp(RBTree
|
859
|
+
PP.pp(RBTree.new, ""))
|
706
860
|
assert_equal(%(#<RBTree: {"a"=>"A", "b"=>"B"}, default=nil, cmp_proc=nil>\n),
|
707
861
|
PP.pp(RBTree[*%w(a A b B)], ""))
|
708
862
|
|
@@ -728,7 +882,7 @@ class RBTreeTest < Test::Unit::TestCase
|
|
728
882
|
EOS
|
729
883
|
assert_equal(expected, PP.pp(rbtree, ""))
|
730
884
|
|
731
|
-
rbtree = RBTree
|
885
|
+
rbtree = RBTree.new
|
732
886
|
rbtree[rbtree] = rbtree
|
733
887
|
rbtree.default = rbtree
|
734
888
|
expected = <<EOS
|
@@ -776,10 +930,6 @@ class MultiRBTreeTest < Test::Unit::TestCase
|
|
776
930
|
assert_equal([%w(a A), %w(b B), %w(b C), %w(b D), %w(c C)],
|
777
931
|
@rbtree.to_a)
|
778
932
|
end
|
779
|
-
|
780
|
-
def test_to_s
|
781
|
-
assert_equal("aAbBbCbDcC", @rbtree.to_s)
|
782
|
-
end
|
783
933
|
|
784
934
|
def test_to_hash
|
785
935
|
assert_raises(TypeError) {
|
@@ -828,11 +978,11 @@ class MultiRBTreeTest < Test::Unit::TestCase
|
|
828
978
|
end
|
829
979
|
|
830
980
|
def test_each
|
831
|
-
|
981
|
+
result = []
|
832
982
|
@rbtree.each {|k, v|
|
833
|
-
|
983
|
+
result << k << v
|
834
984
|
}
|
835
|
-
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)
|
836
986
|
end
|
837
987
|
|
838
988
|
def test_delete
|
@@ -854,6 +1004,17 @@ class MultiRBTreeTest < Test::Unit::TestCase
|
|
854
1004
|
assert_equal(%w(a A c C), @rbtree.to_a.flatten)
|
855
1005
|
end
|
856
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
|
+
|
857
1018
|
def test_inspect
|
858
1019
|
assert_equal(%(#<MultiRBTree: {"a"=>"A", "b"=>"B", "b"=>"C", "b"=>"D", "c"=>"C"}, default=nil, cmp_proc=nil>),
|
859
1020
|
@rbtree.inspect)
|
@@ -877,7 +1038,7 @@ class MultiRBTreeTest < Test::Unit::TestCase
|
|
877
1038
|
end
|
878
1039
|
|
879
1040
|
def test_bound
|
880
|
-
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)
|
881
1042
|
end
|
882
1043
|
|
883
1044
|
def test_first
|
@@ -910,11 +1071,6 @@ class MultiRBTreeTest < Test::Unit::TestCase
|
|
910
1071
|
assert_equal(true, @rbtree.has_value?("D"))
|
911
1072
|
end
|
912
1073
|
|
913
|
-
def test_select
|
914
|
-
assert_equal(%w(b B b C b D), @rbtree.select {|k, v| k == "b"}.flatten)
|
915
|
-
assert_equal(%w(b C c C), @rbtree.select {|k, v| v == "C"}.flatten)
|
916
|
-
end
|
917
|
-
|
918
1074
|
def test_values_at
|
919
1075
|
assert_equal(%w(A B), @rbtree.values_at("a", "b"))
|
920
1076
|
end
|
@@ -931,9 +1087,9 @@ class MultiRBTreeTest < Test::Unit::TestCase
|
|
931
1087
|
assert_equal(%w(A B C D C), @rbtree.values)
|
932
1088
|
end
|
933
1089
|
|
934
|
-
def
|
935
|
-
assert_equal("b", @rbtree.
|
936
|
-
assert_equal("b", @rbtree.
|
937
|
-
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"))
|
938
1094
|
end
|
939
1095
|
end
|