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
@@ -0,0 +1,29 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
+
3
+ describe Hamster::Set do
4
+
5
+ [:include?, :member?].each do |method|
6
+
7
+ describe "##{method}" do
8
+
9
+ before do
10
+ @set = Hamster.set("A", "B", "C", nil)
11
+ end
12
+
13
+ ["A", "B", "C", nil].each do |value|
14
+
15
+ it "returns true for an existing value (#{value.inspect})" do
16
+ @set.send(method, value).should be_true
17
+ end
18
+
19
+ end
20
+
21
+ it "returns false for a non-existing value" do
22
+ @set.send(method, "D").should be_false
23
+ end
24
+
25
+ end
26
+
27
+ end
28
+
29
+ end
@@ -0,0 +1,66 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
+
3
+ describe Hamster::Set 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.set
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.set("A", "B", "C")
26
+ end
27
+
28
+ describe "with a block" do
29
+
30
+ before do
31
+ @mapped = @original.send(method) { |item| item.downcase }
32
+ end
33
+
34
+ it "preserves the original values" do
35
+ @original.should == Hamster.set("A", "B", "C")
36
+ end
37
+
38
+ it "returns a new set with the mapped values" do
39
+ @mapped.should == Hamster.set("a", "b", "c")
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.set("A", "B", "C")
52
+ end
53
+
54
+ it "returns an enumerator over the values" do
55
+ Hamster.set(*@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,61 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
+
3
+ describe Hamster::Set do
4
+
5
+ describe "#none?" do
6
+
7
+ describe "when empty" do
8
+
9
+ before do
10
+ @set = Hamster.set
11
+ end
12
+
13
+ it "with a block returns true" do
14
+ @set.none? {}.should be_true
15
+ end
16
+
17
+ it "with no block returns true" do
18
+ @set.none?.should be_true
19
+ end
20
+
21
+ end
22
+
23
+ describe "when not empty" do
24
+
25
+ describe "with a block" do
26
+
27
+ before do
28
+ @set = Hamster.set("A", "B", "C", nil)
29
+ end
30
+
31
+ ["A", "B", "C", nil].each do |value|
32
+
33
+ it "returns false if the block ever returns true (#{value.inspect})" do
34
+ @set.none? { |item| item == value }.should be_false
35
+ end
36
+
37
+ end
38
+
39
+ it "returns true if the block always returns false" do
40
+ @set.none? { |item| item == "D" }.should be_true
41
+ end
42
+
43
+ end
44
+
45
+ describe "with no block" do
46
+
47
+ it "returns false if any value is truthy" do
48
+ Hamster.set(nil, false, true, "A").none?.should be_false
49
+ end
50
+
51
+ it "returns true if all values are falsey" do
52
+ Hamster.set(nil, false).none?.should be_true
53
+ end
54
+
55
+ end
56
+
57
+ end
58
+
59
+ end
60
+
61
+ end
@@ -0,0 +1,58 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
+
3
+ describe Hamster::Set do
4
+
5
+ [:reduce, :inject].each do |method|
6
+
7
+ describe "##{method}" do
8
+
9
+ describe "when empty" do
10
+
11
+ before do
12
+ @original = Hamster.set
13
+ @result = @original.send(method, "ABC") {}
14
+ end
15
+
16
+ it "returns the memo" do
17
+ @result.should == "ABC"
18
+ end
19
+
20
+ end
21
+
22
+ describe "when not empty" do
23
+
24
+ before do
25
+ @original = Hamster.set("A", "B", "C")
26
+ end
27
+
28
+ describe "with a block" do
29
+
30
+ before do
31
+ @result = @original.send(method, 0) { |memo, item| memo + 1 }
32
+ end
33
+
34
+ it "returns the final memo" do
35
+ @result.should == 3
36
+ end
37
+
38
+ end
39
+
40
+ describe "with no block" do
41
+
42
+ before do
43
+ @result = @original.send(method, "ABC")
44
+ end
45
+
46
+ it "returns the memo" do
47
+ @result.should == "ABC"
48
+ end
49
+
50
+ end
51
+
52
+ end
53
+
54
+ end
55
+
56
+ end
57
+
58
+ end
@@ -0,0 +1,57 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
+
3
+ describe Hamster::Set do
4
+
5
+ describe "#reject" do
6
+
7
+ before do
8
+ @original = Hamster.set("A", "B", "C")
9
+ end
10
+
11
+ describe "when nothing matches" do
12
+
13
+ before do
14
+ @result = @original.reject { |item| false }
15
+ end
16
+
17
+ it "returns self" do
18
+ @result.should equal(@original)
19
+ end
20
+
21
+ end
22
+
23
+ describe "when only some things match" do
24
+
25
+ describe "with a block" do
26
+
27
+ before do
28
+ @result = @original.reject { |item| item == "A" }
29
+ end
30
+
31
+ it "preserves the original" do
32
+ @original.should == Hamster.set("A", "B", "C")
33
+ end
34
+
35
+ it "returns a set with the matching values" do
36
+ @result.should == Hamster.set("B", "C")
37
+ end
38
+
39
+ end
40
+
41
+ describe "with no block" do
42
+
43
+ before do
44
+ @enumerator = @original.reject
45
+ end
46
+
47
+ it "returns an enumerator over the values" do
48
+ Hamster.set(*@enumerator.to_a).should == Hamster.set("A", "B", "C")
49
+ end
50
+
51
+ end
52
+
53
+ end
54
+
55
+ end
56
+
57
+ end
@@ -0,0 +1,45 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
+
3
+ describe Hamster::Set do
4
+
5
+ describe "#remove" do
6
+
7
+ before do
8
+ @original = Hamster.set("A", "B", "C")
9
+ end
10
+
11
+ describe "with an existing value" do
12
+
13
+ before do
14
+ @result = @original.remove("B")
15
+ end
16
+
17
+ it "preserves the original" do
18
+ @original.should == Hamster.set("A", "B", "C")
19
+ end
20
+
21
+ it "returns a copy with the remaining of values" do
22
+ @result.should == Hamster.set("A", "C")
23
+ end
24
+
25
+ end
26
+
27
+ describe "with a non-existing value" do
28
+
29
+ before do
30
+ @result = @original.remove("D")
31
+ end
32
+
33
+ it "preserves the original values" do
34
+ @original.should == Hamster.set("A", "B", "C")
35
+ end
36
+
37
+ it "returns self" do
38
+ @result.should equal(@original)
39
+ end
40
+
41
+ end
42
+
43
+ end
44
+
45
+ end
@@ -0,0 +1,25 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
+
3
+ describe Hamster::Set do
4
+
5
+ [:size, :length].each do |method|
6
+
7
+ describe "##{method}" do
8
+
9
+ [
10
+ [[], 0],
11
+ [["A"], 1],
12
+ [["A", "B", "C"], 3],
13
+ ].each do |values, result|
14
+
15
+ it "returns #{result} for #{values.inspect}" do
16
+ Hamster.set(*values).send(method).should == result
17
+ end
18
+
19
+ end
20
+
21
+ end
22
+
23
+ end
24
+
25
+ end
@@ -3,7 +3,7 @@ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
3
3
  describe Hamster::Stack do
4
4
 
5
5
  before do
6
- @stack = Hamster::Stack.new
6
+ @stack = Hamster.stack.push("A").push("B").push("C")
7
7
  end
8
8
 
9
9
  describe "#dup" do
@@ -5,11 +5,11 @@ describe Hamster::Stack do
5
5
  describe "#empty?" do
6
6
 
7
7
  it "initially returns true" do
8
- Hamster::Stack.new.should be_empty
8
+ Hamster.stack.should be_empty
9
9
  end
10
10
 
11
11
  it "returns false once items have been added" do
12
- Hamster::Stack.new.push("A").should_not be_empty
12
+ Hamster.stack.push("A").should_not be_empty
13
13
  end
14
14
 
15
15
  end
@@ -4,13 +4,24 @@ describe Hamster::Stack do
4
4
 
5
5
  describe "#eql?" do
6
6
 
7
+ before do
8
+ @stack = Hamster.stack.push("A").push("B").push("C")
9
+ end
10
+
7
11
  it "is true for the same instance" do
8
- stack = Hamster::Stack.new
9
- stack.should eql(stack)
12
+ @stack.should eql(@stack)
13
+ end
14
+
15
+ it "is true for two instances with the same sequence of values" do
16
+ @stack.should eql(Hamster.stack.push("A").push("B").push("C"))
17
+ end
18
+
19
+ it "is false for two instances with the difference sequence of values" do
20
+ @stack.should_not eql(Hamster.stack.push("A").push("C").push("B"))
10
21
  end
11
22
 
12
- it "is true for two empty instances" do
13
- Hamster::Stack.new.should eql(Hamster::Stack.new)
23
+ it "is false for two instances with the similar but differently sized sequence of values" do
24
+ @stack.should_not eql(Hamster.stack.push("A").push("B"))
14
25
  end
15
26
 
16
27
  end
@@ -5,36 +5,16 @@ describe Hamster::Stack do
5
5
  describe "#push" do
6
6
 
7
7
  before do
8
- @original = Hamster::Stack.new.push("A")
9
- @copy = @original.push("B")
8
+ @original = Hamster.stack.push("A")
9
+ @result = @original.push("B")
10
10
  end
11
11
 
12
- it "returns a modified copy" do
13
- @copy.should_not equal(@original)
12
+ it "preserves the original" do
13
+ @original.top.should == "A"
14
14
  end
15
15
 
16
- describe "the original" do
17
-
18
- it "still has the original top" do
19
- @original.top.should == "A"
20
- end
21
-
22
- it "still has the original size" do
23
- @original.size.should == 1
24
- end
25
-
26
- end
27
-
28
- describe "the modified copy" do
29
-
30
- it "has a new top" do
31
- @copy.top.should == "B"
32
- end
33
-
34
- it "size is increased by one" do
35
- @copy.size == 2
36
- end
37
-
16
+ it "returns a new stack with the new value at the top" do
17
+ @result.top.should == "B"
38
18
  end
39
19
 
40
20
  end