rubytree 1.0.2 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/spec/tree_spec.rb CHANGED
@@ -3,70 +3,171 @@
3
3
  # tree_spec.rb
4
4
  #
5
5
  # Author: Anupam Sengupta
6
- # Time-stamp: <2015-12-31 22:57:59 anupam>
7
- # Copyright (C) 2015 Anupam Sengupta <anupamsg@gmail.com>
6
+ # Time-stamp: <2022-06-20 22:16:11 anupam>
7
+ # Copyright (C) 2015-2022 Anupam Sengupta <anupamsg@gmail.com>
8
8
  #
9
+ # frozen_string_literal: true
9
10
 
10
11
  require 'rspec'
11
12
  require 'spec_helper'
12
13
 
14
+ class SpecializedTreeNode < Tree::TreeNode; end
15
+
13
16
  describe Tree do
14
17
  shared_examples_for 'any detached node' do
15
- it 'should not equal "Object.new"' do
16
- expect(@tree).not_to eq(Object.new)
18
+ it 'does not equal "Object.new"' do
19
+ expect(tree).not_to eq(Object.new)
17
20
  end
18
- it 'should not equal 1 or any other fixnum' do
19
- expect(@tree).not_to eq(1)
21
+
22
+ it 'does not equal 1 or any other fixnum' do
23
+ expect(tree).not_to eq(1)
20
24
  end
25
+
21
26
  it 'identifies itself as a root node' do
22
- expect(@tree.is_root?).to eq(true)
27
+ expect(tree.root?).to be(true)
23
28
  end
29
+
24
30
  it 'does not have a parent node' do
25
- expect(@tree.parent).to eq(nil)
31
+ expect(tree.parent).to be_nil
26
32
  end
27
33
  end
28
- context '#initialize', 'with empty name and nil content' do
29
- before(:each) do
30
- @tree = Tree::TreeNode.new('')
31
- end
34
+
35
+ describe '#initialize with empty name and nil content' do
36
+ let(:tree) { Tree::TreeNode.new('') }
37
+
32
38
  it 'creates the tree node with name as ""' do
33
- expect(@tree.name).to eq('')
39
+ expect(tree.name).to eq('')
34
40
  end
41
+
35
42
  it "has 'nil' content" do
36
- expect(@tree.content).to eq(nil)
43
+ expect(tree.content).to be_nil
37
44
  end
38
45
 
39
46
  it_behaves_like 'any detached node'
40
47
  end
41
48
 
42
- context '#initialize', "with name 'A' and nil content" do
43
- before(:each) do
44
- @tree = Tree::TreeNode.new('A')
45
- end
49
+ describe "#initialize with name 'A' and nil content" do
50
+ let(:tree) { Tree::TreeNode.new('A') }
46
51
 
47
52
  it 'creates the tree node with name as "A"' do
48
- expect(@tree.name).to eq('A')
53
+ expect(tree.name).to eq('A')
49
54
  end
55
+
50
56
  it "has 'nil' content" do
51
- expect(@tree.content).to eq(nil)
57
+ expect(tree.content).to be_nil
52
58
  end
53
59
 
54
60
  it_behaves_like 'any detached node'
55
61
  end
56
62
 
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
63
+ describe "#initialize with node name 'A' and some content" do
64
+ sample = 'some content'
65
+ let(:tree) { Tree::TreeNode.new('A', sample) }
62
66
 
63
67
  it 'creates the tree node with name as "A"' do
64
- expect(@tree.name).to eq('A')
68
+ expect(tree.name).to eq('A')
65
69
  end
66
- it "has some content #{@sample}" do
67
- expect(@tree.content).to eq(@sample)
70
+
71
+ it "has some content #{sample}" do
72
+ expect(tree.content).to eq(sample)
68
73
  end
69
74
 
70
75
  it_behaves_like 'any detached node'
71
76
  end
