hamster 0.1.13 → 0.1.14

Sign up to get free protection for your applications and to get access to all the features.
Files changed (50) hide show
  1. data/History.rdoc +14 -0
  2. data/README.rdoc +2 -2
  3. data/lib/hamster/hash.rb +10 -9
  4. data/lib/hamster/list.rb +96 -64
  5. data/lib/hamster/set.rb +12 -5
  6. data/lib/hamster/version.rb +1 -1
  7. data/spec/hamster/hash/all_spec.rb +1 -1
  8. data/spec/hamster/hash/eql_spec.rb +42 -13
  9. data/spec/hamster/hash/filter_spec.rb +1 -1
  10. data/spec/hamster/hash/reduce_spec.rb +1 -1
  11. data/spec/hamster/hash/reject_spec.rb +31 -27
  12. data/spec/hamster/list/all_spec.rb +73 -0
  13. data/spec/hamster/list/any_spec.rb +77 -0
  14. data/spec/hamster/list/cadr_spec.rb +4 -2
  15. data/spec/hamster/list/cons_spec.rb +6 -4
  16. data/spec/hamster/list/copying_spec.rb +4 -2
  17. data/spec/hamster/list/drop_spec.rb +16 -2
  18. data/spec/hamster/list/drop_while_spec.rb +17 -3
  19. data/spec/hamster/list/each_spec.rb +18 -4
  20. data/spec/hamster/list/empty_spec.rb +4 -2
  21. data/spec/hamster/list/eql_spec.rb +41 -7
  22. data/spec/hamster/list/filter_spec.rb +20 -6
  23. data/spec/hamster/list/find_spec.rb +63 -0
  24. data/spec/hamster/list/head_spec.rb +4 -2
  25. data/spec/hamster/list/include_spec.rb +16 -2
  26. data/spec/hamster/list/inspect_spec.rb +16 -2
  27. data/spec/hamster/list/map_spec.rb +18 -4
  28. data/spec/hamster/list/none_spec.rb +73 -0
  29. data/spec/hamster/list/reduce_spec.rb +66 -12
  30. data/spec/hamster/list/reject_spec.rb +39 -21
  31. data/spec/hamster/list/size_spec.rb +16 -2
  32. data/spec/hamster/list/tail_spec.rb +4 -2
  33. data/spec/hamster/list/take_spec.rb +16 -2
  34. data/spec/hamster/list/take_while_spec.rb +18 -4
  35. data/spec/hamster/list/to_a_spec.rb +16 -2
  36. data/spec/hamster/list/to_ary_spec.rb +42 -0
  37. data/spec/hamster/set/eql_spec.rb +35 -5
  38. data/spec/hamster/set/filter_spec.rb +1 -1
  39. data/spec/hamster/set/reduce_spec.rb +1 -1
  40. data/spec/hamster/set/reject_spec.rb +31 -27
  41. data/spec/hamster/set/to_a_spec.rb +29 -0
  42. data/spec/hamster/stack/copying_spec.rb +4 -2
  43. data/spec/hamster/stack/empty_spec.rb +4 -2
  44. data/spec/hamster/stack/eql_spec.rb +27 -7
  45. data/spec/hamster/stack/inspect_spec.rb +4 -2
  46. data/spec/hamster/stack/pop_spec.rb +12 -8
  47. data/spec/hamster/stack/push_spec.rb +6 -4
  48. data/spec/hamster/stack/size_spec.rb +4 -2
  49. data/spec/hamster/stack/top_spec.rb +6 -4
  50. metadata +8 -2
@@ -6,25 +6,59 @@ describe Hamster::List do
6
6
 
7
7
  describe "##{method}" do
8
8
 
9
+ describe "on a really big list" do
10
+
11
+ before do
12
+ @a = Hamster.interval(0, 10000)
13
+ @b = Hamster.interval(0, 10000)
14
+ end
15
+
16
+ it "doesn't run out of stack space" do
17
+ @a.send(method, @b)
18
+ end
19
+
20
+ end
21
+
22
+ describe "returns false when comparing with" do
23
+
24
+ before do
25
+ @list = Hamster.list("A", "B", "C")
26
+ end
27
+
28
+ it "an array" do
29
+ @list.send(method, ["A", "B", "C"]).should be_false
30
+ end
31
+
32
+ it "an aribtrary object" do
33
+ @list.send(method, Object.new).should be_false
34
+ end
35
+
36
+ end
37
+
9
38
  [
10
39
  [[], [], true],
40
+ [[], [nil], false],
11
41
  [["A"], [], false],
12
- [[], ["A"], false],
13
42
  [["A"], ["A"], true],
14
43
  [["A"], ["B"], false],
15
44
  [["A", "B"], ["A"], false],
16
- [["A"], ["A", "B"], false],
17
45
  [["A", "B", "C"], ["A", "B", "C"], true],
18
46
  [["C", "A", "B"], ["A", "B", "C"], false],
19
47
  ].each do |a, b, expected|
