rubytree 0.9.7 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/API-CHANGES.md +153 -0
- data/Gemfile +2 -13
- data/Gemfile.lock +60 -61
- data/History.md +410 -0
- data/LICENSE.md +1 -2
- data/README.md +24 -28
- data/Rakefile +65 -48
- data/TODO.org +19 -15
- data/examples/example_basic.rb +19 -12
- data/lib/rubytree.rb +3 -4
- data/lib/tree/binarytree.rb +27 -27
- data/lib/tree/tree_deps.rb +9 -12
- data/lib/tree/utils/hash_converter.rb +127 -121
- data/lib/tree/utils/json_converter.rb +81 -79
- data/lib/tree/utils/metrics_methods.rb +18 -48
- data/lib/tree/utils/path_methods.rb +15 -17
- data/lib/tree/utils/tree_merge_handler.rb +79 -80
- data/lib/tree/utils/utils.rb +9 -6
- data/lib/tree/version.rb +3 -5
- data/lib/tree.rb +194 -177
- data/rubytree.gemspec +67 -44
- data/spec/spec_helper.rb +5 -3
- data/spec/tree_spec.rb +136 -37
- data/test/run_test.rb +9 -8
- data/test/test_binarytree.rb +86 -105
- data/test/test_rubytree_require.rb +4 -5
- data/test/test_subclassed_node.rb +5 -26
- data/test/test_thread_and_fiber.rb +13 -16
- data/test/test_tree.rb +577 -657
- metadata +142 -55
- data/API-CHANGES.rdoc +0 -99
- data/History.rdoc +0 -303
- data/TAGS +0 -248
- data/gem_graph.png +0 -0
- data/lib/tree/utils/camel_case_method_handler.rb +0 -79
- data/setup.rb +0 -1585
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, 2022 Anupam Sengupta
|
7
7
|
#
|
8
8
|
# All rights reserved.
|
9
9
|
#
|
@@ -32,20 +32,20 @@
|
|
32
32
|
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
33
33
|
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
34
34
|
#
|
35
|
+
# frozen_string_literal: true
|
35
36
|
|
36
37
|
require 'test/unit'
|
37
|
-
|
38
|
+
require_relative '../lib/tree/binarytree'
|
38
39
|
|
39
40
|
module TestTree
|
40
41
|
# Test class for the binary tree node.
|
41
42
|
class TestBinaryTreeNode < Test::Unit::TestCase
|
42
|
-
|
43
43
|
# Setup the test data scaffolding.
|
44
44
|
def setup
|
45
|
-
@root = Tree::BinaryTreeNode.new(
|
45
|
+
@root = Tree::BinaryTreeNode.new('ROOT', 'Root Node')
|
46
46
|
|
47
|
-
@left_child1 = Tree::BinaryTreeNode.new(
|
48
|
-
@right_child1 = Tree::BinaryTreeNode.new(
|
47
|
+
@left_child1 = Tree::BinaryTreeNode.new('A Child at Left', 'Child Node @ left')
|
48
|
+
@right_child1 = Tree::BinaryTreeNode.new('B Child at Right', 'Child Node @ right')
|
49
49
|
end
|
50
50
|
|
51
51
|
# Tear down the test data scaffolding.
|
@@ -58,22 +58,22 @@ module TestTree
|
|
58
58
|
# Test initialization of the binary tree.
|
59
59
|
def test_initialize
|
60
60
|
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,
|
61
|
+
assert_nil(@root.left_child, 'The initial left child of root should be nil')
|
62
|
+
assert_nil(@root.right_child, 'The initial right child of root should be nil')
|
63
|
+
assert_equal(@root.children.size, 0, 'Initially no children should be present')
|
64
64
|
end
|
65
65
|
|
66
66
|
def test_from_hash
|
67
67
|
# Can't make a root node without a name
|
68
|
-
assert_raise
|
68
|
+
assert_raise(ArgumentError) { Tree::BinaryTreeNode.from_hash({}) }
|
69
69
|
# Can't have multiple roots
|
70
|
-
assert_raise
|
70
|
+
assert_raise(ArgumentError) { Tree::BinaryTreeNode.from_hash({ A: {}, B: {} }) }
|
71
71
|
|
72
72
|
# Can't have more than 2 children
|
73
|
-
too_many_kids = {:
|
73
|
+
too_many_kids = { A: { B: {}, C: {}, D: {} } }
|
74
74
|
assert_raise(ArgumentError) { Tree::BinaryTreeNode.from_hash(too_many_kids) }
|
75
75
|
|
76
|
-
valid_hash = {:
|
76
|
+
valid_hash = { A: { B: {}, C: { D: {} } } }
|
77
77
|
tree = Tree::BinaryTreeNode.from_hash(valid_hash)
|
78
78
|
# A
|
79
79
|
# / \
|
@@ -83,24 +83,24 @@ module TestTree
|
|
83
83
|
|
84
84
|
assert_same(tree.class, Tree::BinaryTreeNode)
|
85
85
|
assert_same(:A, tree.name)
|
86
|
-
assert_equal(true, tree.
|
87
|
-
assert_equal(false, tree.
|
86
|
+
assert_equal(true, tree.root?)
|
87
|
+
assert_equal(false, tree.leaf?)
|
88
88
|
assert_equal(2, tree.children.count) # B, C, D
|
89
89
|
assert_equal(4, tree.size)
|
90
90
|
|
91
|
-
valid_hash_with_content = {[:A,
|
91
|
+
valid_hash_with_content = { [:A, 'Content!'] => { B: {}, C: { [:D, 'More content'] => {} } } }
|
92
92
|
tree2 = Tree::BinaryTreeNode.from_hash(valid_hash_with_content)
|
93
93
|
|
94
94
|
assert_equal(Tree::BinaryTreeNode, tree2.class)
|
95
|
-
assert_equal(
|
96
|
-
assert_equal(
|
95
|
+
assert_equal('Content!', tree2.content)
|
96
|
+
assert_equal('More content', tree2[:C][:D].content)
|
97
97
|
end
|
98
98
|
|
99
99
|
def test_add_from_hash
|
100
|
-
root = Tree::BinaryTreeNode.new(
|
100
|
+
root = Tree::BinaryTreeNode.new('Root')
|
101
101
|
|
102
102
|
# Can't have too many children
|
103
|
-
too_many_kids = {:
|
103
|
+
too_many_kids = { child1: {}, child2: {}, child3: {} }
|
104
104
|
assert_raise(ArgumentError) { root.add_from_hash(too_many_kids) }
|
105
105
|
assert_equal(0, root.children.count) # Nothing added
|
106
106
|
|
@@ -108,7 +108,7 @@ module TestTree
|
|
108
108
|
assert_equal([], root.add_from_hash({}))
|
109
109
|
assert_equal(1, root.size)
|
110
110
|
|
111
|
-
valid_hash = {:
|
111
|
+
valid_hash = { A: {}, B: { C: {}, [:D, 'leaf'] => {} } }
|
112
112
|
added = root.add_from_hash(valid_hash)
|
113
113
|
# root
|
114
114
|
# / \
|
@@ -120,48 +120,48 @@ module TestTree
|
|
120
120
|
assert_equal(2, added.count)
|
121
121
|
assert_equal(5, root.size)
|
122
122
|
assert_equal(root.children.count, 2)
|
123
|
-
assert_equal(
|
123
|
+
assert_equal('leaf', root[:B][:D].content)
|
124
124
|
|
125
125
|
# 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({:
|
126
|
+
assert_raise(ArgumentError) { root.add_from_hash({ X: {} }) }
|
127
|
+
node = Tree::BinaryTreeNode.new('Root 2')
|
128
|
+
assert_raise(ArgumentError) { node.add_from_hash({ A: {}, B: {}, C: {} }) }
|
129
129
|
end
|
130
130
|
|
131
131
|
# Test the add method.
|
132
132
|
def test_add
|
133
133
|
@root.add @left_child1
|
134
|
-
assert(!@left_child1.
|
134
|
+
assert(!@left_child1.root?, 'Left child1 cannot be a root after addition to the ROOT node')
|
135
135
|
|
136
|
-
assert_same(@left_child1, @root.left_child,
|
137
|
-
assert_same(@left_child1, @root.first_child,
|
136
|
+
assert_same(@left_child1, @root.left_child, 'The left node should be left_child1')
|
137
|
+
assert_same(@left_child1, @root.first_child, 'The first node should be left_child1')
|
138
138
|
|
139
139
|
@root.add @right_child1
|
140
|
-
assert(!@right_child1.
|
140
|
+
assert(!@right_child1.root?, 'Right child1 cannot be a root after addition to the ROOT node')
|
141
141
|
|
142
|
-
assert_same(@right_child1, @root.right_child,
|
143
|
-
assert_same(@right_child1, @root.last_child,
|
142
|
+
assert_same(@right_child1, @root.right_child, 'The right node should be right_child1')
|
143
|
+
assert_same(@right_child1, @root.last_child, 'The first node should be right_child1')
|
144
144
|
|
145
145
|
assert_raise ArgumentError do
|
146
|
-
@root.add Tree::BinaryTreeNode.new(
|
146
|
+
@root.add Tree::BinaryTreeNode.new('The third child!')
|
147
147
|
end
|
148
148
|
|
149
149
|
assert_raise ArgumentError do
|
150
|
-
@root << Tree::BinaryTreeNode.new(
|
150
|
+
@root << Tree::BinaryTreeNode.new('The third child!')
|
151
151
|
end
|
152
152
|
end
|
153
153
|
|
154
154
|
# Test the inordered_each method.
|
155
155
|
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(
|
156
|
+
a = Tree::BinaryTreeNode.new('a')
|
157
|
+
b = Tree::BinaryTreeNode.new('b')
|
158
|
+
c = Tree::BinaryTreeNode.new('c')
|
159
|
+
d = Tree::BinaryTreeNode.new('d')
|
160
|
+
e = Tree::BinaryTreeNode.new('e')
|
161
|
+
f = Tree::BinaryTreeNode.new('f')
|
162
|
+
g = Tree::BinaryTreeNode.new('g')
|
163
|
+
h = Tree::BinaryTreeNode.new('h')
|
164
|
+
i = Tree::BinaryTreeNode.new('i')
|
165
165
|
|
166
166
|
# Create the following Tree
|
167
167
|
# f <-- level 0 (Root)
|
@@ -175,24 +175,25 @@ module TestTree
|
|
175
175
|
f << g
|
176
176
|
b << d << c
|
177
177
|
d << e
|
178
|
-
g.right_child = i
|
178
|
+
g.right_child = i # This needs to be explicit
|
179
179
|
i << h
|
180
180
|
|
181
181
|
# The expected order of response
|
182
182
|
expected_array = [a, b, c, d, e, f, g, h, i]
|
183
183
|
|
184
184
|
result_array = []
|
185
|
-
result = f.inordered_each { |node| result_array << node.detached_copy}
|
185
|
+
result = f.inordered_each { |node| result_array << node.detached_copy }
|
186
186
|
|
187
|
-
assert_equal(f, result)
|
187
|
+
assert_equal(f, result) # each should return the original object
|
188
188
|
|
189
|
-
expected_array.each_index do |
|
189
|
+
expected_array.each_index do |idx|
|
190
190
|
# Match only the names.
|
191
|
-
assert_equal(expected_array[
|
191
|
+
assert_equal(expected_array[idx].name, result_array[idx].name)
|
192
192
|
end
|
193
193
|
|
194
|
-
assert_equal(Enumerator, f.inordered_each.class) if defined?(Enumerator.class
|
195
|
-
|
194
|
+
assert_equal(Enumerator, f.inordered_each.class) if defined?(Enumerator.class) # Without a block
|
195
|
+
|
196
|
+
assert_equal(Enumerable::Enumerator, f.inordered_each.class) if defined?(Enumerable::Enumerator.class)
|
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.
|
220
|
+
assert(!@left_child1.root?, 'The left child now cannot be a root.')
|
220
221
|
|
221
|
-
@root.left_child = Tree::BinaryTreeNode.new(
|
222
|
-
assert(!@root.left_child.
|
223
|
-
assert_equal(
|
224
|
-
assert_equal(
|
222
|
+
@root.left_child = Tree::BinaryTreeNode.new('New Left Child')
|
223
|
+
assert(!@root.left_child.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.
|
239
|
+
assert(!@right_child1.root?, 'The right child now cannot be a root.')
|
239
240
|
|
240
|
-
@root.right_child = Tree::BinaryTreeNode.new(
|
241
|
-
assert(!@root.right_child.
|
242
|
-
assert_equal(
|
243
|
-
assert_equal(
|
244
|
-
assert_equal(
|
241
|
+
@root.right_child = Tree::BinaryTreeNode.new('New Right Child')
|
242
|
+
assert(!@root.right_child.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,28 +256,28 @@ module TestTree
|
|
255
256
|
@root << @left_child1
|
256
257
|
@root << @right_child1
|
257
258
|
|
258
|
-
assert(@left_child1.
|
259
|
-
assert(!@right_child1.
|
259
|
+
assert(@left_child1.left_child?, 'left_child1 should be the left child')
|
260
|
+
assert(!@right_child1.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.
|
264
|
+
assert(@left_child1.left_child?, 'left_child1 should be the left child')
|
264
265
|
|
265
|
-
assert(!@root.
|
266
|
+
assert(!@root.left_child?, 'Root is neither left child nor right')
|
266
267
|
end
|
267
268
|
|
268
|
-
# Test
|
269
|
+
# Test right_child? method.
|
269
270
|
def test_is_right_child_eh
|
270
271
|
@root << @left_child1
|
271
272
|
@root << @right_child1
|
272
273
|
|
273
|
-
assert(@right_child1.
|
274
|
-
assert(!@left_child1.
|
274
|
+
assert(@right_child1.right_child?, 'right_child1 should be the right child')
|
275
|
+
assert(!@left_child1.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.
|
279
|
-
assert(!@root.
|
279
|
+
assert(@right_child1.right_child?, 'right_child1 should be the right child')
|
280
|
+
assert(!@root.right_child?, 'Root is neither left child nor right')
|
280
281
|
end
|
281
282
|
|
282
283
|
# Test swap_children method.
|
@@ -284,37 +285,17 @@ module TestTree
|
|
284
285
|
@root << @left_child1
|
285
286
|
@root << @right_child1
|
286
287
|
|
287
|
-
assert(@right_child1.
|
288
|
-
assert(!@left_child1.
|
288
|
+
assert(@right_child1.right_child?, 'right_child1 should be the right child')
|
289
|
+
assert(!@left_child1.right_child?, 'right_child1 should be the right child')
|
289
290
|
|
290
291
|
@root.swap_children
|
291
292
|
|
292
|
-
assert(@right_child1.
|
293
|
-
assert(@left_child1.
|
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.left_child?, 'right_child1 should now be the left child')
|
294
|
+
assert(@left_child1.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
|
-
# Test the old CamelCase method names
|
301
|
-
def test_old_camelCase_method_names
|
302
|
-
@left_child2 = Tree::BinaryTreeNode.new("A Child at Left", "Child Node @ left")
|
303
|
-
@right_child2 = Tree::BinaryTreeNode.new("B Child at Right", "Child Node @ right")
|
304
|
-
|
305
|
-
require 'structured_warnings'
|
306
|
-
|
307
|
-
meth_names_for_test = %w{leftChild isLeftChild? rightChild isRightChild?}
|
308
|
-
|
309
|
-
meth_names_for_test.each do |meth_name|
|
310
|
-
assert_warn(DeprecatedMethodWarning) {@root.send(meth_name)}
|
311
|
-
end
|
312
|
-
|
313
|
-
assert_warn(DeprecatedMethodWarning) {@root.leftChild = @left_child2}
|
314
|
-
assert_warn(DeprecatedMethodWarning) {@root.rightChild = @right_child2}
|
315
|
-
assert_raise(NoMethodError) {@root.to_snake_case("ABCD")} # Make sure the right method is visible
|
316
|
-
|
317
|
-
end
|
318
|
-
|
319
300
|
end
|
320
301
|
end
|
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
# test_rubytree_require.rb - This file is part of the RubyTree package.
|
4
4
|
#
|
5
|
-
# Copyright (c) 2012 Anupam Sengupta
|
5
|
+
# Copyright (c) 2012, 2022 Anupam Sengupta
|
6
6
|
#
|
7
7
|
# All rights reserved.
|
8
8
|
#
|
@@ -31,18 +31,17 @@
|
|
31
31
|
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
32
32
|
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
33
33
|
#
|
34
|
+
# frozen_string_literal: true
|
34
35
|
|
35
36
|
require 'test/unit'
|
36
|
-
|
37
|
+
require_relative '../lib/rubytree'
|
37
38
|
|
38
39
|
module TestTree
|
39
|
-
|
40
40
|
# Test class for checking whether require 'rubytree' works.
|
41
41
|
class TestRequireRubytree < Test::Unit::TestCase
|
42
|
-
|
43
42
|
# A simple test. We are just checking whether the require worked.
|
44
43
|
def test_create_a_simple_node
|
45
|
-
assert_not_nil(Tree::TreeNode.new(
|
44
|
+
assert_not_nil(Tree::TreeNode.new('Root', 'A Root node'))
|
46
45
|
end
|
47
46
|
end
|
48
47
|
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, 2022 Anupam Sengupta
|
6
6
|
#
|
7
7
|
# All rights reserved.
|
8
8
|
#
|
@@ -31,44 +31,23 @@
|
|
31
31
|
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
32
32
|
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
33
33
|
#
|
34
|
+
# frozen_string_literal: true
|
34
35
|
|
35
36
|
require 'test/unit'
|
36
37
|
require 'json'
|
37
|
-
|
38
|
+
require_relative '../lib/tree'
|
38
39
|
|
39
40
|
module TestTree
|
40
|
-
|
41
41
|
# Test class for the Tree node.
|
42
42
|
class TestSubclassedTreeNode < Test::Unit::TestCase
|
43
|
-
|
44
43
|
# A subclassed node to test various inheritance related features.
|
45
44
|
class MyNode < Tree::TreeNode
|
46
|
-
# A dummy method to test the camelCasedMethod resolution
|
47
|
-
def my_dummy_method
|
48
|
-
"Hello"
|
49
|
-
end
|
50
45
|
end
|
51
46
|
|
52
|
-
def
|
53
|
-
root = MyNode.new(
|
54
|
-
|
55
|
-
assert_equal("Hello", root.my_dummy_method)
|
56
|
-
|
57
|
-
# We should get a warning as we are invoking the camelCase version of the dummy method.
|
58
|
-
assert_warn(DeprecatedMethodWarning) { root.send('MyDummyMethod') }
|
59
|
-
|
60
|
-
# Test if the structured_warnings can be disabled to call the CamelCa
|
61
|
-
DeprecatedMethodWarning.disable do
|
62
|
-
assert_equal("Hello", root.myDummyMethod)
|
63
|
-
end
|
64
|
-
|
65
|
-
end
|
66
|
-
|
67
|
-
def test_subclassed_detached_copy_is_same_class
|
68
|
-
root = MyNode.new("Root")
|
47
|
+
def test_detached_copy_same_clz
|
48
|
+
root = MyNode.new('Root')
|
69
49
|
assert_equal(MyNode, root.detached_copy.class)
|
70
50
|
end
|
71
|
-
|
72
51
|
end
|
73
52
|
end
|
74
53
|
|
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
# test_thread_and_fiber.rb - This file is part of the RubyTree package.
|
4
4
|
#
|
5
|
-
# Copyright (c) 2012, 2013 Anupam Sengupta
|
5
|
+
# Copyright (c) 2012, 2013, 2022 Anupam Sengupta
|
6
6
|
#
|
7
7
|
# All rights reserved.
|
8
8
|
#
|
@@ -31,26 +31,25 @@
|
|
31
31
|
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
32
32
|
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
33
33
|
#
|
34
|
+
# frozen_string_literal: true
|
34
35
|
|
35
36
|
require 'test/unit'
|
36
37
|
require 'json'
|
37
|
-
|
38
|
+
require_relative '../lib/tree'
|
38
39
|
|
39
40
|
module TestTree
|
40
41
|
# Test class for the Tree node.
|
41
42
|
class TestFiberAndThreadOnNode < Test::Unit::TestCase
|
42
|
-
|
43
43
|
# Test long and unbalanced trees
|
44
|
-
def create_long_depth_trees(depth=100)
|
45
|
-
tree = Tree::TreeNode.new(
|
44
|
+
def create_long_depth_trees(depth = 100)
|
45
|
+
tree = Tree::TreeNode.new('/')
|
46
46
|
current = tree
|
47
47
|
depth.times do |i|
|
48
|
-
new_node = Tree::TreeNode.new(
|
48
|
+
new_node = Tree::TreeNode.new(i.to_s)
|
49
49
|
current << new_node
|
50
50
|
current = new_node
|
51
51
|
end
|
52
52
|
|
53
|
-
tree.each { |n| 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
|
-
end # test_fiber
|
68
|
+
end
|
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
|
-
end # test_thread
|
84
|
-
|
81
|
+
end
|
85
82
|
end
|
86
83
|
end
|
87
84
|
|