hamster 0.1.8 → 0.1.11

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.
Files changed (69) hide show
  1. data/README.rdoc +34 -10
  2. data/lib/hamster.rb +0 -1
  3. data/lib/hamster/hash.rb +77 -20
  4. data/lib/hamster/list.rb +189 -52
  5. data/lib/hamster/set.rb +78 -23
  6. data/lib/hamster/stack.rb +13 -14
  7. data/lib/hamster/trie.rb +13 -4
  8. data/lib/hamster/version.rb +1 -1
  9. data/spec/hamster/hash/all_spec.rb +53 -0
  10. data/spec/hamster/hash/any_spec.rb +62 -0
  11. data/spec/hamster/hash/construction_spec.rb +21 -5
  12. data/spec/hamster/hash/copying_spec.rb +6 -10
  13. data/spec/hamster/hash/each_spec.rb +6 -18
  14. data/spec/hamster/hash/empty_spec.rb +9 -5
  15. data/spec/hamster/hash/eql_spec.rb +17 -22
  16. data/spec/hamster/hash/filter_spec.rb +61 -0
  17. data/spec/hamster/hash/get_spec.rb +24 -12
  18. data/spec/hamster/hash/has_key_spec.rb +17 -13
  19. data/spec/hamster/hash/map_spec.rb +66 -0
  20. data/spec/hamster/hash/none_spec.rb +62 -0
  21. data/spec/hamster/hash/put_spec.rb +17 -73
  22. data/spec/hamster/hash/reduce_spec.rb +58 -0
  23. data/spec/hamster/hash/reject_spec.rb +57 -0
  24. data/spec/hamster/hash/remove_spec.rb +13 -85
  25. data/spec/hamster/hash/size_spec.rb +25 -0
  26. data/spec/hamster/list/cadr_spec.rb +37 -0
  27. data/spec/hamster/list/construction_spec.rb +46 -6
  28. data/spec/hamster/list/copying_spec.rb +13 -11
  29. data/spec/hamster/list/drop_spec.rb +29 -0
  30. data/spec/hamster/list/drop_while_spec.rb +39 -0
  31. data/spec/hamster/list/each_spec.rb +24 -24
  32. data/spec/hamster/list/empty_spec.rb +15 -6
  33. data/spec/hamster/list/eql_spec.rb +27 -7
  34. data/spec/hamster/list/filter_spec.rb +51 -0
  35. data/spec/hamster/list/head_spec.rb +27 -0
  36. data/spec/hamster/list/include_spec.rb +37 -0
  37. data/spec/hamster/list/lazy_spec.rb +21 -0
  38. data/spec/hamster/list/map_spec.rb +21 -19
  39. data/spec/hamster/list/reduce_spec.rb +27 -15
  40. data/spec/hamster/list/reject_spec.rb +47 -0
  41. data/spec/hamster/list/size_spec.rb +31 -0
  42. data/spec/hamster/list/tail_spec.rb +27 -0
  43. data/spec/hamster/list/take_spec.rb +29 -0
  44. data/spec/hamster/list/take_while_spec.rb +45 -0
  45. data/spec/hamster/set/add_spec.rb +49 -0
  46. data/spec/hamster/set/all_spec.rb +61 -0
  47. data/spec/hamster/set/any_spec.rb +61 -0
  48. data/spec/hamster/set/construction_spec.rb +3 -3
  49. data/spec/hamster/set/copying_spec.rb +21 -0
  50. data/spec/hamster/set/each_spec.rb +36 -0
  51. data/spec/hamster/set/empty_spec.rb +21 -0
  52. data/spec/hamster/set/eql_spec.rb +31 -0
  53. data/spec/hamster/set/filter_spec.rb +61 -0
  54. data/spec/hamster/set/include_spec.rb +29 -0
  55. data/spec/hamster/set/map_spec.rb +66 -0
  56. data/spec/hamster/set/none_spec.rb +61 -0
  57. data/spec/hamster/set/reduce_spec.rb +58 -0
  58. data/spec/hamster/set/reject_spec.rb +57 -0
  59. data/spec/hamster/set/remove_spec.rb +45 -0
  60. data/spec/hamster/set/size_spec.rb +25 -0
  61. data/spec/hamster/stack/copying_spec.rb +1 -1
  62. data/spec/hamster/stack/empty_spec.rb +2 -2
  63. data/spec/hamster/stack/eql_spec.rb +15 -4
  64. data/spec/hamster/stack/push_spec.rb +6 -26
  65. data/spec/hamster/trie/remove_spec.rb +117 -0
  66. metadata +39 -7
  67. data/TODO +0 -1
  68. data/spec/hamster/list/accessor_spec.rb +0 -26
  69. data/spec/hamster/list/car_spec.rb +0 -17
