rubytree 0.9.5 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +5 -5
- data/Gemfile +0 -6
- data/Gemfile.lock +75 -44
- data/History.rdoc +18 -2
- data/LICENSE.md +1 -2
- data/README.md +10 -8
- data/Rakefile +43 -35
- data/examples/example_basic.rb +11 -8
- data/lib/rubytree.rb +1 -1
- data/lib/tree/binarytree.rb +17 -18
- data/lib/tree/tree_deps.rb +8 -8
- data/lib/tree/utils/camel_case_method_handler.rb +12 -14
- data/lib/tree/utils/hash_converter.rb +62 -64
- data/lib/tree/utils/json_converter.rb +16 -17
- data/lib/tree/utils/metrics_methods.rb +12 -9
- data/lib/tree/utils/path_methods.rb +8 -10
- data/lib/tree/utils/tree_merge_handler.rb +9 -10
- data/lib/tree/utils/utils.rb +3 -2
- data/lib/tree/version.rb +2 -3
- data/lib/tree.rb +143 -123
- data/rubytree.gemspec +27 -25
- data/setup.rb +239 -259
- data/spec/spec_helper.rb +10 -0
- data/spec/tree_spec.rb +72 -0
- data/test/run_test.rb +6 -6
- data/test/test_binarytree.rb +93 -92
- data/test/test_rubytree_require.rb +2 -4
- data/test/test_subclassed_node.rb +13 -16
- data/test/test_thread_and_fiber.rb +10 -13
- data/test/test_tree.rb +565 -572
- metadata +98 -29
- data/TAGS +0 -248
- data/gem_graph.png +0 -0
data/spec/spec_helper.rb
ADDED
data/spec/tree_spec.rb
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
#
|
3
|
+
# tree_spec.rb
|
4
|
+
#
|
5
|
+
# Author: Anupam Sengupta
|
6
|
+
# Time-stamp: <2015-12-31 22:57:59 anupam>
|
7
|
+
# Copyright (C) 2015 Anupam Sengupta <anupamsg@gmail.com>
|
8
|
+
#
|
9
|
+
|
10
|
+
require 'rspec'
|
11
|
+
require 'spec_helper'
|
12
|
+
|
13
|
+
describe Tree do
|
14
|
+
shared_examples_for 'any detached node' do
|
15
|
+
it 'should not equal "Object.new"' do
|
16
|
+
expect(@tree).not_to eq(Object.new)
|
17
|
+
end
|
18
|
+
it 'should not equal 1 or any other fixnum' do
|
19
|
+
expect(@tree).not_to eq(1)
|
20
|
+
end
|
21
|
+
it 'identifies itself as a root node' do
|
22
|
+
expect(@tree.is_root?).to eq(true)
|
23
|
+
end
|
24
|
+
it 'does not have a parent node' do
|
25
|
+
expect(@tree.parent).to eq(nil)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
context '#initialize', 'with empty name and nil content' do
|
29
|
+
before(:each) do
|
30
|
+
@tree = Tree::TreeNode.new('')
|
31
|
+
end
|
32
|
+
it 'creates the tree node with name as ""' do
|
33
|
+
expect(@tree.name).to eq('')
|
34
|
+
end
|
35
|
+
it "has 'nil' content" do
|
36
|
+
expect(@tree.content).to eq(nil)
|
37
|
+
end
|
38
|
+
|
39
|
+
it_behaves_like 'any detached node'
|
40
|
+
end
|
41
|
+
|
42
|
+
context '#initialize', "with name 'A' and nil content" do
|
43
|
+
before(:each) do
|
44
|
+
@tree = Tree::TreeNode.new('A')
|
45
|
+
end
|
46
|
+
|
47
|
+
it 'creates the tree node with name as "A"' do
|
48
|
+
expect(@tree.name).to eq('A')
|
49
|
+
end
|
50
|
+
it "has 'nil' content" do
|
51
|
+
expect(@tree.content).to eq(nil)
|
52
|
+
end
|
53
|
+
|
54
|
+
it_behaves_like 'any detached node'
|
55
|
+
end
|
56
|
+
|
57
|
+
context '#initialize', "with node name 'A' and some content" do
|
58
|
+
before(:each) do
|
59
|
+
@sample = 'sample'
|
60
|
+
@tree = Tree::TreeNode.new('A', @sample)
|
61
|
+
end
|
62
|
+
|
63
|
+
it 'creates the tree node with name as "A"' do
|
64
|
+
expect(@tree.name).to eq('A')
|
65
|
+
end
|
66
|
+
it "has some content #{@sample}" do
|
67
|
+
expect(@tree.content).to eq(@sample)
|
68
|
+
end
|
69
|
+
|
70
|
+
it_behaves_like 'any detached node'
|
71
|
+
end
|
72
|
+
end
|
data/test/run_test.rb
CHANGED
@@ -32,13 +32,13 @@
|
|
32
32
|
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
33
33
|
#
|
34
34
|
|
35
|
-
base_dir = File.expand_path(File.join(File.dirname(__FILE__),
|
36
|
-
lib_dir = File.join(base_dir,
|
37
|
-
test_dir = File.join(base_dir,
|
35
|
+
base_dir = File.expand_path(File.join(File.dirname(__FILE__), '..'))
|
36
|
+
lib_dir = File.join(base_dir, 'lib')
|
37
|
+
test_dir = File.join(base_dir, 'test')
|
38
38
|
|
39
39
|
$LOAD_PATH.unshift(lib_dir)
|
40
40
|
|
41
|
-
if ENV[
|
41
|
+
if ENV['COVERAGE']
|
42
42
|
begin
|
43
43
|
require 'simplecov'
|
44
44
|
require 'coveralls'
|
@@ -51,10 +51,10 @@ if ENV["COVERAGE"]
|
|
51
51
|
add_group 'Internal Utilities', '/lib/tree/utils/.*.rb'
|
52
52
|
end
|
53
53
|
rescue LoadError => e
|
54
|
-
puts
|
54
|
+
puts 'Could not load simplecov; continuing without code coverage' + e.cause
|
55
55
|
end
|
56
56
|
end
|
57
57
|
|
58
58
|
require 'test/unit'
|
59
59
|
|
60
|
-
|
60
|
+
Test::Unit::AutoRunner.run(true, test_dir)
|
data/test/test_binarytree.rb
CHANGED
@@ -3,7 +3,7 @@
|
|
3
3
|
# test_binarytree.rb - This file is part of the RubyTree package.
|
4
4
|
#
|
5
5
|
#
|
6
|
-
# Copyright (c) 2006, 2007, 2008, 2009, 2010, 2012, 2013, 2015 Anupam Sengupta
|
6
|
+
# Copyright (c) 2006, 2007, 2008, 2009, 2010, 2012, 2013, 2015, 2017 Anupam Sengupta
|
7
7
|
#
|
8
8
|
# All rights reserved.
|
9
9
|
#
|
@@ -34,18 +34,17 @@
|
|
34
34
|
#
|
35
35
|
|
36
36
|
require 'test/unit'
|
37
|
-
|
37
|
+
require_relative '../lib/tree/binarytree'
|
38
38
|
|
39
39
|
module TestTree
|
40
40
|
# Test class for the binary tree node.
|
41
41
|
class TestBinaryTreeNode < Test::Unit::TestCase
|
42
|
-
|
43
42
|
# Setup the test data scaffolding.
|
44
43
|
def setup
|
45
|
-
@root = Tree::BinaryTreeNode.new(
|
44
|
+
@root = Tree::BinaryTreeNode.new('ROOT', 'Root Node')
|
46
45
|
|
47
|
-
@left_child1 = Tree::BinaryTreeNode.new(
|
48
|
-
@right_child1 = Tree::BinaryTreeNode.new(
|
46
|
+
@left_child1 = Tree::BinaryTreeNode.new('A Child at Left', 'Child Node @ left')
|
47
|
+
@right_child1 = Tree::BinaryTreeNode.new('B Child at Right', 'Child Node @ right')
|
49
48
|
end
|
50
49
|
|
51
50
|
# Tear down the test data scaffolding.
|
@@ -58,22 +57,22 @@ module TestTree
|
|
58
57
|
# Test initialization of the binary tree.
|
59
58
|
def test_initialize
|
60
59
|
assert_not_nil(@root, "Binary tree's Root should have been created")
|
61
|
-
assert_nil(@root.left_child,
|
62
|
-
assert_nil(@root.right_child,
|
63
|
-
assert_equal(@root.children.size, 0,
|
60
|
+
assert_nil(@root.left_child, 'The initial left child of root should be nil')
|
61
|
+
assert_nil(@root.right_child, 'The initial right child of root should be nil')
|
62
|
+
assert_equal(@root.children.size, 0, 'Initially no children should be present')
|
64
63
|
end
|
65
64
|
|
66
65
|
def test_from_hash
|
67
66
|
# Can't make a root node without a name
|
68
|
-
assert_raise
|
67
|
+
assert_raise(ArgumentError) { Tree::BinaryTreeNode.from_hash({}) }
|
69
68
|
# Can't have multiple roots
|
70
|
-
assert_raise
|
69
|
+
assert_raise(ArgumentError) { Tree::BinaryTreeNode.from_hash({ A: {}, B: {} }) }
|
71
70
|
|
72
71
|
# Can't have more than 2 children
|
73
|
-
too_many_kids = {:
|
72
|
+
too_many_kids = { A: { B: {}, C: {}, D: {} } }
|
74
73
|
assert_raise(ArgumentError) { Tree::BinaryTreeNode.from_hash(too_many_kids) }
|
75
74
|
|
76
|
-
valid_hash = {:
|
75
|
+
valid_hash = { A: { B: {}, C: { D: {} } } }
|
77
76
|
tree = Tree::BinaryTreeNode.from_hash(valid_hash)
|
78
77
|
# A
|
79
78
|
# / \
|
@@ -88,19 +87,19 @@ module TestTree
|
|
88
87
|
assert_equal(2, tree.children.count) # B, C, D
|
89
88
|
assert_equal(4, tree.size)
|
90
89
|
|
91
|
-
valid_hash_with_content = {[:A,
|
90
|
+
valid_hash_with_content = { [:A, 'Content!'] => { B: {}, C: { [:D, 'More content'] => {} } } }
|
92
91
|
tree2 = Tree::BinaryTreeNode.from_hash(valid_hash_with_content)
|
93
92
|
|
94
93
|
assert_equal(Tree::BinaryTreeNode, tree2.class)
|
95
|
-
assert_equal(
|
96
|
-
assert_equal(
|
94
|
+
assert_equal('Content!', tree2.content)
|
95
|
+
assert_equal('More content', tree2[:C][:D].content)
|
97
96
|
end
|
98
97
|
|
99
98
|
def test_add_from_hash
|
100
|
-
root = Tree::BinaryTreeNode.new(
|
99
|
+
root = Tree::BinaryTreeNode.new('Root')
|
101
100
|
|
102
101
|
# Can't have too many children
|
103
|
-
too_many_kids = {:
|
102
|
+
too_many_kids = { child1: {}, child2: {}, child3: {} }
|
104
103
|
assert_raise(ArgumentError) { root.add_from_hash(too_many_kids) }
|
105
104
|
assert_equal(0, root.children.count) # Nothing added
|
106
105
|
|
@@ -108,7 +107,7 @@ module TestTree
|
|
108
107
|
assert_equal([], root.add_from_hash({}))
|
109
108
|
assert_equal(1, root.size)
|
110
109
|
|
111
|
-
valid_hash = {:
|
110
|
+
valid_hash = { A: {}, B: { C: {}, [:D, 'leaf'] => {} } }
|
112
111
|
added = root.add_from_hash(valid_hash)
|
113
112
|
# root
|
114
113
|
# / \
|
@@ -120,48 +119,48 @@ module TestTree
|
|
120
119
|
assert_equal(2, added.count)
|
121
120
|
assert_equal(5, root.size)
|
122
121
|
assert_equal(root.children.count, 2)
|
123
|
-
assert_equal(
|
122
|
+
assert_equal('leaf', root[:B][:D].content)
|
124
123
|
|
125
124
|
# Can't add more than two children
|
126
|
-
assert_raise(ArgumentError) { root.add_from_hash({:
|
127
|
-
node = Tree::BinaryTreeNode.new(
|
128
|
-
assert_raise(ArgumentError) { node.add_from_hash({:
|
125
|
+
assert_raise(ArgumentError) { root.add_from_hash({ X: {} }) }
|
126
|
+
node = Tree::BinaryTreeNode.new('Root 2')
|
127
|
+
assert_raise(ArgumentError) { node.add_from_hash({ A: {}, B: {}, C: {} }) }
|
129
128
|
end
|
130
129
|
|
131
130
|
# Test the add method.
|
132
131
|
def test_add
|
133
132
|
@root.add @left_child1
|
134
|
-
assert(!@left_child1.is_root?,
|
133
|
+
assert(!@left_child1.is_root?, 'Left child1 cannot be a root after addition to the ROOT node')
|
135
134
|
|
136
|
-
assert_same(@left_child1, @root.left_child,
|
137
|
-
assert_same(@left_child1, @root.first_child,
|
135
|
+
assert_same(@left_child1, @root.left_child, 'The left node should be left_child1')
|
136
|
+
assert_same(@left_child1, @root.first_child, 'The first node should be left_child1')
|
138
137
|
|
139
138
|
@root.add @right_child1
|
140
|
-
assert(!@right_child1.is_root?,
|
139
|
+
assert(!@right_child1.is_root?, 'Right child1 cannot be a root after addition to the ROOT node')
|
141
140
|
|
142
|
-
assert_same(@right_child1, @root.right_child,
|
143
|
-
assert_same(@right_child1, @root.last_child,
|
141
|
+
assert_same(@right_child1, @root.right_child, 'The right node should be right_child1')
|
142
|
+
assert_same(@right_child1, @root.last_child, 'The first node should be right_child1')
|
144
143
|
|
145
144
|
assert_raise ArgumentError do
|
146
|
-
@root.add Tree::BinaryTreeNode.new(
|
145
|
+
@root.add Tree::BinaryTreeNode.new('The third child!')
|
147
146
|
end
|
148
147
|
|
149
148
|
assert_raise ArgumentError do
|
150
|
-
@root << Tree::BinaryTreeNode.new(
|
149
|
+
@root << Tree::BinaryTreeNode.new('The third child!')
|
151
150
|
end
|
152
151
|
end
|
153
152
|
|
154
153
|
# Test the inordered_each method.
|
155
154
|
def test_inordered_each
|
156
|
-
a = Tree::BinaryTreeNode.new(
|
157
|
-
b = Tree::BinaryTreeNode.new(
|
158
|
-
c = Tree::BinaryTreeNode.new(
|
159
|
-
d = Tree::BinaryTreeNode.new(
|
160
|
-
e = Tree::BinaryTreeNode.new(
|
161
|
-
f = Tree::BinaryTreeNode.new(
|
162
|
-
g = Tree::BinaryTreeNode.new(
|
163
|
-
h = Tree::BinaryTreeNode.new(
|
164
|
-
i = Tree::BinaryTreeNode.new(
|
155
|
+
a = Tree::BinaryTreeNode.new('a')
|
156
|
+
b = Tree::BinaryTreeNode.new('b')
|
157
|
+
c = Tree::BinaryTreeNode.new('c')
|
158
|
+
d = Tree::BinaryTreeNode.new('d')
|
159
|
+
e = Tree::BinaryTreeNode.new('e')
|
160
|
+
f = Tree::BinaryTreeNode.new('f')
|
161
|
+
g = Tree::BinaryTreeNode.new('g')
|
162
|
+
h = Tree::BinaryTreeNode.new('h')
|
163
|
+
i = Tree::BinaryTreeNode.new('i')
|
165
164
|
|
166
165
|
# Create the following Tree
|
167
166
|
# f <-- level 0 (Root)
|
@@ -175,24 +174,26 @@ module TestTree
|
|
175
174
|
f << g
|
176
175
|
b << d << c
|
177
176
|
d << e
|
178
|
-
g.right_child = i
|
177
|
+
g.right_child = i # This needs to be explicit
|
179
178
|
i << h
|
180
179
|
|
181
180
|
# The expected order of response
|
182
181
|
expected_array = [a, b, c, d, e, f, g, h, i]
|
183
182
|
|
184
183
|
result_array = []
|
185
|
-
result = f.inordered_each { |node| result_array << node.detached_copy}
|
184
|
+
result = f.inordered_each { |node| result_array << node.detached_copy }
|
186
185
|
|
187
|
-
assert_equal(f, result)
|
186
|
+
assert_equal(f, result) # each should return the original object
|
188
187
|
|
189
|
-
expected_array.each_index do |
|
188
|
+
expected_array.each_index do |idx|
|
190
189
|
# Match only the names.
|
191
|
-
assert_equal(expected_array[
|
190
|
+
assert_equal(expected_array[idx].name, result_array[idx].name)
|
192
191
|
end
|
193
192
|
|
194
|
-
assert_equal(Enumerator, f.inordered_each.class) if defined?(Enumerator.class
|
195
|
-
|
193
|
+
assert_equal(Enumerator, f.inordered_each.class) if defined?(Enumerator.class) # Without a block
|
194
|
+
if defined?(Enumerable::Enumerator.class)
|
195
|
+
assert_equal(Enumerable::Enumerator, f.inordered_each.class)
|
196
|
+
end # Without a block
|
196
197
|
end
|
197
198
|
|
198
199
|
# Test the left_child method.
|
@@ -200,7 +201,7 @@ module TestTree
|
|
200
201
|
@root << @left_child1
|
201
202
|
@root << @right_child1
|
202
203
|
assert_same(@left_child1, @root.left_child, "The left child should be 'left_child1")
|
203
|
-
assert_not_same(@right_child1, @root.left_child,
|
204
|
+
assert_not_same(@right_child1, @root.left_child, 'The right_child1 is not the left child')
|
204
205
|
end
|
205
206
|
|
206
207
|
# Test the right_child method.
|
@@ -208,7 +209,7 @@ module TestTree
|
|
208
209
|
@root << @left_child1
|
209
210
|
@root << @right_child1
|
210
211
|
assert_same(@right_child1, @root.right_child, "The right child should be 'right_child1")
|
211
|
-
assert_not_same(@left_child1, @root.right_child,
|
212
|
+
assert_not_same(@left_child1, @root.right_child, 'The left_child1 is not the left child')
|
212
213
|
end
|
213
214
|
|
214
215
|
# Test left_child= method.
|
@@ -216,18 +217,18 @@ module TestTree
|
|
216
217
|
@root << @left_child1
|
217
218
|
@root << @right_child1
|
218
219
|
assert_same(@left_child1, @root.left_child, "The left child should be 'left_child1")
|
219
|
-
assert(!@left_child1.is_root?,
|
220
|
+
assert(!@left_child1.is_root?, 'The left child now cannot be a root.')
|
220
221
|
|
221
|
-
@root.left_child = Tree::BinaryTreeNode.new(
|
222
|
-
assert(!@root.left_child.is_root?,
|
223
|
-
assert_equal(
|
224
|
-
assert_equal(
|
222
|
+
@root.left_child = Tree::BinaryTreeNode.new('New Left Child')
|
223
|
+
assert(!@root.left_child.is_root?, 'The left child now cannot be a root.')
|
224
|
+
assert_equal('New Left Child', @root.left_child.name, 'The left child should now be the new child')
|
225
|
+
assert_equal('B Child at Right', @root.last_child.name, 'The last child should now be the right child')
|
225
226
|
|
226
227
|
# Now set the left child as nil, and retest
|
227
228
|
@root.left_child = nil
|
228
|
-
assert_nil(@root.left_child,
|
229
|
-
assert_nil(@root.first_child,
|
230
|
-
assert_equal(
|
229
|
+
assert_nil(@root.left_child, 'The left child should now be nil')
|
230
|
+
assert_nil(@root.first_child, 'The first child is now nil')
|
231
|
+
assert_equal('B Child at Right', @root.last_child.name, 'The last child should now be the right child')
|
231
232
|
end
|
232
233
|
|
233
234
|
# Test right_child= method.
|
@@ -235,19 +236,19 @@ module TestTree
|
|
235
236
|
@root << @left_child1
|
236
237
|
@root << @right_child1
|
237
238
|
assert_same(@right_child1, @root.right_child, "The right child should be 'right_child1")
|
238
|
-
assert(!@right_child1.is_root?,
|
239
|
+
assert(!@right_child1.is_root?, 'The right child now cannot be a root.')
|
239
240
|
|
240
|
-
@root.right_child = Tree::BinaryTreeNode.new(
|
241
|
-
assert(!@root.right_child.is_root?,
|
242
|
-
assert_equal(
|
243
|
-
assert_equal(
|
244
|
-
assert_equal(
|
241
|
+
@root.right_child = Tree::BinaryTreeNode.new('New Right Child')
|
242
|
+
assert(!@root.right_child.is_root?, 'The right child now cannot be a root.')
|
243
|
+
assert_equal('New Right Child', @root.right_child.name, 'The right child should now be the new child')
|
244
|
+
assert_equal('A Child at Left', @root.first_child.name, 'The first child should now be the left child')
|
245
|
+
assert_equal('New Right Child', @root.last_child.name, 'The last child should now be the right child')
|
245
246
|
|
246
247
|
# Now set the right child as nil, and retest
|
247
248
|
@root.right_child = nil
|
248
|
-
assert_nil(@root.right_child,
|
249
|
-
assert_equal(
|
250
|
-
assert_nil(@root.last_child,
|
249
|
+
assert_nil(@root.right_child, 'The right child should now be nil')
|
250
|
+
assert_equal('A Child at Left', @root.first_child.name, 'The first child should now be the left child')
|
251
|
+
assert_nil(@root.last_child, 'The first child is now nil')
|
251
252
|
end
|
252
253
|
|
253
254
|
# Test isLeft_child? method.
|
@@ -255,14 +256,14 @@ module TestTree
|
|
255
256
|
@root << @left_child1
|
256
257
|
@root << @right_child1
|
257
258
|
|
258
|
-
assert(@left_child1.is_left_child?,
|
259
|
-
assert(!@right_child1.is_left_child?,
|
259
|
+
assert(@left_child1.is_left_child?, 'left_child1 should be the left child')
|
260
|
+
assert(!@right_child1.is_left_child?, 'left_child1 should be the left child')
|
260
261
|
|
261
262
|
# Now set the right child as nil, and retest
|
262
263
|
@root.right_child = nil
|
263
|
-
assert(@left_child1.is_left_child?,
|
264
|
+
assert(@left_child1.is_left_child?, 'left_child1 should be the left child')
|
264
265
|
|
265
|
-
assert(!@root.is_left_child?,
|
266
|
+
assert(!@root.is_left_child?, 'Root is neither left child nor right')
|
266
267
|
end
|
267
268
|
|
268
269
|
# Test is_right_child? method.
|
@@ -270,13 +271,13 @@ module TestTree
|
|
270
271
|
@root << @left_child1
|
271
272
|
@root << @right_child1
|
272
273
|
|
273
|
-
assert(@right_child1.is_right_child?,
|
274
|
-
assert(!@left_child1.is_right_child?,
|
274
|
+
assert(@right_child1.is_right_child?, 'right_child1 should be the right child')
|
275
|
+
assert(!@left_child1.is_right_child?, 'right_child1 should be the right child')
|
275
276
|
|
276
277
|
# Now set the left child as nil, and retest
|
277
278
|
@root.left_child = nil
|
278
|
-
assert(@right_child1.is_right_child?,
|
279
|
-
assert(!@root.is_right_child?,
|
279
|
+
assert(@right_child1.is_right_child?, 'right_child1 should be the right child')
|
280
|
+
assert(!@root.is_right_child?, 'Root is neither left child nor right')
|
280
281
|
end
|
281
282
|
|
282
283
|
# Test swap_children method.
|
@@ -284,37 +285,37 @@ module TestTree
|
|
284
285
|
@root << @left_child1
|
285
286
|
@root << @right_child1
|
286
287
|
|
287
|
-
assert(@right_child1.is_right_child?,
|
288
|
-
assert(!@left_child1.is_right_child?,
|
288
|
+
assert(@right_child1.is_right_child?, 'right_child1 should be the right child')
|
289
|
+
assert(!@left_child1.is_right_child?, 'right_child1 should be the right child')
|
289
290
|
|
290
291
|
@root.swap_children
|
291
292
|
|
292
|
-
assert(@right_child1.is_left_child?,
|
293
|
-
assert(@left_child1.is_right_child?,
|
294
|
-
assert_equal(@right_child1, @root.first_child,
|
295
|
-
assert_equal(@left_child1, @root.last_child,
|
296
|
-
assert_equal(@right_child1, @root[0],
|
297
|
-
assert_equal(@left_child1, @root[1],
|
293
|
+
assert(@right_child1.is_left_child?, 'right_child1 should now be the left child')
|
294
|
+
assert(@left_child1.is_right_child?, 'left_child1 should now be the right child')
|
295
|
+
assert_equal(@right_child1, @root.first_child, 'right_child1 should now be the first child')
|
296
|
+
assert_equal(@left_child1, @root.last_child, 'left_child1 should now be the last child')
|
297
|
+
assert_equal(@right_child1, @root[0], 'right_child1 should now be the first child')
|
298
|
+
assert_equal(@left_child1, @root[1], 'left_child1 should now be the last child')
|
298
299
|
end
|
299
300
|
|
300
301
|
# Test the old CamelCase method names
|
301
|
-
def
|
302
|
-
@left_child2 = Tree::BinaryTreeNode.new(
|
303
|
-
@right_child2 = Tree::BinaryTreeNode.new(
|
302
|
+
def test_old_camel_case_names
|
303
|
+
@left_child2 = Tree::BinaryTreeNode.new('A Child at Left', 'Child Node @ left')
|
304
|
+
@right_child2 = Tree::BinaryTreeNode.new('B Child at Right', 'Child Node @ right')
|
304
305
|
|
305
306
|
require 'structured_warnings'
|
306
307
|
|
307
|
-
meth_names_for_test = %w
|
308
|
+
meth_names_for_test = %w[leftChild isLeftChild? rightChild isRightChild?]
|
308
309
|
|
309
310
|
meth_names_for_test.each do |meth_name|
|
310
|
-
assert_warn(DeprecatedMethodWarning) {@root.send(meth_name)}
|
311
|
+
assert_warn(StructuredWarnings::DeprecatedMethodWarning) { @root.send(meth_name) }
|
311
312
|
end
|
312
313
|
|
313
|
-
|
314
|
-
assert_warn(DeprecatedMethodWarning) {@root.
|
315
|
-
|
316
|
-
|
314
|
+
# noinspection RubyResolve
|
315
|
+
assert_warn(StructuredWarnings::DeprecatedMethodWarning) { @root.leftChild = @left_child2 }
|
316
|
+
# noinspection RubyResolve
|
317
|
+
assert_warn(StructuredWarnings::DeprecatedMethodWarning) { @root.rightChild = @right_child2 }
|
318
|
+
assert_raise(NoMethodError) { @root.DummyMethodDoesNotExist } # Make sure the right method is visible
|
317
319
|
end
|
318
|
-
|
319
320
|
end
|
320
321
|
end
|
@@ -33,16 +33,14 @@
|
|
33
33
|
#
|
34
34
|
|
35
35
|
require 'test/unit'
|
36
|
-
|
36
|
+
require_relative '../lib/rubytree'
|
37
37
|
|
38
38
|
module TestTree
|
39
|
-
|
40
39
|
# Test class for checking whether require 'rubytree' works.
|
41
40
|
class TestRequireRubytree < Test::Unit::TestCase
|
42
|
-
|
43
41
|
# A simple test. We are just checking whether the require worked.
|
44
42
|
def test_create_a_simple_node
|
45
|
-
assert_not_nil(Tree::TreeNode.new(
|
43
|
+
assert_not_nil(Tree::TreeNode.new('Root', 'A Root node'))
|
46
44
|
end
|
47
45
|
end
|
48
46
|
end
|
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
# test_subclassed_node.rb - This file is part of the RubyTree package.
|
4
4
|
#
|
5
|
-
# Copyright (c) 2012 Anupam Sengupta
|
5
|
+
# Copyright (c) 2012, 2017 Anupam Sengupta
|
6
6
|
#
|
7
7
|
# All rights reserved.
|
8
8
|
#
|
@@ -34,41 +34,38 @@
|
|
34
34
|
|
35
35
|
require 'test/unit'
|
36
36
|
require 'json'
|
37
|
-
|
37
|
+
require_relative '../lib/tree'
|
38
38
|
|
39
39
|
module TestTree
|
40
|
-
|
41
40
|
# Test class for the Tree node.
|
42
41
|
class TestSubclassedTreeNode < Test::Unit::TestCase
|
43
|
-
|
44
42
|
# A subclassed node to test various inheritance related features.
|
45
43
|
class MyNode < Tree::TreeNode
|
46
44
|
# A dummy method to test the camelCasedMethod resolution
|
47
45
|
def my_dummy_method
|
48
|
-
|
46
|
+
'Hello'
|
49
47
|
end
|
50
48
|
end
|
51
49
|
|
52
|
-
def
|
53
|
-
root = MyNode.new(
|
50
|
+
def test_camelcase_methods
|
51
|
+
root = MyNode.new('Root')
|
54
52
|
|
55
|
-
assert_equal(
|
53
|
+
assert_equal('Hello', root.my_dummy_method)
|
56
54
|
|
57
55
|
# We should get a warning as we are invoking the camelCase version of the dummy method.
|
58
|
-
assert_warn(DeprecatedMethodWarning) { root.send('MyDummyMethod') }
|
56
|
+
assert_warn(StructuredWarnings::DeprecatedMethodWarning) { root.send('MyDummyMethod') }
|
59
57
|
|
60
|
-
# Test if the structured_warnings can be disabled to call the
|
61
|
-
DeprecatedMethodWarning.disable do
|
62
|
-
|
58
|
+
# Test if the structured_warnings can be disabled to call the CamelCase methods.
|
59
|
+
StructuredWarnings::DeprecatedMethodWarning.disable do
|
60
|
+
# noinspection RubyResolve
|
61
|
+
assert_equal('Hello', root.myDummyMethod)
|
63
62
|
end
|
64
|
-
|
65
63
|
end
|
66
64
|
|
67
|
-
def
|
68
|
-
root = MyNode.new(
|
65
|
+
def test_detached_copy_same_clz
|
66
|
+
root = MyNode.new('Root')
|
69
67
|
assert_equal(MyNode, root.detached_copy.class)
|
70
68
|
end
|
71
|
-
|
72
69
|
end
|
73
70
|
end
|
74
71
|
|
@@ -34,23 +34,22 @@
|
|
34
34
|
|
35
35
|
require 'test/unit'
|
36
36
|
require 'json'
|
37
|
-
|
37
|
+
require_relative '../lib/tree'
|
38
38
|
|
39
39
|
module TestTree
|
40
40
|
# Test class for the Tree node.
|
41
41
|
class TestFiberAndThreadOnNode < Test::Unit::TestCase
|
42
|
-
|
43
42
|
# Test long and unbalanced trees
|
44
|
-
def create_long_depth_trees(depth=100)
|
45
|
-
tree = Tree::TreeNode.new(
|
43
|
+
def create_long_depth_trees(depth = 100)
|
44
|
+
tree = Tree::TreeNode.new('/')
|
46
45
|
current = tree
|
47
46
|
depth.times do |i|
|
48
|
-
new_node = Tree::TreeNode.new(
|
47
|
+
new_node = Tree::TreeNode.new(i.to_s)
|
49
48
|
current << new_node
|
50
49
|
current = new_node
|
51
50
|
end
|
52
51
|
|
53
|
-
tree.each { |
|
52
|
+
tree.each { |_| nil }
|
54
53
|
tree
|
55
54
|
end
|
56
55
|
|
@@ -58,30 +57,28 @@ module TestTree
|
|
58
57
|
# failure for very large depths on unbalanced nodes.
|
59
58
|
def test_fiber_for_recursion
|
60
59
|
return unless defined?(Fiber.class) # Fibers exist only from Ruby 1.9 onwards.
|
60
|
+
|
61
61
|
assert_nothing_thrown do
|
62
62
|
Fiber.new do
|
63
|
-
depth = 1000
|
63
|
+
depth = 1000 # Use a reasonably large depth, which would trip a recursive stack
|
64
64
|
root = create_long_depth_trees(depth)
|
65
|
-
assert_equal(depth+1, root.size)
|
65
|
+
assert_equal(depth + 1, root.size)
|
66
66
|
end.resume
|
67
67
|
end
|
68
|
-
|
69
68
|
end # test_fiber
|
70
69
|
|
71
70
|
# Test the recursive methods with a thread. The stack usage is causing
|
72
71
|
# failure for very large depths on unbalanced nodes.
|
73
72
|
def test_thread_for_recursion
|
74
73
|
assert_nothing_thrown do
|
75
|
-
depth = 1000
|
74
|
+
depth = 1000 # Use a reasonably large depth, which would trip a recursive stack
|
76
75
|
Thread.abort_on_exception = true
|
77
76
|
Thread.new do
|
78
77
|
root = create_long_depth_trees(depth)
|
79
|
-
assert_equal(depth+1, root.size)
|
78
|
+
assert_equal(depth + 1, root.size)
|
80
79
|
end
|
81
80
|
end
|
82
|
-
|
83
81
|
end # test_thread
|
84
|
-
|
85
82
|
end
|
86
83
|
end
|
87
84
|
|