diff options
Diffstat (limited to 'spec/ruby/shared/rational')
-rw-r--r-- | spec/ruby/shared/rational/Rational.rb | 57 | ||||
-rw-r--r-- | spec/ruby/shared/rational/coerce.rb | 34 | ||||
-rw-r--r-- | spec/ruby/shared/rational/divmod.rb | 4 | ||||
-rw-r--r-- | spec/ruby/shared/rational/exponent.rb | 8 | ||||
-rw-r--r-- | spec/ruby/shared/rational/marshal_dump.rb | 5 | ||||
-rw-r--r-- | spec/ruby/shared/rational/marshal_load.rb | 5 | ||||
-rw-r--r-- | spec/ruby/shared/rational/minus.rb | 48 | ||||
-rw-r--r-- | spec/ruby/shared/rational/quo.rb | 5 | ||||
-rw-r--r-- | spec/ruby/shared/rational/to_f.rb | 6 | ||||
-rw-r--r-- | spec/ruby/shared/rational/truncate.rb | 26 |
10 files changed, 71 insertions, 127 deletions
diff --git a/spec/ruby/shared/rational/Rational.rb b/spec/ruby/shared/rational/Rational.rb index 936a90c086..500f7ed271 100644 --- a/spec/ruby/shared/rational/Rational.rb +++ b/spec/ruby/shared/rational/Rational.rb @@ -65,40 +65,45 @@ describe :kernel_Rational, shared: true do r_s.should == r r_s.should_not == f_r end + 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) + 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 + 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 - -> { Rational(Complex(1, 2)) }.should raise_error(RangeError) - 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 TypeError if the first argument is nil" do - -> { Rational(nil) }.should raise_error(TypeError) + it "raises a RangeError if the imaginary part is not 0" do + -> { Rational(Complex(1, 2)) }.should raise_error(RangeError) end + end - it "raises a TypeError if the second argument is nil" do - -> { Rational(1, nil) }.should raise_error(TypeError) - end + it "raises a ZeroDivisionError if the second argument is 0" do + -> { Rational(1, 0) }.should raise_error(ZeroDivisionError, "divided by 0") + -> { Rational(1, 0.0) }.should raise_error(ZeroDivisionError, "divided by 0") + end - it "raises a TypeError if the first argument is a Symbol" do - -> { Rational(:sym) }.should raise_error(TypeError) - end + it "raises a TypeError if the first argument is nil" do + -> { Rational(nil) }.should raise_error(TypeError) + end - it "raises a TypeError if the second argument is a Symbol" do - -> { Rational(1, :sym) }.should raise_error(TypeError) - end + it "raises a TypeError if the second argument is nil" do + -> { Rational(1, nil) }.should raise_error(TypeError) + end + + it "raises a TypeError if the first argument is a Symbol" do + -> { Rational(:sym) }.should raise_error(TypeError) + end + + it "raises a TypeError if the second argument is a Symbol" do + -> { Rational(1, :sym) }.should raise_error(TypeError) end describe "when passed exception: false" do @@ -138,4 +143,8 @@ describe :kernel_Rational, shared: true do end end end + + it "freezes its result" do + Rational(1).frozen?.should == true + end end diff --git a/spec/ruby/shared/rational/coerce.rb b/spec/ruby/shared/rational/coerce.rb deleted file mode 100644 index ccc8901ba0..0000000000 --- a/spec/ruby/shared/rational/coerce.rb +++ /dev/null @@ -1,34 +0,0 @@ -require_relative '../../spec_helper' - -require 'bigdecimal' - -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 "coerces to Rational, when given a Complex" do - Rational(3, 4).coerce(Complex(5)).should == [Rational(5, 1), Rational(3, 4)] - Rational(12, 4).coerce(Complex(5, 1)).should == [Complex(5, 1), Complex(3)] - 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 - - it "raises an error when passed a BigDecimal" do - -> { - Rational(500, 3).coerce(BigDecimal('166.666666666')) - }.should raise_error(TypeError, /BigDecimal can't be coerced into Rational/) - end -end diff --git a/spec/ruby/shared/rational/divmod.rb b/spec/ruby/shared/rational/divmod.rb index 471cd7a967..9e23a18186 100644 --- a/spec/ruby/shared/rational/divmod.rb +++ b/spec/ruby/shared/rational/divmod.rb @@ -6,7 +6,7 @@ describe :rational_divmod_rat, shared: true do 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)]) + Rational(bignum_value, 4).divmod(Rational(4, 3)).should eql([3458764513820540928, Rational(0, 1)]) end it "raises a ZeroDivisionError when passed a Rational with a numerator of 0" do @@ -19,7 +19,7 @@ describe :rational_divmod_int, shared: true 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)] + Rational(bignum_value, 4).divmod(3).should eql([1537228672809129301, Rational(1, 1)]) end it "raises a ZeroDivisionError when passed 0" do diff --git a/spec/ruby/shared/rational/exponent.rb b/spec/ruby/shared/rational/exponent.rb index 3fd02de08f..b0e9b23574 100644 --- a/spec/ruby/shared/rational/exponent.rb +++ b/spec/ruby/shared/rational/exponent.rb @@ -40,10 +40,10 @@ describe :rational_exponent, shared: true do (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) + (Rational(bignum_value, 4) ** 4).should == Rational(452312848583266388373324160190187140051835877600158453279131187530910662656, 1) + (Rational(3, bignum_value) ** -4).should == Rational(115792089237316195423570985008687907853269984665640564039457584007913129639936, 81) + (Rational(-bignum_value, 4) ** -4).should == Rational(1, 452312848583266388373324160190187140051835877600158453279131187530910662656) + (Rational(3, -bignum_value) ** -4).should == Rational(115792089237316195423570985008687907853269984665640564039457584007913129639936, 81) end # Guard against the Mathn library diff --git a/spec/ruby/shared/rational/marshal_dump.rb b/spec/ruby/shared/rational/marshal_dump.rb deleted file mode 100644 index 09782b45a5..0000000000 --- a/spec/ruby/shared/rational/marshal_dump.rb +++ /dev/null @@ -1,5 +0,0 @@ -require_relative '../../spec_helper' - -describe :rational_marshal_dump, shared: true do - it "needs to be reviewed for spec completeness" -end diff --git a/spec/ruby/shared/rational/marshal_load.rb b/spec/ruby/shared/rational/marshal_load.rb deleted file mode 100644 index 20bdd6fdf4..0000000000 --- a/spec/ruby/shared/rational/marshal_load.rb +++ /dev/null @@ -1,5 +0,0 @@ -require_relative '../../spec_helper' - -describe :rational_marshal_load, shared: true do - it "needs to be reviewed for spec completeness" -end diff --git a/spec/ruby/shared/rational/minus.rb b/spec/ruby/shared/rational/minus.rb deleted file mode 100644 index 0a0946fdb9..0000000000 --- a/spec/ruby/shared/rational/minus.rb +++ /dev/null @@ -1,48 +0,0 @@ -require_relative '../../spec_helper' - -describe :rational_minus_rat, shared: true do - it "returns the result of subtracting 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 subtracting 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 subtracting 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 diff --git a/spec/ruby/shared/rational/quo.rb b/spec/ruby/shared/rational/quo.rb deleted file mode 100644 index 53b32fed2f..0000000000 --- a/spec/ruby/shared/rational/quo.rb +++ /dev/null @@ -1,5 +0,0 @@ -require_relative '../../spec_helper' - -describe :rational_quo, shared: true do - it "needs to be reviewed for spec completeness" -end diff --git a/spec/ruby/shared/rational/to_f.rb b/spec/ruby/shared/rational/to_f.rb index 56e0b61d68..472a585daa 100644 --- a/spec/ruby/shared/rational/to_f.rb +++ b/spec/ruby/shared/rational/to_f.rb @@ -7,4 +7,10 @@ describe :rational_to_f, shared: true do Rational(-1, 4).to_f.should eql(-0.25) Rational(-1, -4).to_f.should eql(0.25) end + + it "converts to a Float for large numerator and denominator" do + num = 1000000000000000000000000000000000048148248609680896326399448564623182963452541226153892315137780403285956264146010000000000000000000000000000000000048148248609680896326399448564623182963452541226153892315137780403285956264146010000000000000000000000000000000000048148248609680896326399448564623182963452541226153892315137780403285956264146009 + den = 2000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 + Rational(num, den).to_f.should == 500.0 + end end diff --git a/spec/ruby/shared/rational/truncate.rb b/spec/ruby/shared/rational/truncate.rb index 761dd3113a..df5198ca02 100644 --- a/spec/ruby/shared/rational/truncate.rb +++ b/spec/ruby/shared/rational/truncate.rb @@ -17,6 +17,18 @@ describe :rational_truncate, shared: true do end end + describe "with an explicit precision = 0" do + it "returns an integer" do + @rational.truncate(0).should be_kind_of(Integer) + end + + it "returns the truncated value toward 0" do + @rational.truncate(0).should == 314 + Rational(1, 2).truncate(0).should == 0 + Rational(-1, 2).truncate(0).should == 0 + end + end + describe "with a precision < 0" do it "returns an integer" do @rational.truncate(-2).should be_kind_of(Integer) @@ -42,4 +54,18 @@ describe :rational_truncate, shared: true do @rational.truncate(3).should == Rational(62857, 200) end end + + describe "with an invalid value for precision" do + it "raises a TypeError" do + -> { @rational.truncate(nil) }.should raise_error(TypeError, "not an integer") + -> { @rational.truncate(1.0) }.should raise_error(TypeError, "not an integer") + -> { @rational.truncate('') }.should raise_error(TypeError, "not an integer") + end + + it "does not call to_int on the argument" do + object = Object.new + object.should_not_receive(:to_int) + -> { @rational.truncate(object) }.should raise_error(TypeError, "not an integer") + end + end end |