Commit 87cc58ba authored by KOBAYASHI Shuji's avatar KOBAYASHI Shuji

Refine `Array#(permutation|combination) test`

- No guarantees about the order in which the permutations/combinations
  are yielded.

- Drop dependency on `Enumerator`.
parent 5779464e
## ##
# Array(Ext) Test # Array(Ext) Test
def assert_permutation_combination(exp, receiver, meth, *args)
act = []
receiver.__send__(meth, *args) { |v| act << v }
assert_equal(exp, act.sort)
end
def assert_permutation(exp, receiver, *args)
assert_permutation_combination(exp, receiver, :permutation, *args)
end
def assert_combination(exp, receiver, *args)
assert_permutation_combination(exp, receiver, :combination, *args)
end
assert("Array#assoc") do assert("Array#assoc") do
s1 = [ "colors", "red", "blue", "green" ] s1 = [ "colors", "red", "blue", "green" ]
s2 = [ "letters", "a", "b", "c" ] s2 = [ "letters", "a", "b", "c" ]
...@@ -369,30 +383,22 @@ end ...@@ -369,30 +383,22 @@ end
assert("Array#permutation") do assert("Array#permutation") do
a = [1, 2, 3] a = [1, 2, 3]
assert_equal([[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]], assert_permutation([[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]], a)
a.permutation.to_a) assert_permutation([[1],[2],[3]], a, 1)
assert_equal([[1],[2],[3]], assert_permutation([[1,2],[1,3],[2,1],[2,3],[3,1],[3,2]], a, 2)
a.permutation(1).to_a) assert_permutation([[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]], a, 3)
assert_equal([[1,2],[1,3],[2,1],[2,3],[3,1],[3,2]], assert_permutation([[]], a, 0)
a.permutation(2).to_a) assert_permutation([], a, 4)
assert_equal([[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]],
a.permutation(3).to_a)
assert_equal([[]], a.permutation(0).to_a)
assert_equal([], a.permutation(4).to_a)
end end
assert("Array#combination") do assert("Array#combination") do
a = [1, 2, 3, 4] a = [1, 2, 3, 4]
assert_equal([[1],[2],[3],[4]], assert_combination([[1],[2],[3],[4]], a, 1)
a.combination(1).to_a) assert_combination([[1,2],[1,3],[1,4],[2,3],[2,4],[3,4]], a, 2)
assert_equal([[1,2],[1,3],[1,4],[2,3],[2,4],[3,4]], assert_combination([[1,2,3],[1,2,4],[1,3,4],[2,3,4]], a, 3)
a.combination(2).to_a) assert_combination([[1,2,3,4]], a, 4)
assert_equal([[1,2,3],[1,2,4],[1,3,4],[2,3,4]], assert_combination([[]], a, 0)
a.combination(3).to_a) assert_combination([], a, 5)
assert_equal([[1,2,3,4]],
a.combination(4).to_a)
assert_equal([[]], a.combination(0).to_a)
assert_equal([], a.combination(5).to_a)
end end
assert('Array#transpose') do assert('Array#transpose') do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment