ruby-changes:40120
From: marcandre <ko1@a...>
Date: Tue, 20 Oct 2015 12:20:01 +0900 (JST)
Subject: [ruby-changes:40120] marcandRe: r52201 (trunk): * lib/prime.rb: Add basic argument checking to Prime.prime?
marcandre 2015-10-20 12:19:44 +0900 (Tue, 20 Oct 2015) New Revision: 52201 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=52201 Log: * lib/prime.rb: Add basic argument checking to Prime.prime? [Bug #11606] Modified files: trunk/ChangeLog trunk/lib/prime.rb trunk/test/test_prime.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 52200) +++ ChangeLog (revision 52201) @@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Tue Oct 20 12:17:56 2015 Marc-Andre Lafortune <ruby-core@m...> + + * lib/prime.rb: Add basic argument checking to Prime.prime? + [Bug #11606] + Tue Oct 20 12:17:50 2015 Marc-Andre Lafortune <ruby-core@m...> * lib/prime.rb: Optimize Integer#prime? Index: lib/prime.rb =================================================================== --- lib/prime.rb (revision 52200) +++ lib/prime.rb (revision 52201) @@ -144,6 +144,8 @@ class Prime https://github.com/ruby/ruby/blob/trunk/lib/prime.rb#L144 # +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 Index: test/test_prime.rb =================================================================== --- test/test_prime.rb (revision 52200) +++ test/test_prime.rb (revision 52201) @@ -118,6 +118,11 @@ class TestPrime < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/test_prime.rb#L118 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 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/