diff options
author | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2017-02-24 08:36:16 +0000 |
---|---|---|
committer | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2017-02-24 08:36:16 +0000 |
commit | bdd6b995f91277a258db8614371be08e3fe9988d (patch) | |
tree | 9bd730f01373ca5ebf32160f2da0382121bde394 /test/ruby/test_integer.rb | |
parent | 395ad27e7235bf32d89e747507f7f1518d5aa844 (diff) | |
download | ruby-bdd6b995f91277a258db8614371be08e3fe9988d.tar.gz |
Integer.sqrt [Feature #13219]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@57705 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'test/ruby/test_integer.rb')
-rw-r--r-- | test/ruby/test_integer.rb | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/test/ruby/test_integer.rb b/test/ruby/test_integer.rb index 0a6b2f4539..05b0b68ccc 100644 --- a/test/ruby/test_integer.rb +++ b/test/ruby/test_integer.rb @@ -464,4 +464,28 @@ class TestInteger < Test::Unit::TestCase end assert_equal([0, 1], 10.digits(o)) end + + def test_square_root + assert_raise(Math::DomainError) {Integer.sqrt(-1)} + assert_equal(0, Integer.sqrt(0)) + (1...4).each {|i| assert_equal(1, Integer.sqrt(i))} + (4...9).each {|i| assert_equal(2, Integer.sqrt(i))} + (9...16).each {|i| assert_equal(3, Integer.sqrt(i))} + (1..40).each do |i| + mesg = "10**#{i}" + s = Integer.sqrt(n = 10**i) + if i.even? + assert_equal(10**(i/2), Integer.sqrt(n), mesg) + else + assert_include((s**2)...(s+1)**2, n, mesg) + end + end + 50.step(400, 10) do |i| + exact = 10**(i/2) + x = 10**i + assert_equal(exact, Integer.sqrt(x), "10**#{i}") + assert_equal(exact, Integer.sqrt(x+1), "10**#{i}+1") + assert_equal(exact-1, Integer.sqrt(x-1), "10**#{i}-1") + end + end end |