summaryrefslogtreecommitdiffstats
path: root/test/ruby/test_array.rb
diff options
context:
space:
mode:
authormatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2007-09-29 08:43:59 +0000
committermatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2007-09-29 08:43:59 +0000
commitbf2a8104b4c3e5011ba05f549146ecc722c4efaf (patch)
tree715aba650bcb5a560ff6a283a755214443321d76 /test/ruby/test_array.rb
parentee2c93e8004437cc10ad642439adc11dfae5a0f4 (diff)
downloadruby-bf2a8104b4c3e5011ba05f549146ecc722c4efaf.tar.gz
ruby-bf2a8104b4c3e5011ba05f549146ecc722c4efaf.tar.xz
ruby-bf2a8104b4c3e5011ba05f549146ecc722c4efaf.zip
* array.c (rb_ary_combination): new method to give all combination
of elements from an array. [ruby-list:42671] * array.c (rb_ary_product): a new method to get all combinations of elements from two arrays. can be extended to combinations of n-arrays, e.g. a.product(b,c,d). anyone volunteer? * array.c (rb_ary_permutation): empty function body to calculate permutations of array elements. need volunteer. git-svn-id: http://svn.ruby-lang.org/repos/ruby/trunk@13568 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'test/ruby/test_array.rb')
-rw-r--r--test/ruby/test_array.rb22
1 files changed, 22 insertions, 0 deletions
diff --git a/test/ruby/test_array.rb b/test/ruby/test_array.rb
index 0d88f26dc..4d2b05304 100644
--- a/test/ruby/test_array.rb
+++ b/test/ruby/test_array.rb
@@ -1177,4 +1177,26 @@ class TestArray < Test::Unit::TestCase
assert_equal(@cls[1,2], @cls[1, 2] | @cls[1, 2])
end
+# def test_permutation
+# assert_equal(@cls[], @cls[1,2,3].permutation(0).to_a)
+# assert_equal(@cls[[1],[2],[3]], @cls[1,2,3].permutation(1).to_a)
+# assert_equal(@cls[[1,2],[1,3],[2,1],[2,3],[3,1],[3,2]], @cls[1,2,3].permutation(2).to_a)
+# assert_equal(@cls[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]], @cls[1,2,3].permutation(3).to_a)
+# assert_equal(@cls[], @cls[1,2,3].permutation(4).to_a)
+# end
+
+ def test_combination
+ assert_equal(@cls[], @cls[1,2,3,4].combination(0).to_a)
+ assert_equal(@cls[[1],[2],[3],[4]], @cls[1,2,3,4].combination(1).to_a)
+ assert_equal(@cls[[1,2],[1,3],[1,4],[2,3],[2,4],[3,4]], @cls[1,2,3,4].combination(2).to_a)
+ assert_equal(@cls[[1,2,3],[1,2,4],[1,3,4],[2,3,4]], @cls[1,2,3,4].combination(3).to_a)
+ assert_equal(@cls[[1,2,3,4]], @cls[1,2,3,4].combination(4).to_a)
+ assert_equal(@cls[], @cls[1,2,3,4].combination(5).to_a)
+ end
+
+ def test_product
+ assert_equal(@cls[[1,4],[1,5],[2,4],[2,5],[3,4],[3,5]],
+ @cls[1,2,3].product([4,5]))
+ assert_equal(@cls[[1,1],[1,2],[2,1],[2,2]], @cls[1,2].product([1,2]))
+ end
end