@@ -4,12 +4,16 @@ describe Hamster::Hash do
4
4
 
5
5
  describe "#empty?" do
6
6
 
7
- it "initially returns true" do
8
- Hamster::Hash.new.should be_empty
9
- end
7
+ [
8
+ [[], true],
9
+ [["A" => "aye"], false],
10
+ [["A" => "aye", "B" => "bee", "C" => "see"], false],
11
+ ].each do |pairs, result|
12
+
13
+ it "returns #{result} for #{pairs.inspect}" do
14
+ Hamster.hash(*pairs).empty?.should == result
15
+ end
10
16
 
11
- it "returns false once items have been added" do
12
- Hamster::Hash.new.put("A", "aye").should_not be_empty
13
17
  end
14
18
 
15
19
  end
@@ -2,33 +2,28 @@ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
2
 
3
3
  describe Hamster::Hash do
4
4
 
5
- describe "#eql?" do
5
+ [:eql?, :==].each do |method|
6
6
 
7
- it "is true for the same instance" do
8
- hash = Hamster::Hash.new
9
- hash.should eql(hash)
10
- end
7
+ describe "##{method}" do
11
8
 
12
- it "is true for two empty instances" do
13
- Hamster::Hash.new.should eql(Hamster::Hash.new)
14
- end
9
+ [
10
+ [[], [], true],
11
+ [["A" => "aye"], [], false],
12
+ [[], ["A" => "aye"], false],
13
+ [["A" => "aye"], ["A" => "aye"], true],
14
+ [["A" => "aye"], ["B" => "bee"], false],
15
+ [["A" => "aye", "B" => "bee"], ["A" => "aye"], false],
16
+ [["A" => "aye"], ["A" => "aye", "B" => "bee"], false],
17
+ [["A" => "aye", "B" => "bee", "C" => "see"], ["A" => "aye", "B" => "bee", "C" => "see"], true],
18
+ [["C" => "see", "A" => "aye", "B" => "bee"], ["A" => "aye", "B" => "bee", "C" => "see"], true],
19
+ ].each do |a, b, result|
15
20
 
16
- it "is true for two instances with the same key/value pairs" do
17
- a = Hamster::Hash.new.put("a", "Aye").put("b", "Bee").put("c", "See")
18
- b = Hamster::Hash.new.put("a", "Aye").put("b", "Bee").put("c", "See")
19
- a.should eql(b)
20
- end
21
+ it "returns #{result} for #{a.inspect} and #{b.inspect}" do
22
+ Hamster.hash(*a).send(method, Hamster.hash(*b)).should == result
23
+ end
21
24
 
22
- it "is false for two instances with different key/value pairs" do
23
- a = Hamster::Hash.new.put("a", "Aye").put("b", "Bee").put("c", "See")
24
- b = Hamster::Hash.new.put("a", "Aye").put("b", "Bee").put("d", "Dee")
25
- a.should_not eql(b)
26
- end
25
+ end
27
26
 
28
- it "is false for two instances with different numbers of overlapping key/value pairs" do
29
- a = Hamster::Hash.new.put("a", "Aye").put("b", "Bee").put("c", "See")
30
- b = Hamster::Hash.new.put("a", "Aye").put("b", "Bee")
31
- a.should_not eql(b)
32
27
  end
33
28
 
34
29
  end
@@ -0,0 +1,61 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
+
3
+ describe Hamster::Hash do
4
+
5
+ [:filter, :select].each do |method|
6
+
7
+ describe "##{method}" do
8
+
9
+ before do
10
+ @original = Hamster.hash("A" => "aye", "B" => "bee", "C" => "see")
11
+ end
12
+
13
+ describe "when everything matches" do
14
+
15
+ before do
16
+ @result = @original.send(method) { |key, value| true }
17
+ end
18
+
19
+ it "returns self" do
20
+ @result.should equal(@original)
21
+ end
22
+
23
+ end
24
+
25
+ describe "when only some things match" do
26
+
27
+ describe "with a block" do
28
+
29
+ before do
30
+ @result = @original.send(method) { |key, value| key == "A" && value == "aye" }
31
+ end
32
+
33
+ it "preserves the original" do
34
+ @original.should == Hamster.hash("A" => "aye", "B" => "bee", "C" => "see")
35
+ end
36
+
37
+ it "returns a set with the matching values" do
38
+ @result.should == Hamster.hash("A" => "aye")
39
+ end
40
+
41
+ end
42
+
43
+ describe "with no block" do
44
+
45
+ before do
46
+ @enumerator = @original.send(method)
47
+ end
48
+
49
+ it "returns an enumerator over the values" do
50
+ Hamster.hash(@enumerator.to_a).should == Hamster.hash("A" => "aye", "B" => "bee", "C" => "see")
51
+ end
52
+
53
+ end
54
+
55
+ end
56
+
57
+ end
58
+
59
+ end
60
+
61
+ end
@@ -2,21 +2,33 @@ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
2
 