77
+
78
+ describe 'comparison' do
79
+ let(:node1) { Tree::TreeNode.new('a') }
80
+ let(:node2) { SpecializedTreeNode.new('b') }
81
+
82
+ it 'allows comparison of specialized tree nodes' do
83
+ expect(node1 <=> node2).to be_eql(-1)
84
+ end
85
+
86
+ it 'does not allow comparison with nil' do
87
+ expect(node1 <=> nil).to be_nil
88
+ end
89
+
90
+ it 'does not allow comparison with other objects' do
91
+ expect(node1 <=> 'c').to be_nil
92
+ end
93
+ end
94
+
95
+ describe 'serialization' do
96
+ let(:serialized_node1) { Marshal.dump(SpecializedTreeNode.new('a')) }
97
+ let(:serialized_node2) { Marshal.dump(Tree::TreeNode.new('b')) }
98
+ let(:tree) do
99
+ SpecializedTreeNode.new('root').tap do |root|
100
+ root << SpecializedTreeNode.new('a')
101
+ root << Tree::TreeNode.new('b')
102
+ end
103
+ end
104
+ let(:serialized_tree) { Marshal.dump(tree) }
105
+
106
+ it 'parses the serialized specialized tree node correctly (root)' do
107
+ expect(Marshal.load(serialized_tree)).to be_a(SpecializedTreeNode)
108
+ end
109
+
110
+ it 'parses the serialized specialized tree node correctly (child)' do
111
+ expect(Marshal.load(serialized_tree).children.first).to \
112
+ be_a(SpecializedTreeNode)
113
+ end
114
+
115
+ it 'parses the serialized tree node correctly' do
116
+ expect(Marshal.load(serialized_node2)).to be_a(Tree::TreeNode)
117
+ end
118
+ end
119
+
120
+ shared_examples_for 'any cloned node' do
121
+ it 'is equal to the original' do
122
+ expect(clone).to eq tree
123
+ end
124
+
125
+ it 'is not identical to the original' do
126
+ expect(clone).not_to be tree
127
+ end
128
+ end
129
+
130
+ describe '#detached_copy', 'Without content' do
131
+ let(:tree) { Tree::TreeNode.new('A', nil) }
132
+ let(:clone) { tree.detached_copy }
133
+
134
+ it_behaves_like 'any cloned node'
135
+ end
136
+
137
+ describe '#detached_copy with clonable content' do
138
+ let(:tree) { Tree::TreeNode.new('A', 'clonable content') }
139
+ let(:clone) { tree.detached_copy }
140
+
141
+ it 'makes a clone of the content' do
142
+ expect(clone.content).to eq tree.content
143
+ end
144
+
145
+ it 'is not the same as the original content' do
146
+ expect(clone.content).not_to be tree.content
147
+ end
148
+
149
+ it_behaves_like 'any cloned node'
150
+ end
151
+
152
+ describe '#detached_copy with unclonable content' do
153
+ let(:tree) { Tree::TreeNode.new('A', :unclonable_content) }
154
+ let(:clone) { tree.detached_copy }
155
+
156
+ it 'retains the original content' do
157
+ expect(clone.content).to be tree.content
158
+ end
159
+
160
+ it_behaves_like 'any cloned node'
161
+ end
162
+
163
+ describe '#detached_copy with false as content' do
164
+ let(:tree) { Tree::TreeNode.new('A', false) }
165
+ let(:clone) { tree.detached_copy }
166
+
167
+ it 'retains the original content' do
168
+ expect(clone.content).to be tree.content
169
+ end
170
+
171
+ it_behaves_like 'any cloned node'
172
+ end
72
173
  end
data/test/run_test.rb CHANGED
@@ -3,8 +3,8 @@
3
3
  # run_test.rb:: Run all the tests from the Ruby command line.
4
4
  #
5
5
  # Author: Anupam Sengupta
6
- # Time-stamp: <2014-01-03 21:15:37 anupam>
7
- # Copyright (C) 2014 Anupam Sengupta <anupamsg@gmail.com>
6
+ # Time-stamp: <2022-06-19 22:44:56 anupam>
7
+ # Copyright (C) 2014, 2022 Anupam Sengupta <anupamsg@gmail.com>
8
8
  #
9
9
  # Redistribution and use in source and binary forms, with or without modification,
