rbtree-pure 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.project +17 -0
- data/Gemfile +11 -0
- data/Gemfile.lock +20 -0
- data/LICENSE.txt +20 -0
- data/README.rdoc +17 -0
- data/Rakefile +57 -0
- data/VERSION +1 -0
- data/lib/rbtree.rb +10 -0
- data/lib/rbtree/guard_node.rb +28 -0
- data/lib/rbtree/multi_rb_tree.rb +317 -0
- data/lib/rbtree/node.rb +59 -0
- data/lib/rbtree/rb_tree.rb +485 -0
- data/lib/rbtree/tree.rb +382 -0
- data/lib/rbtree/tree_cmp.rb +92 -0
- data/old_ext/dict.c +1216 -0
- data/old_ext/dict.h +123 -0
- data/old_ext/rbtree.c +1706 -0
- data/test/helper.rb +17 -0
- data/test/multi_rbtree_test.rb +226 -0
- data/test/rbtree_test.rb +791 -0
- metadata +145 -0
data/test/helper.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
begin
|
4
|
+
Bundler.setup(:default, :development)
|
5
|
+
rescue Bundler::BundlerError => e
|
6
|
+
$stderr.puts e.message
|
7
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
8
|
+
exit e.status_code
|
9
|
+
end
|
10
|
+
require 'test/unit'
|
11
|
+
|
12
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
13
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
14
|
+
require 'rbtree'
|
15
|
+
|
16
|
+
class Test::Unit::TestCase
|
17
|
+
end
|
@@ -0,0 +1,226 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
# The code below is lifted from the rbtree gem. Here is its LICENSE.
|
4
|
+
#
|
5
|
+
# Copyright (c) 2002-2004, 2007, 2009-2010 OZAWA Takuma
|
6
|
+
#
|
7
|
+
# Permission is hereby granted, free of charge, to any person
|
8
|
+
# obtaining a copy of this software and associated documentation
|
9
|
+
# files (the "Software"), to deal in the Software without
|
10
|
+
# restriction, including without limitation the rights to use,
|
11
|
+
# copy, modify, merge, publish, distribute, sublicense, and/or sell
|
12
|
+
# copies of the Software, and to permit persons to whom the
|
13
|
+
# Software is furnished to do so, subject to the following
|
14
|
+
# conditions:
|
15
|
+
#
|
16
|
+
# The above copyright notice and this permission notice shall be
|
17
|
+
# included in all copies or substantial portions of the Software.
|
18
|
+
#
|
19
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
20
|
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
21
|
+
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
22
|
+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
23
|
+
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
24
|
+
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
25
|
+
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
26
|
+
# OTHER DEALINGS IN THE SOFTWARE.
|
27
|
+
|
28
|
+
class MultiRBTreeTest < Test::Unit::TestCase
|
29
|
+
def setup
|
30
|
+
@rbtree = MultiRBTree[*%w(a A b B b C b D c C)]
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_create
|
34
|
+
assert_equal(%w(a A b B b C b D c C), @rbtree.to_a.flatten)
|
35
|
+
|
36
|
+
assert_equal(MultiRBTree[*%w(a A)], MultiRBTree[RBTree[*%w(a A)]])
|
37
|
+
assert_raises(TypeError) {
|
38
|
+
RBTree[MultiRBTree[*%w(a A)]]
|
39
|
+
}
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_size
|
43
|
+
assert_equal(5, @rbtree.size)
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_clear
|
47
|
+
@rbtree.clear
|
48
|
+
assert_equal(0, @rbtree.size)
|
49
|
+
end
|
50
|
+
|
51
|
+
def test_empty
|
52
|
+
assert_equal(false, @rbtree.empty?)
|
53
|
+
@rbtree.clear
|
54
|
+
assert_equal(true, @rbtree.empty?)
|
55
|
+
end
|
56
|
+
|
57
|
+
def test_to_a
|
58
|
+
assert_equal([%w(a A), %w(b B), %w(b C), %w(b D), %w(c C)],
|
59
|
+
@rbtree.to_a)
|
60
|
+
end
|
61
|
+
|
62
|
+
def test_to_s
|
63
|
+
if RUBY_VERSION < "1.9"
|
64
|
+
assert_equal("aAbBbCbDcC", @rbtree.to_s)
|
65
|
+
else
|
66
|
+
expected = "[[\"a\", \"A\"], [\"b\", \"B\"], [\"b\", \"C\"], \[\"b\", \"D\"], [\"c\", \"C\"]]"
|
67
|
+
assert_equal(expected, @rbtree.to_s)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
def test_to_hash
|
72
|
+
assert_raises(TypeError) {
|
73
|
+
@rbtree.to_hash
|
74
|
+
}
|
75
|
+
end
|
76
|
+
|
77
|
+
def test_to_rbtree
|
78
|
+
assert_equal(@rbtree, @rbtree.to_rbtree)
|
79
|
+
end
|
80
|
+
|
81
|
+
def test_aref
|
82
|
+
assert_equal("B", @rbtree["b"])
|
83
|
+
end
|
84
|
+
|
85
|
+
def test_aset
|
86
|
+
@rbtree["b"] = "A"
|
87
|
+
assert_equal("B", @rbtree["b"])
|
88
|
+
assert_equal(%w(a A b B b C b D b A c C), @rbtree.to_a.flatten)
|
89
|
+
end
|
90
|
+
|
91
|
+
def test_equal
|
92
|
+
assert_equal(true, MultiRBTree[*%w(a A b B b C b D c C)] == @rbtree)
|
93
|
+
assert_equal(true, RBTree[*%w(a A)] == MultiRBTree[*%w(a A)])
|
94
|
+
assert_equal(true, MultiRBTree[*%w(a A)] == RBTree[*%w(a A)])
|
95
|
+
end
|
96
|
+
|
97
|
+
def test_replace
|
98
|
+
assert_equal(RBTree[*%w(a A)],
|
99
|
+
MultiRBTree[*%w(a A)].replace(RBTree[*%w(a A)]))
|
100
|
+
assert_raises(TypeError) {
|
101
|
+
RBTree[*%w(a A)].replace(MultiRBTree[*%w(a A)])
|
102
|
+
}
|
103
|
+
end
|
104
|
+
|
105
|
+
def test_update
|
106
|
+
assert_equal(MultiRBTree[*%w(a A b B)],
|
107
|
+
MultiRBTree[*%w(a A)].update(RBTree[*%w(b B)]))
|
108
|
+
assert_raises(TypeError) {
|
109
|
+
RBTree[*%w(a A)].update(MultiRBTree[*%w(b B)])
|
110
|
+
}
|
111
|
+
end
|
112
|
+
|
113
|
+
def test_clone
|
114
|
+
assert_equal(@rbtree, @rbtree.clone)
|
115
|
+
end
|
116
|
+
|
117
|
+
def test_each
|
118
|
+
ret = []
|
119
|
+
@rbtree.each {|k, v|
|
120
|
+
ret << k << v
|
121
|
+
}
|
122
|
+
assert_equal(%w(a A b B b C b D c C), ret)
|
123
|
+
end
|
124
|
+
|
125
|
+
def test_delete
|
126
|
+
@rbtree.delete("b")
|
127
|
+
assert_equal(4, @rbtree.size)
|
128
|
+
assert_equal(%w(a A b C b D c C), @rbtree.to_a.flatten)
|
129
|
+
|
130
|
+
@rbtree.delete("b")
|
131
|
+
assert_equal(3, @rbtree.size)
|
132
|
+
assert_equal(%w(a A b D c C), @rbtree.to_a.flatten)
|
133
|
+
|
134
|
+
@rbtree.delete("b")
|
135
|
+
assert_equal(2, @rbtree.size)
|
136
|
+
assert_equal(%w(a A c C), @rbtree.to_a.flatten)
|
137
|
+
end
|
138
|
+
|
139
|
+
def test_delete_if
|
140
|
+
@rbtree.delete_if {|k, v| k == "b" }
|
141
|
+
assert_equal(%w(a A c C), @rbtree.to_a.flatten)
|
142
|
+
end
|
143
|
+
|
144
|
+
def test_inspect
|
145
|
+
assert_equal(%(#<MultiRBTree: {"a"=>"A", "b"=>"B", "b"=>"C", "b"=>"D", "c"=>"C"}, default=nil, cmp_proc=nil>),
|
146
|
+
@rbtree.inspect)
|
147
|
+
end
|
148
|
+
|
149
|
+
def test_readjust
|
150
|
+
@rbtree.readjust {|a, b| b <=> a }
|
151
|
+
assert_equal(%w(c C b B b C b D a A), @rbtree.to_a.flatten)
|
152
|
+
end
|
153
|
+
|
154
|
+
def test_marshal
|
155
|
+
assert_equal(@rbtree, Marshal.load(Marshal.dump(@rbtree)))
|
156
|
+
end
|
157
|
+
|
158
|
+
def test_lower_bound
|
159
|
+
assert_equal(%w(b B), @rbtree.lower_bound("b"))
|
160
|
+
end
|
161
|
+
|
162
|
+
def test_upper_bound
|
163
|
+
assert_equal(%w(b D), @rbtree.upper_bound("b"))
|
164
|
+
end
|
165
|
+
|
166
|
+
def test_bound
|
167
|
+
assert_equal(%w(b B b C b D), @rbtree.bound("b").flatten)
|
168
|
+
end
|
169
|
+
|
170
|
+
def test_first
|
171
|
+
assert_equal(%w(a A), @rbtree.first)
|
172
|
+
end
|
173
|
+
|
174
|
+
def test_last
|
175
|
+
assert_equal(%w(c C), @rbtree.last)
|
176
|
+
end
|
177
|
+
|
178
|
+
def test_shift
|
179
|
+
assert_equal(%w(a A), @rbtree.shift)
|
180
|
+
assert_equal(4, @rbtree.size)
|
181
|
+
assert_equal(nil, @rbtree["a"])
|
182
|
+
end
|
183
|
+
|
184
|
+
def test_pop
|
185
|
+
assert_equal(%w(c C), @rbtree.pop)
|
186
|
+
assert_equal(4, @rbtree.size)
|
187
|
+
assert_equal(nil, @rbtree["c"])
|
188
|
+
end
|
189
|
+
|
190
|
+
def test_has_key
|
191
|
+
assert_equal(true, @rbtree.has_key?("b"))
|
192
|
+
end
|
193
|
+
|
194
|
+
def test_has_value
|
195
|
+
assert_equal(true, @rbtree.has_value?("B"))
|
196
|
+
assert_equal(true, @rbtree.has_value?("C"))
|
197
|
+
assert_equal(true, @rbtree.has_value?("D"))
|
198
|
+
end
|
199
|
+
|
200
|
+
def test_select
|
201
|
+
assert_equal(%w(b B b C b D), @rbtree.select {|k, v| k == "b"}.flatten)
|
202
|
+
assert_equal(%w(b C c C), @rbtree.select {|k, v| v == "C"}.flatten)
|
203
|
+
end
|
204
|
+
|
205
|
+
def test_values_at
|
206
|
+
assert_equal(%w(A B), @rbtree.values_at("a", "b"))
|
207
|
+
end
|
208
|
+
|
209
|
+
def test_invert
|
210
|
+
assert_equal(MultiRBTree[*%w(A a B b C b C c D b)], @rbtree.invert)
|
211
|
+
end
|
212
|
+
|
213
|
+
def test_keys
|
214
|
+
assert_equal(%w(a b b b c), @rbtree.keys)
|
215
|
+
end
|
216
|
+
|
217
|
+
def test_values
|
218
|
+
assert_equal(%w(A B C D C), @rbtree.values)
|
219
|
+
end
|
220
|
+
|
221
|
+
def test_index
|
222
|
+
assert_equal("b", @rbtree.index("B"))
|
223
|
+
assert_equal("b", @rbtree.index("C"))
|
224
|
+
assert_equal("b", @rbtree.index("D"))
|
225
|
+
end
|
226
|
+
end
|
data/test/rbtree_test.rb
ADDED
@@ -0,0 +1,791 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
# The code below is lifted from the rbtree gem. Here is its LICENSE.
|
4
|
+
#
|
5
|
+
# Copyright (c) 2002-2004, 2007, 2009-2010 OZAWA Takuma
|
6
|
+
#
|
7
|
+
# Permission is hereby granted, free of charge, to any person
|
8
|
+
# obtaining a copy of this software and associated documentation
|
9
|
+
# files (the "Software"), to deal in the Software without
|
10
|
+
# restriction, including without limitation the rights to use,
|
11
|
+
# copy, modify, merge, publish, distribute, sublicense, and/or sell
|
12
|
+
# copies of the Software, and to permit persons to whom the
|
13
|
+
# Software is furnished to do so, subject to the following
|
14
|
+
# conditions:
|
15
|
+
#
|
16
|
+
# The above copyright notice and this permission notice shall be
|
17
|
+
# included in all copies or substantial portions of the Software.
|
18
|
+
#
|
19
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
20
|
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
21
|
+
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
22
|
+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
23
|
+
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
24
|
+
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
25
|
+
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
26
|
+
# OTHER DEALINGS IN THE SOFTWARE.
|
27
|
+
|
28
|
+
class RBTreeTest < Test::Unit::TestCase
|
29
|
+
def setup
|
30
|
+
@rbtree = RBTree[*%w(b B d D a A c C)]
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_new
|
34
|
+
assert_nothing_raised {
|
35
|
+
RBTree.new
|
36
|
+
RBTree.new("a")
|
37
|
+
RBTree.new { "a" }
|
38
|
+
}
|
39
|
+
assert_raises(ArgumentError) { RBTree.new("a") {} }
|
40
|
+
assert_raises(ArgumentError) { RBTree.new("a", "a") }
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_aref
|
44
|
+
assert_equal("A", @rbtree["a"])
|
45
|
+
assert_equal("B", @rbtree["b"])
|
46
|
+
assert_equal("C", @rbtree["c"])
|
47
|
+
assert_equal("D", @rbtree["d"])
|
48
|
+
|
49
|
+
assert_equal(nil, @rbtree["e"])
|
50
|
+
@rbtree.default = "E"
|
51
|
+
assert_equal("E", @rbtree["e"])
|
52
|
+
end
|
53
|
+
|
54
|
+
def test_size
|
55
|
+
assert_equal(4, @rbtree.size)
|
56
|
+
end
|
57
|
+
|
58
|
+
def test_create
|
59
|
+
rbtree = RBTree[]
|
60
|
+
assert_equal(0, rbtree.size)
|
61
|
+
|
62
|
+
rbtree = RBTree[@rbtree]
|
63
|
+
assert_equal(4, rbtree.size)
|
64
|
+
assert_equal("A", @rbtree["a"])
|
65
|
+
assert_equal("B", @rbtree["b"])
|
66
|
+
assert_equal("C", @rbtree["c"])
|
67
|
+
assert_equal("D", @rbtree["d"])
|
68
|
+
|
69
|
+
rbtree = RBTree[RBTree.new("e")]
|
70
|
+
assert_equal(nil, rbtree.default)
|
71
|
+
rbtree = RBTree[RBTree.new { "e" }]
|
72
|
+
assert_equal(nil, rbtree.default_proc)
|
73
|
+
@rbtree.readjust {|a,b| b <=> a }
|
74
|
+
assert_equal(nil, RBTree[@rbtree].cmp_proc)
|
75
|
+
|
76
|
+
assert_raises(ArgumentError) { RBTree["e"] }
|
77
|
+
|
78
|
+
rbtree = RBTree[Hash[*%w(b B d D a A c C)]]
|
79
|
+
assert_equal(4, rbtree.size)
|
80
|
+
assert_equal("A", rbtree["a"])
|
81
|
+
assert_equal("B", rbtree["b"])
|
82
|
+
assert_equal("C", rbtree["c"])
|
83
|
+
assert_equal("D", rbtree["d"])
|
84
|
+
|
85
|
+
rbtree = RBTree[[%w(a A), %w(b B), %w(c C), %w(d D)]];
|
86
|
+
assert_equal(4, rbtree.size)
|
87
|
+
assert_equal("A", rbtree["a"])
|
88
|
+
assert_equal("B", rbtree["b"])
|
89
|
+
assert_equal("C", rbtree["c"])
|
90
|
+
assert_equal("D", rbtree["d"])
|
91
|
+
|
92
|
+
rbtree = RBTree[[["a"]]]
|
93
|
+
assert_equal(1, rbtree.size)
|
94
|
+
assert_equal(nil, rbtree["a"])
|
95
|
+
end
|
96
|
+
|
97
|
+
def test_clear
|
98
|
+
@rbtree.clear
|
99
|
+
assert_equal(0, @rbtree.size)
|
100
|
+
end
|
101
|
+
|
102
|
+
def test_aset
|
103
|
+
@rbtree["e"] = "E"
|
104
|
+
assert_equal(5, @rbtree.size)
|
105
|
+
assert_equal("E", @rbtree["e"])
|
106
|
+
|
107
|
+
@rbtree["c"] = "E"
|
108
|
+
assert_equal(5, @rbtree.size)
|
109
|
+
assert_equal("E", @rbtree["c"])
|
110
|
+
|
111
|
+
assert_raises(ArgumentError) { @rbtree[100] = 100 }
|
112
|
+
assert_equal(5, @rbtree.size)
|
113
|
+
|
114
|
+
|
115
|
+
key = "f"
|
116
|
+
@rbtree[key] = "F"
|
117
|
+
cloned_key = @rbtree.last[0]
|
118
|
+
assert_equal("f", cloned_key)
|
119
|
+
assert_not_same(key, cloned_key)
|
120
|
+
assert_equal(true, cloned_key.frozen?)
|
121
|
+
|
122
|
+
@rbtree["f"] = "F"
|
123
|
+
assert_same(cloned_key, @rbtree.last[0])
|
124
|
+
|
125
|
+
rbtree = RBTree.new
|
126
|
+
key = ["g"]
|
127
|
+
rbtree[key] = "G"
|
128
|
+
assert_same(key, rbtree.first[0])
|
129
|
+
assert_equal(false, key.frozen?)
|
130
|
+
end
|
131
|
+
|
132
|
+
def test_clone
|
133
|
+
clone = @rbtree.clone
|
134
|
+
assert_equal(4, @rbtree.size)
|
135
|
+
assert_equal("A", @rbtree["a"])
|
136
|
+
assert_equal("B", @rbtree["b"])
|
137
|
+
assert_equal("C", @rbtree["c"])
|
138
|
+
assert_equal("D", @rbtree["d"])
|
139
|
+
|
140
|
+
rbtree = RBTree.new("e")
|
141
|
+
clone = rbtree.clone
|
142
|
+
assert_equal("e", clone.default)
|
143
|
+
|
144
|
+
rbtree = RBTree.new { "e" }
|
145
|
+
clone = rbtree.clone
|
146
|
+
assert_equal("e", clone.default(nil))
|
147
|
+
|
148
|
+
rbtree = RBTree.new
|
149
|
+
rbtree.readjust {|a, b| a <=> b }
|
150
|
+
clone = rbtree.clone
|
151
|
+
assert_equal(rbtree.cmp_proc, clone.cmp_proc)
|
152
|
+
end
|
153
|
+
|
154
|
+
def test_default
|
155
|
+
assert_equal(nil, @rbtree.default)
|
156
|
+
|
157
|
+
rbtree = RBTree.new("e")
|
158
|
+
assert_equal("e", rbtree.default)
|
159
|
+
assert_equal("e", rbtree.default("f"))
|
160
|
+
assert_raises(ArgumentError) { rbtree.default("e", "f") }
|
161
|
+
|
162
|
+
rbtree = RBTree.new {|tree, key| @rbtree[key || "c"] }
|
163
|
+
assert_equal("C", rbtree.default(nil))
|
164
|
+
assert_equal("B", rbtree.default("b"))
|
165
|
+
end
|
166
|
+
|
167
|
+
def test_set_default
|
168
|
+
rbtree = RBTree.new { "e" }
|
169
|
+
rbtree.default = "f"
|
170
|
+
assert_equal("f", rbtree.default)
|
171
|
+
end
|
172
|
+
|
173
|
+
def test_default_proc
|
174
|
+
rbtree = RBTree.new("e")
|
175
|
+
assert_equal(nil, rbtree.default_proc)
|
176
|
+
|
177
|
+
rbtree = RBTree.new { "e" }
|
178
|
+
assert_equal("e", rbtree.default_proc.call)
|
179
|
+
end
|
180
|
+
|
181
|
+
def test_equal
|
182
|
+
assert_equal(RBTree.new, RBTree.new)
|
183
|
+
assert_equal(@rbtree, @rbtree)
|
184
|
+
assert_not_equal(@rbtree, RBTree.new)
|
185
|
+
|
186
|
+
rbtree = RBTree[*%w(b B d D a A c C)]
|
187
|
+
assert_equal(@rbtree, rbtree)
|
188
|
+
rbtree["d"] = "A"
|
189
|
+
assert_not_equal(@rbtree, rbtree)
|
190
|
+
rbtree["d"] = "D"
|
191
|
+
rbtree["e"] = "E"
|
192
|
+
assert_not_equal(@rbtree, rbtree)
|
193
|
+
@rbtree["e"] = "E"
|
194
|
+
assert_equal(@rbtree, rbtree)
|
195
|
+
|
196
|
+
rbtree.default = "e"
|
197
|
+
assert_equal(@rbtree, rbtree)
|
198
|
+
@rbtree.default = "f"
|
199
|
+
assert_equal(@rbtree, rbtree)
|
200
|
+
|
201
|
+
a = RBTree.new("e")
|
202
|
+
b = RBTree.new { "f" }
|
203
|
+
assert_equal(a, b)
|
204
|
+
assert_equal(b, b.clone)
|
205
|
+
|
206
|
+
a = RBTree.new
|
207
|
+
b = RBTree.new
|
208
|
+
a.readjust {|x, y| x <=> y }
|
209
|
+
assert_not_equal(a, b)
|
210
|
+
b.readjust(a.cmp_proc)
|
211
|
+
assert_equal(a, b)
|
212
|
+
end
|
213
|
+
|
214
|
+
def test_fetch
|
215
|
+
assert_equal("A", @rbtree.fetch("a"))
|
216
|
+
assert_equal("B", @rbtree.fetch("b"))
|
217
|
+
assert_equal("C", @rbtree.fetch("c"))
|
218
|
+
assert_equal("D", @rbtree.fetch("d"))
|
219
|
+
|
220
|
+
assert_raises(IndexError) { @rbtree.fetch("e") }
|
221
|
+
|
222
|
+
assert_equal("E", @rbtree.fetch("e", "E"))
|
223
|
+
assert_equal("E", @rbtree.fetch("e") { "E" })
|
224
|
+
|
225
|
+
class << (stderr = "")
|
226
|
+
alias write <<
|
227
|
+
end
|
228
|
+
$stderr, stderr, $VERBOSE, verbose = stderr, $stderr, false, $VERBOSE
|
229
|
+
begin
|
230
|
+
assert_equal("E", @rbtree.fetch("e", "F") { "E" })
|
231
|
+
ensure
|
232
|
+
$stderr, stderr, $VERBOSE, verbose = stderr, $stderr, false, $VERBOSE
|
233
|
+
end
|
234
|
+
assert_match(/warning: block supersedes default value argument/, stderr)
|
235
|
+
|
236
|
+
assert_raises(ArgumentError) { @rbtree.fetch }
|
237
|
+
assert_raises(ArgumentError) { @rbtree.fetch("e", "E", "E") }
|
238
|
+
end
|
239
|
+
|
240
|
+
def test_index
|
241
|
+
assert_equal("a", @rbtree.index("A"))
|
242
|
+
assert_equal(nil, @rbtree.index("E"))
|
243
|
+
end
|
244
|
+
|
245
|
+
def test_empty_p
|
246
|
+
assert_equal(false, @rbtree.empty?)
|
247
|
+
@rbtree.clear
|
248
|
+
assert_equal(true, @rbtree.empty?)
|
249
|
+
end
|
250
|
+
|
251
|
+
def test_each
|
252
|
+
ret = []
|
253
|
+
@rbtree.each {|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 { @rbtree["e"] = "E" }
|
258
|
+
}
|
259
|
+
assert_equal(4, @rbtree.size)
|
260
|
+
|
261
|
+
@rbtree.each {
|
262
|
+
@rbtree.each {}
|
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
|
272
|
+
assert_equal(%w(a A b B c C d D), enumerator.map.flatten)
|
273
|
+
end
|
274
|
+
end
|
275
|
+
|
276
|
+
def test_each_pair
|
277
|
+
ret = []
|
278
|
+
@rbtree.each_pair {|key, val| ret << key << val }
|
279
|
+
assert_equal(%w(a A b B c C d D), ret)
|
280
|
+
|
281
|
+
assert_raises(TypeError) {
|
282
|
+
@rbtree.each_pair { @rbtree["e"] = "E" }
|
283
|
+
}
|
284
|
+
assert_equal(4, @rbtree.size)
|
285
|
+
|
286
|
+
@rbtree.each_pair {
|
287
|
+
@rbtree.each_pair {}
|
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_pair
|
297
|
+
assert_equal(%w(a A b B c C d D), enumerator.map.flatten)
|
298
|
+
end
|
299
|
+
end
|
300
|
+
|
301
|
+
def test_each_key
|
302
|
+
ret = []
|
303
|
+
@rbtree.each_key {|key| ret.push(key) }
|
304
|
+
assert_equal(%w(a b c d), ret)
|
305
|
+
|
306
|
+
assert_raises(TypeError) {
|
307
|
+
@rbtree.each_key { @rbtree["e"] = "E" }
|
308
|
+
}
|
309
|
+
assert_equal(4, @rbtree.size)
|
310
|
+
|
311
|
+
@rbtree.each_key {
|
312
|
+
@rbtree.each_key {}
|
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_key
|
322
|
+
assert_equal(%w(a b c d), enumerator.map.flatten)
|
323
|
+
end
|
324
|
+
end
|
325
|
+
|
326
|
+
def test_each_value
|
327
|
+
ret = []
|
328
|
+
@rbtree.each_value {|val| ret.push(val) }
|
329
|
+
assert_equal(%w(A B C D), ret)
|
330
|
+
|
331
|
+
assert_raises(TypeError) {
|
332
|
+
@rbtree.each_value { @rbtree["e"] = "E" }
|
333
|
+
}
|
334
|
+
assert_equal(4, @rbtree.size)
|
335
|
+
|
336
|
+
@rbtree.each_value {
|
337
|
+
@rbtree.each_value {}
|
338
|
+
assert_raises(TypeError) {
|
339
|
+
@rbtree["e"] = "E"
|
340
|
+
}
|
341
|
+
break
|
342
|
+
}
|
343
|
+
assert_equal(4, @rbtree.size)
|
344
|
+
|
345
|
+
if defined?(Enumerable::Enumerator)
|
346
|
+
enumerator = @rbtree.each_value
|
347
|
+
assert_equal(%w(A B C D), enumerator.map.flatten)
|
348
|
+
end
|
349
|
+
end
|
350
|
+
|
351
|
+
def test_shift
|
352
|
+
ret = @rbtree.shift
|
353
|
+
assert_equal(3, @rbtree.size)
|
354
|
+
assert_equal(["a", "A"], ret)
|
355
|
+
assert_equal(nil, @rbtree["a"])
|
356
|
+
|
357
|
+
3.times { @rbtree.shift }
|
358
|
+
assert_equal(0, @rbtree.size)
|
359
|
+
assert_equal(nil, @rbtree.shift)
|
360
|
+
@rbtree.default = "e"
|
361
|
+
assert_equal("e", @rbtree.shift)
|
362
|
+
|
363
|
+
rbtree = RBTree.new { "e" }
|
364
|
+
assert_equal("e", rbtree.shift)
|
365
|
+
end
|
366
|
+
|
367
|
+
def test_pop
|
368
|
+
ret = @rbtree.pop
|
369
|
+
assert_equal(3, @rbtree.size)
|
370
|
+
assert_equal(["d", "D"], ret)
|
371
|
+
assert_equal(nil, @rbtree["d"])
|
372
|
+
|
373
|
+
3.times { @rbtree.pop }
|
374
|
+
assert_equal(0, @rbtree.size)
|
375
|
+
assert_equal(nil, @rbtree.pop)
|
376
|
+
@rbtree.default = "e"
|
377
|
+
assert_equal("e", @rbtree.pop)
|
378
|
+
|
379
|
+
rbtree = RBTree.new { "e" }
|
380
|
+
assert_equal("e", rbtree.pop)
|
381
|
+
end
|
382
|
+
|
383
|
+
def test_delete
|
384
|
+
ret = @rbtree.delete("c")
|
385
|
+
assert_equal("C", ret)
|
386
|
+
assert_equal(3, @rbtree.size)
|
387
|
+
assert_equal(nil, @rbtree["c"])
|
388
|
+
|
389
|
+
assert_equal(nil, @rbtree.delete("e"))
|
390
|
+
assert_equal("E", @rbtree.delete("e") { "E" })
|
391
|
+
end
|
392
|
+
|
393
|
+
def test_delete_if
|
394
|
+
@rbtree.delete_if {|key, val| val == "A" || val == "B" }
|
395
|
+
assert_equal(RBTree[*%w(c C d D)], @rbtree)
|
396
|
+
|
397
|
+
assert_raises(ArgumentError) {
|
398
|
+
@rbtree.delete_if {|key, val| key == "c" or raise ArgumentError }
|
399
|
+
}
|
400
|
+
assert_equal(2, @rbtree.size)
|
401
|
+
|
402
|
+
assert_raises(TypeError) {
|
403
|
+
@rbtree.delete_if { @rbtree["e"] = "E" }
|
404
|
+
}
|
405
|
+
assert_equal(2, @rbtree.size)
|
406
|
+
|
407
|
+
@rbtree.delete_if {
|
408
|
+
@rbtree.each {
|
409
|
+
assert_equal(2, @rbtree.size)
|
410
|
+
}
|
411
|
+
assert_raises(TypeError) {
|
412
|
+
@rbtree["e"] = "E"
|
413
|
+
}
|
414
|
+
true
|
415
|
+
}
|
416
|
+
assert_equal(0, @rbtree.size)
|
417
|
+
|
418
|
+
if defined?(Enumerable::Enumerator)
|
419
|
+
rbtree = RBTree[*%w(b B d D a A c C)]
|
420
|
+
enumerator = rbtree.delete_if
|
421
|
+
assert_equal([true, true, false, false], enumerator.map {|key, val| val == "A" || val == "B" })
|
422
|
+
end
|
423
|
+
end
|
424
|
+
|
425
|
+
def test_reject_bang
|
426
|
+
ret = @rbtree.reject! { false }
|
427
|
+
assert_equal(nil, ret)
|
428
|
+
assert_equal(4, @rbtree.size)
|
429
|
+
|
430
|
+
ret = @rbtree.reject! {|key, val| val == "A" || val == "B" }
|
431
|
+
assert_same(@rbtree, ret)
|
432
|
+
assert_equal(RBTree[*%w(c C d D)], ret)
|
433
|
+
|
434
|
+
if defined?(Enumerable::Enumerator)
|
435
|
+
rbtree = RBTree[*%w(b B d D a A c C)]
|
436
|
+
enumerator = rbtree.reject!
|
437
|
+
assert_equal([true, true, false, false], enumerator.map {|key, val| val == "A" || val == "B" })
|
438
|
+
end
|
439
|
+
end
|
440
|
+
|
441
|
+
def test_reject
|
442
|
+
ret = @rbtree.reject { false }
|
443
|
+
assert_equal(nil, ret)
|
444
|
+
assert_equal(4, @rbtree.size)
|
445
|
+
|
446
|
+
ret = @rbtree.reject {|key, val| val == "A" || val == "B" }
|
447
|
+
assert_equal(RBTree[*%w(c C d D)], ret)
|
448
|
+
assert_equal(4, @rbtree.size)
|
449
|
+
|
450
|
+
if defined?(Enumerable::Enumerator)
|
451
|
+
enumerator = @rbtree.reject
|
452
|
+
assert_equal([true, true, false, false], enumerator.map {|key, val| val == "A" || val == "B" })
|
453
|
+
end
|
454
|
+
end
|
455
|
+
|
456
|
+
def test_select
|
457
|
+
ret = @rbtree.select {|key, val| val == "A" || val == "B" }
|
458
|
+
assert_equal(%w(a A b B), ret.flatten)
|
459
|
+
assert_raises(ArgumentError) { @rbtree.select("c") }
|
460
|
+
|
461
|
+
if defined?(Enumerable::Enumerator)
|
462
|
+
enumerator = @rbtree.select
|
463
|
+
assert_equal([true, true, false, false], enumerator.map {|key, val| val == "A" || val == "B"})
|
464
|
+
end
|
465
|
+
end
|
466
|
+
|
467
|
+
def test_values_at
|
468
|
+
ret = @rbtree.values_at("d", "a", "e")
|
469
|
+
assert_equal(["D", "A", nil], ret)
|
470
|
+
end
|
471
|
+
|
472
|
+
def test_invert
|
473
|
+
assert_equal(RBTree[*%w(A a B b C c D d)], @rbtree.invert)
|
474
|
+
end
|
475
|
+
|
476
|
+
def test_update
|
477
|
+
rbtree = RBTree.new
|
478
|
+
rbtree["e"] = "E"
|
479
|
+
@rbtree.update(rbtree)
|
480
|
+
assert_equal(RBTree[*%w(a A b B c C d D e E)], @rbtree)
|
481
|
+
|
482
|
+
@rbtree.clear
|
483
|
+
@rbtree["d"] = "A"
|
484
|
+
rbtree.clear
|
485
|
+
rbtree["d"] = "B"
|
486
|
+
|
487
|
+
@rbtree.update(rbtree) {|key, val1, val2|
|
488
|
+
val1 + val2 if key == "d"
|
489
|
+
}
|
490
|
+
assert_equal(RBTree[*%w(d AB)], @rbtree)
|
491
|
+
|
492
|
+
assert_raises(TypeError) { @rbtree.update("e") }
|
493
|
+
end
|
494
|
+
|
495
|
+
def test_merge
|
496
|
+
rbtree = RBTree.new
|
497
|
+
rbtree["e"] = "E"
|
498
|
+
|
499
|
+
ret = @rbtree.merge(rbtree)
|
500
|
+
assert_equal(RBTree[*%w(a A b B c C d D e E)], ret)
|
501
|
+
|
502
|
+
assert_equal(4, @rbtree.size)
|
503
|
+
end
|
504
|
+
|
505
|
+
def test_has_key
|
506
|
+
assert_equal(true, @rbtree.has_key?("a"))
|
507
|
+
assert_equal(true, @rbtree.has_key?("b"))
|
508
|
+
assert_equal(true, @rbtree.has_key?("c"))
|
509
|
+
assert_equal(true, @rbtree.has_key?("d"))
|
510
|
+
assert_equal(false, @rbtree.has_key?("e"))
|
511
|
+
end
|
512
|
+
|
513
|
+
def test_has_value
|
514
|
+
assert_equal(true, @rbtree.has_value?("A"))
|
515
|
+
assert_equal(true, @rbtree.has_value?("B"))
|
516
|
+
assert_equal(true, @rbtree.has_value?("C"))
|
517
|
+
assert_equal(true, @rbtree.has_value?("D"))
|
518
|
+
assert_equal(false, @rbtree.has_value?("E"))
|
519
|
+
end
|
520
|
+
|
521
|
+
def test_keys
|
522
|
+
assert_equal(%w(a b c d), @rbtree.keys)
|
523
|
+
end
|
524
|
+
|
525
|
+
def test_values
|
526
|
+
assert_equal(%w(A B C D), @rbtree.values)
|
527
|
+
end
|
528
|
+
|
529
|
+
def test_to_a
|
530
|
+
assert_equal([%w(a A), %w(b B), %w(c C), %w(d D)], @rbtree.to_a)
|
531
|
+
end
|
532
|
+
|
533
|
+
def test_to_s
|
534
|
+
if RUBY_VERSION < "1.9"
|
535
|
+
assert_equal("aAbBcCdD", @rbtree.to_s)
|
536
|
+
else
|
537
|
+
expected = "[[\"a\", \"A\"], [\"b\", \"B\"], [\"c\", \"C\"], [\"d\", \"D\"]]"
|
538
|
+
assert_equal(expected, @rbtree.to_s)
|
539
|
+
|
540
|
+
rbtree = RBTree.new
|
541
|
+
rbtree[rbtree] = rbtree
|
542
|
+
rbtree.default = rbtree
|
543
|
+
expected = "[[#<RBTree: {#<RBTree: ...>=>#<RBTree: ...>}, default=#<RBTree: ...>, cmp_proc=nil>, #<RBTree: {#<RBTree: ...>=>#<RBTree: ...>}, default=#<RBTree: ...>, cmp_proc=nil>]]"
|
544
|
+
assert_equal(expected, rbtree.to_s)
|
545
|
+
end
|
546
|
+
end
|
547
|
+
|
548
|
+
def test_to_hash
|
549
|
+
@rbtree.default = "e"
|
550
|
+
hash = @rbtree.to_hash
|
551
|
+
assert_equal(@rbtree.to_a.flatten, hash.to_a.flatten)
|
552
|
+
assert_equal("e", hash.default)
|
553
|
+
|
554
|
+
rbtree = RBTree.new { "e" }
|
555
|
+
hash = rbtree.to_hash
|
556
|
+
if (hash.respond_to?(:default_proc))
|
557
|
+
assert_equal(rbtree.default_proc, hash.default_proc)
|
558
|
+
else
|
559
|
+
assert_equal(rbtree.default_proc, hash.default)
|
560
|
+
end
|
561
|
+
end
|
562
|
+
|
563
|
+
def test_to_rbtree
|
564
|
+
assert_same(@rbtree, @rbtree.to_rbtree)
|
565
|
+
end
|
566
|
+
|
567
|
+
def test_inspect
|
568
|
+
@rbtree.default = "e"
|
569
|
+
@rbtree.readjust {|a, b| a <=> b}
|
570
|
+
re = /#<RBTree: (\{.*\}), default=(.*), cmp_proc=(.*)>/
|
571
|
+
|
572
|
+
assert_match(re, @rbtree.inspect)
|
573
|
+
match = re.match(@rbtree.inspect)
|
574
|
+
tree, default, cmp_proc = match.to_a[1..-1]
|
575
|
+
assert_equal(%({"a"=>"A", "b"=>"B", "c"=>"C", "d"=>"D"}), tree)
|
576
|
+
assert_equal(%("e"), default)
|
577
|
+
assert_match(/#<Proc:\w+(@#{__FILE__}:\d+)?>/o, cmp_proc)
|
578
|
+
|
579
|
+
rbtree = RBTree.new
|
580
|
+
assert_match(re, rbtree.inspect)
|
581
|
+
match = re.match(rbtree.inspect)
|
582
|
+
tree, default, cmp_proc = match.to_a[1..-1]
|
583
|
+
assert_equal("{}", tree)
|
584
|
+
assert_equal("nil", default)
|
585
|
+
assert_equal("nil", cmp_proc)
|
586
|
+
|
587
|
+
rbtree = RBTree.new
|
588
|
+
rbtree[rbtree] = rbtree
|
589
|
+
rbtree.default = rbtree
|
590
|
+
match = re.match(rbtree.inspect)
|
591
|
+
tree, default, cmp_proc = match.to_a[1..-1]
|
592
|
+
assert_equal("{#<RBTree: ...>=>#<RBTree: ...>}", tree)
|
593
|
+
assert_equal("#<RBTree: ...>", default)
|
594
|
+
assert_equal("nil", cmp_proc)
|
595
|
+
end
|
596
|
+
|
597
|
+
def test_lower_bound
|
598
|
+
rbtree = RBTree[*%w(a A c C e E)]
|
599
|
+
assert_equal(["c", "C"], rbtree.lower_bound("c"))
|
600
|
+
assert_equal(["c", "C"], rbtree.lower_bound("b"))
|
601
|
+
assert_equal(nil, rbtree.lower_bound("f"))
|
602
|
+
end
|
603
|
+
|
604
|
+
def test_upper_bound
|
605
|
+
rbtree = RBTree[*%w(a A c C e E)]
|
606
|
+
assert_equal(["c", "C"], rbtree.upper_bound("c"))
|
607
|
+
assert_equal(["c", "C"], rbtree.upper_bound("d"))
|
608
|
+
assert_equal(nil, rbtree.upper_bound("Z"))
|
609
|
+
end
|
610
|
+
|
611
|
+
def test_bound
|
612
|
+
rbtree = RBTree[*%w(a A c C e E)]
|
613
|
+
assert_equal(%w(a A c C), rbtree.bound("a", "c").flatten)
|
614
|
+
assert_equal(%w(a A), rbtree.bound("a").flatten)
|
615
|
+
assert_equal(%w(c C e E), rbtree.bound("b", "f").flatten)
|
616
|
+
|
617
|
+
assert_equal([], rbtree.bound("b", "b"))
|
618
|
+
assert_equal([], rbtree.bound("Y", "Z"))
|
619
|
+
assert_equal([], rbtree.bound("f", "g"))
|
620
|
+
assert_equal([], rbtree.bound("f", "Z"))
|
621
|
+
end
|
622
|
+
|
623
|
+
def test_bound_block
|
624
|
+
ret = []
|
625
|
+
@rbtree.bound("b", "c") {|key, val|
|
626
|
+
ret.push(key)
|
627
|
+
}
|
628
|
+
assert_equal(%w(b c), ret)
|
629
|
+
|
630
|
+
assert_raises(TypeError) {
|
631
|
+
@rbtree.bound("a", "d") {
|
632
|
+
@rbtree["e"] = "E"
|
633
|
+
}
|
634
|
+
}
|
635
|
+
assert_equal(4, @rbtree.size)
|
636
|
+
|
637
|
+
@rbtree.bound("b", "c") {
|
638
|
+
@rbtree.bound("b", "c") {}
|
639
|
+
assert_raises(TypeError) {
|
640
|
+
@rbtree["e"] = "E"
|
641
|
+
}
|
642
|
+
break
|
643
|
+
}
|
644
|
+
assert_equal(4, @rbtree.size)
|
645
|
+
end
|
646
|
+
|
647
|
+
def test_first
|
648
|
+
assert_equal(["a", "A"], @rbtree.first)
|
649
|
+
|
650
|
+
rbtree = RBTree.new("e")
|
651
|
+
assert_equal("e", rbtree.first)
|
652
|
+
|
653
|
+
rbtree = RBTree.new { "e" }
|
654
|
+
assert_equal("e", rbtree.first)
|
655
|
+
end
|
656
|
+
|
657
|
+
def test_last
|
658
|
+
assert_equal(["d", "D"], @rbtree.last)
|
659
|
+
|
660
|
+
rbtree = RBTree.new("e")
|
661
|
+
assert_equal("e", rbtree.last)
|
662
|
+
|
663
|
+
rbtree = RBTree.new { "e" }
|
664
|
+
assert_equal("e", rbtree.last)
|
665
|
+
end
|
666
|
+
|
667
|
+
def test_readjust
|
668
|
+
assert_equal(nil, @rbtree.cmp_proc)
|
669
|
+
|
670
|
+
@rbtree.readjust {|a, b| b <=> a }
|
671
|
+
assert_equal(%w(d c b a), @rbtree.keys)
|
672
|
+
assert_not_equal(nil, @rbtree.cmp_proc)
|
673
|
+
|
674
|
+
proc = Proc.new {|a,b| a.to_s <=> b.to_s }
|
675
|
+
@rbtree.readjust(proc)
|
676
|
+
assert_equal(%w(a b c d), @rbtree.keys)
|
677
|
+
assert_equal(proc, @rbtree.cmp_proc)
|
678
|
+
|
679
|
+
@rbtree[0] = nil
|
680
|
+
assert_raises(ArgumentError) { @rbtree.readjust(nil) }
|
681
|
+
assert_equal(5, @rbtree.size)
|
682
|
+
assert_equal(proc, @rbtree.cmp_proc)
|
683
|
+
|
684
|
+
@rbtree.delete(0)
|
685
|
+
@rbtree.readjust(nil)
|
686
|
+
assert_raises(ArgumentError) { @rbtree[0] = nil }
|
687
|
+
|
688
|
+
|
689
|
+
rbtree = RBTree.new
|
690
|
+
key = ["a"]
|
691
|
+
rbtree[key] = nil
|
692
|
+
rbtree[["e"]] = nil
|
693
|
+
key[0] = "f"
|
694
|
+
|
695
|
+
assert_equal([["f"], ["e"]], rbtree.keys)
|
696
|
+
rbtree.readjust
|
697
|
+
assert_equal([["e"], ["f"]], rbtree.keys)
|
698
|
+
|
699
|
+
assert_raises(ArgumentError) { @rbtree.readjust { "e" } }
|
700
|
+
assert_raises(TypeError) { @rbtree.readjust("e") }
|
701
|
+
assert_raises(ArgumentError) {
|
702
|
+
@rbtree.readjust(proc) {|a,b| a <=> b }
|
703
|
+
}
|
704
|
+
assert_raises(ArgumentError) { @rbtree.readjust(proc, proc) }
|
705
|
+
end
|
706
|
+
|
707
|
+
def test_replace
|
708
|
+
rbtree = RBTree.new { "e" }
|
709
|
+
rbtree.readjust {|a, b| a <=> b}
|
710
|
+
rbtree["a"] = "A"
|
711
|
+
rbtree["e"] = "E"
|
712
|
+
|
713
|
+
@rbtree.replace(rbtree)
|
714
|
+
assert_equal(%w(a A e E), @rbtree.to_a.flatten)
|
715
|
+
assert_equal(rbtree.default, @rbtree.default)
|
716
|
+
assert_equal(rbtree.cmp_proc, @rbtree.cmp_proc)
|
717
|
+
|
718
|
+
assert_raises(TypeError) { @rbtree.replace("e") }
|
719
|
+
end
|
720
|
+
|
721
|
+
def test_reverse_each
|
722
|
+
ret = []
|
723
|
+
@rbtree.reverse_each { |key, val| ret.push([key, val]) }
|
724
|
+
assert_equal(%w(d D c C b B a A), ret.flatten)
|
725
|
+
|
726
|
+
if defined?(Enumerable::Enumerator)
|
727
|
+
enumerator = @rbtree.reverse_each
|
728
|
+
assert_equal(%w(d D c C b B a A), enumerator.map.flatten)
|
729
|
+
end
|
730
|
+
end
|
731
|
+
|
732
|
+
def test_marshal
|
733
|
+
assert_equal(@rbtree, Marshal.load(Marshal.dump(@rbtree)))
|
734
|
+
|
735
|
+
@rbtree.default = "e"
|
736
|
+
assert_equal(@rbtree, Marshal.load(Marshal.dump(@rbtree)))
|
737
|
+
|
738
|
+
assert_raises(TypeError) {
|
739
|
+
Marshal.dump(RBTree.new { "e" })
|
740
|
+
}
|
741
|
+
|
742
|
+
assert_raises(TypeError) {
|
743
|
+
@rbtree.readjust {|a, b| a <=> b}
|
744
|
+
Marshal.dump(@rbtree)
|
745
|
+
}
|
746
|
+
end
|
747
|
+
|
748
|
+
begin
|
749
|
+
require "pp"
|
750
|
+
|
751
|
+
def test_pp
|
752
|
+
assert_equal(%(#<RBTree: {}, default=nil, cmp_proc=nil>\n),
|
753
|
+
PP.pp(RBTree.new, ""))
|
754
|
+
assert_equal(%(#<RBTree: {"a"=>"A", "b"=>"B"}, default=nil, cmp_proc=nil>\n),
|
755
|
+
PP.pp(RBTree[*%w(a A b B)], ""))
|
756
|
+
|
757
|
+
rbtree = RBTree[*("a".."z").to_a]
|
758
|
+
rbtree.default = "a"
|
759
|
+
rbtree.readjust {|a, b| a <=> b }
|
760
|
+
expected = <<EOS
|
761
|
+
#<RBTree: {"a"=>"b",
|
762
|
+
"c"=>"d",
|
763
|
+
"e"=>"f",
|
764
|
+
"g"=>"h",
|
765
|
+
"i"=>"j",
|
766
|
+
"k"=>"l",
|
767
|
+
"m"=>"n",
|
768
|
+
"o"=>"p",
|
769
|
+
"q"=>"r",
|
770
|
+
"s"=>"t",
|
771
|
+
"u"=>"v",
|
772
|
+
"w"=>"x",
|
773
|
+
"y"=>"z"},
|
774
|
+
default="a",
|
775
|
+
cmp_proc=#{rbtree.cmp_proc}>
|
776
|
+
EOS
|
777
|
+
assert_equal(expected, PP.pp(rbtree, ""))
|
778
|
+
|
779
|
+
rbtree = RBTree.new
|
780
|
+
rbtree[rbtree] = rbtree
|
781
|
+
rbtree.default = rbtree
|
782
|
+
expected = <<EOS
|
783
|
+
#<RBTree: {"#<RBTree: ...>"=>"#<RBTree: ...>"},
|
784
|
+
default="#<RBTree: ...>",
|
785
|
+
cmp_proc=nil>
|
786
|
+
EOS
|
787
|
+
assert_equal(expected, PP.pp(rbtree, ""))
|
788
|
+
end
|
789
|
+
rescue LoadError
|
790
|
+
end
|
791
|
+
end # class RBTreeTest
|