rbtree-jruby 0.1.0

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