From 87cc58ba3d851651ba90947df475388a3dc7ded8 Mon Sep 17 00:00:00 2001 From: KOBAYASHI Shuji Date: Thu, 25 Jul 2019 22:31:54 +0900 Subject: Refine `Array#(permutation|combination) test` - No guarantees about the order in which the permutations/combinations are yielded. - Drop dependency on `Enumerator`. --- mrbgems/mruby-array-ext/test/array.rb | 46 ++++++++++++++++++++--------------- 1 file changed, 26 insertions(+), 20 deletions(-) (limited to 'mrbgems/mruby-array-ext/test/array.rb') diff --git a/mrbgems/mruby-array-ext/test/array.rb b/mrbgems/mruby-array-ext/test/array.rb index 82f09297a..4fad42518 100644 --- a/mrbgems/mruby-array-ext/test/array.rb +++ b/mrbgems/mruby-array-ext/test/array.rb @@ -1,6 +1,20 @@ ## # 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 s1 = [ "colors", "red", "blue", "green" ] s2 = [ "letters", "a", "b", "c" ] @@ -369,30 +383,22 @@ end assert("Array#permutation") do a = [1, 2, 3] - assert_equal([[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]], - a.permutation.to_a) - assert_equal([[1],[2],[3]], - a.permutation(1).to_a) - assert_equal([[1,2],[1,3],[2,1],[2,3],[3,1],[3,2]], - a.permutation(2).to_a) - 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) + assert_permutation([[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]], a) + assert_permutation([[1],[2],[3]], a, 1) + assert_permutation([[1,2],[1,3],[2,1],[2,3],[3,1],[3,2]], a, 2) + assert_permutation([[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]], a, 3) + assert_permutation([[]], a, 0) + assert_permutation([], a, 4) end assert("Array#combination") do a = [1, 2, 3, 4] - assert_equal([[1],[2],[3],[4]], - a.combination(1).to_a) - assert_equal([[1,2],[1,3],[1,4],[2,3],[2,4],[3,4]], - a.combination(2).to_a) - assert_equal([[1,2,3],[1,2,4],[1,3,4],[2,3,4]], - a.combination(3).to_a) - 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) + assert_combination([[1],[2],[3],[4]], a, 1) + assert_combination([[1,2],[1,3],[1,4],[2,3],[2,4],[3,4]], a, 2) + assert_combination([[1,2,3],[1,2,4],[1,3,4],[2,3,4]], a, 3) + assert_combination([[1,2,3,4]], a, 4) + assert_combination([[]], a, 0) + assert_combination([], a, 5) end assert('Array#transpose') do -- cgit v1.2.3