diff options
Diffstat (limited to 'spec/ruby/core/complex')
39 files changed, 548 insertions, 0 deletions
diff --git a/spec/ruby/core/complex/abs2_spec.rb b/spec/ruby/core/complex/abs2_spec.rb new file mode 100644 index 0000000000..debfade075 --- /dev/null +++ b/spec/ruby/core/complex/abs2_spec.rb @@ -0,0 +1,5 @@ +require File.expand_path('../../../shared/complex/abs2', __FILE__) + +describe "Complex#abs2" do + it_behaves_like(:complex_abs2, :abs2) +end diff --git a/spec/ruby/core/complex/abs_spec.rb b/spec/ruby/core/complex/abs_spec.rb new file mode 100644 index 0000000000..a00d161ee9 --- /dev/null +++ b/spec/ruby/core/complex/abs_spec.rb @@ -0,0 +1,5 @@ +require File.expand_path('../../../shared/complex/abs', __FILE__) + +describe "Complex#abs" do + it_behaves_like(:complex_abs, :abs) +end diff --git a/spec/ruby/core/complex/angle_spec.rb b/spec/ruby/core/complex/angle_spec.rb new file mode 100644 index 0000000000..f0c46bfd03 --- /dev/null +++ b/spec/ruby/core/complex/angle_spec.rb @@ -0,0 +1,7 @@ +require File.expand_path('../../../spec_helper', __FILE__) + +require File.expand_path('../../../shared/complex/arg', __FILE__) + +describe "Complex#angle" do + it_behaves_like(:complex_arg, :angle) +end diff --git a/spec/ruby/core/complex/arg_spec.rb b/spec/ruby/core/complex/arg_spec.rb new file mode 100644 index 0000000000..48f8a94cf5 --- /dev/null +++ b/spec/ruby/core/complex/arg_spec.rb @@ -0,0 +1,7 @@ +require File.expand_path('../../../spec_helper', __FILE__) + +require File.expand_path('../../../shared/complex/arg', __FILE__) + +describe "Complex#arg" do + it_behaves_like(:complex_arg, :arg) +end diff --git a/spec/ruby/core/complex/coerce_spec.rb b/spec/ruby/core/complex/coerce_spec.rb new file mode 100644 index 0000000000..7c01170fde --- /dev/null +++ b/spec/ruby/core/complex/coerce_spec.rb @@ -0,0 +1,5 @@ +require File.expand_path('../../../shared/complex/coerce', __FILE__) + +describe "Complex#coerce" do + it_behaves_like(:complex_coerce, :coerce) +end diff --git a/spec/ruby/core/complex/conj_spec.rb b/spec/ruby/core/complex/conj_spec.rb new file mode 100644 index 0000000000..ad2c885b3b --- /dev/null +++ b/spec/ruby/core/complex/conj_spec.rb @@ -0,0 +1,6 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require File.expand_path('../../../shared/complex/conjugate', __FILE__) + +describe "Complex#conj" do + it_behaves_like(:complex_conjugate, :conj) +end diff --git a/spec/ruby/core/complex/conjugate_spec.rb b/spec/ruby/core/complex/conjugate_spec.rb new file mode 100644 index 0000000000..7fc2ddb430 --- /dev/null +++ b/spec/ruby/core/complex/conjugate_spec.rb @@ -0,0 +1,6 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require File.expand_path('../../../shared/complex/conjugate', __FILE__) + +describe "Complex#conjugate" do + it_behaves_like(:complex_conjugate, :conjugate) +end diff --git a/spec/ruby/core/complex/constants_spec.rb b/spec/ruby/core/complex/constants_spec.rb new file mode 100644 index 0000000000..a8fcebbd31 --- /dev/null +++ b/spec/ruby/core/complex/constants_spec.rb @@ -0,0 +1,5 @@ +require File.expand_path('../../../shared/complex/constants', __FILE__) + +describe "Complex::I" do + it_behaves_like :complex_I, :I +end diff --git a/spec/ruby/core/complex/denominator_spec.rb b/spec/ruby/core/complex/denominator_spec.rb new file mode 100644 index 0000000000..2568967968 --- /dev/null +++ b/spec/ruby/core/complex/denominator_spec.rb @@ -0,0 +1,5 @@ +require File.expand_path('../../../shared/complex/denominator', __FILE__) + +describe "Complex#denominator" do + it_behaves_like(:complex_denominator, :denominator) +end diff --git a/spec/ruby/core/complex/divide_spec.rb b/spec/ruby/core/complex/divide_spec.rb new file mode 100644 index 0000000000..71614c76e1 --- /dev/null +++ b/spec/ruby/core/complex/divide_spec.rb @@ -0,0 +1,5 @@ +require File.expand_path('../../../shared/complex/divide', __FILE__) + +describe "Complex#/" do + it_behaves_like :complex_divide, :/ +end diff --git a/spec/ruby/core/complex/eql_spec.rb b/spec/ruby/core/complex/eql_spec.rb new file mode 100644 index 0000000000..c8e432029f --- /dev/null +++ b/spec/ruby/core/complex/eql_spec.rb @@ -0,0 +1,31 @@ +require File.expand_path('../../../spec_helper', __FILE__) + +describe "Complex#eql?" do + it "returns false if other is not Complex" do + Complex(1).eql?(1).should be_false + end + + it "returns true when the respective parts are of the same classes and self == other" do + Complex(1, 2).eql?(Complex(1, 2)).should be_true + end + + it "returns false when the real parts are of different classes" do + Complex(1).eql?(Complex(1.0)).should be_false + end + + it "returns false when the imaginary parts are of different classes" do + Complex(1, 2).eql?(Complex(1, 2.0)).should be_false + end + + it "returns false when self == other is false" do + Complex(1, 2).eql?(Complex(2, 3)).should be_false + end + + it "does NOT send #eql? to real or imaginary parts" do + real = mock_numeric('real') + imag = mock_numeric('imag') + real.should_not_receive(:eql?) + imag.should_not_receive(:eql?) + Complex(real, imag).eql?(Complex(real, imag)).should be_true + end +end diff --git a/spec/ruby/core/complex/equal_value_spec.rb b/spec/ruby/core/complex/equal_value_spec.rb new file mode 100644 index 0000000000..b3d93911bd --- /dev/null +++ b/spec/ruby/core/complex/equal_value_spec.rb @@ -0,0 +1,5 @@ +require File.expand_path('../../../shared/complex/equal_value', __FILE__) + +describe "Complex#==" do + it_behaves_like :complex_equal_value, :== +end diff --git a/spec/ruby/core/complex/exponent_spec.rb b/spec/ruby/core/complex/exponent_spec.rb new file mode 100644 index 0000000000..62f61a2bf3 --- /dev/null +++ b/spec/ruby/core/complex/exponent_spec.rb @@ -0,0 +1,5 @@ +require File.expand_path('../../../shared/complex/exponent', __FILE__) + +describe "Complex#**" do + it_behaves_like :complex_exponent, :** +end diff --git a/spec/ruby/core/complex/fdiv_spec.rb b/spec/ruby/core/complex/fdiv_spec.rb new file mode 100644 index 0000000000..8211dfc9de --- /dev/null +++ b/spec/ruby/core/complex/fdiv_spec.rb @@ -0,0 +1,129 @@ +require File.expand_path('../../../spec_helper', __FILE__) + +describe "Complex#fdiv" do + it "accepts a numeric argument" do + lambda { Complex(20).fdiv(2) }.should_not raise_error(TypeError) + lambda { Complex(20).fdiv(2.0) }.should_not raise_error(TypeError) + lambda { Complex(20).fdiv(bignum_value) }.should_not raise_error(TypeError) + end + + it "accepts a negative numeric argument" do + lambda { Complex(20).fdiv(-2) }.should_not raise_error(TypeError) + lambda { Complex(20).fdiv(-2.0) }.should_not raise_error(TypeError) + lambda { Complex(20).fdiv(-bignum_value) }.should_not raise_error(TypeError) + end + + it "raises a TypeError if passed a non-numeric argument" do + lambda { Complex(20).fdiv([]) }.should raise_error(TypeError) + lambda { Complex(20).fdiv(:sym) }.should raise_error(TypeError) + lambda { Complex(20).fdiv('s') }.should raise_error(TypeError) + end + + it "sets the real part to NaN if self's real part is NaN" do + Complex(nan_value).fdiv(2).real.nan?.should be_true + end + + it "sets the imaginary part to NaN if self's imaginary part is NaN" do + Complex(2, nan_value).fdiv(2).imag.nan?.should be_true + end + + it "sets the real and imaginary part to NaN if self's real and imaginary parts are NaN" do + Complex(nan_value, nan_value).fdiv(2).imag.nan?.should be_true + Complex(nan_value, nan_value).fdiv(2).real.nan?.should be_true + end + + it "sets the real and imaginary part to NaN if self's real part and the argument are both NaN" do + Complex(nan_value, 2).fdiv(nan_value).imag.nan?.should be_true + Complex(nan_value, 2).fdiv(nan_value).real.nan?.should be_true + end + + it "sets the real and imaginary part to NaN if self's real part, self's imaginary part, and the argument are NaN" do + Complex(nan_value, nan_value).fdiv(nan_value).imag.nan?.should be_true + Complex(nan_value, nan_value).fdiv(nan_value).real.nan?.should be_true + end + + it "sets the real part to Infinity if self's real part is Infinity" do + Complex(infinity_value).fdiv(2).real.infinite?.should == 1 + Complex(infinity_value,2).fdiv(2).real.infinite?.should == 1 + end + + it "sets the imaginary part to Infinity if self's imaginary part is Infinity" do + Complex(2, infinity_value).fdiv(2).imag.infinite?.should == 1 + Complex(2, infinity_value).fdiv(2).imag.infinite?.should == 1 + end + + it "sets the imaginary and real part to Infinity if self's imaginary and real parts are Infinity" do + Complex(infinity_value, infinity_value).fdiv(2).real.infinite?.should == 1 + Complex(infinity_value, infinity_value).fdiv(2).imag.infinite?.should == 1 + end + + it "sets the real part to NaN and the imaginary part to NaN if self's imaginary part, self's real part, and the argument are Infinity" do + Complex(infinity_value, infinity_value).fdiv(infinity_value).real.nan?.should be_true + Complex(infinity_value, infinity_value).fdiv(infinity_value).imag.nan?.should be_true + end +end + +describe "Complex#fdiv with no imaginary part" do + before :each do + @numbers = [1, 5.43, 10, bignum_value, 99872.2918710].map{|n| [n,-n]}.flatten + end + + it "returns a Complex number" do + @numbers.each do |real| + @numbers.each do |other| + Complex(real).fdiv(other).should be_an_instance_of(Complex) + end + end + end + + it "sets the real part to self's real part fdiv'd with the argument" do + @numbers.each do |real| + @numbers.each do |other| + Complex(real).fdiv(other).real.should == real.fdiv(other) + end + end + end + + it "sets the imaginary part to 0.0" do + @numbers.each do |real| + @numbers.each do |other| + Complex(real).fdiv(other).imaginary.should == 0.0 + end + end + end +end + +describe "Complex#fdiv with an imaginary part" do + before :each do + @numbers = [1, 5.43, 10, bignum_value, 99872.2918710].map{|n| [n,-n]}.flatten + end + + it "returns a Complex number" do + @numbers.each do |real| + @numbers.each_with_index do |other,idx| + Complex( + real,@numbers[idx == 0 ? -1 : idx-1] + ).fdiv(other).should be_an_instance_of(Complex) + end + end + end + + it "sets the real part to self's real part fdiv'd with the argument" do + @numbers.each do |real| + @numbers.each_with_index do |other,idx| + Complex( + real,@numbers[idx == 0 ? -1 : idx-1] + ).fdiv(other).real.should == real.fdiv(other) + end + end + end + + it "sets the imaginary part to the imaginary part fdiv'd with the argument" do + @numbers.each do |real| + @numbers.each_with_index do |other,idx| + im = @numbers[idx == 0 ? -1 : idx-1] + Complex(real, im).fdiv(other).imag.should == im.fdiv(other) + end + end + end +end diff --git a/spec/ruby/core/complex/hash_spec.rb b/spec/ruby/core/complex/hash_spec.rb new file mode 100644 index 0000000000..db4b3590df --- /dev/null +++ b/spec/ruby/core/complex/hash_spec.rb @@ -0,0 +1,6 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require File.expand_path('../../../shared/complex/hash', __FILE__) + +describe "Complex#hash" do + it_behaves_like(:complex_hash, :hash) +end diff --git a/spec/ruby/core/complex/imag_spec.rb b/spec/ruby/core/complex/imag_spec.rb new file mode 100644 index 0000000000..6aa8803f5d --- /dev/null +++ b/spec/ruby/core/complex/imag_spec.rb @@ -0,0 +1,5 @@ +require File.expand_path('../../../shared/complex/image', __FILE__) + +describe "Complex#imag" do + it_behaves_like(:complex_image, :imag) +end diff --git a/spec/ruby/core/complex/imaginary_spec.rb b/spec/ruby/core/complex/imaginary_spec.rb new file mode 100644 index 0000000000..ecae19283b --- /dev/null +++ b/spec/ruby/core/complex/imaginary_spec.rb @@ -0,0 +1,5 @@ +require File.expand_path('../../../shared/complex/image', __FILE__) + +describe "Complex#imaginary" do + it_behaves_like :complex_image, :imaginary +end diff --git a/spec/ruby/core/complex/inspect_spec.rb b/spec/ruby/core/complex/inspect_spec.rb new file mode 100644 index 0000000000..b766e7f730 --- /dev/null +++ b/spec/ruby/core/complex/inspect_spec.rb @@ -0,0 +1,5 @@ +require File.expand_path('../../../shared/complex/inspect', __FILE__) + +describe "Complex#inspect" do + it_behaves_like(:complex_inspect, :inspect) +end diff --git a/spec/ruby/core/complex/integer_spec.rb b/spec/ruby/core/complex/integer_spec.rb new file mode 100644 index 0000000000..54b420e62c --- /dev/null +++ b/spec/ruby/core/complex/integer_spec.rb @@ -0,0 +1,9 @@ +describe "Complex#integer?" do + it "returns false for a Complex with no imaginary part" do + Complex(20).integer?.should be_false + end + + it "returns false for a Complex with an imaginary part" do + Complex(20,3).integer?.should be_false + end +end diff --git a/spec/ruby/core/complex/magnitude_spec.rb b/spec/ruby/core/complex/magnitude_spec.rb new file mode 100644 index 0000000000..e9175d763e --- /dev/null +++ b/spec/ruby/core/complex/magnitude_spec.rb @@ -0,0 +1,5 @@ +require File.expand_path('../../../shared/complex/abs', __FILE__) + +describe "Complex#magnitude" do + it_behaves_like(:complex_abs, :magnitude) +end diff --git a/spec/ruby/core/complex/marshal_dump_spec.rb b/spec/ruby/core/complex/marshal_dump_spec.rb new file mode 100644 index 0000000000..8d37929f54 --- /dev/null +++ b/spec/ruby/core/complex/marshal_dump_spec.rb @@ -0,0 +1,11 @@ +require File.expand_path('../../../spec_helper', __FILE__) + +describe "Complex#marshal_dump" do + it "is a private method" do + Complex.should have_private_instance_method(:marshal_dump, false) + end + + it "dumps real and imaginary parts" do + Complex(1, 2).send(:marshal_dump).should == [1, 2] + end +end diff --git a/spec/ruby/core/complex/minus_spec.rb b/spec/ruby/core/complex/minus_spec.rb new file mode 100644 index 0000000000..2b7b8bb270 --- /dev/null +++ b/spec/ruby/core/complex/minus_spec.rb @@ -0,0 +1,5 @@ +require File.expand_path('../../../shared/complex/minus', __FILE__) + +describe "Complex#-" do + it_behaves_like :complex_minus, :- +end diff --git a/spec/ruby/core/complex/multiply_spec.rb b/spec/ruby/core/complex/multiply_spec.rb new file mode 100644 index 0000000000..7f600fc1ab --- /dev/null +++ b/spec/ruby/core/complex/multiply_spec.rb @@ -0,0 +1,5 @@ +require File.expand_path('../../../shared/complex/multiply', __FILE__) + +describe "Complex#*" do + it_behaves_like :complex_multiply, :* +end diff --git a/spec/ruby/core/complex/negative_spec.rb b/spec/ruby/core/complex/negative_spec.rb new file mode 100644 index 0000000000..5b51933106 --- /dev/null +++ b/spec/ruby/core/complex/negative_spec.rb @@ -0,0 +1,11 @@ +describe "Complex#negative?" do + it "is undefined" do + c = Complex(1) + + c.methods.should_not include(:negative?) + + lambda { + c.negative? + }.should raise_error(NoMethodError) + end +end diff --git a/spec/ruby/core/complex/numerator_spec.rb b/spec/ruby/core/complex/numerator_spec.rb new file mode 100644 index 0000000000..8c0e8761bd --- /dev/null +++ b/spec/ruby/core/complex/numerator_spec.rb @@ -0,0 +1,5 @@ +require File.expand_path('../../../shared/complex/numerator', __FILE__) + +describe "Complex#numerator" do + it_behaves_like(:complex_numerator, :numerator) +end diff --git a/spec/ruby/core/complex/phase_spec.rb b/spec/ruby/core/complex/phase_spec.rb new file mode 100644 index 0000000000..c17f922c7f --- /dev/null +++ b/spec/ruby/core/complex/phase_spec.rb @@ -0,0 +1,6 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require File.expand_path('../../../shared/complex/arg', __FILE__) + +describe "Complex#phase" do + it_behaves_like :complex_arg, :phase +end diff --git a/spec/ruby/core/complex/plus_spec.rb b/spec/ruby/core/complex/plus_spec.rb new file mode 100644 index 0000000000..076582681f --- /dev/null +++ b/spec/ruby/core/complex/plus_spec.rb @@ -0,0 +1,5 @@ +require File.expand_path('../../../shared/complex/plus', __FILE__) + +describe "Complex#+" do + it_behaves_like :complex_plus, :+ +end diff --git a/spec/ruby/core/complex/polar_spec.rb b/spec/ruby/core/complex/polar_spec.rb new file mode 100644 index 0000000000..d847e916ff --- /dev/null +++ b/spec/ruby/core/complex/polar_spec.rb @@ -0,0 +1,14 @@ +require File.expand_path('../../../shared/complex/polar', __FILE__) + +describe "Complex.polar" do + it_behaves_like(:complex_polar_class, :polar) + + it "raises a TypeError when given non real arguments" do + lambda{ Complex.polar(nil) }.should raise_error(TypeError) + lambda{ Complex.polar(nil, nil) }.should raise_error(TypeError) + end +end + +describe "Complex#polar" do + it_behaves_like(:complex_polar, :polar) +end diff --git a/spec/ruby/core/complex/positive_spec.rb b/spec/ruby/core/complex/positive_spec.rb new file mode 100644 index 0000000000..88898d31cf --- /dev/null +++ b/spec/ruby/core/complex/positive_spec.rb @@ -0,0 +1,11 @@ +describe "Complex#positive?" do + it "is undefined" do + c = Complex(1) + + c.methods.should_not include(:positive?) + + lambda { + c.positive? + }.should raise_error(NoMethodError) + end +end diff --git a/spec/ruby/core/complex/quo_spec.rb b/spec/ruby/core/complex/quo_spec.rb new file mode 100644 index 0000000000..cb3f1203b4 --- /dev/null +++ b/spec/ruby/core/complex/quo_spec.rb @@ -0,0 +1,5 @@ +require File.expand_path('../../../shared/complex/divide', __FILE__) + +describe "Complex#quo" do + it_behaves_like :complex_divide, :quo +end diff --git a/spec/ruby/core/complex/rationalize_spec.rb b/spec/ruby/core/complex/rationalize_spec.rb new file mode 100644 index 0000000000..2dd1e9b122 --- /dev/null +++ b/spec/ruby/core/complex/rationalize_spec.rb @@ -0,0 +1,29 @@ +describe "Complex#rationalize" do + it "raises RangeError if self has non-zero imaginary part" do + lambda { Complex(1,5).rationalize }.should raise_error(RangeError) + end + + it "raises RangeError if self has 0.0 imaginary part" do + lambda { Complex(1,0.0).rationalize }.should raise_error(RangeError) + end + + it "returns a Rational if self has zero imaginary part" do + Complex(1,0).rationalize.should == Rational(1,1) + Complex(2<<63+5).rationalize.should == Rational(2<<63+5,1) + end + + it "sends #rationalize to the real part" do + real = mock_numeric('real') + real.should_receive(:rationalize).with(0.1).and_return(:result) + Complex(real, 0).rationalize(0.1).should == :result + end + + it "ignores a single argument" do + Complex(1,0).rationalize(0.1).should == Rational(1,1) + end + + it "raises ArgumentError when passed more than one argument" do + lambda { Complex(1,0).rationalize(0.1, 0.1) }.should raise_error(ArgumentError) + lambda { Complex(1,0).rationalize(0.1, 0.1, 2) }.should raise_error(ArgumentError) + end +end diff --git a/spec/ruby/core/complex/real_spec.rb b/spec/ruby/core/complex/real_spec.rb new file mode 100644 index 0000000000..1293e02d3c --- /dev/null +++ b/spec/ruby/core/complex/real_spec.rb @@ -0,0 +1,23 @@ +require File.expand_path('../../../shared/complex/real', __FILE__) + +describe "Complex#real" do + it_behaves_like(:complex_real, :real) +end + +describe "Complex#real?" do + it "returns false if there is an imaginary part" do + Complex(2,3).real?.should be_false + end + + it "returns false if there is not an imaginary part" do + Complex(2).real?.should be_false + end + + it "returns false if the real part is Infinity" do + Complex(infinity_value).real?.should be_false + end + + it "returns false if the real part is NaN" do + Complex(nan_value).real?.should be_false + end +end diff --git a/spec/ruby/core/complex/rect_spec.rb b/spec/ruby/core/complex/rect_spec.rb new file mode 100644 index 0000000000..cf2ff9e83b --- /dev/null +++ b/spec/ruby/core/complex/rect_spec.rb @@ -0,0 +1,9 @@ +require File.expand_path('../../../shared/complex/rect', __FILE__) + +describe "Complex#rect" do + it_behaves_like(:complex_rect, :rect) +end + +describe "Complex.rect" do + it_behaves_like(:complex_rect_class, :rect) +end diff --git a/spec/ruby/core/complex/rectangular_spec.rb b/spec/ruby/core/complex/rectangular_spec.rb new file mode 100644 index 0000000000..0eb29c3500 --- /dev/null +++ b/spec/ruby/core/complex/rectangular_spec.rb @@ -0,0 +1,9 @@ +require File.expand_path('../../../shared/complex/rect', __FILE__) + +describe "Complex#rectangular" do + it_behaves_like(:complex_rect, :rectangular) +end + +describe "Complex.rectangular" do + it_behaves_like(:complex_rect_class, :rectangular) +end diff --git a/spec/ruby/core/complex/to_f_spec.rb b/spec/ruby/core/complex/to_f_spec.rb new file mode 100644 index 0000000000..33342e61cc --- /dev/null +++ b/spec/ruby/core/complex/to_f_spec.rb @@ -0,0 +1,41 @@ +require File.expand_path('../../../spec_helper', __FILE__) + +describe "Complex#to_f" do + describe "when the imaginary part is Fixnum 0" do + it "returns the result of sending #to_f to the real part" do + real = mock_numeric('real') + real.should_receive(:to_f).and_return(:f) + Complex(real, 0).to_f.should == :f + end + end + + describe "when the imaginary part is Rational 0" do + it "returns the result of sending #to_f to the real part" do + real = mock_numeric('real') + real.should_receive(:to_f).and_return(:f) + Complex(real, Rational(0)).to_f.should == :f + end + end + + describe "when the imaginary part responds to #== 0 with true" do + it "returns the result of sending #to_f to the real part" do + real = mock_numeric('real') + real.should_receive(:to_f).and_return(:f) + imag = mock_numeric('imag') + imag.should_receive(:==).with(0).any_number_of_times.and_return(true) + Complex(real, imag).to_f.should == :f + end + end + + describe "when the imaginary part is non-zero" do + it "raises RangeError" do + lambda { Complex(0, 1).to_f }.should raise_error(RangeError) + end + end + + describe "when the imaginary part is Float 0.0" do + it "raises RangeError" do + lambda { Complex(0, 0.0).to_f }.should raise_error(RangeError) + end + end +end diff --git a/spec/ruby/core/complex/to_i_spec.rb b/spec/ruby/core/complex/to_i_spec.rb new file mode 100644 index 0000000000..ea8b199b2e --- /dev/null +++ b/spec/ruby/core/complex/to_i_spec.rb @@ -0,0 +1,41 @@ +require File.expand_path('../../../spec_helper', __FILE__) + +describe "Complex#to_i" do + describe "when the imaginary part is Fixnum 0" do + it "returns the result of sending #to_i to the real part" do + real = mock_numeric('real') + real.should_receive(:to_i).and_return(:i) + Complex(real, 0).to_i.should == :i + end + end + + describe "when the imaginary part is Rational 0" do + it "returns the result of sending #to_i to the real part" do + real = mock_numeric('real') + real.should_receive(:to_i).and_return(:i) + Complex(real, Rational(0)).to_i.should == :i + end + end + + describe "when the imaginary part responds to #== 0 with true" do + it "returns the result of sending #to_i to the real part" do + real = mock_numeric('real') + real.should_receive(:to_i).and_return(:i) + imag = mock_numeric('imag') + imag.should_receive(:==).with(0).any_number_of_times.and_return(true) + Complex(real, imag).to_i.should == :i + end + end + + describe "when the imaginary part is non-zero" do + it "raises RangeError" do + lambda { Complex(0, 1).to_i }.should raise_error(RangeError) + end + end + + describe "when the imaginary part is Float 0.0" do + it "raises RangeError" do + lambda { Complex(0, 0.0).to_i }.should raise_error(RangeError) + end + end +end diff --git a/spec/ruby/core/complex/to_r_spec.rb b/spec/ruby/core/complex/to_r_spec.rb new file mode 100644 index 0000000000..92fcdd3862 --- /dev/null +++ b/spec/ruby/core/complex/to_r_spec.rb @@ -0,0 +1,41 @@ +require File.expand_path('../../../spec_helper', __FILE__) + +describe "Complex#to_r" do + describe "when the imaginary part is Fixnum 0" do + it "returns the result of sending #to_r to the real part" do + real = mock_numeric('real') + real.should_receive(:to_r).and_return(:r) + Complex(real, 0).to_r.should == :r + end + end + + describe "when the imaginary part is Rational 0" do + it "returns the result of sending #to_r to the real part" do + real = mock_numeric('real') + real.should_receive(:to_r).and_return(:r) + Complex(real, Rational(0)).to_r.should == :r + end + end + + describe "when the imaginary part responds to #== 0 with true" do + it "returns the result of sending #to_r to the real part" do + real = mock_numeric('real') + real.should_receive(:to_r).and_return(:r) + imag = mock_numeric('imag') + imag.should_receive(:==).with(0).any_number_of_times.and_return(true) + Complex(real, imag).to_r.should == :r + end + end + + describe "when the imaginary part is non-zero" do + it "raises RangeError" do + lambda { Complex(0, 1).to_r }.should raise_error(RangeError) + end + end + + describe "when the imaginary part is Float 0.0" do + it "raises RangeError" do + lambda { Complex(0, 0.0).to_r }.should raise_error(RangeError) + end + end +end diff --git a/spec/ruby/core/complex/to_s_spec.rb b/spec/ruby/core/complex/to_s_spec.rb new file mode 100644 index 0000000000..c398bb000e --- /dev/null +++ b/spec/ruby/core/complex/to_s_spec.rb @@ -0,0 +1,5 @@ +require File.expand_path('../../../shared/complex/to_s', __FILE__) + +describe "Complex#to_s" do + it_behaves_like(:complex_to_s, :to_s) +end diff --git a/spec/ruby/core/complex/uminus_spec.rb b/spec/ruby/core/complex/uminus_spec.rb new file mode 100644 index 0000000000..1bf56e770b --- /dev/null +++ b/spec/ruby/core/complex/uminus_spec.rb @@ -0,0 +1,11 @@ +require File.expand_path('../../../spec_helper', __FILE__) + +describe "Complex#-@" do + it "sends #-@ to the real and imaginary parts and returns a Complex with the resulting respective parts" do + real = mock_numeric('real') + imag = mock_numeric('imag') + real.should_receive(:-@).and_return(-1) + imag.should_receive(:-@).and_return(-2) + Complex(real, imag).send(:-@).should == Complex(-1, -2) + end +end |