10
10
  # are permitted provided that the following conditions are met:
@@ -31,6 +31,7 @@
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
  base_dir = File.expand_path(File.join(File.dirname(__FILE__), '..'))
36
37
  lib_dir = File.join(base_dir, 'lib')
@@ -51,7 +52,7 @@ if ENV['COVERAGE']
51
52
  add_group 'Internal Utilities', '/lib/tree/utils/.*.rb'
52
53
  end
53
54
  rescue LoadError => e
54
- puts 'Could not load simplecov; continuing without code coverage' + e.cause
55
+ puts "Could not load simplecov; continuing without code coverage #{e.cause}"
55
56
  end
56
57
  end
57
58
 
@@ -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, 2017 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,6 +32,7 @@
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'
@@ -82,8 +83,8 @@ module TestTree
82
83
 
83
84
  assert_same(tree.class, Tree::BinaryTreeNode)
84
85
  assert_same(:A, tree.name)
85
- assert_equal(true, tree.is_root?)
86
- assert_equal(false, tree.is_leaf?)
86
+ assert_equal(true, tree.root?)
87
+ assert_equal(false, tree.leaf?)
87
88
  assert_equal(2, tree.children.count) # B, C, D
88
89
  assert_equal(4, tree.size)
89
90
 
@@ -130,13 +131,13 @@ module TestTree
130
131
  # Test the add method.
131
132
  def test_add
132
133
  @root.add @left_child1
133
- assert(!@left_child1.is_root?, 'Left child1 cannot be a root after addition to the ROOT node')
134
+ assert(!@left_child1.root?, 'Left child1 cannot be a root after addition to the ROOT node')
134
135
 
135
136
  assert_same(@left_child1, @root.left_child, 'The left node should be left_child1')
136
137
  assert_same(@left_child1, @root.first_child, 'The first node should be left_child1')
137
138
 
138
139
  @root.add @right_child1
139
- assert(!@right_child1.is_root?, 'Right child1 cannot be a root after addition to the ROOT node')
140
+ assert(!@right_child1.root?, 'Right child1 cannot be a root after addition to the ROOT node')
140
141
 
141
142
  assert_same(@right_child1, @root.right_child, 'The right node should be right_child1')
142
143
  assert_same(@right_child1, @root.last_child, 'The first node should be right_child1')
@@ -191,9 +192,8 @@ module TestTree
191
192
  end
192
193
 
193
194
  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
195
+
196
+ assert_equal(Enumerable::Enumerator, f.inordered_each.class) if defined?(Enumerable::Enumerator.class)
197
197
  end
198
198
 
199
199
  # Test the left_child method.
@@ -217,10 +217,10 @@ module TestTree
217
217
  @root << @left_child1
218
218
  @root << @right_child1
219
219
  assert_same(@left_child1, @root.left_child, "The left child should be 'left_child1")
220
- assert(!@left_child1.is_root?, 'The left child now cannot be a root.')
220
+ assert(!@left_child1.root?, 'The left child now cannot be a root.')
221
221
 
222
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.')
223
+ assert(!@root.left_child.root?, 'The left child now cannot be a root.')
224
224
  assert_equal('New Left Child', @root.left_child.name, 'The left child should now be the new child')
225
225
  assert_equal('B Child at Right', @root.last_child.name, 'The last child should now be the right child')
226
226
 
@@ -236,10 +236,10 @@ module TestTree
236
236
  @root << @left_child1
237
237
  @root << @right_child1
238
238
  assert_same(@right_child1, @root.right_child, "The right child should be 'right_child1")
239
- assert(!@right_child1.is_root?, 'The right child now cannot be a root.')
239
+ assert(!@right_child1.root?, 'The right child now cannot be a root.')
240
240
 
241
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.')
242
+ assert(!@root.right_child.root?, 'The right child now cannot be a root.')
243
243
  assert_equal('New Right Child', @root.right_child.name, 'The right child should now be the new child')
244
244
  assert_equal('A Child at Left', @root.first_child.name, 'The first child should now be the left child')
245
245
  assert_equal('New Right Child', @root.last_child.name, 'The last child should now be the right child')