20
48
 
21
- describe "on #{a.inspect} and #{b.inspect}" do
49
+ describe "returns #{expected}" do
50
+
51
+ before do
52
+ @a = Hamster.list(*a)
53
+ @b = Hamster.list(*b)
54
+ end
22
55
 
23
- a = Hamster.list(*a)
24
- b = Hamster.list(*b)
56
+ it "for lists #{a.inspect} and #{b.inspect}" do
57
+ @a.send(method, @b).should == expected
58
+ end
25
59
 
26
- it "returns #{expected}" do
27
- a.send(method, b).should == expected
60
+ it "for lists #{b.inspect} and #{a.inspect}" do
61
+ @b.send(method, @a).should == expected
28
62
  end
29
63
 
30
64
  end
@@ -2,9 +2,21 @@ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
2
 
3
3
  describe Hamster::List do
4
4
 
5
- [:filter, :select].each do |method|
5
+ [:filter, :select, :find_all].each do |method|
6
6
 
7
- describe "#filter" do
7
+ describe "##{method}" do
8
+
9
+ describe "on a really big list" do
10
+
11
+ before do
12
+ @list = Hamster.interval(0, 10000)
13
+ end
14
+
15
+ it "doesn't run out of stack space" do
16
+ @list.filter { false }
17
+ end
18
+
19
+ end
8
20
 
