diff --git a/ChangeLog b/ChangeLog index 5d03153dd3..0fee90f0fb 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +Tue Nov 15 15:49:34 2005 Hirokazu Yamamoto + + * array.c (rb_ary_fill): tail elements were vanished when the middle + part of array was filled. (ie: [0,1,2,3,4].fill(-1,2,1) => [0,1,-1]) + + * test/ruby/test_array.rb (test_fill): added. + Tue Nov 15 14:39:16 2005 Yukihiro Matsumoto * array.c (rb_ary_fill): should adjust array length correctly when diff --git a/array.c b/array.c index abf3ab7218..9ca499b5ef 100644 --- a/array.c +++ b/array.c @@ -2137,9 +2137,13 @@ rb_ary_fill(int argc, VALUE *argv, VALUE ary) REALLOC_N(RARRAY(ary)->ptr, VALUE, end); RARRAY(ary)->aux.capa = end; } - if (beg > RARRAY(ary)->len) { - rb_mem_clear(RARRAY(ary)->ptr + RARRAY(ary)->len, end - RARRAY(ary)->len); - } + RARRAY(ary)->len = end; + } + if (beg > RARRAY(ary)->len) { + rb_mem_clear(RARRAY(ary)->ptr + RARRAY(ary)->len, end - RARRAY(ary)->len); + } + else { + rb_mem_clear(RARRAY(ary)->ptr + beg, end - beg); } if (block_p) { @@ -2150,11 +2154,9 @@ rb_ary_fill(int argc, VALUE *argv, VALUE ary) v = rb_yield(LONG2NUM(i)); if (i>=RARRAY(ary)->len) break; RARRAY(ary)->ptr[i] = v; - RARRAY(ary)->len = i+1; } } else { - RARRAY(ary)->len = end; p = RARRAY(ary)->ptr + beg; pend = p + len; while (p < pend) { diff --git a/test/ruby/test_array.rb b/test/ruby/test_array.rb index aebf360260..5d85927da7 100644 --- a/test/ruby/test_array.rb +++ b/test/ruby/test_array.rb @@ -136,4 +136,27 @@ class TestArray < Test::Unit::TestCase assert_equal(["baz","baz"], x.find_all{ |obj| obj == "baz" }) assert_equal([3,3], x.find_all{ |obj| obj == 3 }) end + + def test_fill + assert_equal([-1, -1, -1, -1, -1, -1], [0, 1, 2, 3, 4, 5].fill(-1)) + assert_equal([0, 1, 2, -1, -1, -1], [0, 1, 2, 3, 4, 5].fill(-1, 3)) + assert_equal([0, 1, 2, -1, -1, 5], [0, 1, 2, 3, 4, 5].fill(-1, 3, 2)) + assert_equal([0, 1, 2, -1, -1, -1, -1, -1], [0, 1, 2, 3, 4, 5].fill(-1, 3, 5)) + assert_equal([0, 1, -1, -1, 4, 5], [0, 1, 2, 3, 4, 5].fill(-1, 2, 2)) + assert_equal([0, 1, -1, -1, -1, -1, -1], [0, 1, 2, 3, 4, 5].fill(-1, 2, 5)) + assert_equal([0, 1, 2, 3, -1, 5], [0, 1, 2, 3, 4, 5].fill(-1, -2, 1)) + assert_equal([0, 1, 2, 3, -1, -1, -1], [0, 1, 2, 3, 4, 5].fill(-1, -2, 3)) + assert_equal([0, 1, 2, -1, -1, 5], [0, 1, 2, 3, 4, 5].fill(-1, 3..4)) + assert_equal([0, 1, 2, -1, 4, 5], [0, 1, 2, 3, 4, 5].fill(-1, 3...4)) + assert_equal([0, 1, -1, -1, -1, 5], [0, 1, 2, 3, 4, 5].fill(-1, 2..-2)) + assert_equal([0, 1, -1, -1, 4, 5], [0, 1, 2, 3, 4, 5].fill(-1, 2...-2)) + assert_equal([10, 11, 12, 13, 14, 15], [0, 1, 2, 3, 4, 5].fill{|i| i+10}) + assert_equal([0, 1, 2, 13, 14, 15], [0, 1, 2, 3, 4, 5].fill(3){|i| i+10}) + assert_equal([0, 1, 2, 13, 14, 5], [0, 1, 2, 3, 4, 5].fill(3, 2){|i| i+10}) + assert_equal([0, 1, 2, 13, 14, 15, 16, 17], [0, 1, 2, 3, 4, 5].fill(3, 5){|i| i+10}) + assert_equal([0, 1, 2, 13, 14, 5], [0, 1, 2, 3, 4, 5].fill(3..4){|i| i+10}) + assert_equal([0, 1, 2, 13, 4, 5], [0, 1, 2, 3, 4, 5].fill(3...4){|i| i+10}) + assert_equal([0, 1, 12, 13, 14, 5], [0, 1, 2, 3, 4, 5].fill(2..-2){|i| i+10}) + assert_equal([0, 1, 12, 13, 4, 5], [0, 1, 2, 3, 4, 5].fill(2...-2){|i| i+10}) + end end