diff options
Diffstat (limited to 'mrbgems/mruby-array-ext')
| -rw-r--r-- | mrbgems/mruby-array-ext/mrbgem.rake | 1 | ||||
| -rw-r--r-- | mrbgems/mruby-array-ext/mrblib/array.rb | 267 | ||||
| -rw-r--r-- | mrbgems/mruby-array-ext/src/array.c | 203 | ||||
| -rw-r--r-- | mrbgems/mruby-array-ext/test/array.rb | 119 |
4 files changed, 337 insertions, 253 deletions
diff --git a/mrbgems/mruby-array-ext/mrbgem.rake b/mrbgems/mruby-array-ext/mrbgem.rake index 58d4428d4..882caf1ab 100644 --- a/mrbgems/mruby-array-ext/mrbgem.rake +++ b/mrbgems/mruby-array-ext/mrbgem.rake @@ -2,5 +2,4 @@ MRuby::Gem::Specification.new('mruby-array-ext') do |spec| spec.license = 'MIT' spec.author = 'mruby developers' spec.summary = 'Array class extension' - spec.add_test_dependency 'mruby-enumerator', core: 'mruby-enumerator' end diff --git a/mrbgems/mruby-array-ext/mrblib/array.rb b/mrbgems/mruby-array-ext/mrblib/array.rb index ed3f591fe..7520b932f 100644 --- a/mrbgems/mruby-array-ext/mrblib/array.rb +++ b/mrbgems/mruby-array-ext/mrblib/array.rb @@ -1,32 +1,6 @@ -# coding: cp932 class Array ## # call-seq: - # Array.try_convert(obj) -> array or nil - # - # Tries to convert +obj+ into an array, using +to_ary+ method. - # converted array or +nil+ if +obj+ cannot be converted for any reason. - # This method can be used to check if an argument is an array. - # - # Array.try_convert([1]) #=> [1] - # Array.try_convert("1") #=> nil - # - # if tmp = Array.try_convert(arg) - # # the argument is an array - # elsif tmp = String.try_convert(arg) - # # the argument is a string - # end - # - def self.try_convert(obj) - if obj.respond_to?(:to_ary) - obj.to_ary - else - nil - end - end - - ## - # call-seq: # ary.uniq! -> ary or nil # ary.uniq! { |item| ... } -> ary or nil # @@ -77,7 +51,7 @@ class Array # b.uniq { |s| s.first } # => [["student", "sam"], ["teacher", "matz"]] # def uniq(&block) - ary = self.dup + ary = self[0..-1] ary.uniq!(&block) ary end @@ -116,6 +90,22 @@ class Array ## # call-seq: + # ary.difference(other_ary1, other_ary2, ...) -> new_ary + # + # Returns a new array that is a copy of the original array, removing all + # occurrences of any item that also appear in +other_ary+. The order is + # preserved from the original array. + # + def difference(*args) + ary = self + args.each do |x| + ary = ary - x + end + ary + end + + ## + # call-seq: # ary | other_ary -> new_ary # # Set Union---Returns a new array by joining this array with @@ -160,7 +150,7 @@ class Array # [ 1, 1, 3, 5 ] & [ 1, 2, 3 ] #=> [ 1, 3 ] # def &(elem) - raise TypeError, "can't convert #{elem.class} into Array" unless elem.class == Array + raise TypeError, "cannot convert #{elem.class} into Array" unless elem.class == Array hash = {} array = [] @@ -185,6 +175,65 @@ class Array ## # call-seq: + # ary.intersection(other_ary,...) -> new_ary + # + # Set Intersection---Returns a new array containing elements common to + # this array and <i>other_ary</i>s, removing duplicates. The order is + # preserved from the original array. + # + # [1, 2, 3].intersection([3, 4, 1], [1, 3, 5]) #=> [1, 3] + # + def intersection(*args) + ary = self + args.each do |x| + ary = ary & x + end + ary + end + + ## + # call-seq: + # ary.intersect?(other_ary) -> true or false + # + # Returns +true+ if the array and +other_ary+ have at least one element in + # common, otherwise returns +false+. + # + # a = [ 1, 2, 3 ] + # b = [ 3, 4, 5 ] + # c = [ 5, 6, 7 ] + # a.intersect?(b) #=> true + # a.intersect?(c) #=> false + def intersect?(ary) + raise TypeError, "cannot convert #{ary.class} into Array" unless ary.class == Array + + hash = {} + if self.length > ary.length + shorter = ary + longer = self + else + shorter = self + longer = ary + end + idx = 0 + len = shorter.size + while idx < len + hash[shorter[idx]] = true + idx += 1 + end + idx = 0 + len = size + while idx < len + v = longer[idx] + if hash[v] + return true + end + idx += 1 + end + false + end + + ## + # call-seq: # ary.flatten -> new_ary # ary.flatten(level) -> new_ary # @@ -201,7 +250,7 @@ class Array # a.flatten(1) #=> [1, 2, 3, [4, 5]] # def flatten(depth=nil) - res = dup + res = Array.new(self) res.flatten! depth res end @@ -245,41 +294,6 @@ class Array end end - ## - # call-seq: - # ary.compact -> new_ary - # - # Returns a copy of +self+ with all +nil+ elements removed. - # - # [ "a", nil, "b", nil, "c", nil ].compact - # #=> [ "a", "b", "c" ] - # - def compact - result = self.dup - result.compact! - result - end - - ## - # call-seq: - # ary.compact! -> ary or nil - # - # Removes +nil+ elements from the array. - # Returns +nil+ if no changes were made, otherwise returns - # <i>ary</i>. - # - # [ "a", nil, "b", nil, "c" ].compact! #=> [ "a", "b", "c" ] - # [ "a", "b", "c" ].compact! #=> nil - # - def compact! - result = self.select { |e| !e.nil? } - if result.size == self.size - nil - else - self.replace(result) - end - end - # for efficiency def reverse_each(&block) return to_enum :reverse_each unless block @@ -292,7 +306,6 @@ class Array self end - NONE=Object.new ## # call-seq: # ary.fetch(index) -> obj @@ -317,8 +330,8 @@ class Array # #=> "100 is out of bounds" # - def fetch(n=nil, ifnone=NONE, &block) - warn "block supersedes default value argument" if !n.nil? && ifnone != NONE && block + def fetch(n, ifnone=NONE, &block) + #warn "block supersedes default value argument" if !n.nil? && ifnone != NONE && block idx = n if idx < 0 @@ -373,7 +386,6 @@ class Array end beg = len = 0 - ary = [] if block if arg0.nil? && arg1.nil? && arg2.nil? # ary.fill { |index| block } -> ary @@ -437,57 +449,6 @@ class Array ## # call-seq: - # ary.rotate(count=1) -> new_ary - # - # Returns a new array by rotating +self+ so that the element at +count+ is - # the first element of the new array. - # - # If +count+ is negative then it rotates in the opposite direction, starting - # from the end of +self+ where +-1+ is the last element. - # - # a = [ "a", "b", "c", "d" ] - # a.rotate #=> ["b", "c", "d", "a"] - # a #=> ["a", "b", "c", "d"] - # a.rotate(2) #=> ["c", "d", "a", "b"] - # a.rotate(-3) #=> ["b", "c", "d", "a"] - - def rotate(count=1) - ary = [] - len = self.length - - if len > 0 - idx = (count < 0) ? (len - (~count % len) - 1) : (count % len) # rotate count - len.times do - ary << self[idx] - idx += 1 - idx = 0 if idx > len-1 - end - end - ary - end - - ## - # call-seq: - # ary.rotate!(count=1) -> ary - # - # Rotates +self+ in place so that the element at +count+ comes first, and - # returns +self+. - # - # If +count+ is negative then it rotates in the opposite direction, starting - # from the end of the array where +-1+ is the last element. - # - # a = [ "a", "b", "c", "d" ] - # a.rotate! #=> ["b", "c", "d", "a"] - # a #=> ["b", "c", "d", "a"] - # a.rotate!(2) #=> ["d", "a", "b", "c"] - # a.rotate!(-3) #=> ["a", "b", "c", "d"] - - def rotate!(count=1) - self.replace(self.rotate(count)) - end - - ## - # call-seq: # ary.delete_if { |item| block } -> ary # ary.delete_if -> Enumerator # @@ -681,37 +642,6 @@ class Array ## # call-seq: - # ary.delete_if { |item| block } -> ary - # ary.delete_if -> Enumerator - # - # Deletes every element of +self+ for which block evaluates to +true+. - # - # The array is changed instantly every time the block is called, not after - # the iteration is over. - # - # See also Array#reject! - # - # If no block is given, an Enumerator is returned instead. - # - # scores = [ 97, 42, 75 ] - # scores.delete_if {|score| score < 80 } #=> [97] - - def delete_if(&block) - return to_enum :delete_if unless block - - idx = 0 - while idx < self.size do - if block.call(self[idx]) - self.delete_at(idx) - else - idx += 1 - end - end - self - end - - ## - # call-seq: # ary.keep_if { |item| block } -> ary # ary.keep_if -> Enumerator # @@ -729,7 +659,6 @@ class Array return to_enum :keep_if unless block idx = 0 - len = self.size while idx < self.size do if block.call(self[idx]) idx += 1 @@ -799,16 +728,6 @@ class Array end ## - # call-seq: - # ary.to_ary -> ary - # - # Returns +self+. - # - def to_ary - self - end - - ## # call-seq: # ary.dig(idx, ...) -> object # @@ -852,12 +771,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 0 < n && n <= size i = 0 while i<size result = [self[i]] @@ -872,6 +790,7 @@ class Array i += 1 end end + self end ## @@ -898,9 +817,8 @@ class Array # a.combination(5).to_a #=> [] # 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 @@ -909,7 +827,7 @@ class Array yield [self[i]] i += 1 end - else + elsif n <= size i = 0 while i<size result = [self[i]] @@ -919,6 +837,7 @@ class Array i += 1 end end + self end ## @@ -927,7 +846,7 @@ class Array # # Assumes that self is an array of arrays and transposes the rows and columns. # - # If the length of the subarrays don’t match, an IndexError is raised. + # If the length of the subarrays don't match, an IndexError is raised. # # Examples: # @@ -940,8 +859,8 @@ class Array column_count = nil self.each do |row| raise TypeError unless row.is_a?(Array) - column_count ||= row.count - raise IndexError, 'element size differs' unless column_count == row.count + column_count ||= row.size + raise IndexError, 'element size differs' unless column_count == row.size end Array.new(column_count) do |column_index| @@ -954,7 +873,7 @@ class Array # ary.to_h -> Hash # ary.to_h{|item| ... } -> Hash # - # Returns the result of interpreting <i>aray</i> as an array of + # Returns the result of interpreting <i>array</i> as an array of # <tt>[key, value]</tt> pairs. If a block is given, it should # return <tt>[key, value]</tt> pairs to construct a hash. # @@ -973,4 +892,8 @@ class Array end h end + + alias append push + alias prepend unshift + alias filter! select! end diff --git a/mrbgems/mruby-array-ext/src/array.c b/mrbgems/mruby-array-ext/src/array.c index b6d9c9c80..d97778642 100644 --- a/mrbgems/mruby-array-ext/src/array.c +++ b/mrbgems/mruby-array-ext/src/array.c @@ -3,6 +3,7 @@ #include <mruby/array.h> #include <mruby/range.h> #include <mruby/hash.h> +#include <mruby/presym.h> /* * call-seq: @@ -28,9 +29,8 @@ static mrb_value mrb_ary_assoc(mrb_state *mrb, mrb_value ary) { mrb_int i; - mrb_value v, k; - - mrb_get_args(mrb, "o", &k); + mrb_value v; + mrb_value k = mrb_get_arg1(mrb); for (i = 0; i < RARRAY_LEN(ary); ++i) { v = mrb_check_array_type(mrb, RARRAY_PTR(ary)[i]); @@ -59,13 +59,12 @@ static mrb_value mrb_ary_rassoc(mrb_state *mrb, mrb_value ary) { mrb_int i; - mrb_value v, value; - - mrb_get_args(mrb, "o", &value); + mrb_value v; + mrb_value value = mrb_get_arg1(mrb); for (i = 0; i < RARRAY_LEN(ary); ++i) { v = RARRAY_PTR(ary)[i]; - if (mrb_type(v) == MRB_TT_ARRAY && + if (mrb_array_p(v) && RARRAY_LEN(v) > 1 && mrb_equal(mrb, RARRAY_PTR(v)[1], value)) return v; @@ -96,17 +95,22 @@ mrb_ary_at(mrb_state *mrb, mrb_value ary) } static mrb_value +ary_ref(mrb_state *mrb, mrb_value ary, mrb_int n) +{ + return mrb_ary_entry(ary, n); +} + +static mrb_value mrb_ary_values_at(mrb_state *mrb, mrb_value self) { mrb_int argc; - mrb_value *argv; + const mrb_value *argv; mrb_get_args(mrb, "*", &argv, &argc); - return mrb_get_values_at(mrb, self, RARRAY_LEN(self), argc, argv, mrb_ary_ref); + return mrb_get_values_at(mrb, self, RARRAY_LEN(self), argc, argv, ary_ref); } - /* * call-seq: * ary.slice!(index) -> obj or nil @@ -140,22 +144,21 @@ mrb_ary_slice_bang(mrb_state *mrb, mrb_value self) mrb_ary_modify(mrb, a); if (mrb_get_argc(mrb) == 1) { - mrb_value index; + mrb_value index = mrb_get_arg1(mrb); - mrb_get_args(mrb, "o|i", &index, &len); switch (mrb_type(index)) { case MRB_TT_RANGE: - if (mrb_range_beg_len(mrb, index, &i, &len, ARY_LEN(a), TRUE) == 1) { + if (mrb_range_beg_len(mrb, index, &i, &len, ARY_LEN(a), TRUE) == MRB_RANGE_OK) { goto delete_pos_len; } else { return mrb_nil_value(); } - case MRB_TT_FIXNUM: - val = mrb_funcall(mrb, self, "delete_at", 1, index); + case MRB_TT_INTEGER: + val = mrb_funcall_id(mrb, self, MRB_SYM(delete_at), 1, index); return val; default: - val = mrb_funcall(mrb, self, "delete_at", 1, index); + val = mrb_funcall_id(mrb, self, MRB_SYM(delete_at), 1, index); return val; } } @@ -185,6 +188,168 @@ mrb_ary_slice_bang(mrb_state *mrb, mrb_value self) return ary; } +/* + * call-seq: + * ary.compact -> new_ary + * + * Returns a copy of +self+ with all +nil+ elements removed. + * + * [ "a", nil, "b", nil, "c", nil ].compact + * #=> [ "a", "b", "c" ] + */ + +static mrb_value +mrb_ary_compact(mrb_state *mrb, mrb_value self) +{ + mrb_value ary = mrb_ary_new(mrb); + mrb_int len = RARRAY_LEN(self); + mrb_value *p = RARRAY_PTR(self); + + for (mrb_int i = 0; i < len; ++i) { + if (!mrb_nil_p(p[i])) { + mrb_ary_push(mrb, ary, p[i]); + } + } + return ary; +} + +/* + * call-seq: + * ary.compact! -> ary or nil + * + * Removes +nil+ elements from the array. + * Returns +nil+ if no changes were made, otherwise returns + * <i>ary</i>. + * + * [ "a", nil, "b", nil, "c" ].compact! #=> [ "a", "b", "c" ] + * [ "a", "b", "c" ].compact! #=> nil + */ +static mrb_value +mrb_ary_compact_bang(mrb_state *mrb, mrb_value self) +{ + struct RArray *a = mrb_ary_ptr(self); + mrb_int i, j = 0; + mrb_int len = ARY_LEN(a); + mrb_value *p = ARY_PTR(a); + + mrb_ary_modify(mrb, a); + for (i = 0; i < len; ++i) { + if (!mrb_nil_p(p[i])) { + if (i != j) p[j] = p[i]; + j++; + } + } + if (i == j) return mrb_nil_value(); + if (j < len) ARY_SET_LEN(RARRAY(self), j); + return self; +} + + +/* + * call-seq: + * ary.rotate(count=1) -> new_ary + * + * Returns a new array by rotating +self+ so that the element at +count+ is + * the first element of the new array. + * + * If +count+ is negative then it rotates in the opposite direction, starting + * from the end of +self+ where +-1+ is the last element. + * + * a = [ "a", "b", "c", "d" ] + * a.rotate #=> ["b", "c", "d", "a"] + * a #=> ["a", "b", "c", "d"] + * a.rotate(2) #=> ["c", "d", "a", "b"] + * a.rotate(-3) #=> ["b", "c", "d", "a"] + */ +static mrb_value +mrb_ary_rotate(mrb_state *mrb, mrb_value self) +{ + mrb_value ary = mrb_ary_new(mrb); + mrb_int len = RARRAY_LEN(self); + mrb_value *p = RARRAY_PTR(self); + mrb_int count=1, idx; + + mrb_get_args(mrb, "|i", &count); + if (len <= 0) return ary; + if (count < 0) { + idx = len - (~count % len) - 1; + } + else { + idx = count % len; + } + for (mrb_int i = 0; i<len; i++) { + mrb_ary_push(mrb, ary, p[idx++]); + if (idx == len) idx = 0; + } + return ary; +} + +static void +rev(mrb_value *p, mrb_int beg, mrb_int end) +{ + for (mrb_int i=beg,j=end-1; i<j; i++,j--) { + mrb_value v = p[i]; + p[i] = p[j]; + p[j] = v; + } +} + +/* + * call-seq: + * ary.rotate!(count=1) -> ary + * + * Rotates +self+ in place so that the element at +count+ comes first, and + * returns +self+. + * + * If +count+ is negative then it rotates in the opposite direction, starting + * from the end of the array where +-1+ is the last element. + * + * a = [ "a", "b", "c", "d" ] + * a.rotate! #=> ["b", "c", "d", "a"] + * a #=> ["b", "c", "d", "a"] + * a.rotate!(2) #=> ["d", "a", "b", "c"] + * a.rotate!(-3) #=> ["a", "b", "c", "d"] + */ +static mrb_value +mrb_ary_rotate_bang(mrb_state *mrb, mrb_value self) +{ + struct RArray *a = mrb_ary_ptr(self); + mrb_int len = ARY_LEN(a); + mrb_value *p = ARY_PTR(a); + mrb_int count=1, idx; + + mrb_get_args(mrb, "|i", &count); + mrb_ary_modify(mrb, a); + if (len == 0 || count == 0) return self; + if (count == 1) { + mrb_value v = p[0]; + for (mrb_int i=1; i<len; i++) { + p[i-1] = p[i]; + } + p[len-1] = v; + return self; + } + if (count < 0) { + idx = len - (~count % len) - 1; + } + else { + idx = count % len; + } + /* e.g. [1,2,3,4,5].rotate!(2) -> [3,4,5,1,2] */ + /* first, reverse the whole array */ + /* [1,2,3,4,5] -> [5,4,3,2,1] */ + rev(p, 0, len); + /* then, re-reverse part before idx */ + /* [5,4,3,2,1] -> [3,4,5,2,1] */ + /* ^idx ~~~~~ */ + rev(p, 0, len-idx); + /* finally, re-reverse part after idx */ + /* [3,4,5,2,1] -> [3,4,5,1,2] */ + /* ^idx ~~~ */ + rev(p, len-idx, len); + return self; +} + void mrb_mruby_array_ext_gem_init(mrb_state* mrb) { @@ -194,7 +359,11 @@ mrb_mruby_array_ext_gem_init(mrb_state* mrb) mrb_define_method(mrb, a, "at", mrb_ary_at, MRB_ARGS_REQ(1)); mrb_define_method(mrb, a, "rassoc", mrb_ary_rassoc, MRB_ARGS_REQ(1)); mrb_define_method(mrb, a, "values_at", mrb_ary_values_at, MRB_ARGS_ANY()); - mrb_define_method(mrb, a, "slice!", mrb_ary_slice_bang, MRB_ARGS_ANY()); + mrb_define_method(mrb, a, "slice!", mrb_ary_slice_bang, MRB_ARGS_ARG(1,1)); + mrb_define_method(mrb, a, "compact", mrb_ary_compact, MRB_ARGS_NONE()); + mrb_define_method(mrb, a, "compact!", mrb_ary_compact_bang, MRB_ARGS_NONE()); + mrb_define_method(mrb, a, "rotate", mrb_ary_rotate, MRB_ARGS_OPT(1)); + mrb_define_method(mrb, a, "rotate!", mrb_ary_rotate_bang, MRB_ARGS_OPT(1)); } void diff --git a/mrbgems/mruby-array-ext/test/array.rb b/mrbgems/mruby-array-ext/test/array.rb index 84f9cfeaf..3f73ad8b9 100644 --- a/mrbgems/mruby-array-ext/test/array.rb +++ b/mrbgems/mruby-array-ext/test/array.rb @@ -1,11 +1,21 @@ ## # Array(Ext) Test -assert("Array.try_convert") do - assert_nil Array.try_convert(0) - assert_nil Array.try_convert(nil) - assert_equal [], Array.try_convert([]) - assert_equal [1,2,3], Array.try_convert([1,2,3]) +def assert_permutation_combination(exp, receiver, meth, *args) + act = [] + ret = receiver.__send__(meth, *args) { |v| act << v } + assert "assert_#{meth}" do + assert_equal(exp, act.sort) + assert_same(receiver, ret) + end +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 @@ -83,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, 6, 7] + b = [1, 4, 6] + c = [1, 5, 7] + + assert_equal [2, 3], a.difference(b,c) +end + assert("Array#&") do a = [1, 2, 3, 1] b = [1, 4] @@ -93,6 +111,22 @@ 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#intersect?") do + a = [ 1, 2, 3 ] + b = [ 3, 4, 5 ] + c = [ 5, 6, 7 ] + assert_true(a.intersect?(b)) + assert_false(a.intersect?(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 @@ -169,12 +203,6 @@ assert("Array#reverse_each") do b << i end assert_equal [ "d", "c", "b", "a" ], b - - if Object.const_defined?(:Enumerator) - assert_equal [ "d", "c", "b", "a" ], a.reverse_each.to_a - else - true - end end assert("Array#rotate") do @@ -275,22 +303,9 @@ assert("Array#bsearch") do end end -assert("Array#bsearch_index") do - # tested through Array#bsearch -end - -assert("Array#delete_if") do - a = [1, 2, 3, 4, 5] - assert_equal [1, 2, 3, 4, 5], a.delete_if { false } - assert_equal [1, 2, 3, 4, 5], a - - a = [1, 2, 3, 4, 5] - assert_equal [], a.delete_if { true } - assert_equal [], a - - a = [ 1, 2, 3, 4, 5 ] - assert_equal [1, 2, 3], a.delete_if { |val| val > 3 } -end +# tested through Array#bsearch +#assert("Array#bsearch_index") do +#end assert("Array#keep_if") do a = [1, 2, 3, 4, 5] @@ -338,27 +353,11 @@ assert('Array#to_h') do assert_raise(ArgumentError) { [[1]].to_h } end -assert('Array#to_h (Modified)') do - class A - def to_ary - $a.clear - nil - end - end - $a = [A.new] - assert_raise(TypeError) { $a.to_h } -end - assert("Array#index (block)") do assert_nil (1..10).to_a.index { |i| i % 5 == 0 and i % 7 == 0 } assert_equal 34, (1..100).to_a.index { |i| i % 5 == 0 and i % 7 == 0 } end -assert("Array#to_ary") do - assert_equal [], [].to_ary - assert_equal [1,2,3], [1,2,3].to_ary -end - assert("Array#dig") do h = [[[1]], 0] assert_equal(1, h.dig(0, 0, 0)) @@ -392,30 +391,24 @@ 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) + assert_permutation([], a, -1) 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) + assert_combination([], a, -1) end assert('Array#transpose') do |
