diff options
author | Yusuke Endoh <mame@ruby-lang.org> | 2019-04-28 23:24:09 +0900 |
---|---|---|
committer | Yusuke Endoh <mame@ruby-lang.org> | 2019-04-28 23:40:57 +0900 |
commit | 6bedbf462544a7917fdc8d8c44276079a6e156cf (patch) | |
tree | f1adf82ecab59b605930a6321b119a80f2e85d72 /test/ruby/test_integer.rb | |
parent | cb550246136b90a63b4f75f5e7cfaccb9da08eda (diff) | |
download | ruby-6bedbf462544a7917fdc8d8c44276079a6e156cf.tar.gz |
numeric.c: Extend Integer#[] to support range arguments
````
0b01001101[2, 4] #=> 0b0011
0b01001100[2..5] #=> 0b0011
0b01001100[2...6] #=> 0b0011
^^^^
````
[Feature #8842]
Diffstat (limited to 'test/ruby/test_integer.rb')
-rw-r--r-- | test/ruby/test_integer.rb | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/test/ruby/test_integer.rb b/test/ruby/test_integer.rb index ad088aa72f..2334cab50a 100644 --- a/test/ruby/test_integer.rb +++ b/test/ruby/test_integer.rb @@ -23,6 +23,31 @@ class TestInteger < Test::Unit::TestCase (-64..64).each do |idx| assert_equal((n >> idx) & 1, n[idx]) end + [*-66..-62, *-34..-30, *-5..5, *30..34, *62..66].each do |idx| + (0..100).each do |len| + assert_equal((n >> idx) & ((1 << len) - 1), n[idx, len], "#{ n }[#{ idx }, #{ len }]") + end + (0..100).each do |len| + assert_equal((n >> idx) & ((1 << (len + 1)) - 1), n[idx..idx+len], "#{ n }[#{ idx }..#{ idx+len }]") + assert_equal((n >> idx) & ((1 << len) - 1), n[idx...idx+len], "#{ n }[#{ idx }...#{ idx+len }]") + end + + # endless + assert_equal((n >> idx), n[idx..], "#{ n }[#{ idx }..]") + assert_equal((n >> idx), n[idx...], "#{ n }[#{ idx }...#]") + + # beginless + if idx >= 0 && n & ((1 << (idx + 1)) - 1) != 0 + assert_raise(ArgumentError, "#{ n }[..#{ idx }]") { n[..idx] } + else + assert_equal(0, n[..idx], "#{ n }[..#{ idx }]") + end + if idx >= 0 && n & ((1 << idx) - 1) != 0 + assert_raise(ArgumentError, "#{ n }[...#{ idx }]") { n[...idx] } + else + assert_equal(0, n[...idx], "#{ n }[...#{ idx }]") + end + end end # assert_equal(1, (1 << 0x40000000)[0x40000000], "[ruby-dev:31271]") |