3
3
  describe Hamster::Hash do
4
4
 
5
- describe "#get" do
5
+ [:get, :[]].each do |method|
6
6
 
7
- before do
8
- @hash = Hamster::Hash.new
9
- @hash = @hash.put("A", "aye")
10
- end
7
+ describe "##{method}" do
11
8
 
12
- it "returns the value for an existing key" do
13
- @hash.get("A").should == "aye"
14
- end
9
+ before do
10
+ @hash = Hamster.hash("A" => "aye", "B" => "bee", "C" => "see", nil => "NIL")
11
+ end
12
+
13
+ [
14
+ ["A", "aye"],
15
+ ["B", "bee"],
16
+ ["C", "see"],
17
+ [nil, "NIL"]
18
+ ].each do |key, value|
19
+
20
+ it "returns the value (#{value.inspect}) for an existing key (#{key.inspect})" do
21
+ @hash.send(method, key).should == value
22
+ end
23
+
24
+ end
25
+
26
+ it "returns nil for a non-existing key" do
27
+ @hash.send(method, "D").should be_nil
28
+ end
29
+
30
+ end
15
31
 
16
- it "returns nil for a non-existing key" do
17
- @hash.get("B").should be_nil
18
32
  end
19
33
 
20
34
  end
21
-
22
- end
@@ -2,22 +2,26 @@ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
2
 
3
3
  describe Hamster::Hash do
4
4
 
5
- describe "#has_key?" do
5
+ [:has_key?, :key?, :include?, :member?].each do |method|
6
6
 
7
- before do
8
- @hash = Hamster::Hash.new.put("A", "aye").put("NIL", nil)
9
- end
7
+ describe "#has_key?" do
10
8
 
11
- it "returns true for an existing key" do
12
- @hash.has_key?("A").should be_true
13
- end
9
+ before do
10
+ @hash = Hamster.hash("A" => "aye", "B" => "bee", "C" => "see", nil => "NIL")
11
+ end
12
+
13
+ ["A", "B", "C", nil].each do |key|
14
+
15
+ it "returns true for an existing key (#{key.inspect})" do
16
+ @hash.send(method, key).should be_true
17
+ end
18
+
19
+ end
20
+
21
+ it "returns false for a non-existing key" do
22
+ @hash.send(method, "D").should be_false
23
+ end
14
24
 
15
- it "returns false for a non-existing key" do
16
- @hash.has_key?("B").should be_false
17
- end
18
-
19
- it "returns true for a nil value" do
20
- @hash.has_key?("NIL").should be_true
21
25
  end
22
26
 
23
27
  end
