From c1e6f324c29d16014a8c6c27b9e27cf9af9fc1d5 Mon Sep 17 00:00:00 2001 From: KOBAYASHI Shuji Date: Fri, 31 May 2019 20:50:54 +0900 Subject: Comment out the empty `Array#bsearch_index` test --- mrbgems/mruby-array-ext/test/array.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 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 853554bcc..82f09297a 100644 --- a/mrbgems/mruby-array-ext/test/array.rb +++ b/mrbgems/mruby-array-ext/test/array.rb @@ -268,9 +268,9 @@ assert("Array#bsearch") do end end -assert("Array#bsearch_index") do - # tested through Array#bsearch -end +# tested through Array#bsearch +#assert("Array#bsearch_index") do +#end assert("Array#delete_if") do a = [1, 2, 3, 4, 5] -- cgit v1.2.3 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 From 28d1f6cdc48782247b6d39be0e1a794404da26f5 Mon Sep 17 00:00:00 2001 From: KOBAYASHI Shuji Date: Fri, 30 Aug 2019 17:04:25 +0900 Subject: `Array#(permutation|combination)` without block should return `self` --- mrbgems/mruby-array-ext/mrblib/array.rb | 14 +++++++------- mrbgems/mruby-array-ext/test/array.rb | 7 +++++-- 2 files changed, 12 insertions(+), 9 deletions(-) (limited to 'mrbgems/mruby-array-ext/test/array.rb') diff --git a/mrbgems/mruby-array-ext/mrblib/array.rb b/mrbgems/mruby-array-ext/mrblib/array.rb index 1cd1eb643..fef78f83f 100644 --- a/mrbgems/mruby-array-ext/mrblib/array.rb +++ b/mrbgems/mruby-array-ext/mrblib/array.rb @@ -815,12 +815,11 @@ class Array # a.permutation(0).to_a #=> [[]] # one permutation of length 0 # a.permutation(4).to_a #=> [] # no permutations of length 4 def permutation(n=self.size, &block) - size = self.size return to_enum(:permutation, n) unless block - return if n > size + size = self.size if n == 0 - yield [] - else + yield [] + elsif n <= size i = 0 while i [] # no combinations of length 5 def combination(n, &block) - size = self.size return to_enum(:combination, n) unless block - return if n > size + size = self.size if n == 0 yield [] elsif n == 1 @@ -872,7 +871,7 @@ class Array yield [self[i]] i += 1 end - else + elsif n <= size i = 0 while i Date: Sat, 31 Aug 2019 13:03:30 +0900 Subject: `Array#permutation` with a negative argument should not yield Before this patch: $ bin/mruby -e '[1].permutation(-1){|v| p v}' #=> [1] After this patch (same as Ruby): $ bin/mruby -e '[1].permutation(-1){|v| p v}' #=> no output --- mrbgems/mruby-array-ext/mrblib/array.rb | 2 +- mrbgems/mruby-array-ext/test/array.rb | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) (limited to 'mrbgems/mruby-array-ext/test/array.rb') diff --git a/mrbgems/mruby-array-ext/mrblib/array.rb b/mrbgems/mruby-array-ext/mrblib/array.rb index fef78f83f..fc5d87f2c 100644 --- a/mrbgems/mruby-array-ext/mrblib/array.rb +++ b/mrbgems/mruby-array-ext/mrblib/array.rb @@ -819,7 +819,7 @@ class Array size = self.size if n == 0 yield [] - elsif n <= size + elsif 0 < n && n <= size i = 0 while i Date: Mon, 16 Sep 2019 07:52:19 +0900 Subject: Add `Array#difference` method from Ruby2.6. --- mrbgems/mruby-array-ext/mrblib/array.rb | 16 ++++++++++++++++ mrbgems/mruby-array-ext/test/array.rb | 8 ++++++++ 2 files changed, 24 insertions(+) (limited to 'mrbgems/mruby-array-ext/test/array.rb') diff --git a/mrbgems/mruby-array-ext/mrblib/array.rb b/mrbgems/mruby-array-ext/mrblib/array.rb index fc5d87f2c..f9f232321 100644 --- a/mrbgems/mruby-array-ext/mrblib/array.rb +++ b/mrbgems/mruby-array-ext/mrblib/array.rb @@ -124,6 +124,22 @@ class Array ary end + ## + # call-seq: + # ary.difference(other_ary1, other_ary2, ...) -> new_ary + # + # Returns a new array that is a copy of the original array, removing all + # occurences of any item that also appear in +other_ary+. The order is + # preserved from the original array. + # + def difference(*args) + ary = self.dup + args.each do |x| + ary = self - x + end + ary + end + ## # call-seq: # ary & other_ary -> new_ary diff --git a/mrbgems/mruby-array-ext/test/array.rb b/mrbgems/mruby-array-ext/test/array.rb index a4e328b71..029325aab 100644 --- a/mrbgems/mruby-array-ext/test/array.rb +++ b/mrbgems/mruby-array-ext/test/array.rb @@ -93,6 +93,14 @@ assert("Array#union") do assert_equal [1, 2, 3, 4, 5], a.union(b,c) end +assert("Array#difference") do + a = [1, 2, 3, 1] + b = [1, 4] + c = [1, 5] + + assert_equal [2, 3], a.difference(b,c) +end + assert("Array#&") do a = [1, 2, 3, 1] b = [1, 4] -- cgit v1.2.3 From 67ea80b2bd2df64bbfefd0ba8f74f6eeb52074aa Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Sat, 12 Oct 2019 12:38:25 +0900 Subject: Fixed a bug in `Array#difference`. --- mrbgems/mruby-array-ext/mrblib/array.rb | 4 ++-- mrbgems/mruby-array-ext/test/array.rb | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) (limited to 'mrbgems/mruby-array-ext/test/array.rb') diff --git a/mrbgems/mruby-array-ext/mrblib/array.rb b/mrbgems/mruby-array-ext/mrblib/array.rb index 7cec44dba..d4a72a927 100644 --- a/mrbgems/mruby-array-ext/mrblib/array.rb +++ b/mrbgems/mruby-array-ext/mrblib/array.rb @@ -133,9 +133,9 @@ class Array # preserved from the original array. # def difference(*args) - ary = self.dup + ary = self args.each do |x| - ary = self - x + ary = ary - x end ary end diff --git a/mrbgems/mruby-array-ext/test/array.rb b/mrbgems/mruby-array-ext/test/array.rb index 029325aab..0cfb1e857 100644 --- a/mrbgems/mruby-array-ext/test/array.rb +++ b/mrbgems/mruby-array-ext/test/array.rb @@ -94,9 +94,9 @@ assert("Array#union") do end assert("Array#difference") do - a = [1, 2, 3, 1] - b = [1, 4] - c = [1, 5] + a = [1, 2, 3, 1, 6, 7] + b = [1, 4, 6] + c = [1, 5, 7] assert_equal [2, 3], a.difference(b,c) end -- cgit v1.2.3 From bdacdfaea9bf4b52770c32d60b7d402b48297c58 Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Sat, 12 Oct 2019 12:39:29 +0900 Subject: Add `Array#intersection` which is new in Ruby2.7. --- mrbgems/mruby-array-ext/mrblib/array.rb | 18 ++++++++++++++++++ mrbgems/mruby-array-ext/test/array.rb | 8 ++++++++ 2 files changed, 26 insertions(+) (limited to 'mrbgems/mruby-array-ext/test/array.rb') diff --git a/mrbgems/mruby-array-ext/mrblib/array.rb b/mrbgems/mruby-array-ext/mrblib/array.rb index fa3db3a70..be1676666 100644 --- a/mrbgems/mruby-array-ext/mrblib/array.rb +++ b/mrbgems/mruby-array-ext/mrblib/array.rb @@ -173,6 +173,24 @@ class Array array end + ## + # call-seq: + # ary.intersection(other_ary,...) -> new_ary + # + # Set Intersection---Returns a new array containing elements common to + # this array and other_ary, removing duplicates. + # + # ["a", "b", "c"].union(["c", "d", "a"], ["a", "c", "e"]) + # #=> ["a", "b", "c", "d", "e"] + # + def intersection(*args) + ary = self + args.each do |x| + ary = ary & x + end + ary + end + ## # call-seq: # ary.flatten -> new_ary diff --git a/mrbgems/mruby-array-ext/test/array.rb b/mrbgems/mruby-array-ext/test/array.rb index 0cfb1e857..cb76559c7 100644 --- a/mrbgems/mruby-array-ext/test/array.rb +++ b/mrbgems/mruby-array-ext/test/array.rb @@ -111,6 +111,14 @@ assert("Array#&") do assert_equal [1, 2, 3, 1], a end +assert("Array#intersection") do + a = [1, 2, 3, 1, 8, 6, 7, 8] + b = [1, 4, 6, 8] + c = [1, 5, 7, 8] + + assert_equal [1, 8], a.intersection(b,c) +end + assert("Array#flatten") do assert_equal [1, 2, "3", {4=>5}, :'6'], [1, 2, "3", {4=>5}, :'6'].flatten assert_equal [1, 2, 3, 4, 5, 6], [1, 2, [3, 4, 5], 6].flatten -- cgit v1.2.3