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

Move spec/rubyspec to spec/ruby for consistency

* Other ruby implementations use the spec/ruby directory.
  [Misc #13792] [ruby-core:82287]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59979 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
eregon 2017-09-20 20:18:52 +00:00
parent 75bfc6440d
commit 1d15d5f080
4370 changed files with 0 additions and 0 deletions

View file

@ -0,0 +1,167 @@
require File.expand_path('../../../spec_helper', __FILE__)
require 'prime'
describe :prime_each, shared: true do
before :each do
ScratchPad.record []
end
it "enumerates primes" do
primes = Prime.instance
result = []
primes.each { |p|
result << p
break if p > 10
}
result.should == [2, 3, 5, 7, 11]
end
it "yields ascending primes to the block" do
previous = 1
@object.each do |prime|
break if prime > 1000
ScratchPad << prime
prime.should > previous
previous = prime
end
all_prime = true
ScratchPad.recorded.all? do |prime|
all_prime &&= (2..Math.sqrt(prime)).all? { |d| prime % d != 0 }
end
all_prime.should be_true
end
it "returns the last evaluated expression in the passed block" do
@object.each { break :value }.should equal(:value)
end
describe "when not passed a block" do
before :each do
@prime_enum = @object.each
end
it "returns an object that is Enumerable" do
@prime_enum.each.should be_kind_of(Enumerable)
end
it "returns an object that responds to #with_index" do
@prime_enum.should respond_to(:with_index)
end
it "returns an object that responds to #with_object" do
@prime_enum.should respond_to(:with_object)
end
it "returns an object that responds to #next" do
@prime_enum.should respond_to(:next)
end
it "returns an object that responds to #rewind" do
@prime_enum.should respond_to(:rewind)
end
it "yields primes starting at 2 independent of prior enumerators" do
@prime_enum.next.should == 2
@prime_enum.next.should == 3
@object.each { |prime| break prime }.should == 2
end
it "returns an enumerator that yields previous primes when #rewind is called" do
@prime_enum.next.should == 2
@prime_enum.next.should == 3
@prime_enum.rewind
@prime_enum.next.should == 2
end
it "returns independent enumerators" do
enum = @object.each
enum.next.should == 2
enum.next.should == 3
@prime_enum.next.should == 2
enum.next.should == 5
end
end
end
describe :prime_each_with_arguments, shared: true do
before :each do
ScratchPad.record []
end
it "yields ascending primes less than or equal to the argument" do
bound = 1000
previous = 1
@object.each(bound) do |prime|
ScratchPad << prime
prime.should > previous
previous = prime
end
ScratchPad.recorded.all? do |prime|
(2..Math.sqrt(prime)).all? { |d| prime % d != 0 }
end.should be_true
ScratchPad.recorded.all? { |prime| prime <= bound }.should be_true
end
it "returns nil when no prime is generated" do
@object.each(1) { :value }.should be_nil
end
it "yields primes starting at 2 independent of prior enumeration" do
@object.each(10) { |prime| prime }.should == 7
@object.each(10) { |prime| break prime }.should == 2
end
it "accepts a pseudo-prime generator as the second argument" do
generator = mock('very bad pseudo-prime generator')
generator.should_receive(:upper_bound=).with(100)
generator.should_receive(:each).and_yield(2).and_yield(3).and_yield(4)
@object.each(100, generator) { |prime| ScratchPad << prime }
ScratchPad.recorded.should == [2, 3, 4]
end
describe "when not passed a block" do
it "returns an object that returns primes less than or equal to the bound" do
bound = 100
@object.each(bound).all? { |prime| prime <= bound }.should be_true
end
end
end
describe "Prime.each" do
it_behaves_like :prime_each, :each, Prime
end
describe "Prime.each" do
it_behaves_like :prime_each_with_arguments, :each, Prime
end
describe "Prime#each with Prime.instance" do
it_behaves_like :prime_each, :each, Prime.instance
end
describe "Prime#each with Prime.instance" do
it_behaves_like :prime_each_with_arguments, :each, Prime.instance
end
describe "Prime#each with Prime.instance" do
before :each do
@object = Prime.instance
end
it_behaves_like :prime_each, :each
it "resets the enumerator with each call" do
@object.each { |prime| break if prime > 10 }
@object.each { |prime| break prime }.should == 2
end
end

View file

@ -0,0 +1,21 @@
require File.expand_path('../../../spec_helper', __FILE__)
require 'prime'
describe "Prime.instance" do
it "returns a object representing the set of prime numbers" do
Prime.instance.should be_kind_of(Prime)
end
it "returns a object with no obsolete features" do
Prime.instance.should_not respond_to(:succ)
Prime.instance.should_not respond_to(:next)
end
it "does not complain anything" do
lambda { Prime.instance }.should_not complain
end
it "raises a ArgumentError when is called with some arguments" do
lambda { Prime.instance(1) }.should raise_error(ArgumentError)
end
end

View file

@ -0,0 +1,13 @@
require File.expand_path('../../../spec_helper', __FILE__)
require 'prime'
describe "Prime.int_from_prime_division" do
it "returns the product of the given factorization" do
Prime.int_from_prime_division([[2,3], [3,3], [5,3], [7,3], [11,3], [13,3], [17,3]]).
should == 2**3 * 3**3 * 5**3 * 7**3 * 11**3 * 13**3 * 17**3
end
it "returns 1 for an empty factorization" do
Prime.int_from_prime_division([]).should == 1
end
end

View file

@ -0,0 +1,13 @@
require File.expand_path('../../../../spec_helper', __FILE__)
require 'prime'
describe "Integer.each_prime" do
it "is transferred to Prime.each" do
Prime.should_receive(:each).with(100).and_yield(2).and_yield(3).and_yield(5)
yielded = []
Integer.each_prime(100) do |prime|
yielded << prime
end
yielded.should == [2,3,5]
end
end

View file

@ -0,0 +1,13 @@
require File.expand_path('../../../../spec_helper', __FILE__)
require 'prime'
describe "Integer.from_prime_division" do
it "returns the product of the given factorization" do
Integer.from_prime_division([[2,3], [3,3], [5,3], [7,3], [11,3], [13,3], [17,3]]).
should == 2**3 * 3**3 * 5**3 * 7**3 * 11**3 * 13**3 * 17**3
end
it "returns 1 for an empty factorization" do
Integer.from_prime_division([]).should == 1
end
end

View file

@ -0,0 +1,19 @@
require File.expand_path('../../../../spec_helper', __FILE__)
require 'prime'
describe "Integer#prime_division" do
it "returns an array of a prime factor and a corresponding exponent" do
(2*3*5*7*11*13*17).prime_division.should ==
[[2,1], [3,1], [5,1], [7,1], [11,1], [13,1], [17,1]]
end
it "returns an empty array for 1" do
1.prime_division.should == []
end
it "returns an empty array for -1" do
-1.prime_division.should == [[-1, 1]]
end
it "raises ZeroDivisionError for 0" do
lambda { 0.prime_division }.should raise_error(ZeroDivisionError)
end
end

View file

@ -0,0 +1,17 @@
require File.expand_path('../../../../spec_helper', __FILE__)
require 'prime'
describe "Integer#prime?" do
it "returns a true value for prime numbers" do
2.prime?.should be_true
3.prime?.should be_true
(2**31-1).prime?.should be_true # 8th Mersenne prime (M8)
end
it "returns a false value for composite numbers" do
4.prime?.should be_false
15.prime?.should be_false
(2**32-1).prime?.should be_false
( (2**17-1)*(2**19-1) ).prime?.should be_false # M6*M7
end
end

View file

@ -0,0 +1,7 @@
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../shared/next', __FILE__)
require 'prime'
describe "Prime#next" do
it_behaves_like :prime_next, :next
end

View file

@ -0,0 +1,25 @@
require File.expand_path('../../../spec_helper', __FILE__)
require 'prime'
describe "Prime.prime_division" do
it "returns an array of a prime factor and a corresponding exponent" do
Prime.prime_division(2*3*5*7*11*13*17).should ==
[[2,1], [3,1], [5,1], [7,1], [11,1], [13,1], [17,1]]
end
it "returns an empty array for 1" do
Prime.prime_division(1).should == []
end
it "returns [[-1, 1]] for -1" do
Prime.prime_division(-1).should == [[-1, 1]]
end
it "includes [[-1, 1]] in the divisors of a negative number" do
Prime.prime_division(-10).should include([-1, 1])
end
it "raises ZeroDivisionError for 0" do
lambda { Prime.prime_division(0) }.should raise_error(ZeroDivisionError)
end
end

View file

@ -0,0 +1,17 @@
require File.expand_path('../../../spec_helper', __FILE__)
require 'prime'
describe "Prime#prime?" do
it "returns a true value for prime numbers" do
Prime.prime?(2).should be_true
Prime.prime?(3).should be_true
Prime.prime?(2**31-1).should be_true # 8th Mersenne prime (M8)
end
it "returns a false value for composite numbers" do
Prime.prime?(4).should be_false
Prime.prime?(15).should be_false
Prime.prime?(2**32-1).should be_false
Prime.prime?( (2**17-1)*(2**19-1) ).should be_false # M6*M7
end
end

View file

@ -0,0 +1,8 @@
describe :prime_next, shared: true do
it "returns the element at the current position and moves forward" do
p = Prime.instance.each
p.next.should == 2
p.next.should == 3
p.next.next.should == 6
end
end

View file

@ -0,0 +1,7 @@
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../shared/next', __FILE__)
require 'prime'
describe "Prime#succ" do
it_behaves_like :prime_next, :succ
end