2015-12-16 08:07:31 +03:00
|
|
|
# frozen_string_literal: false
|
2008-09-03 17:57:21 +04:00
|
|
|
require 'test/unit'
|
|
|
|
require 'prime'
|
2009-10-18 04:55:34 +04:00
|
|
|
require 'timeout'
|
2008-09-03 17:57:21 +04:00
|
|
|
|
|
|
|
class TestPrime < Test::Unit::TestCase
|
|
|
|
# The first 100 prime numbers
|
|
|
|
PRIMES = [
|
|
|
|
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37,
|
2009-03-06 06:56:38 +03:00
|
|
|
41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83,
|
|
|
|
89, 97, 101, 103, 107, 109, 113, 127, 131,
|
|
|
|
137, 139, 149, 151, 157, 163, 167, 173, 179,
|
|
|
|
181, 191, 193, 197, 199, 211, 223, 227, 229,
|
|
|
|
233, 239, 241, 251, 257, 263, 269, 271, 277,
|
|
|
|
281, 283, 293, 307, 311, 313, 317, 331, 337,
|
|
|
|
347, 349, 353, 359, 367, 373, 379, 383, 389,
|
|
|
|
397, 401, 409, 419, 421, 431, 433, 439, 443,
|
|
|
|
449, 457, 461, 463, 467, 479, 487, 491, 499,
|
2008-09-03 17:57:21 +04:00
|
|
|
503, 509, 521, 523, 541,
|
|
|
|
]
|
|
|
|
def test_each
|
|
|
|
primes = []
|
|
|
|
Prime.each do |p|
|
|
|
|
break if p > 541
|
|
|
|
primes << p
|
|
|
|
end
|
|
|
|
assert_equal PRIMES, primes
|
|
|
|
end
|
|
|
|
|
2018-06-24 12:31:06 +03:00
|
|
|
def test_integer_each_prime
|
|
|
|
primes = []
|
|
|
|
Integer.each_prime(1000) do |p|
|
|
|
|
break if p > 541
|
|
|
|
primes << p
|
|
|
|
end
|
|
|
|
assert_equal PRIMES, primes
|
|
|
|
end
|
|
|
|
|
2008-09-03 17:57:21 +04:00
|
|
|
def test_each_by_prime_number_theorem
|
|
|
|
3.upto(15) do |i|
|
|
|
|
max = 2**i
|
|
|
|
primes = []
|
|
|
|
Prime.each do |p|
|
|
|
|
break if p >= max
|
|
|
|
primes << p
|
|
|
|
end
|
|
|
|
|
|
|
|
# Prime number theorem
|
2015-06-14 15:13:04 +03:00
|
|
|
assert_operator primes.length, :>=, max/Math.log(max)
|
2008-09-03 17:57:21 +04:00
|
|
|
delta = 0.05
|
|
|
|
li = (2..max).step(delta).inject(0){|sum,x| sum + delta/Math.log(x)}
|
2015-06-14 15:13:04 +03:00
|
|
|
assert_operator primes.length, :<=, li
|
2008-09-03 17:57:21 +04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_each_without_block
|
|
|
|
enum = Prime.each
|
2015-06-14 15:13:04 +03:00
|
|
|
assert_respond_to(enum, :each)
|
|
|
|
assert_kind_of(Enumerable, enum)
|
|
|
|
assert_respond_to(enum, :with_index)
|
|
|
|
assert_respond_to(enum, :next)
|
|
|
|
assert_respond_to(enum, :succ)
|
|
|
|
assert_respond_to(enum, :rewind)
|
2008-09-03 17:57:21 +04:00
|
|
|
end
|
|
|
|
|
2015-05-22 16:36:39 +03:00
|
|
|
def test_instance_without_block
|
|
|
|
enum = Prime.instance.each
|
2015-06-14 15:13:04 +03:00
|
|
|
assert_respond_to(enum, :each)
|
|
|
|
assert_kind_of(Enumerable, enum)
|
|
|
|
assert_respond_to(enum, :with_index)
|
|
|
|
assert_respond_to(enum, :next)
|
|
|
|
assert_respond_to(enum, :succ)
|
|
|
|
assert_respond_to(enum, :rewind)
|
2015-05-22 16:36:39 +03:00
|
|
|
end
|
2008-09-03 17:57:21 +04:00
|
|
|
|
2015-05-22 16:36:39 +03:00
|
|
|
def test_new
|
2019-06-29 15:19:01 +03:00
|
|
|
assert_raise(NoMethodError) { Prime.new }
|
2008-09-03 17:57:21 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_enumerator_succ
|
|
|
|
enum = Prime.each
|
|
|
|
assert_equal PRIMES[0, 50], 50.times.map{ enum.succ }
|
|
|
|
assert_equal PRIMES[50, 50], 50.times.map{ enum.succ }
|
|
|
|
enum.rewind
|
|
|
|
assert_equal PRIMES[0, 100], 100.times.map{ enum.succ }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_enumerator_with_index
|
|
|
|
enum = Prime.each
|
|
|
|
last = -1
|
|
|
|
enum.with_index do |p,i|
|
|
|
|
break if i >= 100
|
|
|
|
assert_equal last+1, i
|
|
|
|
assert_equal PRIMES[i], p
|
|
|
|
last = i
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-06-12 18:45:22 +03:00
|
|
|
def test_enumerator_with_index_with_offset
|
|
|
|
enum = Prime.each
|
|
|
|
last = 5-1
|
|
|
|
enum.with_index(5).each do |p,i|
|
|
|
|
break if i >= 100+5
|
|
|
|
assert_equal last+1, i
|
|
|
|
assert_equal PRIMES[i-5], p
|
|
|
|
last = i
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-06-12 18:45:44 +03:00
|
|
|
def test_enumerator_with_object
|
|
|
|
object = Object.new
|
|
|
|
enum = Prime.each
|
|
|
|
enum.with_object(object).each do |p, o|
|
|
|
|
assert_equal object, o
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-06-12 18:45:59 +03:00
|
|
|
def test_enumerator_size
|
|
|
|
enum = Prime.each
|
|
|
|
assert_equal Float::INFINITY, enum.size
|
|
|
|
assert_equal Float::INFINITY, enum.with_object(nil).size
|
|
|
|
assert_equal Float::INFINITY, enum.with_index(42).size
|
|
|
|
end
|
|
|
|
|
2009-03-03 18:57:15 +03:00
|
|
|
def test_default_instance_does_not_have_compatibility_methods
|
2015-06-14 15:13:04 +03:00
|
|
|
assert_not_respond_to(Prime.instance, :succ)
|
|
|
|
assert_not_respond_to(Prime.instance, :next)
|
2015-10-20 06:19:44 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_prime_each_basic_argument_checking
|
|
|
|
assert_raise(ArgumentError) { Prime.prime?(1,2) }
|
|
|
|
assert_raise(ArgumentError) { Prime.prime?(1.2) }
|
2009-03-03 18:57:15 +03:00
|
|
|
end
|
|
|
|
|
2018-06-24 12:31:06 +03:00
|
|
|
def test_prime?
|
|
|
|
assert_equal Prime.prime?(1), false
|
|
|
|
assert_equal Prime.prime?(2), true
|
|
|
|
assert_equal Prime.prime?(4), false
|
|
|
|
end
|
|
|
|
|
|
|
|
class TestPseudoPrimeGenerator < Test::Unit::TestCase
|
|
|
|
def test_upper_bound
|
|
|
|
pseudo_prime_generator = Prime::PseudoPrimeGenerator.new(42)
|
|
|
|
assert_equal pseudo_prime_generator.upper_bound, 42
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_succ
|
|
|
|
pseudo_prime_generator = Prime::PseudoPrimeGenerator.new(42)
|
|
|
|
assert_raise(NotImplementedError) { pseudo_prime_generator.succ }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_next
|
|
|
|
pseudo_prime_generator = Prime::PseudoPrimeGenerator.new(42)
|
|
|
|
assert_raise(NotImplementedError) { pseudo_prime_generator.next }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_rewind
|
|
|
|
pseudo_prime_generator = Prime::PseudoPrimeGenerator.new(42)
|
|
|
|
assert_raise(NotImplementedError) { pseudo_prime_generator.rewind }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class TestTrialDivisionGenerator < Test::Unit::TestCase
|
|
|
|
# The first 100 prime numbers
|
|
|
|
PRIMES = [
|
|
|
|
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37,
|
|
|
|
41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83,
|
|
|
|
89, 97, 101, 103, 107, 109, 113, 127, 131,
|
|
|
|
137, 139, 149, 151, 157, 163, 167, 173, 179,
|
|
|
|
181, 191, 193, 197, 199, 211, 223, 227, 229,
|
|
|
|
233, 239, 241, 251, 257, 263, 269, 271, 277,
|
|
|
|
281, 283, 293, 307, 311, 313, 317, 331, 337,
|
|
|
|
347, 349, 353, 359, 367, 373, 379, 383, 389,
|
|
|
|
397, 401, 409, 419, 421, 431, 433, 439, 443,
|
|
|
|
449, 457, 461, 463, 467, 479, 487, 491, 499,
|
|
|
|
503, 509, 521, 523, 541,
|
|
|
|
]
|
|
|
|
|
|
|
|
def test_each
|
|
|
|
primes = []
|
|
|
|
Prime.each(nil, Prime::TrialDivisionGenerator.new) do |p|
|
|
|
|
break if p > 541
|
|
|
|
primes << p
|
|
|
|
end
|
|
|
|
assert_equal PRIMES, primes
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_rewind
|
|
|
|
generator = Prime::TrialDivisionGenerator.new
|
|
|
|
assert_equal generator.next, 2
|
|
|
|
assert_equal generator.next, 3
|
|
|
|
generator.rewind
|
|
|
|
assert_equal generator.next, 2
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class TestGenerator23 < Test::Unit::TestCase
|
|
|
|
def test_rewind
|
|
|
|
generator = Prime::Generator23.new
|
|
|
|
assert_equal generator.next, 2
|
|
|
|
assert_equal generator.next, 3
|
|
|
|
generator.rewind
|
|
|
|
assert_equal generator.next, 2
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-09-03 17:57:21 +04:00
|
|
|
class TestInteger < Test::Unit::TestCase
|
|
|
|
def test_prime_division
|
|
|
|
pd = PRIMES.inject(&:*).prime_division
|
|
|
|
assert_equal PRIMES.map{|p| [p, 1]}, pd
|
2009-07-13 17:51:49 +04:00
|
|
|
|
|
|
|
pd = (-PRIMES.inject(&:*)).prime_division
|
|
|
|
assert_equal [-1, *PRIMES].map{|p| [p, 1]}, pd
|
2008-09-03 17:57:21 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_from_prime_division
|
|
|
|
assert_equal PRIMES.inject(&:*), Integer.from_prime_division(PRIMES.map{|p| [p,1]})
|
2009-07-13 17:51:49 +04:00
|
|
|
|
2010-01-26 02:12:50 +03:00
|
|
|
assert_equal(-PRIMES.inject(&:*), Integer.from_prime_division([[-1, 1]] + PRIMES.map{|p| [p,1]}))
|
2008-09-03 17:57:21 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_prime?
|
2016-08-10 21:17:41 +03:00
|
|
|
PRIMES.each do |p|
|
|
|
|
assert_predicate(p, :prime?)
|
|
|
|
end
|
2008-09-03 17:57:21 +04:00
|
|
|
|
2016-08-10 21:17:41 +03:00
|
|
|
composites = (0..PRIMES.last).to_a - PRIMES
|
|
|
|
composites.each do |c|
|
|
|
|
assert_not_predicate(c, :prime?)
|
|
|
|
end
|
2008-09-03 17:57:21 +04:00
|
|
|
|
|
|
|
# mersenne numbers
|
2015-06-14 15:13:04 +03:00
|
|
|
assert_predicate((2**31-1), :prime?)
|
|
|
|
assert_not_predicate((2**32-1), :prime?)
|
2008-09-03 17:57:21 +04:00
|
|
|
|
|
|
|
# fermat numbers
|
2015-06-14 15:13:04 +03:00
|
|
|
assert_predicate((2**(2**4)+1), :prime?)
|
|
|
|
assert_not_predicate((2**(2**5)+1), :prime?) # Euler!
|
2008-09-03 17:57:21 +04:00
|
|
|
|
|
|
|
# large composite
|
2015-06-14 15:13:04 +03:00
|
|
|
assert_not_predicate(((2**13-1) * (2**17-1)), :prime?)
|
2008-09-03 17:57:21 +04:00
|
|
|
|
|
|
|
# factorial
|
2015-06-14 15:13:04 +03:00
|
|
|
assert_not_predicate((2...100).inject(&:*), :prime?)
|
2009-03-03 18:57:15 +03:00
|
|
|
|
|
|
|
# negative
|
2015-06-14 15:13:04 +03:00
|
|
|
assert_not_predicate(-1, :prime?)
|
|
|
|
assert_not_predicate(-2, :prime?)
|
|
|
|
assert_not_predicate(-3, :prime?)
|
|
|
|
assert_not_predicate(-4, :prime?)
|
2008-09-03 17:57:21 +04:00
|
|
|
end
|
|
|
|
end
|
2009-10-18 04:55:34 +04:00
|
|
|
|
|
|
|
def test_eratosthenes_works_fine_after_timeout
|
|
|
|
sieve = Prime::EratosthenesSieve.instance
|
|
|
|
sieve.send(:initialize)
|
2017-05-20 03:36:55 +03:00
|
|
|
# simulates that Timeout.timeout interrupts Prime::EratosthenesSieve#compute_primes
|
|
|
|
class << Integer
|
|
|
|
alias_method :org_sqrt, :sqrt
|
|
|
|
end
|
2009-10-18 04:55:34 +04:00
|
|
|
begin
|
2017-05-20 03:36:55 +03:00
|
|
|
def Integer.sqrt(n)
|
2013-07-15 08:21:34 +04:00
|
|
|
sleep 10 if /compute_primes/ =~ caller.first
|
2017-05-20 03:36:55 +03:00
|
|
|
org_sqrt(n)
|
2009-10-18 04:55:34 +04:00
|
|
|
end
|
2015-06-25 08:26:31 +03:00
|
|
|
assert_raise(Timeout::Error) do
|
2015-06-25 08:14:18 +03:00
|
|
|
Timeout.timeout(0.5) { Prime.each(7*37){} }
|
2009-10-18 04:55:34 +04:00
|
|
|
end
|
|
|
|
ensure
|
2017-05-20 03:36:55 +03:00
|
|
|
class << Integer
|
|
|
|
alias_method :sqrt, :org_sqrt
|
2017-05-20 13:21:44 +03:00
|
|
|
remove_method :org_sqrt
|
2009-10-18 04:55:34 +04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-02-13 03:56:44 +03:00
|
|
|
assert_not_include Prime.each(7*37).to_a, 7*37, "[ruby-dev:39465]"
|
2009-10-18 04:55:34 +04:00
|
|
|
end
|
2008-09-03 17:57:21 +04:00
|
|
|
end
|