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
61
spec/ruby/library/bigdecimal/shared/eql.rb
Normal file
61
spec/ruby/library/bigdecimal/shared/eql.rb
Normal file
|
@ -0,0 +1,61 @@
|
|||
require 'bigdecimal'
|
||||
|
||||
describe :bigdecimal_eql, shared: true do
|
||||
before :each do
|
||||
@bg6543_21 = BigDecimal.new("6543.21")
|
||||
@bg5667_19 = BigDecimal.new("5667.19")
|
||||
@a = BigDecimal("1.0000000000000000000000000000000000000000005")
|
||||
@b = BigDecimal("1.00000000000000000000000000000000000000000005")
|
||||
@bigint = BigDecimal("1000.0")
|
||||
@nan = BigDecimal("NaN")
|
||||
@infinity = BigDecimal("Infinity")
|
||||
@infinity_minus = BigDecimal("-Infinity")
|
||||
end
|
||||
|
||||
it "tests for equality" do
|
||||
@bg6543_21.send(@method, @bg6543_21).should == true
|
||||
@a.send(@method, @a).should == true
|
||||
@a.send(@method, @b).should == false
|
||||
@bg6543_21.send(@method, @a).should == false
|
||||
@bigint.send(@method, 1000).should == true
|
||||
end
|
||||
|
||||
it "returns false for NaN as it is never equal to any number" do
|
||||
@nan.send(@method, @nan).should == false
|
||||
@a.send(@method, @nan).should == false
|
||||
@nan.send(@method, @a).should == false
|
||||
@nan.send(@method, @infinity).should == false
|
||||
@nan.send(@method, @infinity_minus).should == false
|
||||
@infinity.send(@method, @nan).should == false
|
||||
@infinity_minus.send(@method, @nan).should == false
|
||||
end
|
||||
|
||||
it "returns true for infinity values with the same sign" do
|
||||
@infinity.send(@method, @infinity).should == true
|
||||
@infinity.send(@method, BigDecimal("Infinity")).should == true
|
||||
BigDecimal("Infinity").send(@method, @infinity).should == true
|
||||
|
||||
@infinity_minus.send(@method, @infinity_minus).should == true
|
||||
@infinity_minus.send(@method, BigDecimal("-Infinity")).should == true
|
||||
BigDecimal("-Infinity").send(@method, @infinity_minus).should == true
|
||||
end
|
||||
|
||||
it "returns false for infinity values with different signs" do
|
||||
@infinity.send(@method, @infinity_minus).should == false
|
||||
@infinity_minus.send(@method, @infinity).should == false
|
||||
end
|
||||
|
||||
it "returns false when infinite value compared to finite one" do
|
||||
@infinity.send(@method, @a).should == false
|
||||
@infinity_minus.send(@method, @a).should == false
|
||||
|
||||
@a.send(@method, @infinity).should == false
|
||||
@a.send(@method, @infinity_minus).should == false
|
||||
end
|
||||
|
||||
it "returns false when compared objects that can not be coerced into BigDecimal" do
|
||||
@infinity.send(@method, nil).should == false
|
||||
@bigint.send(@method, nil).should == false
|
||||
@nan.send(@method, nil).should == false
|
||||
end
|
||||
end
|
116
spec/ruby/library/bigdecimal/shared/modulo.rb
Normal file
116
spec/ruby/library/bigdecimal/shared/modulo.rb
Normal file
|
@ -0,0 +1,116 @@
|
|||
require 'bigdecimal'
|
||||
|
||||
describe :bigdecimal_modulo, shared: true do
|
||||
before :each do
|
||||
@one = BigDecimal("1")
|
||||
@zero = BigDecimal("0")
|
||||
@zero_pos = BigDecimal("+0")
|
||||
@zero_neg = BigDecimal("-0")
|
||||
@two = BigDecimal("2")
|
||||
@three = BigDecimal("3")
|
||||
@mixed = BigDecimal("1.23456789")
|
||||
@nan = BigDecimal("NaN")
|
||||
@infinity = BigDecimal("Infinity")
|
||||
@infinity_minus = BigDecimal("-Infinity")
|
||||
@one_minus = BigDecimal("-1")
|
||||
@frac_1 = BigDecimal("1E-9999")
|
||||
@frac_2 = BigDecimal("0.9E-9999")
|
||||
end
|
||||
|
||||
it "returns self modulo other" do
|
||||
bd6543 = BigDecimal.new("6543.21")
|
||||
bd5667 = BigDecimal.new("5667.19")
|
||||
a = BigDecimal("1.0000000000000000000000000000000000000000005")
|
||||
b = BigDecimal("1.00000000000000000000000000000000000000000005")
|
||||
|
||||
bd6543.send(@method, 137).should == BigDecimal("104.21")
|
||||
bd5667.send(@method, bignum_value).should == 5667.19
|
||||
bd6543.send(@method, BigDecimal("137.24")).should == BigDecimal("92.93")
|
||||
bd6543.send(@method, 137).should be_close(6543.21.%(137), TOLERANCE)
|
||||
bd6543.send(@method, 137).should == bd6543 % 137
|
||||
bd5667.send(@method, bignum_value).should be_close(5667.19.%(0xffffffff), TOLERANCE)
|
||||
bd5667.send(@method, bignum_value).should == bd5667.%(0xffffffff)
|
||||
bd6543.send(@method, 137.24).should be_close(6543.21.%(137.24), TOLERANCE)
|
||||
a.send(@method, b).should == BigDecimal("0.45E-42")
|
||||
@zero.send(@method, @one).should == @zero
|
||||
@zero.send(@method, @one_minus).should == @zero
|
||||
@two.send(@method, @one).should == @zero
|
||||
@one.send(@method, @two).should == @one
|
||||
@frac_1.send(@method, @one).should == @frac_1
|
||||
@frac_2.send(@method, @one).should == @frac_2
|
||||
@one_minus.send(@method, @one_minus).should == @zero
|
||||
@one_minus.send(@method, @one).should == @zero
|
||||
@one_minus.send(@method, @two).should == @one
|
||||
@one.send(@method, -@two).should == -@one
|
||||
|
||||
@one_minus.modulo(BigDecimal('0.9')).should == BigDecimal('0.8')
|
||||
@one.modulo(BigDecimal('-0.9')).should == BigDecimal('-0.8')
|
||||
|
||||
@one_minus.modulo(BigDecimal('0.8')).should == BigDecimal('0.6')
|
||||
@one.modulo(BigDecimal('-0.8')).should == BigDecimal('-0.6')
|
||||
|
||||
@one_minus.modulo(BigDecimal('0.6')).should == BigDecimal('0.2')
|
||||
@one.modulo(BigDecimal('-0.6')).should == BigDecimal('-0.2')
|
||||
|
||||
@one_minus.modulo(BigDecimal('0.5')).should == @zero
|
||||
@one.modulo(BigDecimal('-0.5')).should == @zero
|
||||
@one_minus.modulo(BigDecimal('-0.5')).should == @zero
|
||||
|
||||
@one_minus.modulo(BigDecimal('0.4')).should == BigDecimal('0.2')
|
||||
@one.modulo(BigDecimal('-0.4')).should == BigDecimal('-0.2')
|
||||
|
||||
@one_minus.modulo(BigDecimal('0.3')).should == BigDecimal('0.2')
|
||||
@one_minus.modulo(BigDecimal('0.2')).should == @zero
|
||||
end
|
||||
|
||||
it "returns a [Float value] when the argument is Float" do
|
||||
@two.send(@method, 2.0).should == 0.0
|
||||
@one.send(@method, 2.0).should == 1.0
|
||||
res = @two.send(@method, 5.0)
|
||||
res.kind_of?(BigDecimal).should == true
|
||||
end
|
||||
|
||||
it "returns NaN if NaN is involved" do
|
||||
@nan.send(@method, @nan).nan?.should == true
|
||||
@nan.send(@method, @one).nan?.should == true
|
||||
@one.send(@method, @nan).nan?.should == true
|
||||
@infinity.send(@method, @nan).nan?.should == true
|
||||
@nan.send(@method, @infinity).nan?.should == true
|
||||
end
|
||||
|
||||
it "returns NaN if the dividend is Infinity" do
|
||||
@infinity.send(@method, @infinity).nan?.should == true
|
||||
@infinity.send(@method, @one).nan?.should == true
|
||||
@infinity.send(@method, @mixed).nan?.should == true
|
||||
@infinity.send(@method, @one_minus).nan?.should == true
|
||||
@infinity.send(@method, @frac_1).nan?.should == true
|
||||
|
||||
@infinity_minus.send(@method, @infinity_minus).nan?.should == true
|
||||
@infinity_minus.send(@method, @one).nan?.should == true
|
||||
|
||||
@infinity.send(@method, @infinity_minus).nan?.should == true
|
||||
@infinity_minus.send(@method, @infinity).nan?.should == true
|
||||
end
|
||||
|
||||
it "returns the dividend if the divisor is Infinity" do
|
||||
@one.send(@method, @infinity).should == @one
|
||||
@one.send(@method, @infinity_minus).should == @one
|
||||
@frac_2.send(@method, @infinity_minus).should == @frac_2
|
||||
end
|
||||
|
||||
it "raises TypeError if the argument cannot be coerced to BigDecimal" do
|
||||
lambda {
|
||||
@one.send(@method, '2')
|
||||
}.should raise_error(TypeError)
|
||||
end
|
||||
end
|
||||
|
||||
describe :bigdecimal_modulo_zerodivisionerror, shared: true do
|
||||
it "raises ZeroDivisionError if other is zero" do
|
||||
bd5667 = BigDecimal.new("5667.19")
|
||||
|
||||
lambda { bd5667.send(@method, 0) }.should raise_error(ZeroDivisionError)
|
||||
lambda { bd5667.send(@method, BigDecimal("0")) }.should raise_error(ZeroDivisionError)
|
||||
lambda { @zero.send(@method, @zero) }.should raise_error(ZeroDivisionError)
|
||||
end
|
||||
end
|
97
spec/ruby/library/bigdecimal/shared/mult.rb
Normal file
97
spec/ruby/library/bigdecimal/shared/mult.rb
Normal file
|
@ -0,0 +1,97 @@
|
|||
require 'bigdecimal'
|
||||
|
||||
describe :bigdecimal_mult, shared: true do
|
||||
before :each do
|
||||
@zero = BigDecimal "0"
|
||||
@zero_pos = BigDecimal "+0"
|
||||
@zero_neg = BigDecimal "-0"
|
||||
|
||||
@one = BigDecimal "1"
|
||||
@mixed = BigDecimal "1.23456789"
|
||||
@pos_int = BigDecimal "2E5555"
|
||||
@neg_int = BigDecimal "-2E5555"
|
||||
@pos_frac = BigDecimal "2E-9999"
|
||||
@neg_frac = BigDecimal "-2E-9999"
|
||||
@nan = BigDecimal "NaN"
|
||||
@infinity = BigDecimal "Infinity"
|
||||
@infinity_minus = BigDecimal "-Infinity"
|
||||
@one_minus = BigDecimal "-1"
|
||||
@frac_1 = BigDecimal "1E-99999"
|
||||
@frac_2 = BigDecimal "0.9E-99999"
|
||||
|
||||
@e3_minus = BigDecimal "3E-20001"
|
||||
@e = BigDecimal "1.00000000000000000000123456789"
|
||||
@tolerance = @e.sub @one, 1000
|
||||
@tolerance2 = BigDecimal "30001E-20005"
|
||||
|
||||
@special_vals = [@infinity, @infinity_minus, @nan]
|
||||
@regular_vals = [ @one, @mixed, @pos_int, @neg_int,
|
||||
@pos_frac, @neg_frac, @one_minus,
|
||||
@frac_1, @frac_2
|
||||
]
|
||||
@zeroes = [@zero, @zero_pos, @zero_neg]
|
||||
end
|
||||
|
||||
it "returns zero of appropriate sign if self or argument is zero" do
|
||||
@zero.send(@method, @zero, *@object).sign.should == BigDecimal::SIGN_POSITIVE_ZERO
|
||||
@zero_neg.send(@method, @zero_neg, *@object).sign.should == BigDecimal::SIGN_POSITIVE_ZERO
|
||||
@zero.send(@method, @zero_neg, *@object).sign.should == BigDecimal::SIGN_NEGATIVE_ZERO
|
||||
@zero_neg.send(@method, @zero, *@object).sign.should == BigDecimal::SIGN_NEGATIVE_ZERO
|
||||
|
||||
@one.send(@method, @zero, *@object).sign.should == BigDecimal::SIGN_POSITIVE_ZERO
|
||||
@one.send(@method, @zero_neg, *@object).sign.should == BigDecimal::SIGN_NEGATIVE_ZERO
|
||||
|
||||
@zero.send(@method, @one, *@object).sign.should == BigDecimal::SIGN_POSITIVE_ZERO
|
||||
@zero.send(@method, @one_minus, *@object).sign.should == BigDecimal::SIGN_NEGATIVE_ZERO
|
||||
@zero_neg.send(@method, @one_minus, *@object).sign.should == BigDecimal::SIGN_POSITIVE_ZERO
|
||||
@zero_neg.send(@method, @one, *@object).sign.should == BigDecimal::SIGN_NEGATIVE_ZERO
|
||||
end
|
||||
|
||||
it "returns NaN if NaN is involved" do
|
||||
values = @regular_vals + @zeroes
|
||||
|
||||
values.each do |val|
|
||||
@nan.send(@method, val, *@object).nan?.should == true
|
||||
val.send(@method, @nan, *@object).nan?.should == true
|
||||
end
|
||||
end
|
||||
|
||||
it "returns zero if self or argument is zero" do
|
||||
values = @regular_vals + @zeroes
|
||||
|
||||
values.each do |val|
|
||||
@zeroes.each do |zero|
|
||||
zero.send(@method, val, *@object).should == 0
|
||||
zero.send(@method, val, *@object).zero?.should == true
|
||||
val.send(@method, zero, *@object).should == 0
|
||||
val.send(@method, zero, *@object).zero?.should == true
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
it "returns infinite value if self or argument is infinite" do
|
||||
values = @regular_vals
|
||||
infs = [@infinity, @infinity_minus]
|
||||
|
||||
values.each do |val|
|
||||
infs.each do |inf|
|
||||
inf.send(@method, val, *@object).finite?.should == false
|
||||
val.send(@method, inf, *@object).finite?.should == false
|
||||
end
|
||||
end
|
||||
|
||||
@infinity.send(@method, @infinity, *@object).infinite?.should == 1
|
||||
@infinity_minus.send(@method, @infinity_minus, *@object).infinite?.should == 1
|
||||
@infinity.send(@method, @infinity_minus, *@object).infinite?.should == -1
|
||||
@infinity_minus.send(@method, @infinity, *@object).infinite?.should == -1
|
||||
@infinity.send(@method, @one, *@object).infinite?.should == 1
|
||||
@infinity_minus.send(@method, @one, *@object).infinite?.should == -1
|
||||
end
|
||||
|
||||
it "returns NaN if the result is undefined" do
|
||||
@zero.send(@method, @infinity, *@object).nan?.should == true
|
||||
@zero.send(@method, @infinity_minus, *@object).nan?.should == true
|
||||
@infinity.send(@method, @zero, *@object).nan?.should == true
|
||||
@infinity_minus.send(@method, @zero, *@object).nan?.should == true
|
||||
end
|
||||
end
|
72
spec/ruby/library/bigdecimal/shared/power.rb
Normal file
72
spec/ruby/library/bigdecimal/shared/power.rb
Normal file
|
@ -0,0 +1,72 @@
|
|||
require 'bigdecimal'
|
||||
|
||||
describe :bigdecimal_power, shared: true do
|
||||
it "powers of self" do
|
||||
e3_minus = BigDecimal("3E-20001")
|
||||
e3_minus_power_2 = BigDecimal("9E-40002")
|
||||
e3_plus = BigDecimal("3E20001")
|
||||
e2_plus = BigDecimal("2E40001")
|
||||
e5_minus = BigDecimal("5E-40002")
|
||||
e = BigDecimal("1.00000000000000000000123456789")
|
||||
one = BigDecimal("1")
|
||||
ten = BigDecimal("10")
|
||||
# The tolerance is dependent upon the size of BASE_FIG
|
||||
tolerance = BigDecimal("1E-70")
|
||||
ten_powers = BigDecimal("1E10000")
|
||||
pi = BigDecimal("3.14159265358979")
|
||||
e3_minus.send(@method, 2).should == e3_minus_power_2
|
||||
e3_plus.send(@method, 0).should == 1
|
||||
e3_minus.send(@method, 1).should == e3_minus
|
||||
e2_plus.send(@method, -1).should == e5_minus
|
||||
e2_plus.send(@method, -1).should == e5_minus.power(1)
|
||||
(e2_plus.send(@method, -1) * e5_minus.send(@method, -1)).should == 1
|
||||
e.send(@method, 2).should == e * e
|
||||
e.send(@method, -1).should be_close(one.div(e, 120), tolerance)
|
||||
ten.send(@method, 10000).should == ten_powers
|
||||
pi.send(@method, 10).should be_close(Math::PI ** 10, TOLERANCE)
|
||||
end
|
||||
|
||||
it "powers of 1 equal 1" do
|
||||
one = BigDecimal("1")
|
||||
one.send(@method, 0).should == 1
|
||||
one.send(@method, 1).should == 1
|
||||
one.send(@method, 10).should == 1
|
||||
one.send(@method, -10).should == 1
|
||||
end
|
||||
|
||||
it "0 to power of 0 is 1" do
|
||||
zero = BigDecimal("0")
|
||||
zero.send(@method, 0).should == 1
|
||||
end
|
||||
|
||||
it "0 to powers < 0 is Infinity" do
|
||||
zero = BigDecimal("0")
|
||||
infinity = BigDecimal("Infinity")
|
||||
zero.send(@method, -10).should == infinity
|
||||
zero.send(@method, -1).should == infinity
|
||||
end
|
||||
|
||||
it "other powers of 0 are 0" do
|
||||
zero = BigDecimal("0")
|
||||
zero.send(@method, 1).should == 0
|
||||
zero.send(@method, 10).should == 0
|
||||
end
|
||||
|
||||
it "returns NaN if self is NaN" do
|
||||
BigDecimal("NaN").send(@method, -5).nan?.should == true
|
||||
BigDecimal("NaN").send(@method, 5).nan?.should == true
|
||||
end
|
||||
|
||||
it "returns 0.0 if self is infinite and argument is negative" do
|
||||
BigDecimal("Infinity").send(@method, -5).should == 0
|
||||
BigDecimal("-Infinity").send(@method, -5).should == 0
|
||||
end
|
||||
|
||||
it "returns infinite if self is infinite and argument is positive" do
|
||||
infinity = BigDecimal("Infinity")
|
||||
BigDecimal("Infinity").send(@method, 4).should == infinity
|
||||
BigDecimal("-Infinity").send(@method, 4).should == infinity
|
||||
BigDecimal("Infinity").send(@method, 5).should == infinity
|
||||
BigDecimal("-Infinity").send(@method, 5).should == -infinity
|
||||
end
|
||||
end
|
59
spec/ruby/library/bigdecimal/shared/quo.rb
Normal file
59
spec/ruby/library/bigdecimal/shared/quo.rb
Normal file
|
@ -0,0 +1,59 @@
|
|||
require 'bigdecimal'
|
||||
|
||||
describe :bigdecimal_quo, shared: true do
|
||||
before :each do
|
||||
@one = BigDecimal("1")
|
||||
@zero = BigDecimal("0")
|
||||
@zero_plus = BigDecimal("+0")
|
||||
@zero_minus = BigDecimal("-0")
|
||||
@two = BigDecimal("2")
|
||||
@three = BigDecimal("3")
|
||||
@eleven = BigDecimal("11")
|
||||
@nan = BigDecimal("NaN")
|
||||
@infinity = BigDecimal("Infinity")
|
||||
@infinity_minus = BigDecimal("-Infinity")
|
||||
@one_minus = BigDecimal("-1")
|
||||
@frac_1 = BigDecimal("1E-99999")
|
||||
@frac_2 = BigDecimal("0.9E-99999")
|
||||
end
|
||||
|
||||
it "returns a / b" do
|
||||
@two.send(@method, @one, *@object).should == @two
|
||||
@one.send(@method, @two, *@object).should == BigDecimal("0.5")
|
||||
@eleven.send(@method, @three, *@object).should be_close(@three + (@two / @three), TOLERANCE)
|
||||
@one.send(@method, @one_minus, *@object).should == @one_minus
|
||||
@one_minus.send(@method, @one_minus, *@object).should == @one
|
||||
@frac_2.send(@method, @frac_1, *@object).should == BigDecimal("0.9")
|
||||
@frac_1.send(@method, @frac_1, *@object).should == @one
|
||||
@one.send(@method, BigDecimal('-2E5555'), *@object).should == BigDecimal('-0.5E-5555')
|
||||
@one.send(@method, BigDecimal('2E-5555'), *@object).should == BigDecimal('0.5E5555')
|
||||
end
|
||||
|
||||
it "returns 0 if divided by Infinity" do
|
||||
@zero.send(@method, @infinity, *@object).should == 0
|
||||
@frac_2.send(@method, @infinity, *@object).should == 0
|
||||
end
|
||||
|
||||
it "returns (+|-) Infinity if (+|-) Infinity divided by one" do
|
||||
@infinity_minus.send(@method, @one, *@object).should == @infinity_minus
|
||||
@infinity.send(@method, @one, *@object).should == @infinity
|
||||
@infinity_minus.send(@method, @one_minus, *@object).should == @infinity
|
||||
end
|
||||
|
||||
it "returns NaN if Infinity / ((+|-) Infinity)" do
|
||||
@infinity.send(@method, @infinity_minus, *@object).nan?.should == true
|
||||
@infinity_minus.send(@method, @infinity, *@object).nan?.should == true
|
||||
end
|
||||
|
||||
it "returns (+|-) Infinity if divided by zero" do
|
||||
@one.send(@method, @zero, *@object).should == @infinity
|
||||
@one.send(@method, @zero_plus, *@object).should == @infinity
|
||||
@one.send(@method, @zero_minus, *@object).should == @infinity_minus
|
||||
end
|
||||
|
||||
it "returns NaN if zero is divided by zero" do
|
||||
@zero.send(@method, @zero, *@object).nan?.should == true
|
||||
@zero_minus.send(@method, @zero_plus, *@object).nan?.should == true
|
||||
@zero_plus.send(@method, @zero_minus, *@object).nan?.should == true
|
||||
end
|
||||
end
|
16
spec/ruby/library/bigdecimal/shared/to_int.rb
Normal file
16
spec/ruby/library/bigdecimal/shared/to_int.rb
Normal file
|
@ -0,0 +1,16 @@
|
|||
require 'bigdecimal'
|
||||
|
||||
describe :bigdecimal_to_int , shared: true do
|
||||
it "raises FloatDomainError if BigDecimal is infinity or NaN" do
|
||||
lambda { BigDecimal("Infinity").send(@method) }.should raise_error(FloatDomainError)
|
||||
lambda { BigDecimal("NaN").send(@method) }.should raise_error(FloatDomainError)
|
||||
end
|
||||
|
||||
it "returns Integer or Bignum otherwise" do
|
||||
BigDecimal("3E-20001").send(@method).should == 0
|
||||
BigDecimal("2E4000").send(@method).should == 2 * 10 ** 4000
|
||||
BigDecimal("2").send(@method).should == 2
|
||||
BigDecimal("2E10").send(@method).should == 20000000000
|
||||
BigDecimal("3.14159").send(@method).should == 3
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue