rbtree 0.3.0 → 0.4.0

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