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,58 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
+
3
+ describe Hamster::Hash 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.hash
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.hash("A" => "aye", "B" => "bee", "C" => "see")
26
+ end
27
+
28
+ describe "with a block" do
29
+
30
+ before do
31
+ @result = @original.send(method, 0) { |memo, key, value| 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::Hash do
4
+
5
+ describe "#reject" do
6
+
7
+ before do
8
+ @original = Hamster.hash("A" => "aye", "B" => "bee", "C" => "see")
9
+ end
10
+
11
+ describe "when nothing matches" do
12
+
13
+ before do
14
+ @result = @original.reject { |key, value| 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 { |key, value| key == "A" && value == "aye" }
29
+ end
30
+
31
+ it "preserves the original" do
32
+ @original.should == Hamster.hash("A" => "aye", "B" => "bee", "C" => "see")
33
+ end
34
+
35
+ it "returns a set with the matching values" do
36
+ @result.should == Hamster.hash("B" => "bee", "C" => "see")
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.hash(@enumerator.to_a).should == Hamster.hash("A" => "aye", "B" => "bee", "C" => "see")
49
+ end
50
+
51
+ end
52
+
53
+ end
54
+
55
+ end
56
+
57
+ end
@@ -3,111 +3,39 @@ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
3
3
  describe Hamster::Hash do
4
4
 
5
5
  describe "#remove" do
6
-
7
- describe "the last key" do
8
6
 
9
- before do
10
- @hash = Hamster::Hash.new.put("A", "aye").remove("A")
11
- end
12
-
13
- it "no longer provides access to the removed key" do
14
- @hash.has_key?("A").should be_false
15
- end
16
-
7
+ before do
8
+ @original = Hamster.hash("A" => "aye", "B" => "bee", "C" => "see")
17
9
  end
18
10
 
19
11
  describe "with an existing key" do
20
12
 
21
13
  before do
22
- @original = Hamster::Hash.new.put("A", "aye").put("B", "bee")
23
- @copy = @original.remove("A")
14
+ @result = @original.remove("B")
24
15
  end
25
16
 
26
- it "returns a modified copy" do
27
- @copy.should_not equal(@original)
28
- end
29
-
30
- describe "the original" do
31
-
32
- it "still has the original key/value pairs" do
33
- @original.get("A").should == "aye"
34
- @original.get("B").should == "bee"
35
- end
36
-
37
- it "still has the original size" do
38
- @original.size.should == 2
39
- end
40
-
17
+ it "preserves the original" do
18
+ @original.should == Hamster.hash("A" => "aye", "B" => "bee", "C" => "see")
41
19
  end
42
20
 
43
- describe "the modified copy" do
44
-
45
- it "has all but the removed original key/value pairs" do
46
- @copy.get("B").should == "bee"
47
- end
48
-
49
- it "doesn't have the removed key" do
50
- @copy.has_key?("A").should be_false
51
- end
52
-
53
- it "has a size one less than the original" do
54
- @copy.size.should == 1
55
- end
56
-
21
+ it "returns a copy with the remaining key/value pairs" do
22
+ @result.should == Hamster.hash("A" => "aye", "C" => "see")
57
23
  end
58
24
 
59
25
  end
60
26
 
61
- describe "with non-existing keys" do
27
+ describe "with a non-existing key" do
62
28
 
63
29
  before do
64
- @original = Hamster::Hash.new.put("A", "aye")
65
- @copy = @original.remove("missing")
30
+ @result = @original.remove("D")
66
31
  end
67
32
 
68
- it "returns self" do
69
- @copy.should equal(@original)
33
+ it "preserves the original values" do
34
+ @original.should == Hamster.hash("A" => "aye", "B" => "bee", "C" => "see")
70
35
  end
71
36
 
72
- describe "the original" do
73
-
74
- it "still has the original key/value pairs" do
75
- @original.get("A").should == "aye"
76
- end
77
-
78
- it "still has the original size" do
79
- @original.size.should == 1
80
- end
81
-
82
- end
83
-
84
- end
85
-
86
- describe "with keys of the same hash value" do
87
-
88
- class Key
89
- def hash; 1; end
90
- end
91
-
92
- def instance_count
93
- ObjectSpace.garbage_collect
94
- ObjectSpace.each_object(Hamster::Trie) {}
95
- end
96
-
97
- before do
98
- @a = Key.new
99
- @b = Key.new
100
- @original = Hamster::Hash.new.put(@a, "aye").put(@b, "bee")
101
- end
102
-
103
- it "no longer provides access to the removed key" do
104
- copy = @original.remove(@b)
105
- copy.has_key?(@b).should be_false
106
- end
107
-
108
- it "provides access to the remaining keys" do
109
- copy = @original.remove(@a)
110
- copy.get(@b).should == "bee"
37
+ it "returns self" do
38
+ @result.should equal(@original)
111
39
  end
112
40
 
113
41
  end
@@ -0,0 +1,25 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
+
3
+ describe Hamster::Hash do
4
+
5
+ [:size, :length].each do |method|
6
+
7
+ describe "##{method}" do
8
+
9
+ [
10
+ [[], 0],
11
+ [["A" => "aye"], 1],
12
+ [["A" => "bee", "B" => "bee", "C" => "see"], 3],
13
+ ].each do |values, result|
14
+
15
+ it "returns #{result} for #{values.inspect}" do
16
+ Hamster.hash(*values).send(method).should == result
17
+ end
18
+
19
+ end
20
+
21
+ end
22
+
23
+ end
24
+
25
+ end
@@ -0,0 +1,37 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
+
3
+ describe Hamster::List do
4
+
5
+ describe "#cadr" do
6
+
7
+ [
8
+ [[], :car, nil],
9
+ [["A"], :car, "A"],
10
+ [["A", "B", "C"], :car, "A"],
11
+ [["A", "B", "C"], :cadr, "B"],
12
+ [["A", "B", "C"], :caddr, "C"],
13
+ [["A", "B", "C"], :cadddr, nil],
14
+ [["A", "B", "C"], :caddddr, nil],
15
+ [[], :cdr, Hamster.list],
16
+ [["A"], :cdr, Hamster.list],
17
+ [["A", "B", "C"], :cdr, Hamster.list("B", "C")],
18
+ [["A", "B", "C"], :cddr, Hamster.list("C")],
19
+ [["A", "B", "C"], :cdddr, Hamster.list],
20
+ [["A", "B", "C"], :cddddr, Hamster.list],
21
+ ].each do |values, method, result|
22
+
23
+ describe "on #{values.inspect}" do
24
+
25
+ list = Hamster.list(*values)
26
+
27
+ it "returns #{result}" do
28
+ list.send(method).should == result
29
+ end
30
+
31
+ end
32
+
33
+ end
34
+
35
+ end
36
+
37
+ end
@@ -1,15 +1,55 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
2
 
3
- describe Hamster::List do
3
+ describe Hamster do
4
4
 
5
- describe ".[]" do
5
+ describe ".list" do
6
+
7
+ describe "with no arguments" do
8
+
9
+ before do
10
+ @list = Hamster.list
11
+ end
12
+
13
+ it "always returns the same instance" do
14
+ @list.should equal(Hamster.list)
15
+ end
16
+
17
+ it "returns an empty list" do
18
+ @list.should be_empty
19
+ end
20
+
21
+ end
22
+
23
+ describe "with a number of items" do
24
+
25
+ before do
26
+ @list = Hamster.list("A", "B", "C")
27
+ end
28
+
29
+ it "always returns a different instance" do
30
+ @list.should_not equal(Hamster.list("A", "B", "C"))
31
+ end
32
+
33
+ it "is the same as repeatedly using #cons" do
34
+ @list.should == Hamster.list.cons("C").cons("B").cons("A")
35
+ end
6
36
 
7
- before do
8
- @list = Hamster::List["A", "B", "C"]
9
37
  end
10
38
 
11
- it "is equivalent to repeatedly using #cons" do
12
- @list.should eql(Hamster::List.new.cons("C").cons("B").cons("A"))
39
+ end
40
+
41
+ [:interval, :range].each do |method|
42
+
43
+ describe ".#{method}" do
44
+
45
+ before do
46
+ @interval = Hamster.send(method, "A", "D")
47
+ end
48
+
49
+ it "is equivalent to a list with explicit values" do
50
+ @interval.should == Hamster.list("A", "B", "C", "D")
51
+ end
52
+
13
53
  end
14
54
 
15
55
  end
@@ -2,22 +2,24 @@ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
2
 
3
3
  describe Hamster::List do
4
4
 
5
- before do
6
- @list = Hamster::List.new
7
- end
5
+ [:dup, :clone].each do |method|
8
6
 
9
- describe "#dup" do
7
+ [
8
+ [],
9
+ ["A"],
10
+ ["A", "B", "C"],
11
+ ].each do |values|
10
12
 
11
- it "returns self" do
12
- @list.dup.should equal(@list)
13
- end
13
+ describe "on #{values.inspect}" do
14
14
 
15
- end
15
+ list = Hamster.list(*values)
16
+
17
+ it "returns self" do
18
+ list.send(method).should equal(list)
19
+ end
16
20
 
17
- describe "#clone" do
21
+ end
18
22
 
19
- it "returns self" do
20
- @list.clone.should equal(@list)
21
23
  end
22
24
 
23
25
  end
@@ -0,0 +1,29 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
+
3
+ describe Hamster::List do
4
+
5
+ describe "#drop" do
6
+
7
+ [
8
+ [[], 10, []],
9
+ [["A"], 10, []],
10
+ [["A"], -1, ["A"]],
11
+ [["A", "B", "C"], 0, ["A", "B", "C"]],
12
+ [["A", "B", "C"], 2, ["C"]],
13
+ ].each do |values, number, result|
14
+
15
+ describe "#{number} from #{values.inspect}" do
16
+
17
+ list = Hamster.list(*values)
18
+
19
+ it "returns #{result}" do
20
+ list.drop(number).should == Hamster.list(*result)
21
+ end
22
+
23
+ end
24
+
25
+ end
26
+
27
+ end
28
+
29
+ end
@@ -0,0 +1,39 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
+
3
+ describe Hamster::List do
4
+
5
+ describe "#drop_while" do
6
+
7
+ [
8
+ [[], []],
9
+ [["A"], []],
10
+ [["A", "B", "C"], ["C"]],
11
+ ].each do |values, result|
12
+
13
+ describe "on #{values.inspect}" do
14
+
15
+ list = Hamster.list(*values)
16
+
17
+ describe "with a block" do
18
+
19
+ it "returns #{result}" do
20
+ list.drop_while { |item| item < "C" }.should == Hamster.list(*result)
21
+ end
22
+
23
+ end
24
+
25
+ describe "without a block" do
26
+
27
+ it "returns self" do
28
+ list.drop_while.should == list
29
+ end
30
+
31
+ end
32
+
33
+ end
34
+
35
+ end
36
+
37
+ end
38
+
39
+ end