compsci 0.3.0.1 → 0.3.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,200 +0,0 @@
1
- require 'compsci/node'
2
- require 'compsci/tree'
3
- require 'minitest/autorun'
4
-
5
- include CompSci
6
-
7
- describe Tree do
8
- before do
9
- @tree = Tree.new(FlexNode, 42)
10
- @vals = Array.new(99) { rand 99 }
11
- end
12
-
13
- it "is populated via the root node" do
14
- @vals.each { |v| @tree.root.new_child v }
15
- @tree.root.children.size.must_equal @vals.size
16
- end
17
-
18
- it "does depth_first search" do
19
- vals = (0..30).to_a
20
- tree = Tree.new(FlexNode, vals.shift)
21
- tree.root.new_child vals.shift
22
- tree.root.new_child vals.shift
23
- tree.root.children.each { |c|
24
- c.new_child vals.shift
25
- c.new_child vals.shift
26
-
27
- c.children.each { |cc|
28
- cc.new_child vals.shift
29
- cc.new_child vals.shift
30
- }
31
- }
32
-
33
- visited = []
34
- tree.df_search { |n|
35
- visited << n.value
36
- false
37
- }
38
- visited.wont_be_empty
39
- visited.must_equal [0, 1, 3, 5, 6, 4, 7, 8, 2, 9, 11, 12, 10, 13, 14]
40
- end
41
-
42
- it "does breadth_first search" do
43
- vals = (0..30).to_a
44
- tree = Tree.new(FlexNode, vals.shift)
45
- tree.root.new_child vals.shift
46
- tree.root.new_child vals.shift
47
- tree.root.children.each { |c|
48
- c.new_child vals.shift
49
- c.new_child vals.shift
50
-
51
- c.children.each { |cc|
52
- cc.new_child vals.shift
53
- cc.new_child vals.shift
54
- }
55
- }
56
-
57
- visited = []
58
- tree.bf_search { |n|
59
- visited << n.value
60
- false
61
- }
62
- visited.wont_be_empty
63
- visited.must_equal [0, 1, 2, 3, 4, 9, 10, 5, 6, 7, 8, 11, 12, 13, 14]
64
- end
65
- end
66
-
67
- describe NaryTree do
68
- it "must power_of?" do
69
- powers = {}
70
- basemax = 12
71
- expmax = 10
72
- 2.upto(basemax) { |base|
73
- 0.upto(expmax) { |exp|
74
- powers[base] ||= []
75
- powers[base] << base**exp
76
- }
77
- }
78
-
79
- # 12k assertions below!
80
- 2.upto(basemax) { |base|
81
- 1.upto(2**expmax) { |num|
82
- if powers[base].include?(num)
83
- NaryTree.power_of?(num, base).must_equal true
84
- else
85
- NaryTree.power_of?(num, base).must_equal false
86
- end
87
- }
88
- }
89
- end
90
-
91
- describe "with FlexNode" do
92
- before do
93
- @tree = NaryTree.new(FlexNode, 42, child_slots: 3)
94
- end
95
-
96
- it "must have an open parent" do
97
- @tree.open_parent?(@tree.root).must_equal true
98
- @tree.open_parent?(@tree.open_parent).must_equal true
99
- end
100
-
101
- it "must push a value onto an open parent" do
102
- op = @tree.open_parent
103
- opc = op.children.size
104
- @tree.push 5
105
- @tree.open_parent.children.size.must_equal opc + 1
106
- end
107
- end
108
-
109
- describe "with ChildFlexNode" do
110
- before do
111
- @tree = NaryTree.new(ChildFlexNode, 42, child_slots: 4)
112
- end
113
-
114
- it "must have an open parent" do
115
- @tree.open_parent?(@tree.root).must_equal true
116
- @tree.open_parent?(@tree.open_parent).must_equal true
117
- end
118
-
119
- it "must push a value onto an open parent" do
120
- op = @tree.open_parent
121
- opc = op.children.size
122
- @tree.push 5
123
- @tree.open_parent.children.size.must_equal opc + 1
124
- end
125
- end
126
- end
127
-
128
- describe "BinaryTree" do
129
- before do
130
- @tree = BinaryTree.new(FlexNode, 42)
131
- end
132
-
133
- it "must have 2 child_slots" do
134
- @tree.child_slots.must_equal 2
135
- end
136
-
137
- it "must to_s" do
138
- item_count = 31
139
- # tree already has a root node
140
- (item_count - 1).times { @tree.push rand 99 }
141
- str = @tree.to_s
142
- line_count = str.split("\n").size
143
- line_count.must_equal Math.log(item_count + 1, 2).ceil
144
- end
145
-
146
- describe "searching" do
147
- before do
148
- @tree = NaryTree.new(FlexNode, 42, child_slots: 2)
149
- 99.times { |i| @tree.push i }
150
- end
151
-
152
- it "must find 42 quickly" do
153
- count = 0
154
- @tree.df_search { |n|
155
- count += 1
156
- n.value == 42
157
- }.must_equal @tree.root
158
- count.must_equal 1
159
-
160
- count = 0
161
- @tree.bf_search { |n|
162
- count += 1
163
- n.value == 42
164
- }.must_equal @tree.root
165
- count.must_equal 1
166
- end
167
-
168
- it "must find 99 slowly" do
169
- count = 0
170
- @tree.df_search { |n|
171
- count += 1
172
- n.value == 99
173
- }
174
- count.must_equal 100
175
-
176
- count = 0
177
- @tree.bf_search { |n|
178
- count += 1
179
- n.value == 99
180
- }
181
- count.must_equal 100
182
- end
183
-
184
- it "must find 81 accordingly" do
185
- count = 0
186
- @tree.df_search { |n|
187
- count += 1
188
- n.value == 81
189
- }
190
- count.must_equal 42
191
-
192
- count = 0
193
- @tree.bf_search { |n|
194
- count += 1
195
- n.value == 81
196
- }
197
- count.must_equal 83
198
- end
199
- end
200
- end