hamster 0.1.2 → 0.1.5
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.
- data/History.rdoc +13 -1
- data/README.rdoc +72 -25
- data/TODO +1 -0
- data/lib/hamster.rb +4 -1
- data/lib/hamster/hash.rb +66 -0
- data/lib/hamster/list.rb +105 -0
- data/lib/hamster/set.rb +65 -0
- data/lib/hamster/stack.rb +53 -0
- data/lib/hamster/trie.rb +45 -28
- data/lib/hamster/version.rb +1 -1
- data/spec/hamster/hash/copying_spec.rb +25 -0
- data/spec/hamster/hash/each_spec.rb +47 -0
- data/spec/hamster/hash/empty_spec.rb +17 -0
- data/spec/hamster/hash/eql_spec.rb +20 -0
- data/spec/hamster/hash/get_spec.rb +22 -0
- data/spec/hamster/hash/has_key_spec.rb +21 -0
- data/spec/hamster/hash/put_spec.rb +93 -0
- data/spec/hamster/hash/remove_spec.rb +113 -0
- data/spec/hamster/list/accessor_spec.rb +26 -0
- data/spec/hamster/list/car_spec.rb +17 -0
- data/spec/hamster/list/copying_spec.rb +25 -0
- data/spec/hamster/list/each_spec.rb +45 -0
- data/spec/hamster/list/empty_spec.rb +18 -0
- data/spec/hamster/list/eql_spec.rb +22 -0
- data/spec/hamster/list/map_spec.rb +43 -0
- data/spec/hamster/list/reduce_spec.rb +31 -0
- data/spec/hamster/stack/copying_spec.rb +25 -0
- data/spec/hamster/stack/empty_spec.rb +17 -0
- data/spec/hamster/stack/eql_spec.rb +20 -0
- data/spec/hamster/stack/push_spec.rb +42 -0
- metadata +28 -12
- data/lib/hamster/entry.rb +0 -18
- data/spec/hamster/trie/each_spec.rb +0 -51
- data/spec/hamster/trie/empty_spec.rb +0 -22
- data/spec/hamster/trie/enumerable_spec.rb +0 -13
- data/spec/hamster/trie/get_spec.rb +0 -26
- data/spec/hamster/trie/has_key_spec.rb +0 -25
- data/spec/hamster/trie/put_spec.rb +0 -97
- data/spec/hamster/trie/remove_spec.rb +0 -115
data/lib/hamster/version.rb
CHANGED
@@ -0,0 +1,25 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
|
2
|
+
|
3
|
+
describe Hamster::Hash do
|
4
|
+
|
5
|
+
before do
|
6
|
+
@hash = Hamster::Hash.new
|
7
|
+
end
|
8
|
+
|
9
|
+
describe "#dup" do
|
10
|
+
|
11
|
+
it "returns self" do
|
12
|
+
@hash.dup.should equal(@hash)
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
16
|
+
|
17
|
+
describe "#clone" do
|
18
|
+
|
19
|
+
it "returns self" do
|
20
|
+
@hash.clone.should equal(@hash)
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
|
2
|
+
|
3
|
+
describe Hamster::Hash do
|
4
|
+
|
5
|
+
describe "#each" do
|
6
|
+
|
7
|
+
before do
|
8
|
+
@hash = Hamster::Hash.new
|
9
|
+
@expected_pairs = { "A" => "aye", "B" => "bee", "C" => "sea" }
|
10
|
+
@expected_pairs.each do |key, value|
|
11
|
+
@hash = @hash.put(key, value)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
describe "with a block (internal iteration)" do
|
16
|
+
|
17
|
+
it "returns self" do
|
18
|
+
@hash.each {}.should equal(@hash)
|
19
|
+
end
|
20
|
+
|
21
|
+
it "yields all key value pairs" do
|
22
|
+
actual_pairs = {}
|
23
|
+
@hash.each do |key, value|
|
24
|
+
actual_pairs[key] = value
|
25
|
+
end
|
26
|
+
actual_pairs.should == @expected_pairs
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
|
31
|
+
describe "with no block (external iteration)" do
|
32
|
+
|
33
|
+
it "returns an enumerator over all key value pairs" do
|
34
|
+
actual_pairs = {}
|
35
|
+
enum = @hash.each
|
36
|
+
loop do
|
37
|
+
key, value = enum.next
|
38
|
+
actual_pairs[key] = value
|
39
|
+
end
|
40
|
+
actual_pairs.should == @expected_pairs
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
|
2
|
+
|
3
|
+
describe Hamster::Hash do
|
4
|
+
|
5
|
+
describe "#empty?" do
|
6
|
+
|
7
|
+
it "initially returns true" do
|
8
|
+
Hamster::Hash.new.should be_empty
|
9
|
+
end
|
10
|
+
|
11
|
+
it "returns false once items have been added" do
|
12
|
+
Hamster::Hash.new.put("A", "aye").should_not be_empty
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
|
2
|
+
|
3
|
+
describe Hamster::Hash do
|
4
|
+
|
5
|
+
describe "#eql?" do
|
6
|
+
|
7
|
+
it "is true for the same instance" do
|
8
|
+
hash = Hamster::Hash.new
|
9
|
+
hash.should eql(hash)
|
10
|
+
end
|
11
|
+
|
12
|
+
it "is true for two empty instances" do
|
13
|
+
pending do
|
14
|
+
Hamster::Hash.new.should eql(Hamster::Hash.new)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
|
2
|
+
|
3
|
+
describe Hamster::Hash do
|
4
|
+
|
5
|
+
describe "#get" do
|
6
|
+
|
7
|
+
before do
|
8
|
+
@hash = Hamster::Hash.new
|
9
|
+
@hash = @hash.put("A", "aye")
|
10
|
+
end
|
11
|
+
|
12
|
+
it "returns the value for an existing key" do
|
13
|
+
@hash.get("A").should == "aye"
|
14
|
+
end
|
15
|
+
|
16
|
+
it "returns nil for a non-existing key" do
|
17
|
+
@hash.get("B").should be_nil
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
|
2
|
+
|
3
|
+
describe Hamster::Hash do
|
4
|
+
|
5
|
+
describe "#has_key?" do
|
6
|
+
|
7
|
+
before do
|
8
|
+
@hash = Hamster::Hash.new.put("A", "aye")
|
9
|
+
end
|
10
|
+
|
11
|
+
it "returns true for an existing key" do
|
12
|
+
@hash.has_key?("A").should be_true
|
13
|
+
end
|
14
|
+
|
15
|
+
it "returns false for a non-existing key" do
|
16
|
+
@hash.has_key?("B").should be_false
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
@@ -0,0 +1,93 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
|
2
|
+
|
3
|
+
describe Hamster::Hash do
|
4
|
+
|
5
|
+
describe "#put" do
|
6
|
+
|
7
|
+
describe "with a key/value pair that already exists" do
|
8
|
+
|
9
|
+
before do
|
10
|
+
@original = Hamster::Hash.new.put("A", "aye").put("B", "bee")
|
11
|
+
@copy = @original.put("A", "yes")
|
12
|
+
end
|
13
|
+
|
14
|
+
it "returns a modified copy" do
|
15
|
+
@copy.should_not equal(@original)
|
16
|
+
end
|
17
|
+
|
18
|
+
describe "the original" do
|
19
|
+
|
20
|
+
it "still has the original key/value pairs" do
|
21
|
+
@original.get("A").should == "aye"
|
22
|
+
@original.get("B").should == "bee"
|
23
|
+
end
|
24
|
+
|
25
|
+
it "still has the original size" do
|
26
|
+
@original.size.should == 2
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
|
31
|
+
describe "the modified copy" do
|
32
|
+
|
33
|
+
it "has the new key/value pairs" do
|
34
|
+
@copy.get("A").should == "yes"
|
35
|
+
@copy.get("B").should == "bee"
|
36
|
+
end
|
37
|
+
|
38
|
+
it "has the original size" do
|
39
|
+
@copy.size == 2
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
|
46
|
+
describe "with a key/value pair that doesn't exist" do
|
47
|
+
|
48
|
+
before do
|
49
|
+
@original = Hamster::Hash.new.put("A", "aye")
|
50
|
+
@copy = @original.put("B", "bee")
|
51
|
+
end
|
52
|
+
|
53
|
+
it "returns a modified copy" do
|
54
|
+
@copy.should_not equal(@original)
|
55
|
+
end
|
56
|
+
|
57
|
+
describe "the original" do
|
58
|
+
|
59
|
+
it "still has the original key/value pairs" do
|
60
|
+
@original.get("A").should == "aye"
|
61
|
+
end
|
62
|
+
|
63
|
+
it "doesn't contain the new key/value pair" do
|
64
|
+
@original.has_key?("B").should be_false
|
65
|
+
end
|
66
|
+
|
67
|
+
it "still has the original size" do
|
68
|
+
@original.size.should == 1
|
69
|
+
end
|
70
|
+
|
71
|
+
end
|
72
|
+
|
73
|
+
describe "the modified copy" do
|
74
|
+
|
75
|
+
it "has the original key/value pairs" do
|
76
|
+
@copy.get("A").should == "aye"
|
77
|
+
end
|
78
|
+
|
79
|
+
it "has the new key/value pair" do
|
80
|
+
@copy.get("B").should == "bee"
|
81
|
+
end
|
82
|
+
|
83
|
+
it "size is increased by one" do
|
84
|
+
@copy.size.should == 2
|
85
|
+
end
|
86
|
+
|
87
|
+
end
|
88
|
+
|
89
|
+
end
|
90
|
+
|
91
|
+
end
|
92
|
+
|
93
|
+
end
|
@@ -0,0 +1,113 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
|
2
|
+
|
3
|
+
describe Hamster::Hash do
|
4
|
+
|
5
|
+
describe "#remove" do
|
6
|
+
|
7
|
+
describe "with an existing key" do
|
8
|
+
|
9
|
+
before do
|
10
|
+
@original = Hamster::Hash.new.put("A", "aye").put("B", "bee")
|
11
|
+
@copy = @original.remove("A")
|
12
|
+
end
|
13
|
+
|
14
|
+
it "returns a modified copy" do
|
15
|
+
@copy.should_not equal(@original)
|
16
|
+
end
|
17
|
+
|
18
|
+
describe "the original" do
|
19
|
+
|
20
|
+
it "still has the original key/value pairs" do
|
21
|
+
@original.get("A").should == "aye"
|
22
|
+
@original.get("B").should == "bee"
|
23
|
+
end
|
24
|
+
|
25
|
+
it "still has the original size" do
|
26
|
+
@original.size.should == 2
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
|
31
|
+
describe "the modified copy" do
|
32
|
+
|
33
|
+
it "has all but the removed original key/value pairs" do
|
34
|
+
@copy.get("B").should == "bee"
|
35
|
+
end
|
36
|
+
|
37
|
+
it "doesn't have the removed key" do
|
38
|
+
@copy.has_key?("A").should be_false
|
39
|
+
end
|
40
|
+
|
41
|
+
it "has a size one less than the original" do
|
42
|
+
@copy.size.should == 1
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
|
49
|
+
describe "with non-existing keys" do
|
50
|
+
|
51
|
+
before do
|
52
|
+
@original = Hamster::Hash.new.put("A", "aye")
|
53
|
+
@copy = @original.remove("missing")
|
54
|
+
end
|
55
|
+
|
56
|
+
it "returns self" do
|
57
|
+
@copy.should equal(@original)
|
58
|
+
end
|
59
|
+
|
60
|
+
describe "the original" do
|
61
|
+
|
62
|
+
it "still has the original key/value pairs" do
|
63
|
+
@original.get("A").should == "aye"
|
64
|
+
end
|
65
|
+
|
66
|
+
it "still has the original size" do
|
67
|
+
@original.size.should == 1
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
71
|
+
|
72
|
+
end
|
73
|
+
|
74
|
+
describe "with keys of the same hash value" do
|
75
|
+
|
76
|
+
class Key
|
77
|
+
def hash; 1; end
|
78
|
+
end
|
79
|
+
|
80
|
+
def instance_count
|
81
|
+
ObjectSpace.garbage_collect
|
82
|
+
ObjectSpace.each_object(Hamster::Hash) {}
|
83
|
+
end
|
84
|
+
|
85
|
+
before do
|
86
|
+
@a = Key.new
|
87
|
+
@b = Key.new
|
88
|
+
@original = Hamster::Hash.new.put(@a, "aye").put(@b, "bee")
|
89
|
+
end
|
90
|
+
|
91
|
+
it "no longer provides access to the removed key" do
|
92
|
+
copy = @original.remove(@b)
|
93
|
+
copy.has_key?(@b).should be_false
|
94
|
+
end
|
95
|
+
|
96
|
+
it "provides access to the remaining keys" do
|
97
|
+
copy = @original.remove(@a)
|
98
|
+
copy.get(@b).should == "bee"
|
99
|
+
end
|
100
|
+
|
101
|
+
it "cleans up empty instances" do
|
102
|
+
pending do
|
103
|
+
instance_count_before = instance_count
|
104
|
+
# copy = @original.remove(@b)
|
105
|
+
instance_count.should == instance_count_before + 1
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
end
|
110
|
+
|
111
|
+
end
|
112
|
+
|
113
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
|
2
|
+
|
3
|
+
describe Hamster::List do
|
4
|
+
|
5
|
+
(1..5).each do |i|
|
6
|
+
|
7
|
+
method_name = "ca#{'d' * i}r"
|
8
|
+
values = (1..6).to_a
|
9
|
+
expected_result = i + 1
|
10
|
+
|
11
|
+
describe "##{method_name}" do
|
12
|
+
|
13
|
+
it "initially returns nil" do
|
14
|
+
Hamster::List.new.send(method_name).should be_nil
|
15
|
+
end
|
16
|
+
|
17
|
+
it "with #{values} is #{i + 1}" do
|
18
|
+
list = values.reverse.inject(Hamster::List.new) { |list, i| list.cons(i) }
|
19
|
+
list.send(method_name).should == expected_result
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
|
2
|
+
|
3
|
+
describe Hamster::List do
|
4
|
+
|
5
|
+
describe "#car" do
|
6
|
+
|
7
|
+
it "initially returns nil" do
|
8
|
+
Hamster::List.new.car.should be_nil
|
9
|
+
end
|
10
|
+
|
11
|
+
it "returns the first item in the list" do
|
12
|
+
Hamster::List.new.cons("A").car.should == "A"
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
|
2
|
+
|
3
|
+
describe Hamster::List do
|
4
|
+
|
5
|
+
before do
|
6
|
+
@list = Hamster::List.new
|
7
|
+
end
|
8
|
+
|
9
|
+
describe "#dup" do
|
10
|
+
|
11
|
+
it "returns self" do
|
12
|
+
@list.dup.should equal(@list)
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
16
|
+
|
17
|
+
describe "#clone" do
|
18
|
+
|
19
|
+
it "returns self" do
|
20
|
+
@list.clone.should equal(@list)
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|