diff options
author | Benoit Daloze <eregontp@gmail.com> | 2022-11-07 20:05:30 +0100 |
---|---|---|
committer | Benoit Daloze <eregontp@gmail.com> | 2022-11-07 20:05:30 +0100 |
commit | 83decbb62b8b3f1638927033f12b55f9b11f78c6 (patch) | |
tree | 8995f5b5cdb615f3d19edded66c6a9a9e82f159c /spec/ruby/core/string/to_c_spec.rb | |
parent | c99e4c427897e82a3419abed894d28705f70fa13 (diff) | |
download | ruby-83decbb62b8b3f1638927033f12b55f9b11f78c6.tar.gz |
Update to ruby/spec@740ccc8
Diffstat (limited to 'spec/ruby/core/string/to_c_spec.rb')
-rw-r--r-- | spec/ruby/core/string/to_c_spec.rb | 105 |
1 files changed, 24 insertions, 81 deletions
diff --git a/spec/ruby/core/string/to_c_spec.rb b/spec/ruby/core/string/to_c_spec.rb index 9c84b14f4d..994bdf99f6 100644 --- a/spec/ruby/core/string/to_c_spec.rb +++ b/spec/ruby/core/string/to_c_spec.rb @@ -1,99 +1,42 @@ require_relative '../../spec_helper' +require_relative '../../shared/kernel/complex' +require_relative 'fixtures/to_c' describe "String#to_c" do - it "returns a Complex object" do - '9'.to_c.should be_an_instance_of(Complex) - end - - it "understands integers" do - '20'.to_c.should == Complex(20) - end - - it "understands negative integers" do - '-3'.to_c.should == Complex(-3) - end - - it "understands fractions (numerator/denominator) for the real part" do - '2/3'.to_c.should == Complex(Rational(2, 3)) - end - - it "understands fractions (numerator/denominator) for the imaginary part" do - '4+2/3i'.to_c.should == Complex(4, Rational(2, 3)) - end - - it "understands negative fractions (-numerator/denominator) for the real part" do - '-2/3'.to_c.should == Complex(Rational(-2, 3)) - end - - it "understands negative fractions (-numerator/denominator) for the imaginary part" do - '7-2/3i'.to_c.should == Complex(7, Rational(-2, 3)) - end - - it "understands floats (a.b) for the real part" do - '2.3'.to_c.should == Complex(2.3) - end - - it "understands floats (a.b) for the imaginary part" do - '4+2.3i'.to_c.should == Complex(4, 2.3) - end - - it "understands negative floats (-a.b) for the real part" do - '-2.33'.to_c.should == Complex(-2.33) - end - - it "understands negative floats (-a.b) for the imaginary part" do - '7-28.771i'.to_c.should == Complex(7, -28.771) - end - - it "understands an integer followed by 'i' to mean that integer is the imaginary part" do - '35i'.to_c.should == Complex(0,35) - end - - it "understands a negative integer followed by 'i' to mean that negative integer is the imaginary part" do - '-29i'.to_c.should == Complex(0,-29) - end - - it "understands an 'i' by itself as denoting a complex number with an imaginary part of 1" do - 'i'.to_c.should == Complex(0,1) - end - - it "understands a '-i' by itself as denoting a complex number with an imaginary part of -1" do - '-i'.to_c.should == Complex(0,-1) - end - - it "understands 'a+bi' to mean a complex number with 'a' as the real part, 'b' as the imaginary" do - '79+4i'.to_c.should == Complex(79,4) - end - - it "understands 'a-bi' to mean a complex number with 'a' as the real part, '-b' as the imaginary" do - '79-4i'.to_c.should == Complex(79,-4) - end + it_behaves_like :kernel_complex, :to_c_method, StringSpecs +end - it "understands scientific notation for the real part" do - '2e3+4i'.to_c.should == Complex(2e3,4) +describe "String#to_c" do + it "returns a complex number with 0 as the real part, 0 as the imaginary part for unrecognised Strings" do + 'ruby'.to_c.should == Complex(0, 0) end - it "understands negative scientific notation for the real part" do - '-2e3+4i'.to_c.should == Complex(-2e3,4) + it "ignores trailing garbage" do + '79+4iruby'.to_c.should == Complex(79, 4) end - it "understands scientific notation for the imaginary part" do - '4+2e3i'.to_c.should == Complex(4, 2e3) + it "understands Float::INFINITY" do + 'Infinity'.to_c.should == Complex(0, 1) + '-Infinity'.to_c.should == Complex(0, -1) end - it "understands negative scientific notation for the imaginary part" do - '4-2e3i'.to_c.should == Complex(4, -2e3) + it "understands Float::NAN" do + 'NaN'.to_c.should == Complex(0, 0) end - it "understands scientific notation for the real and imaginary part in the same String" do - '2e3+2e4i'.to_c.should == Complex(2e3,2e4) + it "understands a sequence of _" do + '7__9+4__0i'.to_c.should == Complex(79, 40) end - it "understands negative scientific notation for the real and imaginary part in the same String" do - '-2e3-2e4i'.to_c.should == Complex(-2e3,-2e4) + it "allows null-byte" do + "1-2i\0".to_c.should == Complex(1, -2) + "1\0-2i".to_c.should == Complex(1, 0) + "\01-2i".to_c.should == Complex(0, 0) end - it "returns a complex number with 0 as the real part, 0 as the imaginary part for unrecognised Strings" do - 'ruby'.to_c.should == Complex(0,0) + it "raises Encoding::CompatibilityError if String is in not ASCII-compatible encoding" do + -> { + '79+4i'.encode("UTF-16").to_c + }.should raise_error(Encoding::CompatibilityError, "ASCII incompatible encoding: UTF-16") end end |