@@ -256,28 +256,28 @@ module TestTree
256
256
  @root << @left_child1
257
257
  @root << @right_child1
258
258
 
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')
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')
261
261
 
262
262
  # Now set the right child as nil, and retest
263
263
  @root.right_child = nil
264
- assert(@left_child1.is_left_child?, 'left_child1 should be the left child')
264
+ assert(@left_child1.left_child?, 'left_child1 should be the left child')
265
265
 
266
- assert(!@root.is_left_child?, 'Root is neither left child nor right')
266
+ assert(!@root.left_child?, 'Root is neither left child nor right')
267
267
  end
268
268
 
269
- # Test is_right_child? method.
269
+ # Test right_child? method.
270
270
  def test_is_right_child_eh
271
271
  @root << @left_child1
272
272
  @root << @right_child1
273
273
 
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')
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')
276
276
 
277
277
  # Now set the left child as nil, and retest
278
278
  @root.left_child = nil
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')
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')
281
281
  end
282
282
 
283
283
  # Test swap_children method.
@@ -285,37 +285,17 @@ module TestTree
285
285
  @root << @left_child1
286
286
  @root << @right_child1
287
287
 
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')
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')
290
290
 
291
291
  @root.swap_children
292
292
 
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')
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
295
  assert_equal(@right_child1, @root.first_child, 'right_child1 should now be the first child')
296
296
  assert_equal(@left_child1, @root.last_child, 'left_child1 should now be the last child')
297
297
  assert_equal(@right_child1, @root[0], 'right_child1 should now be the first child')
298
298
  assert_equal(@left_child1, @root[1], 'left_child1 should now be the last child')
299
299
  end
300
-
301
- # Test the old CamelCase method names
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')
305
-
306
- require 'structured_warnings'
307
-
308
- meth_names_for_test = %w[leftChild isLeftChild? rightChild isRightChild?]
309
-
310
- meth_names_for_test.each do |meth_name|
311
- assert_warn(StructuredWarnings::DeprecatedMethodWarning) { @root.send(meth_name) }
312
- end
313
-
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
319
- end
320
300
  end
321
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,6 +31,7 @@
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'
@@ -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, 2017 Anupam Sengupta
5
+ # Copyright (c) 2012, 2017, 2022 Anupam Sengupta
6
6
  #
7
7
  # All rights reserved.
8
8
  #
@@ -31,6 +31,7 @@
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'
@@ -41,25 +42,6 @@ module TestTree
41
42
  class TestSubclassedTreeNode < Test::Unit::TestCase
42
43
  # A subclassed node to test various inheritance related features.
43
44
  class MyNode < Tree::TreeNode
44
- # A dummy method to test the camelCasedMethod resolution
45
- def my_dummy_method
46
- 'Hello'
47
- end
48
- end
49
-
50
- def test_camelcase_methods
51
- root = MyNode.new('Root')
52
-
53
- assert_equal('Hello', root.my_dummy_method)
54
-
55
- # We should get a warning as we are invoking the camelCase version of the dummy method.
56
- assert_warn(StructuredWarnings::DeprecatedMethodWarning) { root.send('MyDummyMethod') }
57
-
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)
62
- end
63
45
  end
64
46
 
65
47
  def test_detached_copy_same_clz
@@ -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,6 +31,7 @@
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'
@@ -49,7 +50,6 @@ module TestTree
49
50
  current = new_node
50
51
  end
51
52
 
52
- tree.each { |_| nil }
53
53
  tree
54
54
  end
55
55
 
@@ -65,7 +65,7 @@ module TestTree
65
65
  assert_equal(depth + 1, root.size)
66
66
  end.resume
67
67
  end
68
- end # test_fiber
68
+ end
69
69
 
70
70
  # Test the recursive methods with a thread. The stack usage is causing
71
71
  # failure for very large depths on unbalanced nodes.
@@ -78,7 +78,7 @@ module TestTree
78
78
  assert_equal(depth + 1, root.size)
79
79
  end
80
80
  end
81
- end # test_thread
81
+ end
82
82
  end
83
83
  end
84
84