1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

lib/prime: Fix primality of some large integers [#13492].

* lib/prime.rb: Use accurate sqrt to insure all factors are tested.
  Patch by Marcus Stollsteimer.

* test/test_prime.rb: Adapt test for timeout

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@58809 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
marcandre 2017-05-20 00:36:55 +00:00
parent 825a1e939b
commit 68354c350a
2 changed files with 10 additions and 9 deletions

View file

@ -174,20 +174,21 @@ class TestPrime < Test::Unit::TestCase
def test_eratosthenes_works_fine_after_timeout
sieve = Prime::EratosthenesSieve.instance
sieve.send(:initialize)
# simulates that Timeout.timeout interrupts Prime::EratosthenesSieve#compute_primes
class << Integer
alias_method :org_sqrt, :sqrt
end
begin
# simulates that Timeout.timeout interrupts Prime::EratosthenesSieve#compute_primes
def sieve.Integer(n)
n = super(n)
def Integer.sqrt(n)
sleep 10 if /compute_primes/ =~ caller.first
return n
org_sqrt(n)
end
assert_raise(Timeout::Error) do
Timeout.timeout(0.5) { Prime.each(7*37){} }
end
ensure
class << sieve
remove_method :Integer
class << Integer
alias_method :sqrt, :org_sqrt
end
end