summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorocean <ocean@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2005-11-15 07:06:00 +0000
committerocean <ocean@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2005-11-15 07:06:00 +0000
commit518851f88d2bf0a66b829f8f05373f8ac8d1fdc4 (patch)
tree4a83f24ae6fc06a55c04332ccf7163ebea1aa5a6
parent8950f856030ff9ed33ad7126d190a3f115c9eba8 (diff)
* 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/branches/ruby_1_8@9541 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog7
-rw-r--r--array.c12
-rw-r--r--test/ruby/test_array.rb23
3 files changed, 37 insertions, 5 deletions
diff --git a/ChangeLog b/ChangeLog
index af23fed..bc21d5d 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+Tue Nov 15 16:04:10 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
diff --git a/array.c b/array.c
index e47ecf3..b23c311 100644
--- a/array.c
+++ b/array.c
@@ -2261,9 +2261,13 @@ rb_ary_fill(argc, argv, 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) {
@@ -2274,11 +2278,9 @@ rb_ary_fill(argc, argv, 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 95cc5a5..6e70dc5 100644
--- a/test/ruby/test_array.rb
+++ b/test/ruby/test_array.rb
@@ -108,4 +108,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