diff options
author | akr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2007-07-19 17:04:53 +0000 |
---|---|---|
committer | akr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2007-07-19 17:04:53 +0000 |
commit | 863e7e95f2a811530df1f5bc8400d098c35cce45 (patch) | |
tree | fd593cc31c4b9a6a7d7bf89b88fc47711200f923 /test/ruby | |
parent | a1c76bc3de808ab3c5b36725369e952210ef8b64 (diff) | |
download | ruby-863e7e95f2a811530df1f5bc8400d098c35cce45.tar.gz |
refine tests for shift.
more tests for string to integer conversion.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12816 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'test/ruby')
-rw-r--r-- | test/ruby/test_integer.rb | 37 | ||||
-rw-r--r-- | test/ruby/test_string.rb | 26 |
2 files changed, 56 insertions, 7 deletions
diff --git a/test/ruby/test_integer.rb b/test/ruby/test_integer.rb index dd2be1e2ca..2744f3df42 100644 --- a/test/ruby/test_integer.rb +++ b/test/ruby/test_integer.rb @@ -107,6 +107,16 @@ class TestInteger < Test::Unit::TestCase assert_equal((a >> i).odd? ? 1 : 0, a[i], "(#{a})[#{i}]") } } + VS.each {|a| + VS.each {|b| + c = a[b] + if b < 0 + assert_equal(0, c, "(#{a})[#{b}]") + else + assert_equal((a >> b).odd? ? 1 : 0, c, "(#{a})[#{b}]") + end + } + } end def test_plus @@ -229,16 +239,17 @@ class TestInteger < Test::Unit::TestCase end def test_lshift - small_values = VS.find_all {|v| -1000 < v && v < 1000 } + small_values = VS.find_all {|v| v < 8000 } VS.each {|a| small_values.each {|b| c = a << b if 0 <= b assert_equal(a, c >> b, "(#{a} << #{b}) >> #{b}") assert_equal(a * 2**b, c, "#{a} << #{b}") - else - assert_equal(a / 2**(-b), c, "#{a} << #{b}") end + 0.upto(c.size*8+10) {|nth| + assert_equal(a[nth-b], c[nth], "(#{a} << #{b})[#{nth}]") + } } } assert_equal(0, 1 << -0x40000000) @@ -249,16 +260,17 @@ class TestInteger < Test::Unit::TestCase end def test_rshift - small_values = VS.find_all {|v| -1000 < v && v < 1000 } + small_values = VS.find_all {|v| -8000 < v } VS.each {|a| small_values.each {|b| c = a >> b - if 0 < b - assert_equal(a / 2**b, c, "#{a} >> #{b}") - else + if b <= 0 assert_equal(a, c << b, "(#{a} >> #{b}) << #{b}") assert_equal(a * 2**(-b), c, "#{a} >> #{b}") end + 0.upto(c.size*8+10) {|nth| + assert_equal(a[nth+b], c[nth], "(#{a} >> #{b})[#{nth}]") + } } } # assert_equal(bdsize(0x40000001), (1 >> -0x40000001).size) @@ -415,5 +427,16 @@ class TestInteger < Test::Unit::TestCase assert_nothing_raised(ArgumentError) { assert_equal(1540841, "0x0x5".to_i(36)) } + assert_raise(ArgumentError) { Integer("--0") } + assert_raise(ArgumentError) { Integer("-+0") } + assert_raise(ArgumentError) { Integer("++1") } + assert_raise(ArgumentError) { Integer("") } + assert_raise(ArgumentError) { Integer("10 x") } + assert_raise(ArgumentError) { Integer("1__2") } + assert_raise(ArgumentError) { Integer("1z") } + assert_raise(ArgumentError) { Integer("46116860184273__87904") } + assert_raise(ArgumentError) { Integer("4611686018427387904_") } + assert_raise(ArgumentError) { Integer("4611686018427387904 :") } + assert_equal(0x4000000000000000, Integer("46_11_686_0184273_87904")) end end diff --git a/test/ruby/test_string.rb b/test/ruby/test_string.rb index e715dfc5ec..6ed3100f3e 100644 --- a/test/ruby/test_string.rb +++ b/test/ruby/test_string.rb @@ -1145,6 +1145,32 @@ class TestString < Test::Unit::TestCase def test_to_i assert_equal(1480, S("1480ft/sec").to_i) assert_equal(0, S("speed of sound in water @20C = 1480ft/sec)").to_i) + assert_equal(0, " 0".to_i) + assert_equal(0, "+0".to_i) + assert_equal(0, "-0".to_i) + assert_equal(0, "--0".to_i) + assert_equal(16, "0x10".to_i(0)) + assert_equal(16, "0X10".to_i(0)) + assert_equal(2, "0b10".to_i(0)) + assert_equal(2, "0B10".to_i(0)) + assert_equal(8, "0o10".to_i(0)) + assert_equal(8, "0O10".to_i(0)) + assert_equal(10, "0d10".to_i(0)) + assert_equal(10, "0D10".to_i(0)) + assert_equal(8, "010".to_i(0)) + assert_raise(ArgumentError) { "010".to_i(-10) } + 2.upto(36) {|radix| + assert_equal(radix, "10".to_i(radix)) + assert_equal(radix**2, "100".to_i(radix)) + } + assert_raise(ArgumentError) { "0".to_i(1) } + assert_raise(ArgumentError) { "0".to_i(37) } + assert_equal(0, "z".to_i(10)) + assert_equal(12, "1_2".to_i(10)) + assert_equal(0x40000000, "1073741824".to_i(10)) + assert_equal(0x4000000000000000, "4611686018427387904".to_i(10)) + assert_equal(12, "1__2".to_i(10)) + assert_equal(1, "1_z".to_i(10)) end def test_to_s |