@@ -0,0 +1,66 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
+
3
+ describe Hamster::Hash do
4
+
5
+ [:map, :collect].each do |method|
6
+
7
+ describe "##{method}" do
8
+
9
+ describe "when empty" do
10
+
11
+ before do
12
+ @original = Hamster.hash
13
+ @mapped = @original.send(method) {}
14
+ end
15
+
16
+ it "returns self" do
17
+ @mapped.should equal(@original)
18
+ end
19
+
20
+ end
21
+
22
+ describe "when not empty" do
23
+
24
+ before do
25
+ @original = Hamster.hash("A" => "aye", "B" => "bee", "C" => "see")
26
+ end
27
+
28
+ describe "with a block" do
29
+
30
+ before do
31
+ @mapped = @original.send(method) { |key, value| [key.downcase, value.upcase] }
32
+ end
33
+
34
+ it "preserves the original values" do
35
+ @original.should == Hamster.hash("A" => "aye", "B" => "bee", "C" => "see")
36
+ end
37
+
38
+ it "returns a new hash with the mapped values" do
39
+ @mapped.should == Hamster.hash("a" => "AYE", "b" => "BEE", "c" => "SEE")
40
+ end
41
+
42
+ end
43
+
44
+ describe "with no block" do
45
+
46
+ before do
47
+ @enumerator = @original.send(method)
48
+ end
49
+
50
+ it "preserves the original values" do
51
+ @original.should == Hamster.hash("A" => "aye", "B" => "bee", "C" => "see")
52
+ end
53
+
54
+ it "returns an enumerator over the key value pairs" do
55
+ Hamster.hash(@enumerator.to_a).should == @original
56
+ end
57
+
58
+ end
59
+
60
+ end
61
+
62
+ end
63
+
64
+ end
65
+
66
+ end
@@ -0,0 +1,62 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
+
3
+ describe Hamster::Hash do
4
+
5
+ describe "#none?" do
6
+
7
+ describe "when empty" do
8
+
9
+ before do
10
+ @hash = Hamster.hash
11
+ end
12
+
13
+ it "with a block returns true" do
14
+ @hash.none? {}.should be_true
15
+ end
16
+
17
+ it "with no block returns true" do
18
+ @hash.none?.should be_true
19
+ end
20
+
21
+ end
22
+
23
+ describe "when not empty" do
24
+
25
+ before do
26
+ @hash = Hamster.hash("A" => "aye", "B" => "bee", "C" => "see", nil => "NIL")
27
+ end
28
+
29
+ describe "with a block" do
30
+
31
+ [
32
+ ["A", "aye"],
33
+ ["B", "bee"],
34
+ ["C", "see"],
35
+ [nil, "NIL"],
36
+ ].each do |pair|
37
+
38
+ it "returns false if the block ever returns true (#{pair.inspect})" do
39
+ @hash.none? { |key, value| key == pair.first && value == pair.last }.should be_false
40
+ end
41
+
42
+ it "returns true if the block always returns false" do
43
+ @hash.none? { |key, value| key == "D" && value == "dee" }.should be_true
44
+ end
45
+
46
+ end
47
+
48
+ end
49
+
50
+ describe "with no block" do
51
+
52
+ it "returns false" do
53
+ @hash.none?.should be_false
54
+ end
55
+
56
+ end
57
+
58
+ end
59
+
60
+ end
61
+
62
+ end
@@ -2,104 +2,48 @@ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
2
 
3
3
  describe Hamster::Hash do
4
4
 
5
- describe "#put" do
5
+ [:put, :[]=].each do |method|
6
6
 
7
- describe "with a key/value pair that already exists" do
7
+ describe "##{method}" do
8
8
 
9
9
  before do
10
- @original = Hamster::Hash.new.put("A", "aye").put("B", "bee")
11
- @copy = @original.put("A", "yes")
10
+ @original = Hamster.hash("A" => "aye", "B" => "bee", "C" => "see")
12
11
  end
13
12
 
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
13
+ describe "with a unique key" do
58
14
 
59
- it "still has the original key/value pairs" do
60
- @original.get("A").should == "aye"
15
+ before do
16
+ @result = @original.send(method, "D", "dee")
61
17
  end
62
18
 
63
- it "doesn't contain the new key/value pair" do
64
- @original.has_key?("B").should be_false
19
+ it "preserves the original" do
20
+ @original.should == Hamster.hash("A" => "aye", "B" => "bee", "C" => "see")
65
21
  end
66
22
 
67
- it "still has the original size" do
68
- @original.size.should == 1
23
+ it "returns a copy with the superset of key/value pairs" do
24
+ @result.should == Hamster.hash("A" => "aye", "B" => "bee", "C" => "see", "D" => "dee")
69
25
  end
70
26
 
71
27
  end
72
28
 
73
- describe "the modified copy" do
29
+ describe "with a duplicate key" do
74
30
 
75
- it "has the original key/value pairs" do
76
- @copy.get("A").should == "aye"
31
+ before do
32
+ @result = @original.send(method, "C", "sea")
77
33
  end
78
34
 
79
- it "has the new key/value pair" do
80
- @copy.get("B").should == "bee"
35
+ it "preserves the original" do
36
+ @original.should == Hamster.hash("A" => "aye", "B" => "bee", "C" => "see")
81
37
  end
82
38
 
83
- it "size is increased by one" do
84
- @copy.size.should == 2
39
+ it "returns a copy with the superset of key/value pairs" do
40
+ @result.should == Hamster.hash("A" => "aye", "B" => "bee", "C" => "sea")
85
41
  end
86
42
 
87
43
  end
88
44
 
89
45
  end
90
46
 
91
- describe "with a nil key" do
92
-
93
- before do
94
- @hash = Hamster::Hash.new.put(nil, "NIL")
95
- end
96
-
97
- it "can locate the key/value pair" do
98
- @hash.get(nil).should == "NIL"
99
- end
100
-
101
- end
102
-
103
47
  end
104
48
 
105
49
  end