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

* lib/prime.rb: Add basic argument checking to Prime.prime?

[Bug #11606]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@52201 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
marcandre 2015-10-20 03:19:44 +00:00
parent 8744d5164d
commit 487736646a
3 changed files with 12 additions and 0 deletions

View file

@ -1,3 +1,8 @@
Tue Oct 20 12:17:56 2015 Marc-Andre Lafortune <ruby-core@marc-andre.ca>
* lib/prime.rb: Add basic argument checking to Prime.prime?
[Bug #11606]
Tue Oct 20 12:17:50 2015 Marc-Andre Lafortune <ruby-core@marc-andre.ca>
* lib/prime.rb: Optimize Integer#prime?

View file

@ -144,6 +144,8 @@ class Prime
# +value+:: an arbitrary integer to be checked.
# +generator+:: optional. A pseudo-prime generator.
def prime?(value, generator = Prime::Generator23.new)
raise ArgumentError, "Expected a prime generator, got #{generator}" unless generator.respond_to? :each
raise ArgumentError, "Expected an integer, got #{value}" unless value.respond_to?(:integer?) && value.integer?
return false if value < 2
generator.each do |num|
q,r = value.divmod num

View file

@ -118,6 +118,11 @@ class TestPrime < Test::Unit::TestCase
assert_not_respond_to(Prime.instance, :next)
end
def test_prime_each_basic_argument_checking
assert_raise(ArgumentError) { Prime.prime?(1,2) }
assert_raise(ArgumentError) { Prime.prime?(1.2) }
end
class TestInteger < Test::Unit::TestCase
def test_prime_division
pd = PRIMES.inject(&:*).prime_division