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:
parent
75bfc6440d
commit
1d15d5f080
4370 changed files with 0 additions and 0 deletions
103
spec/ruby/shared/rational/Rational.rb
Normal file
103
spec/ruby/shared/rational/Rational.rb
Normal file
|
@ -0,0 +1,103 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
require File.expand_path('../../../fixtures/rational', __FILE__)
|
||||
|
||||
describe :kernel_Rational, shared: true do
|
||||
describe "passed Integer" do
|
||||
# Guard against the Mathn library
|
||||
conflicts_with :Prime do
|
||||
it "returns a new Rational number with 1 as the denominator" do
|
||||
Rational(1).should eql(Rational(1, 1))
|
||||
Rational(-3).should eql(Rational(-3, 1))
|
||||
Rational(bignum_value).should eql(Rational(bignum_value, 1))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "passed two integers" do
|
||||
it "returns a new Rational number" do
|
||||
rat = Rational(1, 2)
|
||||
rat.numerator.should == 1
|
||||
rat.denominator.should == 2
|
||||
rat.should be_an_instance_of(Rational)
|
||||
|
||||
rat = Rational(-3, -5)
|
||||
rat.numerator.should == 3
|
||||
rat.denominator.should == 5
|
||||
rat.should be_an_instance_of(Rational)
|
||||
|
||||
rat = Rational(bignum_value, 3)
|
||||
rat.numerator.should == bignum_value
|
||||
rat.denominator.should == 3
|
||||
rat.should be_an_instance_of(Rational)
|
||||
end
|
||||
|
||||
it "reduces the Rational" do
|
||||
rat = Rational(2, 4)
|
||||
rat.numerator.should == 1
|
||||
rat.denominator.should == 2
|
||||
|
||||
rat = Rational(3, 9)
|
||||
rat.numerator.should == 1
|
||||
rat.denominator.should == 3
|
||||
end
|
||||
end
|
||||
|
||||
describe "when passed a String" do
|
||||
it "converts the String to a Rational using the same method as String#to_r" do
|
||||
r = Rational(13, 25)
|
||||
s_r = ".52".to_r
|
||||
r_s = Rational(".52")
|
||||
|
||||
r_s.should == r
|
||||
r_s.should == s_r
|
||||
end
|
||||
|
||||
it "scales the Rational value of the first argument by the Rational value of the second" do
|
||||
Rational(".52", ".6").should == Rational(13, 15)
|
||||
Rational(".52", "1.6").should == Rational(13, 40)
|
||||
end
|
||||
|
||||
it "does not use the same method as Float#to_r" do
|
||||
r = Rational(3, 5)
|
||||
f_r = 0.6.to_r
|
||||
r_s = Rational("0.6")
|
||||
|
||||
r_s.should == r
|
||||
r_s.should_not == f_r
|
||||
end
|
||||
|
||||
describe "when passed a Numeric" do
|
||||
it "calls #to_r to convert the first argument to a Rational" do
|
||||
num = RationalSpecs::SubNumeric.new(2)
|
||||
|
||||
Rational(num).should == Rational(2)
|
||||
end
|
||||
end
|
||||
|
||||
describe "when passed a Complex" do
|
||||
it "returns a Rational from the real part if the imaginary part is 0" do
|
||||
Rational(Complex(1, 0)).should == Rational(1)
|
||||
end
|
||||
|
||||
it "raises a RangeError if the imaginary part is not 0" do
|
||||
lambda { Rational(Complex(1, 2)) }.should raise_error(RangeError)
|
||||
end
|
||||
end
|
||||
|
||||
it "raises a TypeError if the first argument is nil" do
|
||||
lambda { Rational(nil) }.should raise_error(TypeError)
|
||||
end
|
||||
|
||||
it "raises a TypeError if the second argument is nil" do
|
||||
lambda { Rational(1, nil) }.should raise_error(TypeError)
|
||||
end
|
||||
|
||||
it "raises a TypeError if the first argument is a Symbol" do
|
||||
lambda { Rational(:sym) }.should raise_error(TypeError)
|
||||
end
|
||||
|
||||
it "raises a TypeError if the second argument is a Symbol" do
|
||||
lambda { Rational(1, :sym) }.should raise_error(TypeError)
|
||||
end
|
||||
end
|
||||
end
|
11
spec/ruby/shared/rational/abs.rb
Normal file
11
spec/ruby/shared/rational/abs.rb
Normal file
|
@ -0,0 +1,11 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
|
||||
describe :rational_abs, shared: true do
|
||||
it "returns self's absolute value" do
|
||||
Rational(3, 4).send(@method).should == Rational(3, 4)
|
||||
Rational(-3, 4).send(@method).should == Rational(3, 4)
|
||||
Rational(3, -4).send(@method).should == Rational(3, 4)
|
||||
|
||||
Rational(bignum_value, -bignum_value).send(@method).should == Rational(bignum_value, bignum_value)
|
||||
end
|
||||
end
|
45
spec/ruby/shared/rational/ceil.rb
Normal file
45
spec/ruby/shared/rational/ceil.rb
Normal file
|
@ -0,0 +1,45 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
|
||||
describe :rational_ceil, shared: true do
|
||||
before do
|
||||
@rational = Rational(2200, 7)
|
||||
end
|
||||
|
||||
describe "with no arguments (precision = 0)" do
|
||||
it "returns an Integer" do
|
||||
@rational.ceil.should be_kind_of(Integer)
|
||||
end
|
||||
|
||||
it "returns the truncated value toward positive infinity" do
|
||||
@rational.ceil.should == 315
|
||||
Rational(1, 2).ceil.should == 1
|
||||
Rational(-1, 2).ceil.should == 0
|
||||
end
|
||||
end
|
||||
|
||||
describe "with a precision < 0" do
|
||||
it "returns an Integer" do
|
||||
@rational.ceil(-2).should be_kind_of(Integer)
|
||||
@rational.ceil(-1).should be_kind_of(Integer)
|
||||
end
|
||||
|
||||
it "moves the truncation point n decimal places left" do
|
||||
@rational.ceil(-3).should == 1000
|
||||
@rational.ceil(-2).should == 400
|
||||
@rational.ceil(-1).should == 320
|
||||
end
|
||||
end
|
||||
|
||||
describe "with precision > 0" do
|
||||
it "returns a Rational" do
|
||||
@rational.ceil(1).should be_kind_of(Rational)
|
||||
@rational.ceil(2).should be_kind_of(Rational)
|
||||
end
|
||||
|
||||
it "moves the truncation point n decimal places right" do
|
||||
@rational.ceil(1).should == Rational(3143, 10)
|
||||
@rational.ceil(2).should == Rational(31429, 100)
|
||||
@rational.ceil(3).should == Rational(157143, 500)
|
||||
end
|
||||
end
|
||||
end
|
21
spec/ruby/shared/rational/coerce.rb
Normal file
21
spec/ruby/shared/rational/coerce.rb
Normal file
|
@ -0,0 +1,21 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
|
||||
describe :rational_coerce, shared: true do
|
||||
it "returns the passed argument, self as Float, when given a Float" do
|
||||
result = Rational(3, 4).coerce(1.0)
|
||||
result.should == [1.0, 0.75]
|
||||
result.first.is_a?(Float).should be_true
|
||||
result.last.is_a?(Float).should be_true
|
||||
end
|
||||
|
||||
it "returns the passed argument, self as Rational, when given an Integer" do
|
||||
result = Rational(3, 4).coerce(10)
|
||||
result.should == [Rational(10, 1), Rational(3, 4)]
|
||||
result.first.is_a?(Rational).should be_true
|
||||
result.last.is_a?(Rational).should be_true
|
||||
end
|
||||
|
||||
it "returns [argument, self] when given a Rational" do
|
||||
Rational(3, 7).coerce(Rational(9, 2)).should == [Rational(9, 2), Rational(3, 7)]
|
||||
end
|
||||
end
|
85
spec/ruby/shared/rational/comparison.rb
Normal file
85
spec/ruby/shared/rational/comparison.rb
Normal file
|
@ -0,0 +1,85 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
|
||||
describe :rational_cmp_rat, shared: true do
|
||||
it "returns 1 when self is greater than the passed argument" do
|
||||
(Rational(4, 4) <=> Rational(3, 4)).should equal(1)
|
||||
(Rational(-3, 4) <=> Rational(-4, 4)).should equal(1)
|
||||
end
|
||||
|
||||
it "returns 0 when self is equal to the passed argument" do
|
||||
(Rational(4, 4) <=> Rational(4, 4)).should equal(0)
|
||||
(Rational(-3, 4) <=> Rational(-3, 4)).should equal(0)
|
||||
end
|
||||
|
||||
it "returns -1 when self is less than the passed argument" do
|
||||
(Rational(3, 4) <=> Rational(4, 4)).should equal(-1)
|
||||
(Rational(-4, 4) <=> Rational(-3, 4)).should equal(-1)
|
||||
end
|
||||
end
|
||||
|
||||
describe :rational_cmp_int, shared: true do
|
||||
it "returns 1 when self is greater than the passed argument" do
|
||||
(Rational(4, 4) <=> 0).should equal(1)
|
||||
(Rational(4, 4) <=> -10).should equal(1)
|
||||
(Rational(-3, 4) <=> -1).should equal(1)
|
||||
end
|
||||
|
||||
it "returns 0 when self is equal to the passed argument" do
|
||||
(Rational(4, 4) <=> 1).should equal(0)
|
||||
(Rational(-8, 4) <=> -2).should equal(0)
|
||||
end
|
||||
|
||||
it "returns -1 when self is less than the passed argument" do
|
||||
(Rational(3, 4) <=> 1).should equal(-1)
|
||||
(Rational(-4, 4) <=> 0).should equal(-1)
|
||||
end
|
||||
end
|
||||
|
||||
describe :rational_cmp_float, shared: true do
|
||||
it "returns 1 when self is greater than the passed argument" do
|
||||
(Rational(4, 4) <=> 0.5).should equal(1)
|
||||
(Rational(4, 4) <=> -1.5).should equal(1)
|
||||
(Rational(-3, 4) <=> -0.8).should equal(1)
|
||||
end
|
||||
|
||||
it "returns 0 when self is equal to the passed argument" do
|
||||
(Rational(4, 4) <=> 1.0).should equal(0)
|
||||
(Rational(-6, 4) <=> -1.5).should equal(0)
|
||||
end
|
||||
|
||||
it "returns -1 when self is less than the passed argument" do
|
||||
(Rational(3, 4) <=> 1.2).should equal(-1)
|
||||
(Rational(-4, 4) <=> 0.5).should equal(-1)
|
||||
end
|
||||
end
|
||||
|
||||
describe :rational_cmp_coerce, shared: true do
|
||||
it "calls #coerce on the passed argument with self" do
|
||||
rational = Rational(3, 4)
|
||||
|
||||
obj = mock("Object")
|
||||
obj.should_receive(:coerce).with(rational).and_return([1, 2])
|
||||
|
||||
rational <=> obj
|
||||
end
|
||||
|
||||
it "calls #<=> on the coerced Rational with the coerced Object" do
|
||||
rational = Rational(3, 4)
|
||||
|
||||
coerced_rational = mock("Coerced Rational")
|
||||
coerced_rational.should_receive(:<=>).and_return(:result)
|
||||
|
||||
coerced_obj = mock("Coerced Object")
|
||||
|
||||
obj = mock("Object")
|
||||
obj.should_receive(:coerce).and_return([coerced_rational, coerced_obj])
|
||||
|
||||
(rational <=> obj).should == :result
|
||||
end
|
||||
end
|
||||
|
||||
describe :rational_cmp_other, shared: true do
|
||||
it "returns nil" do
|
||||
(Rational <=> mock("Object")).should be_nil
|
||||
end
|
||||
end
|
14
spec/ruby/shared/rational/denominator.rb
Normal file
14
spec/ruby/shared/rational/denominator.rb
Normal file
|
@ -0,0 +1,14 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
|
||||
describe :rational_denominator, shared: true do
|
||||
it "returns the denominator" do
|
||||
Rational(3, 4).denominator.should equal(4)
|
||||
Rational(3, -4).denominator.should equal(4)
|
||||
|
||||
Rational(1, bignum_value).denominator.should == bignum_value
|
||||
end
|
||||
|
||||
it "returns 1 if no denominator was given" do
|
||||
Rational(80).denominator.should == 1
|
||||
end
|
||||
end
|
54
spec/ruby/shared/rational/div.rb
Normal file
54
spec/ruby/shared/rational/div.rb
Normal file
|
@ -0,0 +1,54 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
|
||||
describe :rational_div_rat, shared: true do
|
||||
it "performs integer division and returns the result" do
|
||||
Rational(2, 3).div(Rational(2, 3)).should == 1
|
||||
Rational(-2, 9).div(Rational(-9, 2)).should == 0
|
||||
end
|
||||
|
||||
it "raises a ZeroDivisionError when the argument has a numerator of 0" do
|
||||
lambda { Rational(3, 4).div(Rational(0, 3)) }.should raise_error(ZeroDivisionError)
|
||||
end
|
||||
|
||||
it "raises a ZeroDivisionError when the argument has a numerator of 0.0" do
|
||||
lambda { Rational(3, 4).div(Rational(0.0, 3)) }.should raise_error(ZeroDivisionError)
|
||||
end
|
||||
end
|
||||
|
||||
describe :rational_div_float, shared: true do
|
||||
it "performs integer division and returns the result" do
|
||||
Rational(2, 3).div(30.333).should == 0
|
||||
Rational(2, 9).div(Rational(-8.6)).should == -1
|
||||
Rational(3.12).div(0.5).should == 6
|
||||
end
|
||||
|
||||
it "raises a ZeroDivisionError when the argument is 0.0" do
|
||||
lambda { Rational(3, 4).div(0.0) }.should raise_error(ZeroDivisionError)
|
||||
end
|
||||
end
|
||||
|
||||
describe :rational_div_int, shared: true do
|
||||
it "performs integer division and returns the result" do
|
||||
Rational(2, 1).div(1).should == 2
|
||||
Rational(25, 5).div(-50).should == -1
|
||||
end
|
||||
|
||||
it "raises a ZeroDivisionError when the argument is 0" do
|
||||
lambda { Rational(3, 4).div(0) }.should raise_error(ZeroDivisionError)
|
||||
end
|
||||
end
|
||||
|
||||
describe :rational_div, shared: true do
|
||||
it "returns an Integer" do
|
||||
Rational(229, 21).div(82).should be_kind_of(Integer)
|
||||
end
|
||||
|
||||
it "raises an ArgumentError if passed more than one argument" do
|
||||
lambda { Rational(3, 4).div(2,3) }.should raise_error(ArgumentError)
|
||||
end
|
||||
|
||||
# See http://redmine.ruby-lang.org/issues/show/1648
|
||||
it "raises a TypeError if passed a non-numeric argument" do
|
||||
lambda { Rational(3, 4).div([]) }.should raise_error(TypeError)
|
||||
end
|
||||
end
|
71
spec/ruby/shared/rational/divide.rb
Normal file
71
spec/ruby/shared/rational/divide.rb
Normal file
|
@ -0,0 +1,71 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
|
||||
describe :rational_divide_rat, shared: true do
|
||||
it "returns self divided by other as a Rational" do
|
||||
Rational(3, 4).send(@method, Rational(3, 4)).should eql(Rational(1, 1))
|
||||
Rational(2, 4).send(@method, Rational(1, 4)).should eql(Rational(2, 1))
|
||||
|
||||
Rational(2, 4).send(@method, 2).should == Rational(1, 4)
|
||||
Rational(6, 7).send(@method, -2).should == Rational(-3, 7)
|
||||
end
|
||||
|
||||
it "raises a ZeroDivisionError when passed a Rational with a numerator of 0" do
|
||||
lambda { Rational(3, 4).send(@method, Rational(0, 1)) }.should raise_error(ZeroDivisionError)
|
||||
end
|
||||
end
|
||||
|
||||
describe :rational_divide_int, shared: true do
|
||||
it "returns self divided by other as a Rational" do
|
||||
Rational(3, 4).send(@method, 2).should eql(Rational(3, 8))
|
||||
Rational(2, 4).send(@method, 2).should eql(Rational(1, 4))
|
||||
Rational(6, 7).send(@method, -2).should eql(Rational(-3, 7))
|
||||
end
|
||||
|
||||
it "raises a ZeroDivisionError when passed 0" do
|
||||
lambda { Rational(3, 4).send(@method, 0) }.should raise_error(ZeroDivisionError)
|
||||
end
|
||||
end
|
||||
|
||||
describe :rational_divide_float, shared: true do
|
||||
it "returns self divided by other as a Float" do
|
||||
Rational(3, 4).send(@method, 0.75).should eql(1.0)
|
||||
Rational(3, 4).send(@method, 0.25).should eql(3.0)
|
||||
Rational(3, 4).send(@method, 0.3).should eql(2.5)
|
||||
|
||||
Rational(-3, 4).send(@method, 0.3).should eql(-2.5)
|
||||
Rational(3, -4).send(@method, 0.3).should eql(-2.5)
|
||||
Rational(3, 4).send(@method, -0.3).should eql(-2.5)
|
||||
end
|
||||
|
||||
it "returns infinity when passed 0" do
|
||||
Rational(3, 4).send(@method, 0.0).infinite?.should eql(1)
|
||||
Rational(-3, -4).send(@method, 0.0).infinite?.should eql(1)
|
||||
|
||||
Rational(-3, 4).send(@method, 0.0).infinite?.should eql(-1)
|
||||
Rational(3, -4).send(@method, 0.0).infinite?.should eql(-1)
|
||||
end
|
||||
end
|
||||
|
||||
describe :rational_divide, shared: true do
|
||||
it "calls #coerce on the passed argument with self" do
|
||||
rational = Rational(3, 4)
|
||||
obj = mock("Object")
|
||||
obj.should_receive(:coerce).with(rational).and_return([1, 2])
|
||||
|
||||
rational.send(@method, obj)
|
||||
end
|
||||
|
||||
it "calls #/ on the coerced Rational with the coerced Object" do
|
||||
rational = Rational(3, 4)
|
||||
|
||||
coerced_rational = mock("Coerced Rational")
|
||||
coerced_rational.should_receive(:/).and_return(:result)
|
||||
|
||||
coerced_obj = mock("Coerced Object")
|
||||
|
||||
obj = mock("Object")
|
||||
obj.should_receive(:coerce).and_return([coerced_rational, coerced_obj])
|
||||
|
||||
rational.send(@method, obj).should == :result
|
||||
end
|
||||
end
|
42
spec/ruby/shared/rational/divmod.rb
Normal file
42
spec/ruby/shared/rational/divmod.rb
Normal file
|
@ -0,0 +1,42 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
|
||||
describe :rational_divmod_rat, shared: true do
|
||||
it "returns the quotient as Integer and the remainder as Rational" do
|
||||
Rational(7, 4).divmod(Rational(1, 2)).should eql([3, Rational(1, 4)])
|
||||
Rational(7, 4).divmod(Rational(-1, 2)).should eql([-4, Rational(-1, 4)])
|
||||
Rational(0, 4).divmod(Rational(4, 3)).should eql([0, Rational(0, 1)])
|
||||
|
||||
Rational(bignum_value, 4).divmod(Rational(4, 3)).should eql([1729382256910270464, Rational(0, 1)])
|
||||
end
|
||||
|
||||
it "raises a ZeroDivisonError when passed a Rational with a numerator of 0" do
|
||||
lambda { Rational(7, 4).divmod(Rational(0, 3)) }.should raise_error(ZeroDivisionError)
|
||||
end
|
||||
end
|
||||
|
||||
describe :rational_divmod_int, shared: true do
|
||||
it "returns the quotient as Integer and the remainder as Rational" do
|
||||
Rational(7, 4).divmod(2).should eql([0, Rational(7, 4)])
|
||||
Rational(7, 4).divmod(-2).should eql([-1, Rational(-1, 4)])
|
||||
|
||||
Rational(bignum_value, 4).divmod(3).should == [768614336404564650, Rational(2, 1)]
|
||||
end
|
||||
|
||||
it "raises a ZeroDivisionError when passed 0" do
|
||||
lambda { Rational(7, 4).divmod(0) }.should raise_error(ZeroDivisionError)
|
||||
end
|
||||
end
|
||||
|
||||
describe :rational_divmod_float, shared: true do
|
||||
it "returns the quotient as Integer and the remainder as Float" do
|
||||
Rational(7, 4).divmod(0.5).should eql([3, 0.25])
|
||||
end
|
||||
|
||||
it "returns the quotient as Integer and the remainder as Float" do
|
||||
Rational(7, 4).divmod(-0.5).should eql([-4, -0.25])
|
||||
end
|
||||
|
||||
it "raises a ZeroDivisionError when passed 0" do
|
||||
lambda { Rational(7, 4).divmod(0.0) }.should raise_error(ZeroDivisionError)
|
||||
end
|
||||
end
|
39
spec/ruby/shared/rational/equal_value.rb
Normal file
39
spec/ruby/shared/rational/equal_value.rb
Normal file
|
@ -0,0 +1,39 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
|
||||
describe :rational_equal_value_rat, shared: true do
|
||||
it "returns true if self has the same numerator and denominator as the passed argument" do
|
||||
(Rational(3, 4) == Rational(3, 4)).should be_true
|
||||
(Rational(-3, -4) == Rational(3, 4)).should be_true
|
||||
(Rational(-4, 5) == Rational(4, -5)).should be_true
|
||||
|
||||
(Rational(bignum_value, 3) == Rational(bignum_value, 3)).should be_true
|
||||
(Rational(-bignum_value, 3) == Rational(bignum_value, -3)).should be_true
|
||||
end
|
||||
end
|
||||
|
||||
describe :rational_equal_value_int, shared: true do
|
||||
it "returns true if self has the passed argument as numerator and a denominator of 1" do
|
||||
# Rational(x, y) reduces x and y automatically
|
||||
(Rational(4, 2) == 2).should be_true
|
||||
(Rational(-4, 2) == -2).should be_true
|
||||
(Rational(4, -2) == -2).should be_true
|
||||
end
|
||||
end
|
||||
|
||||
describe :rational_equal_value_float, shared: true do
|
||||
it "converts self to a Float and compares it with the passed argument" do
|
||||
(Rational(3, 4) == 0.75).should be_true
|
||||
(Rational(4, 2) == 2.0).should be_true
|
||||
(Rational(-4, 2) == -2.0).should be_true
|
||||
(Rational(4, -2) == -2.0).should be_true
|
||||
end
|
||||
end
|
||||
|
||||
describe :rational_equal_value, shared: true do
|
||||
it "returns the result of calling #== with self on the passed argument" do
|
||||
obj = mock("Object")
|
||||
obj.should_receive(:==).and_return(:result)
|
||||
|
||||
(Rational(3, 4) == obj).should_not be_false
|
||||
end
|
||||
end
|
176
spec/ruby/shared/rational/exponent.rb
Normal file
176
spec/ruby/shared/rational/exponent.rb
Normal file
|
@ -0,0 +1,176 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
|
||||
describe :rational_exponent, shared: true do
|
||||
describe "when passed Rational" do
|
||||
conflicts_with :Prime do
|
||||
it "returns Rational(1) if the exponent is Rational(0)" do
|
||||
(Rational(0) ** Rational(0)).should eql(Rational(1))
|
||||
(Rational(1) ** Rational(0)).should eql(Rational(1))
|
||||
(Rational(3, 4) ** Rational(0)).should eql(Rational(1))
|
||||
(Rational(-1) ** Rational(0)).should eql(Rational(1))
|
||||
(Rational(-3, 4) ** Rational(0)).should eql(Rational(1))
|
||||
(Rational(bignum_value) ** Rational(0)).should eql(Rational(1))
|
||||
(Rational(-bignum_value) ** Rational(0)).should eql(Rational(1))
|
||||
end
|
||||
|
||||
it "returns self raised to the argument as a Rational if the exponent's denominator is 1" do
|
||||
(Rational(3, 4) ** Rational(1, 1)).should eql(Rational(3, 4))
|
||||
(Rational(3, 4) ** Rational(2, 1)).should eql(Rational(9, 16))
|
||||
(Rational(3, 4) ** Rational(-1, 1)).should eql(Rational(4, 3))
|
||||
(Rational(3, 4) ** Rational(-2, 1)).should eql(Rational(16, 9))
|
||||
end
|
||||
|
||||
it "returns self raised to the argument as a Float if the exponent's denominator is not 1" do
|
||||
(Rational(3, 4) ** Rational(4, 3)).should be_close(0.681420222312052, TOLERANCE)
|
||||
(Rational(3, 4) ** Rational(-4, 3)).should be_close(1.46752322173095, TOLERANCE)
|
||||
(Rational(3, 4) ** Rational(4, -3)).should be_close(1.46752322173095, TOLERANCE)
|
||||
end
|
||||
|
||||
it "returns a complex number when self is negative and the passed argument is not 0" do
|
||||
(Rational(-3, 4) ** Rational(-4, 3)).should be_close(Complex(-0.7337616108654732, 1.2709123906625817), TOLERANCE)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "when passed Integer" do
|
||||
it "returns the Rational value of self raised to the passed argument" do
|
||||
(Rational(3, 4) ** 4).should == Rational(81, 256)
|
||||
(Rational(3, 4) ** -4).should == Rational(256, 81)
|
||||
(Rational(-3, 4) ** -4).should == Rational(256, 81)
|
||||
(Rational(3, -4) ** -4).should == Rational(256, 81)
|
||||
|
||||
(Rational(bignum_value, 4) ** 4).should == Rational(28269553036454149273332760011886696253239742350009903329945699220681916416, 1)
|
||||
(Rational(3, bignum_value) ** -4).should == Rational(7237005577332262213973186563042994240829374041602535252466099000494570602496, 81)
|
||||
(Rational(-bignum_value, 4) ** -4).should == Rational(1, 28269553036454149273332760011886696253239742350009903329945699220681916416)
|
||||
(Rational(3, -bignum_value) ** -4).should == Rational(7237005577332262213973186563042994240829374041602535252466099000494570602496, 81)
|
||||
end
|
||||
|
||||
conflicts_with :Prime do
|
||||
it "returns Rational(1, 1) when the passed argument is 0" do
|
||||
(Rational(3, 4) ** 0).should eql(Rational(1, 1))
|
||||
(Rational(-3, 4) ** 0).should eql(Rational(1, 1))
|
||||
(Rational(3, -4) ** 0).should eql(Rational(1, 1))
|
||||
|
||||
(Rational(bignum_value, 4) ** 0).should eql(Rational(1, 1))
|
||||
(Rational(3, -bignum_value) ** 0).should eql(Rational(1, 1))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "when passed Bignum" do
|
||||
# #5713
|
||||
it "returns Rational(0) when self is Rational(0) and the exponent is positive" do
|
||||
(Rational(0) ** bignum_value).should eql(Rational(0))
|
||||
end
|
||||
|
||||
it "raises ZeroDivisionError when self is Rational(0) and the exponent is negative" do
|
||||
lambda { Rational(0) ** -bignum_value }.should raise_error(ZeroDivisionError)
|
||||
end
|
||||
|
||||
it "returns Rational(1) when self is Rational(1)" do
|
||||
(Rational(1) ** bignum_value).should eql(Rational(1))
|
||||
(Rational(1) ** -bignum_value).should eql(Rational(1))
|
||||
end
|
||||
|
||||
it "returns Rational(1) when self is Rational(-1) and the exponent is positive and even" do
|
||||
(Rational(-1) ** bignum_value(0)).should eql(Rational(1))
|
||||
(Rational(-1) ** bignum_value(2)).should eql(Rational(1))
|
||||
end
|
||||
|
||||
it "returns Rational(-1) when self is Rational(-1) and the exponent is positive and odd" do
|
||||
(Rational(-1) ** bignum_value(1)).should eql(Rational(-1))
|
||||
(Rational(-1) ** bignum_value(3)).should eql(Rational(-1))
|
||||
end
|
||||
|
||||
it "returns positive Infinity when self is > 1" do
|
||||
(Rational(2) ** bignum_value).infinite?.should == 1
|
||||
(Rational(fixnum_max) ** bignum_value).infinite?.should == 1
|
||||
end
|
||||
|
||||
it "returns 0.0 when self is > 1 and the exponent is negative" do
|
||||
(Rational(2) ** -bignum_value).should eql(0.0)
|
||||
(Rational(fixnum_max) ** -bignum_value).should eql(0.0)
|
||||
end
|
||||
|
||||
# Fails on linux due to pow() bugs in glibc: http://sources.redhat.com/bugzilla/show_bug.cgi?id=3866
|
||||
platform_is_not :linux do
|
||||
it "returns positive Infinity when self < -1" do
|
||||
(Rational(-2) ** bignum_value).infinite?.should == 1
|
||||
(Rational(-2) ** (bignum_value + 1)).infinite?.should == 1
|
||||
(Rational(fixnum_min) ** bignum_value).infinite?.should == 1
|
||||
end
|
||||
|
||||
it "returns 0.0 when self is < -1 and the exponent is negative" do
|
||||
(Rational(-2) ** -bignum_value).should eql(0.0)
|
||||
(Rational(fixnum_min) ** -bignum_value).should eql(0.0)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "when passed Float" do
|
||||
it "returns self converted to Float and raised to the passed argument" do
|
||||
(Rational(3, 1) ** 3.0).should eql(27.0)
|
||||
(Rational(3, 1) ** 1.5).should be_close(5.19615242270663, TOLERANCE)
|
||||
(Rational(3, 1) ** -1.5).should be_close(0.192450089729875, TOLERANCE)
|
||||
end
|
||||
|
||||
it "returns a complex number if self is negative and the passed argument is not 0" do
|
||||
(Rational(-3, 2) ** 1.5).should be_close(Complex(-3.374618290464398e-16, -1.8371173070873836), TOLERANCE)
|
||||
(Rational(3, -2) ** 1.5).should be_close(Complex(-3.374618290464398e-16, -1.8371173070873836), TOLERANCE)
|
||||
(Rational(3, -2) ** -1.5).should be_close(Complex(-9.998869008783402e-17, 0.5443310539518174), TOLERANCE)
|
||||
end
|
||||
|
||||
it "returns Complex(1.0) when the passed argument is 0.0" do
|
||||
(Rational(3, 4) ** 0.0).should == Complex(1.0)
|
||||
(Rational(-3, 4) ** 0.0).should == Complex(1.0)
|
||||
(Rational(-3, 4) ** 0.0).should == Complex(1.0)
|
||||
end
|
||||
end
|
||||
|
||||
it "calls #coerce on the passed argument with self" do
|
||||
rational = Rational(3, 4)
|
||||
obj = mock("Object")
|
||||
obj.should_receive(:coerce).with(rational).and_return([1, 2])
|
||||
|
||||
rational ** obj
|
||||
end
|
||||
|
||||
it "calls #** on the coerced Rational with the coerced Object" do
|
||||
rational = Rational(3, 4)
|
||||
|
||||
coerced_rational = mock("Coerced Rational")
|
||||
coerced_rational.should_receive(:**).and_return(:result)
|
||||
|
||||
coerced_obj = mock("Coerced Object")
|
||||
|
||||
obj = mock("Object")
|
||||
obj.should_receive(:coerce).and_return([coerced_rational, coerced_obj])
|
||||
|
||||
(rational ** obj).should == :result
|
||||
end
|
||||
|
||||
it "raises ZeroDivisionError for Rational(0, 1) passed a negative Integer" do
|
||||
[-1, -4, -9999].each do |exponent|
|
||||
lambda { Rational(0, 1) ** exponent }.should raise_error(ZeroDivisionError, "divided by 0")
|
||||
end
|
||||
end
|
||||
|
||||
it "raises ZeroDivisionError for Rational(0, 1) passed a negative Rational with denominator 1" do
|
||||
[Rational(-1, 1), Rational(-3, 1)].each do |exponent|
|
||||
lambda { Rational(0, 1) ** exponent }.should raise_error(ZeroDivisionError, "divided by 0")
|
||||
end
|
||||
end
|
||||
|
||||
# #7513
|
||||
it "raises ZeroDivisionError for Rational(0, 1) passed a negative Rational" do
|
||||
lambda { Rational(0, 1) ** Rational(-3, 2) }.should raise_error(ZeroDivisionError, "divided by 0")
|
||||
end
|
||||
|
||||
platform_is_not :solaris do # See https://github.com/ruby/spec/issues/134
|
||||
it "returns Infinity for Rational(0, 1) passed a negative Float" do
|
||||
[-1.0, -3.0, -3.14].each do |exponent|
|
||||
(Rational(0, 1) ** exponent).infinite?.should == 1
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
5
spec/ruby/shared/rational/fdiv.rb
Normal file
5
spec/ruby/shared/rational/fdiv.rb
Normal file
|
@ -0,0 +1,5 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
|
||||
describe :rational_fdiv, shared: true do
|
||||
it "needs to be reviewed for spec completeness"
|
||||
end
|
45
spec/ruby/shared/rational/floor.rb
Normal file
45
spec/ruby/shared/rational/floor.rb
Normal file
|
@ -0,0 +1,45 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
|
||||
describe :rational_floor, shared: true do
|
||||
before do
|
||||
@rational = Rational(2200, 7)
|
||||
end
|
||||
|
||||
describe "with no arguments (precision = 0)" do
|
||||
it "returns an integer" do
|
||||
@rational.floor.should be_kind_of(Integer)
|
||||
end
|
||||
|
||||
it "returns the truncated value toward negative infinity" do
|
||||
@rational.floor.should == 314
|
||||
Rational(1, 2).floor.should == 0
|
||||
Rational(-1, 2).floor.should == -1
|
||||
end
|
||||
end
|
||||
|
||||
describe "with a precision < 0" do
|
||||
it "returns an integer" do
|
||||
@rational.floor(-2).should be_kind_of(Integer)
|
||||
@rational.floor(-1).should be_kind_of(Integer)
|
||||
end
|
||||
|
||||
it "moves the truncation point n decimal places left" do
|
||||
@rational.floor(-3).should == 0
|
||||
@rational.floor(-2).should == 300
|
||||
@rational.floor(-1).should == 310
|
||||
end
|
||||
end
|
||||
|
||||
describe "with a precision > 0" do
|
||||
it "returns a Rational" do
|
||||
@rational.floor(1).should be_kind_of(Rational)
|
||||
@rational.floor(2).should be_kind_of(Rational)
|
||||
end
|
||||
|
||||
it "moves the truncation point n decimal places right" do
|
||||
@rational.floor(1).should == Rational(1571, 5)
|
||||
@rational.floor(2).should == Rational(7857, 25)
|
||||
@rational.floor(3).should == Rational(62857, 200)
|
||||
end
|
||||
end
|
||||
end
|
9
spec/ruby/shared/rational/hash.rb
Normal file
9
spec/ruby/shared/rational/hash.rb
Normal file
|
@ -0,0 +1,9 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
|
||||
describe :rational_hash, shared: true do
|
||||
# BUG: Rational(2, 3).hash == Rational(3, 2).hash
|
||||
it "is static" do
|
||||
Rational(2, 3).hash.should == Rational(2, 3).hash
|
||||
Rational(2, 4).hash.should_not == Rational(2, 3).hash
|
||||
end
|
||||
end
|
12
spec/ruby/shared/rational/inspect.rb
Normal file
12
spec/ruby/shared/rational/inspect.rb
Normal file
|
@ -0,0 +1,12 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
|
||||
describe :rational_inspect, shared: true do
|
||||
conflicts_with :Prime do
|
||||
it "returns a string representation of self" do
|
||||
Rational(3, 4).inspect.should == "(3/4)"
|
||||
Rational(-5, 8).inspect.should == "(-5/8)"
|
||||
Rational(-1, -2).inspect.should == "(1/2)"
|
||||
Rational(bignum_value, 1).inspect.should == "(#{bignum_value}/1)"
|
||||
end
|
||||
end
|
||||
end
|
5
spec/ruby/shared/rational/marshal_dump.rb
Normal file
5
spec/ruby/shared/rational/marshal_dump.rb
Normal file
|
@ -0,0 +1,5 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
|
||||
describe :rational_marshal_dump, shared: true do
|
||||
it "needs to be reviewed for spec completeness"
|
||||
end
|
5
spec/ruby/shared/rational/marshal_load.rb
Normal file
5
spec/ruby/shared/rational/marshal_load.rb
Normal file
|
@ -0,0 +1,5 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
|
||||
describe :rational_marshal_load, shared: true do
|
||||
it "needs to be reviewed for spec completeness"
|
||||
end
|
48
spec/ruby/shared/rational/minus.rb
Normal file
48
spec/ruby/shared/rational/minus.rb
Normal file
|
@ -0,0 +1,48 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
|
||||
describe :rational_minus_rat, shared: true do
|
||||
it "returns the result of substracting other from self as a Rational" do
|
||||
(Rational(3, 4) - Rational(0, 1)).should eql(Rational(3, 4))
|
||||
(Rational(3, 4) - Rational(1, 4)).should eql(Rational(1, 2))
|
||||
|
||||
(Rational(3, 4) - Rational(2, 1)).should eql(Rational(-5, 4))
|
||||
end
|
||||
end
|
||||
|
||||
describe :rational_minus_int, shared: true do
|
||||
it "returns the result of substracting other from self as a Rational" do
|
||||
(Rational(3, 4) - 1).should eql(Rational(-1, 4))
|
||||
(Rational(3, 4) - 2).should eql(Rational(-5, 4))
|
||||
end
|
||||
end
|
||||
|
||||
describe :rational_minus_float, shared: true do
|
||||
it "returns the result of substracting other from self as a Float" do
|
||||
(Rational(3, 4) - 0.2).should eql(0.55)
|
||||
(Rational(3, 4) - 2.5).should eql(-1.75)
|
||||
end
|
||||
end
|
||||
|
||||
describe :rational_minus, shared: true do
|
||||
it "calls #coerce on the passed argument with self" do
|
||||
rational = Rational(3, 4)
|
||||
obj = mock("Object")
|
||||
obj.should_receive(:coerce).with(rational).and_return([1, 2])
|
||||
|
||||
rational - obj
|
||||
end
|
||||
|
||||
it "calls #- on the coerced Rational with the coerced Object" do
|
||||
rational = Rational(3, 4)
|
||||
|
||||
coerced_rational = mock("Coerced Rational")
|
||||
coerced_rational.should_receive(:-).and_return(:result)
|
||||
|
||||
coerced_obj = mock("Coerced Object")
|
||||
|
||||
obj = mock("Object")
|
||||
obj.should_receive(:coerce).and_return([coerced_rational, coerced_obj])
|
||||
|
||||
(rational - obj).should == :result
|
||||
end
|
||||
end
|
43
spec/ruby/shared/rational/modulo.rb
Normal file
43
spec/ruby/shared/rational/modulo.rb
Normal file
|
@ -0,0 +1,43 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
|
||||
describe :rational_modulo, shared: true do
|
||||
it "returns the remainder when this value is divided by other" do
|
||||
Rational(2, 3).send(@method, Rational(2, 3)).should == Rational(0, 1)
|
||||
Rational(4, 3).send(@method, Rational(2, 3)).should == Rational(0, 1)
|
||||
Rational(2, -3).send(@method, Rational(-2, 3)).should == Rational(0, 1)
|
||||
Rational(0, -1).send(@method, -1).should == Rational(0, 1)
|
||||
|
||||
Rational(7, 4).send(@method, Rational(1, 2)).should == Rational(1, 4)
|
||||
Rational(7, 4).send(@method, 1).should == Rational(3, 4)
|
||||
Rational(7, 4).send(@method, Rational(1, 7)).should == Rational(1, 28)
|
||||
|
||||
Rational(3, 4).send(@method, -1).should == Rational(-1, 4)
|
||||
Rational(1, -5).send(@method, -1).should == Rational(-1, 5)
|
||||
end
|
||||
|
||||
it "returns a Float value when the argument is Float" do
|
||||
Rational(7, 4).send(@method, 1.0).should be_kind_of(Float)
|
||||
Rational(7, 4).send(@method, 1.0).should == 0.75
|
||||
Rational(7, 4).send(@method, 0.26).should be_close(0.19, 0.0001)
|
||||
end
|
||||
|
||||
it "raises ZeroDivisionError on zero denominator" do
|
||||
lambda {
|
||||
Rational(3, 5).send(@method, Rational(0, 1))
|
||||
}.should raise_error(ZeroDivisionError)
|
||||
|
||||
lambda {
|
||||
Rational(0, 1).send(@method, Rational(0, 1))
|
||||
}.should raise_error(ZeroDivisionError)
|
||||
|
||||
lambda {
|
||||
Rational(3, 5).send(@method, 0)
|
||||
}.should raise_error(ZeroDivisionError)
|
||||
end
|
||||
|
||||
it "raises a ZeroDivisionError when the argument is 0.0" do
|
||||
lambda {
|
||||
Rational(3, 5).send(@method, 0.0)
|
||||
}.should raise_error(ZeroDivisionError)
|
||||
end
|
||||
end
|
62
spec/ruby/shared/rational/multiply.rb
Normal file
62
spec/ruby/shared/rational/multiply.rb
Normal file
|
@ -0,0 +1,62 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
|
||||
describe :rational_multiply_rat, shared: true do
|
||||
it "returns self divided by other as a Rational" do
|
||||
(Rational(3, 4) * Rational(3, 4)).should eql(Rational(9, 16))
|
||||
(Rational(2, 4) * Rational(1, 4)).should eql(Rational(1, 8))
|
||||
|
||||
(Rational(3, 4) * Rational(0, 1)).should eql(Rational(0, 4))
|
||||
end
|
||||
end
|
||||
|
||||
describe :rational_multiply_int, shared: true do
|
||||
it "returns self divided by other as a Rational" do
|
||||
(Rational(3, 4) * 2).should eql(Rational(3, 2))
|
||||
(Rational(2, 4) * 2).should eql(Rational(1, 1))
|
||||
(Rational(6, 7) * -2).should eql(Rational(-12, 7))
|
||||
|
||||
(Rational(3, 4) * 0).should eql(Rational(0, 4))
|
||||
end
|
||||
end
|
||||
|
||||
describe :rational_multiply_float, shared: true do
|
||||
it "returns self divided by other as a Float" do
|
||||
(Rational(3, 4) * 0.75).should eql(0.5625)
|
||||
(Rational(3, 4) * 0.25).should eql(0.1875)
|
||||
(Rational(3, 4) * 0.3).should be_close(0.225, TOLERANCE)
|
||||
|
||||
(Rational(-3, 4) * 0.3).should be_close(-0.225, TOLERANCE)
|
||||
(Rational(3, -4) * 0.3).should be_close(-0.225, TOLERANCE)
|
||||
(Rational(3, 4) * -0.3).should be_close(-0.225, TOLERANCE)
|
||||
|
||||
(Rational(3, 4) * 0.0).should eql(0.0)
|
||||
(Rational(-3, -4) * 0.0).should eql(0.0)
|
||||
|
||||
(Rational(-3, 4) * 0.0).should eql(0.0)
|
||||
(Rational(3, -4) * 0.0).should eql(0.0)
|
||||
end
|
||||
end
|
||||
|
||||
describe :rational_multiply, shared: true do
|
||||
it "calls #coerce on the passed argument with self" do
|
||||
rational = Rational(3, 4)
|
||||
obj = mock("Object")
|
||||
obj.should_receive(:coerce).with(rational).and_return([1, 2])
|
||||
|
||||
rational * obj
|
||||
end
|
||||
|
||||
it "calls #* on the coerced Rational with the coerced Object" do
|
||||
rational = Rational(3, 4)
|
||||
|
||||
coerced_rational = mock("Coerced Rational")
|
||||
coerced_rational.should_receive(:*).and_return(:result)
|
||||
|
||||
coerced_obj = mock("Coerced Object")
|
||||
|
||||
obj = mock("Object")
|
||||
obj.should_receive(:coerce).and_return([coerced_rational, coerced_obj])
|
||||
|
||||
(rational * obj).should == :result
|
||||
end
|
||||
end
|
10
spec/ruby/shared/rational/numerator.rb
Normal file
10
spec/ruby/shared/rational/numerator.rb
Normal file
|
@ -0,0 +1,10 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
|
||||
describe :rational_numerator, shared: true do
|
||||
it "returns the numerator" do
|
||||
Rational(3, 4).numerator.should equal(3)
|
||||
Rational(3, -4).numerator.should equal(-3)
|
||||
|
||||
Rational(bignum_value, 1).numerator.should == bignum_value
|
||||
end
|
||||
end
|
48
spec/ruby/shared/rational/plus.rb
Normal file
48
spec/ruby/shared/rational/plus.rb
Normal file
|
@ -0,0 +1,48 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
|
||||
describe :rational_plus_rat, shared: true do
|
||||
it "returns the result of substracting other from self as a Rational" do
|
||||
(Rational(3, 4) + Rational(0, 1)).should eql(Rational(3, 4))
|
||||
(Rational(3, 4) + Rational(1, 4)).should eql(Rational(1, 1))
|
||||
|
||||
(Rational(3, 4) + Rational(2, 1)).should eql(Rational(11, 4))
|
||||
end
|
||||
end
|
||||
|
||||
describe :rational_plus_int, shared: true do
|
||||
it "returns the result of substracting other from self as a Rational" do
|
||||
(Rational(3, 4) + 1).should eql(Rational(7, 4))
|
||||
(Rational(3, 4) + 2).should eql(Rational(11, 4))
|
||||
end
|
||||
end
|
||||
|
||||
describe :rational_plus_float, shared: true do
|
||||
it "returns the result of substracting other from self as a Float" do
|
||||
(Rational(3, 4) + 0.2).should eql(0.95)
|
||||
(Rational(3, 4) + 2.5).should eql(3.25)
|
||||
end
|
||||
end
|
||||
|
||||
describe :rational_plus, shared: true do
|
||||
it "calls #coerce on the passed argument with self" do
|
||||
rational = Rational(3, 4)
|
||||
obj = mock("Object")
|
||||
obj.should_receive(:coerce).with(rational).and_return([1, 2])
|
||||
|
||||
rational + obj
|
||||
end
|
||||
|
||||
it "calls #+ on the coerced Rational with the coerced Object" do
|
||||
rational = Rational(3, 4)
|
||||
|
||||
coerced_rational = mock("Coerced Rational")
|
||||
coerced_rational.should_receive(:+).and_return(:result)
|
||||
|
||||
coerced_obj = mock("Coerced Object")
|
||||
|
||||
obj = mock("Object")
|
||||
obj.should_receive(:coerce).and_return([coerced_rational, coerced_obj])
|
||||
|
||||
(rational + obj).should == :result
|
||||
end
|
||||
end
|
5
spec/ruby/shared/rational/quo.rb
Normal file
5
spec/ruby/shared/rational/quo.rb
Normal file
|
@ -0,0 +1,5 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
|
||||
describe :rational_quo, shared: true do
|
||||
it "needs to be reviewed for spec completeness"
|
||||
end
|
5
spec/ruby/shared/rational/remainder.rb
Normal file
5
spec/ruby/shared/rational/remainder.rb
Normal file
|
@ -0,0 +1,5 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
|
||||
describe :rational_remainder, shared: true do
|
||||
it "needs to be reviewed for spec completeness"
|
||||
end
|
71
spec/ruby/shared/rational/round.rb
Normal file
71
spec/ruby/shared/rational/round.rb
Normal file
|
@ -0,0 +1,71 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
|
||||
describe :rational_round, shared: true do
|
||||
before do
|
||||
@rational = Rational(2200, 7)
|
||||
end
|
||||
|
||||
describe "with no arguments (precision = 0)" do
|
||||
it "returns an integer" do
|
||||
@rational.round.should be_kind_of(Integer)
|
||||
Rational(0, 1).round(0).should be_kind_of(Integer)
|
||||
Rational(124, 1).round(0).should be_kind_of(Integer)
|
||||
end
|
||||
|
||||
it "returns the truncated value toward the nearest integer" do
|
||||
@rational.round.should == 314
|
||||
Rational(0, 1).round(0).should == 0
|
||||
Rational(2, 1).round(0).should == 2
|
||||
end
|
||||
|
||||
it "returns the rounded value toward the nearest integer" do
|
||||
Rational(1, 2).round.should == 1
|
||||
Rational(-1, 2).round.should == -1
|
||||
Rational(3, 2).round.should == 2
|
||||
Rational(-3, 2).round.should == -2
|
||||
Rational(5, 2).round.should == 3
|
||||
Rational(-5, 2).round.should == -3
|
||||
end
|
||||
end
|
||||
|
||||
describe "with a precision < 0" do
|
||||
it "returns an integer" do
|
||||
@rational.round(-2).should be_kind_of(Integer)
|
||||
@rational.round(-1).should be_kind_of(Integer)
|
||||
Rational(0, 1).round(-1).should be_kind_of(Integer)
|
||||
Rational(2, 1).round(-1).should be_kind_of(Integer)
|
||||
end
|
||||
|
||||
it "moves the truncation point n decimal places left" do
|
||||
@rational.round(-3).should == 0
|
||||
@rational.round(-2).should == 300
|
||||
@rational.round(-1).should == 310
|
||||
end
|
||||
end
|
||||
|
||||
describe "with a precision > 0" do
|
||||
it "returns a Rational" do
|
||||
@rational.round(1).should be_kind_of(Rational)
|
||||
@rational.round(2).should be_kind_of(Rational)
|
||||
Rational(0, 1).round(1).should be_kind_of(Rational)
|
||||
Rational(2, 1).round(1).should be_kind_of(Rational)
|
||||
end
|
||||
|
||||
it "moves the truncation point n decimal places right" do
|
||||
@rational.round(1).should == Rational(3143, 10)
|
||||
@rational.round(2).should == Rational(31429, 100)
|
||||
@rational.round(3).should == Rational(157143, 500)
|
||||
Rational(0, 1).round(1).should == Rational(0, 1)
|
||||
Rational(2, 1).round(1).should == Rational(2, 1)
|
||||
end
|
||||
|
||||
it "doesn't alter the value if the precision is too great" do
|
||||
Rational(3, 2).round(10).should == Rational(3, 2).round(20)
|
||||
end
|
||||
|
||||
# #6605
|
||||
it "doesn't fail when rounding to an absurdly large positive precision" do
|
||||
Rational(3, 2).round(2_097_171).should == Rational(3, 2)
|
||||
end
|
||||
end
|
||||
end
|
10
spec/ruby/shared/rational/to_f.rb
Normal file
10
spec/ruby/shared/rational/to_f.rb
Normal file
|
@ -0,0 +1,10 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
|
||||
describe :rational_to_f, shared: true do
|
||||
it "returns self converted to a Float" do
|
||||
Rational(3, 4).to_f.should eql(0.75)
|
||||
Rational(3, -4).to_f.should eql(-0.75)
|
||||
Rational(-1, 4).to_f.should eql(-0.25)
|
||||
Rational(-1, -4).to_f.should eql(0.25)
|
||||
end
|
||||
end
|
12
spec/ruby/shared/rational/to_i.rb
Normal file
12
spec/ruby/shared/rational/to_i.rb
Normal file
|
@ -0,0 +1,12 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
|
||||
describe :rational_to_i, shared: true do
|
||||
it "converts self to an Integer by truncation" do
|
||||
Rational(7, 4).to_i.should eql(1)
|
||||
Rational(11, 4).to_i.should eql(2)
|
||||
end
|
||||
|
||||
it "converts self to an Integer by truncation" do
|
||||
Rational(-7, 4).to_i.should eql(-1)
|
||||
end
|
||||
end
|
13
spec/ruby/shared/rational/to_r.rb
Normal file
13
spec/ruby/shared/rational/to_r.rb
Normal file
|
@ -0,0 +1,13 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
|
||||
describe :rational_to_r, shared: true do
|
||||
conflicts_with :Prime do
|
||||
it "returns self" do
|
||||
a = Rational(3, 4)
|
||||
a.to_r.should equal(a)
|
||||
|
||||
a = Rational(bignum_value, 4)
|
||||
a.to_r.should equal(a)
|
||||
end
|
||||
end
|
||||
end
|
11
spec/ruby/shared/rational/to_s.rb
Normal file
11
spec/ruby/shared/rational/to_s.rb
Normal file
|
@ -0,0 +1,11 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
|
||||
describe :rational_to_s, shared: true do
|
||||
it "returns a string representation of self" do
|
||||
Rational(1, 1).to_s.should == "1/1"
|
||||
Rational(2, 1).to_s.should == "2/1"
|
||||
Rational(1, 2).to_s.should == "1/2"
|
||||
Rational(-1, 3).to_s.should == "-1/3"
|
||||
Rational(1, -3).to_s.should == "-1/3"
|
||||
end
|
||||
end
|
45
spec/ruby/shared/rational/truncate.rb
Normal file
45
spec/ruby/shared/rational/truncate.rb
Normal file
|
@ -0,0 +1,45 @@
|
|||
require File.expand_path('../../../spec_helper', __FILE__)
|
||||
|
||||
describe :rational_truncate, shared: true do
|
||||
before do
|
||||
@rational = Rational(2200, 7)
|
||||
end
|
||||
|
||||
describe "with no arguments (precision = 0)" do
|
||||
it "returns an integer" do
|
||||
@rational.truncate.should be_kind_of(Integer)
|
||||
end
|
||||
|
||||
it "returns the truncated value toward 0" do
|
||||
@rational.truncate.should == 314
|
||||
Rational(1, 2).truncate.should == 0
|
||||
Rational(-1, 2).truncate.should == 0
|
||||
end
|
||||
end
|
||||
|
||||
describe "with a precision < 0" do
|
||||
it "returns an integer" do
|
||||
@rational.truncate(-2).should be_kind_of(Integer)
|
||||
@rational.truncate(-1).should be_kind_of(Integer)
|
||||
end
|
||||
|
||||
it "moves the truncation point n decimal places left" do
|
||||
@rational.truncate(-3).should == 0
|
||||
@rational.truncate(-2).should == 300
|
||||
@rational.truncate(-1).should == 310
|
||||
end
|
||||
end
|
||||
|
||||
describe "with a precision > 0" do
|
||||
it "returns a Rational" do
|
||||
@rational.truncate(1).should be_kind_of(Rational)
|
||||
@rational.truncate(2).should be_kind_of(Rational)
|
||||
end
|
||||
|
||||
it "moves the truncation point n decimal places right" do
|
||||
@rational.truncate(1).should == Rational(1571, 5)
|
||||
@rational.truncate(2).should == Rational(7857, 25)
|
||||
@rational.truncate(3).should == Rational(62857, 200)
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue