diff options
Diffstat (limited to 'spec/ruby/core/complex')
-rw-r--r-- | spec/ruby/core/complex/comparison_spec.rb | 36 | ||||
-rw-r--r-- | spec/ruby/core/complex/inspect_spec.rb | 21 | ||||
-rw-r--r-- | spec/ruby/core/complex/polar_spec.rb | 16 | ||||
-rw-r--r-- | spec/ruby/core/complex/to_r_spec.rb | 12 | ||||
-rw-r--r-- | spec/ruby/core/complex/to_s_spec.rb | 11 |
5 files changed, 75 insertions, 21 deletions
diff --git a/spec/ruby/core/complex/comparison_spec.rb b/spec/ruby/core/complex/comparison_spec.rb index 2a437afb71..3a3142f234 100644 --- a/spec/ruby/core/complex/comparison_spec.rb +++ b/spec/ruby/core/complex/comparison_spec.rb @@ -1,27 +1,25 @@ require_relative '../../spec_helper' describe "Complex#<=>" do - ruby_version_is "2.7" 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 - end + 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 + 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 - 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 + end - it "returns 0, 1, or -1 if self and argument do not have imaginary part" do - (Complex(5) <=> Complex(2)).should == 1 - (Complex(2) <=> Complex(3)).should == -1 - (Complex(2) <=> Complex(2)).should == 0 + it "returns 0, 1, or -1 if self and argument do not have imaginary part" do + (Complex(5) <=> Complex(2)).should == 1 + (Complex(2) <=> Complex(3)).should == -1 + (Complex(2) <=> Complex(2)).should == 0 - (Complex(5) <=> 2).should == 1 - (Complex(2) <=> 3).should == -1 - (Complex(2) <=> 2).should == 0 - end + (Complex(5) <=> 2).should == 1 + (Complex(2) <=> 3).should == -1 + (Complex(2) <=> 2).should == 0 end end diff --git a/spec/ruby/core/complex/inspect_spec.rb b/spec/ruby/core/complex/inspect_spec.rb index 71aabde5be..045be94b22 100644 --- a/spec/ruby/core/complex/inspect_spec.rb +++ b/spec/ruby/core/complex/inspect_spec.rb @@ -1,4 +1,5 @@ require_relative '../../spec_helper' +require_relative '../numeric/fixtures/classes' describe "Complex#inspect" do it "returns (${real}+${image}i) for positive imaginary parts" do @@ -13,4 +14,24 @@ describe "Complex#inspect" do Complex(-1, -4).inspect.should == "(-1-4i)" Complex(-7, -6.7).inspect.should == "(-7-6.7i)" end + + it "calls #inspect on real and imaginary" do + real = NumericSpecs::Subclass.new + # + 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) + Complex(real, imaginary).inspect.should == "(1+2i)" + end + + it "adds an `*' before the `i' if the last character of the imaginary part is not numeric" do + real = NumericSpecs::Subclass.new + # + 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) + Complex(real, imaginary).inspect.should == "((1)+(2)*i)" + end end diff --git a/spec/ruby/core/complex/polar_spec.rb b/spec/ruby/core/complex/polar_spec.rb index 2a5d8ebd69..3bb3751bc6 100644 --- a/spec/ruby/core/complex/polar_spec.rb +++ b/spec/ruby/core/complex/polar_spec.rb @@ -10,6 +10,22 @@ describe "Complex.polar" do ->{ Complex.polar(nil) }.should raise_error(TypeError) ->{ Complex.polar(nil, nil) }.should raise_error(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 + + 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 + end end describe "Complex#polar" do diff --git a/spec/ruby/core/complex/to_r_spec.rb b/spec/ruby/core/complex/to_r_spec.rb index 4559921492..788027a500 100644 --- a/spec/ruby/core/complex/to_r_spec.rb +++ b/spec/ruby/core/complex/to_r_spec.rb @@ -34,8 +34,16 @@ describe "Complex#to_r" do 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_error(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 989a7ae0b7..ceccffe470 100644 --- a/spec/ruby/core/complex/to_s_spec.rb +++ b/spec/ruby/core/complex/to_s_spec.rb @@ -1,4 +1,5 @@ require_relative '../../spec_helper' +require_relative '../numeric/fixtures/classes' describe "Complex#to_s" do describe "when self's real component is 0" do @@ -41,4 +42,14 @@ describe "Complex#to_s" do it "returns 1+NaN*i for Complex(1, NaN)" do Complex(1, nan_value).to_s.should == "1+NaN*i" end + + it "treats real and imaginary parts as strings" do + real = NumericSpecs::Subclass.new + # + 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) + Complex(real, imaginary).to_s.should == "1+2i" + end end |