* 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.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9541 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
ocean 2005-11-15 07:06:00 +00:00
parent 54e04efc10
commit 011e0a5148
3 changed files with 37 additions and 5 deletions

View File

@ -1,3 +1,10 @@
Tue Nov 15 15:49:34 2005 Hirokazu Yamamoto <ocean@m2.ccsnet.ne.jp>
* 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 <matz@ruby-lang.org>
* array.c (rb_ary_fill): should adjust array length correctly when

View File

@ -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;
}
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) {

View File

@ -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