diff options
author | eregon <eregon@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2018-01-29 16:08:16 +0000 |
---|---|---|
committer | eregon <eregon@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2018-01-29 16:08:16 +0000 |
commit | 3fa5bd38af50fb3d98de0ea51043d73f8d06a24b (patch) | |
tree | d473b71cc6925ee1e17727215e9f9a66e3f24802 /spec/ruby/core/integer/shared | |
parent | 1e658d45e1f8dbadab18f9c35b5cfb5a5fec98bf (diff) | |
download | ruby-3fa5bd38af50fb3d98de0ea51043d73f8d06a24b.tar.gz |
Update to ruby/spec@83063a3
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62094 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'spec/ruby/core/integer/shared')
-rw-r--r-- | spec/ruby/core/integer/shared/abs.rb | 18 | ||||
-rw-r--r-- | spec/ruby/core/integer/shared/arithmetic_coerce.rb | 31 | ||||
-rw-r--r-- | spec/ruby/core/integer/shared/arithmetic_exception_in_coerce.rb | 33 | ||||
-rw-r--r-- | spec/ruby/core/integer/shared/comparison_coerce.rb | 33 | ||||
-rw-r--r-- | spec/ruby/core/integer/shared/comparison_exception_in_coerce.rb | 35 | ||||
-rw-r--r-- | spec/ruby/core/integer/shared/equal.rb | 58 | ||||
-rw-r--r-- | spec/ruby/core/integer/shared/exponent.rb | 118 | ||||
-rw-r--r-- | spec/ruby/core/integer/shared/modulo.rb | 74 |
8 files changed, 332 insertions, 68 deletions
diff --git a/spec/ruby/core/integer/shared/abs.rb b/spec/ruby/core/integer/shared/abs.rb new file mode 100644 index 0000000000..946aa21864 --- /dev/null +++ b/spec/ruby/core/integer/shared/abs.rb @@ -0,0 +1,18 @@ +describe :integer_abs, shared: true do + context "fixnum" do + it "returns self's absolute fixnum value" do + { 0 => [0, -0, +0], 2 => [2, -2, +2], 100 => [100, -100, +100] }.each do |key, values| + values.each do |value| + value.send(@method).should == key + end + end + end + end + + context "bignum" do + it "returns the absolute bignum value" do + bignum_value(39).send(@method).should == 9223372036854775847 + (-bignum_value(18)).send(@method).should == 9223372036854775826 + end + end +end diff --git a/spec/ruby/core/integer/shared/arithmetic_coerce.rb b/spec/ruby/core/integer/shared/arithmetic_coerce.rb new file mode 100644 index 0000000000..e33c5ff0f4 --- /dev/null +++ b/spec/ruby/core/integer/shared/arithmetic_coerce.rb @@ -0,0 +1,31 @@ +require File.expand_path('../../fixtures/classes', __FILE__) + +describe :integer_arithmetic_coerce_rescue, shared: true do + it "rescues exception (StandardError and subclasses) raised in other#coerce and raises TypeError" do + b = mock("numeric with failed #coerce") + b.should_receive(:coerce).and_raise(IntegerSpecs::CoerceError) + + # e.g. 1 + b + -> { 1.send(@method, b) }.should raise_error(TypeError, /MockObject can't be coerced into Integer/) + end + + it "does not rescue Exception and StandardError siblings raised in other#coerce" do + [Exception, NoMemoryError].each do |exception| + b = mock("numeric with failed #coerce") + b.should_receive(:coerce).and_raise(exception) + + # e.g. 1 + b + -> { 1.send(@method, b) }.should raise_error(exception) + end + end +end + +describe :integer_arithmetic_coerce_not_rescue, shared: true do + it "does not rescue exception raised in other#coerce" do + b = mock("numeric with failed #coerce") + b.should_receive(:coerce).and_raise(IntegerSpecs::CoerceError) + + # e.g. 1 + b + -> { 1.send(@method, b) }.should raise_error(IntegerSpecs::CoerceError) + end +end diff --git a/spec/ruby/core/integer/shared/arithmetic_exception_in_coerce.rb b/spec/ruby/core/integer/shared/arithmetic_exception_in_coerce.rb deleted file mode 100644 index dd3f55d444..0000000000 --- a/spec/ruby/core/integer/shared/arithmetic_exception_in_coerce.rb +++ /dev/null @@ -1,33 +0,0 @@ -require File.expand_path('../../fixtures/classes', __FILE__) - -describe :integer_arithmetic_exception_in_coerce, shared: true do - ruby_version_is ""..."2.5" do - it "rescues exception (StandardError and subclasses) raised in other#coerce and raises TypeError" do - b = mock("numeric with failed #coerce") - b.should_receive(:coerce).and_raise(IntegerSpecs::CoerceError) - - # e.g. 1 + b - -> { 1.send(@method, b) }.should raise_error(TypeError, /MockObject can't be coerced into #{1.class}/) - end - - it "does not rescue Exception and StandardError siblings raised in other#coerce" do - [Exception, NoMemoryError].each do |exception| - b = mock("numeric with failed #coerce") - b.should_receive(:coerce).and_raise(exception) - - # e.g. 1 + b - -> { 1.send(@method, b) }.should raise_error(exception) - end - end - end - - ruby_version_is "2.5" do - it "does not rescue exception raised in other#coerce" do - b = mock("numeric with failed #coerce") - b.should_receive(:coerce).and_raise(IntegerSpecs::CoerceError) - - # e.g. 1 + b - -> { 1.send(@method, b) }.should raise_error(IntegerSpecs::CoerceError) - end - end -end diff --git a/spec/ruby/core/integer/shared/comparison_coerce.rb b/spec/ruby/core/integer/shared/comparison_coerce.rb new file mode 100644 index 0000000000..6d5e69b774 --- /dev/null +++ b/spec/ruby/core/integer/shared/comparison_coerce.rb @@ -0,0 +1,33 @@ +require File.expand_path('../../fixtures/classes', __FILE__) + +describe :integer_comparison_coerce_rescue, shared: true do + it "rescues exception (StandardError and subclasses) raised in other#coerce and raises ArgumentError" do + b = mock("numeric with failed #coerce") + b.should_receive(:coerce).and_raise(IntegerSpecs::CoerceError) + + # e.g. 1 > b + -> { + -> { 1.send(@method, b) }.should raise_error(ArgumentError, /comparison of Integer with MockObject failed/) + }.should complain(/Numerical comparison operators will no more rescue exceptions of #coerce/) + end + + it "does not rescue Exception and StandardError siblings raised in other#coerce" do + [Exception, NoMemoryError].each do |exception| + b = mock("numeric with failed #coerce") + b.should_receive(:coerce).and_raise(exception) + + # e.g. 1 > b + -> { 1.send(@method, b) }.should raise_error(exception) + end + end +end + +describe :integer_comparison_coerce_not_rescue, shared: true do + it "does not rescue exception raised in other#coerce" do + b = mock("numeric with failed #coerce") + b.should_receive(:coerce).and_raise(IntegerSpecs::CoerceError) + + # e.g. 1 > b + -> { 1.send(@method, b) }.should raise_error(IntegerSpecs::CoerceError) + end +end diff --git a/spec/ruby/core/integer/shared/comparison_exception_in_coerce.rb b/spec/ruby/core/integer/shared/comparison_exception_in_coerce.rb deleted file mode 100644 index f99d680d6d..0000000000 --- a/spec/ruby/core/integer/shared/comparison_exception_in_coerce.rb +++ /dev/null @@ -1,35 +0,0 @@ -require File.expand_path('../../fixtures/classes', __FILE__) - -describe :integer_comparison_exception_in_coerce, shared: true do - ruby_version_is ""..."2.5" do - it "rescues exception (StandardError and subclasses) raised in other#coerce and raises ArgumentError" do - b = mock("numeric with failed #coerce") - b.should_receive(:coerce).and_raise(IntegerSpecs::CoerceError) - - # e.g. 1 > b - -> { - -> { 1.send(@method, b) }.should raise_error(ArgumentError, /comparison of #{1.class} with MockObject failed/) - }.should complain(/Numerical comparison operators will no more rescue exceptions of #coerce/) - end - - it "does not rescue Exception and StandardError siblings raised in other#coerce" do - [Exception, NoMemoryError].each do |exception| - b = mock("numeric with failed #coerce") - b.should_receive(:coerce).and_raise(exception) - - # e.g. 1 > b - -> { 1.send(@method, b) }.should raise_error(exception) - end - end - end - - ruby_version_is "2.5" do - it "does not rescue exception raised in other#coerce" do - b = mock("numeric with failed #coerce") - b.should_receive(:coerce).and_raise(IntegerSpecs::CoerceError) - - # e.g. 1 > b - -> { 1.send(@method, b) }.should raise_error(IntegerSpecs::CoerceError) - end - end -end diff --git a/spec/ruby/core/integer/shared/equal.rb b/spec/ruby/core/integer/shared/equal.rb new file mode 100644 index 0000000000..03416b60f5 --- /dev/null +++ b/spec/ruby/core/integer/shared/equal.rb @@ -0,0 +1,58 @@ +describe :integer_equal, shared: true do + context "fixnum" do + it "returns true if self has the same value as other" do + 1.send(@method, 1).should == true + 9.send(@method, 5).should == false + + # Actually, these call Float#==, Bignum#== etc. + 9.send(@method, 9.0).should == true + 9.send(@method, 9.01).should == false + + 10.send(@method, bignum_value).should == false + end + + it "calls 'other == self' if the given argument is not a Integer" do + 1.send(@method, '*').should == false + + obj = mock('one other') + obj.should_receive(:==).any_number_of_times.and_return(false) + 1.send(@method, obj).should == false + + obj = mock('another') + obj.should_receive(:==).any_number_of_times.and_return(true) + 2.send(@method, obj).should == true + end + end + + context "bignum" do + before :each do + @bignum = bignum_value + end + + it "returns true if self has the same value as the given argument" do + @bignum.send(@method, @bignum).should == true + @bignum.send(@method, @bignum.to_f).should == true + + @bignum.send(@method, @bignum + 1).should == false + (@bignum + 1).send(@method, @bignum).should == false + + @bignum.send(@method, 9).should == false + @bignum.send(@method, 9.01).should == false + + @bignum.send(@method, bignum_value(10)).should == false + end + + it "calls 'other == self' if the given argument is not an Integer" do + obj = mock('not integer') + obj.should_receive(:==).and_return(true) + @bignum.send(@method, obj).should == true + end + + it "returns the result of 'other == self' as a boolean" do + obj = mock('not integer') + obj.should_receive(:==).exactly(2).times.and_return("woot", nil) + @bignum.send(@method, obj).should == true + @bignum.send(@method, obj).should == false + end + end +end diff --git a/spec/ruby/core/integer/shared/exponent.rb b/spec/ruby/core/integer/shared/exponent.rb new file mode 100644 index 0000000000..810e2a7101 --- /dev/null +++ b/spec/ruby/core/integer/shared/exponent.rb @@ -0,0 +1,118 @@ +describe :integer_exponent, shared: true do + context "fixnum" do + it "returns self raised to the given power" do + 2.send(@method, 0).should eql 1 + 2.send(@method, 1).should eql 2 + 2.send(@method, 2).should eql 4 + + 9.send(@method, 0.5).should eql 3.0 + 9.send(@method, Rational(1, 2)).should eql 3.0 + 5.send(@method, -1).to_f.to_s.should == '0.2' + + 2.send(@method, 40).should eql 1099511627776 + end + + it "overflows the answer to a bignum transparently" do + 2.send(@method, 29).should eql 536870912 + 2.send(@method, 30).should eql 1073741824 + 2.send(@method, 31).should eql 2147483648 + 2.send(@method, 32).should eql 4294967296 + + 2.send(@method, 61).should eql 2305843009213693952 + 2.send(@method, 62).should eql 4611686018427387904 + 2.send(@method, 63).should eql 9223372036854775808 + 2.send(@method, 64).should eql 18446744073709551616 + 8.send(@method, 23).should eql 590295810358705651712 + end + + it "raises negative numbers to the given power" do + (-2).send(@method, 29).should eql(-536870912) + (-2).send(@method, 30).should eql(1073741824) + (-2).send(@method, 31).should eql(-2147483648) + (-2).send(@method, 32).should eql(4294967296) + + (-2).send(@method, 61).should eql(-2305843009213693952) + (-2).send(@method, 62).should eql(4611686018427387904) + (-2).send(@method, 63).should eql(-9223372036854775808) + (-2).send(@method, 64).should eql(18446744073709551616) + end + + it "can raise 1 to a bignum safely" do + 1.send(@method, 4611686018427387904).should eql 1 + end + + it "can raise -1 to a bignum safely" do + (-1).send(@method, 4611686018427387904).should eql(1) + (-1).send(@method, 4611686018427387905).should eql(-1) + end + + it "returns Float::INFINITY when the number is too big" do + 2.send(@method, 427387904).should == Float::INFINITY + end + + it "raises a ZeroDivisionError for 0 ** -1" do + -> { 0.send(@method, -1) }.should raise_error(ZeroDivisionError) + -> { 0.send(@method, Rational(-1, 1)) }.should raise_error(ZeroDivisionError) + end + + it "returns Float::INFINITY for 0 ** -1.0" do + 0.send(@method, -1.0).should == Float::INFINITY + end + + it "raises a TypeError when given a non-numeric power" do + -> { 13.send(@method, "10") }.should raise_error(TypeError) + -> { 13.send(@method, :symbol) }.should raise_error(TypeError) + -> { 13.send(@method, nil) }.should raise_error(TypeError) + end + + it "coerces power and calls #**" do + num_2 = mock("2") + num_13 = mock("13") + num_2.should_receive(:coerce).with(13).and_return([num_13, num_2]) + num_13.should_receive(:**).with(num_2).and_return(169) + + 13.send(@method, num_2).should == 169 + end + + it "returns Float when power is Float" do + 2.send(@method, 2.0).should == 4.0 + end + + it "returns Rational when power is Rational" do + 2.send(@method, Rational(2, 1)).should == Rational(4, 1) + end + + it "returns a complex number when negative and raised to a fractional power" do + (-8).send(@method, 1.0/3) .should be_close(Complex(1, 1.73205), TOLERANCE) + (-8).send(@method, Rational(1, 3)).should be_close(Complex(1, 1.73205), TOLERANCE) + end + end + + context "bignum" do + before :each do + @bignum = bignum_value(47) + end + + it "returns self raised to other power" do + (@bignum.send(@method, 4)).should == 7237005577332262361485077344629993318496048279512298547155833600056910050625 + (@bignum.send(@method, 1.2)).should be_close(57262152889751597425762.57804, TOLERANCE) + end + + it "raises a TypeError when given a non-Integer" do + lambda { @bignum.send(@method, mock('10')) }.should raise_error(TypeError) + lambda { @bignum.send(@method, "10") }.should raise_error(TypeError) + lambda { @bignum.send(@method, :symbol) }.should raise_error(TypeError) + end + + it "switch to a Float when the values is too big" do + flt = @bignum.send(@method, @bignum) + flt.should be_kind_of(Float) + flt.infinite?.should == 1 + end + + it "returns a complex number when negative and raised to a fractional power" do + ((-@bignum).send(@method, (1.0/3))) .should be_close(Complex(1048576,1816186.907597341), TOLERANCE) + ((-@bignum).send(@method, Rational(1,3))).should be_close(Complex(1048576,1816186.907597341), TOLERANCE) + end + end +end diff --git a/spec/ruby/core/integer/shared/modulo.rb b/spec/ruby/core/integer/shared/modulo.rb new file mode 100644 index 0000000000..d545a9af55 --- /dev/null +++ b/spec/ruby/core/integer/shared/modulo.rb @@ -0,0 +1,74 @@ +describe :integer_modulo, shared: true do + context "fixnum" do + it "returns the modulus obtained from dividing self by the given argument" do + 13.send(@method, 4).should == 1 + 4.send(@method, 13).should == 4 + + 13.send(@method, 4.0).should == 1 + 4.send(@method, 13.0).should == 4 + + (-200).send(@method, 256).should == 56 + (-1000).send(@method, 512).should == 24 + + (-200).send(@method, -256).should == -200 + (-1000).send(@method, -512).should == -488 + + (200).send(@method, -256).should == -56 + (1000).send(@method, -512).should == -24 + + 1.send(@method, 2.0).should == 1.0 + 200.send(@method, bignum_value).should == 200 + end + + it "raises a ZeroDivisionError when the given argument is 0" do + lambda { 13.send(@method, 0) }.should raise_error(ZeroDivisionError) + lambda { 0.send(@method, 0) }.should raise_error(ZeroDivisionError) + lambda { -10.send(@method, 0) }.should raise_error(ZeroDivisionError) + end + + it "raises a ZeroDivisionError when the given argument is 0 and a Float" do + lambda { 0.send(@method, 0.0) }.should raise_error(ZeroDivisionError) + lambda { 10.send(@method, 0.0) }.should raise_error(ZeroDivisionError) + lambda { -10.send(@method, 0.0) }.should raise_error(ZeroDivisionError) + end + + it "raises a TypeError when given a non-Integer" do + lambda { + (obj = mock('10')).should_receive(:to_int).any_number_of_times.and_return(10) + 13.send(@method, obj) + }.should raise_error(TypeError) + lambda { 13.send(@method, "10") }.should raise_error(TypeError) + lambda { 13.send(@method, :symbol) }.should raise_error(TypeError) + end + end + + context "bignum" do + before :each do + @bignum = bignum_value + end + + it "returns the modulus obtained from dividing self by the given argument" do + @bignum.send(@method, 5).should == 3 + @bignum.send(@method, -5).should == -2 + @bignum.send(@method, -100).should == -92 + @bignum.send(@method, 2.22).should be_close(0.780180180180252, TOLERANCE) + @bignum.send(@method, bignum_value(10)).should == 9223372036854775808 + end + + it "raises a ZeroDivisionError when the given argument is 0" do + lambda { @bignum.send(@method, 0) }.should raise_error(ZeroDivisionError) + lambda { (-@bignum).send(@method, 0) }.should raise_error(ZeroDivisionError) + end + + it "raises a ZeroDivisionError when the given argument is 0 and a Float" do + lambda { @bignum.send(@method, 0.0) }.should raise_error(ZeroDivisionError) + lambda { -@bignum.send(@method, 0.0) }.should raise_error(ZeroDivisionError) + end + + it "raises a TypeError when given a non-Integer" do + lambda { @bignum.send(@method, mock('10')) }.should raise_error(TypeError) + lambda { @bignum.send(@method, "10") }.should raise_error(TypeError) + lambda { @bignum.send(@method, :symbol) }.should raise_error(TypeError) + end + end +end |