9
21
  [
10
22
  [[], []],
@@ -16,17 +28,19 @@ describe Hamster::List do
16
28
 
17
29
  describe "on #{values.inspect}" do
18
30
 
19
- list = Hamster.list(*values)
31
+ before do
32
+ @list = Hamster.list(*values)
33
+ end
20
34
 
21
35
  describe "with a block" do
22
36
 
23
37
  it "returns #{expected}" do
24
- list.send(method) { |item| item == item.upcase }.should == Hamster.list(*expected)
38
+ @list.send(method) { |item| item == item.upcase }.should == Hamster.list(*expected)
25
39
  end
26
40
 
27
41
  it "is lazy" do
28
42
  count = 0
29
- list.send(method) { |item| count += 1; true }
43
+ @list.send(method) { |item| count += 1; true }
30
44
  count.should <= 1
31
45
  end
32
46
 
@@ -35,7 +49,7 @@ describe Hamster::List do
35
49
  describe "without a block" do
36
50
 
37
51
  it "returns self" do
38
- list.send(method).should == list
52
+ @list.send(method).should equal(@list)
39
53
  end
40
54
 
41
55
  end
@@ -0,0 +1,63 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
+
3
+ describe Hamster::List do
4
+
5
+ [:find, :detect].each do |method|
6
+
7
+ describe "##{method}" do
8
+
9
+ describe "on a really big list" do
10
+
11
+ before do
12
+ @list = Hamster.interval(0, 10000)
13
+ end
14
+
15
+ it "doesn't run out of stack space" do
16
+ @list.send(method) { false }
17
+ end
18
+
19
+ end
20
+
21
+ [
22
+ [[], "A", nil],
23
+ [[], nil, nil],
24
+ [["A"], "A", "A"],
25
+ [["A"], "B", nil],
26
+ [["A"], nil, nil],
27
+ [["A", "B", nil], "A", "A"],
28
+ [["A", "B", nil], "B", "B"],
29
+ [["A", "B", nil], nil, nil],
30
+ [["A", "B", nil], "C", nil],
31
+ ].each do |values, item, expected|
32
+
33
+ describe "on #{values.inspect}" do
34
+
35
+ before do
36
+ @list = Hamster.list(*values)
37
+ end
38
+
39
+ describe "with a block" do
40
+
41
+ it "returns #{expected}" do
42
+ @list.send(method) { |x| x == item }.should == expected
43
+ end
44
+
45
+ end
46
+
47
+ describe "without a block" do
48
+
49
+ it "returns nil" do
50
+ @list.send(method).should be_nil
51
+ end
52
+
53
+ end
54
+
55
+ end
56
+
57
+ end
58
+
59
+ end
60
+
61
+ end
62
+
63
+ end
@@ -12,10 +12,12 @@ describe Hamster::List do
12
12
 
13
13
  describe "on #{values.inspect}" do
14
14
 
15
- list = Hamster.list(*values)
15
+ before do
16
+ @list = Hamster.list(*values)
17
+ end
16
18
 
17
19
  it "returns #{expected}" do
18
- list.head.should == expected
20
+ @list.head.should == expected
19
21
  end
20
22
 
21
23
  end
@@ -6,6 +6,18 @@ describe Hamster::List do
6
6
 
7
7
  describe "##{method}" do
8
8
 
9
+ describe "on a really big list" do
10
+
11
+ before do
12
+ @list = Hamster.interval(0, 10000)
13
+ end
14
+
15
+ it "doesn't run out of stack space" do
16
+ @list.send(method, nil)
17
+ end
18
+
19
+ end
20
+
9
21
  [
10
22
  [[], "A", false],
11
23
  [[], nil, false],
@@ -20,10 +32,12 @@ describe Hamster::List do
20
32
 
21
33
  describe "on #{values.inspect}" do
22
34
 
23
- list = Hamster.list(*values)
35
+ before do
36
+ @list = Hamster.list(*values)
37
+ end
24
38
 
25
39
  it "returns #{expected}" do
26
- list.send(method, item).should == expected
40
+ @list.send(method, item).should == expected
27
41
  end
28
42
 
29
43
  end
@@ -4,6 +4,18 @@ describe Hamster::List do
4
4
 
5
5
  describe "#inspect" do
6
6
 
7
+ describe "on a really big list" do
8
+
9
+ before do
10
+ @list = Hamster.interval(0, 10000)
11
+ end
12
+
13
+ it "doesn't run out of stack space" do
14
+ @list.to_a
15
+ end
16
+
17
+ end
18
+
7
19
  [
8
20
  [[], "[]"],
9
21
  [["A"], "[\"A\"]"],
@@ -12,10 +24,12 @@ describe Hamster::List do
12
24
 
13
25
  describe "on #{values.inspect}" do
14
26
 
15
- list = Hamster.list(*values)
27
+ before do
28
+ @list = Hamster.list(*values)
29
+ end
16
30
 
17
31
  it "returns #{expected}" do
18
- list.inspect.should == expected
32
+ @list.inspect.should == expected
19
33
  end
20
34
 
21
35
  end
@@ -6,6 +6,18 @@ describe Hamster::List do
6
6
 
7
7
  describe "##{method}" do
8
8
 
9
+ describe "on a really big list" do
10
+
11
+ before do
12
+ @list = Hamster.interval(0, 10000)
13
+ end
14
+
15
+ it "doesn't run out of stack space" do
16
+ @list.send(method) { |item| item }
17
+ end
18
+
19
+ end
20
+
9
21
  [
10
22
  [[], []],
11
23
  [["A"], ["a"]],
@@ -14,17 +26,19 @@ describe Hamster::List do
14
26
 
15
27
  describe "on #{values.inspect}" do
16
28
 
17
- list = Hamster.list(*values)
29
+ before do
30
+ @list = Hamster.list(*values)
31
+ end
18
32
 
19
33
  describe "with a block" do
20
34
 
21
35
  it "returns #{expected}" do
22
- list.send(method) { |item| item.downcase }.should == Hamster.list(*expected)
36
+ @list.send(method) { |item| item.downcase }.should == Hamster.list(*expected)
23
37
  end
24
38
 
25
39
  it "is lazy" do
26
40
  count = 0
27
- list.send(method) { |item| count += 1 }
41
+ @list.send(method) { |item| count += 1 }
28
42
  count.should <= 1
29
43
  end
30
44
 
@@ -33,7 +47,7 @@ describe Hamster::List do
33
47
  describe "without a block" do
34
48
 
35
49
  it "returns self" do
36
- list.send(method).should == list
50
+ @list.send(method).should equal(@list)
37
51
  end
38
52
 
39
53
  end
@@ -0,0 +1,73 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
+
3
+ describe Hamster::List do
4
+
5
+ describe "#none?" do
6
+
7
+ describe "on a really big list" do
8
+
9
+ before do
10
+ @list = Hamster.interval(0, 10000)
11
+ end
12
+
13
+ it "doesn't run out of stack space" do
14
+ @list.none? { false }
15
+ end
16
+
17
+ end
18
+
19
+ describe "when empty" do
20
+
21
+ before do
22
+ @list = Hamster.list
23
+ end
24
+
25
+ it "with a block returns true" do
26
+ @list.none? {}.should be_true
27
+ end
28
+
29
+ it "with no block returns true" do
30
+ @list.none?.should be_true
31
+ end
32
+
33
+ end
34
+
35
+ describe "when not empty" do
36
+
37
+ describe "with a block" do
38
+
39
+ before do
40
+ @list = Hamster.list("A", "B", "C", nil)
41
+ end
42
+
43
+ ["A", "B", "C", nil].each do |value|
44
+
45
+ it "returns false if the block ever returns true (#{value.inspect})" do
46
+ @list.none? { |item| item == value }.should be_false
47
+ end
48
+
49
+ end
50
+
51
+ it "returns true if the block always returns false" do
52
+ @list.none? { |item| item == "D" }.should be_true
53
+ end
54
+
55
+ end
56
+
57
+ describe "with no block" do
58
+
59
+ it "returns false if any value is truthy" do
60
+ Hamster.list(nil, false, true, "A").none?.should be_false
61
+ end
62
+
63
+ it "returns true if all values are falsey" do
64
+ Hamster.list(nil, false).none?.should be_true
65
+ end
66
+
67
+ end
68
+
69
+ end
70
+
71
+ end
72
+
73
+ end
@@ -2,32 +2,86 @@ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
2
 
3
3
  describe Hamster::List do
4
4
 
5
- [:reduce, :inject].each do |method|
5
+ [:reduce, :inject, :fold].each do |method|
6
6
 
7
7
  describe "##{method}" do
8
8
 
9
+ describe "on a really big list" do
10
+
11
+ before do
12
+ @list = Hamster.interval(0, 10000)
13
+ end
14
+
15
+ it "doesn't run out of stack space" do
16
+ @list.reduce(nil) { }
17
+ end
18
+
19
+ end
20
+
9
21
  [
10
- [[], "@"],
11
- [["A"], "@a"],
12
- [["A", "B", "C"], "@abc"],
13
- ].each do |values, expected|
22
+ [[], 10, 10],
23
+ [[1], 10, 9],
24
+ [[1, 2, 3], 10, 4],
25
+ ].each do |values, initial, expected|
14
26
 
15
27
  describe "on #{values.inspect}" do
16
28
 
17
- list = Hamster.list(*values)
29
+ before do
30
+ @list = Hamster.list(*values)
31
+ end
32
+
33
+ describe "with an initial value of #{initial}" do
34
+
35
+ describe "and a block" do
36
+
37
+ it "returns #{expected.inspect}" do
38
+ @list.send(method, initial) { |memo, item| memo - item }.should == expected
39
+ end
18
40
 
19
- describe "with a block" do
41
+ end
42
+
43
+ describe "and no block" do
44
+
45
+ it "returns the memo" do
46
+ @list.send(method, initial).should == initial
47
+ end
20
48
 
21
- it "returns #{expected.inspect}" do
22
- list.send(method, "@") { |memo, item| memo << item.downcase }.should == expected
23
49
  end
24
50
 
25
51
  end
26
52
 
27
- describe "without a block" do
53
+ end
54
+
55
+ end
56
+
57
+ [
58
+ [[], nil],
59
+ [[1], 1],
60
+ [[1, 2, 3], -4],
61
+ ].each do |values, expected|
62
+
63
+ describe "on #{values.inspect}" do
64
+
65
+ before do
66
+ @list = Hamster.list(*values)
67
+ end
68
+
69
+ describe "with no initial value" do
70
+
71
+ describe "and a block" do
72
+
73
+ it "returns #{expected.inspect}" do
74
+ @list.send(method) { |memo, item| memo - item }.should == expected
75
+ end
76
+
77
+ end
78
+
79
+ describe "and no block" do
80
+
81
+ it "returns the first value in the list" do
82
+ @list.send(method).should == values.first
83
+ end
28
84
 
29
- it "returns the memo" do
30
- list.send(method, "@").should == "@"
31
85
  end
32
86
 
33
87
  end