From e87fb88be844f0fae736768846954b6f6f7dc7c3 Mon Sep 17 00:00:00 2001 From: eregon Date: Tue, 25 Sep 2018 10:41:16 +0000 Subject: Update to ruby/spec@241f9e7 git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64831 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- spec/ruby/core/complex/abs2_spec.rb | 8 +- spec/ruby/core/complex/abs_spec.rb | 3 +- spec/ruby/core/complex/angle_spec.rb | 3 +- spec/ruby/core/complex/arg_spec.rb | 3 +- spec/ruby/core/complex/coerce_spec.rb | 69 +++++++++- spec/ruby/core/complex/conj_spec.rb | 2 +- spec/ruby/core/complex/conjugate_spec.rb | 2 +- spec/ruby/core/complex/constants_spec.rb | 6 +- spec/ruby/core/complex/denominator_spec.rb | 12 +- spec/ruby/core/complex/divide_spec.rb | 3 +- spec/ruby/core/complex/equal_value_spec.rb | 92 +++++++++++++- spec/ruby/core/complex/exponent_spec.rb | 60 ++++++++- spec/ruby/core/complex/imag_spec.rb | 3 +- spec/ruby/core/complex/imaginary_spec.rb | 3 +- spec/ruby/core/complex/inspect_spec.rb | 15 ++- spec/ruby/core/complex/integer_spec.rb | 2 + spec/ruby/core/complex/magnitude_spec.rb | 3 +- spec/ruby/core/complex/minus_spec.rb | 44 ++++++- spec/ruby/core/complex/multiply_spec.rb | 48 ++++++- spec/ruby/core/complex/negative_spec.rb | 2 + spec/ruby/core/complex/numerator_spec.rb | 18 ++- spec/ruby/core/complex/phase_spec.rb | 2 +- spec/ruby/core/complex/plus_spec.rb | 44 ++++++- spec/ruby/core/complex/polar_spec.rb | 19 ++- spec/ruby/core/complex/positive_spec.rb | 2 + spec/ruby/core/complex/quo_spec.rb | 3 +- spec/ruby/core/complex/rationalize_spec.rb | 2 + spec/ruby/core/complex/real_spec.rb | 9 +- spec/ruby/core/complex/rect_spec.rb | 3 +- spec/ruby/core/complex/rectangular_spec.rb | 3 +- spec/ruby/core/complex/shared/abs.rb | 10 ++ spec/ruby/core/complex/shared/arg.rb | 9 ++ spec/ruby/core/complex/shared/conjugate.rb | 8 ++ spec/ruby/core/complex/shared/divide.rb | 82 ++++++++++++ spec/ruby/core/complex/shared/image.rb | 8 ++ spec/ruby/core/complex/shared/rect.rb | 94 ++++++++++++++ spec/ruby/core/complex/to_s_spec.rb | 43 ++++++- spec/ruby/core/encoding/converter/convpath_spec.rb | 55 ++------ .../encoding/converter/search_convpath_spec.rb | 68 ++-------- spec/ruby/core/float/abs_spec.rb | 1 + spec/ruby/core/float/angle_spec.rb | 3 +- spec/ruby/core/float/arg_spec.rb | 3 +- spec/ruby/core/float/phase_spec.rb | 3 +- spec/ruby/core/float/shared/arg.rb | 36 ++++++ spec/ruby/core/kernel/Complex_spec.rb | 139 +++++++++++++++++++- spec/ruby/core/kernel/caller_spec.rb | 9 ++ spec/ruby/core/kernel/fixtures/caller_at_exit.rb | 7 + spec/ruby/core/kernel/lambda_spec.rb | 15 +++ spec/ruby/core/marshal/dump_spec.rb | 5 + spec/ruby/core/math/acos_spec.rb | 14 +- spec/ruby/core/math/acosh_spec.rb | 10 +- spec/ruby/core/math/asin_spec.rb | 14 +- spec/ruby/core/math/log10_spec.rb | 6 +- spec/ruby/core/math/log_spec.rb | 6 +- spec/ruby/core/module/autoload_spec.rb | 59 +++++++++ .../module/fixtures/autoload_required_directly.rb | 7 + .../fixtures/autoload_required_directly_nested.rb | 1 + spec/ruby/core/numeric/abs_spec.rb | 1 + spec/ruby/core/numeric/angle_spec.rb | 2 +- spec/ruby/core/numeric/arg_spec.rb | 2 +- spec/ruby/core/numeric/conj_spec.rb | 2 +- spec/ruby/core/numeric/conjugate_spec.rb | 2 +- spec/ruby/core/numeric/imag_spec.rb | 2 +- spec/ruby/core/numeric/imaginary_spec.rb | 2 +- spec/ruby/core/numeric/phase_spec.rb | 2 +- spec/ruby/core/numeric/polar_spec.rb | 48 ++++++- spec/ruby/core/numeric/real_spec.rb | 28 +++- spec/ruby/core/numeric/shared/arg.rb | 38 ++++++ spec/ruby/core/numeric/shared/conj.rb | 20 +++ spec/ruby/core/numeric/shared/imag.rb | 26 ++++ spec/ruby/core/proc/source_location_spec.rb | 14 ++ spec/ruby/core/rational/integer_spec.rb | 7 +- spec/ruby/core/struct/shared/select.rb | 3 +- .../backtrace/location/absolute_path_spec.rb | 11 ++ .../fixtures/absolute_path_method_added.rb | 10 ++ spec/ruby/language/fixtures/rescue.rb | 4 + spec/ruby/language/precedence_spec.rb | 2 +- spec/ruby/language/rescue_spec.rb | 13 ++ spec/ruby/library/conditionvariable/wait_spec.rb | 60 ++++----- spec/ruby/library/matrix/divide_spec.rb | 5 +- spec/ruby/library/matrix/real_spec.rb | 3 +- spec/ruby/library/monitor/mon_initialize_spec.rb | 31 +++++ spec/ruby/library/rbconfig/sizeof/sizeof_spec.rb | 30 +++++ .../library/socket/addrinfo/getnameinfo_spec.rb | 2 +- .../library/socket/ancillarydata/family_spec.rb | 2 +- .../socket/ancillarydata/initialize_spec.rb | 2 +- spec/ruby/library/socket/ancillarydata/int_spec.rb | 4 +- .../socket/ancillarydata/ip_pktinfo_spec.rb | 4 +- .../socket/ancillarydata/ipv6_pktinfo_spec.rb | 4 +- .../library/socket/ancillarydata/level_spec.rb | 2 +- .../ruby/library/socket/ancillarydata/type_spec.rb | 2 +- .../library/socket/basicsocket/getsockopt_spec.rb | 4 +- .../socket/basicsocket/recvmsg_nonblock_spec.rb | 4 +- .../library/socket/basicsocket/recvmsg_spec.rb | 4 +- .../library/socket/basicsocket/shutdown_spec.rb | 2 +- spec/ruby/library/socket/option/initialize_spec.rb | 2 +- spec/ruby/library/socket/shared/socketpair.rb | 4 +- .../library/socket/socket/connect_nonblock_spec.rb | 2 +- .../ruby/library/socket/socket/getaddrinfo_spec.rb | 42 +++--- .../library/socket/socket/gethostbyaddr_spec.rb | 4 +- spec/ruby/library/socket/socket/getifaddrs_spec.rb | 10 +- spec/ruby/library/socket/socket/initialize_spec.rb | 4 +- spec/ruby/library/socket/socket/listen_spec.rb | 2 +- spec/ruby/library/socket/socket/sysaccept_spec.rb | 4 +- .../library/socket/tcpserver/initialize_spec.rb | 12 +- spec/ruby/library/socket/tcpserver/listen_spec.rb | 2 +- spec/ruby/library/socket/tcpserver/new_spec.rb | 10 +- .../library/socket/tcpserver/sysaccept_spec.rb | 6 +- .../library/socket/tcpsocket/gethostbyname_spec.rb | 2 +- .../library/socket/tcpsocket/initialize_spec.rb | 2 +- spec/ruby/library/socket/tcpsocket/shared/new.rb | 2 +- .../library/socket/udpsocket/initialize_spec.rb | 2 +- spec/ruby/library/socket/udpsocket/new_spec.rb | 2 +- spec/ruby/library/socket/udpsocket/send_spec.rb | 6 +- .../library/socket/unixserver/sysaccept_spec.rb | 8 +- .../library/stringio/external_encoding_spec.rb | 6 + spec/ruby/optional/capi/ext/thread_spec.c | 8 +- spec/ruby/optional/capi/spec_helper.rb | 28 ++-- spec/ruby/optional/capi/thread_spec.rb | 22 ++++ spec/ruby/shared/complex/Complex.rb | 141 --------------------- spec/ruby/shared/complex/abs.rb | 12 -- spec/ruby/shared/complex/abs2.rb | 12 -- spec/ruby/shared/complex/arg.rb | 9 -- spec/ruby/shared/complex/coerce.rb | 70 ---------- spec/ruby/shared/complex/conjugate.rb | 8 -- spec/ruby/shared/complex/constants.rb | 7 - spec/ruby/shared/complex/denominator.rb | 13 -- spec/ruby/shared/complex/divide.rb | 84 ------------ spec/ruby/shared/complex/equal_value.rb | 93 -------------- spec/ruby/shared/complex/exponent.rb | 61 --------- spec/ruby/shared/complex/float/arg.rb | 38 ------ spec/ruby/shared/complex/image.rb | 10 -- spec/ruby/shared/complex/inspect.rb | 14 -- spec/ruby/shared/complex/minus.rb | 45 ------- spec/ruby/shared/complex/multiply.rb | 49 ------- spec/ruby/shared/complex/numerator.rb | 19 --- spec/ruby/shared/complex/numeric/arg.rb | 38 ------ spec/ruby/shared/complex/numeric/conj.rb | 20 --- spec/ruby/shared/complex/numeric/imag.rb | 26 ---- spec/ruby/shared/complex/numeric/polar.rb | 50 -------- spec/ruby/shared/complex/numeric/real.rb | 30 ----- spec/ruby/shared/complex/plus.rb | 45 ------- spec/ruby/shared/complex/polar.rb | 22 ---- spec/ruby/shared/complex/real.rb | 8 -- spec/ruby/shared/complex/rect.rb | 96 -------------- spec/ruby/shared/complex/to_s.rb | 44 ------- spec/ruby/shared/rational/Rational.rb | 2 +- spec/ruby/shared/rational/exponent.rb | 6 +- spec/ruby/shared/rational/inspect.rb | 12 +- spec/ruby/shared/rational/round.rb | 7 +- spec/ruby/shared/rational/to_r.rb | 12 +- spec/ruby/shared/rational/to_s.rb | 7 +- 152 files changed, 1498 insertions(+), 1411 deletions(-) create mode 100644 spec/ruby/core/complex/shared/abs.rb create mode 100644 spec/ruby/core/complex/shared/arg.rb create mode 100644 spec/ruby/core/complex/shared/conjugate.rb create mode 100644 spec/ruby/core/complex/shared/divide.rb create mode 100644 spec/ruby/core/complex/shared/image.rb create mode 100644 spec/ruby/core/complex/shared/rect.rb create mode 100644 spec/ruby/core/float/shared/arg.rb create mode 100644 spec/ruby/core/kernel/fixtures/caller_at_exit.rb create mode 100644 spec/ruby/core/module/fixtures/autoload_required_directly.rb create mode 100644 spec/ruby/core/module/fixtures/autoload_required_directly_nested.rb create mode 100644 spec/ruby/core/numeric/shared/arg.rb create mode 100644 spec/ruby/core/numeric/shared/conj.rb create mode 100644 spec/ruby/core/numeric/shared/imag.rb create mode 100644 spec/ruby/core/thread/backtrace/location/fixtures/absolute_path_method_added.rb create mode 100644 spec/ruby/library/monitor/mon_initialize_spec.rb create mode 100644 spec/ruby/library/rbconfig/sizeof/sizeof_spec.rb delete mode 100644 spec/ruby/shared/complex/Complex.rb delete mode 100644 spec/ruby/shared/complex/abs.rb delete mode 100644 spec/ruby/shared/complex/abs2.rb delete mode 100644 spec/ruby/shared/complex/arg.rb delete mode 100644 spec/ruby/shared/complex/coerce.rb delete mode 100644 spec/ruby/shared/complex/conjugate.rb delete mode 100644 spec/ruby/shared/complex/constants.rb delete mode 100644 spec/ruby/shared/complex/denominator.rb delete mode 100644 spec/ruby/shared/complex/divide.rb delete mode 100644 spec/ruby/shared/complex/equal_value.rb delete mode 100644 spec/ruby/shared/complex/exponent.rb delete mode 100644 spec/ruby/shared/complex/float/arg.rb delete mode 100644 spec/ruby/shared/complex/image.rb delete mode 100644 spec/ruby/shared/complex/inspect.rb delete mode 100644 spec/ruby/shared/complex/minus.rb delete mode 100644 spec/ruby/shared/complex/multiply.rb delete mode 100644 spec/ruby/shared/complex/numerator.rb delete mode 100644 spec/ruby/shared/complex/numeric/arg.rb delete mode 100644 spec/ruby/shared/complex/numeric/conj.rb delete mode 100644 spec/ruby/shared/complex/numeric/imag.rb delete mode 100644 spec/ruby/shared/complex/numeric/polar.rb delete mode 100644 spec/ruby/shared/complex/numeric/real.rb delete mode 100644 spec/ruby/shared/complex/plus.rb delete mode 100644 spec/ruby/shared/complex/polar.rb delete mode 100644 spec/ruby/shared/complex/real.rb delete mode 100644 spec/ruby/shared/complex/rect.rb delete mode 100644 spec/ruby/shared/complex/to_s.rb diff --git a/spec/ruby/core/complex/abs2_spec.rb b/spec/ruby/core/complex/abs2_spec.rb index de40197c76..3e5c5fd225 100644 --- a/spec/ruby/core/complex/abs2_spec.rb +++ b/spec/ruby/core/complex/abs2_spec.rb @@ -1,5 +1,9 @@ -require_relative '../../shared/complex/abs2' +require_relative '../../spec_helper' describe "Complex#abs2" do - it_behaves_like :complex_abs2, :abs2 + it "returns the sum of the squares of the real and imaginary parts" do + Complex(1, -2).abs2.should == 1 + 4 + Complex(-0.1, 0.2).abs2.should be_close(0.01 + 0.04, TOLERANCE) + Complex(0).abs2.should == 0 + end end diff --git a/spec/ruby/core/complex/abs_spec.rb b/spec/ruby/core/complex/abs_spec.rb index 8114d70a7e..43912c517f 100644 --- a/spec/ruby/core/complex/abs_spec.rb +++ b/spec/ruby/core/complex/abs_spec.rb @@ -1,4 +1,5 @@ -require_relative '../../shared/complex/abs' +require_relative '../../spec_helper' +require_relative 'shared/abs' describe "Complex#abs" do it_behaves_like :complex_abs, :abs diff --git a/spec/ruby/core/complex/angle_spec.rb b/spec/ruby/core/complex/angle_spec.rb index 237838fc2d..4aa176956f 100644 --- a/spec/ruby/core/complex/angle_spec.rb +++ b/spec/ruby/core/complex/angle_spec.rb @@ -1,6 +1,5 @@ require_relative '../../spec_helper' - -require_relative '../../shared/complex/arg' +require_relative 'shared/arg' describe "Complex#angle" do it_behaves_like :complex_arg, :angle diff --git a/spec/ruby/core/complex/arg_spec.rb b/spec/ruby/core/complex/arg_spec.rb index 04c406e60e..009f19429f 100644 --- a/spec/ruby/core/complex/arg_spec.rb +++ b/spec/ruby/core/complex/arg_spec.rb @@ -1,6 +1,5 @@ require_relative '../../spec_helper' - -require_relative '../../shared/complex/arg' +require_relative 'shared/arg' describe "Complex#arg" do it_behaves_like :complex_arg, :arg diff --git a/spec/ruby/core/complex/coerce_spec.rb b/spec/ruby/core/complex/coerce_spec.rb index 955309d5cd..ce2fb36b73 100644 --- a/spec/ruby/core/complex/coerce_spec.rb +++ b/spec/ruby/core/complex/coerce_spec.rb @@ -1,5 +1,70 @@ -require_relative '../../shared/complex/coerce' +require_relative '../../spec_helper' describe "Complex#coerce" do - it_behaves_like :complex_coerce, :coerce + before :each do + @one = Complex(1) + end + + 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) + 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) + 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) + 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) + 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) + end + + it "returns an array containing other as Complex and self when other is a Numeric which responds to #real? with true" do + other = mock_numeric('other') + 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) + 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) + lambda { @one.coerce(other) }.should raise_error(TypeError) + end + + it "raises a TypeError when other is a String" do + lambda { @one.coerce("20") }.should raise_error(TypeError) + end + + it "raises a TypeError when other is nil" do + lambda { @one.coerce(nil) }.should raise_error(TypeError) + end + + it "raises a TypeError when other is false" do + lambda { @one.coerce(false) }.should raise_error(TypeError) + end end diff --git a/spec/ruby/core/complex/conj_spec.rb b/spec/ruby/core/complex/conj_spec.rb index 59208e9e8d..5e3bc1acb8 100644 --- a/spec/ruby/core/complex/conj_spec.rb +++ b/spec/ruby/core/complex/conj_spec.rb @@ -1,5 +1,5 @@ require_relative '../../spec_helper' -require_relative '../../shared/complex/conjugate' +require_relative 'shared/conjugate' describe "Complex#conj" do it_behaves_like :complex_conjugate, :conj diff --git a/spec/ruby/core/complex/conjugate_spec.rb b/spec/ruby/core/complex/conjugate_spec.rb index 8fda364027..f658bab4da 100644 --- a/spec/ruby/core/complex/conjugate_spec.rb +++ b/spec/ruby/core/complex/conjugate_spec.rb @@ -1,5 +1,5 @@ require_relative '../../spec_helper' -require_relative '../../shared/complex/conjugate' +require_relative 'shared/conjugate' describe "Complex#conjugate" do it_behaves_like :complex_conjugate, :conjugate diff --git a/spec/ruby/core/complex/constants_spec.rb b/spec/ruby/core/complex/constants_spec.rb index f9584f6075..50303de16c 100644 --- a/spec/ruby/core/complex/constants_spec.rb +++ b/spec/ruby/core/complex/constants_spec.rb @@ -1,5 +1,7 @@ -require_relative '../../shared/complex/constants' +require_relative '../../spec_helper' describe "Complex::I" do - it_behaves_like :complex_I, :I + it "is Complex(0, 1)" do + Complex::I.should eql(Complex(0, 1)) + end end diff --git a/spec/ruby/core/complex/denominator_spec.rb b/spec/ruby/core/complex/denominator_spec.rb index 511f0d2b72..c1a2003820 100644 --- a/spec/ruby/core/complex/denominator_spec.rb +++ b/spec/ruby/core/complex/denominator_spec.rb @@ -1,5 +1,13 @@ -require_relative '../../shared/complex/denominator' +require_relative '../../spec_helper' describe "Complex#denominator" do - it_behaves_like :complex_denominator, :denominator + it "returns the least common multiple denominator of the real and imaginary parts" do + Complex(3, 4).denominator.should == 1 + Complex(3, bignum_value).denominator.should == 1 + + Complex(3, Rational(3,4)).denominator.should == 4 + + Complex(Rational(4,8), Rational(3,4)).denominator.should == 4 + Complex(Rational(3,8), Rational(3,4)).denominator.should == 8 + end end diff --git a/spec/ruby/core/complex/divide_spec.rb b/spec/ruby/core/complex/divide_spec.rb index 81f28c08e0..bebf862312 100644 --- a/spec/ruby/core/complex/divide_spec.rb +++ b/spec/ruby/core/complex/divide_spec.rb @@ -1,4 +1,5 @@ -require_relative '../../shared/complex/divide' +require_relative '../../spec_helper' +require_relative 'shared/divide' describe "Complex#/" do it_behaves_like :complex_divide, :/ diff --git a/spec/ruby/core/complex/equal_value_spec.rb b/spec/ruby/core/complex/equal_value_spec.rb index b31eba1bb3..b1e4f9cfcd 100644 --- a/spec/ruby/core/complex/equal_value_spec.rb +++ b/spec/ruby/core/complex/equal_value_spec.rb @@ -1,5 +1,93 @@ -require_relative '../../shared/complex/equal_value' +require_relative '../../spec_helper' describe "Complex#==" do - it_behaves_like :complex_equal_value, :== + describe "with Complex" do + it "returns true when self and other have numerical equality" do + Complex(1, 2).should == Complex(1, 2) + Complex(3, 9).should == Complex(3, 9) + Complex(-3, -9).should == Complex(-3, -9) + + Complex(1, 2).should_not == Complex(3, 4) + Complex(3, 9).should_not == Complex(9, 3) + + Complex(1.0, 2.0).should == Complex(1, 2) + Complex(3.0, 9.0).should_not == Complex(9.0, 3.0) + + Complex(1.5, 2.5).should == Complex(1.5, 2.5) + Complex(1.5, 2.5).should == Complex(1.5, 2.5) + Complex(-1.5, 2.5).should == Complex(-1.5, 2.5) + + Complex(1.5, 2.5).should_not == Complex(2.5, 1.5) + Complex(3.75, 2.5).should_not == Complex(1.5, 2.5) + + Complex(bignum_value, 2.5).should == Complex(bignum_value, 2.5) + Complex(3.75, bignum_value).should_not == Complex(1.5, bignum_value) + + Complex(nan_value).should_not == Complex(nan_value) + end + end + + describe "with Numeric" do + it "returns true when self's imaginary part is 0 and the real part and other have numerical equality" do + Complex(3, 0).should == 3 + Complex(-3, 0).should == -3 + + Complex(3.5, 0).should == 3.5 + Complex(-3.5, 0).should == -3.5 + + Complex(bignum_value, 0).should == bignum_value + Complex(-bignum_value, 0).should == -bignum_value + + Complex(3.0, 0).should == 3 + Complex(-3.0, 0).should == -3 + + Complex(3, 0).should_not == 4 + Complex(-3, 0).should_not == -4 + + Complex(3.5, 0).should_not == -4.5 + Complex(-3.5, 0).should_not == 2.5 + + Complex(bignum_value, 0).should_not == bignum_value(10) + Complex(-bignum_value, 0).should_not == -bignum_value(20) + end + end + + describe "with Object" do + # Fixnum#==, Float#== and Bignum#== only return booleans - Bug? + it "calls other#== with self" do + value = Complex(3, 0) + + obj = mock("Object") + obj.should_receive(:==).with(value).and_return(:expected) + + (value == obj).should_not be_false + end + end + + describe "with a Numeric which responds to #real? with true" do + before do + @other = mock_numeric('other') + @other.should_receive(:real?).any_number_of_times.and_return(true) + end + + 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 + end + + it "returns false when when the imaginary part is not zero" do + (Complex(3, 1) == @other).should be_false + end + end + + describe "with a Numeric which responds to #real? with false" do + it "returns other == self" do + complex = Complex(3, 0) + 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 + end + end end diff --git a/spec/ruby/core/complex/exponent_spec.rb b/spec/ruby/core/complex/exponent_spec.rb index 47381d7e43..bf07c90038 100644 --- a/spec/ruby/core/complex/exponent_spec.rb +++ b/spec/ruby/core/complex/exponent_spec.rb @@ -1,5 +1,61 @@ -require_relative '../../shared/complex/exponent' +require_relative '../../spec_helper' describe "Complex#**" do - it_behaves_like :complex_exponent, :** + describe "with Fixnum 0" do + it "returns Complex(1)" do + (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)) + end + end + + describe "with Complex" do + it "returns self raised to the given power" do + (Complex(2, 1) ** Complex(2, 1)).should be_close(Complex(-0.504824688978319, 3.10414407699553), TOLERANCE) + (Complex(2, 1) ** Complex(3, 4)).should be_close(Complex(-0.179174656916581, -1.74071656397662), TOLERANCE) + + (Complex(2, 1) ** Complex(-2, -1)).should be_close(Complex(-0.051041070450869, -0.313849223270419), TOLERANCE) + (Complex(-2, -1) ** Complex(2, 1)).should be_close(Complex(-11.6819929610857, 71.8320439736158), TOLERANCE) + end + end + + describe "with Integer" do + it "returns self raised to the given power" do + (Complex(2, 1) ** 2).should == Complex(3, 4) + (Complex(3, 4) ** 2).should == Complex(-7, 24) + (Complex(3, 4) ** -2).should be_close(Complex(-0.0112, -0.0384), TOLERANCE) + + + (Complex(2, 1) ** 2.5).should be_close(Complex(2.99179707178602, 6.85206901006896), TOLERANCE) + (Complex(3, 4) ** 2.5).should be_close(Complex(-38.0, 41.0), TOLERANCE) + (Complex(3, 4) ** -2.5).should be_close(Complex(-0.01216, -0.01312), TOLERANCE) + + (Complex(1) ** 1).should == Complex(1) + + # NOTE: Takes way too long... + #(Complex(2, 1) ** bignum_value) + end + end + + describe "with Rational" do + it "returns self raised to the given power" do + (Complex(2, 1) ** Rational(3, 4)).should be_close(Complex(1.71913265276568, 0.623124744394697), TOLERANCE) + (Complex(2, 1) ** Rational(4, 3)).should be_close(Complex(2.3828547125173, 1.69466313833091), TOLERANCE) + (Complex(2, 1) ** Rational(-4, 3)).should be_close(Complex(0.278700377879388, -0.198209003071003), TOLERANCE) + 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([2, 5]) + (value ** obj).should == 2 ** 5 + end + end end diff --git a/spec/ruby/core/complex/imag_spec.rb b/spec/ruby/core/complex/imag_spec.rb index d8635389cc..2bafd1ab54 100644 --- a/spec/ruby/core/complex/imag_spec.rb +++ b/spec/ruby/core/complex/imag_spec.rb @@ -1,4 +1,5 @@ -require_relative '../../shared/complex/image' +require_relative '../../spec_helper' +require_relative 'shared/image' describe "Complex#imag" do it_behaves_like :complex_image, :imag diff --git a/spec/ruby/core/complex/imaginary_spec.rb b/spec/ruby/core/complex/imaginary_spec.rb index 325cce961a..a8a1bfea90 100644 --- a/spec/ruby/core/complex/imaginary_spec.rb +++ b/spec/ruby/core/complex/imaginary_spec.rb @@ -1,4 +1,5 @@ -require_relative '../../shared/complex/image' +require_relative '../../spec_helper' +require_relative 'shared/image' describe "Complex#imaginary" do it_behaves_like :complex_image, :imaginary diff --git a/spec/ruby/core/complex/inspect_spec.rb b/spec/ruby/core/complex/inspect_spec.rb index a1c9469d71..71aabde5be 100644 --- a/spec/ruby/core/complex/inspect_spec.rb +++ b/spec/ruby/core/complex/inspect_spec.rb @@ -1,5 +1,16 @@ -require_relative '../../shared/complex/inspect' +require_relative '../../spec_helper' describe "Complex#inspect" do - it_behaves_like :complex_inspect, :inspect + it "returns (${real}+${image}i) for positive imaginary parts" do + Complex(1).inspect.should == "(1+0i)" + Complex(7).inspect.should == "(7+0i)" + Complex(-1, 4).inspect.should == "(-1+4i)" + Complex(-7, 6.7).inspect.should == "(-7+6.7i)" + end + + it "returns (${real}-${image}i) for negative imaginary parts" do + Complex(0, -1).inspect.should == "(0-1i)" + Complex(-1, -4).inspect.should == "(-1-4i)" + Complex(-7, -6.7).inspect.should == "(-7-6.7i)" + end end diff --git a/spec/ruby/core/complex/integer_spec.rb b/spec/ruby/core/complex/integer_spec.rb index 54b420e62c..0957accb70 100644 --- a/spec/ruby/core/complex/integer_spec.rb +++ b/spec/ruby/core/complex/integer_spec.rb @@ -1,3 +1,5 @@ +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 diff --git a/spec/ruby/core/complex/magnitude_spec.rb b/spec/ruby/core/complex/magnitude_spec.rb index ae655f7acf..86f3b29868 100644 --- a/spec/ruby/core/complex/magnitude_spec.rb +++ b/spec/ruby/core/complex/magnitude_spec.rb @@ -1,4 +1,5 @@ -require_relative '../../shared/complex/abs' +require_relative '../../spec_helper' +require_relative 'shared/abs' describe "Complex#magnitude" do it_behaves_like :complex_abs, :magnitude diff --git a/spec/ruby/core/complex/minus_spec.rb b/spec/ruby/core/complex/minus_spec.rb index d391ae4890..7c104ce784 100644 --- a/spec/ruby/core/complex/minus_spec.rb +++ b/spec/ruby/core/complex/minus_spec.rb @@ -1,5 +1,45 @@ -require_relative '../../shared/complex/minus' +require_relative '../../spec_helper' describe "Complex#-" do - it_behaves_like :complex_minus, :- + describe "with Complex" do + it "subtracts both the real and imaginary components" do + (Complex(1, 2) - Complex(10, 20)).should == Complex(1 - 10, 2 - 20) + (Complex(1.5, 2.1) - Complex(100.2, -30.3)).should == Complex(1.5 - 100.2, 2.1 - (-30.3)) + end + end + + describe "with Integer" do + it "subtracts the real number from the real component of self" do + (Complex(1, 2) - 50).should == Complex(-49, 2) + (Complex(1, 2) - 50.5).should == Complex(-49.5, 2) + 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([2, 5]) + (value - obj).should == 2 - 5 + end + end + + describe "passed Numeric which responds to #real? with true" do + it "coerces the passed argument to the type of the real part and subtracts the resulting elements" do + n = mock_numeric('n') + n.should_receive(:real?).and_return(true) + n.should_receive(:coerce).with(1).and_return([1, 4]) + (Complex(1, 2) - n).should == Complex(-3, 2) + end + end + + describe "passed Numeric which responds to #real? with false" do + it "coerces the passed argument to Complex and subtracts the resulting elements" do + n = mock_numeric('n') + n.should_receive(:real?).and_return(false) + n.should_receive(:coerce).with(Complex(1, 2)).and_return([Complex(1, 2), Complex(3, 4)]) + (Complex(1, 2) - n).should == Complex(-2, -2) + end + end end diff --git a/spec/ruby/core/complex/multiply_spec.rb b/spec/ruby/core/complex/multiply_spec.rb index b43855f695..35bf7c8455 100644 --- a/spec/ruby/core/complex/multiply_spec.rb +++ b/spec/ruby/core/complex/multiply_spec.rb @@ -1,5 +1,49 @@ -require_relative '../../shared/complex/multiply' +require_relative '../../spec_helper' describe "Complex#*" do - it_behaves_like :complex_multiply, :* + describe "with Complex" do + it "multiplies according to the usual rule for complex numbers: (a + bi) * (c + di) = ac - bd + (ad + bc)i" do + (Complex(1, 2) * Complex(10, 20)).should == Complex((1 * 10) - (2 * 20), (1 * 20) + (2 * 10)) + (Complex(1.5, 2.1) * Complex(100.2, -30.3)).should == Complex((1.5 * 100.2) - (2.1 * -30.3), (1.5 * -30.3) + (2.1 * 100.2)) + end + end + + describe "with Integer" do + it "multiplies both parts of self by the given Integer" do + (Complex(3, 2) * 50).should == Complex(150, 100) + (Complex(-3, 2) * 50.5).should == Complex(-151.5, 101) + 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([2, 5]) + (value * obj).should == 2 * 5 + end + end + + describe "with a Numeric which responds to #real? with true" do + it "multiples both parts of self by other" do + other = mock_numeric('other') + real = mock_numeric('real') + imag = mock_numeric('imag') + other.should_receive(:real?).and_return(true) + real.should_receive(:*).with(other).and_return(1) + imag.should_receive(:*).with(other).and_return(2) + (Complex(real, imag) * other).should == Complex(1, 2) + end + + describe "with a Numeric which responds to #real? with false" do + it "coerces the passed argument to Complex and multiplies 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 == 10 + end + end + end end diff --git a/spec/ruby/core/complex/negative_spec.rb b/spec/ruby/core/complex/negative_spec.rb index 5b51933106..1ea5392f39 100644 --- a/spec/ruby/core/complex/negative_spec.rb +++ b/spec/ruby/core/complex/negative_spec.rb @@ -1,3 +1,5 @@ +require_relative '../../spec_helper' + describe "Complex#negative?" do it "is undefined" do c = Complex(1) diff --git a/spec/ruby/core/complex/numerator_spec.rb b/spec/ruby/core/complex/numerator_spec.rb index 527a703dfb..7ab66e6a61 100644 --- a/spec/ruby/core/complex/numerator_spec.rb +++ b/spec/ruby/core/complex/numerator_spec.rb @@ -1,5 +1,19 @@ -require_relative '../../shared/complex/numerator' +require_relative '../../spec_helper' describe "Complex#numerator" do - it_behaves_like :complex_numerator, :numerator + it "returns self's numerator" do + Complex(2).numerator.should == Complex(2) + Complex(3, 4).numerator.should == Complex(3, 4) + + Complex(Rational(3, 4), Rational(3, 4)).numerator.should == Complex(3, 3) + Complex(Rational(7, 4), Rational(8, 4)).numerator.should == Complex(7, 8) + + Complex(Rational(7, 8), Rational(8, 4)).numerator.should == Complex(7, 16) + Complex(Rational(7, 4), Rational(8, 8)).numerator.should == Complex(7, 4) + + # NOTE: + # Bug? - Fails with a MethodMissingError + # (undefined method `denominator' for 3.5:Float) + # Complex(3.5, 3.7).numerator + end end diff --git a/spec/ruby/core/complex/phase_spec.rb b/spec/ruby/core/complex/phase_spec.rb index da168d9e8e..89574bf533 100644 --- a/spec/ruby/core/complex/phase_spec.rb +++ b/spec/ruby/core/complex/phase_spec.rb @@ -1,5 +1,5 @@ require_relative '../../spec_helper' -require_relative '../../shared/complex/arg' +require_relative 'shared/arg' describe "Complex#phase" do it_behaves_like :complex_arg, :phase diff --git a/spec/ruby/core/complex/plus_spec.rb b/spec/ruby/core/complex/plus_spec.rb index 71cf499c38..2056ca786c 100644 --- a/spec/ruby/core/complex/plus_spec.rb +++ b/spec/ruby/core/complex/plus_spec.rb @@ -1,5 +1,45 @@ -require_relative '../../shared/complex/plus' +require_relative '../../spec_helper' describe "Complex#+" do - it_behaves_like :complex_plus, :+ + describe "with Complex" do + it "adds both the real and imaginary components" do + (Complex(1, 2) + Complex(10, 20)).should == Complex(1 + 10, 2 + 20) + (Complex(1.5, 2.1) + Complex(100.2, -30.3)).should == Complex(1.5 + 100.2, 2.1 + (-30.3)) + end + end + + describe "with Integer" do + it "adds the real number to the real component of self" do + (Complex(1, 2) + 50).should == Complex(51, 2) + (Complex(1, 2) + 50.5).should == Complex(51.5, 2) + 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([2, 5]) + (value + obj).should == 2 + 5 + end + end + + describe "passed Numeric which responds to #real? with true" do + it "coerces the passed argument to the type of the real part and adds the resulting elements" do + n = mock_numeric('n') + n.should_receive(:real?).and_return(true) + n.should_receive(:coerce).with(1).and_return([1, 4]) + (Complex(1, 2) + n).should == Complex(5, 2) + end + end + + describe "passed Numeric which responds to #real? with false" do + it "coerces the passed argument to Complex and adds the resulting elements" do + n = mock_numeric('n') + n.should_receive(:real?).and_return(false) + n.should_receive(:coerce).with(Complex(1, 2)).and_return([Complex(1, 2), Complex(3, 4)]) + (Complex(1, 2) + n).should == Complex(4, 6) + end + end end diff --git a/spec/ruby/core/complex/polar_spec.rb b/spec/ruby/core/complex/polar_spec.rb index 14ed67b0fc..f9d28b8388 100644 --- a/spec/ruby/core/complex/polar_spec.rb +++ b/spec/ruby/core/complex/polar_spec.rb @@ -1,7 +1,10 @@ -require_relative '../../shared/complex/polar' +require_relative '../../spec_helper' describe "Complex.polar" do - it_behaves_like :complex_polar_class, :polar + it "returns a complex number in terms of radius and angle" do + Complex.polar(50, 60).should be_close(Complex(-47.6206490207578, -15.2405310551108), TOLERANCE) + Complex.polar(-10, -20).should be_close(Complex(-4.08082061813392, 9.12945250727628), TOLERANCE) + end it "raises a TypeError when given non real arguments" do lambda{ Complex.polar(nil) }.should raise_error(TypeError) @@ -10,5 +13,15 @@ describe "Complex.polar" do end describe "Complex#polar" do - it_behaves_like :complex_polar, :polar + it "returns the absolute value and the argument" do + a = Complex(3, 4) + a.polar.size.should == 2 + a.polar.first.should == 5.0 + a.polar.last.should be_close(0.927295218001612, TOLERANCE) + + b = Complex(-3.5, 4.7) + b.polar.size.should == 2 + b.polar.first.should be_close(5.86003412959345, TOLERANCE) + b.polar.last.should be_close(2.21088447955664, TOLERANCE) + end end diff --git a/spec/ruby/core/complex/positive_spec.rb b/spec/ruby/core/complex/positive_spec.rb index 88898d31cf..eea0af3755 100644 --- a/spec/ruby/core/complex/positive_spec.rb +++ b/spec/ruby/core/complex/positive_spec.rb @@ -1,3 +1,5 @@ +require_relative '../../spec_helper' + describe "Complex#positive?" do it "is undefined" do c = Complex(1) diff --git a/spec/ruby/core/complex/quo_spec.rb b/spec/ruby/core/complex/quo_spec.rb index f305159ade..ee6fd65c79 100644 --- a/spec/ruby/core/complex/quo_spec.rb +++ b/spec/ruby/core/complex/quo_spec.rb @@ -1,4 +1,5 @@ -require_relative '../../shared/complex/divide' +require_relative '../../spec_helper' +require_relative 'shared/divide' describe "Complex#quo" do it_behaves_like :complex_divide, :quo diff --git a/spec/ruby/core/complex/rationalize_spec.rb b/spec/ruby/core/complex/rationalize_spec.rb index 2dd1e9b122..165f3d2700 100644 --- a/spec/ruby/core/complex/rationalize_spec.rb +++ b/spec/ruby/core/complex/rationalize_spec.rb @@ -1,3 +1,5 @@ +require_relative '../../spec_helper' + describe "Complex#rationalize" do it "raises RangeError if self has non-zero imaginary part" do lambda { Complex(1,5).rationalize }.should raise_error(RangeError) diff --git a/spec/ruby/core/complex/real_spec.rb b/spec/ruby/core/complex/real_spec.rb index 50726e9894..2ea791c005 100644 --- a/spec/ruby/core/complex/real_spec.rb +++ b/spec/ruby/core/complex/real_spec.rb @@ -1,7 +1,12 @@ -require_relative '../../shared/complex/real' +require_relative '../../spec_helper' describe "Complex#real" do - it_behaves_like :complex_real, :real + it "returns the real part of self" do + Complex(1, 0).real.should == 1 + Complex(2, 1).real.should == 2 + Complex(6.7, 8.9).real.should == 6.7 + Complex(bignum_value, 3).real.should == bignum_value + end end describe "Complex#real?" do diff --git a/spec/ruby/core/complex/rect_spec.rb b/spec/ruby/core/complex/rect_spec.rb index db6756dbbc..9e95f3efc2 100644 --- a/spec/ruby/core/complex/rect_spec.rb +++ b/spec/ruby/core/complex/rect_spec.rb @@ -1,4 +1,5 @@ -require_relative '../../shared/complex/rect' +require_relative '../../spec_helper' +require_relative 'shared/rect' describe "Complex#rect" do it_behaves_like :complex_rect, :rect diff --git a/spec/ruby/core/complex/rectangular_spec.rb b/spec/ruby/core/complex/rectangular_spec.rb index c1d9a74aa8..d4b8ad9782 100644 --- a/spec/ruby/core/complex/rectangular_spec.rb +++ b/spec/ruby/core/complex/rectangular_spec.rb @@ -1,4 +1,5 @@ -require_relative '../../shared/complex/rect' +require_relative '../../spec_helper' +require_relative 'shared/rect' describe "Complex#rectangular" do it_behaves_like :complex_rect, :rectangular diff --git a/spec/ruby/core/complex/shared/abs.rb b/spec/ruby/core/complex/shared/abs.rb new file mode 100644 index 0000000000..2299479341 --- /dev/null +++ b/spec/ruby/core/complex/shared/abs.rb @@ -0,0 +1,10 @@ +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 new file mode 100644 index 0000000000..c81f197433 --- /dev/null +++ b/spec/ruby/core/complex/shared/arg.rb @@ -0,0 +1,9 @@ +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 new file mode 100644 index 0000000000..d1ae47bcb6 --- /dev/null +++ b/spec/ruby/core/complex/shared/conjugate.rb @@ -0,0 +1,8 @@ +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 new file mode 100644 index 0000000000..e3701405b9 --- /dev/null +++ b/spec/ruby/core/complex/shared/divide.rb @@ -0,0 +1,82 @@ +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 + lambda { 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 new file mode 100644 index 0000000000..f839dbcaf9 --- /dev/null +++ b/spec/ruby/core/complex/shared/image.rb @@ -0,0 +1,8 @@ +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 new file mode 100644 index 0000000000..971821ac33 --- /dev/null +++ b/spec/ruby/core/complex/shared/rect.rb @@ -0,0 +1,94 @@ +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| + lambda { 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) + lambda { 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) + lambda { 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 + lambda { Complex.send(@method, :sym) }.should raise_error(TypeError) + lambda { Complex.send(@method, 0, :sym) }.should raise_error(TypeError) + end + end +end diff --git a/spec/ruby/core/complex/to_s_spec.rb b/spec/ruby/core/complex/to_s_spec.rb index edb1b2c493..989a7ae0b7 100644 --- a/spec/ruby/core/complex/to_s_spec.rb +++ b/spec/ruby/core/complex/to_s_spec.rb @@ -1,5 +1,44 @@ -require_relative '../../shared/complex/to_s' +require_relative '../../spec_helper' describe "Complex#to_s" do - it_behaves_like :complex_to_s, :to_s + describe "when self's real component is 0" do + it "returns both the real and imaginary component even when the real is 0" do + Complex(0, 5).to_s.should == "0+5i" + Complex(0, -3.2).to_s.should == "0-3.2i" + end + end + + it "returns self as String" do + Complex(1, 5).to_s.should == "1+5i" + Complex(-2.5, 1.5).to_s.should == "-2.5+1.5i" + + Complex(1, -5).to_s.should == "1-5i" + Complex(-2.5, -1.5).to_s.should == "-2.5-1.5i" + + # Guard against the Mathn library + guard -> { !defined?(Math.rsqrt) } do + Complex(1, 0).to_s.should == "1+0i" + Complex(1, -0).to_s.should == "1+0i" + end + end + + it "returns 1+0.0i for Complex(1, 0.0)" do + Complex(1, 0.0).to_s.should == "1+0.0i" + end + + it "returns 1-0.0i for Complex(1, -0.0)" do + Complex(1, -0.0).to_s.should == "1-0.0i" + end + + it "returns 1+Infinity*i for Complex(1, Infinity)" do + Complex(1, infinity_value).to_s.should == "1+Infinity*i" + end + + it "returns 1-Infinity*i for Complex(1, -Infinity)" do + Complex(1, -infinity_value).to_s.should == "1-Infinity*i" + end + + it "returns 1+NaN*i for Complex(1, NaN)" do + Complex(1, nan_value).to_s.should == "1+NaN*i" + end end diff --git a/spec/ruby/core/encoding/converter/convpath_spec.rb b/spec/ruby/core/encoding/converter/convpath_spec.rb index 5e8aa8c2a2..e41a6c4205 100644 --- a/spec/ruby/core/encoding/converter/convpath_spec.rb +++ b/spec/ruby/core/encoding/converter/convpath_spec.rb @@ -2,56 +2,17 @@ require_relative '../../../spec_helper' with_feature :encoding do describe "Encoding::Converter#convpath" do - before :all do - @perms = Encoding.name_list.permutation(2).map do |pair| - Encoding::Converter.new(pair.first, pair.last) rescue nil - end.compact.map{|ec| ec.convpath} - end - - it "returns an Array" do - ec = Encoding::Converter.new('ASCII', 'EUC-JP') - ec.convpath.should be_an_instance_of(Array) - end - - it "returns each encoding pair as a sub-Array" do - ec = Encoding::Converter.new('ASCII', 'EUC-JP') - ec.convpath.first.should be_an_instance_of(Array) - ec.convpath.first.size.should == 2 - end - - it "returns each encoding as an Encoding object" do - ec = Encoding::Converter.new('ASCII', 'EUC-JP') - ec.convpath.first.first.should be_an_instance_of(Encoding) - ec.convpath.first.last.should be_an_instance_of(Encoding) + it "returns an Array with a single element if there is a direct converter" do + cp = Encoding::Converter.new('ASCII', 'UTF-8').convpath + cp.should == [[Encoding::US_ASCII, Encoding::UTF_8]] end it "returns multiple encoding pairs when direct conversion is impossible" do - ec = Encoding::Converter.new('ascii','Big5') - ec.convpath.size.should == 2 - ec.convpath.first.first.should == Encoding::US_ASCII - ec.convpath.first.last.should == ec.convpath.last.first - ec.convpath.last.last.should == Encoding::Big5 - end - - it "sets the last element of each pair to the first element of the next" do - @perms.each do |convpath| - next if convpath.size == 1 - convpath.each_with_index do |pair, idx| - break if idx == convpath.size - 1 - pair.last.should == convpath[idx+1].first - end - end - end - - it "only lists a source encoding once" do - @perms.each do |convpath| - next if convpath.size < 2 - seen = Hash.new(false) - convpath.each_with_index do |pair, idx| - seen.key?(pair.first).should be_false if idx > 0 - seen[pair.first] = true - end - end + cp = Encoding::Converter.new('ascii','Big5').convpath + cp.should == [ + [Encoding::US_ASCII, Encoding::UTF_8], + [Encoding::UTF_8, Encoding::Big5] + ] end it "indicates if crlf_newline conversion would occur" do diff --git a/spec/ruby/core/encoding/converter/search_convpath_spec.rb b/spec/ruby/core/encoding/converter/search_convpath_spec.rb index 0e305a1236..9e924da023 100644 --- a/spec/ruby/core/encoding/converter/search_convpath_spec.rb +++ b/spec/ruby/core/encoding/converter/search_convpath_spec.rb @@ -1,70 +1,23 @@ -# frozen_string_literal: true - require_relative '../../../spec_helper' with_feature :encoding do describe "Encoding::Converter.search_convpath" do - before :all do - t = [] - temp = ''.dup -# Encoding.list.reject { |e| e.dummy? }.map { |e| e.to_s }.permutation(2).each { |a| t << a if Array === a } -# Encoding.list.map { |e| e.to_s }.permutation(2).each { |a| t << a if Array === a } -# Encoding.name_list.permutation(2).each { |a| t << a if Array === a } - Encoding.name_list.permutation(2).each { |a| t << a if Array === a } - @perms = t.map do |a, b| - temp << "#{a.ljust(15)} #{b}" - Encoding::Converter.search_convpath(a, b) rescue nil - end.compact - end - - it "returns an Array" do - Encoding::Converter.search_convpath('ASCII', 'EUC-JP').\ - should be_an_instance_of(Array) - end - - it "returns each encoding pair as a sub-Array" do - cp = Encoding::Converter.search_convpath('ASCII', 'EUC-JP') - cp.first.should be_an_instance_of(Array) - cp.first.size.should == 2 - end - - it "returns each encoding as an Encoding object" do - cp = Encoding::Converter.search_convpath('ASCII', 'EUC-JP') - cp.first.first.should be_an_instance_of(Encoding) - cp.first.last.should be_an_instance_of(Encoding) + it "returns an Array with a single element if there is a direct converter" do + cp = Encoding::Converter.search_convpath('ASCII', 'UTF-8') + cp.should == [[Encoding::US_ASCII, Encoding::UTF_8]] end it "returns multiple encoding pairs when direct conversion is impossible" do cp = Encoding::Converter.search_convpath('ascii','Big5') - cp.size.should == 2 - cp.first.should == [Encoding::US_ASCII, Encoding::UTF_8] - cp.last.should == [Encoding::UTF_8, Encoding::Big5] - end - - it "sets the last element of each pair to the first element of the next" do - @perms.each do |convpath| - next if convpath.size == 1 - convpath.each_with_index do |pair, idx| - break if idx == convpath.size - 1 - pair.last.should == convpath[idx+1].first - end - end - end - - it "only lists a source encoding once" do - @perms.each do |convpath| - next if convpath.size < 2 - seen = Hash.new(false) - convpath.each_with_index do |pair, idx| - seen.key?(pair.first).should be_false if idx > 0 - seen[pair.first] = true - end - end + cp.should == [ + [Encoding::US_ASCII, Encoding::UTF_8], + [Encoding::UTF_8, Encoding::Big5] + ] end it "indicates if crlf_newline conversion would occur" do cp = Encoding::Converter.search_convpath( - "ISo-8859-1", "EUC-JP", {crlf_newline: true}) + "ISO-8859-1", "EUC-JP", {crlf_newline: true}) cp.last.should == "crlf_newline" cp = Encoding::Converter.search_convpath( @@ -78,13 +31,10 @@ with_feature :encoding do # Encoding::ASCII_8BIT, Encoding::Emacs_Mule) # end.should raise_error(Encoding::ConverterNotFoundError) begin - Encoding::Converter.search_convpath(Encoding::ASCII_8BIT.to_s, Encoding::Emacs_Mule) + Encoding::Converter.search_convpath(Encoding::ASCII_8BIT, Encoding::Emacs_Mule) rescue => e e.class.should == Encoding::ConverterNotFoundError - else - e.class.should == Encoding::ConverterNotFoundError end - end end end diff --git a/spec/ruby/core/float/abs_spec.rb b/spec/ruby/core/float/abs_spec.rb index 8f016d0cd9..a08601926d 100644 --- a/spec/ruby/core/float/abs_spec.rb +++ b/spec/ruby/core/float/abs_spec.rb @@ -1,3 +1,4 @@ +require_relative '../../spec_helper' require_relative 'shared/abs' describe "Float#abs" do diff --git a/spec/ruby/core/float/angle_spec.rb b/spec/ruby/core/float/angle_spec.rb index 9247b17cb4..c07249aa97 100644 --- a/spec/ruby/core/float/angle_spec.rb +++ b/spec/ruby/core/float/angle_spec.rb @@ -1,4 +1,5 @@ -require_relative '../../shared/complex/float/arg' +require_relative '../../spec_helper' +require_relative 'shared/arg' describe "Float#angle" do it_behaves_like :float_arg, :angle diff --git a/spec/ruby/core/float/arg_spec.rb b/spec/ruby/core/float/arg_spec.rb index 971fe06b45..d3a50668f8 100644 --- a/spec/ruby/core/float/arg_spec.rb +++ b/spec/ruby/core/float/arg_spec.rb @@ -1,4 +1,5 @@ -require_relative '../../shared/complex/float/arg' +require_relative '../../spec_helper' +require_relative 'shared/arg' describe "Float#arg" do it_behaves_like :float_arg, :arg diff --git a/spec/ruby/core/float/phase_spec.rb b/spec/ruby/core/float/phase_spec.rb index cebc7954ac..2aa84024b4 100644 --- a/spec/ruby/core/float/phase_spec.rb +++ b/spec/ruby/core/float/phase_spec.rb @@ -1,4 +1,5 @@ -require_relative '../../shared/complex/float/arg' +require_relative '../../spec_helper' +require_relative 'shared/arg' describe "Float#phase" do it_behaves_like :float_arg, :phase diff --git a/spec/ruby/core/float/shared/arg.rb b/spec/ruby/core/float/shared/arg.rb new file mode 100644 index 0000000000..136cf19ec8 --- /dev/null +++ b/spec/ruby/core/float/shared/arg.rb @@ -0,0 +1,36 @@ +describe :float_arg, shared: true do + it "returns NaN if NaN" do + f = nan_value + f.send(@method).nan?.should be_true + end + + it "returns self if NaN" do + f = nan_value + f.send(@method).should equal(f) + end + + it "returns 0 if positive" do + 1.0.send(@method).should == 0 + end + + it "returns 0 if +0.0" do + 0.0.send(@method).should == 0 + end + + it "returns 0 if +Infinity" do + infinity_value.send(@method).should == 0 + end + + it "returns Pi if negative" do + (-1.0).send(@method).should == Math::PI + end + + # This was established in r23960 + it "returns Pi if -0.0" do + (-0.0).send(@method).should == Math::PI + end + + it "returns Pi if -Infinity" do + (-infinity_value).send(@method).should == Math::PI + end +end diff --git a/spec/ruby/core/kernel/Complex_spec.rb b/spec/ruby/core/kernel/Complex_spec.rb index b132292161..f738f43a10 100644 --- a/spec/ruby/core/kernel/Complex_spec.rb +++ b/spec/ruby/core/kernel/Complex_spec.rb @@ -1,6 +1,141 @@ require_relative '../../spec_helper' -require_relative '../../shared/complex/Complex' describe "Kernel.Complex()" do - it_behaves_like :kernel_Complex, :Complex + describe "when passed [Complex, Complex]" do + it "returns a new Complex number based on the two given numbers" do + Complex(Complex(3, 4), Complex(5, 6)).should == Complex(3 - 6, 4 + 5) + Complex(Complex(1.5, 2), Complex(-5, 6.3)).should == Complex(1.5 - 6.3, 2 - 5) + end + end + + describe "when passed [Complex]" do + it "returns the passed Complex number" do + Complex(Complex(1, 2)).should == Complex(1, 2) + Complex(Complex(-3.4, bignum_value)).should == Complex(-3.4, bignum_value) + end + end + + describe "when passed [Integer, Integer]" do + it "returns a new Complex number" do + Complex(1, 2).should be_an_instance_of(Complex) + Complex(1, 2).real.should == 1 + Complex(1, 2).imag.should == 2 + + Complex(-3, -5).should be_an_instance_of(Complex) + Complex(-3, -5).real.should == -3 + Complex(-3, -5).imag.should == -5 + + Complex(3.5, -4.5).should be_an_instance_of(Complex) + Complex(3.5, -4.5).real.should == 3.5 + Complex(3.5, -4.5).imag.should == -4.5 + + Complex(bignum_value, 30).should be_an_instance_of(Complex) + Complex(bignum_value, 30).real.should == bignum_value + Complex(bignum_value, 30).imag.should == 30 + end + end + + describe "when passed [Integer/Float]" do + it "returns a new Complex number with 0 as the imaginary component" do + # Guard against the Mathn library + guard -> { !defined?(Math.rsqrt) } do + Complex(1).should be_an_instance_of(Complex) + Complex(1).imag.should == 0 + Complex(1).real.should == 1 + + Complex(-3).should be_an_instance_of(Complex) + Complex(-3).imag.should == 0 + Complex(-3).real.should == -3 + + Complex(-4.5).should be_an_instance_of(Complex) + Complex(-4.5).imag.should == 0 + Complex(-4.5).real.should == -4.5 + + Complex(bignum_value).should be_an_instance_of(Complex) + Complex(bignum_value).imag.should == 0 + Complex(bignum_value).real.should == bignum_value + end + end + end + + describe "when passed a String" do + it "needs to be reviewed for spec completeness" + end + + describe "when passed an Object which responds to #to_c" do + it "returns the passed argument" do + obj = Object.new; def obj.to_c; 1i end + Complex(obj).should == Complex(0, 1) + end + end + + describe "when passed a Numeric which responds to #real? with false" do + it "returns the passed argument" do + n = mock_numeric("unreal") + n.should_receive(:real?).any_number_of_times.and_return(false) + Complex(n).should equal(n) + end + end + + describe "when passed a Numeric which responds to #real? with true" do + it "returns a Complex with the passed argument as the real component and 0 as the imaginary component" do + n = mock_numeric("real") + n.should_receive(:real?).any_number_of_times.and_return(true) + result = Complex(n) + result.real.should equal(n) + result.imag.should equal(0) + end + end + + describe "when 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 "returns n1 + n2 * Complex(0, 1)" do + n1 = mock_numeric("n1") + n2 = mock_numeric("n2") + n3 = mock_numeric("n3") + n4 = mock_numeric("n4") + n1.should_receive(:real?).any_number_of_times.and_return(r1) + n2.should_receive(:real?).any_number_of_times.and_return(r2) + n2.should_receive(:*).with(Complex(0, 1)).and_return(n3) + n1.should_receive(:+).with(n3).and_return(n4) + Complex(n1, n2).should equal(n4) + end + end + end + + describe "when passed two Numerics and both respond to #real? with true" do + it "returns a Complex with the passed arguments as real and imaginary components respectively" 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(n1, n2) + result.real.should equal(n1) + result.imag.should equal(n2) + end + end + + describe "when passed a single non-Numeric" do + it "coerces the passed argument using #to_c" do + n = mock("n") + c = Complex(0, 0) + n.should_receive(:to_c).and_return(c) + Complex(n).should equal(c) + end + end + + describe "when passed a non-Numeric second argument" do + it "raises TypeError" do + lambda { Complex.send(@method, :sym, :sym) }.should raise_error(TypeError) + lambda { Complex.send(@method, 0, :sym) }.should raise_error(TypeError) + end + end + + describe "when passed nil" do + it "raises TypeError" do + lambda { Complex(nil) }.should raise_error(TypeError, "can't convert nil into Complex") + lambda { Complex(0, nil) }.should raise_error(TypeError, "can't convert nil into Complex") + lambda { Complex(nil, 0) }.should raise_error(TypeError, "can't convert nil into Complex") + end + end end diff --git a/spec/ruby/core/kernel/caller_spec.rb b/spec/ruby/core/kernel/caller_spec.rb index c4c2752859..8469319c94 100644 --- a/spec/ruby/core/kernel/caller_spec.rb +++ b/spec/ruby/core/kernel/caller_spec.rb @@ -34,4 +34,13 @@ describe 'Kernel#caller' do locations[0].should include("#{__FILE__}:#{line}:in") end + + it "returns an Array with the block given to #at_exit at the base of the stack" do + path = fixture(__FILE__, "caller_at_exit.rb") + lines = ruby_exe(path).lines + lines.should == [ + "#{path}:6:in `foo'\n", + "#{path}:2:in `block in
'\n" + ] + end end diff --git a/spec/ruby/core/kernel/fixtures/caller_at_exit.rb b/spec/ruby/core/kernel/fixtures/caller_at_exit.rb new file mode 100644 index 0000000000..ca9d808597 --- /dev/null +++ b/spec/ruby/core/kernel/fixtures/caller_at_exit.rb @@ -0,0 +1,7 @@ +at_exit { + foo +} + +def foo + puts caller(0) +end diff --git a/spec/ruby/core/kernel/lambda_spec.rb b/spec/ruby/core/kernel/lambda_spec.rb index 52f340dde5..4796d65352 100644 --- a/spec/ruby/core/kernel/lambda_spec.rb +++ b/spec/ruby/core/kernel/lambda_spec.rb @@ -16,6 +16,21 @@ describe "Kernel.lambda" do l.lambda?.should be_true end + it "creates a lambda-style Proc if given a literal block via #send" do + l = send(:lambda) { 42 } + l.lambda?.should be_true + end + + it "creates a lambda-style Proc if given a literal block via #__send__" do + l = __send__(:lambda) { 42 } + l.lambda?.should be_true + end + + it "creates a lambda-style Proc if given a literal block via Kernel.public_send" do + l = Kernel.public_send(:lambda) { 42 } + l.lambda?.should be_true + end + it "returned the passed Proc if given an existing Proc" do some_proc = proc {} l = lambda(&some_proc) diff --git a/spec/ruby/core/marshal/dump_spec.rb b/spec/ruby/core/marshal/dump_spec.rb index f214abc31d..644a88b4a3 100644 --- a/spec/ruby/core/marshal/dump_spec.rb +++ b/spec/ruby/core/marshal/dump_spec.rb @@ -566,6 +566,11 @@ describe "Marshal.dump" do lambda { Marshal.dump(/(.)/.match("foo")) }.should raise_error(TypeError) end + it "raises a TypeError if dumping a Mutex instance" do + m = Mutex.new + lambda { Marshal.dump(m) }.should raise_error(TypeError) + end + it "returns an untainted string if object is untainted" do Marshal.dump(Object.new).tainted?.should be_false end diff --git a/spec/ruby/core/math/acos_spec.rb b/spec/ruby/core/math/acos_spec.rb index 20af20a000..0f104b8efc 100644 --- a/spec/ruby/core/math/acos_spec.rb +++ b/spec/ruby/core/math/acos_spec.rb @@ -20,14 +20,12 @@ describe "Math.acos" do Math.acos(0.75).should be_close(0.722734247813416, TOLERANCE) end - conflicts_with :Complex do - it "raises an Errno::EDOM if the argument is greater than 1.0" do - lambda { Math.acos(1.0001) }.should raise_error(Errno::EDOM) - end - - it "raises an Errno::EDOM if the argument is less than -1.0" do - lambda { Math.acos(-1.0001) }.should raise_error(Errno::EDOM) - end + it "raises an Math::DomainError if the argument is greater than 1.0" do + lambda { Math.acos(1.0001) }.should raise_error(Math::DomainError) + end + + it "raises an Math::DomainError if the argument is less than -1.0" do + lambda { Math.acos(-1.0001) }.should raise_error(Math::DomainError) end it "raises a TypeError if the string argument cannot be coerced with Float()" do diff --git a/spec/ruby/core/math/acosh_spec.rb b/spec/ruby/core/math/acosh_spec.rb index 6616ea3faa..26c18a530f 100644 --- a/spec/ruby/core/math/acosh_spec.rb +++ b/spec/ruby/core/math/acosh_spec.rb @@ -11,12 +11,10 @@ describe "Math.acosh" do Math.acosh(1.0).should be_close(0.0, TOLERANCE) end - conflicts_with :Complex do - it "raises Errno::EDOM if the passed argument is less than -1.0 or greater than 1.0" do - lambda { Math.acosh(1.0 - TOLERANCE) }.should raise_error(Errno::EDOM) - lambda { Math.acosh(0) }.should raise_error(Errno::EDOM) - lambda { Math.acosh(-1.0) }.should raise_error(Errno::EDOM) - end + it "raises Math::DomainError if the passed argument is less than -1.0 or greater than 1.0" do + lambda { Math.acosh(1.0 - TOLERANCE) }.should raise_error(Math::DomainError) + lambda { Math.acosh(0) }.should raise_error(Math::DomainError) + lambda { Math.acosh(-1.0) }.should raise_error(Math::DomainError) end it "raises a TypeError if the argument cannot be coerced with Float()" do diff --git a/spec/ruby/core/math/asin_spec.rb b/spec/ruby/core/math/asin_spec.rb index e569ce65c4..8591ea80ea 100644 --- a/spec/ruby/core/math/asin_spec.rb +++ b/spec/ruby/core/math/asin_spec.rb @@ -16,14 +16,12 @@ describe "Math.asin" do Math.asin(0.75).should be_close(0.8480620789814816,TOLERANCE) end - conflicts_with :Complex do - it "raises an Errno::EDOM if the argument is greater than 1.0" do - lambda { Math.asin(1.0001) }.should raise_error( Errno::EDOM) - end - - it "raises an Errno::EDOM if the argument is less than -1.0" do - lambda { Math.asin(-1.0001) }.should raise_error( Errno::EDOM) - end + it "raises an Math::DomainError if the argument is greater than 1.0" do + lambda { Math.asin(1.0001) }.should raise_error( Math::DomainError) + end + + it "raises an Math::DomainError if the argument is less than -1.0" do + lambda { Math.asin(-1.0001) }.should raise_error( Math::DomainError) end it "raises a TypeError if the argument cannot be coerced with Float()" do diff --git a/spec/ruby/core/math/log10_spec.rb b/spec/ruby/core/math/log10_spec.rb index 4f2df87760..2c594999b7 100644 --- a/spec/ruby/core/math/log10_spec.rb +++ b/spec/ruby/core/math/log10_spec.rb @@ -15,10 +15,8 @@ describe "Math.log10" do Math.log10(10e15).should be_close(16.0, TOLERANCE) end - conflicts_with :Complex do - it "raises an Errno::EDOM if the argument is less than 0" do - lambda { Math.log10(-1e-15) }.should raise_error( Errno::EDOM) - end + it "raises an Math::DomainError if the argument is less than 0" do + lambda { Math.log10(-1e-15) }.should raise_error(Math::DomainError) end it "raises a TypeError if the argument cannot be coerced with Float()" do diff --git a/spec/ruby/core/math/log_spec.rb b/spec/ruby/core/math/log_spec.rb index 2d9fbeff03..c09e270c06 100644 --- a/spec/ruby/core/math/log_spec.rb +++ b/spec/ruby/core/math/log_spec.rb @@ -15,10 +15,8 @@ describe "Math.log" do Math.log(10e15).should be_close(36.8413614879047, TOLERANCE) end - conflicts_with :Complex do - it "raises an Errno::EDOM if the argument is less than 0" do - lambda { Math.log(-1e-15) }.should raise_error(Errno::EDOM) - end + it "raises an Math::DomainError if the argument is less than 0" do + lambda { Math.log(-1e-15) }.should raise_error(Math::DomainError) end it "raises a TypeError if the argument cannot be coerced with Float()" do diff --git a/spec/ruby/core/module/autoload_spec.rb b/spec/ruby/core/module/autoload_spec.rb index 89c061ff47..d6af169cff 100644 --- a/spec/ruby/core/module/autoload_spec.rb +++ b/spec/ruby/core/module/autoload_spec.rb @@ -199,6 +199,65 @@ describe "Module#autoload" do end end + describe "the autoload is removed when the same file is required directly without autoload" do + before :each do + module ModuleSpecs::Autoload + autoload :RequiredDirectly, fixture(__FILE__, "autoload_required_directly.rb") + end + @path = fixture(__FILE__, "autoload_required_directly.rb") + @check = -> { + [ + defined?(ModuleSpecs::Autoload::RequiredDirectly), + ModuleSpecs::Autoload.autoload?(:RequiredDirectly) + ] + } + ScratchPad.record @check + end + + after :each do + ModuleSpecs::Autoload.send(:remove_const, :RequiredDirectly) + end + + it "with a full path" do + @check.call.should == ["constant", @path] + require @path + ScratchPad.recorded.should == [nil, nil] + @check.call.should == ["constant", nil] + end + + it "with a relative path" do + @check.call.should == ["constant", @path] + $:.push File.dirname(@path) + begin + require "autoload_required_directly.rb" + ensure + $:.pop + end + ScratchPad.recorded.should == [nil, nil] + @check.call.should == ["constant", nil] + end + + it "in a nested require" do + nested = fixture(__FILE__, "autoload_required_directly_nested.rb") + nested_require = -> { + result = nil + ScratchPad.record -> { + result = [@check.call, Thread.new { @check.call }.value] + } + require nested + result + } + ScratchPad.record nested_require + + @check.call.should == ["constant", @path] + require @path + cur, other = ScratchPad.recorded + cur.should == [nil, nil] + other.should == [nil, nil] + @check.call.should == ["constant", nil] + end + end + describe "during the autoload before the constant is assigned" do before :each do @path = fixture(__FILE__, "autoload_during_autoload.rb") diff --git a/spec/ruby/core/module/fixtures/autoload_required_directly.rb b/spec/ruby/core/module/fixtures/autoload_required_directly.rb new file mode 100644 index 0000000000..bed60a71ec --- /dev/null +++ b/spec/ruby/core/module/fixtures/autoload_required_directly.rb @@ -0,0 +1,7 @@ +block = ScratchPad.recorded +ScratchPad.record(block.call) + +module ModuleSpecs::Autoload + class RequiredDirectly + end +end diff --git a/spec/ruby/core/module/fixtures/autoload_required_directly_nested.rb b/spec/ruby/core/module/fixtures/autoload_required_directly_nested.rb new file mode 100644 index 0000000000..a9f11c2188 --- /dev/null +++ b/spec/ruby/core/module/fixtures/autoload_required_directly_nested.rb @@ -0,0 +1 @@ +ScratchPad.recorded.call diff --git a/spec/ruby/core/numeric/abs_spec.rb b/spec/ruby/core/numeric/abs_spec.rb index ac4a7053f1..8bec50e337 100644 --- a/spec/ruby/core/numeric/abs_spec.rb +++ b/spec/ruby/core/numeric/abs_spec.rb @@ -1,3 +1,4 @@ +require_relative '../../spec_helper' require_relative 'shared/abs' describe "Numeric#abs" do diff --git a/spec/ruby/core/numeric/angle_spec.rb b/spec/ruby/core/numeric/angle_spec.rb index 2df5906be1..bb38165777 100644 --- a/spec/ruby/core/numeric/angle_spec.rb +++ b/spec/ruby/core/numeric/angle_spec.rb @@ -1,5 +1,5 @@ require_relative '../../spec_helper' -require_relative '../../shared/complex/numeric/arg' +require_relative 'shared/arg' describe "Numeric#angle" do it_behaves_like :numeric_arg, :angle diff --git a/spec/ruby/core/numeric/arg_spec.rb b/spec/ruby/core/numeric/arg_spec.rb index 37f4bff9b8..ba3b57c687 100644 --- a/spec/ruby/core/numeric/arg_spec.rb +++ b/spec/ruby/core/numeric/arg_spec.rb @@ -1,5 +1,5 @@ require_relative '../../spec_helper' -require_relative '../../shared/complex/numeric/arg' +require_relative 'shared/arg' describe "Numeric#arg" do it_behaves_like :numeric_arg, :arg diff --git a/spec/ruby/core/numeric/conj_spec.rb b/spec/ruby/core/numeric/conj_spec.rb index 59875829bf..7d4777ca60 100644 --- a/spec/ruby/core/numeric/conj_spec.rb +++ b/spec/ruby/core/numeric/conj_spec.rb @@ -1,5 +1,5 @@ require_relative '../../spec_helper' -require_relative '../../shared/complex/numeric/conj' +require_relative 'shared/conj' describe "Numeric#conj" do it_behaves_like :numeric_conj, :conj diff --git a/spec/ruby/core/numeric/conjugate_spec.rb b/spec/ruby/core/numeric/conjugate_spec.rb index 77d70d99a4..99854766e7 100644 --- a/spec/ruby/core/numeric/conjugate_spec.rb +++ b/spec/ruby/core/numeric/conjugate_spec.rb @@ -1,5 +1,5 @@ require_relative '../../spec_helper' -require_relative '../../shared/complex/numeric/conj' +require_relative 'shared/conj' describe "Numeric#conjugate" do it_behaves_like :numeric_conj, :conjugate diff --git a/spec/ruby/core/numeric/imag_spec.rb b/spec/ruby/core/numeric/imag_spec.rb index c72593cd1d..b9e343cee9 100644 --- a/spec/ruby/core/numeric/imag_spec.rb +++ b/spec/ruby/core/numeric/imag_spec.rb @@ -1,5 +1,5 @@ require_relative '../../spec_helper' -require_relative '../../shared/complex/numeric/imag' +require_relative 'shared/imag' describe "Numeric#imag" do it_behaves_like :numeric_imag, :imag diff --git a/spec/ruby/core/numeric/imaginary_spec.rb b/spec/ruby/core/numeric/imaginary_spec.rb index 59d9c1bb54..ec708cb505 100644 --- a/spec/ruby/core/numeric/imaginary_spec.rb +++ b/spec/ruby/core/numeric/imaginary_spec.rb @@ -1,5 +1,5 @@ require_relative '../../spec_helper' -require_relative '../../shared/complex/numeric/imag' +require_relative 'shared/imag' describe "Numeric#imaginary" do it_behaves_like :numeric_imag, :imaginary diff --git a/spec/ruby/core/numeric/phase_spec.rb b/spec/ruby/core/numeric/phase_spec.rb index d8309397d1..bc1995303f 100644 --- a/spec/ruby/core/numeric/phase_spec.rb +++ b/spec/ruby/core/numeric/phase_spec.rb @@ -1,5 +1,5 @@ require_relative '../../spec_helper' -require_relative '../../shared/complex/numeric/arg' +require_relative 'shared/arg' describe "Numeric#phase" do it_behaves_like :numeric_arg, :phase diff --git a/spec/ruby/core/numeric/polar_spec.rb b/spec/ruby/core/numeric/polar_spec.rb index b5aec52eec..b594e408b2 100644 --- a/spec/ruby/core/numeric/polar_spec.rb +++ b/spec/ruby/core/numeric/polar_spec.rb @@ -1,6 +1,50 @@ require_relative '../../spec_helper' -require_relative '../../shared/complex/numeric/polar' describe "Numeric#polar" do - it_behaves_like :numeric_polar, :polar + before :each do + @pos_numbers = [ + 1, + 3898172610**9, + 987.18273, + Float::MAX, + Rational(13,7), + infinity_value, + ] + @neg_numbers = @pos_numbers.map {|n| -n} + @numbers = @pos_numbers + @neg_numbers + @numbers.push(0, 0.0) + end + + it "returns a two-element Array" do + @numbers.each do |number| + number.polar.should be_an_instance_of(Array) + number.polar.size.should == 2 + end + end + + it "sets the first value to the absolute value of self" do + @numbers.each do |number| + number.polar.first.should == number.abs + end + end + + it "sets the last value to 0 if self is positive" do + (@numbers - @neg_numbers).each do |number| + number.should >= 0 + number.polar.last.should == 0 + end + end + + it "sets the last value to Pi if self is negative" do + @neg_numbers.each do |number| + number.should < 0 + number.polar.last.should == Math::PI + end + end + + it "returns [NaN, NaN] if self is NaN" do + nan_value.polar.size.should == 2 + nan_value.polar.first.nan?.should be_true + nan_value.polar.last.nan?.should be_true + end end diff --git a/spec/ruby/core/numeric/real_spec.rb b/spec/ruby/core/numeric/real_spec.rb index d5d6a097f9..8c318dce0f 100644 --- a/spec/ruby/core/numeric/real_spec.rb +++ b/spec/ruby/core/numeric/real_spec.rb @@ -1,9 +1,33 @@ require_relative '../../spec_helper' -require_relative '../../shared/complex/numeric/real' require_relative 'fixtures/classes' describe "Numeric#real" do - it_behaves_like :numeric_real, :real + before :each do + @numbers = [ + 20, # Integer + 398.72, # Float + Rational(3, 4), # Rational + bignum_value, # Bignum + infinity_value, + nan_value + ].map{ |n| [n, -n] }.flatten + end + + it "returns self" do + @numbers.each do |number| + if number.to_f.nan? + number.real.nan?.should be_true + else + number.real.should == number + end + end + end + + it "raises an ArgumentError if given any arguments" do + @numbers.each do |number| + lambda { number.real(number) }.should raise_error(ArgumentError) + end + end end describe "Numeric#real?" do diff --git a/spec/ruby/core/numeric/shared/arg.rb b/spec/ruby/core/numeric/shared/arg.rb new file mode 100644 index 0000000000..c8e7ad8333 --- /dev/null +++ b/spec/ruby/core/numeric/shared/arg.rb @@ -0,0 +1,38 @@ +require_relative '../../../spec_helper' + +describe :numeric_arg, shared: true do + before :each do + @numbers = [ + 20, + Rational(3, 4), + bignum_value, + infinity_value + ] + end + + it "returns 0 if positive" do + @numbers.each do |number| + number.send(@method).should == 0 + end + end + + it "returns Pi if negative" do + @numbers.each do |number| + (0-number).send(@method).should == Math::PI + end + end + + describe "with a Numeric subclass" do + it "returns 0 if self#<(0) returns false" do + numeric = mock_numeric('positive') + numeric.should_receive(:<).with(0).and_return(false) + numeric.send(@method).should == 0 + end + + it "returns Pi if self#<(0) returns true" do + numeric = mock_numeric('positive') + numeric.should_receive(:<).with(0).and_return(true) + numeric.send(@method).should == Math::PI + end + end +end diff --git a/spec/ruby/core/numeric/shared/conj.rb b/spec/ruby/core/numeric/shared/conj.rb new file mode 100644 index 0000000000..6d5197ecab --- /dev/null +++ b/spec/ruby/core/numeric/shared/conj.rb @@ -0,0 +1,20 @@ +require_relative '../../../spec_helper' + +describe :numeric_conj, shared: true do + before :each do + @numbers = [ + 20, # Integer + 398.72, # Float + Rational(3, 4), # Rational + bignum_value, + infinity_value, + nan_value + ] + end + + it "returns self" do + @numbers.each do |number| + number.send(@method).should equal(number) + end + end +end diff --git a/spec/ruby/core/numeric/shared/imag.rb b/spec/ruby/core/numeric/shared/imag.rb new file mode 100644 index 0000000000..11daf0af55 --- /dev/null +++ b/spec/ruby/core/numeric/shared/imag.rb @@ -0,0 +1,26 @@ +require_relative '../../../spec_helper' + +describe :numeric_imag, shared: true do + before :each do + @numbers = [ + 20, # Integer + 398.72, # Float + Rational(3, 4), # Rational + bignum_value, # Bignum + infinity_value, + nan_value + ].map{|n| [n,-n]}.flatten + end + + it "returns 0" do + @numbers.each do |number| + number.send(@method).should == 0 + end + end + + it "raises an ArgumentError if given any arguments" do + @numbers.each do |number| + lambda { number.send(@method, number) }.should raise_error(ArgumentError) + end + end +end diff --git a/spec/ruby/core/proc/source_location_spec.rb b/spec/ruby/core/proc/source_location_spec.rb index 772ca7876d..89409c357b 100644 --- a/spec/ruby/core/proc/source_location_spec.rb +++ b/spec/ruby/core/proc/source_location_spec.rb @@ -69,4 +69,18 @@ describe "Proc#source_location" do ProcSpecs::SourceLocation.my_detached_proc_new.source_location.last.should == 51 ProcSpecs::SourceLocation.my_detached_lambda.source_location.last.should == 46 end + + it "returns the same value for a proc-ified method as the method reports" do + method = ProcSpecs::SourceLocation.method(:my_proc) + proc = method.to_proc + + method.source_location.should == proc.source_location + end + + it "returns nil for a core method that has been proc-ified" do + method = [].method(:<<) + proc = method.to_proc + + proc.source_location.should == nil + end end diff --git a/spec/ruby/core/rational/integer_spec.rb b/spec/ruby/core/rational/integer_spec.rb index 451e42ee14..0f9a3bdead 100644 --- a/spec/ruby/core/rational/integer_spec.rb +++ b/spec/ruby/core/rational/integer_spec.rb @@ -1,6 +1,9 @@ describe "Rational#integer?" do - it "returns false for a rational with a numerator and no denominator" do - Rational(20).integer?.should be_false + # Guard against the Mathn library + guard -> { !defined?(Math.rsqrt) } do + it "returns false for a rational with a numerator and no denominator" do + Rational(20).integer?.should be_false + end end it "returns false for a rational with a numerator and a denominator" do diff --git a/spec/ruby/core/struct/shared/select.rb b/spec/ruby/core/struct/shared/select.rb index a488dfd656..35abee461b 100644 --- a/spec/ruby/core/struct/shared/select.rb +++ b/spec/ruby/core/struct/shared/select.rb @@ -3,7 +3,8 @@ require_relative '../fixtures/classes' describe :struct_select, shared: true do it "raises an ArgumentError if given any non-block arguments" do - lambda { StructClasses::Car.new.send(@method, 1) { } }.should raise_error(ArgumentError) + struct = StructClasses::Car.new + -> { struct.send(@method, 1) { } }.should raise_error(ArgumentError) end it "returns a new array of elements for which block is true" do diff --git a/spec/ruby/core/thread/backtrace/location/absolute_path_spec.rb b/spec/ruby/core/thread/backtrace/location/absolute_path_spec.rb index 5bd97442d9..d38659c257 100644 --- a/spec/ruby/core/thread/backtrace/location/absolute_path_spec.rb +++ b/spec/ruby/core/thread/backtrace/location/absolute_path_spec.rb @@ -18,6 +18,17 @@ describe 'Thread::Backtrace::Location#absolute_path' do end end + context "when used in #method_added" do + it "returns the user filename that defined the method" do + path = fixture(__FILE__, "absolute_path_method_added.rb") + load path + locations = ScratchPad.recorded + locations[0].absolute_path.should == path + # Make sure it's from the class body, not from the file top-level + locations[0].label.should include 'MethodAddedAbsolutePath' + end + end + platform_is_not :windows do before :each do @file = fixture(__FILE__, "absolute_path.rb") diff --git a/spec/ruby/core/thread/backtrace/location/fixtures/absolute_path_method_added.rb b/spec/ruby/core/thread/backtrace/location/fixtures/absolute_path_method_added.rb new file mode 100644 index 0000000000..26d6298a19 --- /dev/null +++ b/spec/ruby/core/thread/backtrace/location/fixtures/absolute_path_method_added.rb @@ -0,0 +1,10 @@ +module ThreadBacktraceLocationSpecs + class MethodAddedAbsolutePath + def self.method_added(name) + ScratchPad.record caller_locations + end + + def foo + end + end +end diff --git a/spec/ruby/language/fixtures/rescue.rb b/spec/ruby/language/fixtures/rescue.rb index 3fa5df1eb5..b906e17a2f 100644 --- a/spec/ruby/language/fixtures/rescue.rb +++ b/spec/ruby/language/fixtures/rescue.rb @@ -60,4 +60,8 @@ module RescueSpecs ScratchPad << :outside_begin :return_val end + + def self.raise_standard_error + raise StandardError, "an error occurred" + end end diff --git a/spec/ruby/language/precedence_spec.rb b/spec/ruby/language/precedence_spec.rb index 5966a4c2a4..a342c82156 100644 --- a/spec/ruby/language/precedence_spec.rb +++ b/spec/ruby/language/precedence_spec.rb @@ -136,7 +136,7 @@ describe "Operators" do # Guard against the Mathn library # TODO: Make these specs not rely on specific behaviour / result values # by using mocks. - conflicts_with :Prime do + guard -> { !defined?(Math.rsqrt) } do (2*1/2).should_not == 2*(1/2) end diff --git a/spec/ruby/language/rescue_spec.rb b/spec/ruby/language/rescue_spec.rb index 7c761750df..7df8b6db90 100644 --- a/spec/ruby/language/rescue_spec.rb +++ b/spec/ruby/language/rescue_spec.rb @@ -142,6 +142,19 @@ describe "The rescue keyword" do ScratchPad.recorded.should == [:standard_error] end + it "rescues the exception in the deepest rescue block declared to handle the appropriate exception type" do + begin + begin + RescueSpecs.raise_standard_error + rescue ArgumentError + end + rescue StandardError => e + e.backtrace.first.should include ":in `raise_standard_error'" + else + fail("exception wasn't handled by the correct rescue block") + end + end + it "will execute an else block only if no exceptions were raised" do result = begin ScratchPad << :one diff --git a/spec/ruby/library/conditionvariable/wait_spec.rb b/spec/ruby/library/conditionvariable/wait_spec.rb index d4950a7b27..8bdb7829d3 100644 --- a/spec/ruby/library/conditionvariable/wait_spec.rb +++ b/spec/ruby/library/conditionvariable/wait_spec.rb @@ -52,39 +52,41 @@ describe "ConditionVariable#wait" do owned.should == true end - it "reacquires the lock even if the thread is killed after being signaled" do - m = Mutex.new - cv = ConditionVariable.new - in_synchronize = false - owned = nil - - th = Thread.new do - m.synchronize do - in_synchronize = true - begin - cv.wait(m) - ensure - owned = m.owned? - $stderr.puts "\nThe Thread doesn't own the Mutex!" unless owned + ruby_bug '#14999', ''...'2.5' do + it "reacquires the lock even if the thread is killed after being signaled" do + m = Mutex.new + cv = ConditionVariable.new + in_synchronize = false + owned = nil + + th = Thread.new do + m.synchronize do + in_synchronize = true + begin + cv.wait(m) + ensure + owned = m.owned? + $stderr.puts "\nThe Thread doesn't own the Mutex!" unless owned + end end end - end - - # wait for m to acquire the mutex - Thread.pass until in_synchronize - # wait until th is sleeping (ie waiting) - Thread.pass while th.status and th.status != "sleep" - m.synchronize { - cv.signal - # Wait that the thread is blocked on acquiring the Mutex - sleep 0.001 - # Kill the thread, yet the thread should first acquire the Mutex before going on - th.kill - } + # wait for m to acquire the mutex + Thread.pass until in_synchronize + # wait until th is sleeping (ie waiting) + Thread.pass while th.status and th.status != "sleep" + + m.synchronize { + cv.signal + # Wait that the thread is blocked on acquiring the Mutex + sleep 0.001 + # Kill the thread, yet the thread should first acquire the Mutex before going on + th.kill + } - th.join - owned.should == true + th.join + owned.should == true + end end it "supports multiple Threads waiting on the same ConditionVariable and Mutex" do diff --git a/spec/ruby/library/matrix/divide_spec.rb b/spec/ruby/library/matrix/divide_spec.rb index d656985f38..205bc5d892 100644 --- a/spec/ruby/library/matrix/divide_spec.rb +++ b/spec/ruby/library/matrix/divide_spec.rb @@ -14,13 +14,12 @@ describe "Matrix#/" do (@a / @b).should be_close_to_matrix([[2.5, -1.5], [1.5, -0.5]]) end - conflicts_with :Prime do + # Guard against the Mathn library + guard -> { !defined?(Math.rsqrt) } do it "returns the result of dividing self by a Fixnum" do (@a / 2).should == Matrix[ [0, 1], [1, 2] ] end - end - conflicts_with :Prime do it "returns the result of dividing self by a Bignum" do (@a / bignum_value).should == Matrix[ [0, 0], [0, 0] ] end diff --git a/spec/ruby/library/matrix/real_spec.rb b/spec/ruby/library/matrix/real_spec.rb index e3648e4769..38033c63c8 100644 --- a/spec/ruby/library/matrix/real_spec.rb +++ b/spec/ruby/library/matrix/real_spec.rb @@ -16,7 +16,8 @@ describe "Matrix#real?" do Matrix[ [Complex(1,1), 2], [3, 4] ].real?.should be_false end - conflicts_with :CMath do + # Guard against the Mathn library + guard -> { !defined?(Math.rsqrt) } do it "returns false if one element is a Complex whose imaginary part is 0" do Matrix[ [Complex(1,0), 2], [3, 4] ].real?.should be_false end diff --git a/spec/ruby/library/monitor/mon_initialize_spec.rb b/spec/ruby/library/monitor/mon_initialize_spec.rb new file mode 100644 index 0000000000..e0fe6c2d97 --- /dev/null +++ b/spec/ruby/library/monitor/mon_initialize_spec.rb @@ -0,0 +1,31 @@ +require_relative '../../spec_helper' +require 'monitor' + +describe "MonitorMixin#mon_initialize" do + it "can be called in initialize_copy to get a new Mutex and used with synchronize" do + cls = Class.new do + include MonitorMixin + + def initialize(*array) + mon_initialize + @array = array + end + + def to_a + synchronize { @array.dup } + end + + def initialize_copy(other) + mon_initialize + + synchronize do + @array = other.to_a + end + end + end + + instance = cls.new(1, 2, 3) + copy = instance.dup + copy.should_not equal(instance) + end +end diff --git a/spec/ruby/library/rbconfig/sizeof/sizeof_spec.rb b/spec/ruby/library/rbconfig/sizeof/sizeof_spec.rb new file mode 100644 index 0000000000..f2582dc4fd --- /dev/null +++ b/spec/ruby/library/rbconfig/sizeof/sizeof_spec.rb @@ -0,0 +1,30 @@ +require_relative '../../../spec_helper' +require 'rbconfig/sizeof' + +describe "RbConfig::SIZEOF" do + it "is a Hash" do + RbConfig::SIZEOF.should be_kind_of(Hash) + end + + it "has string keys and integer values" do + RbConfig::SIZEOF.each do |key, value| + key.should be_kind_of String + value.should be_kind_of Integer + end + end + + it "contains the sizeof(void*)" do + (RbConfig::SIZEOF["void*"] * 8).should == PlatformGuard::POINTER_SIZE + end + + it "contains the sizeof(float) and sizeof(double)" do + RbConfig::SIZEOF["float"].should == 4 + RbConfig::SIZEOF["double"].should == 8 + end + + it "contains the size of short, int and long" do + RbConfig::SIZEOF["short"].should > 0 + RbConfig::SIZEOF["int"].should > 0 + RbConfig::SIZEOF["long"].should > 0 + end +end diff --git a/spec/ruby/library/socket/addrinfo/getnameinfo_spec.rb b/spec/ruby/library/socket/addrinfo/getnameinfo_spec.rb index 3c1a37d9fb..c5284f1c0f 100644 --- a/spec/ruby/library/socket/addrinfo/getnameinfo_spec.rb +++ b/spec/ruby/library/socket/addrinfo/getnameinfo_spec.rb @@ -13,7 +13,7 @@ describe 'Addrinfo#getnameinfo' do service.should == 'ftp' end - it 'accepts flags as a Fixnum as the first argument' do + it 'accepts flags as an Integer as the first argument' do host, service = @addr.getnameinfo(Socket::NI_NUMERICSERV) service.should == '21' end diff --git a/spec/ruby/library/socket/ancillarydata/family_spec.rb b/spec/ruby/library/socket/ancillarydata/family_spec.rb index b742e0c6db..975f0d2538 100644 --- a/spec/ruby/library/socket/ancillarydata/family_spec.rb +++ b/spec/ruby/library/socket/ancillarydata/family_spec.rb @@ -2,7 +2,7 @@ require_relative '../spec_helper' with_feature :ancillary_data do describe 'Socket::AncillaryData#family' do - it 'returns the family as a Fixnum' do + it 'returns the family as an Integer' do Socket::AncillaryData.new(:INET, :SOCKET, :RIGHTS, '').family.should == Socket::AF_INET end end diff --git a/spec/ruby/library/socket/ancillarydata/initialize_spec.rb b/spec/ruby/library/socket/ancillarydata/initialize_spec.rb index c700404799..d4788d0f68 100644 --- a/spec/ruby/library/socket/ancillarydata/initialize_spec.rb +++ b/spec/ruby/library/socket/ancillarydata/initialize_spec.rb @@ -2,7 +2,7 @@ require_relative '../spec_helper' with_feature :ancillary_data do describe 'Socket::AncillaryData#initialize' do - describe 'using Fixnums for the family, level, and type' do + describe 'using Integers for the family, level, and type' do before do @data = Socket::AncillaryData .new(Socket::AF_INET, Socket::IPPROTO_IP, Socket::IP_RECVTTL, 'ugh') diff --git a/spec/ruby/library/socket/ancillarydata/int_spec.rb b/spec/ruby/library/socket/ancillarydata/int_spec.rb index 75608a28b8..0d7c5e3652 100644 --- a/spec/ruby/library/socket/ancillarydata/int_spec.rb +++ b/spec/ruby/library/socket/ancillarydata/int_spec.rb @@ -28,13 +28,13 @@ with_feature :ancillary_data do end describe 'Socket::AncillaryData#int' do - it 'returns the data as a Fixnum' do + it 'returns the data as an Integer' do data = Socket::AncillaryData.int(:UNIX, :SOCKET, :RIGHTS, 4) data.int.should == 4 end - it 'raises when the data is not a Fixnum' do + it 'raises when the data is not an Integer' do data = Socket::AncillaryData.new(:UNIX, :SOCKET, :RIGHTS, 'ugh') lambda { data.int }.should raise_error(TypeError) diff --git a/spec/ruby/library/socket/ancillarydata/ip_pktinfo_spec.rb b/spec/ruby/library/socket/ancillarydata/ip_pktinfo_spec.rb index 1bc92e3373..809d087161 100644 --- a/spec/ruby/library/socket/ancillarydata/ip_pktinfo_spec.rb +++ b/spec/ruby/library/socket/ancillarydata/ip_pktinfo_spec.rb @@ -71,7 +71,7 @@ with_feature :ancillary_data, :pktinfo do end it 'stores the ifindex at index 1' do - @info[1].should be_an_instance_of(Fixnum) + @info[1].should be_kind_of(Integer) end it 'stores an Addrinfo at index 2' do @@ -94,7 +94,7 @@ with_feature :ancillary_data, :pktinfo do end describe 'the ifindex' do - it 'is a Fixnum' do + it 'is an Integer' do @data.ip_pktinfo[1].should == 4 end end diff --git a/spec/ruby/library/socket/ancillarydata/ipv6_pktinfo_spec.rb b/spec/ruby/library/socket/ancillarydata/ipv6_pktinfo_spec.rb index 6315aba89c..dfaa5bf0f5 100644 --- a/spec/ruby/library/socket/ancillarydata/ipv6_pktinfo_spec.rb +++ b/spec/ruby/library/socket/ancillarydata/ipv6_pktinfo_spec.rb @@ -44,7 +44,7 @@ with_feature :ancillary_data, :ipv6_pktinfo do end it 'stores the ifindex at index 1' do - @info[1].should be_an_instance_of(Fixnum) + @info[1].should be_kind_of(Integer) end end @@ -63,7 +63,7 @@ with_feature :ancillary_data, :ipv6_pktinfo do end describe 'the ifindex' do - it 'is a Fixnum' do + it 'is an Integer' do @data.ipv6_pktinfo[1].should == 4 end end diff --git a/spec/ruby/library/socket/ancillarydata/level_spec.rb b/spec/ruby/library/socket/ancillarydata/level_spec.rb index 40b070a6d8..a2ff216f9d 100644 --- a/spec/ruby/library/socket/ancillarydata/level_spec.rb +++ b/spec/ruby/library/socket/ancillarydata/level_spec.rb @@ -2,7 +2,7 @@ require_relative '../spec_helper' with_feature :ancillary_data do describe 'Socket::AncillaryData#level' do - it 'returns the level as a Fixnum' do + it 'returns the level as an Integer' do Socket::AncillaryData.new(:INET, :SOCKET, :RIGHTS, '').level.should == Socket::SOL_SOCKET end end diff --git a/spec/ruby/library/socket/ancillarydata/type_spec.rb b/spec/ruby/library/socket/ancillarydata/type_spec.rb index 1a4b04ed57..972beeeca0 100644 --- a/spec/ruby/library/socket/ancillarydata/type_spec.rb +++ b/spec/ruby/library/socket/ancillarydata/type_spec.rb @@ -2,7 +2,7 @@ require_relative '../spec_helper' with_feature :ancillary_data do describe 'Socket::AncillaryData#type' do - it 'returns the type as a Fixnum' do + it 'returns the type as an Integer' do Socket::AncillaryData.new(:INET, :SOCKET, :RIGHTS, '').type.should == Socket::SCM_RIGHTS end end diff --git a/spec/ruby/library/socket/basicsocket/getsockopt_spec.rb b/spec/ruby/library/socket/basicsocket/getsockopt_spec.rb index 926d7505e8..aad21ca535 100644 --- a/spec/ruby/library/socket/basicsocket/getsockopt_spec.rb +++ b/spec/ruby/library/socket/basicsocket/getsockopt_spec.rb @@ -59,7 +59,7 @@ describe "BasicSocket#getsockopt" do it 'returns a Socket::Option for a numeric option' do opt = @sock.getsockopt(Socket::IPPROTO_IP, Socket::IP_TTL) - opt.int.should be_an_instance_of(Fixnum) + opt.int.should be_kind_of(Integer) end it 'returns a Socket::Option for a struct option' do @@ -171,7 +171,7 @@ describe "BasicSocket#getsockopt" do opt = @sock.getsockopt(Socket::IPPROTO_IP, Socket::IP_TTL).to_s array = opt.unpack('i') - array[0].should be_an_instance_of(Fixnum) + array[0].should be_kind_of(Integer) array[0].should > 0 end diff --git a/spec/ruby/library/socket/basicsocket/recvmsg_nonblock_spec.rb b/spec/ruby/library/socket/basicsocket/recvmsg_nonblock_spec.rb index 70708e0cbc..8f6b75029c 100644 --- a/spec/ruby/library/socket/basicsocket/recvmsg_nonblock_spec.rb +++ b/spec/ruby/library/socket/basicsocket/recvmsg_nonblock_spec.rb @@ -75,7 +75,7 @@ describe 'BasicSocket#recvmsg_nonblock' do platform_is_not :windows do it 'stores the flags at index 2' do - @array[2].should be_an_instance_of(Fixnum) + @array[2].should be_kind_of(Integer) end end @@ -169,7 +169,7 @@ describe 'BasicSocket#recvmsg_nonblock' do end it 'stores the flags at index 2' do - @array[2].should be_an_instance_of(Fixnum) + @array[2].should be_kind_of(Integer) end describe 'the returned Addrinfo' do diff --git a/spec/ruby/library/socket/basicsocket/recvmsg_spec.rb b/spec/ruby/library/socket/basicsocket/recvmsg_spec.rb index d7de83d72b..58eb8d0360 100644 --- a/spec/ruby/library/socket/basicsocket/recvmsg_spec.rb +++ b/spec/ruby/library/socket/basicsocket/recvmsg_spec.rb @@ -71,7 +71,7 @@ describe 'BasicSocket#recvmsg' do platform_is_not :windows do it 'stores the flags at index 2' do - @array[2].should be_an_instance_of(Fixnum) + @array[2].should be_kind_of(Integer) end end @@ -161,7 +161,7 @@ describe 'BasicSocket#recvmsg' do end it 'stores the flags at index 2' do - @array[2].should be_an_instance_of(Fixnum) + @array[2].should be_kind_of(Integer) end describe 'the returned Addrinfo' do diff --git a/spec/ruby/library/socket/basicsocket/shutdown_spec.rb b/spec/ruby/library/socket/basicsocket/shutdown_spec.rb index 40e2d77570..87a32ae1dc 100644 --- a/spec/ruby/library/socket/basicsocket/shutdown_spec.rb +++ b/spec/ruby/library/socket/basicsocket/shutdown_spec.rb @@ -19,7 +19,7 @@ platform_is_not :windows do # hangs @server.close end - describe 'using a Fixnum' do + describe 'using an Integer' do it 'shuts down a socket for reading' do @client.shutdown(Socket::SHUT_RD) diff --git a/spec/ruby/library/socket/option/initialize_spec.rb b/spec/ruby/library/socket/option/initialize_spec.rb index 986cfa8ad4..0d4621b71e 100644 --- a/spec/ruby/library/socket/option/initialize_spec.rb +++ b/spec/ruby/library/socket/option/initialize_spec.rb @@ -5,7 +5,7 @@ describe 'Socket::Option#initialize' do @bool = [0].pack('i') end - describe 'using Fixnums' do + describe 'using Integers' do it 'returns a Socket::Option' do opt = Socket::Option .new(Socket::AF_INET, Socket::SOL_SOCKET, Socket::SO_KEEPALIVE, @bool) diff --git a/spec/ruby/library/socket/shared/socketpair.rb b/spec/ruby/library/socket/shared/socketpair.rb index 1e08deccc2..08db2e59b8 100644 --- a/spec/ruby/library/socket/shared/socketpair.rb +++ b/spec/ruby/library/socket/shared/socketpair.rb @@ -20,7 +20,7 @@ describe :socket_socketpair, shared: true do end end - describe 'using a Fixnum as the 1st and 2nd argument' do + describe 'using an Integer as the 1st and 2nd argument' do it 'returns two Socket objects' do s1, s2 = Socket.public_send(@method, Socket::AF_UNIX, Socket::SOCK_STREAM) @@ -116,7 +116,7 @@ describe :socket_socketpair, shared: true do end end - it 'accepts a custom protocol as a Fixnum as the 3rd argument' do + it 'accepts a custom protocol as an Integer as the 3rd argument' do s1, s2 = Socket.public_send(@method, :UNIX, :STREAM, Socket::IPPROTO_IP) s1.should be_an_instance_of(Socket) s2.should be_an_instance_of(Socket) diff --git a/spec/ruby/library/socket/socket/connect_nonblock_spec.rb b/spec/ruby/library/socket/socket/connect_nonblock_spec.rb index 037dc39131..0012b5ada7 100644 --- a/spec/ruby/library/socket/socket/connect_nonblock_spec.rb +++ b/spec/ruby/library/socket/socket/connect_nonblock_spec.rb @@ -94,7 +94,7 @@ describe 'Socket#connect_nonblock' do @client.connect_nonblock(@server.connect_address).should == 0 end - it 'raises TypeError when passed a Fixnum' do + it 'raises TypeError when passed an Integer' do lambda { @client.connect_nonblock(666) }.should raise_error(TypeError) end end diff --git a/spec/ruby/library/socket/socket/getaddrinfo_spec.rb b/spec/ruby/library/socket/socket/getaddrinfo_spec.rb index a4b6ada2e5..e0eff3cef4 100644 --- a/spec/ruby/library/socket/socket/getaddrinfo_spec.rb +++ b/spec/ruby/library/socket/socket/getaddrinfo_spec.rb @@ -115,7 +115,7 @@ describe 'Socket.getaddrinfo' do Socket.getaddrinfo(nil, 'ftp').should be_an_instance_of(Array) end - it 'accepts a Fixnum as the address family' do + it 'accepts an Integer as the address family' do array = Socket.getaddrinfo(nil, 'ftp', Socket::AF_INET)[0] array[0].should == 'AF_INET' @@ -123,11 +123,11 @@ describe 'Socket.getaddrinfo' do array[2].should == '127.0.0.1' array[3].should == '127.0.0.1' array[4].should == Socket::AF_INET - array[5].should be_an_instance_of(Fixnum) - array[6].should be_an_instance_of(Fixnum) + array[5].should be_kind_of(Integer) + array[6].should be_kind_of(Integer) end - it 'accepts a Fixnum as the address family using IPv6' do + it 'accepts an Integer as the address family using IPv6' do array = Socket.getaddrinfo(nil, 'ftp', Socket::AF_INET6)[0] array[0].should == 'AF_INET6' @@ -135,8 +135,8 @@ describe 'Socket.getaddrinfo' do array[2].should == '::1' array[3].should == '::1' array[4].should == Socket::AF_INET6 - array[5].should be_an_instance_of(Fixnum) - array[6].should be_an_instance_of(Fixnum) + array[5].should be_kind_of(Integer) + array[6].should be_kind_of(Integer) end it 'accepts a Symbol as the address family' do @@ -147,8 +147,8 @@ describe 'Socket.getaddrinfo' do array[2].should == '127.0.0.1' array[3].should == '127.0.0.1' array[4].should == Socket::AF_INET - array[5].should be_an_instance_of(Fixnum) - array[6].should be_an_instance_of(Fixnum) + array[5].should be_kind_of(Integer) + array[6].should be_kind_of(Integer) end it 'accepts a Symbol as the address family using IPv6' do @@ -159,8 +159,8 @@ describe 'Socket.getaddrinfo' do array[2].should == '::1' array[3].should == '::1' array[4].should == Socket::AF_INET6 - array[5].should be_an_instance_of(Fixnum) - array[6].should be_an_instance_of(Fixnum) + array[5].should be_kind_of(Integer) + array[6].should be_kind_of(Integer) end it 'accepts a String as the address family' do @@ -171,8 +171,8 @@ describe 'Socket.getaddrinfo' do array[2].should == '127.0.0.1' array[3].should == '127.0.0.1' array[4].should == Socket::AF_INET - array[5].should be_an_instance_of(Fixnum) - array[6].should be_an_instance_of(Fixnum) + array[5].should be_kind_of(Integer) + array[6].should be_kind_of(Integer) end it 'accepts a String as the address family using IPv6' do @@ -183,8 +183,8 @@ describe 'Socket.getaddrinfo' do array[2].should == '::1' array[3].should == '::1' array[4].should == Socket::AF_INET6 - array[5].should be_an_instance_of(Fixnum) - array[6].should be_an_instance_of(Fixnum) + array[5].should be_kind_of(Integer) + array[6].should be_kind_of(Integer) end it 'accepts an object responding to #to_str as the host' do @@ -199,8 +199,8 @@ describe 'Socket.getaddrinfo' do array[2].should == '127.0.0.1' array[3].should == '127.0.0.1' array[4].should == Socket::AF_INET - array[5].should be_an_instance_of(Fixnum) - array[6].should be_an_instance_of(Fixnum) + array[5].should be_kind_of(Integer) + array[6].should be_kind_of(Integer) end it 'accepts an object responding to #to_str as the address family' do @@ -215,11 +215,11 @@ describe 'Socket.getaddrinfo' do array[2].should == '127.0.0.1' array[3].should == '127.0.0.1' array[4].should == Socket::AF_INET - array[5].should be_an_instance_of(Fixnum) - array[6].should be_an_instance_of(Fixnum) + array[5].should be_kind_of(Integer) + array[6].should be_kind_of(Integer) end - it 'accepts a Fixnum as the socket type' do + it 'accepts an Integer as the socket type' do *array, proto = Socket.getaddrinfo(nil, 'ftp', :INET, Socket::SOCK_STREAM)[0] array.should == [ 'AF_INET', @@ -276,7 +276,7 @@ describe 'Socket.getaddrinfo' do end platform_is_not :windows do - it 'accepts a Fixnum as the protocol family' do + it 'accepts an Integer as the protocol family' do *array, proto = Socket.getaddrinfo(nil, 'discard', :INET, :DGRAM, Socket::IPPROTO_UDP)[0] array.should == [ 'AF_INET', @@ -290,7 +290,7 @@ describe 'Socket.getaddrinfo' do end end - it 'accepts a Fixnum as the flags' do + it 'accepts an Integer as the flags' do *array, proto = Socket.getaddrinfo(nil, 'ftp', :INET, :STREAM, Socket::IPPROTO_TCP, Socket::AI_PASSIVE)[0] array.should == [ diff --git a/spec/ruby/library/socket/socket/gethostbyaddr_spec.rb b/spec/ruby/library/socket/socket/gethostbyaddr_spec.rb index 08cadcf93b..639a318132 100644 --- a/spec/ruby/library/socket/socket/gethostbyaddr_spec.rb +++ b/spec/ruby/library/socket/socket/gethostbyaddr_spec.rb @@ -48,7 +48,7 @@ describe 'Socket.gethostbyaddr' do end describe 'with an explicit address family' do - it 'returns an Array when using a Fixnum as the address family' do + it 'returns an Array when using an Integer as the address family' do Socket.gethostbyaddr(@addr, Socket::AF_INET).should be_an_instance_of(Array) end @@ -105,7 +105,7 @@ describe 'Socket.gethostbyaddr' do end describe 'with an explicit address family' do - it 'returns an Array when using a Fixnum as the address family' do + it 'returns an Array when using an Integer as the address family' do Socket.gethostbyaddr(@addr, Socket::AF_INET6).should be_an_instance_of(Array) end diff --git a/spec/ruby/library/socket/socket/getifaddrs_spec.rb b/spec/ruby/library/socket/socket/getifaddrs_spec.rb index 588a355700..ad23499ef9 100644 --- a/spec/ruby/library/socket/socket/getifaddrs_spec.rb +++ b/spec/ruby/library/socket/socket/getifaddrs_spec.rb @@ -25,7 +25,7 @@ describe 'Socket.getifaddrs' do describe 'each returned Socket::Ifaddr' do it 'has an interface index' do @ifaddrs.each do |ifaddr| - ifaddr.ifindex.should be_an_instance_of(Fixnum) + ifaddr.ifindex.should be_kind_of(Integer) end end @@ -37,7 +37,7 @@ describe 'Socket.getifaddrs' do it 'has a set of flags' do @ifaddrs.each do |ifaddr| - ifaddr.flags.should be_an_instance_of(Fixnum) + ifaddr.flags.should be_kind_of(Integer) end end end @@ -55,7 +55,7 @@ describe 'Socket.getifaddrs' do it 'has an address family' do @addrs.each do |addr| - addr.afamily.should be_an_instance_of(Fixnum) + addr.afamily.should be_kind_of(Integer) addr.afamily.should_not == Socket::AF_UNSPEC end end @@ -75,7 +75,7 @@ describe 'Socket.getifaddrs' do it 'has an address family' do @addrs.each do |addr| - addr.afamily.should be_an_instance_of(Fixnum) + addr.afamily.should be_kind_of(Integer) addr.afamily.should_not == Socket::AF_UNSPEC end end @@ -94,7 +94,7 @@ describe 'Socket.getifaddrs' do it 'has an address family' do @addrs.each do |addr| - addr.afamily.should be_an_instance_of(Fixnum) + addr.afamily.should be_kind_of(Integer) addr.afamily.should_not == Socket::AF_UNSPEC end end diff --git a/spec/ruby/library/socket/socket/initialize_spec.rb b/spec/ruby/library/socket/socket/initialize_spec.rb index 375eabfbcb..2343c6e289 100644 --- a/spec/ruby/library/socket/socket/initialize_spec.rb +++ b/spec/ruby/library/socket/socket/initialize_spec.rb @@ -9,7 +9,7 @@ describe 'Socket#initialize' do @socket.close if @socket end - describe 'using a Fixnum as the 1st and 2nd arguments' do + describe 'using an Integer as the 1st and 2nd arguments' do it 'returns a Socket' do @socket = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM) @@ -58,7 +58,7 @@ describe 'Socket#initialize' do end describe 'using a custom protocol' do - it 'returns a Socket when using a Fixnum' do + it 'returns a Socket when using an Integer' do @socket = Socket.new(:INET, :STREAM, Socket::IPPROTO_TCP) @socket.should be_an_instance_of(Socket) diff --git a/spec/ruby/library/socket/socket/listen_spec.rb b/spec/ruby/library/socket/socket/listen_spec.rb index 326307f942..d0f9c70c4e 100644 --- a/spec/ruby/library/socket/socket/listen_spec.rb +++ b/spec/ruby/library/socket/socket/listen_spec.rb @@ -56,7 +56,7 @@ describe 'Socket#listen' do @server.listen(1).should == 0 end - it "raises when the given argument can't be coerced to a Fixnum" do + it "raises when the given argument can't be coerced to an Integer" do lambda { @server.listen('cats') }.should raise_error(TypeError) end end diff --git a/spec/ruby/library/socket/socket/sysaccept_spec.rb b/spec/ruby/library/socket/socket/sysaccept_spec.rb index 3832041818..f039096707 100644 --- a/spec/ruby/library/socket/socket/sysaccept_spec.rb +++ b/spec/ruby/library/socket/socket/sysaccept_spec.rb @@ -75,10 +75,10 @@ describe 'Socket#sysaccept' do @client.close end - it 'returns an Array containing a Fixnum and an Addrinfo' do + it 'returns an Array containing an Integer and an Addrinfo' do @fd, addrinfo = @server.sysaccept - @fd.should be_an_instance_of(Fixnum) + @fd.should be_kind_of(Integer) addrinfo.should be_an_instance_of(Addrinfo) end diff --git a/spec/ruby/library/socket/tcpserver/initialize_spec.rb b/spec/ruby/library/socket/tcpserver/initialize_spec.rb index 9a55cf17e2..412bdbfb9d 100644 --- a/spec/ruby/library/socket/tcpserver/initialize_spec.rb +++ b/spec/ruby/library/socket/tcpserver/initialize_spec.rb @@ -2,7 +2,7 @@ require_relative '../spec_helper' require_relative '../fixtures/classes' describe 'TCPServer#initialize' do - describe 'with a single Fixnum argument' do + describe 'with a single Integer argument' do before do @server = TCPServer.new(0) end @@ -12,7 +12,7 @@ describe 'TCPServer#initialize' do end it 'sets the port to the given argument' do - @server.local_address.ip_port.should be_an_instance_of(Fixnum) + @server.local_address.ip_port.should be_kind_of(Integer) @server.local_address.ip_port.should > 0 end @@ -38,7 +38,7 @@ describe 'TCPServer#initialize' do end it 'sets the port to the given argument' do - @server.local_address.ip_port.should be_an_instance_of(Fixnum) + @server.local_address.ip_port.should be_kind_of(Integer) @server.local_address.ip_port.should > 0 end @@ -56,7 +56,7 @@ describe 'TCPServer#initialize' do end end - describe 'with a String and a Fixnum' do + describe 'with a String and an Integer' do SocketSpecs.each_ip_protocol do |family, ip_address| before do @server = TCPServer.new(ip_address, 0) @@ -67,7 +67,7 @@ describe 'TCPServer#initialize' do end it 'sets the port to the given port argument' do - @server.local_address.ip_port.should be_an_instance_of(Fixnum) + @server.local_address.ip_port.should be_kind_of(Integer) @server.local_address.ip_port.should > 0 end @@ -90,7 +90,7 @@ describe 'TCPServer#initialize' do end it 'sets the port to the given port argument' do - @server.local_address.ip_port.should be_an_instance_of(Fixnum) + @server.local_address.ip_port.should be_kind_of(Integer) @server.local_address.ip_port.should > 0 end diff --git a/spec/ruby/library/socket/tcpserver/listen_spec.rb b/spec/ruby/library/socket/tcpserver/listen_spec.rb index 1e17de06f1..e266decd61 100644 --- a/spec/ruby/library/socket/tcpserver/listen_spec.rb +++ b/spec/ruby/library/socket/tcpserver/listen_spec.rb @@ -15,7 +15,7 @@ describe 'TCPServer#listen' do @server.listen(1).should == 0 end - it "raises when the given argument can't be coerced to a Fixnum" do + it "raises when the given argument can't be coerced to an Integer" do lambda { @server.listen('cats') }.should raise_error(TypeError) end end diff --git a/spec/ruby/library/socket/tcpserver/new_spec.rb b/spec/ruby/library/socket/tcpserver/new_spec.rb index 94033411e3..4717b95a2c 100644 --- a/spec/ruby/library/socket/tcpserver/new_spec.rb +++ b/spec/ruby/library/socket/tcpserver/new_spec.rb @@ -10,7 +10,7 @@ describe "TCPServer.new" do @server = TCPServer.new('127.0.0.1', 0) addr = @server.addr addr[0].should == 'AF_INET' - addr[1].should be_kind_of(Fixnum) + addr[1].should be_kind_of(Integer) # on some platforms (Mac), MRI # returns comma at the end. addr[2].should =~ /^#{SocketSpecs.hostname}\b/ @@ -20,7 +20,7 @@ describe "TCPServer.new" do it "binds to localhost and a port with either IPv4 or IPv6" do @server = TCPServer.new(SocketSpecs.hostname, 0) addr = @server.addr - addr[1].should be_kind_of(Fixnum) + addr[1].should be_kind_of(Integer) if addr[0] == 'AF_INET' addr[2].should =~ /^#{SocketSpecs.hostname}\b/ addr[3].should == '127.0.0.1' @@ -34,7 +34,7 @@ describe "TCPServer.new" do @server = TCPServer.new('', 0) addr = @server.addr addr[0].should == 'AF_INET' - addr[1].should be_kind_of(Fixnum) + addr[1].should be_kind_of(Integer) addr[2].should == '0.0.0.0' addr[3].should == '0.0.0.0' end @@ -43,7 +43,7 @@ describe "TCPServer.new" do @server = TCPServer.new('', 0) addr = @server.addr addr[0].should == 'AF_INET' - addr[1].should be_kind_of(Fixnum) + addr[1].should be_kind_of(Integer) addr[2].should == '0.0.0.0' addr[3].should == '0.0.0.0' end @@ -56,7 +56,7 @@ describe "TCPServer.new" do @server = TCPServer.new(SocketSpecs.hostname, port) addr = @server.addr - addr[1].should be_kind_of(Fixnum) + addr[1].should be_kind_of(Integer) # TODO: This should also accept strings like 'https', but I don't know how to # pick such a service port that will be able to reliably bind... diff --git a/spec/ruby/library/socket/tcpserver/sysaccept_spec.rb b/spec/ruby/library/socket/tcpserver/sysaccept_spec.rb index 144b6806f1..5543b67755 100644 --- a/spec/ruby/library/socket/tcpserver/sysaccept_spec.rb +++ b/spec/ruby/library/socket/tcpserver/sysaccept_spec.rb @@ -21,7 +21,7 @@ describe "TCPServer#sysaccept" do fd = @server.sysaccept - fd.should be_an_instance_of(Fixnum) + fd.should be_kind_of(Integer) ensure sock.close if sock && !sock.closed? IO.for_fd(fd).close if fd @@ -55,10 +55,10 @@ describe 'TCPServer#sysaccept' do @client.close end - it 'returns a new file descriptor as a Fixnum' do + it 'returns a new file descriptor as an Integer' do @fd = @server.sysaccept - @fd.should be_an_instance_of(Fixnum) + @fd.should be_kind_of(Integer) @fd.should_not == @client.fileno end end diff --git a/spec/ruby/library/socket/tcpsocket/gethostbyname_spec.rb b/spec/ruby/library/socket/tcpsocket/gethostbyname_spec.rb index 230d14bfb0..703abff81c 100644 --- a/spec/ruby/library/socket/tcpsocket/gethostbyname_spec.rb +++ b/spec/ruby/library/socket/tcpsocket/gethostbyname_spec.rb @@ -70,7 +70,7 @@ describe 'TCPSocket#gethostbyname' do end it 'includes the address family as the 3rd value' do - @array[2].should be_an_instance_of(Fixnum) + @array[2].should be_kind_of(Integer) end it 'includes the IP addresses as all the remaining values' do diff --git a/spec/ruby/library/socket/tcpsocket/initialize_spec.rb b/spec/ruby/library/socket/tcpsocket/initialize_spec.rb index 184f9abb7d..a3593f0d5f 100644 --- a/spec/ruby/library/socket/tcpsocket/initialize_spec.rb +++ b/spec/ruby/library/socket/tcpsocket/initialize_spec.rb @@ -20,7 +20,7 @@ describe 'TCPSocket#initialize' do @server.close end - it 'returns a TCPSocket when using a Fixnum as the port' do + it 'returns a TCPSocket when using an Integer as the port' do @client = TCPSocket.new(ip_address, @port) @client.should be_an_instance_of(TCPSocket) end diff --git a/spec/ruby/library/socket/tcpsocket/shared/new.rb b/spec/ruby/library/socket/tcpsocket/shared/new.rb index 818bd69a91..d0358923c9 100644 --- a/spec/ruby/library/socket/tcpsocket/shared/new.rb +++ b/spec/ruby/library/socket/tcpsocket/shared/new.rb @@ -72,7 +72,7 @@ describe :tcpsocket_new, shared: true do @socket.addr[3].should == SocketSpecs.addr(:ipv6) end - @socket.addr[1].should be_kind_of(Fixnum) + @socket.addr[1].should be_kind_of(Integer) @socket.addr[2].should =~ /^#{@hostname}/ end end diff --git a/spec/ruby/library/socket/udpsocket/initialize_spec.rb b/spec/ruby/library/socket/udpsocket/initialize_spec.rb index 5df3bf9800..06f7b5ef1c 100644 --- a/spec/ruby/library/socket/udpsocket/initialize_spec.rb +++ b/spec/ruby/library/socket/udpsocket/initialize_spec.rb @@ -10,7 +10,7 @@ describe 'UDPSocket#initialize' do @socket.should be_an_instance_of(UDPSocket) end - it 'initializes a new UDPSocket using a Fixnum' do + it 'initializes a new UDPSocket using an Integer' do @socket = UDPSocket.new(Socket::AF_INET) @socket.should be_an_instance_of(UDPSocket) end diff --git a/spec/ruby/library/socket/udpsocket/new_spec.rb b/spec/ruby/library/socket/udpsocket/new_spec.rb index 30e7103d16..157ff138be 100644 --- a/spec/ruby/library/socket/udpsocket/new_spec.rb +++ b/spec/ruby/library/socket/udpsocket/new_spec.rb @@ -11,7 +11,7 @@ describe 'UDPSocket.new' do @socket.should be_an_instance_of(UDPSocket) end - it 'using Fixnum argument' do + it 'using Integer argument' do @socket = UDPSocket.new(Socket::AF_INET) @socket.should be_an_instance_of(UDPSocket) end diff --git a/spec/ruby/library/socket/udpsocket/send_spec.rb b/spec/ruby/library/socket/udpsocket/send_spec.rb index b84e84b056..431129723e 100644 --- a/spec/ruby/library/socket/udpsocket/send_spec.rb +++ b/spec/ruby/library/socket/udpsocket/send_spec.rb @@ -34,7 +34,7 @@ describe "UDPSocket#send" do @msg[0].should == "ad hoc" @msg[1][0].should == "AF_INET" - @msg[1][1].should be_kind_of(Fixnum) + @msg[1][1].should be_kind_of(Integer) @msg[1][3].should == "127.0.0.1" end @@ -46,7 +46,7 @@ describe "UDPSocket#send" do @msg[0].should == "ad hoc" @msg[1][0].should == "AF_INET" - @msg[1][1].should be_kind_of(Fixnum) + @msg[1][1].should be_kind_of(Integer) @msg[1][3].should == "127.0.0.1" end @@ -59,7 +59,7 @@ describe "UDPSocket#send" do @msg[0].should == "connection-based" @msg[1][0].should == "AF_INET" - @msg[1][1].should be_kind_of(Fixnum) + @msg[1][1].should be_kind_of(Integer) @msg[1][3].should == "127.0.0.1" end diff --git a/spec/ruby/library/socket/unixserver/sysaccept_spec.rb b/spec/ruby/library/socket/unixserver/sysaccept_spec.rb index 864913af82..65a35c91a9 100644 --- a/spec/ruby/library/socket/unixserver/sysaccept_spec.rb +++ b/spec/ruby/library/socket/unixserver/sysaccept_spec.rb @@ -31,9 +31,9 @@ with_feature :unix_socket do end describe 'without any data' do - it 'returns a Fixnum' do + it 'returns an Integer' do @fd = @server.sysaccept - @fd.should be_an_instance_of(Fixnum) + @fd.should be_kind_of(Integer) end end @@ -42,9 +42,9 @@ with_feature :unix_socket do @client.write('hello') end - it 'returns a Fixnum' do + it 'returns an Integer' do @fd = @server.sysaccept - @fd.should be_an_instance_of(Fixnum) + @fd.should be_kind_of(Integer) end end end diff --git a/spec/ruby/library/stringio/external_encoding_spec.rb b/spec/ruby/library/stringio/external_encoding_spec.rb index 9c3d4041e5..6c5edb1713 100644 --- a/spec/ruby/library/stringio/external_encoding_spec.rb +++ b/spec/ruby/library/stringio/external_encoding_spec.rb @@ -8,6 +8,12 @@ describe "StringIO#external_encoding" do io.external_encoding.should == Encoding::EUC_JP end + it "changes to match string if string's encoding is changed" do + io = StringIO.new + io.string.force_encoding(Encoding::EUC_JP) + io.external_encoding.should == Encoding::EUC_JP + end + it "does not set the encoding of its buffer string if the string is frozen" do str = "foo".freeze enc = str.encoding diff --git a/spec/ruby/optional/capi/ext/thread_spec.c b/spec/ruby/optional/capi/ext/thread_spec.c index 3bfe76b925..fed1b1f3b0 100644 --- a/spec/ruby/optional/capi/ext/thread_spec.c +++ b/spec/ruby/optional/capi/ext/thread_spec.c @@ -27,7 +27,7 @@ static VALUE thread_spec_rb_thread_alone() { /* This is unblocked by unblock_func(). */ static void* blocking_gvl_func(void* data) { int rfd = *(int *)data; - char dummy; + char dummy = ' '; ssize_t r; do { @@ -36,7 +36,7 @@ static void* blocking_gvl_func(void* data) { close(rfd); - return (void*)((r == 1) ? Qtrue : Qfalse); + return (void*)((r == 1 && dummy == 'A') ? Qtrue : Qfalse); } static void unblock_gvl_func(void *data) { @@ -57,7 +57,7 @@ static VALUE thread_spec_rb_thread_call_without_gvl(VALUE self) { void* ret; if (pipe(fds) == -1) { - return Qfalse; + rb_raise(rb_eRuntimeError, "could not create pipe"); } ret = rb_thread_call_without_gvl(blocking_gvl_func, &fds[0], unblock_gvl_func, &fds[1]); @@ -82,7 +82,7 @@ static VALUE thread_spec_rb_thread_call_without_gvl_with_ubf_io(VALUE self) { void* ret; if (pipe(fds) == -1) { - return Qfalse; + rb_raise(rb_eRuntimeError, "could not create pipe"); } ret = rb_thread_call_without_gvl(blocking_gvl_func_for_udf_io, diff --git a/spec/ruby/optional/capi/spec_helper.rb b/spec/ruby/optional/capi/spec_helper.rb index 25658122be..eda4964b69 100644 --- a/spec/ruby/optional/capi/spec_helper.rb +++ b/spec/ruby/optional/capi/spec_helper.rb @@ -16,24 +16,34 @@ def compile_extension(name) debug = false run_mkmf_in_process = RUBY_ENGINE == 'truffleruby' - ext_dir = File.expand_path("../ext", __FILE__) + core_ext_dir = File.expand_path("../ext", __FILE__) + + spec_caller_location = caller_locations.find { |c| c.path.end_with?('_spec.rb') } + spec_file_path = spec_caller_location.path + spec_ext_dir = File.expand_path("../ext", spec_file_path) + ext = "#{name}_spec" lib = "#{object_path}/#{ext}.#{RbConfig::CONFIG['DLEXT']}" ruby_header = "#{RbConfig::CONFIG['rubyhdrdir']}/ruby.h" - libruby_so = RbConfig::CONFIG['LIBRUBY_SO'] if RbConfig::CONFIG["ENABLE_SHARED"] == "yes" - if /mswin|mingw/ =~ RUBY_PLATFORM - libruby_so = RbConfig::CONFIG["LIBRUBY"] if RbConfig::CONFIG["ENABLE_SHARED"] == "yes" + + if RbConfig::CONFIG["ENABLE_SHARED"] == "yes" + if PlatformGuard.windows? + libruby_so = "#{RbConfig::CONFIG['bindir']}/#{RbConfig::CONFIG['LIBRUBY_SO']}" + else + libruby_so = "#{RbConfig::CONFIG['libdir']}/#{RbConfig::CONFIG['LIBRUBY_SO']}" + end end + begin mtime = File.mtime(lib) rescue Errno::ENOENT # not found, then compile else case # if lib is older than headers, source or libruby, then recompile - when mtime <= File.mtime("#{ext_dir}/rubyspec.h") - when mtime <= File.mtime("#{ext_dir}/#{ext}.c") + when mtime <= File.mtime("#{core_ext_dir}/rubyspec.h") + when mtime <= File.mtime("#{spec_ext_dir}/#{ext}.c") when mtime <= File.mtime(ruby_header) - when libruby_so && mtime <= File.mtime("#{RbConfig::CONFIG['libdir']}/#{libruby_so}") + when libruby_so && mtime <= File.mtime(libruby_so) else return lib # up-to-date end @@ -43,8 +53,8 @@ def compile_extension(name) tmpdir = tmp("cext_#{name}") Dir.mkdir(tmpdir) begin - ["rubyspec.h", "#{ext}.c"].each do |file| - cp "#{ext_dir}/#{file}", "#{tmpdir}/#{file}" + ["#{core_ext_dir}/rubyspec.h", "#{spec_ext_dir}/#{ext}.c"].each do |file| + cp file, "#{tmpdir}/#{File.basename(file)}" end Dir.chdir(tmpdir) do diff --git a/spec/ruby/optional/capi/thread_spec.rb b/spec/ruby/optional/capi/thread_spec.rb index f60e9ba065..52070198fd 100644 --- a/spec/ruby/optional/capi/thread_spec.rb +++ b/spec/ruby/optional/capi/thread_spec.rb @@ -123,5 +123,27 @@ describe "C-API Thread function" do # And we got a proper value thr.value.should be_true end + + it "runs a C function with the global lock unlocked amd unlocks IO with the generic RUBY_UBF_IO" do + thr = Thread.new do + @t.rb_thread_call_without_gvl_with_ubf_io + end + + # Wait until it's blocking... + Thread.pass while thr.status and thr.status != "sleep" + + # The thread status is set to sleep by rb_thread_call_without_gvl(), + # but the thread might not be in the blocking read(2) yet, so wait a bit. + sleep 0.1 + + # Wake it up, causing the unblock function to be run. + thr.wakeup + + # Make sure it stopped + thr.join(1).should_not be_nil + + # And we got a proper value + thr.value.should be_true + end end end diff --git a/spec/ruby/shared/complex/Complex.rb b/spec/ruby/shared/complex/Complex.rb deleted file mode 100644 index 201e09e6ce..0000000000 --- a/spec/ruby/shared/complex/Complex.rb +++ /dev/null @@ -1,141 +0,0 @@ -require_relative '../../spec_helper' - -describe :kernel_Complex, shared: true do - describe "when passed [Complex, Complex]" do - it "returns a new Complex number based on the two given numbers" do - Complex(Complex(3, 4), Complex(5, 6)).should == Complex(3 - 6, 4 + 5) - Complex(Complex(1.5, 2), Complex(-5, 6.3)).should == Complex(1.5 - 6.3, 2 - 5) - end - end - - describe "when passed [Complex]" do - it "returns the passed Complex number" do - Complex(Complex(1, 2)).should == Complex(1, 2) - Complex(Complex(-3.4, bignum_value)).should == Complex(-3.4, bignum_value) - end - end - - describe "when passed [Integer, Integer]" do - it "returns a new Complex number" do - Complex(1, 2).should be_an_instance_of(Complex) - Complex(1, 2).real.should == 1 - Complex(1, 2).imag.should == 2 - - Complex(-3, -5).should be_an_instance_of(Complex) - Complex(-3, -5).real.should == -3 - Complex(-3, -5).imag.should == -5 - - Complex(3.5, -4.5).should be_an_instance_of(Complex) - Complex(3.5, -4.5).real.should == 3.5 - Complex(3.5, -4.5).imag.should == -4.5 - - Complex(bignum_value, 30).should be_an_instance_of(Complex) - Complex(bignum_value, 30).real.should == bignum_value - Complex(bignum_value, 30).imag.should == 30 - end - end - - describe "when passed [Integer/Float]" do - it "returns a new Complex number with 0 as the imaginary component" do - # Guard against the Mathn library - conflicts_with :Prime do - Complex(1).should be_an_instance_of(Complex) - Complex(1).imag.should == 0 - Complex(1).real.should == 1 - - Complex(-3).should be_an_instance_of(Complex) - Complex(-3).imag.should == 0 - Complex(-3).real.should == -3 - - Complex(-4.5).should be_an_instance_of(Complex) - Complex(-4.5).imag.should == 0 - Complex(-4.5).real.should == -4.5 - - Complex(bignum_value).should be_an_instance_of(Complex) - Complex(bignum_value).imag.should == 0 - Complex(bignum_value).real.should == bignum_value - end - end - end - - describe "when passed a String" do - it "needs to be reviewed for spec completeness" - end - - describe "when passed an Object which responds to #to_c" do - it "returns the passed argument" do - obj = Object.new; def obj.to_c; 1i end - Complex(obj).should == Complex(0, 1) - end - end - - describe "when passed a Numeric which responds to #real? with false" do - it "returns the passed argument" do - n = mock_numeric("unreal") - n.should_receive(:real?).any_number_of_times.and_return(false) - Complex(n).should equal(n) - end - end - - describe "when passed a Numeric which responds to #real? with true" do - it "returns a Complex with the passed argument as the real component and 0 as the imaginary component" do - n = mock_numeric("real") - n.should_receive(:real?).any_number_of_times.and_return(true) - result = Complex(n) - result.real.should equal(n) - result.imag.should equal(0) - end - end - - describe "when 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 "returns n1 + n2 * Complex(0, 1)" do - n1 = mock_numeric("n1") - n2 = mock_numeric("n2") - n3 = mock_numeric("n3") - n4 = mock_numeric("n4") - n1.should_receive(:real?).any_number_of_times.and_return(r1) - n2.should_receive(:real?).any_number_of_times.and_return(r2) - n2.should_receive(:*).with(Complex(0, 1)).and_return(n3) - n1.should_receive(:+).with(n3).and_return(n4) - Complex(n1, n2).should equal(n4) - end - end - end - - describe "when passed two Numerics and both respond to #real? with true" do - it "returns a Complex with the passed arguments as real and imaginary components respectively" 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(n1, n2) - result.real.should equal(n1) - result.imag.should equal(n2) - end - end - - describe "when passed a single non-Numeric" do - it "coerces the passed argument using #to_c" do - n = mock("n") - c = Complex(0, 0) - n.should_receive(:to_c).and_return(c) - Complex(n).should equal(c) - end - end - - describe "when passed a non-Numeric second argument" do - it "raises TypeError" do - lambda { Complex.send(@method, :sym, :sym) }.should raise_error(TypeError) - lambda { Complex.send(@method, 0, :sym) }.should raise_error(TypeError) - end - end - - describe "when passed nil" do - it "raises TypeError" do - lambda { Complex(nil) }.should raise_error(TypeError, "can't convert nil into Complex") - lambda { Complex(0, nil) }.should raise_error(TypeError, "can't convert nil into Complex") - lambda { Complex(nil, 0) }.should raise_error(TypeError, "can't convert nil into Complex") - end - end -end diff --git a/spec/ruby/shared/complex/abs.rb b/spec/ruby/shared/complex/abs.rb deleted file mode 100644 index 2b51c416c2..0000000000 --- a/spec/ruby/shared/complex/abs.rb +++ /dev/null @@ -1,12 +0,0 @@ -require_relative '../../spec_helper' - -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/shared/complex/abs2.rb b/spec/ruby/shared/complex/abs2.rb deleted file mode 100644 index a39530d4e2..0000000000 --- a/spec/ruby/shared/complex/abs2.rb +++ /dev/null @@ -1,12 +0,0 @@ -require_relative '../../spec_helper' - -describe :complex_abs2, shared: true do - it "returns the sum of the squares of the real and imaginary parts" do - Complex(1, -2).abs2.should == 1 + 4 - Complex(-0.1, 0.2).abs2.should be_close(0.01 + 0.04, TOLERANCE) - # Guard against Mathn library - conflicts_with :Prime do - Complex(0).abs2.should == 0 - end - end -end diff --git a/spec/ruby/shared/complex/arg.rb b/spec/ruby/shared/complex/arg.rb deleted file mode 100644 index c81f197433..0000000000 --- a/spec/ruby/shared/complex/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/shared/complex/coerce.rb b/spec/ruby/shared/complex/coerce.rb deleted file mode 100644 index 2bb08cf5a2..0000000000 --- a/spec/ruby/shared/complex/coerce.rb +++ /dev/null @@ -1,70 +0,0 @@ -require_relative '../../spec_helper' - -describe :complex_coerce, shared: true do - before :each do - @one = Complex(1) - end - - 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) - 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) - 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) - 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) - 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) - end - - it "returns an array containing other as Complex and self when other is a Numeric which responds to #real? with true" do - other = mock_numeric('other') - 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) - 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) - lambda { @one.coerce(other) }.should raise_error(TypeError) - end - - it "raises a TypeError when other is a String" do - lambda { @one.coerce("20") }.should raise_error(TypeError) - end - - it "raises a TypeError when other is nil" do - lambda { @one.coerce(nil) }.should raise_error(TypeError) - end - - it "raises a TypeError when other is false" do - lambda { @one.coerce(false) }.should raise_error(TypeError) - end -end diff --git a/spec/ruby/shared/complex/conjugate.rb b/spec/ruby/shared/complex/conjugate.rb deleted file mode 100644 index d1ae47bcb6..0000000000 --- a/spec/ruby/shared/complex/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/shared/complex/constants.rb b/spec/ruby/shared/complex/constants.rb deleted file mode 100644 index a181fd9e30..0000000000 --- a/spec/ruby/shared/complex/constants.rb +++ /dev/null @@ -1,7 +0,0 @@ -require_relative '../../spec_helper' - -describe :complex_I, shared: true do - it "is Complex(0, 1)" do - Complex::I.should eql(Complex(0, 1)) - end -end diff --git a/spec/ruby/shared/complex/denominator.rb b/spec/ruby/shared/complex/denominator.rb deleted file mode 100644 index aee309cb11..0000000000 --- a/spec/ruby/shared/complex/denominator.rb +++ /dev/null @@ -1,13 +0,0 @@ -require_relative '../../spec_helper' - -describe :complex_denominator, shared: true do - it "returns the least common multiple denominator of the real and imaginary parts" do - Complex(3, 4).denominator.should == 1 - Complex(3, bignum_value).denominator.should == 1 - - Complex(3, Rational(3,4)).denominator.should == 4 - - Complex(Rational(4,8), Rational(3,4)).denominator.should == 4 - Complex(Rational(3,8), Rational(3,4)).denominator.should == 8 - end -end diff --git a/spec/ruby/shared/complex/divide.rb b/spec/ruby/shared/complex/divide.rb deleted file mode 100644 index 90143c6ef0..0000000000 --- a/spec/ruby/shared/complex/divide.rb +++ /dev/null @@ -1,84 +0,0 @@ -require_relative '../../spec_helper' - -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 - lambda { 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/shared/complex/equal_value.rb b/spec/ruby/shared/complex/equal_value.rb deleted file mode 100644 index 9f341d956f..0000000000 --- a/spec/ruby/shared/complex/equal_value.rb +++ /dev/null @@ -1,93 +0,0 @@ -require_relative '../../spec_helper' - -describe :complex_equal_value, shared: true do - describe "with Complex" do - it "returns true when self and other have numerical equality" do - Complex(1, 2).should == Complex(1, 2) - Complex(3, 9).should == Complex(3, 9) - Complex(-3, -9).should == Complex(-3, -9) - - Complex(1, 2).should_not == Complex(3, 4) - Complex(3, 9).should_not == Complex(9, 3) - - Complex(1.0, 2.0).should == Complex(1, 2) - Complex(3.0, 9.0).should_not == Complex(9.0, 3.0) - - Complex(1.5, 2.5).should == Complex(1.5, 2.5) - Complex(1.5, 2.5).should == Complex(1.5, 2.5) - Complex(-1.5, 2.5).should == Complex(-1.5, 2.5) - - Complex(1.5, 2.5).should_not == Complex(2.5, 1.5) - Complex(3.75, 2.5).should_not == Complex(1.5, 2.5) - - Complex(bignum_value, 2.5).should == Complex(bignum_value, 2.5) - Complex(3.75, bignum_value).should_not == Complex(1.5, bignum_value) - - Complex(nan_value).should_not == Complex(nan_value) - end - end - - describe "with Numeric" do - it "returns true when self's imaginary part is 0 and the real part and other have numerical equality" do - Complex(3, 0).should == 3 - Complex(-3, 0).should == -3 - - Complex(3.5, 0).should == 3.5 - Complex(-3.5, 0).should == -3.5 - - Complex(bignum_value, 0).should == bignum_value - Complex(-bignum_value, 0).should == -bignum_value - - Complex(3.0, 0).should == 3 - Complex(-3.0, 0).should == -3 - - Complex(3, 0).should_not == 4 - Complex(-3, 0).should_not == -4 - - Complex(3.5, 0).should_not == -4.5 - Complex(-3.5, 0).should_not == 2.5 - - Complex(bignum_value, 0).should_not == bignum_value(10) - Complex(-bignum_value, 0).should_not == -bignum_value(20) - end - end - - describe "with Object" do - # Fixnum#==, Float#== and Bignum#== only return booleans - Bug? - it "calls other#== with self" do - value = Complex(3, 0) - - obj = mock("Object") - obj.should_receive(:==).with(value).and_return(:expected) - - (value == obj).should_not be_false - end - end - - describe "with a Numeric which responds to #real? with true" do - before do - @other = mock_numeric('other') - @other.should_receive(:real?).any_number_of_times.and_return(true) - end - - 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 - end - - it "returns false when when the imaginary part is not zero" do - (Complex(3, 1) == @other).should be_false - end - end - - describe "with a Numeric which responds to #real? with false" do - it "returns other == self" do - complex = Complex(3, 0) - 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 - end - end -end diff --git a/spec/ruby/shared/complex/exponent.rb b/spec/ruby/shared/complex/exponent.rb deleted file mode 100644 index 650bb6d0f9..0000000000 --- a/spec/ruby/shared/complex/exponent.rb +++ /dev/null @@ -1,61 +0,0 @@ -require_relative '../../spec_helper' - -describe :complex_exponent, shared: true do - describe "with Fixnum 0" do - it "returns Complex(1)" do - (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)) - end - end - - describe "with Complex" do - it "returns self raised to the given power" do - (Complex(2, 1) ** Complex(2, 1)).should be_close(Complex(-0.504824688978319, 3.10414407699553), TOLERANCE) - (Complex(2, 1) ** Complex(3, 4)).should be_close(Complex(-0.179174656916581, -1.74071656397662), TOLERANCE) - - (Complex(2, 1) ** Complex(-2, -1)).should be_close(Complex(-0.051041070450869, -0.313849223270419), TOLERANCE) - (Complex(-2, -1) ** Complex(2, 1)).should be_close(Complex(-11.6819929610857, 71.8320439736158), TOLERANCE) - end - end - - describe "with Integer" do - it "returns self raised to the given power" do - (Complex(2, 1) ** 2).should == Complex(3, 4) - (Complex(3, 4) ** 2).should == Complex(-7, 24) - (Complex(3, 4) ** -2).should be_close(Complex(-0.0112, -0.0384), TOLERANCE) - - - (Complex(2, 1) ** 2.5).should be_close(Complex(2.99179707178602, 6.85206901006896), TOLERANCE) - (Complex(3, 4) ** 2.5).should be_close(Complex(-38.0, 41.0), TOLERANCE) - (Complex(3, 4) ** -2.5).should be_close(Complex(-0.01216, -0.01312), TOLERANCE) - - (Complex(1) ** 1).should == Complex(1) - - # NOTE: Takes way too long... - #(Complex(2, 1) ** bignum_value) - end - end - - describe "with Rational" do - it "returns self raised to the given power" do - (Complex(2, 1) ** Rational(3, 4)).should be_close(Complex(1.71913265276568, 0.623124744394697), TOLERANCE) - (Complex(2, 1) ** Rational(4, 3)).should be_close(Complex(2.3828547125173, 1.69466313833091), TOLERANCE) - (Complex(2, 1) ** Rational(-4, 3)).should be_close(Complex(0.278700377879388, -0.198209003071003), TOLERANCE) - 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([2, 5]) - (value ** obj).should == 2 ** 5 - end - end -end diff --git a/spec/ruby/shared/complex/float/arg.rb b/spec/ruby/shared/complex/float/arg.rb deleted file mode 100644 index a18d3b3e02..0000000000 --- a/spec/ruby/shared/complex/float/arg.rb +++ /dev/null @@ -1,38 +0,0 @@ -require_relative '../../../spec_helper' - -describe :float_arg, shared: true do - it "returns NaN if NaN" do - f = nan_value - f.send(@method).nan?.should be_true - end - - it "returns self if NaN" do - f = nan_value - f.send(@method).should equal(f) - end - - it "returns 0 if positive" do - 1.0.send(@method).should == 0 - end - - it "returns 0 if +0.0" do - 0.0.send(@method).should == 0 - end - - it "returns 0 if +Infinity" do - infinity_value.send(@method).should == 0 - end - - it "returns Pi if negative" do - (-1.0).send(@method).should == Math::PI - end - - # This was established in r23960 - it "returns Pi if -0.0" do - (-0.0).send(@method).should == Math::PI - end - - it "returns Pi if -Infinity" do - (-infinity_value).send(@method).should == Math::PI - end -end diff --git a/spec/ruby/shared/complex/image.rb b/spec/ruby/shared/complex/image.rb deleted file mode 100644 index f214b102f9..0000000000 --- a/spec/ruby/shared/complex/image.rb +++ /dev/null @@ -1,10 +0,0 @@ -require_relative '../../spec_helper' - -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/shared/complex/inspect.rb b/spec/ruby/shared/complex/inspect.rb deleted file mode 100644 index 7c0c3d6b9c..0000000000 --- a/spec/ruby/shared/complex/inspect.rb +++ /dev/null @@ -1,14 +0,0 @@ -describe :complex_inspect, shared: true do - it "returns (${real}+${image}i) for positive imaginary parts" do - Complex(1).inspect.should == "(1+0i)" - Complex(7).inspect.should == "(7+0i)" - Complex(-1, 4).inspect.should == "(-1+4i)" - Complex(-7, 6.7).inspect.should == "(-7+6.7i)" - end - - it "returns (${real}-${image}i) for negative imaginary parts" do - Complex(0, -1).inspect.should == "(0-1i)" - Complex(-1, -4).inspect.should == "(-1-4i)" - Complex(-7, -6.7).inspect.should == "(-7-6.7i)" - end -end diff --git a/spec/ruby/shared/complex/minus.rb b/spec/ruby/shared/complex/minus.rb deleted file mode 100644 index af6c368581..0000000000 --- a/spec/ruby/shared/complex/minus.rb +++ /dev/null @@ -1,45 +0,0 @@ -require_relative '../../spec_helper' - -describe :complex_minus, shared: true do - describe "with Complex" do - it "subtracts both the real and imaginary components" do - (Complex(1, 2) - Complex(10, 20)).should == Complex(1 - 10, 2 - 20) - (Complex(1.5, 2.1) - Complex(100.2, -30.3)).should == Complex(1.5 - 100.2, 2.1 - (-30.3)) - end - end - - describe "with Integer" do - it "subtracts the real number from the real component of self" do - (Complex(1, 2) - 50).should == Complex(-49, 2) - (Complex(1, 2) - 50.5).should == Complex(-49.5, 2) - 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([2, 5]) - (value - obj).should == 2 - 5 - end - end - - describe "passed Numeric which responds to #real? with true" do - it "coerces the passed argument to the type of the real part and subtracts the resulting elements" do - n = mock_numeric('n') - n.should_receive(:real?).and_return(true) - n.should_receive(:coerce).with(1).and_return([1, 4]) - (Complex(1, 2) - n).should == Complex(-3, 2) - end - end - - describe "passed Numeric which responds to #real? with false" do - it "coerces the passed argument to Complex and subtracts the resulting elements" do - n = mock_numeric('n') - n.should_receive(:real?).and_return(false) - n.should_receive(:coerce).with(Complex(1, 2)).and_return([Complex(1, 2), Complex(3, 4)]) - (Complex(1, 2) - n).should == Complex(-2, -2) - end - end -end diff --git a/spec/ruby/shared/complex/multiply.rb b/spec/ruby/shared/complex/multiply.rb deleted file mode 100644 index 0b711c2c80..0000000000 --- a/spec/ruby/shared/complex/multiply.rb +++ /dev/null @@ -1,49 +0,0 @@ -require_relative '../../spec_helper' - -describe :complex_multiply, shared: true do - describe "with Complex" do - it "multiplies according to the usual rule for complex numbers: (a + bi) * (c + di) = ac - bd + (ad + bc)i" do - (Complex(1, 2) * Complex(10, 20)).should == Complex((1 * 10) - (2 * 20), (1 * 20) + (2 * 10)) - (Complex(1.5, 2.1) * Complex(100.2, -30.3)).should == Complex((1.5 * 100.2) - (2.1 * -30.3), (1.5 * -30.3) + (2.1 * 100.2)) - end - end - - describe "with Integer" do - it "multiplies both parts of self by the given Integer" do - (Complex(3, 2) * 50).should == Complex(150, 100) - (Complex(-3, 2) * 50.5).should == Complex(-151.5, 101) - 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([2, 5]) - (value * obj).should == 2 * 5 - end - end - - describe "with a Numeric which responds to #real? with true" do - it "multiples both parts of self by other" do - other = mock_numeric('other') - real = mock_numeric('real') - imag = mock_numeric('imag') - other.should_receive(:real?).and_return(true) - real.should_receive(:*).with(other).and_return(1) - imag.should_receive(:*).with(other).and_return(2) - (Complex(real, imag) * other).should == Complex(1, 2) - end - - describe "with a Numeric which responds to #real? with false" do - it "coerces the passed argument to Complex and multiplies 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 == 10 - end - end - end -end diff --git a/spec/ruby/shared/complex/numerator.rb b/spec/ruby/shared/complex/numerator.rb deleted file mode 100644 index 5113d2ac8b..0000000000 --- a/spec/ruby/shared/complex/numerator.rb +++ /dev/null @@ -1,19 +0,0 @@ -require_relative '../../spec_helper' - -describe :complex_numerator, shared: true do - it "returns self's numerator" do - Complex(2).numerator.should == Complex(2) - Complex(3, 4).numerator.should == Complex(3, 4) - - Complex(Rational(3, 4), Rational(3, 4)).numerator.should == Complex(3, 3) - Complex(Rational(7, 4), Rational(8, 4)).numerator.should == Complex(7, 8) - - Complex(Rational(7, 8), Rational(8, 4)).numerator.should == Complex(7, 16) - Complex(Rational(7, 4), Rational(8, 8)).numerator.should == Complex(7, 4) - - # NOTE: - # Bug? - Fails with a MethodMissingError - # (undefined method `denominator' for 3.5:Float) - # Complex(3.5, 3.7).numerator - end -end diff --git a/spec/ruby/shared/complex/numeric/arg.rb b/spec/ruby/shared/complex/numeric/arg.rb deleted file mode 100644 index c8e7ad8333..0000000000 --- a/spec/ruby/shared/complex/numeric/arg.rb +++ /dev/null @@ -1,38 +0,0 @@ -require_relative '../../../spec_helper' - -describe :numeric_arg, shared: true do - before :each do - @numbers = [ - 20, - Rational(3, 4), - bignum_value, - infinity_value - ] - end - - it "returns 0 if positive" do - @numbers.each do |number| - number.send(@method).should == 0 - end - end - - it "returns Pi if negative" do - @numbers.each do |number| - (0-number).send(@method).should == Math::PI - end - end - - describe "with a Numeric subclass" do - it "returns 0 if self#<(0) returns false" do - numeric = mock_numeric('positive') - numeric.should_receive(:<).with(0).and_return(false) - numeric.send(@method).should == 0 - end - - it "returns Pi if self#<(0) returns true" do - numeric = mock_numeric('positive') - numeric.should_receive(:<).with(0).and_return(true) - numeric.send(@method).should == Math::PI - end - end -end diff --git a/spec/ruby/shared/complex/numeric/conj.rb b/spec/ruby/shared/complex/numeric/conj.rb deleted file mode 100644 index 6d5197ecab..0000000000 --- a/spec/ruby/shared/complex/numeric/conj.rb +++ /dev/null @@ -1,20 +0,0 @@ -require_relative '../../../spec_helper' - -describe :numeric_conj, shared: true do - before :each do - @numbers = [ - 20, # Integer - 398.72, # Float - Rational(3, 4), # Rational - bignum_value, - infinity_value, - nan_value - ] - end - - it "returns self" do - @numbers.each do |number| - number.send(@method).should equal(number) - end - end -end diff --git a/spec/ruby/shared/complex/numeric/imag.rb b/spec/ruby/shared/complex/numeric/imag.rb deleted file mode 100644 index 11daf0af55..0000000000 --- a/spec/ruby/shared/complex/numeric/imag.rb +++ /dev/null @@ -1,26 +0,0 @@ -require_relative '../../../spec_helper' - -describe :numeric_imag, shared: true do - before :each do - @numbers = [ - 20, # Integer - 398.72, # Float - Rational(3, 4), # Rational - bignum_value, # Bignum - infinity_value, - nan_value - ].map{|n| [n,-n]}.flatten - end - - it "returns 0" do - @numbers.each do |number| - number.send(@method).should == 0 - end - end - - it "raises an ArgumentError if given any arguments" do - @numbers.each do |number| - lambda { number.send(@method, number) }.should raise_error(ArgumentError) - end - end -end diff --git a/spec/ruby/shared/complex/numeric/polar.rb b/spec/ruby/shared/complex/numeric/polar.rb deleted file mode 100644 index 74586c52ad..0000000000 --- a/spec/ruby/shared/complex/numeric/polar.rb +++ /dev/null @@ -1,50 +0,0 @@ -require_relative '../../../spec_helper' - -describe :numeric_polar, shared: true do - before :each do - @pos_numbers = [ - 1, - 3898172610**9, - 987.18273, - Float::MAX, - Rational(13,7), - infinity_value, - ] - @neg_numbers = @pos_numbers.map {|n| -n} - @numbers = @pos_numbers + @neg_numbers - @numbers.push(0, 0.0) - end - - it "returns a two-element Array" do - @numbers.each do |number| - number.polar.should be_an_instance_of(Array) - number.polar.size.should == 2 - end - end - - it "sets the first value to the absolute value of self" do - @numbers.each do |number| - number.polar.first.should == number.abs - end - end - - it "sets the last value to 0 if self is positive" do - (@numbers - @neg_numbers).each do |number| - number.should >= 0 - number.polar.last.should == 0 - end - end - - it "sets the last value to Pi if self is negative" do - @neg_numbers.each do |number| - number.should < 0 - number.polar.last.should == Math::PI - end - end - - it "returns [NaN, NaN] if self is NaN" do - nan_value.polar.size.should == 2 - nan_value.polar.first.nan?.should be_true - nan_value.polar.last.nan?.should be_true - end -end diff --git a/spec/ruby/shared/complex/numeric/real.rb b/spec/ruby/shared/complex/numeric/real.rb deleted file mode 100644 index d00e3b41cf..0000000000 --- a/spec/ruby/shared/complex/numeric/real.rb +++ /dev/null @@ -1,30 +0,0 @@ -require_relative '../../../spec_helper' - -describe :numeric_real, shared: true do - before :each do - @numbers = [ - 20, # Integer - 398.72, # Float - Rational(3, 4), # Rational - bignum_value, # Bignum - infinity_value, - nan_value - ].map{|n| [n,-n]}.flatten - end - - it "returns self" do - @numbers.each do |number| - if number.to_f.nan? - number.real.nan?.should be_true - else - number.real.should == number - end - end - end - - it "raises an ArgumentError if given any arguments" do - @numbers.each do |number| - lambda { number.real(number) }.should raise_error(ArgumentError) - end - end -end diff --git a/spec/ruby/shared/complex/plus.rb b/spec/ruby/shared/complex/plus.rb deleted file mode 100644 index d258720e61..0000000000 --- a/spec/ruby/shared/complex/plus.rb +++ /dev/null @@ -1,45 +0,0 @@ -require_relative '../../spec_helper' - -describe :complex_plus, shared: true do - describe "with Complex" do - it "adds both the real and imaginary components" do - (Complex(1, 2) + Complex(10, 20)).should == Complex(1 + 10, 2 + 20) - (Complex(1.5, 2.1) + Complex(100.2, -30.3)).should == Complex(1.5 + 100.2, 2.1 + (-30.3)) - end - end - - describe "with Integer" do - it "adds the real number to the real component of self" do - (Complex(1, 2) + 50).should == Complex(51, 2) - (Complex(1, 2) + 50.5).should == Complex(51.5, 2) - 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([2, 5]) - (value + obj).should == 2 + 5 - end - end - - describe "passed Numeric which responds to #real? with true" do - it "coerces the passed argument to the type of the real part and adds the resulting elements" do - n = mock_numeric('n') - n.should_receive(:real?).and_return(true) - n.should_receive(:coerce).with(1).and_return([1, 4]) - (Complex(1, 2) + n).should == Complex(5, 2) - end - end - - describe "passed Numeric which responds to #real? with false" do - it "coerces the passed argument to Complex and adds the resulting elements" do - n = mock_numeric('n') - n.should_receive(:real?).and_return(false) - n.should_receive(:coerce).with(Complex(1, 2)).and_return([Complex(1, 2), Complex(3, 4)]) - (Complex(1, 2) + n).should == Complex(4, 6) - end - end -end diff --git a/spec/ruby/shared/complex/polar.rb b/spec/ruby/shared/complex/polar.rb deleted file mode 100644 index 9703cd3ce7..0000000000 --- a/spec/ruby/shared/complex/polar.rb +++ /dev/null @@ -1,22 +0,0 @@ -require_relative '../../spec_helper' - -describe :complex_polar_class, shared: true do - it "returns a complex number in terms of radius and angle" do - Complex.polar(50, 60).should be_close(Complex(-47.6206490207578, -15.2405310551108), TOLERANCE) - Complex.polar(-10, -20).should be_close(Complex(-4.08082061813392, 9.12945250727628), TOLERANCE) - end -end - -describe :complex_polar, shared: true do - it "returns the absolute value and the argument" do - a = Complex(3, 4) - a.polar.size.should == 2 - a.polar.first.should == 5.0 - a.polar.last.should be_close(0.927295218001612, TOLERANCE) - - b = Complex(-3.5, 4.7) - b.polar.size.should == 2 - b.polar.first.should be_close(5.86003412959345, TOLERANCE) - b.polar.last.should be_close(2.21088447955664, TOLERANCE) - end -end diff --git a/spec/ruby/shared/complex/real.rb b/spec/ruby/shared/complex/real.rb deleted file mode 100644 index ab8ed07a4d..0000000000 --- a/spec/ruby/shared/complex/real.rb +++ /dev/null @@ -1,8 +0,0 @@ -describe :complex_real, shared: true do - it "returns the real part of self" do - Complex(1, 0).real.should == 1 - Complex(2, 1).real.should == 2 - Complex(6.7, 8.9).real.should == 6.7 - Complex(bignum_value, 3).real.should == bignum_value - end -end diff --git a/spec/ruby/shared/complex/rect.rb b/spec/ruby/shared/complex/rect.rb deleted file mode 100644 index cb4bd75b4a..0000000000 --- a/spec/ruby/shared/complex/rect.rb +++ /dev/null @@ -1,96 +0,0 @@ -require_relative '../../spec_helper' - -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| - lambda { 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) - lambda { 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) - lambda { 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 - lambda { Complex.send(@method, :sym) }.should raise_error(TypeError) - lambda { Complex.send(@method, 0, :sym) }.should raise_error(TypeError) - end - end -end diff --git a/spec/ruby/shared/complex/to_s.rb b/spec/ruby/shared/complex/to_s.rb deleted file mode 100644 index 6cc20bdafe..0000000000 --- a/spec/ruby/shared/complex/to_s.rb +++ /dev/null @@ -1,44 +0,0 @@ -require_relative '../../spec_helper' - -describe :complex_to_s, shared: true do - describe "when self's real component is 0" do - it "returns both the real and imaginary component even when the real is 0" do - Complex(0, 5).to_s.should == "0+5i" - Complex(0, -3.2).to_s.should == "0-3.2i" - end - end - - it "returns self as String" do - Complex(1, 5).to_s.should == "1+5i" - Complex(-2.5, 1.5).to_s.should == "-2.5+1.5i" - - Complex(1, -5).to_s.should == "1-5i" - Complex(-2.5, -1.5).to_s.should == "-2.5-1.5i" - - # Guard against the Mathn library - conflicts_with :Prime do - Complex(1, 0).to_s.should == "1+0i" - Complex(1, -0).to_s.should == "1+0i" - end - end - - it "returns 1+0.0i for Complex(1, 0.0)" do - Complex(1, 0.0).to_s.should == "1+0.0i" - end - - it "returns 1-0.0i for Complex(1, -0.0)" do - Complex(1, -0.0).to_s.should == "1-0.0i" - end - - it "returns 1+Infinity*i for Complex(1, Infinity)" do - Complex(1, infinity_value).to_s.should == "1+Infinity*i" - end - - it "returns 1-Infinity*i for Complex(1, -Infinity)" do - Complex(1, -infinity_value).to_s.should == "1-Infinity*i" - end - - it "returns 1+NaN*i for Complex(1, NaN)" do - Complex(1, nan_value).to_s.should == "1+NaN*i" - end -end diff --git a/spec/ruby/shared/rational/Rational.rb b/spec/ruby/shared/rational/Rational.rb index 68f0150186..752225269e 100644 --- a/spec/ruby/shared/rational/Rational.rb +++ b/spec/ruby/shared/rational/Rational.rb @@ -4,7 +4,7 @@ require_relative '../../fixtures/rational' describe :kernel_Rational, shared: true do describe "passed Integer" do # Guard against the Mathn library - conflicts_with :Prime do + guard -> { !defined?(Math.rsqrt) } do it "returns a new Rational number with 1 as the denominator" do Rational(1).should eql(Rational(1, 1)) Rational(-3).should eql(Rational(-3, 1)) diff --git a/spec/ruby/shared/rational/exponent.rb b/spec/ruby/shared/rational/exponent.rb index f40f3c57d6..ac8237c291 100644 --- a/spec/ruby/shared/rational/exponent.rb +++ b/spec/ruby/shared/rational/exponent.rb @@ -2,7 +2,8 @@ require_relative '../../spec_helper' describe :rational_exponent, shared: true do describe "when passed Rational" do - conflicts_with :Prime do + # Guard against the Mathn library + guard -> { !defined?(Math.rsqrt) } do it "returns Rational(1) if the exponent is Rational(0)" do (Rational(0) ** Rational(0)).should eql(Rational(1)) (Rational(1) ** Rational(0)).should eql(Rational(1)) @@ -45,7 +46,8 @@ describe :rational_exponent, shared: true do (Rational(3, -bignum_value) ** -4).should == Rational(7237005577332262213973186563042994240829374041602535252466099000494570602496, 81) end - conflicts_with :Prime do + # Guard against the Mathn library + guard -> { !defined?(Math.rsqrt) } do it "returns Rational(1, 1) when the passed argument is 0" do (Rational(3, 4) ** 0).should eql(Rational(1, 1)) (Rational(-3, 4) ** 0).should eql(Rational(1, 1)) diff --git a/spec/ruby/shared/rational/inspect.rb b/spec/ruby/shared/rational/inspect.rb index a7fd42ccac..19691a2f25 100644 --- a/spec/ruby/shared/rational/inspect.rb +++ b/spec/ruby/shared/rational/inspect.rb @@ -1,11 +1,13 @@ require_relative '../../spec_helper' describe :rational_inspect, shared: true do - conflicts_with :Prime do - it "returns a string representation of self" do - Rational(3, 4).inspect.should == "(3/4)" - Rational(-5, 8).inspect.should == "(-5/8)" - Rational(-1, -2).inspect.should == "(1/2)" + it "returns a string representation of self" do + Rational(3, 4).inspect.should == "(3/4)" + Rational(-5, 8).inspect.should == "(-5/8)" + Rational(-1, -2).inspect.should == "(1/2)" + + # Guard against the Mathn library + guard -> { !defined?(Math.rsqrt) } do Rational(bignum_value, 1).inspect.should == "(#{bignum_value}/1)" end end diff --git a/spec/ruby/shared/rational/round.rb b/spec/ruby/shared/rational/round.rb index a3f92413e7..36ed476350 100644 --- a/spec/ruby/shared/rational/round.rb +++ b/spec/ruby/shared/rational/round.rb @@ -47,8 +47,11 @@ describe :rational_round, shared: true do it "returns a Rational" do @rational.round(1).should be_kind_of(Rational) @rational.round(2).should be_kind_of(Rational) - Rational(0, 1).round(1).should be_kind_of(Rational) - Rational(2, 1).round(1).should be_kind_of(Rational) + # Guard against the Mathn library + guard -> { !defined?(Math.rsqrt) } do + Rational(0, 1).round(1).should be_kind_of(Rational) + Rational(2, 1).round(1).should be_kind_of(Rational) + end end it "moves the truncation point n decimal places right" do diff --git a/spec/ruby/shared/rational/to_r.rb b/spec/ruby/shared/rational/to_r.rb index 9ae7571ffe..372c086850 100644 --- a/spec/ruby/shared/rational/to_r.rb +++ b/spec/ruby/shared/rational/to_r.rb @@ -1,13 +1,11 @@ require_relative '../../spec_helper' describe :rational_to_r, shared: true do - conflicts_with :Prime do - it "returns self" do - a = Rational(3, 4) - a.to_r.should equal(a) + it "returns self" do + a = Rational(3, 4) + a.to_r.should equal(a) - a = Rational(bignum_value, 4) - a.to_r.should equal(a) - end + a = Rational(bignum_value, 4) + a.to_r.should equal(a) end end diff --git a/spec/ruby/shared/rational/to_s.rb b/spec/ruby/shared/rational/to_s.rb index b16594f10c..e90c6e5e39 100644 --- a/spec/ruby/shared/rational/to_s.rb +++ b/spec/ruby/shared/rational/to_s.rb @@ -2,8 +2,11 @@ require_relative '../../spec_helper' describe :rational_to_s, shared: true do it "returns a string representation of self" do - Rational(1, 1).to_s.should == "1/1" - Rational(2, 1).to_s.should == "2/1" + # Guard against the Mathn library + guard -> { !defined?(Math.rsqrt) } do + Rational(1, 1).to_s.should == "1/1" + Rational(2, 1).to_s.should == "2/1" + end Rational(1, 2).to_s.should == "1/2" Rational(-1, 3).to_s.should == "-1/3" Rational(1, -3).to_s.should == "-1/3" -- cgit v1.2.3