rbtree3 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (10) hide show
  1. checksums.yaml +7 -0
  2. data/ChangeLog +492 -0
  3. data/LICENSE +22 -0
  4. data/README +106 -0
  5. data/dict.c +1216 -0
  6. data/dict.h +123 -0
  7. data/extconf.rb +13 -0
  8. data/rbtree.c +1701 -0
  9. data/test.rb +950 -0
  10. metadata +64 -0
data/test.rb ADDED
@@ -0,0 +1,950 @@
1
+ require "./rbtree"
2
+ require "test/unit.rb"
3
+
4
+ class RBTreeTest < Test::Unit::TestCase
5
+ def setup
6
+ @rbtree = RBTree[*%w(b B d D a A c C)]
7
+ end
8
+
9
+ def test_new
10
+ assert_nothing_raised {
11
+ RBTree.new
12
+ RBTree.new("a")
13
+ RBTree.new { "a" }
14
+ }
15
+ assert_raises(ArgumentError) { RBTree.new("a") {} }
16
+ assert_raises(ArgumentError) { RBTree.new("a", "a") }
17
+ end
18
+
19
+ def test_aref
20
+ assert_equal("A", @rbtree["a"])
21
+ assert_equal("B", @rbtree["b"])
22
+ assert_equal("C", @rbtree["c"])
23
+ assert_equal("D", @rbtree["d"])
24
+
25
+ assert_equal(nil, @rbtree["e"])
26
+ @rbtree.default = "E"
27
+ assert_equal("E", @rbtree["e"])
28
+ end
29
+
30
+ def test_size
31
+ assert_equal(4, @rbtree.size)
32
+ end
33
+
34
+ def test_create
35
+ rbtree = RBTree[]
36
+ assert_equal(0, rbtree.size)
37
+
38
+ rbtree = RBTree[@rbtree]
39
+ assert_equal(4, rbtree.size)
40
+ assert_equal("A", @rbtree["a"])
41
+ assert_equal("B", @rbtree["b"])
42
+ assert_equal("C", @rbtree["c"])
43
+ assert_equal("D", @rbtree["d"])
44
+
45
+ rbtree = RBTree[RBTree.new("e")]
46
+ assert_equal(nil, rbtree.default)
47
+ rbtree = RBTree[RBTree.new { "e" }]
48
+ assert_equal(nil, rbtree.default_proc)
49
+ @rbtree.readjust {|a,b| b <=> a }
50
+ assert_equal(nil, RBTree[@rbtree].cmp_proc)
51
+
52
+ assert_raises(ArgumentError) { RBTree["e"] }
53
+
54
+ rbtree = RBTree[Hash[*%w(b B d D a A c C)]]
55
+ assert_equal(4, rbtree.size)
56
+ assert_equal("A", rbtree["a"])
57
+ assert_equal("B", rbtree["b"])
58
+ assert_equal("C", rbtree["c"])
59
+ assert_equal("D", rbtree["d"])
60
+
61
+ rbtree = RBTree[[%w(a A), %w(b B), %w(c C), %w(d D)]];
62
+ assert_equal(4, rbtree.size)
63
+ assert_equal("A", rbtree["a"])
64
+ assert_equal("B", rbtree["b"])
65
+ assert_equal("C", rbtree["c"])
66
+ assert_equal("D", rbtree["d"])
67
+
68
+ rbtree = RBTree[[["a"]]]
69
+ assert_equal(1, rbtree.size)
70
+ assert_equal(nil, rbtree["a"])
71
+ end
72
+
73
+ def test_clear
74
+ @rbtree.clear
75
+ assert_equal(0, @rbtree.size)
76
+ end
77
+
78
+ def test_aset
79
+ @rbtree["e"] = "E"
80
+ assert_equal(5, @rbtree.size)
81
+ assert_equal("E", @rbtree["e"])
82
+
83
+ @rbtree["c"] = "E"
84
+ assert_equal(5, @rbtree.size)
85
+ assert_equal("E", @rbtree["c"])
86
+
87
+ assert_raises(ArgumentError) { @rbtree[100] = 100 }
88
+ assert_equal(5, @rbtree.size)
89
+
90
+
91
+ key = "f"
92
+ @rbtree[key] = "F"
93
+ cloned_key = @rbtree.last[0]
94
+ assert_equal("f", cloned_key)
95
+ assert_not_same(key, cloned_key)
96
+ assert_equal(true, cloned_key.frozen?)
97
+
98
+ @rbtree["f"] = "F"
99
+ assert_same(cloned_key, @rbtree.last[0])
100
+
101
+ rbtree = RBTree.new
102
+ key = ["g"]
103
+ rbtree[key] = "G"
104
+ assert_same(key, rbtree.first[0])
105
+ assert_equal(false, key.frozen?)
106
+ end
107
+
108
+ def test_clone
109
+ clone = @rbtree.clone
110
+ assert_equal(4, @rbtree.size)
111
+ assert_equal("A", @rbtree["a"])
112
+ assert_equal("B", @rbtree["b"])
113
+ assert_equal("C", @rbtree["c"])
114
+ assert_equal("D", @rbtree["d"])
115
+
116
+ rbtree = RBTree.new("e")
117
+ clone = rbtree.clone
118
+ assert_equal("e", clone.default)
119
+
120
+ rbtree = RBTree.new { "e" }
121
+ clone = rbtree.clone
122
+ assert_equal("e", clone.default(nil))
123
+
124
+ rbtree = RBTree.new
125
+ rbtree.readjust {|a, b| a <=> b }
126
+ clone = rbtree.clone
127
+ assert_equal(rbtree.cmp_proc, clone.cmp_proc)
128
+ end
129
+
130
+ def test_default
131
+ assert_equal(nil, @rbtree.default)
132
+
133
+ rbtree = RBTree.new("e")
134
+ assert_equal("e", rbtree.default)
135
+ assert_equal("e", rbtree.default("f"))
136
+ assert_raises(ArgumentError) { rbtree.default("e", "f") }
137
+
138
+ rbtree = RBTree.new {|tree, key| @rbtree[key || "c"] }
139
+ assert_equal("C", rbtree.default(nil))
140
+ assert_equal("B", rbtree.default("b"))
141
+ end
142
+
143
+ def test_set_default
144
+ rbtree = RBTree.new { "e" }
145
+ rbtree.default = "f"
146
+ assert_equal("f", rbtree.default)
147
+ end
148
+
149
+ def test_default_proc
150
+ rbtree = RBTree.new("e")
151
+ assert_equal(nil, rbtree.default_proc)
152
+
153
+ rbtree = RBTree.new { "e" }
154
+ assert_equal("e", rbtree.default_proc.call)
155
+ end
156
+
157
+ def test_equal
158
+ assert_equal(RBTree.new, RBTree.new)
159
+ assert_equal(@rbtree, @rbtree)
160
+ assert_not_equal(@rbtree, RBTree.new)
161
+
162
+ rbtree = RBTree[*%w(b B d D a A c C)]
163
+ assert_equal(@rbtree, rbtree)
164
+ rbtree["d"] = "A"
165
+ assert_not_equal(@rbtree, rbtree)
166
+ rbtree["d"] = "D"
167
+ rbtree["e"] = "E"
168
+ assert_not_equal(@rbtree, rbtree)
169
+ @rbtree["e"] = "E"
170
+ assert_equal(@rbtree, rbtree)
171
+
172
+ rbtree.default = "e"
173
+ assert_equal(@rbtree, rbtree)
174
+ @rbtree.default = "f"
175
+ assert_equal(@rbtree, rbtree)
176
+
177
+ a = RBTree.new("e")
178
+ b = RBTree.new { "f" }
179
+ assert_equal(a, b)
180
+ assert_equal(b, b.clone)
181
+
182
+ a = RBTree.new
183
+ b = RBTree.new
184
+ a.readjust {|x, y| x <=> y }
185
+ assert_not_equal(a, b)
186
+ b.readjust(a.cmp_proc)
187
+ assert_equal(a, b)
188
+ end
189
+
190
+ def test_fetch
191
+ assert_equal("A", @rbtree.fetch("a"))
192
+ assert_equal("B", @rbtree.fetch("b"))
193
+ assert_equal("C", @rbtree.fetch("c"))
194
+ assert_equal("D", @rbtree.fetch("d"))
195
+
196
+ assert_raises(IndexError) { @rbtree.fetch("e") }
197
+
198
+ assert_equal("E", @rbtree.fetch("e", "E"))
199
+ assert_equal("E", @rbtree.fetch("e") { "E" })
200
+ class << (stderr = "")
201
+ alias write <<
202
+ end
203
+ $stderr, stderr, $VERBOSE, verbose = stderr, $stderr, false, $VERBOSE
204
+ begin
205
+ assert_equal("E", @rbtree.fetch("e", "F") { "E" })
206
+ ensure
207
+ $stderr, stderr, $VERBOSE, verbose = stderr, $stderr, false, $VERBOSE
208
+ end
209
+ assert_match(/warning: block supersedes default value argument/, stderr)
210
+
211
+ assert_raises(ArgumentError) { @rbtree.fetch }
212
+ assert_raises(ArgumentError) { @rbtree.fetch("e", "E", "E") }
213
+ end
214
+
215
+ def test_index
216
+ assert_equal("a", @rbtree.index("A"))
217
+ assert_equal(nil, @rbtree.index("E"))
218
+ end
219
+
220
+ def test_empty_p
221
+ assert_equal(false, @rbtree.empty?)
222
+ @rbtree.clear
223
+ assert_equal(true, @rbtree.empty?)
224
+ end
225
+
226
+ def test_each
227
+ ret = []
228
+ @rbtree.each {|key, val| ret << key << val }
229
+ assert_equal(%w(a A b B c C d D), ret)
230
+
231
+ assert_raises(TypeError) {
232
+ @rbtree.each { @rbtree["e"] = "E" }
233
+ }
234
+ assert_equal(4, @rbtree.size)
235
+
236
+ @rbtree.each {
237
+ @rbtree.each {}
238
+ assert_raises(TypeError) {
239
+ @rbtree["e"] = "E"
240
+ }
241
+ break
242
+ }
243
+ assert_equal(4, @rbtree.size)
244
+
245
+ if defined?(Enumerable::Enumerator)
246
+ enumerator = @rbtree.each
247
+ assert_equal(%w(a A b B c C d D), enumerator.map.flatten)
248
+ end
249
+ end
250
+
251
+ def test_each_pair
252
+ ret = []
253
+ @rbtree.each_pair {|key, val| ret << key << val }
254
+ assert_equal(%w(a A b B c C d D), ret)
255
+
256
+ assert_raises(TypeError) {
257
+ @rbtree.each_pair { @rbtree["e"] = "E" }
258
+ }
259
+ assert_equal(4, @rbtree.size)
260
+
261
+ @rbtree.each_pair {
262
+ @rbtree.each_pair {}
263
+ assert_raises(TypeError) {
264
+ @rbtree["e"] = "E"
265
+ }
266
+ break
267
+ }
268
+ assert_equal(4, @rbtree.size)
269
+
270
+ if defined?(Enumerable::Enumerator)
271
+ enumerator = @rbtree.each_pair
272
+ assert_equal(%w(a A b B c C d D), enumerator.map.flatten)
273
+ end
274
+ end
275
+
276
+ def test_each_key
277
+ ret = []
278
+ @rbtree.each_key {|key| ret.push(key) }
279
+ assert_equal(%w(a b c d), ret)
280
+
281
+ assert_raises(TypeError) {
282
+ @rbtree.each_key { @rbtree["e"] = "E" }
283
+ }
284
+ assert_equal(4, @rbtree.size)
285
+
286
+ @rbtree.each_key {
287
+ @rbtree.each_key {}
288
+ assert_raises(TypeError) {
289
+ @rbtree["e"] = "E"
290
+ }
291
+ break
292
+ }
293
+ assert_equal(4, @rbtree.size)
294
+
295
+ if defined?(Enumerable::Enumerator)
296
+ enumerator = @rbtree.each_key
297
+ assert_equal(%w(a b c d), enumerator.map.flatten)
298
+ end
299
+ end
300
+
301
+ def test_each_value
302
+ ret = []
303
+ @rbtree.each_value {|val| ret.push(val) }
304
+ assert_equal(%w(A B C D), ret)
305
+
306
+ assert_raises(TypeError) {
307
+ @rbtree.each_value { @rbtree["e"] = "E" }
308
+ }
309
+ assert_equal(4, @rbtree.size)
310
+
311
+ @rbtree.each_value {
312
+ @rbtree.each_value {}
313
+ assert_raises(TypeError) {
314
+ @rbtree["e"] = "E"
315
+ }
316
+ break
317
+ }
318
+ assert_equal(4, @rbtree.size)
319
+
320
+ if defined?(Enumerable::Enumerator)
321
+ enumerator = @rbtree.each_value
322
+ assert_equal(%w(A B C D), enumerator.map.flatten)
323
+ end
324
+ end
325
+
326
+ def test_shift
327
+ ret = @rbtree.shift
328
+ assert_equal(3, @rbtree.size)
329
+ assert_equal(["a", "A"], ret)
330
+ assert_equal(nil, @rbtree["a"])
331
+
332
+ 3.times { @rbtree.shift }
333
+ assert_equal(0, @rbtree.size)
334
+ assert_equal(nil, @rbtree.shift)
335
+ @rbtree.default = "e"
336
+ assert_equal("e", @rbtree.shift)
337
+
338
+ rbtree = RBTree.new { "e" }
339
+ assert_equal("e", rbtree.shift)
340
+ end
341
+
342
+ def test_pop
343
+ ret = @rbtree.pop
344
+ assert_equal(3, @rbtree.size)
345
+ assert_equal(["d", "D"], ret)
346
+ assert_equal(nil, @rbtree["d"])
347
+
348
+ 3.times { @rbtree.pop }
349
+ assert_equal(0, @rbtree.size)
350
+ assert_equal(nil, @rbtree.pop)
351
+ @rbtree.default = "e"
352
+ assert_equal("e", @rbtree.pop)
353
+
354
+ rbtree = RBTree.new { "e" }
355
+ assert_equal("e", rbtree.pop)
356
+ end
357
+
358
+ def test_delete
359
+ ret = @rbtree.delete("c")
360
+ assert_equal("C", ret)
361
+ assert_equal(3, @rbtree.size)
362
+ assert_equal(nil, @rbtree["c"])
363
+
364
+ assert_equal(nil, @rbtree.delete("e"))
365
+ assert_equal("E", @rbtree.delete("e") { "E" })
366
+ end
367
+
368
+ def test_delete_if
369
+ @rbtree.delete_if {|key, val| val == "A" || val == "B" }
370
+ assert_equal(RBTree[*%w(c C d D)], @rbtree)
371
+
372
+ assert_raises(ArgumentError) {
373
+ @rbtree.delete_if {|key, val| key == "c" or raise ArgumentError }
374
+ }
375
+ assert_equal(2, @rbtree.size)
376
+
377
+ assert_raises(TypeError) {
378
+ @rbtree.delete_if { @rbtree["e"] = "E" }
379
+ }
380
+ assert_equal(2, @rbtree.size)
381
+
382
+ @rbtree.delete_if {
383
+ @rbtree.each {
384
+ assert_equal(2, @rbtree.size)
385
+ }
386
+ assert_raises(TypeError) {
387
+ @rbtree["e"] = "E"
388
+ }
389
+ true
390
+ }
391
+ assert_equal(0, @rbtree.size)
392
+
393
+ if defined?(Enumerable::Enumerator)
394
+ rbtree = RBTree[*%w(b B d D a A c C)]
395
+ enumerator = rbtree.delete_if
396
+ assert_equal([true, true, false, false], enumerator.map {|key, val| val == "A" || val == "B" })
397
+ end
398
+ end
399
+
400
+ def test_reject_bang
401
+ ret = @rbtree.reject! { false }
402
+ assert_equal(nil, ret)
403
+ assert_equal(4, @rbtree.size)
404
+
405
+ ret = @rbtree.reject! {|key, val| val == "A" || val == "B" }
406
+ assert_same(@rbtree, ret)
407
+ assert_equal(RBTree[*%w(c C d D)], ret)
408
+
409
+ if defined?(Enumerable::Enumerator)
410
+ rbtree = RBTree[*%w(b B d D a A c C)]
411
+ enumerator = rbtree.reject!
412
+ assert_equal([true, true, false, false], enumerator.map {|key, val| val == "A" || val == "B" })
413
+ end
414
+ end
415
+
416
+ def test_reject
417
+ ret = @rbtree.reject { false }
418
+ assert_equal(nil, ret)
419
+ assert_equal(4, @rbtree.size)
420
+
421
+ ret = @rbtree.reject {|key, val| val == "A" || val == "B" }
422
+ assert_equal(RBTree[*%w(c C d D)], ret)
423
+ assert_equal(4, @rbtree.size)
424
+
425
+ if defined?(Enumerable::Enumerator)
426
+ enumerator = @rbtree.reject
427
+ assert_equal([true, true, false, false], enumerator.map {|key, val| val == "A" || val == "B" })
428
+ end
429
+ end
430
+
431
+ def test_select
432
+ ret = @rbtree.select {|key, val| val == "A" || val == "B" }
433
+ assert_equal(%w(a A b B), ret.flatten)
434
+ assert_raises(ArgumentError) { @rbtree.select("c") }
435
+
436
+ if defined?(Enumerable::Enumerator)
437
+ enumerator = @rbtree.select
438
+ assert_equal([true, true, false, false], enumerator.map {|key, val| val == "A" || val == "B"})
439
+ end
440
+ end
441
+
442
+ def test_values_at
443
+ ret = @rbtree.values_at("d", "a", "e")
444
+ assert_equal(["D", "A", nil], ret)
445
+ end
446
+
447
+ def test_invert
448
+ assert_equal(RBTree[*%w(A a B b C c D d)], @rbtree.invert)
449
+ end
450
+
451
+ def test_update
452
+ rbtree = RBTree.new
453
+ rbtree["e"] = "E"
454
+ @rbtree.update(rbtree)
455
+ assert_equal(RBTree[*%w(a A b B c C d D e E)], @rbtree)
456
+
457
+ @rbtree.clear
458
+ @rbtree["d"] = "A"
459
+ rbtree.clear
460
+ rbtree["d"] = "B"
461
+
462
+ @rbtree.update(rbtree) {|key, val1, val2|
463
+ val1 + val2 if key == "d"
464
+ }
465
+ assert_equal(RBTree[*%w(d AB)], @rbtree)
466
+
467
+ assert_raises(TypeError) { @rbtree.update("e") }
468
+ end
469
+
470
+ def test_merge
471
+ rbtree = RBTree.new
472
+ rbtree["e"] = "E"
473
+
474
+ ret = @rbtree.merge(rbtree)
475
+ assert_equal(RBTree[*%w(a A b B c C d D e E)], ret)
476
+
477
+ assert_equal(4, @rbtree.size)
478
+ end
479
+
480
+ def test_has_key
481
+ assert_equal(true, @rbtree.has_key?("a"))
482
+ assert_equal(true, @rbtree.has_key?("b"))
483
+ assert_equal(true, @rbtree.has_key?("c"))
484
+ assert_equal(true, @rbtree.has_key?("d"))
485
+ assert_equal(false, @rbtree.has_key?("e"))
486
+ end
487
+
488
+ def test_has_value
489
+ assert_equal(true, @rbtree.has_value?("A"))
490
+ assert_equal(true, @rbtree.has_value?("B"))
491
+ assert_equal(true, @rbtree.has_value?("C"))
492
+ assert_equal(true, @rbtree.has_value?("D"))
493
+ assert_equal(false, @rbtree.has_value?("E"))
494
+ end
495
+
496
+ def test_keys
497
+ assert_equal(%w(a b c d), @rbtree.keys)
498
+ end
499
+
500
+ def test_values
501
+ assert_equal(%w(A B C D), @rbtree.values)
502
+ end
503
+
504
+ def test_to_a
505
+ assert_equal([%w(a A), %w(b B), %w(c C), %w(d D)], @rbtree.to_a)
506
+ end
507
+
508
+ def test_to_s
509
+ if RUBY_VERSION < "1.9"
510
+ assert_equal("aAbBcCdD", @rbtree.to_s)
511
+ else #Ruby 1.9 Array#to_s behaves differently
512
+ val = "[[\"a\", \"A\"], [\"b\", \"B\"], [\"c\", \"C\"], [\"d\", \"D\"]]"
513
+ assert_equal(val, @rbtree.to_s)
514
+ end
515
+ end
516
+
517
+ def test_to_hash
518
+ @rbtree.default = "e"
519
+ hash = @rbtree.to_hash
520
+ assert_equal(@rbtree.to_a.flatten, hash.to_a.flatten)
521
+ end
522
+
523
+ def test_to_rbtree
524
+ assert_same(@rbtree, @rbtree.to_rbtree)
525
+ end
526
+
527
+ def test_inspect
528
+ @rbtree.default = "e"
529
+ @rbtree.readjust {|a, b| a <=> b}
530
+ re = /#<RBTree: (\{.*\}), default=(.*), cmp_proc=(.*)>/
531
+
532
+ assert_match(re, @rbtree.inspect)
533
+ match = re.match(@rbtree.inspect)
534
+ tree, default, cmp_proc = match.to_a[1..-1]
535
+ assert_equal(%({"a"=>"A", "b"=>"B", "c"=>"C", "d"=>"D"}), tree)
536
+ assert_equal(%("e"), default)
537
+ assert_match(/#<Proc:\w+(@#{__FILE__}:\d+)?>/o, cmp_proc)
538
+
539
+ rbtree = RBTree.new
540
+ assert_match(re, rbtree.inspect)
541
+ match = re.match(rbtree.inspect)
542
+ tree, default, cmp_proc = match.to_a[1..-1]
543
+ assert_equal("{}", tree)
544
+ assert_equal("nil", default)
545
+ assert_equal("nil", cmp_proc)
546
+
547
+ rbtree = RBTree.new
548
+ rbtree["e"] = rbtree
549
+ assert_match(re, rbtree.inspect)
550
+ match = re.match(rbtree.inspect)
551
+ assert_equal(%({"e"=>#<RBTree: ...>}), match[1])
552
+ end
553
+
554
+ def test_lower_bound
555
+ rbtree = RBTree[*%w(a A c C e E)]
556
+ assert_equal(["c", "C"], rbtree.lower_bound("c"))
557
+ assert_equal(["c", "C"], rbtree.lower_bound("b"))
558
+ assert_equal(nil, rbtree.lower_bound("f"))
559
+ end
560
+
561
+ def test_upper_bound
562
+ rbtree = RBTree[*%w(a A c C e E)]
563
+ assert_equal(["c", "C"], rbtree.upper_bound("c"))
564
+ assert_equal(["c", "C"], rbtree.upper_bound("d"))
565
+ assert_equal(nil, rbtree.upper_bound("Z"))
566
+ end
567
+
568
+ def test_bound
569
+ rbtree = RBTree[*%w(a A c C e E)]
570
+ assert_equal(%w(a A c C), rbtree.bound("a", "c").flatten)
571
+ assert_equal(%w(a A), rbtree.bound("a").flatten)
572
+ assert_equal(%w(c C e E), rbtree.bound("b", "f").flatten)
573
+
574
+ assert_equal([], rbtree.bound("b", "b"))
575
+ assert_equal([], rbtree.bound("Y", "Z"))
576
+ assert_equal([], rbtree.bound("f", "g"))
577
+ assert_equal([], rbtree.bound("f", "Z"))
578
+ end
579
+
580
+ def test_bound_block
581
+ ret = []
582
+ @rbtree.bound("b", "c") {|key, val|
583
+ ret.push(key)
584
+ }
585
+ assert_equal(%w(b c), ret)
586
+
587
+ assert_raises(TypeError) {
588
+ @rbtree.bound("a", "d") {
589
+ @rbtree["e"] = "E"
590
+ }
591
+ }
592
+ assert_equal(4, @rbtree.size)
593
+
594
+ @rbtree.bound("b", "c") {
595
+ @rbtree.bound("b", "c") {}
596
+ assert_raises(TypeError) {
597
+ @rbtree["e"] = "E"
598
+ }
599
+ break
600
+ }
601
+ assert_equal(4, @rbtree.size)
602
+ end
603
+
604
+ def test_first
605
+ assert_equal(["a", "A"], @rbtree.first)
606
+
607
+ rbtree = RBTree.new("e")
608
+ assert_equal("e", rbtree.first)
609
+
610
+ rbtree = RBTree.new { "e" }
611
+ assert_equal("e", rbtree.first)
612
+ end
613
+
614
+ def test_last
615
+ assert_equal(["d", "D"], @rbtree.last)
616
+
617
+ rbtree = RBTree.new("e")
618
+ assert_equal("e", rbtree.last)
619
+
620
+ rbtree = RBTree.new { "e" }
621
+ assert_equal("e", rbtree.last)
622
+ end
623
+
624
+ def test_readjust
625
+ assert_equal(nil, @rbtree.cmp_proc)
626
+
627
+ @rbtree.readjust {|a, b| b <=> a }
628
+ assert_equal(%w(d c b a), @rbtree.keys)
629
+ assert_not_equal(nil, @rbtree.cmp_proc)
630
+
631
+ proc = Proc.new {|a,b| a.to_s <=> b.to_s }
632
+ @rbtree.readjust(proc)
633
+ assert_equal(%w(a b c d), @rbtree.keys)
634
+ assert_equal(proc, @rbtree.cmp_proc)
635
+
636
+ @rbtree[0] = nil
637
+ assert_raises(ArgumentError) { @rbtree.readjust(nil) }
638
+ assert_equal(5, @rbtree.size)
639
+ assert_equal(proc, @rbtree.cmp_proc)
640
+
641
+ @rbtree.delete(0)
642
+ @rbtree.readjust(nil)
643
+ assert_raises(ArgumentError) { @rbtree[0] = nil }
644
+
645
+
646
+ rbtree = RBTree.new
647
+ key = ["a"]
648
+ rbtree[key] = nil
649
+ rbtree[["e"]] = nil
650
+ key[0] = "f"
651
+
652
+ assert_equal([["f"], ["e"]], rbtree.keys)
653
+ rbtree.readjust
654
+ assert_equal([["e"], ["f"]], rbtree.keys)
655
+
656
+ assert_raises(ArgumentError) { @rbtree.readjust { "e" } }
657
+ assert_raises(TypeError) { @rbtree.readjust("e") }
658
+ assert_raises(ArgumentError) {
659
+ @rbtree.readjust(proc) {|a,b| a <=> b }
660
+ }
661
+ assert_raises(ArgumentError) { @rbtree.readjust(proc, proc) }
662
+ end
663
+
664
+ def test_replace
665
+ rbtree = RBTree.new { "e" }
666
+ rbtree.readjust {|a, b| a <=> b}
667
+ rbtree["a"] = "A"
668
+ rbtree["e"] = "E"
669
+
670
+ @rbtree.replace(rbtree)
671
+ assert_equal(%w(a A e E), @rbtree.to_a.flatten)
672
+ assert_equal(rbtree.default, @rbtree.default)
673
+ assert_equal(rbtree.cmp_proc, @rbtree.cmp_proc)
674
+
675
+ assert_raises(TypeError) { @rbtree.replace("e") }
676
+ end
677
+
678
+ def test_reverse_each
679
+ ret = []
680
+ @rbtree.reverse_each { |key, val| ret.push([key, val]) }
681
+ assert_equal(%w(d D c C b B a A), ret.flatten)
682
+
683
+ if defined?(Enumerable::Enumerator)
684
+ enumerator = @rbtree.reverse_each
685
+ assert_equal(%w(d D c C b B a A), enumerator.map.flatten)
686
+ end
687
+ end
688
+
689
+ def test_marshal
690
+ assert_equal(@rbtree, Marshal.load(Marshal.dump(@rbtree)))
691
+
692
+ @rbtree.default = "e"
693
+ assert_equal(@rbtree, Marshal.load(Marshal.dump(@rbtree)))
694
+
695
+ assert_raises(TypeError) {
696
+ Marshal.dump(RBTree.new { "e" })
697
+ }
698
+
699
+ assert_raises(TypeError) {
700
+ @rbtree.readjust {|a, b| a <=> b}
701
+ Marshal.dump(@rbtree)
702
+ }
703
+ end
704
+
705
+ begin
706
+ require "pp"
707
+
708
+ def test_pp
709
+ assert_equal(%(#<RBTree: {}, default=nil, cmp_proc=nil>\n),
710
+ PP.pp(RBTree[], ""))
711
+ assert_equal(%(#<RBTree: {"a"=>"A", "b"=>"B"}, default=nil, cmp_proc=nil>\n),
712
+ PP.pp(RBTree[*%w(a A b B)], ""))
713
+
714
+ rbtree = RBTree[*("a".."z").to_a]
715
+ rbtree.default = "a"
716
+ rbtree.readjust {|a, b| a <=> b }
717
+ expected = <<EOS
718
+ #<RBTree: {"a"=>"b",
719
+ "c"=>"d",
720
+ "e"=>"f",
721
+ "g"=>"h",
722
+ "i"=>"j",
723
+ "k"=>"l",
724
+ "m"=>"n",
725
+ "o"=>"p",
726
+ "q"=>"r",
727
+ "s"=>"t",
728
+ "u"=>"v",
729
+ "w"=>"x",
730
+ "y"=>"z"},
731
+ default="a",
732
+ cmp_proc=#{rbtree.cmp_proc}>
733
+ EOS
734
+ assert_equal(expected, PP.pp(rbtree, ""))
735
+
736
+ rbtree = RBTree[]
737
+ rbtree[rbtree] = rbtree
738
+ rbtree.default = rbtree
739
+ expected = <<EOS
740
+ #<RBTree: {"#<RBTree: ...>"=>"#<RBTree: ...>"},
741
+ default="#<RBTree: ...>",
742
+ cmp_proc=nil>
743
+ EOS
744
+ assert_equal(expected, PP.pp(rbtree, ""))
745
+ end
746
+ rescue LoadError
747
+ end
748
+ end
749
+
750
+
751
+ class MultiRBTreeTest < Test::Unit::TestCase
752
+ def setup
753
+ @rbtree = MultiRBTree[*%w(a A b B b C b D c C)]
754
+ end
755
+
756
+ def test_create
757
+ assert_equal(%w(a A b B b C b D c C), @rbtree.to_a.flatten)
758
+
759
+ assert_equal(MultiRBTree[*%w(a A)], MultiRBTree[RBTree[*%w(a A)]])
760
+ assert_raises(TypeError) {
761
+ RBTree[MultiRBTree[*%w(a A)]]
762
+ }
763
+ end
764
+
765
+ def test_size
766
+ assert_equal(5, @rbtree.size)
767
+ end
768
+
769
+ def test_clear
770
+ @rbtree.clear
771
+ assert_equal(0, @rbtree.size)
772
+ end
773
+
774
+ def test_empty
775
+ assert_equal(false, @rbtree.empty?)
776
+ @rbtree.clear
777
+ assert_equal(true, @rbtree.empty?)
778
+ end
779
+
780
+ def test_to_a
781
+ assert_equal([%w(a A), %w(b B), %w(b C), %w(b D), %w(c C)],
782
+ @rbtree.to_a)
783
+ end
784
+
785
+ def test_to_s
786
+ if RUBY_VERSION < "1.9"
787
+ assert_equal("aAbBbCbDcC", @rbtree.to_s)
788
+ else
789
+ val = "[[\"a\", \"A\"], [\"b\", \"B\"], [\"b\", \"C\"], \
790
+ [\"b\", \"D\"], [\"c\", \"C\"]]"
791
+ assert_equal(val, @rbtree.to_s)
792
+ end
793
+ end
794
+
795
+ def test_to_hash
796
+ assert_raises(TypeError) {
797
+ @rbtree.to_hash
798
+ }
799
+ end
800
+
801
+ def test_to_rbtree
802
+ assert_equal(@rbtree, @rbtree.to_rbtree)
803
+ end
804
+
805
+ def test_aref
806
+ assert_equal("B", @rbtree["b"])
807
+ end
808
+
809
+ def test_aset
810
+ @rbtree["b"] = "A"
811
+ assert_equal("B", @rbtree["b"])
812
+ assert_equal(%w(a A b B b C b D b A c C), @rbtree.to_a.flatten)
813
+ end
814
+
815
+ def test_equal
816
+ assert_equal(true, MultiRBTree[*%w(a A b B b C b D c C)] == @rbtree)
817
+ assert_equal(true, RBTree[*%w(a A)] == MultiRBTree[*%w(a A)])
818
+ assert_equal(true, MultiRBTree[*%w(a A)] == RBTree[*%w(a A)])
819
+ end
820
+
821
+ def test_replace
822
+ assert_equal(RBTree[*%w(a A)],
823
+ MultiRBTree[*%w(a A)].replace(RBTree[*%w(a A)]))
824
+ assert_raises(TypeError) {
825
+ RBTree[*%w(a A)].replace(MultiRBTree[*%w(a A)])
826
+ }
827
+ end
828
+
829
+ def test_update
830
+ assert_equal(MultiRBTree[*%w(a A b B)],
831
+ MultiRBTree[*%w(a A)].update(RBTree[*%w(b B)]))
832
+ assert_raises(TypeError) {
833
+ RBTree[*%w(a A)].update(MultiRBTree[*%w(b B)])
834
+ }
835
+ end
836
+
837
+ def test_clone
838
+ assert_equal(@rbtree, @rbtree.clone)
839
+ end
840
+
841
+ def test_each
842
+ ret = []
843
+ @rbtree.each {|k, v|
844
+ ret << k << v
845
+ }
846
+ assert_equal(%w(a A b B b C b D c C), ret)
847
+ end
848
+
849
+ def test_delete
850
+ @rbtree.delete("b")
851
+ assert_equal(4, @rbtree.size)
852
+ assert_equal(%w(a A b C b D c C), @rbtree.to_a.flatten)
853
+
854
+ @rbtree.delete("b")
855
+ assert_equal(3, @rbtree.size)
856
+ assert_equal(%w(a A b D c C), @rbtree.to_a.flatten)
857
+
858
+ @rbtree.delete("b")
859
+ assert_equal(2, @rbtree.size)
860
+ assert_equal(%w(a A c C), @rbtree.to_a.flatten)
861
+ end
862
+
863
+ def test_delete_if
864
+ @rbtree.delete_if {|k, v| k == "b" }
865
+ assert_equal(%w(a A c C), @rbtree.to_a.flatten)
866
+ end
867
+
868
+ def test_inspect
869
+ assert_equal(%(#<MultiRBTree: {"a"=>"A", "b"=>"B", "b"=>"C", "b"=>"D", "c"=>"C"}, default=nil, cmp_proc=nil>),
870
+ @rbtree.inspect)
871
+ end
872
+
873
+ def test_readjust
874
+ @rbtree.readjust {|a, b| b <=> a }
875
+ assert_equal(%w(c C b B b C b D a A), @rbtree.to_a.flatten)
876
+ end
877
+
878
+ def test_marshal
879
+ assert_equal(@rbtree, Marshal.load(Marshal.dump(@rbtree)))
880
+ end
881
+
882
+ def test_lower_bound
883
+ assert_equal(%w(b B), @rbtree.lower_bound("b"))
884
+ end
885
+
886
+ def test_upper_bound
887
+ assert_equal(%w(b D), @rbtree.upper_bound("b"))
888
+ end
889
+
890
+ def test_bound
891
+ assert_equal(%w(b B b C b D), @rbtree.bound("b").flatten)
892
+ end
893
+
894
+ def test_first
895
+ assert_equal(%w(a A), @rbtree.first)
896
+ end
897
+
898
+ def test_last
899
+ assert_equal(%w(c C), @rbtree.last)
900
+ end
901
+
902
+ def test_shift
903
+ assert_equal(%w(a A), @rbtree.shift)
904
+ assert_equal(4, @rbtree.size)
905
+ assert_equal(nil, @rbtree["a"])
906
+ end
907
+
908
+ def test_pop
909
+ assert_equal(%w(c C), @rbtree.pop)
910
+ assert_equal(4, @rbtree.size)
911
+ assert_equal(nil, @rbtree["c"])
912
+ end
913
+
914
+ def test_has_key
915
+ assert_equal(true, @rbtree.has_key?("b"))
916
+ end
917
+
918
+ def test_has_value
919
+ assert_equal(true, @rbtree.has_value?("B"))
920
+ assert_equal(true, @rbtree.has_value?("C"))
921
+ assert_equal(true, @rbtree.has_value?("D"))
922
+ end
923
+
924
+ def test_select
925
+ assert_equal(%w(b B b C b D), @rbtree.select {|k, v| k == "b"}.flatten)
926
+ assert_equal(%w(b C c C), @rbtree.select {|k, v| v == "C"}.flatten)
927
+ end
928
+
929
+ def test_values_at
930
+ assert_equal(%w(A B), @rbtree.values_at("a", "b"))
931
+ end
932
+
933
+ def test_invert
934
+ assert_equal(MultiRBTree[*%w(A a B b C b C c D b)], @rbtree.invert)
935
+ end
936
+
937
+ def test_keys
938
+ assert_equal(%w(a b b b c), @rbtree.keys)
939
+ end
940
+
941
+ def test_values
942
+ assert_equal(%w(A B C D C), @rbtree.values)
943
+ end
944
+
945
+ def test_index
946
+ assert_equal("b", @rbtree.index("B"))
947
+ assert_equal("b", @rbtree.index("C"))
948
+ assert_equal("b", @rbtree.index("D"))
949
+ end
950
+ end