diff --git a/ChangeLog b/ChangeLog index a28f914860..c000924a55 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +Wed Mar 4 00:54:43 2009 Yuki Sonoda (Yugui) + + * lib/prime.rb (Prime::prime?): used to return a wrong answer. + [ruby-core:22646]. + + * test/test_prime.rb (test_prime?): test case for [ruby-core:22646]. + Tue Mar 3 17:10:09 2009 Nobuyoshi Nakada * ext/extmk.rb (extmake): removes object files no longer used and diff --git a/lib/prime.rb b/lib/prime.rb index 650d279bc4..ce71d5e00f 100644 --- a/lib/prime.rb +++ b/lib/prime.rb @@ -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) + value = -value if value < 0 + return false if value < 2 for num in generator q,r = value.divmod num return true if q < num diff --git a/test/test_prime.rb b/test/test_prime.rb index aca5109689..0dad7cf987 100644 --- a/test/test_prime.rb +++ b/test/test_prime.rb @@ -89,6 +89,11 @@ class TestPrime < Test::Unit::TestCase end end + def test_default_instance_does_not_have_compatibility_methods + assert !Prime.instance.respond_to?(:succ) + assert !Prime.instance.respond_to?(:next) + end + class TestInteger < Test::Unit::TestCase def test_prime_division pd = PRIMES.inject(&:*).prime_division @@ -100,6 +105,10 @@ class TestPrime < Test::Unit::TestCase end def test_prime? + # zero and unit + assert !0.prime? + assert !1.prime? + # small primes assert 2.prime? assert 3.prime? @@ -121,6 +130,12 @@ class TestPrime < Test::Unit::TestCase # factorial assert !(2...100).inject(&:*).prime? + + # negative + assert !-1.prime? + assert -2.prime? + assert -3.prime? + assert !-4.prime? end end end