summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoryugui <yugui@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2009-03-03 15:57:15 +0000
committeryugui <yugui@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2009-03-03 15:57:15 +0000
commit294f45b0fc23b91bf5291fd2936f2076f375e467 (patch)
tree2abb7f737521889c80d5c6230eaede1bd353211f
parent27cc677088c21785815b1c137ec8e65e51c2dc83 (diff)
* 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]. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@22741 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog7
-rw-r--r--lib/prime.rb2
-rw-r--r--test/test_prime.rb15
3 files changed, 24 insertions, 0 deletions
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) <yugui@yugui.jp>
+
+ * 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 <nobu@ruby-lang.org>
* 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