combinatorics 0.4.3 → 0.4.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.github/workflows/ruby.yml +28 -0
- data/.gitignore +4 -5
- data/ChangeLog.md +5 -0
- data/Gemfile +14 -0
- data/LICENSE.txt +1 -1
- data/README.md +144 -105
- data/Rakefile +6 -30
- data/combinatorics.gemspec +1 -0
- data/gemspec.yml +7 -3
- data/lib/combinatorics/choose/cardinality.rb +1 -1
- data/lib/combinatorics/generator.rb +1 -1
- data/lib/combinatorics/version.rb +1 -1
- data/spec/cartesian_product/cardinality_spec.rb +13 -13
- data/spec/cartesian_product/mixin_examples.rb +14 -14
- data/spec/choose/cardinality_spec.rb +31 -31
- data/spec/choose/mixin_examples.rb +6 -6
- data/spec/combinatorics_spec.rb +1 -1
- data/spec/derange/mixin_examples.rb +6 -6
- data/spec/enumerator_spec.rb +1 -1
- data/spec/extensions/math_spec.rb +19 -19
- data/spec/extensions/range_spec.rb +12 -12
- data/spec/generator_spec.rb +1 -1
- data/spec/list_comprehension_spec.rb +10 -10
- data/spec/permute/cardinality_spec.rb +28 -28
- data/spec/permute/mixin_examples.rb +5 -5
- data/spec/power_set/mixin_examples.rb +4 -4
- data/spec/spec_helper.rb +0 -2
- metadata +26 -57
@@ -7,52 +7,52 @@ shared_examples_for "CartesianProduct::Mixin" do
|
|
7
7
|
set = subject[1]
|
8
8
|
results = set.cartprod(set)
|
9
9
|
|
10
|
-
results.
|
10
|
+
expect(results).to be_kind_of(Enumerator)
|
11
11
|
end
|
12
12
|
|
13
13
|
it "the cartprod of two empty Set's should return an empty Set" do
|
14
14
|
set = subject[]
|
15
15
|
results = set.cartprod([]).to_a
|
16
16
|
|
17
|
-
results.
|
17
|
+
expect(results).to be_empty
|
18
18
|
end
|
19
19
|
|
20
20
|
it "the cartprod of a single empty set should return an empty Set" do
|
21
21
|
set = subject[1,2]
|
22
22
|
results = set.cartprod([2,3],[]).to_a
|
23
23
|
|
24
|
-
results.
|
24
|
+
expect(results).to be_empty
|
25
25
|
end
|
26
26
|
|
27
27
|
it "the cartprod of another empty set should also return an empty Set" do
|
28
28
|
set = subject[]
|
29
29
|
results = set.cartprod([1]).to_a
|
30
30
|
|
31
|
-
results.
|
31
|
+
expect(results).to be_empty
|
32
32
|
end
|
33
33
|
|
34
34
|
it "the cartprod of [1] and [1] should be [[1, 1]]" do
|
35
35
|
set = subject[1]
|
36
36
|
results = set.cartprod([1]).to_a
|
37
37
|
|
38
|
-
results.
|
38
|
+
expect(results).to eq([[1, 1]])
|
39
39
|
end
|
40
40
|
|
41
41
|
it "the cartprod of [1, 2] and [3] should be [[1, 3], [2, 3]]" do
|
42
42
|
set = subject[1, 2]
|
43
43
|
results = set.cartprod([3]).to_a
|
44
44
|
|
45
|
-
results.
|
45
|
+
expect(results).to match_array([[1, 3], [2, 3]])
|
46
46
|
end
|
47
47
|
|
48
48
|
it "the cartprod of [1, 2] and [3, 4] should be [[1, 3], [1, 4], [2, 3], [2, 4]]" do
|
49
49
|
set = subject[1, 2]
|
50
50
|
results = set.cartprod([3, 4]).to_a
|
51
51
|
|
52
|
-
results.
|
52
|
+
expect(results).to match_array([
|
53
53
|
[1, 3], [1, 4],
|
54
54
|
[2, 3], [2, 4]
|
55
|
-
]
|
55
|
+
])
|
56
56
|
end
|
57
57
|
|
58
58
|
it "the cartprod of ['a'].cartprod(['b', 'c', 'd']) should be [['a', 'b'], ['a', 'c'], ['a', 'd']]" do
|
@@ -60,7 +60,7 @@ shared_examples_for "CartesianProduct::Mixin" do
|
|
60
60
|
set2 = subject['b', 'c', 'd']
|
61
61
|
results = set1.cartprod(set2).to_a
|
62
62
|
|
63
|
-
results.
|
63
|
+
expect(results).to match_array([['a', 'b'], ['a', 'c'], ['a', 'd']])
|
64
64
|
end
|
65
65
|
|
66
66
|
it "the cartprod of [0, 1] and [[2, 3], [4, 5]] should be [[0, 2, 4], [1, 2, 4], [0, 3, 4], [1, 3, 4], [0, 2, 5], [1, 2, 5], [0, 3, 5], [1, 3, 5]]" do
|
@@ -69,10 +69,10 @@ shared_examples_for "CartesianProduct::Mixin" do
|
|
69
69
|
set3 = subject[4, 5]
|
70
70
|
results = set1.cartprod(set2, set3).to_a
|
71
71
|
|
72
|
-
results.
|
72
|
+
expect(results).to match_array([
|
73
73
|
[0, 2, 4], [0, 2, 5], [0, 3, 4], [0, 3, 5],
|
74
74
|
[1, 2, 4], [1, 2, 5], [1, 3, 4], [1, 3, 5],
|
75
|
-
]
|
75
|
+
])
|
76
76
|
end
|
77
77
|
|
78
78
|
it "should take an optional block argument" do
|
@@ -81,18 +81,18 @@ shared_examples_for "CartesianProduct::Mixin" do
|
|
81
81
|
|
82
82
|
set.cartprod(set) { |result| results << result }
|
83
83
|
|
84
|
-
results.
|
84
|
+
expect(results).to eq([[1, 1]])
|
85
85
|
end
|
86
86
|
|
87
87
|
it "should alias cartprod to cartesian_product" do
|
88
88
|
aset = subject[1]
|
89
89
|
|
90
|
-
aset.
|
90
|
+
expect(aset).to respond_to(:cartesian_product)
|
91
91
|
end
|
92
92
|
|
93
93
|
it "should alias cartprod to cartesian" do
|
94
94
|
aset = subject[1]
|
95
95
|
|
96
|
-
aset.
|
96
|
+
expect(aset).to respond_to(:cartesian)
|
97
97
|
end
|
98
98
|
end
|
@@ -6,123 +6,123 @@ describe Choose do
|
|
6
6
|
|
7
7
|
describe "cardinality" do
|
8
8
|
it "should raise RangeError if n is negative" do
|
9
|
-
|
9
|
+
expect { subject.cardinality(-1) }.to raise_error(RangeError)
|
10
10
|
end
|
11
11
|
|
12
12
|
it "should raise RangeError if n is negative" do
|
13
|
-
|
13
|
+
expect { subject.cardinality(-1, 1) }.to raise_error(RangeError)
|
14
14
|
end
|
15
15
|
|
16
16
|
it "should raise RangeError if r is negative" do
|
17
|
-
|
17
|
+
expect { subject.cardinality(1, -1) }.to raise_error(RangeError)
|
18
18
|
end
|
19
19
|
|
20
20
|
it "should raise RangeError if r is greater than n" do
|
21
|
-
|
21
|
+
expect { subject.cardinality(2, 3) }.to raise_error(RangeError)
|
22
22
|
end
|
23
23
|
|
24
24
|
it "should return 1 for subject.cardinality(0)" do
|
25
|
-
subject.cardinality(0).
|
25
|
+
expect(subject.cardinality(0)).to eq(1)
|
26
26
|
end
|
27
27
|
|
28
28
|
it "should return 1 for subject.cardinality(1)" do
|
29
|
-
subject.cardinality(1).
|
29
|
+
expect(subject.cardinality(1)).to eq(1)
|
30
30
|
end
|
31
31
|
|
32
32
|
it "should return 2 for subject.cardinality(2)" do
|
33
|
-
subject.cardinality(2).
|
33
|
+
expect(subject.cardinality(2)).to eq(2)
|
34
34
|
end
|
35
35
|
|
36
36
|
it "should return 6 for subject.cardinality(3)" do
|
37
|
-
subject.cardinality(3).
|
37
|
+
expect(subject.cardinality(3)).to eq(6)
|
38
38
|
end
|
39
39
|
|
40
40
|
it "should return 24 for subject.cardinality(4)" do
|
41
|
-
subject.cardinality(4).
|
41
|
+
expect(subject.cardinality(4)).to eq(24)
|
42
42
|
end
|
43
43
|
|
44
|
-
it "should return
|
45
|
-
subject.cardinality(1, 0).
|
44
|
+
it "should return 1 for subject.cardinality(1, 0)" do
|
45
|
+
expect(subject.cardinality(1, 0)).to eq(1)
|
46
46
|
end
|
47
47
|
|
48
48
|
it "should return 1 for subject.cardinality(1, 1)" do
|
49
|
-
subject.cardinality(1, 1).
|
49
|
+
expect(subject.cardinality(1, 1)).to eq(1)
|
50
50
|
end
|
51
51
|
|
52
52
|
it "should return 2 for subject.cardinality(2, 1)" do
|
53
|
-
subject.cardinality(2, 1).
|
53
|
+
expect(subject.cardinality(2, 1)).to eq(2)
|
54
54
|
end
|
55
55
|
|
56
56
|
it "should return 1 for subject.cardinality(2, 2)" do
|
57
|
-
subject.cardinality(2, 2).
|
57
|
+
expect(subject.cardinality(2, 2)).to eq(1)
|
58
58
|
end
|
59
59
|
|
60
60
|
it "should return 3 for subject.cardinality(3, 1)" do
|
61
|
-
subject.cardinality(3, 1).
|
61
|
+
expect(subject.cardinality(3, 1)).to eq(3)
|
62
62
|
end
|
63
63
|
|
64
64
|
it "should return 3 for subject.cardinality(3, 2)" do
|
65
|
-
subject.cardinality(3, 2).
|
65
|
+
expect(subject.cardinality(3, 2)).to eq(3)
|
66
66
|
end
|
67
67
|
|
68
68
|
it "should return 1 for subject.cardinality(3, 3)" do
|
69
|
-
subject.cardinality(3, 3).
|
69
|
+
expect(subject.cardinality(3, 3)).to eq(1)
|
70
70
|
end
|
71
71
|
|
72
72
|
it "should return 4 for subject.cardinality(4, 1)" do
|
73
|
-
subject.cardinality(4, 1).
|
73
|
+
expect(subject.cardinality(4, 1)).to eq(4)
|
74
74
|
end
|
75
75
|
|
76
76
|
it "should return 6 for subject.cardinality(4, 2)" do
|
77
|
-
subject.cardinality(4, 2).
|
77
|
+
expect(subject.cardinality(4, 2)).to eq(6)
|
78
78
|
end
|
79
79
|
|
80
80
|
it "should return 4 for subject.cardinality(4, 3)" do
|
81
|
-
subject.cardinality(4, 3).
|
81
|
+
expect(subject.cardinality(4, 3)).to eq(4)
|
82
82
|
end
|
83
83
|
|
84
84
|
it "should return 1 for subject.cardinality(4, 4)" do
|
85
|
-
subject.cardinality(4, 4).
|
85
|
+
expect(subject.cardinality(4, 4)).to eq(1)
|
86
86
|
end
|
87
87
|
|
88
88
|
it "should return 15 for subject.cardinality(6, 4)" do
|
89
|
-
subject.cardinality(6, 4).
|
89
|
+
expect(subject.cardinality(6, 4)).to eq(15)
|
90
90
|
end
|
91
91
|
|
92
92
|
it "should return 3628800 for subject.cardinality(10)" do
|
93
|
-
subject.cardinality(10).
|
93
|
+
expect(subject.cardinality(10)).to eq(3628800)
|
94
94
|
end
|
95
95
|
end
|
96
96
|
|
97
97
|
describe "cardinality_all" do
|
98
98
|
it "should return [] for subject.cardinality_all(0)" do
|
99
|
-
subject.cardinality_all(0).
|
99
|
+
expect(subject.cardinality_all(0)).to be_empty
|
100
100
|
end
|
101
101
|
|
102
102
|
it "should return [1] for subject.cardinality_all(1)" do
|
103
|
-
subject.cardinality_all(1).
|
103
|
+
expect(subject.cardinality_all(1)).to eq([1])
|
104
104
|
end
|
105
105
|
|
106
106
|
it "should return [2, 1] for subject.cardinality_all(2)" do
|
107
|
-
subject.cardinality_all(2).
|
107
|
+
expect(subject.cardinality_all(2)).to eq([2, 1])
|
108
108
|
end
|
109
109
|
|
110
110
|
it "should return [3, 3, 1] for subject.cardinality_all(3)" do
|
111
|
-
subject.cardinality_all(3).
|
111
|
+
expect(subject.cardinality_all(3)).to eq([3, 3, 1])
|
112
112
|
end
|
113
113
|
|
114
114
|
it "should return [4, 6, 4, 1] for subject.cardinality_all(4)" do
|
115
|
-
subject.cardinality_all(4).
|
115
|
+
expect(subject.cardinality_all(4)).to eq([4, 6, 4, 1])
|
116
116
|
end
|
117
117
|
|
118
118
|
it "should allow specifying the range of `r` values" do
|
119
|
-
subject.cardinality_all(10,5..10).
|
119
|
+
expect(subject.cardinality_all(10,5..10)).to eq([
|
120
120
|
252, 210, 120, 45, 10, 1
|
121
|
-
]
|
121
|
+
])
|
122
122
|
end
|
123
123
|
|
124
124
|
it "should raise RangeError for subject.cardinality_all(-1)" do
|
125
|
-
|
125
|
+
expect { subject.cardinality_all(-1) }.to raise_error(RangeError)
|
126
126
|
end
|
127
127
|
|
128
128
|
it "should wrap cardinality with Choose.C" do
|
@@ -8,41 +8,41 @@ shared_examples_for "Choose::Mixin" do
|
|
8
8
|
set = subject[]
|
9
9
|
results = set.choose(0).to_a
|
10
10
|
|
11
|
-
results.
|
11
|
+
expect(results).to eq([empty_set])
|
12
12
|
end
|
13
13
|
|
14
14
|
it "should return [[]] for [1].choose(0).to_a" do
|
15
15
|
set = subject[1]
|
16
16
|
results = set.choose(0).to_a
|
17
17
|
|
18
|
-
results.
|
18
|
+
expect(results).to eq([empty_set])
|
19
19
|
end
|
20
20
|
|
21
21
|
it "should return [[1]] for [1].choose(1).to_a" do
|
22
22
|
set = subject[1]
|
23
23
|
results = set.choose(1).to_a
|
24
24
|
|
25
|
-
results.
|
25
|
+
expect(results).to eq([Set[1]])
|
26
26
|
end
|
27
27
|
|
28
28
|
it "should return [[1], [2]] for [1, 2].choose(1).to_a" do
|
29
29
|
set = subject[1, 2]
|
30
30
|
results = set.choose(1).to_a
|
31
31
|
|
32
|
-
results.
|
32
|
+
expect(results).to match_array([Set[1], Set[2]])
|
33
33
|
end
|
34
34
|
|
35
35
|
it "should return [[1, 2]] for [1, 2].choose(2).to_a" do
|
36
36
|
set = subject[1, 2]
|
37
37
|
results = set.choose(2).to_a
|
38
38
|
|
39
|
-
results.
|
39
|
+
expect(results).to eq([Set[1, 2]])
|
40
40
|
end
|
41
41
|
|
42
42
|
it "should filter out repeated elements" do
|
43
43
|
set1 = subject[1,1,2,3]
|
44
44
|
set2 = subject[1,2,3]
|
45
45
|
|
46
|
-
set1.choose(2).to_a.
|
46
|
+
expect(set1.choose(2).to_a).to eq(set2.choose(2).to_a)
|
47
47
|
end
|
48
48
|
end
|
data/spec/combinatorics_spec.rb
CHANGED
@@ -7,28 +7,28 @@ shared_examples_for "Derange::Mixin" do
|
|
7
7
|
set = subject[]
|
8
8
|
results = set.derange.to_a
|
9
9
|
|
10
|
-
results.
|
10
|
+
expect(results).to eq([[]])
|
11
11
|
end
|
12
12
|
|
13
13
|
it "should return [[]] for [1].derange.to_a" do
|
14
14
|
set = subject[1]
|
15
15
|
results = set.derange.to_a
|
16
16
|
|
17
|
-
results.
|
17
|
+
expect(results).to eq([[]])
|
18
18
|
end
|
19
19
|
|
20
20
|
it "should return [[2, 1]] for [1, 2].derange.to_a" do
|
21
21
|
set = subject[1, 2]
|
22
22
|
results = set.derange.to_a
|
23
23
|
|
24
|
-
results.
|
24
|
+
expect(results).to eq([[2, 1]])
|
25
25
|
end
|
26
26
|
|
27
27
|
it "should return [[2, 1, 4, 3], [2, 3, 4, 1], [2, 4, 1, 3], [3, 1, 4, 2], [3, 4, 1, 2], [3, 4, 2, 1], [4, 1, 2, 3], [4, 3, 1, 2], [4, 3, 2, 1]] for [1, 2, 3, 4].derange.to_a" do
|
28
28
|
set = [1, 2, 3, 4]
|
29
29
|
results = set.derange.to_a
|
30
30
|
|
31
|
-
results.
|
31
|
+
expect(results).to eq([
|
32
32
|
[2, 1, 4, 3],
|
33
33
|
[2, 3, 4, 1],
|
34
34
|
[2, 4, 1, 3],
|
@@ -38,7 +38,7 @@ shared_examples_for "Derange::Mixin" do
|
|
38
38
|
[4, 1, 2, 3],
|
39
39
|
[4, 3, 1, 2],
|
40
40
|
[4, 3, 2, 1]
|
41
|
-
]
|
41
|
+
])
|
42
42
|
end
|
43
43
|
|
44
44
|
it "should take an optional block argument" do
|
@@ -47,6 +47,6 @@ shared_examples_for "Derange::Mixin" do
|
|
47
47
|
|
48
48
|
set.derange { |deranged| results << deranged }
|
49
49
|
|
50
|
-
results.
|
50
|
+
expect(results).to eq([[2, 3, 1], [3, 1, 2]])
|
51
51
|
end
|
52
52
|
end
|
data/spec/enumerator_spec.rb
CHANGED
@@ -12,89 +12,89 @@ describe Math do
|
|
12
12
|
|
13
13
|
describe "sigma" do
|
14
14
|
it "should return 6 for sigma(1..3)" do
|
15
|
-
subject.sigma(1..3).
|
15
|
+
expect(subject.sigma(1..3)).to eq(6)
|
16
16
|
end
|
17
17
|
|
18
18
|
it "should return 60 for sigma(3..5)" do
|
19
|
-
subject.sigma(3..5).
|
19
|
+
expect(subject.sigma(3..5)).to eq(12)
|
20
20
|
end
|
21
21
|
|
22
22
|
it "should take an optional block argument" do
|
23
23
|
result = subject.sigma(1..5) { |i| i * 2 }
|
24
24
|
|
25
|
-
result.
|
25
|
+
expect(result).to eq((1 * 2) + (2 * 2) + (3 * 2) + (4 * 2) + (5 * 2))
|
26
26
|
end
|
27
27
|
end
|
28
28
|
|
29
29
|
describe "pi" do
|
30
30
|
it "should return 24 for pi(1..4)" do
|
31
|
-
subject.pi(1..4).
|
31
|
+
expect(subject.pi(1..4)).to eq(24)
|
32
32
|
end
|
33
33
|
|
34
34
|
it "should return 30 for pi(5..6)" do
|
35
|
-
subject.pi(5..6).
|
35
|
+
expect(subject.pi(5..6)).to eq(30)
|
36
36
|
end
|
37
37
|
|
38
38
|
it "should take an optional block argument" do
|
39
39
|
result = subject.pi(1..3) { |i| i * 2 }
|
40
40
|
|
41
|
-
result.
|
41
|
+
expect(result).to eq((1 * 2) * (2 * 2) * (3 * 2))
|
42
42
|
end
|
43
43
|
end
|
44
44
|
|
45
45
|
describe "factorial" do
|
46
46
|
it "should return 1 for factorial(0)" do
|
47
|
-
subject.factorial(0).
|
47
|
+
expect(subject.factorial(0)).to eq(1)
|
48
48
|
end
|
49
49
|
|
50
50
|
it "should return 1 for factorial(1)" do
|
51
|
-
subject.factorial(1).
|
51
|
+
expect(subject.factorial(1)).to eq(1)
|
52
52
|
end
|
53
53
|
|
54
54
|
it "should return 2 for factorial(2)" do
|
55
|
-
subject.factorial(2).
|
55
|
+
expect(subject.factorial(2)).to eq(2)
|
56
56
|
end
|
57
57
|
|
58
58
|
it "should return 6 for factorial(3)" do
|
59
|
-
subject.factorial(3).
|
59
|
+
expect(subject.factorial(3)).to eq(6)
|
60
60
|
end
|
61
61
|
|
62
62
|
it "should return 3628800 for factorial(10)" do
|
63
|
-
subject.factorial(10).
|
63
|
+
expect(subject.factorial(10)).to eq(3628800)
|
64
64
|
end
|
65
65
|
|
66
66
|
it "should raise RangeError for factorial(-1)" do
|
67
|
-
|
67
|
+
expect { subject.factorial(-1) }.to raise_error(RangeError)
|
68
68
|
end
|
69
69
|
end
|
70
70
|
|
71
71
|
describe "subfactorial" do
|
72
72
|
it "should return 1 for subfactorial(0)" do
|
73
|
-
subject.subfactorial(0).
|
73
|
+
expect(subject.subfactorial(0)).to eq(1)
|
74
74
|
end
|
75
75
|
|
76
76
|
it "should return 0 for subfactorial(1)" do
|
77
|
-
subject.subfactorial(1).
|
77
|
+
expect(subject.subfactorial(1)).to eq(0)
|
78
78
|
end
|
79
79
|
|
80
80
|
it "should return 1 for subfactorial(2)" do
|
81
|
-
subject.subfactorial(2).
|
81
|
+
expect(subject.subfactorial(2)).to eq(1)
|
82
82
|
end
|
83
83
|
|
84
84
|
it "should return 2 for subfactorial(3)" do
|
85
|
-
subject.subfactorial(3).
|
85
|
+
expect(subject.subfactorial(3)).to eq(2)
|
86
86
|
end
|
87
87
|
|
88
88
|
it "should return 9 for subfactorial(4)" do
|
89
|
-
subject.subfactorial(4).
|
89
|
+
expect(subject.subfactorial(4)).to eq(9)
|
90
90
|
end
|
91
91
|
|
92
92
|
it "should return 44 for subfactorial(5)" do
|
93
|
-
subject.subfactorial(5).
|
93
|
+
expect(subject.subfactorial(5)).to eq(44)
|
94
94
|
end
|
95
95
|
|
96
96
|
it "should raise RangeError for subfactorial(-1)" do
|
97
|
-
|
97
|
+
expect { subject.subfactorial(-1) }.to raise_error(RangeError)
|
98
98
|
end
|
99
99
|
end
|
100
100
|
end
|
@@ -4,11 +4,11 @@ require 'combinatorics/extensions/range'
|
|
4
4
|
describe Range do
|
5
5
|
describe "&" do
|
6
6
|
it "should pick the maximum beginning value" do
|
7
|
-
((100..200) & (150..200)).first.
|
7
|
+
expect(((100..200) & (150..200)).first).to eq(150)
|
8
8
|
end
|
9
9
|
|
10
10
|
it "should pick the minimum ending value" do
|
11
|
-
((100..150) & (100..200)).last.
|
11
|
+
expect(((100..150) & (100..200)).last).to eq(150)
|
12
12
|
end
|
13
13
|
end
|
14
14
|
|
@@ -16,28 +16,28 @@ describe Range do
|
|
16
16
|
subject { 1..10 }
|
17
17
|
|
18
18
|
it "should iterate over every beginning value" do
|
19
|
-
subject.upto(5..10).to_a.
|
19
|
+
expect(subject.upto(5..10).to_a).to eq([
|
20
20
|
(1..10),
|
21
21
|
(2..10),
|
22
22
|
(3..10),
|
23
23
|
(4..10),
|
24
24
|
(5..10)
|
25
|
-
]
|
25
|
+
])
|
26
26
|
end
|
27
27
|
|
28
28
|
it "should iterate over every ending value" do
|
29
|
-
subject.upto(1..15).to_a.
|
29
|
+
expect(subject.upto(1..15).to_a).to eq([
|
30
30
|
(1..10),
|
31
31
|
(1..11),
|
32
32
|
(1..12),
|
33
33
|
(1..13),
|
34
34
|
(1..14),
|
35
35
|
(1..15)
|
36
|
-
]
|
36
|
+
])
|
37
37
|
end
|
38
38
|
|
39
39
|
it "should not iterate up to lower bounding ranges" do
|
40
|
-
subject.upto(0..5).to_a.
|
40
|
+
expect(subject.upto(0..5).to_a).to be_empty
|
41
41
|
end
|
42
42
|
end
|
43
43
|
|
@@ -45,28 +45,28 @@ describe Range do
|
|
45
45
|
subject { 5..15 }
|
46
46
|
|
47
47
|
it "should iterate over every beginning value" do
|
48
|
-
subject.downto(1..15).to_a.
|
48
|
+
expect(subject.downto(1..15).to_a).to eq([
|
49
49
|
(5..15),
|
50
50
|
(4..15),
|
51
51
|
(3..15),
|
52
52
|
(2..15),
|
53
53
|
(1..15)
|
54
|
-
]
|
54
|
+
])
|
55
55
|
end
|
56
56
|
|
57
57
|
it "should iterate over every ending value" do
|
58
|
-
subject.downto(5..10).to_a.
|
58
|
+
expect(subject.downto(5..10).to_a).to eq([
|
59
59
|
(5..15),
|
60
60
|
(5..14),
|
61
61
|
(5..13),
|
62
62
|
(5..12),
|
63
63
|
(5..11),
|
64
64
|
(5..10)
|
65
|
-
]
|
65
|
+
])
|
66
66
|
end
|
67
67
|
|
68
68
|
it "should not iterate down to upward bounding ranges" do
|
69
|
-
subject.downto(10..20).to_a.
|
69
|
+
expect(subject.downto(10..20).to_a).to be_empty
|
70
70
|
end
|
71
71
|
end
|
72
72
|
end
|
data/spec/generator_spec.rb
CHANGED
@@ -6,46 +6,46 @@ describe "Array#comprehension" do
|
|
6
6
|
it "should return an Enumerator object if no block is given" do
|
7
7
|
a = [1..5]
|
8
8
|
|
9
|
-
a.comprehension.
|
9
|
+
expect(a.comprehension).not_to be_kind_of(Array)
|
10
10
|
end
|
11
11
|
|
12
12
|
it "should yield iterations to the given block" do
|
13
13
|
range = (1..5)
|
14
14
|
a = [range]
|
15
15
|
|
16
|
-
a.comprehension.to_a.
|
16
|
+
expect(a.comprehension.to_a).to eq([[1],[2],[3],[4],[5]])
|
17
17
|
end
|
18
18
|
|
19
19
|
it "should do nothing an Array of all non-enumerable objects" do
|
20
20
|
a = [1,2,3]
|
21
21
|
|
22
|
-
a.comprehension.to_a.
|
22
|
+
expect(a.comprehension.to_a).to eq([a])
|
23
23
|
end
|
24
24
|
|
25
25
|
it "should pass through an empty Array" do
|
26
26
|
a = []
|
27
27
|
|
28
|
-
a.comprehension.to_a.
|
28
|
+
expect(a.comprehension.to_a).to eq([a])
|
29
29
|
end
|
30
30
|
|
31
31
|
it "should iterate over the values within an enumerable value" do
|
32
32
|
range = (1..5)
|
33
33
|
a = [range]
|
34
34
|
|
35
|
-
a.comprehension.to_a.
|
35
|
+
expect(a.comprehension.to_a).to eq([[1],[2],[3],[4],[5]])
|
36
36
|
end
|
37
37
|
|
38
38
|
it "should iterate over repeating values" do
|
39
39
|
range = [1,2,3,1,2,4]
|
40
40
|
a = [range]
|
41
41
|
|
42
|
-
a.comprehension.to_a.
|
42
|
+
expect(a.comprehension.to_a).to eq([[1],[2],[3],[1],[2],[4]])
|
43
43
|
end
|
44
44
|
|
45
45
|
it "should iterate over values from a generator" do
|
46
46
|
a = [Combinatorics::Generator.new { |g| 5.times { |i| g.yield i } }]
|
47
47
|
|
48
|
-
a.comprehension.to_a.
|
48
|
+
expect(a.comprehension.to_a).to eq([[0],[1],[2],[3],[4]])
|
49
49
|
end
|
50
50
|
|
51
51
|
it "should iterate over values from a non-repeating generator" do
|
@@ -58,16 +58,16 @@ describe "Array#comprehension" do
|
|
58
58
|
}
|
59
59
|
]
|
60
60
|
|
61
|
-
a.comprehension.to_a.
|
61
|
+
expect(a.comprehension.to_a).to eq([
|
62
62
|
[1,0],[1,1],[1,2],[1,3],[1,4],
|
63
63
|
[2,0],[2,2],[2,4],[2,6],[2,8]
|
64
|
-
]
|
64
|
+
])
|
65
65
|
end
|
66
66
|
|
67
67
|
it "should ignore non-enumerable values" do
|
68
68
|
range = (1..5)
|
69
69
|
a = [1,range]
|
70
70
|
|
71
|
-
a.comprehension.to_a.
|
71
|
+
expect(a.comprehension.to_a).to eq([[1,1],[1,2],[1,3],[1,4],[1,5]])
|
72
72
|
end
|
73
73
|
end
|