diff options
Diffstat (limited to 'spec/ruby/core/complex')
39 files changed, 345 insertions, 337 deletions
diff --git a/spec/ruby/core/complex/abs_spec.rb b/spec/ruby/core/complex/abs_spec.rb index 43912c517f..ed5aebb11d 100644 --- a/spec/ruby/core/complex/abs_spec.rb +++ b/spec/ruby/core/complex/abs_spec.rb @@ -1,6 +1,12 @@ require_relative '../../spec_helper' -require_relative 'shared/abs' describe "Complex#abs" do - it_behaves_like :complex_abs, :abs + it "returns the modulus: |a + bi| = sqrt((a ^ 2) + (b ^ 2))" do + Complex(0, 0).abs.should == 0 + Complex(3, 4).abs.should == 5 # well-known integer case + Complex(-3, 4).abs.should == 5 + Complex(1, -1).abs.should be_close(Math.sqrt(2), TOLERANCE) + Complex(6.5, 0).abs.should be_close(6.5, TOLERANCE) + Complex(0, -7.2).abs.should be_close(7.2, TOLERANCE) + end end diff --git a/spec/ruby/core/complex/angle_spec.rb b/spec/ruby/core/complex/angle_spec.rb index 4aa176956f..7551214d2b 100644 --- a/spec/ruby/core/complex/angle_spec.rb +++ b/spec/ruby/core/complex/angle_spec.rb @@ -1,6 +1,7 @@ require_relative '../../spec_helper' -require_relative 'shared/arg' describe "Complex#angle" do - it_behaves_like :complex_arg, :angle + it "is an alias of Complex#arg" do + Complex.instance_method(:angle).should == Complex.instance_method(:arg) + end end diff --git a/spec/ruby/core/complex/arg_spec.rb b/spec/ruby/core/complex/arg_spec.rb index 009f19429f..dd64102d77 100644 --- a/spec/ruby/core/complex/arg_spec.rb +++ b/spec/ruby/core/complex/arg_spec.rb @@ -1,6 +1,11 @@ require_relative '../../spec_helper' -require_relative 'shared/arg' describe "Complex#arg" do - it_behaves_like :complex_arg, :arg + it "returns the argument -- i.e., the angle from (1, 0) in the complex plane" do + two_pi = 2 * Math::PI + (Complex(1, 0).arg % two_pi).should be_close(0, TOLERANCE) + (Complex(0, 2).arg % two_pi).should be_close(Math::PI * 0.5, TOLERANCE) + (Complex(-100, 0).arg % two_pi).should be_close(Math::PI, TOLERANCE) + (Complex(0, -75.3).arg % two_pi).should be_close(Math::PI * 1.5, TOLERANCE) + end end diff --git a/spec/ruby/core/complex/coerce_spec.rb b/spec/ruby/core/complex/coerce_spec.rb index a30a6c1d5f..d4ea85a713 100644 --- a/spec/ruby/core/complex/coerce_spec.rb +++ b/spec/ruby/core/complex/coerce_spec.rb @@ -8,37 +8,37 @@ describe "Complex#coerce" do it "returns an array containing other and self as Complex when other is an Integer" do result = @one.coerce(2) result.should == [2, 1] - result.first.should be_kind_of(Complex) - result.last.should be_kind_of(Complex) + result.first.should.is_a?(Complex) + result.last.should.is_a?(Complex) end it "returns an array containing other and self as Complex when other is a Float" do result = @one.coerce(20.5) result.should == [20.5, 1] - result.first.should be_kind_of(Complex) - result.last.should be_kind_of(Complex) + result.first.should.is_a?(Complex) + result.last.should.is_a?(Complex) end it "returns an array containing other and self as Complex when other is a Bignum" do result = @one.coerce(4294967296) result.should == [4294967296, 1] - result.first.should be_kind_of(Complex) - result.last.should be_kind_of(Complex) + result.first.should.is_a?(Complex) + result.last.should.is_a?(Complex) end it "returns an array containing other and self as Complex when other is a Rational" do result = @one.coerce(Rational(5,6)) result.should == [Rational(5,6), 1] - result.first.should be_kind_of(Complex) - result.last.should be_kind_of(Complex) + result.first.should.is_a?(Complex) + result.last.should.is_a?(Complex) end it "returns an array containing other and self when other is a Complex" do other = Complex(2) result = @one.coerce(other) result.should == [other, @one] - result.first.should equal(other) - result.last.should equal(@one) + result.first.should.equal?(other) + result.last.should.equal?(@one) end it "returns an array containing other as Complex and self when other is a Numeric which responds to #real? with true" do @@ -46,25 +46,25 @@ describe "Complex#coerce" do other.should_receive(:real?).any_number_of_times.and_return(true) result = @one.coerce(other) result.should == [other, @one] - result.first.should eql(Complex(other)) - result.last.should equal(@one) + result.first.should.eql?(Complex(other)) + result.last.should.equal?(@one) end it "raises TypeError when other is a Numeric which responds to #real? with false" do other = mock_numeric('other') other.should_receive(:real?).any_number_of_times.and_return(false) - -> { @one.coerce(other) }.should raise_error(TypeError) + -> { @one.coerce(other) }.should.raise(TypeError) end it "raises a TypeError when other is a String" do - -> { @one.coerce("20") }.should raise_error(TypeError) + -> { @one.coerce("20") }.should.raise(TypeError) end it "raises a TypeError when other is nil" do - -> { @one.coerce(nil) }.should raise_error(TypeError) + -> { @one.coerce(nil) }.should.raise(TypeError) end it "raises a TypeError when other is false" do - -> { @one.coerce(false) }.should raise_error(TypeError) + -> { @one.coerce(false) }.should.raise(TypeError) end end diff --git a/spec/ruby/core/complex/comparison_spec.rb b/spec/ruby/core/complex/comparison_spec.rb index 3a3142f234..3115f4e205 100644 --- a/spec/ruby/core/complex/comparison_spec.rb +++ b/spec/ruby/core/complex/comparison_spec.rb @@ -2,15 +2,15 @@ require_relative '../../spec_helper' describe "Complex#<=>" do it "returns nil if either self or argument has imaginary part" do - (Complex(5, 1) <=> Complex(2)).should be_nil - (Complex(1) <=> Complex(2, 1)).should be_nil - (5 <=> Complex(2, 1)).should be_nil + (Complex(5, 1) <=> Complex(2)).should == nil + (Complex(1) <=> Complex(2, 1)).should == nil + (5 <=> Complex(2, 1)).should == nil end it "returns nil if argument is not numeric" do - (Complex(5, 1) <=> "cmp").should be_nil - (Complex(1) <=> "cmp").should be_nil - (Complex(1) <=> Object.new).should be_nil + (Complex(5, 1) <=> "cmp").should == nil + (Complex(1) <=> "cmp").should == nil + (Complex(1) <=> Object.new).should == nil end it "returns 0, 1, or -1 if self and argument do not have imaginary part" do diff --git a/spec/ruby/core/complex/conj_spec.rb b/spec/ruby/core/complex/conj_spec.rb index 5e3bc1acb8..063c85faec 100644 --- a/spec/ruby/core/complex/conj_spec.rb +++ b/spec/ruby/core/complex/conj_spec.rb @@ -1,6 +1,7 @@ require_relative '../../spec_helper' -require_relative 'shared/conjugate' describe "Complex#conj" do - it_behaves_like :complex_conjugate, :conj + it "is an alias of Complex#conjugate" do + Complex.instance_method(:conj).should == Complex.instance_method(:conjugate) + end end diff --git a/spec/ruby/core/complex/conjugate_spec.rb b/spec/ruby/core/complex/conjugate_spec.rb index f658bab4da..256fe4b3be 100644 --- a/spec/ruby/core/complex/conjugate_spec.rb +++ b/spec/ruby/core/complex/conjugate_spec.rb @@ -1,6 +1,10 @@ require_relative '../../spec_helper' -require_relative 'shared/conjugate' describe "Complex#conjugate" do - it_behaves_like :complex_conjugate, :conjugate + it "returns the complex conjugate: conj a + bi = a - bi" do + Complex(3, 5).conjugate.should == Complex(3, -5) + Complex(3, -5).conjugate.should == Complex(3, 5) + Complex(-3.0, 5.2).conjugate.should be_close(Complex(-3.0, -5.2), TOLERANCE) + Complex(3.0, -5.2).conjugate.should be_close(Complex(3.0, 5.2), TOLERANCE) + end end diff --git a/spec/ruby/core/complex/constants_spec.rb b/spec/ruby/core/complex/constants_spec.rb index 50303de16c..200e97731a 100644 --- a/spec/ruby/core/complex/constants_spec.rb +++ b/spec/ruby/core/complex/constants_spec.rb @@ -2,6 +2,6 @@ require_relative '../../spec_helper' describe "Complex::I" do it "is Complex(0, 1)" do - Complex::I.should eql(Complex(0, 1)) + Complex::I.should.eql?(Complex(0, 1)) end end diff --git a/spec/ruby/core/complex/divide_spec.rb b/spec/ruby/core/complex/divide_spec.rb index bebf862312..d5b4aa3885 100644 --- a/spec/ruby/core/complex/divide_spec.rb +++ b/spec/ruby/core/complex/divide_spec.rb @@ -1,6 +1,84 @@ require_relative '../../spec_helper' -require_relative 'shared/divide' describe "Complex#/" do - it_behaves_like :complex_divide, :/ + describe "with Complex" do + it "divides according to the usual rule for complex numbers" do + a = Complex((1 * 10) - (2 * 20), (1 * 20) + (2 * 10)) + b = Complex(1, 2) + (a / b).should == Complex(10, 20) + + c = Complex((1.5 * 100.2) - (2.1 * -30.3), (1.5 * -30.3) + (2.1 * 100.2)) + d = Complex(1.5, 2.1) + # remember the floating-point arithmetic + (c / d).should be_close(Complex(100.2, -30.3), TOLERANCE) + end + end + + describe "with Fixnum" do + it "divides both parts of the Complex number" do + (Complex(20, 40) / 2).should == Complex(10, 20) + (Complex(30, 30) / 10).should == Complex(3, 3) + end + + it "raises a ZeroDivisionError when given zero" do + -> { Complex(20, 40) / 0 }.should.raise(ZeroDivisionError) + end + + it "produces Rational parts" do + (Complex(5, 9) / 2).should.eql?(Complex(Rational(5,2), Rational(9,2))) + end + end + + describe "with Bignum" do + it "divides both parts of the Complex number" do + (Complex(20, 40) / 2).should == Complex(10, 20) + (Complex(15, 16) / 2.0).should be_close(Complex(7.5, 8), TOLERANCE) + end + end + + describe "with Float" do + it "divides both parts of the Complex number" do + (Complex(3, 9) / 1.5).should == Complex(2, 6) + (Complex(15, 16) / 2.0).should be_close(Complex(7.5, 8), TOLERANCE) + end + + it "returns Complex(Infinity, Infinity) when given zero" do + (Complex(20, 40) / 0.0).real.infinite?.should == 1 + (Complex(20, 40) / 0.0).imag.infinite?.should == 1 + (Complex(-20, 40) / 0.0).real.infinite?.should == -1 + (Complex(-20, 40) / 0.0).imag.infinite?.should == 1 + end + end + + describe "with Object" do + it "tries to coerce self into other" do + value = Complex(3, 9) + + obj = mock("Object") + obj.should_receive(:coerce).with(value).and_return([4, 2]) + (value / obj).should == 2 + end + end + + describe "with a Numeric which responds to #real? with true" do + it "returns Complex(real.quo(other), imag.quo(other))" do + other = mock_numeric('other') + real = mock_numeric('real') + imag = mock_numeric('imag') + other.should_receive(:real?).and_return(true) + real.should_receive(:quo).with(other).and_return(1) + imag.should_receive(:quo).with(other).and_return(2) + (Complex(real, imag) / other).should == Complex(1, 2) + end + end + + describe "with a Numeric which responds to #real? with false" do + it "coerces the passed argument to Complex and divides the resulting elements" do + complex = Complex(3, 0) + other = mock_numeric('other') + other.should_receive(:real?).any_number_of_times.and_return(false) + other.should_receive(:coerce).with(complex).and_return([5, 2]) + (complex / other).should.eql?(Rational(5, 2)) + end + end end diff --git a/spec/ruby/core/complex/eql_spec.rb b/spec/ruby/core/complex/eql_spec.rb index 9194efc074..2082a22feb 100644 --- a/spec/ruby/core/complex/eql_spec.rb +++ b/spec/ruby/core/complex/eql_spec.rb @@ -2,23 +2,23 @@ require_relative '../../spec_helper' describe "Complex#eql?" do it "returns false if other is not Complex" do - Complex(1).eql?(1).should be_false + Complex(1).eql?(1).should == false end it "returns true when the respective parts are of the same classes and self == other" do - Complex(1, 2).eql?(Complex(1, 2)).should be_true + Complex(1, 2).eql?(Complex(1, 2)).should == true end it "returns false when the real parts are of different classes" do - Complex(1).eql?(Complex(1.0)).should be_false + Complex(1).eql?(Complex(1.0)).should == false end it "returns false when the imaginary parts are of different classes" do - Complex(1, 2).eql?(Complex(1, 2.0)).should be_false + Complex(1, 2).eql?(Complex(1, 2.0)).should == false end it "returns false when self == other is false" do - Complex(1, 2).eql?(Complex(2, 3)).should be_false + Complex(1, 2).eql?(Complex(2, 3)).should == false end it "does NOT send #eql? to real or imaginary parts" do @@ -26,6 +26,6 @@ describe "Complex#eql?" do imag = mock_numeric('imag') real.should_not_receive(:eql?) imag.should_not_receive(:eql?) - Complex(real, imag).eql?(Complex(real, imag)).should be_true + Complex(real, imag).eql?(Complex(real, imag)).should == true end end diff --git a/spec/ruby/core/complex/equal_value_spec.rb b/spec/ruby/core/complex/equal_value_spec.rb index ad7236b1bd..b3562ff3fb 100644 --- a/spec/ruby/core/complex/equal_value_spec.rb +++ b/spec/ruby/core/complex/equal_value_spec.rb @@ -60,7 +60,7 @@ describe "Complex#==" do obj = mock("Object") obj.should_receive(:==).with(value).and_return(:expected) - (value == obj).should_not be_false + (value == obj).should_not == false end end @@ -73,11 +73,11 @@ describe "Complex#==" do it "returns real == other when the imaginary part is zero" do real = mock_numeric('real') real.should_receive(:==).with(@other).and_return(true) - (Complex(real, 0) == @other).should be_true + (Complex(real, 0) == @other).should == true end - it "returns false when when the imaginary part is not zero" do - (Complex(3, 1) == @other).should be_false + it "returns false when the imaginary part is not zero" do + (Complex(3, 1) == @other).should == false end end @@ -87,7 +87,7 @@ describe "Complex#==" do other = mock_numeric('other') other.should_receive(:real?).any_number_of_times.and_return(false) other.should_receive(:==).with(complex).and_return(true) - (complex == other).should be_true + (complex == other).should == true end end end diff --git a/spec/ruby/core/complex/exponent_spec.rb b/spec/ruby/core/complex/exponent_spec.rb index 86f827aece..d0db0a40c2 100644 --- a/spec/ruby/core/complex/exponent_spec.rb +++ b/spec/ruby/core/complex/exponent_spec.rb @@ -3,13 +3,13 @@ require_relative '../../spec_helper' describe "Complex#**" do describe "with Integer 0" do it "returns Complex(1)" do - (Complex(3, 4) ** 0).should eql(Complex(1)) + (Complex(3, 4) ** 0).should.eql?(Complex(1)) end end describe "with Float 0.0" do it "returns Complex(1.0, 0.0)" do - (Complex(3, 4) ** 0.0).should eql(Complex(1.0, 0.0)) + (Complex(3, 4) ** 0.0).should.eql?(Complex(1.0, 0.0)) end end diff --git a/spec/ruby/core/complex/fdiv_spec.rb b/spec/ruby/core/complex/fdiv_spec.rb index 68f7d1b309..fdcbc6a95d 100644 --- a/spec/ruby/core/complex/fdiv_spec.rb +++ b/spec/ruby/core/complex/fdiv_spec.rb @@ -2,44 +2,44 @@ require_relative '../../spec_helper' describe "Complex#fdiv" do it "accepts a numeric argument" do - -> { Complex(20).fdiv(2) }.should_not raise_error(TypeError) - -> { Complex(20).fdiv(2.0) }.should_not raise_error(TypeError) - -> { Complex(20).fdiv(bignum_value) }.should_not raise_error(TypeError) + -> { Complex(20).fdiv(2) }.should_not.raise(TypeError) + -> { Complex(20).fdiv(2.0) }.should_not.raise(TypeError) + -> { Complex(20).fdiv(bignum_value) }.should_not.raise(TypeError) end it "accepts a negative numeric argument" do - -> { Complex(20).fdiv(-2) }.should_not raise_error(TypeError) - -> { Complex(20).fdiv(-2.0) }.should_not raise_error(TypeError) - -> { Complex(20).fdiv(-bignum_value) }.should_not raise_error(TypeError) + -> { Complex(20).fdiv(-2) }.should_not.raise(TypeError) + -> { Complex(20).fdiv(-2.0) }.should_not.raise(TypeError) + -> { Complex(20).fdiv(-bignum_value) }.should_not.raise(TypeError) end it "raises a TypeError if passed a non-numeric argument" do - -> { Complex(20).fdiv([]) }.should raise_error(TypeError) - -> { Complex(20).fdiv(:sym) }.should raise_error(TypeError) - -> { Complex(20).fdiv('s') }.should raise_error(TypeError) + -> { Complex(20).fdiv([]) }.should.raise(TypeError) + -> { Complex(20).fdiv(:sym) }.should.raise(TypeError) + -> { Complex(20).fdiv('s') }.should.raise(TypeError) end it "sets the real part to NaN if self's real part is NaN" do - Complex(nan_value).fdiv(2).real.nan?.should be_true + Complex(nan_value).fdiv(2).real.nan?.should == true end it "sets the imaginary part to NaN if self's imaginary part is NaN" do - Complex(2, nan_value).fdiv(2).imag.nan?.should be_true + Complex(2, nan_value).fdiv(2).imag.nan?.should == true end it "sets the real and imaginary part to NaN if self's real and imaginary parts are NaN" do - Complex(nan_value, nan_value).fdiv(2).imag.nan?.should be_true - Complex(nan_value, nan_value).fdiv(2).real.nan?.should be_true + Complex(nan_value, nan_value).fdiv(2).imag.nan?.should == true + Complex(nan_value, nan_value).fdiv(2).real.nan?.should == true end it "sets the real and imaginary part to NaN if self's real part and the argument are both NaN" do - Complex(nan_value, 2).fdiv(nan_value).imag.nan?.should be_true - Complex(nan_value, 2).fdiv(nan_value).real.nan?.should be_true + Complex(nan_value, 2).fdiv(nan_value).imag.nan?.should == true + Complex(nan_value, 2).fdiv(nan_value).real.nan?.should == true end it "sets the real and imaginary part to NaN if self's real part, self's imaginary part, and the argument are NaN" do - Complex(nan_value, nan_value).fdiv(nan_value).imag.nan?.should be_true - Complex(nan_value, nan_value).fdiv(nan_value).real.nan?.should be_true + Complex(nan_value, nan_value).fdiv(nan_value).imag.nan?.should == true + Complex(nan_value, nan_value).fdiv(nan_value).real.nan?.should == true end it "sets the real part to Infinity if self's real part is Infinity" do @@ -58,8 +58,8 @@ describe "Complex#fdiv" do end it "sets the real part to NaN and the imaginary part to NaN if self's imaginary part, self's real part, and the argument are Infinity" do - Complex(infinity_value, infinity_value).fdiv(infinity_value).real.nan?.should be_true - Complex(infinity_value, infinity_value).fdiv(infinity_value).imag.nan?.should be_true + Complex(infinity_value, infinity_value).fdiv(infinity_value).real.nan?.should == true + Complex(infinity_value, infinity_value).fdiv(infinity_value).imag.nan?.should == true end end @@ -71,7 +71,7 @@ describe "Complex#fdiv with no imaginary part" do it "returns a Complex number" do @numbers.each do |real| @numbers.each do |other| - Complex(real).fdiv(other).should be_an_instance_of(Complex) + Complex(real).fdiv(other).should.instance_of?(Complex) end end end @@ -103,7 +103,7 @@ describe "Complex#fdiv with an imaginary part" do @numbers.each_with_index do |other,idx| Complex( real,@numbers[idx == 0 ? -1 : idx-1] - ).fdiv(other).should be_an_instance_of(Complex) + ).fdiv(other).should.instance_of?(Complex) end end end diff --git a/spec/ruby/core/complex/imag_spec.rb b/spec/ruby/core/complex/imag_spec.rb index 2bafd1ab54..225f168e78 100644 --- a/spec/ruby/core/complex/imag_spec.rb +++ b/spec/ruby/core/complex/imag_spec.rb @@ -1,6 +1,7 @@ require_relative '../../spec_helper' -require_relative 'shared/image' describe "Complex#imag" do - it_behaves_like :complex_image, :imag + it "is an alias of Complex#imaginary" do + Complex.instance_method(:imag).should == Complex.instance_method(:imaginary) + end end diff --git a/spec/ruby/core/complex/imaginary_spec.rb b/spec/ruby/core/complex/imaginary_spec.rb index a8a1bfea90..ac9284e934 100644 --- a/spec/ruby/core/complex/imaginary_spec.rb +++ b/spec/ruby/core/complex/imaginary_spec.rb @@ -1,6 +1,10 @@ require_relative '../../spec_helper' -require_relative 'shared/image' describe "Complex#imaginary" do - it_behaves_like :complex_image, :imaginary + it "returns the imaginary part of self" do + Complex(1, 0).imaginary.should == 0 + Complex(2, 1).imaginary.should == 1 + Complex(6.7, 8.9).imaginary.should == 8.9 + Complex(1, bignum_value).imaginary.should == bignum_value + end end diff --git a/spec/ruby/core/complex/inspect_spec.rb b/spec/ruby/core/complex/inspect_spec.rb index 7a89ec6854..045be94b22 100644 --- a/spec/ruby/core/complex/inspect_spec.rb +++ b/spec/ruby/core/complex/inspect_spec.rb @@ -17,7 +17,8 @@ describe "Complex#inspect" do it "calls #inspect on real and imaginary" do real = NumericSpecs::Subclass.new - real.should_receive(:inspect).and_return("1") + # + because of https://bugs.ruby-lang.org/issues/20337 + real.should_receive(:inspect).and_return(+"1") imaginary = NumericSpecs::Subclass.new imaginary.should_receive(:inspect).and_return("2") imaginary.should_receive(:<).any_number_of_times.and_return(false) @@ -26,7 +27,8 @@ describe "Complex#inspect" do it "adds an `*' before the `i' if the last character of the imaginary part is not numeric" do real = NumericSpecs::Subclass.new - real.should_receive(:inspect).and_return("(1)") + # + because of https://bugs.ruby-lang.org/issues/20337 + real.should_receive(:inspect).and_return(+"(1)") imaginary = NumericSpecs::Subclass.new imaginary.should_receive(:inspect).and_return("(2)") imaginary.should_receive(:<).any_number_of_times.and_return(false) diff --git a/spec/ruby/core/complex/integer_spec.rb b/spec/ruby/core/complex/integer_spec.rb index 0957accb70..559bfbccfd 100644 --- a/spec/ruby/core/complex/integer_spec.rb +++ b/spec/ruby/core/complex/integer_spec.rb @@ -2,10 +2,10 @@ require_relative '../../spec_helper' describe "Complex#integer?" do it "returns false for a Complex with no imaginary part" do - Complex(20).integer?.should be_false + Complex(20).integer?.should == false end it "returns false for a Complex with an imaginary part" do - Complex(20,3).integer?.should be_false + Complex(20,3).integer?.should == false end end diff --git a/spec/ruby/core/complex/magnitude_spec.rb b/spec/ruby/core/complex/magnitude_spec.rb index 86f3b29868..6341b4eec8 100644 --- a/spec/ruby/core/complex/magnitude_spec.rb +++ b/spec/ruby/core/complex/magnitude_spec.rb @@ -1,6 +1,7 @@ require_relative '../../spec_helper' -require_relative 'shared/abs' describe "Complex#magnitude" do - it_behaves_like :complex_abs, :magnitude + it "is an alias of Complex#abs" do + Complex.instance_method(:magnitude).should == Complex.instance_method(:abs) + end end diff --git a/spec/ruby/core/complex/marshal_dump_spec.rb b/spec/ruby/core/complex/marshal_dump_spec.rb index 116899b0ad..201d55e9e5 100644 --- a/spec/ruby/core/complex/marshal_dump_spec.rb +++ b/spec/ruby/core/complex/marshal_dump_spec.rb @@ -2,7 +2,7 @@ require_relative '../../spec_helper' describe "Complex#marshal_dump" do it "is a private method" do - Complex.should have_private_instance_method(:marshal_dump, false) + Complex.private_instance_methods(false).should.include?(:marshal_dump) end it "dumps real and imaginary parts" do diff --git a/spec/ruby/core/complex/negative_spec.rb b/spec/ruby/core/complex/negative_spec.rb index 62ab89c04a..566975b8e1 100644 --- a/spec/ruby/core/complex/negative_spec.rb +++ b/spec/ruby/core/complex/negative_spec.rb @@ -4,10 +4,10 @@ describe "Complex#negative?" do it "is undefined" do c = Complex(1) - c.methods.should_not include(:negative?) + c.methods.should_not.include?(:negative?) -> { c.negative? - }.should raise_error(NoMethodError) + }.should.raise(NoMethodError) end end diff --git a/spec/ruby/core/complex/phase_spec.rb b/spec/ruby/core/complex/phase_spec.rb index 89574bf533..2ab90989e1 100644 --- a/spec/ruby/core/complex/phase_spec.rb +++ b/spec/ruby/core/complex/phase_spec.rb @@ -1,6 +1,7 @@ require_relative '../../spec_helper' -require_relative 'shared/arg' describe "Complex#phase" do - it_behaves_like :complex_arg, :phase + it "is an alias of Complex#arg" do + Complex.instance_method(:phase).should == Complex.instance_method(:arg) + end end diff --git a/spec/ruby/core/complex/polar_spec.rb b/spec/ruby/core/complex/polar_spec.rb index 3bb3751bc6..824211fcd0 100644 --- a/spec/ruby/core/complex/polar_spec.rb +++ b/spec/ruby/core/complex/polar_spec.rb @@ -7,24 +7,22 @@ describe "Complex.polar" do end it "raises a TypeError when given non real arguments" do - ->{ Complex.polar(nil) }.should raise_error(TypeError) - ->{ Complex.polar(nil, nil) }.should raise_error(TypeError) + ->{ Complex.polar(nil) }.should.raise(TypeError) + ->{ Complex.polar(nil, nil) }.should.raise(TypeError) end - ruby_bug "#19004", ""..."3.2" do - it "computes the real values of the real & imaginary parts from the polar form" do - a = Complex.polar(1.0+0.0i, Math::PI/2+0.0i) - a.real.should be_close(0.0, TOLERANCE) - a.imag.should be_close(1.0, TOLERANCE) - a.real.real?.should be_true - a.imag.real?.should be_true + it "computes the real values of the real & imaginary parts from the polar form" do + a = Complex.polar(1.0+0.0i, Math::PI/2+0.0i) + a.real.should be_close(0.0, TOLERANCE) + a.imag.should be_close(1.0, TOLERANCE) + a.real.real?.should == true + a.imag.real?.should == true - b = Complex.polar(1+0.0i) - b.real.should be_close(1.0, TOLERANCE) - b.imag.should be_close(0.0, TOLERANCE) - b.real.real?.should be_true - b.imag.real?.should be_true - end + b = Complex.polar(1+0.0i) + b.real.should be_close(1.0, TOLERANCE) + b.imag.should be_close(0.0, TOLERANCE) + b.real.real?.should == true + b.imag.real?.should == true end end diff --git a/spec/ruby/core/complex/positive_spec.rb b/spec/ruby/core/complex/positive_spec.rb index f1bad8608c..d2fb256538 100644 --- a/spec/ruby/core/complex/positive_spec.rb +++ b/spec/ruby/core/complex/positive_spec.rb @@ -4,10 +4,10 @@ describe "Complex#positive?" do it "is undefined" do c = Complex(1) - c.methods.should_not include(:positive?) + c.methods.should_not.include?(:positive?) -> { c.positive? - }.should raise_error(NoMethodError) + }.should.raise(NoMethodError) end end diff --git a/spec/ruby/core/complex/quo_spec.rb b/spec/ruby/core/complex/quo_spec.rb index ee6fd65c79..be0a44d532 100644 --- a/spec/ruby/core/complex/quo_spec.rb +++ b/spec/ruby/core/complex/quo_spec.rb @@ -1,6 +1,7 @@ require_relative '../../spec_helper' -require_relative 'shared/divide' describe "Complex#quo" do - it_behaves_like :complex_divide, :quo + it "is an alias of Complex#/" do + Complex.instance_method(:quo).should == Complex.instance_method(:/) + end end diff --git a/spec/ruby/core/complex/rationalize_spec.rb b/spec/ruby/core/complex/rationalize_spec.rb index 043b8ddf2a..d49bb52def 100644 --- a/spec/ruby/core/complex/rationalize_spec.rb +++ b/spec/ruby/core/complex/rationalize_spec.rb @@ -2,11 +2,11 @@ require_relative '../../spec_helper' describe "Complex#rationalize" do it "raises RangeError if self has non-zero imaginary part" do - -> { Complex(1,5).rationalize }.should raise_error(RangeError) + -> { Complex(1,5).rationalize }.should.raise(RangeError) end it "raises RangeError if self has 0.0 imaginary part" do - -> { Complex(1,0.0).rationalize }.should raise_error(RangeError) + -> { Complex(1,0.0).rationalize }.should.raise(RangeError) end it "returns a Rational if self has zero imaginary part" do @@ -25,7 +25,7 @@ describe "Complex#rationalize" do end it "raises ArgumentError when passed more than one argument" do - -> { Complex(1,0).rationalize(0.1, 0.1) }.should raise_error(ArgumentError) - -> { Complex(1,0).rationalize(0.1, 0.1, 2) }.should raise_error(ArgumentError) + -> { Complex(1,0).rationalize(0.1, 0.1) }.should.raise(ArgumentError) + -> { Complex(1,0).rationalize(0.1, 0.1, 2) }.should.raise(ArgumentError) end end diff --git a/spec/ruby/core/complex/real_spec.rb b/spec/ruby/core/complex/real_spec.rb index 2ea791c005..41734b23f0 100644 --- a/spec/ruby/core/complex/real_spec.rb +++ b/spec/ruby/core/complex/real_spec.rb @@ -11,18 +11,18 @@ end describe "Complex#real?" do it "returns false if there is an imaginary part" do - Complex(2,3).real?.should be_false + Complex(2,3).real?.should == false end it "returns false if there is not an imaginary part" do - Complex(2).real?.should be_false + Complex(2).real?.should == false end it "returns false if the real part is Infinity" do - Complex(infinity_value).real?.should be_false + Complex(infinity_value).real?.should == false end it "returns false if the real part is NaN" do - Complex(nan_value).real?.should be_false + Complex(nan_value).real?.should == false end end diff --git a/spec/ruby/core/complex/rect_spec.rb b/spec/ruby/core/complex/rect_spec.rb index 9e95f3efc2..72f2bf9930 100644 --- a/spec/ruby/core/complex/rect_spec.rb +++ b/spec/ruby/core/complex/rect_spec.rb @@ -1,10 +1,13 @@ require_relative '../../spec_helper' -require_relative 'shared/rect' describe "Complex#rect" do - it_behaves_like :complex_rect, :rect + it "is an alias of Complex#rectangular" do + Complex.instance_method(:rect).should == Complex.instance_method(:rectangular) + end end describe "Complex.rect" do - it_behaves_like :complex_rect_class, :rect + it "is an alias of Complex#rectangular" do + Complex.method(:rect).should == Complex.method(:rectangular) + end end diff --git a/spec/ruby/core/complex/rectangular_spec.rb b/spec/ruby/core/complex/rectangular_spec.rb index d4b8ad9782..7789bf925e 100644 --- a/spec/ruby/core/complex/rectangular_spec.rb +++ b/spec/ruby/core/complex/rectangular_spec.rb @@ -1,10 +1,114 @@ require_relative '../../spec_helper' -require_relative 'shared/rect' describe "Complex#rectangular" do - it_behaves_like :complex_rect, :rectangular + before :each do + @numbers = [ + Complex(1), + Complex(0, 20), + Complex(0, 0), + Complex(0.0), + Complex(9999999**99), + Complex(-20), + Complex.polar(76, 10) + ] + end + + it "returns an Array" do + @numbers.each do |number| + number.rectangular.should.instance_of?(Array) + end + end + + it "returns a two-element Array" do + @numbers.each do |number| + number.rectangular.size.should == 2 + end + end + + it "returns the real part of self as the first element" do + @numbers.each do |number| + number.rectangular.first.should == number.real + end + end + + it "returns the imaginary part of self as the last element" do + @numbers.each do |number| + number.rectangular.last.should == number.imaginary + end + end + + it "raises an ArgumentError if given any arguments" do + @numbers.each do |number| + -> { number.rectangular(number) }.should.raise(ArgumentError) + end + end end describe "Complex.rectangular" do - it_behaves_like :complex_rect_class, :rectangular + describe "passed a Numeric n which responds to #real? with true" do + it "returns a Complex with real part n and imaginary part 0" do + n = mock_numeric('n') + n.should_receive(:real?).any_number_of_times.and_return(true) + result = Complex.rectangular(n) + result.real.should == n + result.imag.should == 0 + end + end + + describe "passed a Numeric which responds to #real? with false" do + it "raises TypeError" do + n = mock_numeric('n') + n.should_receive(:real?).any_number_of_times.and_return(false) + -> { Complex.rectangular(n) }.should.raise(TypeError) + end + end + + describe "passed Numerics n1 and n2 and at least one responds to #real? with false" do + [[false, false], [false, true], [true, false]].each do |r1, r2| + it "raises TypeError" do + n1 = mock_numeric('n1') + n2 = mock_numeric('n2') + n1.should_receive(:real?).any_number_of_times.and_return(r1) + n2.should_receive(:real?).any_number_of_times.and_return(r2) + -> { Complex.rectangular(n1, n2) }.should.raise(TypeError) + end + end + end + + describe "passed Numerics n1 and n2 and both respond to #real? with true" do + it "returns a Complex with real part n1 and imaginary part n2" do + n1 = mock_numeric('n1') + n2 = mock_numeric('n2') + n1.should_receive(:real?).any_number_of_times.and_return(true) + n2.should_receive(:real?).any_number_of_times.and_return(true) + result = Complex.rectangular(n1, n2) + result.real.should == n1 + result.imag.should == n2 + end + end + + describe "when passed a Complex" do + it "raises a TypeError when the imaginary part is not zero" do + -> { + Complex.rectangular(1.0+1i, 2) + }.should.raise(TypeError) + + -> { + Complex.rectangular(1.0, 2i) + }.should.raise(TypeError) + end + + it "ignores the imaginary part if it is zero" do + result = Complex.rectangular(1.0+0i, 2+0.0i) + result.real.should == 1.0 + result.imag.should == 2 + end + end + + describe "passed a non-Numeric" do + it "raises TypeError" do + -> { Complex.rectangular(:sym) }.should.raise(TypeError) + -> { Complex.rectangular(0, :sym) }.should.raise(TypeError) + end + end end diff --git a/spec/ruby/core/complex/shared/abs.rb b/spec/ruby/core/complex/shared/abs.rb deleted file mode 100644 index 2299479341..0000000000 --- a/spec/ruby/core/complex/shared/abs.rb +++ /dev/null @@ -1,10 +0,0 @@ -describe :complex_abs, shared: true do - it "returns the modulus: |a + bi| = sqrt((a ^ 2) + (b ^ 2))" do - Complex(0, 0).send(@method).should == 0 - Complex(3, 4).send(@method).should == 5 # well-known integer case - Complex(-3, 4).send(@method).should == 5 - Complex(1, -1).send(@method).should be_close(Math.sqrt(2), TOLERANCE) - Complex(6.5, 0).send(@method).should be_close(6.5, TOLERANCE) - Complex(0, -7.2).send(@method).should be_close(7.2, TOLERANCE) - end -end diff --git a/spec/ruby/core/complex/shared/arg.rb b/spec/ruby/core/complex/shared/arg.rb deleted file mode 100644 index c81f197433..0000000000 --- a/spec/ruby/core/complex/shared/arg.rb +++ /dev/null @@ -1,9 +0,0 @@ -describe :complex_arg, shared: true do - it "returns the argument -- i.e., the angle from (1, 0) in the complex plane" do - two_pi = 2 * Math::PI - (Complex(1, 0).send(@method) % two_pi).should be_close(0, TOLERANCE) - (Complex(0, 2).send(@method) % two_pi).should be_close(Math::PI * 0.5, TOLERANCE) - (Complex(-100, 0).send(@method) % two_pi).should be_close(Math::PI, TOLERANCE) - (Complex(0, -75.3).send(@method) % two_pi).should be_close(Math::PI * 1.5, TOLERANCE) - end -end diff --git a/spec/ruby/core/complex/shared/conjugate.rb b/spec/ruby/core/complex/shared/conjugate.rb deleted file mode 100644 index d1ae47bcb6..0000000000 --- a/spec/ruby/core/complex/shared/conjugate.rb +++ /dev/null @@ -1,8 +0,0 @@ -describe :complex_conjugate, shared: true do - it "returns the complex conjugate: conj a + bi = a - bi" do - Complex(3, 5).send(@method).should == Complex(3, -5) - Complex(3, -5).send(@method).should == Complex(3, 5) - Complex(-3.0, 5.2).send(@method).should be_close(Complex(-3.0, -5.2), TOLERANCE) - Complex(3.0, -5.2).send(@method).should be_close(Complex(3.0, 5.2), TOLERANCE) - end -end diff --git a/spec/ruby/core/complex/shared/divide.rb b/spec/ruby/core/complex/shared/divide.rb deleted file mode 100644 index a60802c74c..0000000000 --- a/spec/ruby/core/complex/shared/divide.rb +++ /dev/null @@ -1,82 +0,0 @@ -describe :complex_divide, shared: true do - describe "with Complex" do - it "divides according to the usual rule for complex numbers" do - a = Complex((1 * 10) - (2 * 20), (1 * 20) + (2 * 10)) - b = Complex(1, 2) - a.send(@method, b).should == Complex(10, 20) - - c = Complex((1.5 * 100.2) - (2.1 * -30.3), (1.5 * -30.3) + (2.1 * 100.2)) - d = Complex(1.5, 2.1) - # remember the floating-point arithmetic - c.send(@method, d).should be_close(Complex(100.2, -30.3), TOLERANCE) - end - end - - describe "with Fixnum" do - it "divides both parts of the Complex number" do - Complex(20, 40).send(@method, 2).should == Complex(10, 20) - Complex(30, 30).send(@method, 10).should == Complex(3, 3) - end - - it "raises a ZeroDivisionError when given zero" do - -> { Complex(20, 40).send(@method, 0) }.should raise_error(ZeroDivisionError) - end - - it "produces Rational parts" do - Complex(5, 9).send(@method, 2).should eql(Complex(Rational(5,2), Rational(9,2))) - end - end - - describe "with Bignum" do - it "divides both parts of the Complex number" do - Complex(20, 40).send(@method, 2).should == Complex(10, 20) - Complex(15, 16).send(@method, 2.0).should be_close(Complex(7.5, 8), TOLERANCE) - end - end - - describe "with Float" do - it "divides both parts of the Complex number" do - Complex(3, 9).send(@method, 1.5).should == Complex(2, 6) - Complex(15, 16).send(@method, 2.0).should be_close(Complex(7.5, 8), TOLERANCE) - end - - it "returns Complex(Infinity, Infinity) when given zero" do - Complex(20, 40).send(@method, 0.0).real.infinite?.should == 1 - Complex(20, 40).send(@method, 0.0).imag.infinite?.should == 1 - Complex(-20, 40).send(@method, 0.0).real.infinite?.should == -1 - Complex(-20, 40).send(@method, 0.0).imag.infinite?.should == 1 - end - end - - describe "with Object" do - it "tries to coerce self into other" do - value = Complex(3, 9) - - obj = mock("Object") - obj.should_receive(:coerce).with(value).and_return([4, 2]) - value.send(@method, obj).should == 2 - end - end - - describe "with a Numeric which responds to #real? with true" do - it "returns Complex(real.quo(other), imag.quo(other))" do - other = mock_numeric('other') - real = mock_numeric('real') - imag = mock_numeric('imag') - other.should_receive(:real?).and_return(true) - real.should_receive(:quo).with(other).and_return(1) - imag.should_receive(:quo).with(other).and_return(2) - Complex(real, imag).send(@method, other).should == Complex(1, 2) - end - end - - describe "with a Numeric which responds to #real? with false" do - it "coerces the passed argument to Complex and divides the resulting elements" do - complex = Complex(3, 0) - other = mock_numeric('other') - other.should_receive(:real?).any_number_of_times.and_return(false) - other.should_receive(:coerce).with(complex).and_return([5, 2]) - complex.send(@method, other).should eql(Rational(5, 2)) - end - end -end diff --git a/spec/ruby/core/complex/shared/image.rb b/spec/ruby/core/complex/shared/image.rb deleted file mode 100644 index f839dbcaf9..0000000000 --- a/spec/ruby/core/complex/shared/image.rb +++ /dev/null @@ -1,8 +0,0 @@ -describe :complex_image, shared: true do - it "returns the imaginary part of self" do - Complex(1, 0).send(@method).should == 0 - Complex(2, 1).send(@method).should == 1 - Complex(6.7, 8.9).send(@method).should == 8.9 - Complex(1, bignum_value).send(@method).should == bignum_value - end -end diff --git a/spec/ruby/core/complex/shared/rect.rb b/spec/ruby/core/complex/shared/rect.rb deleted file mode 100644 index 9f5de1ffeb..0000000000 --- a/spec/ruby/core/complex/shared/rect.rb +++ /dev/null @@ -1,94 +0,0 @@ -describe :complex_rect, shared: true do - before :each do - @numbers = [ - Complex(1), - Complex(0, 20), - Complex(0, 0), - Complex(0.0), - Complex(9999999**99), - Complex(-20), - Complex.polar(76, 10) - ] - end - - it "returns an Array" do - @numbers.each do |number| - number.send(@method).should be_an_instance_of(Array) - end - end - - it "returns a two-element Array" do - @numbers.each do |number| - number.send(@method).size.should == 2 - end - end - - it "returns the real part of self as the first element" do - @numbers.each do |number| - number.send(@method).first.should == number.real - end - end - - it "returns the imaginary part of self as the last element" do - @numbers.each do |number| - number.send(@method).last.should == number.imaginary - end - end - - it "raises an ArgumentError if given any arguments" do - @numbers.each do |number| - -> { number.send(@method, number) }.should raise_error(ArgumentError) - end - end -end - -describe :complex_rect_class, shared: true do - describe "passed a Numeric n which responds to #real? with true" do - it "returns a Complex with real part n and imaginary part 0" do - n = mock_numeric('n') - n.should_receive(:real?).any_number_of_times.and_return(true) - result = Complex.send(@method, n) - result.real.should == n - result.imag.should == 0 - end - end - - describe "passed a Numeric which responds to #real? with false" do - it "raises TypeError" do - n = mock_numeric('n') - n.should_receive(:real?).any_number_of_times.and_return(false) - -> { Complex.send(@method, n) }.should raise_error(TypeError) - end - end - - describe "passed Numerics n1 and n2 and at least one responds to #real? with false" do - [[false, false], [false, true], [true, false]].each do |r1, r2| - it "raises TypeError" do - n1 = mock_numeric('n1') - n2 = mock_numeric('n2') - n1.should_receive(:real?).any_number_of_times.and_return(r1) - n2.should_receive(:real?).any_number_of_times.and_return(r2) - -> { Complex.send(@method, n1, n2) }.should raise_error(TypeError) - end - end - end - - describe "passed Numerics n1 and n2 and both respond to #real? with true" do - it "returns a Complex with real part n1 and imaginary part n2" do - n1 = mock_numeric('n1') - n2 = mock_numeric('n2') - n1.should_receive(:real?).any_number_of_times.and_return(true) - n2.should_receive(:real?).any_number_of_times.and_return(true) - result = Complex.send(@method, n1, n2) - result.real.should == n1 - result.imag.should == n2 - end - end - - describe "passed a non-Numeric" do - it "raises TypeError" do - -> { Complex.send(@method, :sym) }.should raise_error(TypeError) - -> { Complex.send(@method, 0, :sym) }.should raise_error(TypeError) - end - end -end diff --git a/spec/ruby/core/complex/to_c_spec.rb b/spec/ruby/core/complex/to_c_spec.rb index 5ce01d9d4e..cd7195556c 100644 --- a/spec/ruby/core/complex/to_c_spec.rb +++ b/spec/ruby/core/complex/to_c_spec.rb @@ -3,7 +3,7 @@ require_relative '../../spec_helper' describe "Complex#to_c" do it "returns self" do value = Complex(1, 5) - value.to_c.should equal(value) + value.to_c.should.equal?(value) end it 'returns the same value' do diff --git a/spec/ruby/core/complex/to_f_spec.rb b/spec/ruby/core/complex/to_f_spec.rb index b53471c1fc..9f3265cdb9 100644 --- a/spec/ruby/core/complex/to_f_spec.rb +++ b/spec/ruby/core/complex/to_f_spec.rb @@ -29,13 +29,13 @@ describe "Complex#to_f" do describe "when the imaginary part is non-zero" do it "raises RangeError" do - -> { Complex(0, 1).to_f }.should raise_error(RangeError) + -> { Complex(0, 1).to_f }.should.raise(RangeError) end end describe "when the imaginary part is Float 0.0" do it "raises RangeError" do - -> { Complex(0, 0.0).to_f }.should raise_error(RangeError) + -> { Complex(0, 0.0).to_f }.should.raise(RangeError) end end end diff --git a/spec/ruby/core/complex/to_i_spec.rb b/spec/ruby/core/complex/to_i_spec.rb index 1e78f5ec0e..9149ffbbaa 100644 --- a/spec/ruby/core/complex/to_i_spec.rb +++ b/spec/ruby/core/complex/to_i_spec.rb @@ -29,13 +29,13 @@ describe "Complex#to_i" do describe "when the imaginary part is non-zero" do it "raises RangeError" do - -> { Complex(0, 1).to_i }.should raise_error(RangeError) + -> { Complex(0, 1).to_i }.should.raise(RangeError) end end describe "when the imaginary part is Float 0.0" do it "raises RangeError" do - -> { Complex(0, 0.0).to_i }.should raise_error(RangeError) + -> { Complex(0, 0.0).to_i }.should.raise(RangeError) end end end diff --git a/spec/ruby/core/complex/to_r_spec.rb b/spec/ruby/core/complex/to_r_spec.rb index 4559921492..6587ae9e2e 100644 --- a/spec/ruby/core/complex/to_r_spec.rb +++ b/spec/ruby/core/complex/to_r_spec.rb @@ -29,13 +29,21 @@ describe "Complex#to_r" do describe "when the imaginary part is non-zero" do it "raises RangeError" do - -> { Complex(0, 1).to_r }.should raise_error(RangeError) + -> { Complex(0, 1).to_r }.should.raise(RangeError) end end describe "when the imaginary part is Float 0.0" do - it "raises RangeError" do - -> { Complex(0, 0.0).to_r }.should raise_error(RangeError) + ruby_version_is ''...'3.4' do + it "raises RangeError" do + -> { Complex(0, 0.0).to_r }.should.raise(RangeError) + end + end + + ruby_version_is '3.4' do + it "returns a Rational" do + Complex(0, 0.0).to_r.should == 0r + end end end end diff --git a/spec/ruby/core/complex/to_s_spec.rb b/spec/ruby/core/complex/to_s_spec.rb index 7677dcd0b5..ceccffe470 100644 --- a/spec/ruby/core/complex/to_s_spec.rb +++ b/spec/ruby/core/complex/to_s_spec.rb @@ -45,7 +45,8 @@ describe "Complex#to_s" do it "treats real and imaginary parts as strings" do real = NumericSpecs::Subclass.new - real.should_receive(:to_s).and_return("1") + # + because of https://bugs.ruby-lang.org/issues/20337 + real.should_receive(:to_s).and_return(+"1") imaginary = NumericSpecs::Subclass.new imaginary.should_receive(:to_s).and_return("2") imaginary.should_receive(:<).any_number_of_times.and_return(false) |
