diff options
author | eregon <eregon@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2017-09-20 20:18:52 +0000 |
---|---|---|
committer | eregon <eregon@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2017-09-20 20:18:52 +0000 |
commit | 1d15d5f08032acf1b7bceacbb450d617ff6e0931 (patch) | |
tree | a3785a79899302bc149e4a6e72f624ac27dc1f10 /spec/ruby/core/numeric | |
parent | 75bfc6440d595bf339007f4fb280fd4d743e89c1 (diff) | |
download | ruby-1d15d5f08032acf1b7bceacbb450d617ff6e0931.tar.gz |
Move spec/rubyspec to spec/ruby for consistency
* Other ruby implementations use the spec/ruby directory.
[Misc #13792] [ruby-core:82287]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59979 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'spec/ruby/core/numeric')
47 files changed, 1559 insertions, 0 deletions
diff --git a/spec/ruby/core/numeric/abs2_spec.rb b/spec/ruby/core/numeric/abs2_spec.rb new file mode 100644 index 0000000000..f1094845fb --- /dev/null +++ b/spec/ruby/core/numeric/abs2_spec.rb @@ -0,0 +1,34 @@ +require File.expand_path('../../../spec_helper', __FILE__) + +describe "Numeric#abs2" do + before :each do + @numbers = [ + 0, + 0.0, + 1, + 20, + bignum_value, + 278202.292871, + 72829, + 3.333333333333, + 0.1, + infinity_value + ].map { |n| [-n, n] }.flatten + end + + it "returns the square of the absolute value of self" do + @numbers.each do |number| + number.abs2.should eql(number.abs ** 2) + end + end + + it "calls #* on self" do + number = mock_numeric('numeric') + number.should_receive(:*).and_return(:result) + number.abs2.should == :result + end + + it "returns NaN when self is NaN" do + nan_value.abs2.nan?.should be_true + end +end diff --git a/spec/ruby/core/numeric/abs_spec.rb b/spec/ruby/core/numeric/abs_spec.rb new file mode 100644 index 0000000000..4aa25359a2 --- /dev/null +++ b/spec/ruby/core/numeric/abs_spec.rb @@ -0,0 +1,5 @@ +require File.expand_path('../shared/abs', __FILE__) + +describe "Numeric#abs" do + it_behaves_like(:numeric_abs, :abs) +end diff --git a/spec/ruby/core/numeric/angle_spec.rb b/spec/ruby/core/numeric/angle_spec.rb new file mode 100644 index 0000000000..d7134168b3 --- /dev/null +++ b/spec/ruby/core/numeric/angle_spec.rb @@ -0,0 +1,6 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require File.expand_path('../../../shared/complex/numeric/arg', __FILE__) + +describe "Numeric#angle" do + it_behaves_like(:numeric_arg, :angle) +end diff --git a/spec/ruby/core/numeric/arg_spec.rb b/spec/ruby/core/numeric/arg_spec.rb new file mode 100644 index 0000000000..0729a29226 --- /dev/null +++ b/spec/ruby/core/numeric/arg_spec.rb @@ -0,0 +1,6 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require File.expand_path('../../../shared/complex/numeric/arg', __FILE__) + +describe "Numeric#arg" do + it_behaves_like(:numeric_arg, :arg) +end diff --git a/spec/ruby/core/numeric/ceil_spec.rb b/spec/ruby/core/numeric/ceil_spec.rb new file mode 100644 index 0000000000..6e7d8211fb --- /dev/null +++ b/spec/ruby/core/numeric/ceil_spec.rb @@ -0,0 +1,15 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require File.expand_path('../fixtures/classes', __FILE__) + +describe "Numeric#ceil" do + it "converts self to a Float (using #to_f) and returns the #ceil'ed result" do + o = mock_numeric("ceil") + o.should_receive(:to_f).and_return(1 + TOLERANCE) + o.ceil.should == 2 + + o2 = mock_numeric("ceil") + v = -1 - TOLERANCE + o2.should_receive(:to_f).and_return(v) + o2.ceil.should == -1 + end +end diff --git a/spec/ruby/core/numeric/coerce_spec.rb b/spec/ruby/core/numeric/coerce_spec.rb new file mode 100644 index 0000000000..9b6297d5da --- /dev/null +++ b/spec/ruby/core/numeric/coerce_spec.rb @@ -0,0 +1,76 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require File.expand_path('../fixtures/classes', __FILE__) + +describe "Numeric#coerce" do + before :each do + @obj = NumericSpecs::Subclass.new + @obj.should_receive(:to_f).any_number_of_times.and_return(10.5) + end + + it "returns [other, self] if self and other are instances of the same class" do + a = NumericSpecs::Subclass.new + b = NumericSpecs::Subclass.new + + a.coerce(b).should == [b, a] + end + + # I (emp) think that this behavior is actually a bug in MRI. It's here as documentation + # of the behavior until we find out if it's a bug. + quarantine! do + it "considers the presense of a metaclass when checking the class of the objects" do + a = NumericSpecs::Subclass.new + b = NumericSpecs::Subclass.new + + # inject a metaclass on a + class << a; true; end + + # watch it explode + lambda { a.coerce(b) }.should raise_error(TypeError) + end + end + + it "calls #to_f to convert other if self responds to #to_f" do + # Do not use NumericSpecs::Subclass here, because coerce checks the classes of the receiver + # and arguments before calling #to_f. + other = mock("numeric") + lambda { @obj.coerce(other) }.should raise_error(TypeError) + end + + it "returns [other.to_f, self.to_f] if self and other are instances of different classes" do + result = @obj.coerce(2.5) + result.should == [2.5, 10.5] + result.first.should be_kind_of(Float) + result.last.should be_kind_of(Float) + + result = @obj.coerce(3) + result.should == [3.0, 10.5] + result.first.should be_kind_of(Float) + result.last.should be_kind_of(Float) + + result = @obj.coerce("4.4") + result.should == [4.4, 10.5] + result.first.should be_kind_of(Float) + result.last.should be_kind_of(Float) + + result = @obj.coerce(bignum_value) + result.should == [bignum_value.to_f, 10.5] + result.first.should be_kind_of(Float) + result.last.should be_kind_of(Float) + end + + it "raises a TypeError when passed nil" do + lambda { @obj.coerce(nil) }.should raise_error(TypeError) + end + + it "raises a TypeError when passed a boolean" do + lambda { @obj.coerce(false) }.should raise_error(TypeError) + end + + it "raises a TypeError when passed a Symbol" do + lambda { @obj.coerce(:symbol) }.should raise_error(TypeError) + end + + it "raises an ArgumentError when passed a String" do + lambda { @obj.coerce("test") }.should raise_error(ArgumentError) + end +end diff --git a/spec/ruby/core/numeric/comparison_spec.rb b/spec/ruby/core/numeric/comparison_spec.rb new file mode 100644 index 0000000000..59676c01ad --- /dev/null +++ b/spec/ruby/core/numeric/comparison_spec.rb @@ -0,0 +1,48 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require File.expand_path('../fixtures/classes', __FILE__) + +describe "Numeric#<=>" do + before :each do + @obj = NumericSpecs::Subclass.new + end + + it "returns 0 if self equals other" do + (@obj <=> @obj).should == 0 + end + + it "returns nil if self does not equal other" do + (@obj <=> NumericSpecs::Subclass.new).should == nil + (@obj <=> 10).should == nil + (@obj <=> -3.5).should == nil + (@obj <=> bignum_value).should == nil + end + + describe "with subclasses of Numeric" do + before :each do + @a = NumericSpecs::Comparison.new + @b = NumericSpecs::Comparison.new + + ScratchPad.clear + end + + it "is called when instances are compared with #<" do + (@a < @b).should be_false + ScratchPad.recorded.should == :numeric_comparison + end + + it "is called when instances are compared with #<=" do + (@a <= @b).should be_false + ScratchPad.recorded.should == :numeric_comparison + end + + it "is called when instances are compared with #>" do + (@a > @b).should be_true + ScratchPad.recorded.should == :numeric_comparison + end + + it "is called when instances are compared with #>=" do + (@a >= @b).should be_true + ScratchPad.recorded.should == :numeric_comparison + end + end +end diff --git a/spec/ruby/core/numeric/conj_spec.rb b/spec/ruby/core/numeric/conj_spec.rb new file mode 100644 index 0000000000..8fa0fd9457 --- /dev/null +++ b/spec/ruby/core/numeric/conj_spec.rb @@ -0,0 +1,6 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require File.expand_path('../../../shared/complex/numeric/conj', __FILE__) + +describe "Numeric#conj" do + it_behaves_like(:numeric_conj, :conj) +end diff --git a/spec/ruby/core/numeric/conjugate_spec.rb b/spec/ruby/core/numeric/conjugate_spec.rb new file mode 100644 index 0000000000..f7e095514e --- /dev/null +++ b/spec/ruby/core/numeric/conjugate_spec.rb @@ -0,0 +1,6 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require File.expand_path('../../../shared/complex/numeric/conj', __FILE__) + +describe "Numeric#conjugate" do + it_behaves_like(:numeric_conj, :conjugate) +end diff --git a/spec/ruby/core/numeric/denominator_spec.rb b/spec/ruby/core/numeric/denominator_spec.rb new file mode 100644 index 0000000000..3ae4530c18 --- /dev/null +++ b/spec/ruby/core/numeric/denominator_spec.rb @@ -0,0 +1,24 @@ +require File.expand_path('../../../spec_helper', __FILE__) + +describe "Numeric#denominator" do + # The Numeric child classes override this method, so their behaviour is + # specified in the appropriate place + before :each do + @numbers = [ + 20, # Integer + 99999999**99, # Bignum + ] + end + + it "returns 1" do + @numbers.each {|number| number.denominator.should == 1} + end + + it "works with Numeric subclasses" do + rational = mock_numeric('rational') + rational.should_receive(:denominator).and_return(:denominator) + numeric = mock_numeric('numeric') + numeric.should_receive(:to_r).and_return(rational) + numeric.denominator.should == :denominator + end +end diff --git a/spec/ruby/core/numeric/div_spec.rb b/spec/ruby/core/numeric/div_spec.rb new file mode 100644 index 0000000000..f3f82c3ca4 --- /dev/null +++ b/spec/ruby/core/numeric/div_spec.rb @@ -0,0 +1,22 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require File.expand_path('../fixtures/classes', __FILE__) + +describe "Numeric#div" do + before :each do + @obj = NumericSpecs::Subclass.new + end + + it "calls self#/ with other, then returns the #floor'ed result" do + result = mock("Numeric#div result") + result.should_receive(:floor).and_return(12) + @obj.should_receive(:/).with(10).and_return(result) + + @obj.div(10).should == 12 + end + + it "raises ZeroDivisionError for 0" do + lambda { @obj.div(0) }.should raise_error(ZeroDivisionError) + lambda { @obj.div(0.0) }.should raise_error(ZeroDivisionError) + lambda { @obj.div(Complex(0,0)) }.should raise_error(ZeroDivisionError) + end +end diff --git a/spec/ruby/core/numeric/divmod_spec.rb b/spec/ruby/core/numeric/divmod_spec.rb new file mode 100644 index 0000000000..5de2d86c77 --- /dev/null +++ b/spec/ruby/core/numeric/divmod_spec.rb @@ -0,0 +1,15 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require File.expand_path('../fixtures/classes', __FILE__) + +describe "Numeric#divmod" do + before :each do + @obj = NumericSpecs::Subclass.new + end + + it "returns [quotient, modulus], with quotient being obtained as in Numeric#div then #floor and modulus being obtained by calling self#- with quotient * other" do + @obj.should_receive(:/).twice.with(10).and_return(13 - TOLERANCE, 13 - TOLERANCE) + @obj.should_receive(:-).with(120).and_return(3) + + @obj.divmod(10).should == [12, 3] + end +end diff --git a/spec/ruby/core/numeric/eql_spec.rb b/spec/ruby/core/numeric/eql_spec.rb new file mode 100644 index 0000000000..367a298a74 --- /dev/null +++ b/spec/ruby/core/numeric/eql_spec.rb @@ -0,0 +1,22 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require File.expand_path('../fixtures/classes', __FILE__) + +describe "Numeric#eql?" do + before :each do + @obj = NumericSpecs::Subclass.new + end + + it "returns false if self's and other's types don't match" do + @obj.should_not eql(1) + @obj.should_not eql(-1.5) + @obj.should_not eql(bignum_value) + @obj.should_not eql(:sym) + end + + it "returns the result of calling self#== with other when self's and other's types match" do + other = NumericSpecs::Subclass.new + @obj.should_receive(:==).with(other).and_return("result", nil) + @obj.should eql(other) + @obj.should_not eql(other) + end +end diff --git a/spec/ruby/core/numeric/fdiv_spec.rb b/spec/ruby/core/numeric/fdiv_spec.rb new file mode 100644 index 0000000000..4b27382012 --- /dev/null +++ b/spec/ruby/core/numeric/fdiv_spec.rb @@ -0,0 +1,32 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require File.expand_path('../shared/quo', __FILE__) + +describe "Numeric#fdiv" do + it "coerces self with #to_f" do + numeric = mock_numeric('numeric') + numeric.should_receive(:to_f).and_return(3.0) + numeric.fdiv(0.5).should == 6.0 + end + + it "coerces other with #to_f" do + numeric = mock_numeric('numeric') + numeric.should_receive(:to_f).and_return(3.0) + 6.fdiv(numeric).should == 2.0 + end + + it "performs floating-point division" do + 3.fdiv(2).should == 1.5 + end + + it "returns a Float" do + bignum_value.fdiv(Float::MAX).should be_an_instance_of(Float) + end + + it "returns Infinity if other is 0" do + 8121.92821.fdiv(0).infinite?.should == 1 + end + + it "returns NaN if other is NaN" do + 3334.fdiv(nan_value).nan?.should be_true + end +end diff --git a/spec/ruby/core/numeric/fixtures/classes.rb b/spec/ruby/core/numeric/fixtures/classes.rb new file mode 100644 index 0000000000..1505584889 --- /dev/null +++ b/spec/ruby/core/numeric/fixtures/classes.rb @@ -0,0 +1,17 @@ +module NumericSpecs + class Comparison < Numeric + # This method is used because we cannot define + # singleton methods on subclasses of Numeric, + # which is needed for a.should_receive to work. + def <=>(other) + ScratchPad.record :numeric_comparison + 1 + end + end + + class Subclass < Numeric + # Allow methods to be mocked + def singleton_method_added(val) + end + end +end diff --git a/spec/ruby/core/numeric/floor_spec.rb b/spec/ruby/core/numeric/floor_spec.rb new file mode 100644 index 0000000000..c68ed0423a --- /dev/null +++ b/spec/ruby/core/numeric/floor_spec.rb @@ -0,0 +1,14 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require File.expand_path('../fixtures/classes', __FILE__) + +describe "Numeric#floor" do + before :each do + @obj = NumericSpecs::Subclass.new + end + + it "converts self to a Float (using #to_f) and returns the #floor'ed result" do + @obj.should_receive(:to_f).and_return(2 - TOLERANCE, TOLERANCE - 2) + @obj.floor.should == 1 + @obj.floor.should == -2 + end +end diff --git a/spec/ruby/core/numeric/i_spec.rb b/spec/ruby/core/numeric/i_spec.rb new file mode 100644 index 0000000000..fae4fefe3d --- /dev/null +++ b/spec/ruby/core/numeric/i_spec.rb @@ -0,0 +1,15 @@ +require File.expand_path('../../../spec_helper', __FILE__) + +describe "Numeric#i" do + it "returns a Complex object" do + 34.i.should be_an_instance_of(Complex) + end + + it "sets the real part to 0" do + 7342.i.real.should == 0 + end + + it "sets the imaginary part to self" do + 62.81.i.imag.should == 62.81 + end +end diff --git a/spec/ruby/core/numeric/imag_spec.rb b/spec/ruby/core/numeric/imag_spec.rb new file mode 100644 index 0000000000..a80e42d265 --- /dev/null +++ b/spec/ruby/core/numeric/imag_spec.rb @@ -0,0 +1,6 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require File.expand_path('../../../shared/complex/numeric/imag', __FILE__) + +describe "Numeric#imag" do + it_behaves_like(:numeric_imag, :imag) +end diff --git a/spec/ruby/core/numeric/imaginary_spec.rb b/spec/ruby/core/numeric/imaginary_spec.rb new file mode 100644 index 0000000000..41226569b3 --- /dev/null +++ b/spec/ruby/core/numeric/imaginary_spec.rb @@ -0,0 +1,6 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require File.expand_path('../../../shared/complex/numeric/imag', __FILE__) + +describe "Numeric#imaginary" do + it_behaves_like(:numeric_imag, :imaginary) +end diff --git a/spec/ruby/core/numeric/integer_spec.rb b/spec/ruby/core/numeric/integer_spec.rb new file mode 100644 index 0000000000..acff8eb830 --- /dev/null +++ b/spec/ruby/core/numeric/integer_spec.rb @@ -0,0 +1,8 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require File.expand_path('../fixtures/classes', __FILE__) + +describe "Numeric#integer?" do + it "returns false" do + NumericSpecs::Subclass.new.integer?.should == false + end +end diff --git a/spec/ruby/core/numeric/magnitude_spec.rb b/spec/ruby/core/numeric/magnitude_spec.rb new file mode 100644 index 0000000000..947ee69730 --- /dev/null +++ b/spec/ruby/core/numeric/magnitude_spec.rb @@ -0,0 +1,5 @@ +require File.expand_path('../shared/abs', __FILE__) + +describe "Numeric#magnitude" do + it_behaves_like(:numeric_abs, :magnitude) +end diff --git a/spec/ruby/core/numeric/modulo_spec.rb b/spec/ruby/core/numeric/modulo_spec.rb new file mode 100644 index 0000000000..a6ea7a8f14 --- /dev/null +++ b/spec/ruby/core/numeric/modulo_spec.rb @@ -0,0 +1,24 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require File.expand_path('../fixtures/classes', __FILE__) + +describe :numeric_modulo_19, shared: true do + it "returns self - other * self.div(other)" do + s = mock_numeric('self') + o = mock_numeric('other') + n3 = mock_numeric('n3') + n4 = mock_numeric('n4') + n5 = mock_numeric('n5') + s.should_receive(:div).with(o).and_return(n3) + o.should_receive(:*).with(n3).and_return(n4) + s.should_receive(:-).with(n4).and_return(n5) + s.send(@method, o).should == n5 + end +end + +describe "Numeric#modulo" do + it_behaves_like :numeric_modulo_19, :modulo +end + +describe "Numeric#%" do + it_behaves_like :numeric_modulo_19, :% +end diff --git a/spec/ruby/core/numeric/negative_spec.rb b/spec/ruby/core/numeric/negative_spec.rb new file mode 100644 index 0000000000..27e5c65fe3 --- /dev/null +++ b/spec/ruby/core/numeric/negative_spec.rb @@ -0,0 +1,43 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require File.expand_path('../fixtures/classes', __FILE__) + +ruby_version_is "2.3" do + describe "Numeric#negative?" do + describe "on positive numbers" do + it "returns false" do + 1.negative?.should be_false + 0.1.negative?.should be_false + end + end + + describe "on zero" do + it "returns false" do + 0.negative?.should be_false + 0.0.negative?.should be_false + end + end + + describe "on negative numbers" do + it "returns true" do + -1.negative?.should be_true + -0.1.negative?.should be_true + end + end + end + + describe "Numeric#negative?" do + before(:each) do + @obj = NumericSpecs::Subclass.new + end + + it "returns true if self is less than 0" do + @obj.should_receive(:<).with(0).and_return(true) + @obj.negative?.should == true + end + + it "returns false if self is greater than 0" do + @obj.should_receive(:<).with(0).and_return(false) + @obj.negative?.should == false + end + end +end diff --git a/spec/ruby/core/numeric/nonzero_spec.rb b/spec/ruby/core/numeric/nonzero_spec.rb new file mode 100644 index 0000000000..55b7880d0e --- /dev/null +++ b/spec/ruby/core/numeric/nonzero_spec.rb @@ -0,0 +1,18 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require File.expand_path('../fixtures/classes', __FILE__) + +describe "Numeric#nonzero?" do + before :each do + @obj = NumericSpecs::Subclass.new + end + + it "returns self if self#zero? is false" do + @obj.should_receive(:zero?).and_return(false) + @obj.nonzero?.should == @obj + end + + it "returns nil if self#zero? is true" do + @obj.should_receive(:zero?).and_return(true) + @obj.nonzero?.should == nil + end +end diff --git a/spec/ruby/core/numeric/numerator_spec.rb b/spec/ruby/core/numeric/numerator_spec.rb new file mode 100644 index 0000000000..4a20f8792a --- /dev/null +++ b/spec/ruby/core/numeric/numerator_spec.rb @@ -0,0 +1,33 @@ +require File.expand_path('../../../spec_helper', __FILE__) + +describe "Numeric#numerator" do + before :all do + @numbers = [ + 0, + 29871, + 99999999999999**99, + -72628191273, + 29282.2827, + -2927.00091, + 0.0, + 12.0, + Float::MAX, + ] + end + + # This isn't entirely true, as NaN.numerator works, whereas + # Rational(NaN) raises an exception, but we test this in Float#numerator + it "converts self to a Rational object then returns its numerator" do + @numbers.each do |number| + number.numerator.should == Rational(number).numerator + end + end + + it "works with Numeric subclasses" do + rational = mock_numeric('rational') + rational.should_receive(:numerator).and_return(:numerator) + numeric = mock_numeric('numeric') + numeric.should_receive(:to_r).and_return(rational) + numeric.numerator.should == :numerator + end +end diff --git a/spec/ruby/core/numeric/numeric_spec.rb b/spec/ruby/core/numeric/numeric_spec.rb new file mode 100644 index 0000000000..9429ab55a0 --- /dev/null +++ b/spec/ruby/core/numeric/numeric_spec.rb @@ -0,0 +1,7 @@ +require File.expand_path('../../../spec_helper', __FILE__) + +describe "Numeric" do + it "includes Comparable" do + Numeric.include?(Comparable).should == true + end +end diff --git a/spec/ruby/core/numeric/phase_spec.rb b/spec/ruby/core/numeric/phase_spec.rb new file mode 100644 index 0000000000..7c408db83b --- /dev/null +++ b/spec/ruby/core/numeric/phase_spec.rb @@ -0,0 +1,6 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require File.expand_path('../../../shared/complex/numeric/arg', __FILE__) + +describe "Numeric#phase" do + it_behaves_like(:numeric_arg, :phase) +end diff --git a/spec/ruby/core/numeric/polar_spec.rb b/spec/ruby/core/numeric/polar_spec.rb new file mode 100644 index 0000000000..5492483215 --- /dev/null +++ b/spec/ruby/core/numeric/polar_spec.rb @@ -0,0 +1,6 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require File.expand_path('../../../shared/complex/numeric/polar', __FILE__) + +describe "Numeric#polar" do + it_behaves_like(:numeric_polar, :polar) +end diff --git a/spec/ruby/core/numeric/positive_spec.rb b/spec/ruby/core/numeric/positive_spec.rb new file mode 100644 index 0000000000..516de7db89 --- /dev/null +++ b/spec/ruby/core/numeric/positive_spec.rb @@ -0,0 +1,43 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require File.expand_path('../fixtures/classes', __FILE__) + +ruby_version_is "2.3" do + describe "Numeric#positive?" do + describe "on positive numbers" do + it "returns true" do + 1.positive?.should be_true + 0.1.positive?.should be_true + end + end + + describe "on zero" do + it "returns false" do + 0.positive?.should be_false + 0.0.positive?.should be_false + end + end + + describe "on negative numbers" do + it "returns false" do + -1.positive?.should be_false + -0.1.positive?.should be_false + end + end + end + + describe "Numeric#positive?" do + before(:each) do + @obj = NumericSpecs::Subclass.new + end + + it "returns true if self is greater than 0" do + @obj.should_receive(:>).with(0).and_return(true) + @obj.positive?.should == true + end + + it "returns false if self is less than 0" do + @obj.should_receive(:>).with(0).and_return(false) + @obj.positive?.should == false + end + end +end diff --git a/spec/ruby/core/numeric/quo_spec.rb b/spec/ruby/core/numeric/quo_spec.rb new file mode 100644 index 0000000000..24c752aac1 --- /dev/null +++ b/spec/ruby/core/numeric/quo_spec.rb @@ -0,0 +1,55 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require File.expand_path('../fixtures/classes', __FILE__) +require File.expand_path('../shared/quo', __FILE__) + +describe "Numeric#quo" do + it "returns the result of self divided by the given Integer as a Rational" do + 5.quo(2).should eql(Rational(5,2)) + end + + it "returns the result of self divided by the given Float as a Float" do + 2.quo(2.5).should eql(0.8) + end + + it "returns the result of self divided by the given Bignum as a Float" do + 45.quo(bignum_value).should be_close(1.04773789668636e-08, TOLERANCE) + end + + it "raises a ZeroDivisionError when the given Integer is 0" do + lambda { 0.quo(0) }.should raise_error(ZeroDivisionError) + lambda { 10.quo(0) }.should raise_error(ZeroDivisionError) + lambda { -10.quo(0) }.should raise_error(ZeroDivisionError) + lambda { bignum_value.quo(0) }.should raise_error(ZeroDivisionError) + lambda { -bignum_value.quo(0) }.should raise_error(ZeroDivisionError) + end + + it "calls #to_r to convert the object to a Rational" do + obj = NumericSpecs::Subclass.new + obj.should_receive(:to_r).and_return(Rational(1)) + + obj.quo(19).should == Rational(1, 19) + end + + it "raises a TypeError of #to_r does not return a Rational" do + obj = NumericSpecs::Subclass.new + obj.should_receive(:to_r).and_return(1) + + lambda { obj.quo(19) }.should raise_error(TypeError) + end + + it "raises a TypeError when given a non-Integer" do + lambda { + (obj = mock('x')).should_not_receive(:to_int) + 13.quo(obj) + }.should raise_error(TypeError) + lambda { 13.quo("10") }.should raise_error(TypeError) + lambda { 13.quo(:symbol) }.should raise_error(TypeError) + end + + it "returns the result of calling self#/ with other" do + obj = NumericSpecs::Subclass.new + obj.should_receive(:to_r).and_return(19.quo(20)) + + obj.quo(19).should == 1.quo(20) + end +end diff --git a/spec/ruby/core/numeric/real_spec.rb b/spec/ruby/core/numeric/real_spec.rb new file mode 100644 index 0000000000..3e34410155 --- /dev/null +++ b/spec/ruby/core/numeric/real_spec.rb @@ -0,0 +1,13 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require File.expand_path('../../../shared/complex/numeric/real', __FILE__) +require File.expand_path('../fixtures/classes', __FILE__) + +describe "Numeric#real" do + it_behaves_like(:numeric_real, :real) +end + +describe "Numeric#real?" do + it "returns true" do + NumericSpecs::Subclass.new.real?.should == true + end +end diff --git a/spec/ruby/core/numeric/rect_spec.rb b/spec/ruby/core/numeric/rect_spec.rb new file mode 100644 index 0000000000..88d5ee3881 --- /dev/null +++ b/spec/ruby/core/numeric/rect_spec.rb @@ -0,0 +1,6 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require File.expand_path('../shared/rect', __FILE__) + +describe "Numeric#rect" do + it_behaves_like(:numeric_rect, :rect) +end diff --git a/spec/ruby/core/numeric/rectangular_spec.rb b/spec/ruby/core/numeric/rectangular_spec.rb new file mode 100644 index 0000000000..b34100ca74 --- /dev/null +++ b/spec/ruby/core/numeric/rectangular_spec.rb @@ -0,0 +1,6 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require File.expand_path('../shared/rect', __FILE__) + +describe "Numeric#rectangular" do + it_behaves_like(:numeric_rect, :rectangular) +end diff --git a/spec/ruby/core/numeric/remainder_spec.rb b/spec/ruby/core/numeric/remainder_spec.rb new file mode 100644 index 0000000000..6d26d39669 --- /dev/null +++ b/spec/ruby/core/numeric/remainder_spec.rb @@ -0,0 +1,67 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require File.expand_path('../fixtures/classes', __FILE__) + +describe "Numeric#remainder" do + before :each do + @obj = NumericSpecs::Subclass.new + @result = mock("Numeric#% result") + @other = mock("Passed Object") + end + + it "returns the result of calling self#% with other if self is 0" do + @obj.should_receive(:%).with(@other).and_return(@result) + @result.should_receive(:==).with(0).and_return(true) + + @obj.remainder(@other).should equal(@result) + end + + it "returns the result of calling self#% with other if self and other are greater than 0" do + @obj.should_receive(:%).with(@other).and_return(@result) + @result.should_receive(:==).with(0).and_return(false) + + @obj.should_receive(:<).with(0).and_return(false) + + @obj.should_receive(:>).with(0).and_return(true) + @other.should_receive(:<).with(0).and_return(false) + + @obj.remainder(@other).should equal(@result) + end + + it "returns the result of calling self#% with other if self and other are less than 0" do + @obj.should_receive(:%).with(@other).and_return(@result) + @result.should_receive(:==).with(0).and_return(false) + + @obj.should_receive(:<).with(0).and_return(true) + @other.should_receive(:>).with(0).and_return(false) + + @obj.should_receive(:>).with(0).and_return(false) + + @obj.remainder(@other).should equal(@result) + end + + it "returns the result of calling self#% with other - other if self is greater than 0 and other is less than 0" do + @obj.should_receive(:%).with(@other).and_return(@result) + @result.should_receive(:==).with(0).and_return(false) + + @obj.should_receive(:<).with(0).and_return(false) + + @obj.should_receive(:>).with(0).and_return(true) + @other.should_receive(:<).with(0).and_return(true) + + @result.should_receive(:-).with(@other).and_return(:result) + + @obj.remainder(@other).should == :result + end + + it "returns the result of calling self#% with other - other if self is less than 0 and other is greater than 0" do + @obj.should_receive(:%).with(@other).and_return(@result) + @result.should_receive(:==).with(0).and_return(false) + + @obj.should_receive(:<).with(0).and_return(true) + @other.should_receive(:>).with(0).and_return(true) + + @result.should_receive(:-).with(@other).and_return(:result) + + @obj.remainder(@other).should == :result + end +end diff --git a/spec/ruby/core/numeric/round_spec.rb b/spec/ruby/core/numeric/round_spec.rb new file mode 100644 index 0000000000..6e79decfa0 --- /dev/null +++ b/spec/ruby/core/numeric/round_spec.rb @@ -0,0 +1,14 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require File.expand_path('../fixtures/classes', __FILE__) + +describe "Numeric#round" do + before :each do + @obj = NumericSpecs::Subclass.new + end + + it "converts self to a Float (using #to_f) and returns the #round'ed result" do + @obj.should_receive(:to_f).and_return(2 - TOLERANCE, TOLERANCE - 2) + @obj.round.should == 2 + @obj.round.should == -2 + end +end diff --git a/spec/ruby/core/numeric/shared/abs.rb b/spec/ruby/core/numeric/shared/abs.rb new file mode 100644 index 0000000000..406c9f3981 --- /dev/null +++ b/spec/ruby/core/numeric/shared/abs.rb @@ -0,0 +1,19 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require File.expand_path('../../fixtures/classes', __FILE__) + +describe :numeric_abs, shared: true do + before :each do + @obj = NumericSpecs::Subclass.new + end + + it "returns self when self is greater than 0" do + @obj.should_receive(:<).with(0).and_return(false) + @obj.send(@method).should == @obj + end + + it "returns self\#@- when self is less than 0" do + @obj.should_receive(:<).with(0).and_return(true) + @obj.should_receive(:-@).and_return(:absolute_value) + @obj.send(@method).should == :absolute_value + end +end diff --git a/spec/ruby/core/numeric/shared/quo.rb b/spec/ruby/core/numeric/shared/quo.rb new file mode 100644 index 0000000000..2392636fe7 --- /dev/null +++ b/spec/ruby/core/numeric/shared/quo.rb @@ -0,0 +1,7 @@ +describe :numeric_quo_18, shared: true do + it "returns the result of calling self#/ with other" do + obj = mock_numeric('numeric') + obj.should_receive(:/).with(19).and_return(:result) + obj.send(@method, 19).should == :result + end +end diff --git a/spec/ruby/core/numeric/shared/rect.rb b/spec/ruby/core/numeric/shared/rect.rb new file mode 100644 index 0000000000..cda5fede7f --- /dev/null +++ b/spec/ruby/core/numeric/shared/rect.rb @@ -0,0 +1,48 @@ +require File.expand_path('../../../../spec_helper', __FILE__) + +describe :numeric_rect, shared: true do + before :each do + @numbers = [ + 20, # Integer + 398.72, # Float + Rational(3, 4), # Rational + 99999999**99, # Bignum + infinity_value, + nan_value + ] + 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 self as the first element" do + @numbers.each do |number| + if Float === number and number.nan? + number.send(@method).first.nan?.should be_true + else + number.send(@method).first.should == number + end + end + end + + it "returns 0 as the last element" do + @numbers.each do |number| + number.send(@method).last.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/numeric/shared/step.rb b/spec/ruby/core/numeric/shared/step.rb new file mode 100644 index 0000000000..3072830534 --- /dev/null +++ b/spec/ruby/core/numeric/shared/step.rb @@ -0,0 +1,425 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require File.expand_path('../../fixtures/classes', __FILE__) + +# Describes Numeric#step shared specs between different argument styles. +# To be able to do it, the @step_args var must contain a Proc that transforms +# the step call arguments passed as positional arguments to the style of +# arguments pretended to test. +describe :numeric_step, :shared => true do + before :each do + ScratchPad.record [] + @prc = lambda { |x| ScratchPad << x } + end + + it "defaults to step = 1" do + 1.send(@method, *@step_args.call(5), &@prc) + ScratchPad.recorded.should eql [1, 2, 3, 4, 5] + end + + describe "when self, stop and step are Fixnums" do + it "yields only Fixnums" do + 1.send(@method, *@step_args.call(5, 1)) { |x| x.should be_an_instance_of(Fixnum) } + end + + describe "with a positive step" do + it "yields while increasing self by step until stop is reached" do + 1.send(@method, *@step_args.call(5, 1), &@prc) + ScratchPad.recorded.should eql [1, 2, 3, 4, 5] + end + + it "yields once when self equals stop" do + 1.send(@method, *@step_args.call(1, 1), &@prc) + ScratchPad.recorded.should eql [1] + end + + it "does not yield when self is greater than stop" do + 2.send(@method, *@step_args.call(1, 1), &@prc) + ScratchPad.recorded.should eql [] + end + end + + describe "with a negative step" do + it "yields while decreasing self by step until stop is reached" do + 5.send(@method, *@step_args.call(1, -1), &@prc) + ScratchPad.recorded.should eql [5, 4, 3, 2, 1] + end + + it "yields once when self equals stop" do + 5.send(@method, *@step_args.call(5, -1), &@prc) + ScratchPad.recorded.should eql [5] + end + + it "does not yield when self is less than stop" do + 1.send(@method, *@step_args.call(5, -1), &@prc) + ScratchPad.recorded.should == [] + end + end + end + + describe "when at least one of self, stop or step is a Float" do + it "yields Floats even if only self is a Float" do + 1.5.send(@method, *@step_args.call(5, 1)) { |x| x.should be_an_instance_of(Float) } + end + + it "yields Floats even if only stop is a Float" do + 1.send(@method, *@step_args.call(5.0, 1)) { |x| x.should be_an_instance_of(Float) } + end + + it "yields Floats even if only step is a Float" do + 1.send(@method, *@step_args.call(5, 1.0)) { |x| x.should be_an_instance_of(Float) } + end + + describe "with a positive step" do + it "yields while increasing self by step while < stop" do + 1.5.send(@method, *@step_args.call(5, 1), &@prc) + ScratchPad.recorded.should eql [1.5, 2.5, 3.5, 4.5] + end + + it "yields once when self equals stop" do + 1.5.send(@method, *@step_args.call(1.5, 1), &@prc) + ScratchPad.recorded.should eql [1.5] + end + + it "does not yield when self is greater than stop" do + 2.5.send(@method, *@step_args.call(1.5, 1), &@prc) + ScratchPad.recorded.should == [] + end + + it "is careful about not yielding a value greater than limit" do + # As 9*1.3+1.0 == 12.700000000000001 > 12.7, we test: + 1.0.send(@method, *@step_args.call(12.7, 1.3), &@prc) + ScratchPad.recorded.should eql [1.0, 2.3, 3.6, 4.9, 6.2, 7.5, 8.8, 10.1, 11.4, 12.7] + end + end + + describe "with a negative step" do + it "yields while decreasing self by step while self > stop" do + 5.send(@method, *@step_args.call(1.5, -1), &@prc) + ScratchPad.recorded.should eql [5.0, 4.0, 3.0, 2.0] + end + + it "yields once when self equals stop" do + 1.5.send(@method, *@step_args.call(1.5, -1), &@prc) + ScratchPad.recorded.should eql [1.5] + end + + it "does not yield when self is less than stop" do + 1.send(@method, *@step_args.call(5, -1.5), &@prc) + ScratchPad.recorded.should == [] + end + + it "is careful about not yielding a value smaller than limit" do + # As -9*1.3-1.0 == -12.700000000000001 < -12.7, we test: + -1.0.send(@method, *@step_args.call(-12.7, -1.3), &@prc) + ScratchPad.recorded.should eql [-1.0, -2.3, -3.6, -4.9, -6.2, -7.5, -8.8, -10.1, -11.4, -12.7] + end + end + + describe "with a positive Infinity step" do + it "yields once if self < stop" do + 42.send(@method, *@step_args.call(100, infinity_value), &@prc) + ScratchPad.recorded.should eql [42.0] + end + + it "yields once when stop is Infinity" do + 42.send(@method, *@step_args.call(infinity_value, infinity_value), &@prc) + ScratchPad.recorded.should eql [42.0] + end + + it "yields once when self equals stop" do + 42.send(@method, *@step_args.call(42, infinity_value), &@prc) + ScratchPad.recorded.should eql [42.0] + end + + it "yields once when self and stop are Infinity" do + (infinity_value).send(@method, *@step_args.call(infinity_value, infinity_value), &@prc) + ScratchPad.recorded.should == [infinity_value] + end + + it "does not yield when self > stop" do + 100.send(@method, *@step_args.call(42, infinity_value), &@prc) + ScratchPad.recorded.should == [] + end + + it "does not yield when stop is -Infinity" do + 42.send(@method, *@step_args.call(-infinity_value, infinity_value), &@prc) + ScratchPad.recorded.should == [] + end + end + + describe "with a negative Infinity step" do + it "yields once if self > stop" do + 42.send(@method, *@step_args.call(6, -infinity_value), &@prc) + ScratchPad.recorded.should eql [42.0] + end + + it "yields once if stop is -Infinity" do + 42.send(@method, *@step_args.call(-infinity_value, -infinity_value), &@prc) + ScratchPad.recorded.should eql [42.0] + end + + it "yields once when self equals stop" do + 42.send(@method, *@step_args.call(42, -infinity_value), &@prc) + ScratchPad.recorded.should eql [42.0] + end + + it "yields once when self and stop are Infinity" do + (infinity_value).send(@method, *@step_args.call(infinity_value, -infinity_value), &@prc) + ScratchPad.recorded.should == [infinity_value] + end + + it "does not yield when self > stop" do + 42.send(@method, *@step_args.call(100, -infinity_value), &@prc) + ScratchPad.recorded.should == [] + end + + it "does not yield when stop is Infinity" do + 42.send(@method, *@step_args.call(infinity_value, -infinity_value), &@prc) + ScratchPad.recorded.should == [] + end + end + + describe "with a Infinity stop and a positive step" do + it "does not yield when self is infinity" do + (infinity_value).send(@method, *@step_args.call(infinity_value, 1), &@prc) + ScratchPad.recorded.should == [] + end + end + + describe "with a Infinity stop and a negative step" do + it "does not yield when self is negative infinity" do + (-infinity_value).send(@method, *@step_args.call(infinity_value, -1), &@prc) + ScratchPad.recorded.should == [] + end + + it "does not yield when self is positive infinity" do + infinity_value.send(@method, *@step_args.call(infinity_value, -1), &@prc) + ScratchPad.recorded.should == [] + end + end + + describe "with a negative Infinity stop and a positive step" do + it "does not yield when self is negative infinity" do + (-infinity_value).send(@method, *@step_args.call(-infinity_value, 1), &@prc) + ScratchPad.recorded.should == [] + end + end + + describe "with a negative Infinity stop and a negative step" do + it "does not yield when self is negative infinity" do + (-infinity_value).send(@method, *@step_args.call(-infinity_value, -1), &@prc) + ScratchPad.recorded.should == [] + end + end + + end + + describe "when step is a String" do + error = nil + ruby_version_is ""..."2.4" do + error = ArgumentError + end + ruby_version_is "2.4"..."2.5" do + error = TypeError + end + ruby_version_is "2.5" do + error = ArgumentError + end + + describe "with self and stop as Fixnums" do + it "raises an #{error} when step is a numeric representation" do + lambda { 1.send(@method, *@step_args.call(5, "1")) {} }.should raise_error(error) + lambda { 1.send(@method, *@step_args.call(5, "0.1")) {} }.should raise_error(error) + lambda { 1.send(@method, *@step_args.call(5, "1/3")) {} }.should raise_error(error) + end + it "raises an #{error} with step as an alphanumeric string" do + lambda { 1.send(@method, *@step_args.call(5, "foo")) {} }.should raise_error(error) + end + end + + describe "with self and stop as Floats" do + it "raises an #{error} when step is a numeric representation" do + lambda { 1.1.send(@method, *@step_args.call(5.1, "1")) {} }.should raise_error(error) + lambda { 1.1.send(@method, *@step_args.call(5.1, "0.1")) {} }.should raise_error(error) + lambda { 1.1.send(@method, *@step_args.call(5.1, "1/3")) {} }.should raise_error(error) + end + it "raises an #{error} with step as an alphanumeric string" do + lambda { 1.1.send(@method, *@step_args.call(5.1, "foo")) {} }.should raise_error(error) + end + end + end + + it "does not rescue ArgumentError exceptions" do + lambda { 1.send(@method, *@step_args.call(2)) { raise ArgumentError, "" }}.should raise_error(ArgumentError) + end + + it "does not rescue TypeError exceptions" do + lambda { 1.send(@method, *@step_args.call(2)) { raise TypeError, "" } }.should raise_error(TypeError) + end + + describe "when no block is given" do + it "returns an Enumerator when step is 0" do + 1.send(@method, *@step_args.call(2, 0)).should be_an_instance_of(Enumerator) + end + + it "returns an Enumerator when not passed a block and self > stop" do + 1.send(@method, *@step_args.call(0, 2)).should be_an_instance_of(Enumerator) + end + + it "returns an Enumerator when not passed a block and self < stop" do + 1.send(@method, *@step_args.call(2, 3)).should be_an_instance_of(Enumerator) + end + + it "returns an Enumerator that uses the given step" do + 0.send(@method, *@step_args.call(5, 2)).to_a.should eql [0, 2, 4] + end + + describe "when step is a String" do + describe "with self and stop as Fixnums" do + it "returns an Enumerator" do + 1.send(@method, *@step_args.call(5, "foo")).should be_an_instance_of(Enumerator) + end + end + + describe "with self and stop as Floats" do + it "returns an Enumerator" do + 1.1.send(@method, *@step_args.call(5.1, "foo")).should be_an_instance_of(Enumerator) + end + end + end + + describe "returned Enumerator" do + describe "size" do + describe "when step is a String" do + error = nil + ruby_version_is ""..."2.4" do + error = ArgumentError + end + ruby_version_is "2.4"..."2.5" do + error = TypeError + end + ruby_version_is "2.5" do + error = ArgumentError + end + + describe "with self and stop as Fixnums" do + it "raises an #{error} when step is a numeric representation" do + lambda { 1.send(@method, *@step_args.call(5, "1")).size }.should raise_error(error) + lambda { 1.send(@method, *@step_args.call(5, "0.1")).size }.should raise_error(error) + lambda { 1.send(@method, *@step_args.call(5, "1/3")).size }.should raise_error(error) + end + it "raises an #{error} with step as an alphanumeric string" do + lambda { 1.send(@method, *@step_args.call(5, "foo")).size }.should raise_error(error) + end + end + + describe "with self and stop as Floats" do + it "raises an #{error} when step is a numeric representation" do + lambda { 1.1.send(@method, *@step_args.call(5.1, "1")).size }.should raise_error(error) + lambda { 1.1.send(@method, *@step_args.call(5.1, "0.1")).size }.should raise_error(error) + lambda { 1.1.send(@method, *@step_args.call(5.1, "1/3")).size }.should raise_error(error) + end + it "raises an #{error} with step as an alphanumeric string" do + lambda { 1.1.send(@method, *@step_args.call(5.1, "foo")).size }.should raise_error(error) + end + end + end + + describe "when self, stop and step are Fixnums and step is positive" do + it "returns the difference between self and stop divided by the number of steps" do + 5.send(@method, *@step_args.call(10, 11)).size.should == 1 + 5.send(@method, *@step_args.call(10, 6)).size.should == 1 + 5.send(@method, *@step_args.call(10, 5)).size.should == 2 + 5.send(@method, *@step_args.call(10, 4)).size.should == 2 + 5.send(@method, *@step_args.call(10, 2)).size.should == 3 + 5.send(@method, *@step_args.call(10, 1)).size.should == 6 + 5.send(@method, *@step_args.call(10)).size.should == 6 + 10.send(@method, *@step_args.call(10, 1)).size.should == 1 + end + + it "returns 0 if value > limit" do + 11.send(@method, *@step_args.call(10, 1)).size.should == 0 + end + end + + describe "when self, stop and step are Fixnums and step is negative" do + it "returns the difference between self and stop divided by the number of steps" do + 10.send(@method, *@step_args.call(5, -11)).size.should == 1 + 10.send(@method, *@step_args.call(5, -6)).size.should == 1 + 10.send(@method, *@step_args.call(5, -5)).size.should == 2 + 10.send(@method, *@step_args.call(5, -4)).size.should == 2 + 10.send(@method, *@step_args.call(5, -2)).size.should == 3 + 10.send(@method, *@step_args.call(5, -1)).size.should == 6 + 10.send(@method, *@step_args.call(10, -1)).size.should == 1 + end + + it "returns 0 if value < limit" do + 10.send(@method, *@step_args.call(11, -1)).size.should == 0 + end + end + + describe "when self, stop or step is a Float" do + describe "and step is positive" do + it "returns the difference between self and stop divided by the number of steps" do + 5.send(@method, *@step_args.call(10, 11.0)).size.should == 1 + 5.send(@method, *@step_args.call(10, 6.0)).size.should == 1 + 5.send(@method, *@step_args.call(10, 5.0)).size.should == 2 + 5.send(@method, *@step_args.call(10, 4.0)).size.should == 2 + 5.send(@method, *@step_args.call(10, 2.0)).size.should == 3 + 5.send(@method, *@step_args.call(10, 0.5)).size.should == 11 + 5.send(@method, *@step_args.call(10, 1.0)).size.should == 6 + 5.send(@method, *@step_args.call(10.5)).size.should == 6 + 10.send(@method, *@step_args.call(10, 1.0)).size.should == 1 + end + + it "returns 0 if value > limit" do + 10.send(@method, *@step_args.call(5.5)).size.should == 0 + 11.send(@method, *@step_args.call(10, 1.0)).size.should == 0 + 11.send(@method, *@step_args.call(10, 1.5)).size.should == 0 + 10.send(@method, *@step_args.call(5, infinity_value)).size.should == 0 + end + + it "returns 1 if step is infinity_value" do + 5.send(@method, *@step_args.call(10, infinity_value)).size.should == 1 + end + end + + describe "and step is negative" do + it "returns the difference between self and stop divided by the number of steps" do + 10.send(@method, *@step_args.call(5, -11.0)).size.should == 1 + 10.send(@method, *@step_args.call(5, -6.0)).size.should == 1 + 10.send(@method, *@step_args.call(5, -5.0)).size.should == 2 + 10.send(@method, *@step_args.call(5, -4.0)).size.should == 2 + 10.send(@method, *@step_args.call(5, -2.0)).size.should == 3 + 10.send(@method, *@step_args.call(5, -0.5)).size.should == 11 + 10.send(@method, *@step_args.call(5, -1.0)).size.should == 6 + 10.send(@method, *@step_args.call(10, -1.0)).size.should == 1 + end + + it "returns 0 if value < limit" do + 10.send(@method, *@step_args.call(11, -1.0)).size.should == 0 + 10.send(@method, *@step_args.call(11, -1.5)).size.should == 0 + 5.send(@method, *@step_args.call(10, -infinity_value)).size.should == 0 + end + + it "returns 1 if step is infinity_value" do + 10.send(@method, *@step_args.call(5, -infinity_value)).size.should == 1 + end + end + end + + describe "when stop is not passed" do + it "returns infinity_value" do + 1.send(@method, *@step_args.call()).size.should == infinity_value + end + end + + describe "when stop is nil" do + it "returns infinity_value" do + 1.send(@method, *@step_args.call(nil, 5)).size.should == infinity_value + end + end + end + end + end +end diff --git a/spec/ruby/core/numeric/singleton_method_added_spec.rb b/spec/ruby/core/numeric/singleton_method_added_spec.rb new file mode 100644 index 0000000000..a650d286f2 --- /dev/null +++ b/spec/ruby/core/numeric/singleton_method_added_spec.rb @@ -0,0 +1,41 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require File.expand_path('../fixtures/classes', __FILE__) + +describe "Numeric#singleton_method_added" do + before :all do + class ::NumericSpecs::Subclass + # We want restore default Numeric behaviour for this particular test + remove_method :singleton_method_added + end + end + + after :all do + class ::NumericSpecs::Subclass + # Allow mocking methods again + def singleton_method_added(val) + end + end + end + + it "raises a TypeError when trying to define a singleton method on a Numeric" do + lambda do + a = NumericSpecs::Subclass.new + def a.test; end + end.should raise_error(TypeError) + + lambda do + a = 1 + def a.test; end + end.should raise_error(TypeError) + + lambda do + a = 1.5 + def a.test; end + end.should raise_error(TypeError) + + lambda do + a = bignum_value + def a.test; end + end.should raise_error(TypeError) + end +end diff --git a/spec/ruby/core/numeric/step_spec.rb b/spec/ruby/core/numeric/step_spec.rb new file mode 100644 index 0000000000..256c150d4e --- /dev/null +++ b/spec/ruby/core/numeric/step_spec.rb @@ -0,0 +1,163 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require File.expand_path('../fixtures/classes', __FILE__) +require File.expand_path('../shared/step', __FILE__) + +describe "Numeric#step" do + + describe 'with positional args' do + it "raises an ArgumentError when step is 0" do + lambda { 1.step(5, 0) {} }.should raise_error(ArgumentError) + end + + it "raises an ArgumentError when step is 0.0" do + lambda { 1.step(2, 0.0) {} }.should raise_error(ArgumentError) + end + + before :all do + # This lambda definition limits to return the arguments it receives. + # It's needed to test numeric_step behaviour with positional arguments. + @step_args = ->(*args) { args } + end + + it_behaves_like :numeric_step, :step + + describe "when no block is given" do + it "returns an Enumerator when step is 0" do + 1.step(5, 0).should be_an_instance_of(Enumerator) + end + + it "returns an Enumerator when step is 0.0" do + 1.step(2, 0.0).should be_an_instance_of(Enumerator) + end + + describe "returned Enumerator" do + describe "size" do + it "raises an ArgumentError when step is 0" do + enum = 1.step(5, 0) + lambda { enum.size }.should raise_error(ArgumentError) + end + + it "raises an ArgumentError when step is 0.0" do + enum = 1.step(2, 0.0) + lambda { enum.size }.should raise_error(ArgumentError) + end + end + end + end + + end + + describe 'with keyword arguments' do + it "doesn't raise an error when step is 0" do + lambda { 1.step(to: 5, by: 0) { break } }.should_not raise_error + end + + it "doesn't raise an error when step is 0.0" do + lambda { 1.step(to: 2, by: 0.0) { break } }.should_not raise_error + end + + it "should loop over self when step is 0 or 0.0" do + 1.step(to: 2, by: 0.0).take(5).should eql [1.0, 1.0, 1.0, 1.0, 1.0] + 1.step(to: 2, by: 0).take(5).should eql [1, 1, 1, 1, 1] + 1.1.step(to: 2, by: 0).take(5).should eql [1.1, 1.1, 1.1, 1.1, 1.1] + end + + describe "when no block is given" do + describe "returned Enumerator" do + describe "size" do + it "should return infinity_value when limit is nil" do + 1.step(by: 42).size.should == infinity_value + end + + it "should return infinity_value when step is 0" do + 1.step(to: 5, by: 0).size.should == infinity_value + end + + it "should return infinity_value when step is 0.0" do + 1.step(to: 2, by: 0.0).size.should == infinity_value + end + + it "should return infinity_value when ascending towards a limit of Float::INFINITY" do + 1.step(to: Float::INFINITY, by: 42).size.should == infinity_value + end + + it "should return infinity_value when decending towards a limit of -Float::INFINITY" do + 1.step(to: -Float::INFINITY, by: -42).size.should == infinity_value + end + + it "should return 1 when the both limit and step are Float::INFINITY" do + 1.step(to: Float::INFINITY, by: Float::INFINITY).size.should == 1 + end + + it "should return 1 when the both limit and step are -Float::INFINITY" do + 1.step(to: -Float::INFINITY, by: -Float::INFINITY).size.should == 1 + end + end + end + end + + before :all do + # This lambda transforms a positional step method args into + # keyword arguments. + # It's needed to test numeric_step behaviour with keyword arguments. + @step_args = ->(*args) do + kw_args = {to: args[0]} + kw_args[:by] = args[1] if args.size == 2 + [kw_args] + end + end + it_behaves_like :numeric_step, :step + end + + describe 'with mixed arguments' do + it "doesn't raise an error when step is 0" do + lambda { 1.step(5, by: 0) { break } }.should_not raise_error + end + + it "doesn't raise an error when step is 0.0" do + lambda { 1.step(2, by: 0.0) { break } }.should_not raise_error + end + + it "raises a ArgumentError when limit and to are defined" do + lambda { 1.step(5, 1, to: 5) { break } }.should raise_error(ArgumentError) + end + + it "raises a ArgumentError when step and by are defined" do + lambda { 1.step(5, 1, by: 5) { break } }.should raise_error(ArgumentError) + end + + it "should loop over self when step is 0 or 0.0" do + 1.step(2, by: 0.0).take(5).should eql [1.0, 1.0, 1.0, 1.0, 1.0] + 1.step(2, by: 0).take(5).should eql [1, 1, 1, 1, 1] + 1.1.step(2, by: 0).take(5).should eql [1.1, 1.1, 1.1, 1.1, 1.1] + end + + describe "when no block is given" do + describe "returned Enumerator" do + describe "size" do + it "should return infinity_value when step is 0" do + 1.step(5, by: 0).size.should == infinity_value + end + + it "should return infinity_value when step is 0.0" do + 1.step(2, by: 0.0).size.should == infinity_value + end + end + end + end + before :all do + # This lambda definition transforms a positional step method args into + # a mix of positional and keyword arguments. + # It's needed to test numeric_step behaviour with positional mixed with + # keyword arguments. + @step_args = ->(*args) do + if args.size == 2 + [args[0], {by: args[1]}] + else + args + end + end + end + it_behaves_like :numeric_step, :step + end +end diff --git a/spec/ruby/core/numeric/to_c_spec.rb b/spec/ruby/core/numeric/to_c_spec.rb new file mode 100644 index 0000000000..38452231b0 --- /dev/null +++ b/spec/ruby/core/numeric/to_c_spec.rb @@ -0,0 +1,45 @@ +require File.expand_path('../../../spec_helper', __FILE__) + +describe "Numeric#to_c" do + before :all do + @numbers = [ + 0, + 29871, + 99999999999999**99, + -72628191273, + Rational(2,3), + Rational(1.898), + Rational(-238), + 29282.2827, + -2927.00091, + 0.0, + 12.0, + Float::MAX, + infinity_value, + nan_value + ] + end + + it "returns a Complex object" do + @numbers.each do |number| + number.to_c.should be_an_instance_of(Complex) + end + end + + it "uses self as the real component" do + @numbers.each do |number| + real = number.to_c.real + if Float === number and number.nan? + real.nan?.should be_true + else + real.should == number + end + end + end + + it "uses 0 as the imaginary component" do + @numbers.each do |number| + number.to_c.imag.should == 0 + end + end +end diff --git a/spec/ruby/core/numeric/to_int_spec.rb b/spec/ruby/core/numeric/to_int_spec.rb new file mode 100644 index 0000000000..4f1df3e042 --- /dev/null +++ b/spec/ruby/core/numeric/to_int_spec.rb @@ -0,0 +1,10 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require File.expand_path('../fixtures/classes', __FILE__) + +describe "Numeric#to_int" do + it "returns self#to_i" do + obj = NumericSpecs::Subclass.new + obj.should_receive(:to_i).and_return(:result) + obj.to_int.should == :result + end +end diff --git a/spec/ruby/core/numeric/truncate_spec.rb b/spec/ruby/core/numeric/truncate_spec.rb new file mode 100644 index 0000000000..f1a2d4de64 --- /dev/null +++ b/spec/ruby/core/numeric/truncate_spec.rb @@ -0,0 +1,14 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require File.expand_path('../fixtures/classes', __FILE__) + +describe "Numeric#truncate" do + before :each do + @obj = NumericSpecs::Subclass.new + end + + it "converts self to a Float (using #to_f) and returns the #truncate'd result" do + @obj.should_receive(:to_f).and_return(2.5555, -2.3333) + @obj.truncate.should == 2 + @obj.truncate.should == -2 + end +end diff --git a/spec/ruby/core/numeric/uminus_spec.rb b/spec/ruby/core/numeric/uminus_spec.rb new file mode 100644 index 0000000000..7385f5f599 --- /dev/null +++ b/spec/ruby/core/numeric/uminus_spec.rb @@ -0,0 +1,31 @@ +require File.expand_path('../../../spec_helper', __FILE__) + +describe "Numeric#-@" do + it "returns the same value with opposite sign (integers)" do + 0.send(:-@).should == 0 + 100.send(:-@).should == -100 + -100.send(:-@).should == 100 + end + + it "returns the same value with opposite sign (floats)" do + 34.56.send(:-@).should == -34.56 + -34.56.send(:-@).should == 34.56 + end + + it "returns the same value with opposite sign (two complement)" do + 2147483648.send(:-@).should == -2147483648 + -2147483648.send(:-@).should == 2147483648 + 9223372036854775808.send(:-@).should == -9223372036854775808 + -9223372036854775808.send(:-@).should == 9223372036854775808 + end + + describe "with a Numeric subclass" do + it "calls #coerce(0) on self, then subtracts the second element of the result from the first" do + ten = mock_numeric('10') + zero = mock_numeric('0') + ten.should_receive(:coerce).with(0).and_return([zero, ten]) + zero.should_receive(:-).with(ten).and_return(-10) + ten.send(:-@).should == -10 + end + end +end diff --git a/spec/ruby/core/numeric/uplus_spec.rb b/spec/ruby/core/numeric/uplus_spec.rb new file mode 100644 index 0000000000..557142295a --- /dev/null +++ b/spec/ruby/core/numeric/uplus_spec.rb @@ -0,0 +1,9 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require File.expand_path('../fixtures/classes', __FILE__) + +describe "Numeric#+@" do + it "returns self" do + obj = NumericSpecs::Subclass.new + obj.send(:+@).should == obj + end +end diff --git a/spec/ruby/core/numeric/zero_spec.rb b/spec/ruby/core/numeric/zero_spec.rb new file mode 100644 index 0000000000..d46e8807ea --- /dev/null +++ b/spec/ruby/core/numeric/zero_spec.rb @@ -0,0 +1,18 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require File.expand_path('../fixtures/classes', __FILE__) + +describe "Numeric#zero?" do + before :each do + @obj = NumericSpecs::Subclass.new + end + + it "returns true if self is 0" do + @obj.should_receive(:==).with(0).and_return(true) + @obj.zero?.should == true + end + + it "returns false if self is not 0" do + @obj.should_receive(:==).with(0).and_return(false) + @obj.zero?.should == false + end +end |