compsci 0.0.1.1
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 +7 -0
- data/README.md +48 -0
- data/Rakefile +108 -0
- data/VERSION +1 -0
- data/compsci.gemspec +38 -0
- data/examples/binary_tree.rb +21 -0
- data/examples/heap.rb +65 -0
- data/examples/timer.rb +42 -0
- data/lib/compsci/fib.rb +24 -0
- data/lib/compsci/fit.rb +137 -0
- data/lib/compsci/heap.rb +100 -0
- data/lib/compsci/timer.rb +36 -0
- data/lib/compsci/tree.rb +140 -0
- data/lib/compsci.rb +2 -0
- data/test/bench/fib.rb +114 -0
- data/test/bench/heap.rb +15 -0
- data/test/bench/tree.rb +19 -0
- data/test/fib.rb +13 -0
- data/test/fit.rb +162 -0
- data/test/heap.rb +46 -0
- data/test/timer.rb +48 -0
- data/test/tree.rb +209 -0
- metadata +78 -0
data/test/tree.rb
ADDED
@@ -0,0 +1,209 @@
|
|
1
|
+
require 'compsci/tree'
|
2
|
+
require 'minitest/autorun'
|
3
|
+
|
4
|
+
include CompSci
|
5
|
+
|
6
|
+
describe Tree do
|
7
|
+
before do
|
8
|
+
@node = Tree::Node.new 42
|
9
|
+
@tree = Tree.new(@node, child_slots: 3)
|
10
|
+
end
|
11
|
+
|
12
|
+
it "must have an open parent" do
|
13
|
+
@tree.open_parent?(@node).must_equal true
|
14
|
+
@tree.open_parent?(@tree.root).must_equal true
|
15
|
+
@tree.open_parent?(@tree.open_parent).must_equal true
|
16
|
+
end
|
17
|
+
|
18
|
+
it "must push a value onto an open parent" do
|
19
|
+
op = @tree.open_parent
|
20
|
+
opc = op.children.size
|
21
|
+
@tree.push 5
|
22
|
+
@tree.open_parent.children.size.must_equal opc + 1
|
23
|
+
end
|
24
|
+
|
25
|
+
describe "searching" do
|
26
|
+
before do
|
27
|
+
@tree = Tree.new(Tree::Node.new 42)
|
28
|
+
99.times { |i| @tree.push i }
|
29
|
+
end
|
30
|
+
|
31
|
+
it "must find 42 quickly" do
|
32
|
+
count = 0
|
33
|
+
@tree.df_search { |n|
|
34
|
+
count += 1
|
35
|
+
n.value == 42
|
36
|
+
}.must_equal @tree.root
|
37
|
+
count.must_equal 1
|
38
|
+
|
39
|
+
count = 0
|
40
|
+
@tree.bf_search { |n|
|
41
|
+
count += 1
|
42
|
+
n.value == 42
|
43
|
+
}.must_equal @tree.root
|
44
|
+
count.must_equal 1
|
45
|
+
end
|
46
|
+
|
47
|
+
it "must find 99 slowly" do
|
48
|
+
count = 0
|
49
|
+
@tree.df_search { |n|
|
50
|
+
count += 1
|
51
|
+
n.value == 99
|
52
|
+
}
|
53
|
+
count.must_equal 100
|
54
|
+
|
55
|
+
count = 0
|
56
|
+
@tree.bf_search { |n|
|
57
|
+
count += 1
|
58
|
+
n.value == 99
|
59
|
+
}
|
60
|
+
count.must_equal 100
|
61
|
+
end
|
62
|
+
|
63
|
+
it "must find 81 accordingly" do
|
64
|
+
count = 0
|
65
|
+
@tree.df_search { |n|
|
66
|
+
count += 1
|
67
|
+
n.value == 81
|
68
|
+
}
|
69
|
+
count.must_equal 42
|
70
|
+
|
71
|
+
count = 0
|
72
|
+
@tree.bf_search { |n|
|
73
|
+
count += 1
|
74
|
+
n.value == 81
|
75
|
+
}
|
76
|
+
count.must_equal 83
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
describe Tree::Node do
|
81
|
+
before do
|
82
|
+
@martin_sheen = Tree::Node.new 'martin'
|
83
|
+
@charlie_sheen = Tree::Node.new 'charlie'
|
84
|
+
@emilio_estevez = Tree::Node.new 'emilio'
|
85
|
+
end
|
86
|
+
|
87
|
+
it "must start with no relations" do
|
88
|
+
[@martin_sheen, @charlie_sheen, @emilio_estevez].each { |n|
|
89
|
+
n.parent.nil?.must_equal true
|
90
|
+
n.children.must_be_empty
|
91
|
+
}
|
92
|
+
end
|
93
|
+
|
94
|
+
it "must allow relations" do
|
95
|
+
@charlie_sheen.add_parent(@martin_sheen)
|
96
|
+
@charlie_sheen.parent.must_equal @martin_sheen
|
97
|
+
@martin_sheen.children.must_include @charlie_sheen
|
98
|
+
|
99
|
+
@martin_sheen.children.wont_include @emilio_estevez
|
100
|
+
@martin_sheen.add_child @emilio_estevez
|
101
|
+
@martin_sheen.children.must_include @emilio_estevez
|
102
|
+
@emilio_estevez.parent.must_equal @martin_sheen
|
103
|
+
end
|
104
|
+
|
105
|
+
it "must create children from scalars" do
|
106
|
+
@martin_sheen.new_child 'fake_emilio'
|
107
|
+
@martin_sheen.children.size.must_equal 1
|
108
|
+
@martin_sheen.children.first.value.must_equal 'fake_emilio'
|
109
|
+
@martin_sheen.children.wont_include @emilio_estevez
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
describe BinaryTree do
|
115
|
+
before do
|
116
|
+
@tree = BinaryTree.new(Tree::Node.new 42)
|
117
|
+
end
|
118
|
+
|
119
|
+
it "must have 2 child_slots" do
|
120
|
+
@tree.child_slots.must_equal 2
|
121
|
+
end
|
122
|
+
|
123
|
+
it "must bf_print" do
|
124
|
+
31.times { @tree.push rand 99 }
|
125
|
+
out, err = capture_io do
|
126
|
+
@tree.bf_print
|
127
|
+
end
|
128
|
+
line_count = out.split("\n").size
|
129
|
+
line_count.must_be :>, 4
|
130
|
+
line_count.must_be :<, 7
|
131
|
+
err.must_be_empty
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
describe CompleteBinaryTree do
|
136
|
+
it "must calculate a parent index" do
|
137
|
+
valid = {
|
138
|
+
1 => 0,
|
139
|
+
2 => 0,
|
140
|
+
3 => 1,
|
141
|
+
4 => 1,
|
142
|
+
5 => 2,
|
143
|
+
6 => 2,
|
144
|
+
7 => 3,
|
145
|
+
8 => 3,
|
146
|
+
9 => 4,
|
147
|
+
10 => 4,
|
148
|
+
}
|
149
|
+
|
150
|
+
invalid = {
|
151
|
+
0 => -1,
|
152
|
+
-1 => -1,
|
153
|
+
-2 => -2,
|
154
|
+
}
|
155
|
+
valid.each { |idx, pidx|
|
156
|
+
CompleteBinaryTree.parent_idx(idx).must_equal pidx
|
157
|
+
}
|
158
|
+
invalid.each { |idx, pidx|
|
159
|
+
CompleteBinaryTree.parent_idx(idx).must_equal pidx
|
160
|
+
}
|
161
|
+
end
|
162
|
+
|
163
|
+
it "must calculate children indices" do
|
164
|
+
valid = {
|
165
|
+
0 => [1, 2],
|
166
|
+
1 => [3, 4],
|
167
|
+
2 => [5, 6],
|
168
|
+
3 => [7, 8],
|
169
|
+
4 => [9, 10],
|
170
|
+
5 => [11, 12],
|
171
|
+
6 => [13, 14],
|
172
|
+
7 => [15, 16],
|
173
|
+
8 => [17, 18],
|
174
|
+
9 => [19, 20],
|
175
|
+
10 => [21, 22],
|
176
|
+
}
|
177
|
+
|
178
|
+
invalid = {
|
179
|
+
-3 => [-5, -4],
|
180
|
+
-2 => [-3, -2],
|
181
|
+
-1 => [-1, 0],
|
182
|
+
}
|
183
|
+
|
184
|
+
valid.each { |idx, cidx|
|
185
|
+
CompleteBinaryTree.children_idx(idx).must_equal cidx
|
186
|
+
}
|
187
|
+
invalid.each { |idx, cidx|
|
188
|
+
CompleteBinaryTree.children_idx(idx).must_equal cidx
|
189
|
+
}
|
190
|
+
end
|
191
|
+
|
192
|
+
describe "instance" do
|
193
|
+
before do
|
194
|
+
@array = (0..99).sort_by { rand }
|
195
|
+
@empty = CompleteBinaryTree.new
|
196
|
+
@nonempty = CompleteBinaryTree.new(store: @array)
|
197
|
+
end
|
198
|
+
|
199
|
+
it "must have a size" do
|
200
|
+
@empty.size.must_equal 0
|
201
|
+
@nonempty.size.must_equal @array.size
|
202
|
+
end
|
203
|
+
|
204
|
+
it "must have a last_idx, nil when empty" do
|
205
|
+
@empty.last_idx.nil?.must_equal true
|
206
|
+
@nonempty.last_idx.must_equal 99
|
207
|
+
end
|
208
|
+
end
|
209
|
+
end
|
metadata
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: compsci
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Rick Hull
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-10-26 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: minitest
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '5.0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '5.0'
|
27
|
+
description: Trees, Heaps, Timers, Error fitting, etc
|
28
|
+
email:
|
29
|
+
executables: []
|
30
|
+
extensions: []
|
31
|
+
extra_rdoc_files: []
|
32
|
+
files:
|
33
|
+
- README.md
|
34
|
+
- Rakefile
|
35
|
+
- VERSION
|
36
|
+
- compsci.gemspec
|
37
|
+
- examples/binary_tree.rb
|
38
|
+
- examples/heap.rb
|
39
|
+
- examples/timer.rb
|
40
|
+
- lib/compsci.rb
|
41
|
+
- lib/compsci/fib.rb
|
42
|
+
- lib/compsci/fit.rb
|
43
|
+
- lib/compsci/heap.rb
|
44
|
+
- lib/compsci/timer.rb
|
45
|
+
- lib/compsci/tree.rb
|
46
|
+
- test/bench/fib.rb
|
47
|
+
- test/bench/heap.rb
|
48
|
+
- test/bench/tree.rb
|
49
|
+
- test/fib.rb
|
50
|
+
- test/fit.rb
|
51
|
+
- test/heap.rb
|
52
|
+
- test/timer.rb
|
53
|
+
- test/tree.rb
|
54
|
+
homepage: https://github.com/rickhull/compsci
|
55
|
+
licenses:
|
56
|
+
- LGPL-3.0
|
57
|
+
metadata: {}
|
58
|
+
post_install_message:
|
59
|
+
rdoc_options: []
|
60
|
+
require_paths:
|
61
|
+
- lib
|
62
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
63
|
+
requirements:
|
64
|
+
- - "~>"
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: '2'
|
67
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
68
|
+
requirements:
|
69
|
+
- - ">="
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: '0'
|
72
|
+
requirements: []
|
73
|
+
rubyforge_project:
|
74
|
+
rubygems_version: 2.6.8
|
75
|
+
signing_key:
|
76
|
+
specification_version: 4
|
77
|
+
summary: Toy implementations for some basic computer science problems
|
78
|
+
test_files: []
|