diff options
author | naruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2011-03-01 01:35:17 +0000 |
---|---|---|
committer | naruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2011-03-01 01:35:17 +0000 |
commit | b8b48f9d58619e55c33106f8dfa21fc70c63e7ad (patch) | |
tree | 49b2899415ce9c76c3fe118ac527a95e486d6fdf /test | |
parent | af4d75f9586b002e63bb36220bf98dabc0456c06 (diff) |
* string.c (rb_str_byteslice): Add String#byteslice. [ruby-core:35376]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@30991 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'test')
-rw-r--r-- | test/ruby/test_string.rb | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/test/ruby/test_string.rb b/test/ruby/test_string.rb index c5d3a53850..f18c8148e4 100644 --- a/test/ruby/test_string.rb +++ b/test/ruby/test_string.rb @@ -1944,4 +1944,33 @@ class TestString < Test::Unit::TestCase assert_equal(S("hello world"), a) assert_equal(S("hello "), b) end + + def b(str) + str.force_encoding(Encoding::ASCII_8BIT) + end + + def test_byteslice + assert_equal(b("h"), "hello".byteslice(0)) + assert_equal(nil, "hello".byteslice(5)) + assert_equal(b("o"), "hello".byteslice(-1)) + assert_equal(nil, "hello".byteslice(-6)) + + assert_equal(b(""), "hello".byteslice(0, 0)) + assert_equal(b("hello"), "hello".byteslice(0, 6)) + assert_equal(b("hello"), "hello".byteslice(0, 6)) + assert_equal(b(""), "hello".byteslice(5, 1)) + assert_equal(b("o"), "hello".byteslice(-1, 6)) + assert_equal(nil, "hello".byteslice(-6, 1)) + + assert_equal(b("h"), "hello".byteslice(0..0)) + assert_equal(b(""), "hello".byteslice(5..0)) + assert_equal(b("o"), "hello".byteslice(4..5)) + assert_equal(nil, "hello".byteslice(6..0)) + assert_equal(b(""), "hello".byteslice(-1..0)) + assert_equal(b("llo"), "hello".byteslice(-3..5)) + + assert_equal(b("\x81"), "\u3042".byteslice(1)) + assert_equal(b("\x81\x82"), "\u3042".byteslice(1, 2)) + assert_equal(b("\x81\x82"), "\u3042".byteslice(1..2)) + end end |