diff options
Diffstat (limited to 'spec/ruby/core/fixnum')
40 files changed, 1181 insertions, 0 deletions
diff --git a/spec/ruby/core/fixnum/abs_spec.rb b/spec/ruby/core/fixnum/abs_spec.rb new file mode 100644 index 0000000000..5e6a7de891 --- /dev/null +++ b/spec/ruby/core/fixnum/abs_spec.rb @@ -0,0 +1,7 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require File.expand_path('../shared/abs', __FILE__) + +describe "Fixnum#abs" do + it_behaves_like :fixnum_abs, :abs +end + diff --git a/spec/ruby/core/fixnum/bit_and_spec.rb b/spec/ruby/core/fixnum/bit_and_spec.rb new file mode 100644 index 0000000000..9586075039 --- /dev/null +++ b/spec/ruby/core/fixnum/bit_and_spec.rb @@ -0,0 +1,46 @@ +require File.expand_path('../../../spec_helper', __FILE__) + +describe "Fixnum#&" do + it "returns self bitwise AND other" do + (256 & 16).should == 0 + (2010 & 5).should == 0 + (65535 & 1).should == 1 + (0xffff & bignum_value + 0xffff_ffff).should == 65535 + end + + it "returns self bitwise AND other when one operand is negative" do + ((1 << 33) & -1).should == (1 << 33) + (-1 & (1 << 33)).should == (1 << 33) + + ((-(1<<33)-1) & 5).should == 5 + (5 & (-(1<<33)-1)).should == 5 + end + + it "returns self bitwise AND other when both operands are negative" do + (-5 & -1).should == -5 + (-3 & -4).should == -4 + (-12 & -13).should == -16 + (-13 & -12).should == -16 + end + + it "returns self bitwise AND a Bignum" do + (-1 & 2**64).should == 18446744073709551616 + end + + it "coerces the rhs and calls #coerce" do + obj = mock("fixnum bit and") + obj.should_receive(:coerce).with(6).and_return([3, 6]) + (6 & obj).should == 2 + end + + it "raises a TypeError when passed a Float" do + lambda { (3 & 3.4) }.should raise_error(TypeError) + end + + it "raises a TypeError and does not call #to_int when defined on an object" do + obj = mock("fixnum bit and") + obj.should_not_receive(:to_int) + + lambda { 3 & obj }.should raise_error(TypeError) + end +end diff --git a/spec/ruby/core/fixnum/bit_length_spec.rb b/spec/ruby/core/fixnum/bit_length_spec.rb new file mode 100644 index 0000000000..8c9f69b7d7 --- /dev/null +++ b/spec/ruby/core/fixnum/bit_length_spec.rb @@ -0,0 +1,42 @@ +require File.expand_path('../../../spec_helper', __FILE__) + +describe "Fixnum#bit_length" do + it "returns the position of the leftmost bit of a positive number" do + 0.bit_length.should == 0 + 1.bit_length.should == 1 + 2.bit_length.should == 2 + 3.bit_length.should == 2 + 4.bit_length.should == 3 + n = fixnum_max.bit_length + fixnum_max[n].should == 0 + fixnum_max[n-1].should == 1 + + 0.bit_length.should == 0 + 1.bit_length.should == 1 + 0xff.bit_length.should == 8 + 0x100.bit_length.should == 9 + (2**12-1).bit_length.should == 12 + (2**12).bit_length.should == 13 + (2**12+1).bit_length.should == 13 + end + + it "returns the position of the leftmost 0 bit of a negative number" do + -1.bit_length.should == 0 + -2.bit_length.should == 1 + -3.bit_length.should == 2 + -4.bit_length.should == 2 + -5.bit_length.should == 3 + n = fixnum_min.bit_length + fixnum_min[n].should == 1 + fixnum_min[n-1].should == 0 + + (-2**12-1).bit_length.should == 13 + (-2**12).bit_length.should == 12 + (-2**12+1).bit_length.should == 12 + -0x101.bit_length.should == 9 + -0x100.bit_length.should == 8 + -0xff.bit_length.should == 8 + -2.bit_length.should == 1 + -1.bit_length.should == 0 + end +end diff --git a/spec/ruby/core/fixnum/bit_or_spec.rb b/spec/ruby/core/fixnum/bit_or_spec.rb new file mode 100644 index 0000000000..cd1865ffe9 --- /dev/null +++ b/spec/ruby/core/fixnum/bit_or_spec.rb @@ -0,0 +1,26 @@ +require File.expand_path('../../../spec_helper', __FILE__) + +describe "Fixnum#|" do + it "returns self bitwise OR other" do + (1 | 0).should == 1 + (5 | 4).should == 5 + (5 | 6).should == 7 + (248 | 4096).should == 4344 + (0xffff | bignum_value + 0xf0f0).should == 0x8000_0000_0000_ffff + end + + it "returns self bitwise OR a Bignum" do + (-1 | 2**64).should == -1 + end + + it "raises a TypeError when passed a Float" do + lambda { (3 | 3.4) }.should raise_error(TypeError) + end + + it "raises a TypeError and does not call #to_int when defined on an object" do + obj = mock("fixnum bit or") + obj.should_not_receive(:to_int) + + lambda { 3 | obj }.should raise_error(TypeError) + end +end diff --git a/spec/ruby/core/fixnum/bit_xor_spec.rb b/spec/ruby/core/fixnum/bit_xor_spec.rb new file mode 100644 index 0000000000..38a90a4dfa --- /dev/null +++ b/spec/ruby/core/fixnum/bit_xor_spec.rb @@ -0,0 +1,24 @@ +require File.expand_path('../../../spec_helper', __FILE__) + +describe "Fixnum#^" do + it "returns self bitwise EXCLUSIVE OR other" do + (3 ^ 5).should == 6 + (-2 ^ -255).should == 255 + (5 ^ bignum_value + 0xffff_ffff).should == 0x8000_0000_ffff_fffa + end + + it "returns self bitwise EXCLUSIVE OR a Bignum" do + (-1 ^ 2**64).should == -18446744073709551617 + end + + it "raises a TypeError when passed a Float" do + lambda { (3 ^ 3.4) }.should raise_error(TypeError) + end + + it "raises a TypeError and does not call #to_int when defined on an object" do + obj = mock("fixnum bit xor") + obj.should_not_receive(:to_int) + + lambda { 3 ^ obj }.should raise_error(TypeError) + end +end diff --git a/spec/ruby/core/fixnum/case_compare_spec.rb b/spec/ruby/core/fixnum/case_compare_spec.rb new file mode 100644 index 0000000000..53bfc38eb8 --- /dev/null +++ b/spec/ruby/core/fixnum/case_compare_spec.rb @@ -0,0 +1,6 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require File.expand_path('../shared/equal', __FILE__) + +describe "Fixnum#===" do + it_behaves_like :fixnum_equal, :=== +end diff --git a/spec/ruby/core/fixnum/coerce_spec.rb b/spec/ruby/core/fixnum/coerce_spec.rb new file mode 100644 index 0000000000..3ca7cb2df4 --- /dev/null +++ b/spec/ruby/core/fixnum/coerce_spec.rb @@ -0,0 +1,39 @@ +require File.expand_path('../../../spec_helper', __FILE__) + +describe "Fixnum#coerce when given a Fixnum" do + it "returns an array containing two Fixnums" do + 1.coerce(2).should == [2, 1] + 1.coerce(2).map { |i| i.class }.should == [Fixnum, Fixnum] + end +end + +describe "Fixnum#coerce when given a String" do + it "raises an ArgumentError when trying to coerce with a non-number String" do + lambda { 1.coerce(":)") }.should raise_error(ArgumentError) + end + + it "returns an array containing two Floats" do + 1.coerce("2").should == [2.0, 1.0] + 1.coerce("-2").should == [-2.0, 1.0] + end +end + +describe "Fixnum#coerce" do + it "raises a TypeError when trying to coerce with nil" do + lambda { 1.coerce(nil) }.should raise_error(TypeError) + end + + it "tries to convert the given Object into a Float by using #to_f" do + (obj = mock('1.0')).should_receive(:to_f).and_return(1.0) + 2.coerce(obj).should == [1.0, 2.0] + + (obj = mock('0')).should_receive(:to_f).and_return('0') + lambda { 2.coerce(obj).should == [1.0, 2.0] }.should raise_error(TypeError) + end + + it "raises a TypeError when given an Object that does not respond to #to_f" do + lambda { 1.coerce(mock('x')) }.should raise_error(TypeError) + lambda { 1.coerce(1..4) }.should raise_error(TypeError) + lambda { 1.coerce(:test) }.should raise_error(TypeError) + end +end diff --git a/spec/ruby/core/fixnum/comparison_spec.rb b/spec/ruby/core/fixnum/comparison_spec.rb new file mode 100644 index 0000000000..4c932d213d --- /dev/null +++ b/spec/ruby/core/fixnum/comparison_spec.rb @@ -0,0 +1,26 @@ +require File.expand_path('../../../spec_helper', __FILE__) + +describe "Fixnum#<=>" do + it "returns -1 when self is less than the given argument" do + (-3 <=> -1).should == -1 + (-5 <=> 10).should == -1 + (-5 <=> -4.5).should == -1 + end + + it "returns 0 when self is equal to the given argument" do + (0 <=> 0).should == 0 + (954 <=> 954).should == 0 + (954 <=> 954.0).should == 0 + end + + it "returns 1 when self is greater than the given argument" do + (496 <=> 5).should == 1 + (200 <=> 100).should == 1 + (51 <=> 50.5).should == 1 + end + + it "returns nil when the given argument is not an Integer" do + (3 <=> mock('x')).should == nil + (3 <=> 'test').should == nil + end +end diff --git a/spec/ruby/core/fixnum/complement_spec.rb b/spec/ruby/core/fixnum/complement_spec.rb new file mode 100644 index 0000000000..06692579c5 --- /dev/null +++ b/spec/ruby/core/fixnum/complement_spec.rb @@ -0,0 +1,10 @@ +require File.expand_path('../../../spec_helper', __FILE__) + +describe "Fixnum#~" do + it "returns self with each bit flipped" do + (~0).should == -1 + (~1221).should == -1222 + (~-2).should == 1 + (~-599).should == 598 + end +end diff --git a/spec/ruby/core/fixnum/div_spec.rb b/spec/ruby/core/fixnum/div_spec.rb new file mode 100644 index 0000000000..be9b498508 --- /dev/null +++ b/spec/ruby/core/fixnum/div_spec.rb @@ -0,0 +1,44 @@ +require File.expand_path('../../../spec_helper', __FILE__) + +describe "Fixnum#div with a Fixnum" do + it "returns self divided by the given argument as an Integer" do + 2.div(2).should == 1 + 1.div(2).should == 0 + 5.div(2).should == 2 + end +end + +describe "Fixnum#div" do + it "rounds towards -inf" do + 8192.div(10).should == 819 + 8192.div(-10).should == -820 + (-8192).div(10).should == -820 + (-8192).div(-10).should == 819 + end + + it "coerces self and the given argument to Floats and returns self divided by other as Fixnum" do + 1.div(0.2).should == 5 + 1.div(0.16).should == 6 + 1.div(0.169).should == 5 + -1.div(50.4).should == -1 + 1.div(bignum_value).should == 0 + end + + it "raises a ZeroDivisionError when the given argument is 0 and a Float" do + lambda { 0.div(0.0) }.should raise_error(ZeroDivisionError) + lambda { 10.div(0.0) }.should raise_error(ZeroDivisionError) + lambda { -10.div(0.0) }.should raise_error(ZeroDivisionError) + end + + it "raises a ZeroDivisionError when the given argument is 0" do + lambda { 13.div(0) }.should raise_error(ZeroDivisionError) + end + + it "raises a TypeError when given a non-Integer" do + lambda { + (obj = mock('10')).should_receive(:to_int).any_number_of_times.and_return(10) + 13.div(obj) + }.should raise_error(TypeError) + lambda { 5.div("2") }.should raise_error(TypeError) + end +end diff --git a/spec/ruby/core/fixnum/divide_spec.rb b/spec/ruby/core/fixnum/divide_spec.rb new file mode 100644 index 0000000000..1e7c17e58f --- /dev/null +++ b/spec/ruby/core/fixnum/divide_spec.rb @@ -0,0 +1,35 @@ +require File.expand_path('../../../spec_helper', __FILE__) + +describe "Fixnum#/" do + it "returns self divided by the given argument" do + (2 / 2).should == 1 + (3 / 2).should == 1 + end + + it "supports dividing negative numbers" do + (-1 / 10).should == -1 + end + + it "raises a ZeroDivisionError if the given argument is zero and not a Float" do + lambda { 1 / 0 }.should raise_error(ZeroDivisionError) + end + + it "does NOT raise ZeroDivisionError if the given argument is zero and is a Float" do + (1 / 0.0).to_s.should == 'Infinity' + (-1 / 0.0).to_s.should == '-Infinity' + end + + it "coerces fixnum and return self divided by other" do + (-1 / 50.4).should be_close(-0.0198412698412698, TOLERANCE) + (1 / bignum_value).should == 0 + end + + it "raises a TypeError when given a non-Integer" do + lambda { + (obj = mock('10')).should_receive(:to_int).any_number_of_times.and_return(10) + 13 / obj + }.should raise_error(TypeError) + lambda { 13 / "10" }.should raise_error(TypeError) + lambda { 13 / :symbol }.should raise_error(TypeError) + end +end diff --git a/spec/ruby/core/fixnum/divmod_spec.rb b/spec/ruby/core/fixnum/divmod_spec.rb new file mode 100644 index 0000000000..21c9afe315 --- /dev/null +++ b/spec/ruby/core/fixnum/divmod_spec.rb @@ -0,0 +1,35 @@ +require File.expand_path('../../../spec_helper', __FILE__) + +describe "Fixnum#divmod" do + it "returns an Array containing quotient and modulus obtained from dividing self by the given argument" do + 13.divmod(4).should == [3, 1] + 4.divmod(13).should == [0, 4] + + 13.divmod(4.0).should == [3, 1] + 4.divmod(13.0).should == [0, 4] + + 1.divmod(2.0).should == [0, 1.0] + 200.divmod(bignum_value).should == [0, 200] + end + + it "raises a ZeroDivisionError when the given argument is 0" do + lambda { 13.divmod(0) }.should raise_error(ZeroDivisionError) + lambda { 0.divmod(0) }.should raise_error(ZeroDivisionError) + lambda { -10.divmod(0) }.should raise_error(ZeroDivisionError) + end + + it "raises a ZeroDivisionError when the given argument is 0 and a Float" do + lambda { 0.divmod(0.0) }.should raise_error(ZeroDivisionError) + lambda { 10.divmod(0.0) }.should raise_error(ZeroDivisionError) + lambda { -10.divmod(0.0) }.should raise_error(ZeroDivisionError) + end + + it "raises a TypeError when given a non-Integer" do + lambda { + (obj = mock('10')).should_receive(:to_int).any_number_of_times.and_return(10) + 13.divmod(obj) + }.should raise_error(TypeError) + lambda { 13.divmod("10") }.should raise_error(TypeError) + lambda { 13.divmod(:symbol) }.should raise_error(TypeError) + end +end diff --git a/spec/ruby/core/fixnum/element_reference_spec.rb b/spec/ruby/core/fixnum/element_reference_spec.rb new file mode 100644 index 0000000000..736e8a549b --- /dev/null +++ b/spec/ruby/core/fixnum/element_reference_spec.rb @@ -0,0 +1,80 @@ +require File.expand_path('../../../spec_helper', __FILE__) + +describe "Fixnum#[]" do + it "behaves like (n >> b) & 1" do + 0b101[1].should == 0 + 0b101[2].should == 1 + end + + it "returns 1 if the nth bit is set" do + 15[1].should == 1 + end + + it "returns 1 if the nth bit is set (in two's-complement representation)" do + (-1)[1].should == 1 + end + + it "returns 0 if the nth bit is not set" do + 8[2].should == 0 + end + + it "returns 0 if the nth bit is not set (in two's-complement representation)" do + (-2)[0].should == 0 + end + + it "returns 0 if the nth bit is greater than the most significant bit" do + 2[3].should == 0 + end + + it "returns 1 if self is negative and the nth bit is greater than the most significant bit" do + (-1)[3].should == 1 + end + + it "returns 0 when passed a negative argument" do + 3[-1].should == 0 + (-1)[-1].should == 0 + end + + it "calls #to_int to convert the argument to an Integer and returns 1 if the nth bit is set" do + obj = mock('1') + obj.should_receive(:to_int).and_return(1) + + 2[obj].should == 1 + end + + it "calls #to_int to convert the argument to an Integer and returns 0 if the nth bit is set" do + obj = mock('0') + obj.should_receive(:to_int).and_return(0) + + 2[obj].should == 0 + end + + it "accepts a Float argument and returns 0 if the bit at the truncated value is not set" do + 13[1.3].should == 0 + end + + it "accepts a Float argument and returns 1 if the bit at the truncated value is set" do + 13[2.1].should == 1 + end + + it "raises a TypeError when passed a String" do + lambda { 3["3"] }.should raise_error(TypeError) + end + + it "raises a TypeError when #to_int does not return an Integer" do + obj = mock('asdf') + obj.should_receive(:to_int).and_return("asdf") + lambda { 3[obj] }.should raise_error(TypeError) + end + + it "calls #to_int to coerce a String to a Bignum and returns 0" do + obj = mock('bignum value') + obj.should_receive(:to_int).and_return(bignum_value) + + 3[obj].should == 0 + end + + it "returns 0 when passed a Float in the range of a Bignum" do + 3[bignum_value.to_f].should == 0 + end +end diff --git a/spec/ruby/core/fixnum/equal_value_spec.rb b/spec/ruby/core/fixnum/equal_value_spec.rb new file mode 100644 index 0000000000..3a9ef93d24 --- /dev/null +++ b/spec/ruby/core/fixnum/equal_value_spec.rb @@ -0,0 +1,6 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require File.expand_path('../shared/equal', __FILE__) + +describe "Fixnum#==" do + it_behaves_like :fixnum_equal, :== +end diff --git a/spec/ruby/core/fixnum/even_spec.rb b/spec/ruby/core/fixnum/even_spec.rb new file mode 100644 index 0000000000..1cafb7d3a0 --- /dev/null +++ b/spec/ruby/core/fixnum/even_spec.rb @@ -0,0 +1,23 @@ +require File.expand_path('../../../spec_helper', __FILE__) + +describe "Fixnum#even?" do + it "is true for zero" do + 0.even?.should be_true + end + + it "is true for even positive Fixnums" do + 4.even?.should be_true + end + + it "is true for even negative Fixnums" do + (-4).even?.should be_true + end + + it "is false for odd positive Fixnums" do + 5.even?.should be_false + end + + it "is false for odd negative Fixnums" do + (-5).even?.should be_false + end +end diff --git a/spec/ruby/core/fixnum/exponent_spec.rb b/spec/ruby/core/fixnum/exponent_spec.rb new file mode 100644 index 0000000000..f3e7349ace --- /dev/null +++ b/spec/ruby/core/fixnum/exponent_spec.rb @@ -0,0 +1,76 @@ +require File.expand_path('../../../spec_helper', __FILE__) + +describe "Fixnum#**" do + it "returns self raised to the given power" do + (2 ** 0).should eql 1 + (2 ** 1).should eql 2 + (2 ** 2).should eql 4 + + (9 ** 0.5).should eql 3.0 + (5 ** -1).to_f.to_s.should == '0.2' + + (2 ** 40).should eql 1099511627776 + end + + it "overflows the answer to a bignum transparantly" do + (2 ** 29).should eql 536870912 + (2 ** 30).should eql 1073741824 + (2 ** 31).should eql 2147483648 + (2 ** 32).should eql 4294967296 + + (2 ** 61).should eql 2305843009213693952 + (2 ** 62).should eql 4611686018427387904 + (2 ** 63).should eql 9223372036854775808 + (2 ** 64).should eql 18446744073709551616 + (8 ** 23).should eql 590295810358705651712 + end + + it "raises negative numbers to the given power" do + ((-2) ** 29).should eql(-536870912) + ((-2) ** 30).should eql(1073741824) + ((-2) ** 31).should eql(-2147483648) + ((-2) ** 32).should eql(4294967296) + + ((-2) ** 61).should eql(-2305843009213693952) + ((-2) ** 62).should eql(4611686018427387904) + ((-2) ** 63).should eql(-9223372036854775808) + ((-2) ** 64).should eql(18446744073709551616) + end + + it "can raise 1 to a Bignum safely" do + big = bignum_value(4611686018427387904) + (1 ** big).should eql 1 + end + + it "can raise -1 to a Bignum safely" do + ((-1) ** bignum_value(0)).should eql(1) + ((-1) ** bignum_value(1)).should eql(-1) + end + + it "switches to a Float when the number is too big" do + big = bignum_value(4611686018427387904) + flt = (2 ** big) + flt.should be_kind_of(Float) + flt.infinite?.should == 1 + end + + conflicts_with :Rational do + it "raises a ZeroDivisionError for 0**-1" do + lambda { (0**-1) }.should raise_error(ZeroDivisionError) + end + + it "raises a TypeError when given a non-Integer" do + lambda { + (obj = mock('10')).should_receive(:to_int).any_number_of_times.and_return(10) + 13 ** obj + }.should raise_error(TypeError) + lambda { 13 ** "10" }.should raise_error(TypeError) + lambda { 13 ** :symbol }.should raise_error(TypeError) + end + end + + it "returns a complex number when negative and raised to a fractional power" do + ((-8) ** (1.0/3)) .should be_close(Complex(1, 1.73205), TOLERANCE) + ((-8) ** Rational(1,3)).should be_close(Complex(1, 1.73205), TOLERANCE) + end +end diff --git a/spec/ruby/core/fixnum/fdiv_spec.rb b/spec/ruby/core/fixnum/fdiv_spec.rb new file mode 100644 index 0000000000..bb5f09c333 --- /dev/null +++ b/spec/ruby/core/fixnum/fdiv_spec.rb @@ -0,0 +1,49 @@ +require File.expand_path('../../../spec_helper', __FILE__) + +describe "Fixnum#fdiv" do + it "performs floating-point division between self and a Fixnum" do + 8.fdiv(7).should be_close(1.14285714285714, TOLERANCE) + end + + it "performs floating-point division between self and a Bignum" do + 8.fdiv(bignum_value).should be_close(8.673617379884035e-19, TOLERANCE) + end + + it "performs floating-point division between self and a Float" do + 8.fdiv(9.0).should be_close(0.888888888888889, TOLERANCE) + end + + it "returns NaN when the argument is NaN" do + -1.fdiv(nan_value).nan?.should be_true + 1.fdiv(nan_value).nan?.should be_true + end + + it "returns Infinity when the argument is 0" do + 1.fdiv(0).infinite?.should == 1 + end + + it "returns -Infinity when the argument is 0 and self is negative" do + -1.fdiv(0).infinite?.should == -1 + end + + it "returns Infinity when the argument is 0.0" do + 1.fdiv(0.0).infinite?.should == 1 + end + + it "returns -Infinity when the argument is 0.0 and self is negative" do + -1.fdiv(0.0).infinite?.should == -1 + end + + it "raises a TypeError when argument isn't numeric" do + lambda { 1.fdiv(mock('non-numeric')) }.should raise_error(TypeError) + end + + it "raises an ArgumentError when passed multiple arguments" do + lambda { 1.fdiv(6,0.2) }.should raise_error(ArgumentError) + end + + it "follows the coercion protocol" do + (obj = mock('10')).should_receive(:coerce).with(1).and_return([1, 10]) + 1.fdiv(obj).should == 0.1 + end +end diff --git a/spec/ruby/core/fixnum/fixnum_spec.rb b/spec/ruby/core/fixnum/fixnum_spec.rb new file mode 100644 index 0000000000..8a050fd25e --- /dev/null +++ b/spec/ruby/core/fixnum/fixnum_spec.rb @@ -0,0 +1,19 @@ +require File.expand_path('../../../spec_helper', __FILE__) + +describe "Fixnum" do + it "includes Comparable" do + Fixnum.include?(Comparable).should == true + end + + it ".allocate raises a TypeError" do + lambda do + Fixnum.allocate + end.should raise_error(TypeError) + end + + it ".new is undefined" do + lambda do + Fixnum.new + end.should raise_error(NoMethodError) + end +end diff --git a/spec/ruby/core/fixnum/gt_spec.rb b/spec/ruby/core/fixnum/gt_spec.rb new file mode 100644 index 0000000000..2fe70304ae --- /dev/null +++ b/spec/ruby/core/fixnum/gt_spec.rb @@ -0,0 +1,19 @@ +require File.expand_path('../../../spec_helper', __FILE__) + +describe "Fixnum#>" do + it "returns true if self is greater than the given argument" do + (13 > 2).should == true + (-500 > -600).should == true + + (1 > 5).should == false + (5 > 5).should == false + + (900 > bignum_value).should == false + (5 > 4.999).should == true + end + + it "raises an ArgumentError when given a non-Integer" do + lambda { 5 > "4" }.should raise_error(ArgumentError) + lambda { 5 > mock('x') }.should raise_error(ArgumentError) + end +end diff --git a/spec/ruby/core/fixnum/gte_spec.rb b/spec/ruby/core/fixnum/gte_spec.rb new file mode 100644 index 0000000000..1d5c2b70f8 --- /dev/null +++ b/spec/ruby/core/fixnum/gte_spec.rb @@ -0,0 +1,20 @@ +require File.expand_path('../../../spec_helper', __FILE__) + +describe "Fixnum#>=" do + it "returns true if self is greater than or equal to the given argument" do + (13 >= 2).should == true + (-500 >= -600).should == true + + (1 >= 5).should == false + (2 >= 2).should == true + (5 >= 5).should == true + + (900 >= bignum_value).should == false + (5 >= 4.999).should == true + end + + it "raises an ArgumentError when given a non-Integer" do + lambda { 5 >= "4" }.should raise_error(ArgumentError) + lambda { 5 >= mock('x') }.should raise_error(ArgumentError) + end +end diff --git a/spec/ruby/core/fixnum/hash_spec.rb b/spec/ruby/core/fixnum/hash_spec.rb new file mode 100644 index 0000000000..de2996a7c6 --- /dev/null +++ b/spec/ruby/core/fixnum/hash_spec.rb @@ -0,0 +1,11 @@ +require File.expand_path('../../../spec_helper', __FILE__) + +describe "Fixnum#hash" do + it "is provided" do + 1.respond_to?(:hash).should == true + end + + it "is stable" do + 1.hash.should == 1.hash + end +end diff --git a/spec/ruby/core/fixnum/left_shift_spec.rb b/spec/ruby/core/fixnum/left_shift_spec.rb new file mode 100644 index 0000000000..8eb5e424ff --- /dev/null +++ b/spec/ruby/core/fixnum/left_shift_spec.rb @@ -0,0 +1,91 @@ +require File.expand_path('../../../spec_helper', __FILE__) + +describe "Fixnum#<< with n << m" do + it "returns n shifted left m bits when n > 0, m > 0" do + (1 << 1).should == 2 + end + + it "returns n shifted left m bits when n < 0, m > 0" do + (-1 << 1).should == -2 + (-7 << 1).should == -14 + (-42 << 2).should == -168 + end + + it "returns n shifted right m bits when n > 0, m < 0" do + (2 << -1).should == 1 + end + + it "returns n shifted right m bits when n < 0, m < 0" do + (-2 << -1).should == -1 + end + + it "returns 0 when n == 0" do + (0 << 1).should == 0 + end + + it "returns n when n > 0, m == 0" do + (1 << 0).should == 1 + end + + it "returns n when n < 0, m == 0" do + (-1 << 0).should == -1 + end + + it "returns 0 when n > 0, m < 0 and n < 2**-m" do + (3 << -2).should == 0 + (7 << -3).should == 0 + (127 << -7).should == 0 + + # To make sure the exponent is not truncated + (7 << -32).should == 0 + (7 << -64).should == 0 + end + + it "returns -1 when n < 0, m < 0 and n > -(2**-m)" do + (-3 << -2).should == -1 + (-7 << -3).should == -1 + (-127 << -7).should == -1 + + # To make sure the exponent is not truncated + (-7 << -32).should == -1 + (-7 << -64).should == -1 + end + + it "returns 0 when m < 0 and m is a Bignum" do + (3 << -bignum_value).should == 0 + end + + it "returns a Bignum == fixnum_max * 2 when fixnum_max << 1 and n > 0" do + result = fixnum_max << 1 + result.should be_an_instance_of(Bignum) + result.should == fixnum_max * 2 + end + + it "returns a Bignum == fixnum_min * 2 when fixnum_min << 1 and n < 0" do + result = fixnum_min << 1 + result.should be_an_instance_of(Bignum) + result.should == fixnum_min * 2 + end + + it "calls #to_int to convert the argument to an Integer" do + obj = mock("4") + obj.should_receive(:to_int).and_return(4) + + (3 << obj).should == 48 + end + + it "raises a TypeError when #to_int does not return an Integer" do + obj = mock("a string") + obj.should_receive(:to_int).and_return("asdf") + + lambda { 3 << obj }.should raise_error(TypeError) + end + + it "raises a TypeError when passed nil" do + lambda { 3 << nil }.should raise_error(TypeError) + end + + it "raises a TypeError when passed a String" do + lambda { 3 << "4" }.should raise_error(TypeError) + end +end diff --git a/spec/ruby/core/fixnum/lt_spec.rb b/spec/ruby/core/fixnum/lt_spec.rb new file mode 100644 index 0000000000..0bedf428b2 --- /dev/null +++ b/spec/ruby/core/fixnum/lt_spec.rb @@ -0,0 +1,19 @@ +require File.expand_path('../../../spec_helper', __FILE__) + +describe "Fixnum#<" do + it "returns true if self is less than the given argument" do + (2 < 13).should == true + (-600 < -500).should == true + + (5 < 1).should == false + (5 < 5).should == false + + (900 < bignum_value).should == true + (5 < 4.999).should == false + end + + it "raises an ArgumentError when given a non-Integer" do + lambda { 5 < "4" }.should raise_error(ArgumentError) + lambda { 5 < mock('x') }.should raise_error(ArgumentError) + end +end diff --git a/spec/ruby/core/fixnum/lte_spec.rb b/spec/ruby/core/fixnum/lte_spec.rb new file mode 100644 index 0000000000..b9e5810d26 --- /dev/null +++ b/spec/ruby/core/fixnum/lte_spec.rb @@ -0,0 +1,20 @@ +require File.expand_path('../../../spec_helper', __FILE__) + +describe "Fixnum#<=" do + it "returns true if self is less than or equal to other" do + (2 <= 13).should == true + (-600 <= -500).should == true + + (5 <= 1).should == false + (5 <= 5).should == true + (-2 <= -2).should == true + + (900 <= bignum_value).should == true + (5 <= 4.999).should == false + end + + it "raises an ArgumentError when given a non-Integer" do + lambda { 5 <= "4" }.should raise_error(ArgumentError) + lambda { 5 <= mock('x') }.should raise_error(ArgumentError) + end +end diff --git a/spec/ruby/core/fixnum/magnitude_spec.rb b/spec/ruby/core/fixnum/magnitude_spec.rb new file mode 100644 index 0000000000..dc250eba19 --- /dev/null +++ b/spec/ruby/core/fixnum/magnitude_spec.rb @@ -0,0 +1,6 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require File.expand_path('../shared/abs', __FILE__) + +describe "Fixnum#magnitude" do + it_behaves_like :fixnum_abs, :magnitude +end diff --git a/spec/ruby/core/fixnum/minus_spec.rb b/spec/ruby/core/fixnum/minus_spec.rb new file mode 100644 index 0000000000..de3af05179 --- /dev/null +++ b/spec/ruby/core/fixnum/minus_spec.rb @@ -0,0 +1,29 @@ +require File.expand_path('../../../spec_helper', __FILE__) + +describe "Fixnum#-" do + it "returns self minus the given Integer" do + (5 - 10).should == -5 + (9237212 - 5_280).should == 9231932 + + (781 - 0.5).should == 780.5 + (2_560_496 - bignum_value).should == -9223372036852215312 + end + + it "returns a Bignum only if the result is too large to be a Fixnum" do + (5 - 10).should be_an_instance_of Fixnum + (-1 - bignum_value).should be_an_instance_of Bignum + + bignum_zero = bignum_value.coerce(0).first + (1 - bignum_zero).should be_an_instance_of Fixnum + (fixnum_min - 1).should be_an_instance_of(Bignum) + end + + it "raises a TypeError when given a non-Integer" do + lambda { + (obj = mock('10')).should_receive(:to_int).any_number_of_times.and_return(10) + 13 - obj + }.should raise_error(TypeError) + lambda { 13 - "10" }.should raise_error(TypeError) + lambda { 13 - :symbol }.should raise_error(TypeError) + end +end diff --git a/spec/ruby/core/fixnum/modulo_spec.rb b/spec/ruby/core/fixnum/modulo_spec.rb new file mode 100644 index 0000000000..19d3291a11 --- /dev/null +++ b/spec/ruby/core/fixnum/modulo_spec.rb @@ -0,0 +1,10 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require File.expand_path('../shared/modulo', __FILE__) + +describe "Fixnum#%" do + it_behaves_like(:fixnum_modulo, :%) +end + +describe "Fixnum#modulo" do + it_behaves_like(:fixnum_modulo, :modulo) +end diff --git a/spec/ruby/core/fixnum/multiply_spec.rb b/spec/ruby/core/fixnum/multiply_spec.rb new file mode 100644 index 0000000000..2eabd7b632 --- /dev/null +++ b/spec/ruby/core/fixnum/multiply_spec.rb @@ -0,0 +1,27 @@ +require File.expand_path('../../../spec_helper', __FILE__) + +describe "Fixnum#*" do + it "returns self multiplied by the given Integer" do + (4923 * 2).should == 9846 + (1342177 * 800).should == 1073741600 + (65536 * 65536).should == 4294967296 + + (256 * bignum_value).should == 2361183241434822606848 + (6712 * 0.25).should == 1678.0 + end + + it "raises a TypeError when given a non-Integer" do + lambda { + (obj = mock('10')).should_receive(:to_int).any_number_of_times.and_return(10) + 13 * obj + }.should raise_error(TypeError) + lambda { 13 * "10" }.should raise_error(TypeError) + lambda { 13 * :symbol }.should raise_error(TypeError) + end + + it "overflows to Bignum when the result does not fit in Fixnum" do + (fixnum_max * fixnum_max).should be_kind_of(Bignum) + (fixnum_max * fixnum_min).should be_kind_of(Bignum) + end + +end diff --git a/spec/ruby/core/fixnum/odd_spec.rb b/spec/ruby/core/fixnum/odd_spec.rb new file mode 100644 index 0000000000..3cf6bf45ed --- /dev/null +++ b/spec/ruby/core/fixnum/odd_spec.rb @@ -0,0 +1,23 @@ +require File.expand_path('../../../spec_helper', __FILE__) + +describe "Fixnum#odd?" do + it "is false for zero" do + 0.odd?.should be_false + end + + it "is false for even positive Fixnums" do + 4.odd?.should be_false + end + + it "is false for even negative Fixnums" do + (-4).odd?.should be_false + end + + it "is true for odd positive Fixnums" do + 5.odd?.should be_true + end + + it "is true for odd negative Fixnums" do + (-5).odd?.should be_true + end +end diff --git a/spec/ruby/core/fixnum/plus_spec.rb b/spec/ruby/core/fixnum/plus_spec.rb new file mode 100644 index 0000000000..4754bfeaf8 --- /dev/null +++ b/spec/ruby/core/fixnum/plus_spec.rb @@ -0,0 +1,29 @@ +require File.expand_path('../../../spec_helper', __FILE__) + +describe "Fixnum#+" do + it "returns self plus the given Integer" do + (491 + 2).should == 493 + (90210 + 10).should == 90220 + + (9 + bignum_value).should == 9223372036854775817 + (1001 + 5.219).should == 1006.219 + end + + it "raises a TypeError when given a non-Integer" do + lambda { + (obj = mock('10')).should_receive(:to_int).any_number_of_times.and_return(10) + 13 + obj + }.should raise_error(TypeError) + lambda { 13 + "10" }.should raise_error(TypeError) + lambda { 13 + :symbol }.should raise_error(TypeError) + end + + it "overflows to Bignum when the result does not fit in Fixnum" do + (5 + 10).should be_an_instance_of Fixnum + (1 + bignum_value).should be_an_instance_of Bignum + + bignum_zero = bignum_value.coerce(0).first + (1 + bignum_zero).should be_an_instance_of Fixnum + (fixnum_max + 1).should be_an_instance_of(Bignum) + end +end diff --git a/spec/ruby/core/fixnum/right_shift_spec.rb b/spec/ruby/core/fixnum/right_shift_spec.rb new file mode 100644 index 0000000000..9a221ddbe7 --- /dev/null +++ b/spec/ruby/core/fixnum/right_shift_spec.rb @@ -0,0 +1,91 @@ +require File.expand_path('../../../spec_helper', __FILE__) + +describe "Fixnum#>> with n >> m" do + it "returns n shifted right m bits when n > 0, m > 0" do + (2 >> 1).should == 1 + end + + it "returns n shifted right m bits when n < 0, m > 0" do + (-2 >> 1).should == -1 + (-7 >> 1).should == -4 + (-42 >> 2).should == -11 + end + + it "returns n shifted left m bits when n > 0, m < 0" do + (1 >> -1).should == 2 + end + + it "returns n shifted left m bits when n < 0, m < 0" do + (-1 >> -1).should == -2 + end + + it "returns 0 when n == 0" do + (0 >> 1).should == 0 + end + + it "returns n when n > 0, m == 0" do + (1 >> 0).should == 1 + end + + it "returns n when n < 0, m == 0" do + (-1 >> 0).should == -1 + end + + it "returns 0 when n > 0, m > 0 and n < 2**m" do + (3 >> 2).should == 0 + (7 >> 3).should == 0 + (127 >> 7).should == 0 + + # To make sure the exponent is not truncated + (7 >> 32).should == 0 + (7 >> 64).should == 0 + end + + it "returns -1 when n < 0, m > 0 and n > -(2**m)" do + (-3 >> 2).should == -1 + (-7 >> 3).should == -1 + (-127 >> 7).should == -1 + + # To make sure the exponent is not truncated + (-7 >> 32).should == -1 + (-7 >> 64).should == -1 + end + + it "returns 0 when m is a Bignum" do + (3 >> bignum_value).should == 0 + end + + it "returns a Bignum == fixnum_max * 2 when fixnum_max >> -1 and n > 0" do + result = fixnum_max >> -1 + result.should be_an_instance_of(Bignum) + result.should == fixnum_max * 2 + end + + it "returns a Bignum == fixnum_min * 2 when fixnum_min >> -1 and n < 0" do + result = fixnum_min >> -1 + result.should be_an_instance_of(Bignum) + result.should == fixnum_min * 2 + end + + it "calls #to_int to convert the argument to an Integer" do + obj = mock("2") + obj.should_receive(:to_int).and_return(2) + + (8 >> obj).should == 2 + end + + it "raises a TypeError when #to_int does not return an Integer" do + obj = mock("a string") + obj.should_receive(:to_int).and_return("asdf") + + lambda { 3 >> obj }.should raise_error(TypeError) + end + + it "raises a TypeError when passed nil" do + lambda { 3 >> nil }.should raise_error(TypeError) + end + + it "raises a TypeError when passed a String" do + lambda { 3 >> "4" }.should raise_error(TypeError) + end +end diff --git a/spec/ruby/core/fixnum/shared/abs.rb b/spec/ruby/core/fixnum/shared/abs.rb new file mode 100644 index 0000000000..511ec5221b --- /dev/null +++ b/spec/ruby/core/fixnum/shared/abs.rb @@ -0,0 +1,9 @@ +describe :fixnum_abs, shared: true do + it "returns self's absolute value" do + { 0 => [0, -0, +0], 2 => [2, -2, +2], 100 => [100, -100, +100] }.each do |key, values| + values.each do |value| + value.send(@method).should == key + end + end + end +end diff --git a/spec/ruby/core/fixnum/shared/equal.rb b/spec/ruby/core/fixnum/shared/equal.rb new file mode 100644 index 0000000000..01c763f316 --- /dev/null +++ b/spec/ruby/core/fixnum/shared/equal.rb @@ -0,0 +1,24 @@ +describe :fixnum_equal, shared: true do + it "returns true if self has the same value as other" do + 1.send(@method, 1).should == true + 9.send(@method, 5).should == false + + # Actually, these call Float#==, Bignum#== etc. + 9.send(@method, 9.0).should == true + 9.send(@method, 9.01).should == false + + 10.send(@method, bignum_value).should == false + end + + it "calls 'other == self' if the given argument is not a Fixnum" do + 1.send(@method, '*').should == false + + obj = mock('one other') + obj.should_receive(:==).any_number_of_times.and_return(false) + 1.send(@method, obj).should == false + + obj = mock('another') + obj.should_receive(:==).any_number_of_times.and_return(true) + 2.send(@method, obj).should == true + end +end diff --git a/spec/ruby/core/fixnum/shared/modulo.rb b/spec/ruby/core/fixnum/shared/modulo.rb new file mode 100644 index 0000000000..a2f9a2691d --- /dev/null +++ b/spec/ruby/core/fixnum/shared/modulo.rb @@ -0,0 +1,42 @@ +describe :fixnum_modulo, shared: true do + it "returns the modulus obtained from dividing self by the given argument" do + 13.send(@method, 4).should == 1 + 4.send(@method, 13).should == 4 + + 13.send(@method, 4.0).should == 1 + 4.send(@method, 13.0).should == 4 + + (-200).send(@method, 256).should == 56 + (-1000).send(@method, 512).should == 24 + + (-200).send(@method, -256).should == -200 + (-1000).send(@method, -512).should == -488 + + (200).send(@method, -256).should == -56 + (1000).send(@method, -512).should == -24 + + 1.send(@method, 2.0).should == 1.0 + 200.send(@method, bignum_value).should == 200 + end + + it "raises a ZeroDivisionError when the given argument is 0" do + lambda { 13.send(@method, 0) }.should raise_error(ZeroDivisionError) + lambda { 0.send(@method, 0) }.should raise_error(ZeroDivisionError) + lambda { -10.send(@method, 0) }.should raise_error(ZeroDivisionError) + end + + it "raises a ZeroDivisionError when the given argument is 0 and a Float" do + lambda { 0.send(@method, 0.0) }.should raise_error(ZeroDivisionError) + lambda { 10.send(@method, 0.0) }.should raise_error(ZeroDivisionError) + lambda { -10.send(@method, 0.0) }.should raise_error(ZeroDivisionError) + end + + it "raises a TypeError when given a non-Integer" do + lambda { + (obj = mock('10')).should_receive(:to_int).any_number_of_times.and_return(10) + 13.send(@method, obj) + }.should raise_error(TypeError) + lambda { 13.send(@method, "10") }.should raise_error(TypeError) + lambda { 13.send(@method, :symbol) }.should raise_error(TypeError) + end +end diff --git a/spec/ruby/core/fixnum/size_spec.rb b/spec/ruby/core/fixnum/size_spec.rb new file mode 100644 index 0000000000..f973d446ed --- /dev/null +++ b/spec/ruby/core/fixnum/size_spec.rb @@ -0,0 +1,19 @@ +require File.expand_path('../../../spec_helper', __FILE__) + +describe "Fixnum#size" do + platform_is wordsize: 32 do + it "returns the number of bytes in the machine representation of self" do + -1.size.should == 4 + 0.size.should == 4 + 4091.size.should == 4 + end + end + + platform_is wordsize: 64 do + it "returns the number of bytes in the machine representation of self" do + -1.size.should == 8 + 0.size.should == 8 + 4091.size.should == 8 + end + end +end diff --git a/spec/ruby/core/fixnum/succ_spec.rb b/spec/ruby/core/fixnum/succ_spec.rb new file mode 100644 index 0000000000..50dd8c9481 --- /dev/null +++ b/spec/ruby/core/fixnum/succ_spec.rb @@ -0,0 +1,15 @@ +require File.expand_path('../../../spec_helper', __FILE__) + +describe "Fixnum#succ" do + it "returns the next larger positive Fixnum" do + 2.succ.should == 3 + end + + it "returns the next larger negative Fixnum" do + (-2).succ.should == -1 + end + + it "overflows a Fixnum to a Bignum" do + fixnum_max.succ.should == (fixnum_max + 1) + end +end diff --git a/spec/ruby/core/fixnum/to_f_spec.rb b/spec/ruby/core/fixnum/to_f_spec.rb new file mode 100644 index 0000000000..1d66348a5a --- /dev/null +++ b/spec/ruby/core/fixnum/to_f_spec.rb @@ -0,0 +1,9 @@ +require File.expand_path('../../../spec_helper', __FILE__) + +describe "Fixnum#to_f" do + it "returns self converted to a Float" do + 0.to_f.should == 0.0 + -500.to_f.should == -500.0 + 9_641_278.to_f.should == 9641278.0 + end +end diff --git a/spec/ruby/core/fixnum/to_s_spec.rb b/spec/ruby/core/fixnum/to_s_spec.rb new file mode 100644 index 0000000000..4a6649237b --- /dev/null +++ b/spec/ruby/core/fixnum/to_s_spec.rb @@ -0,0 +1,50 @@ +require File.expand_path('../../../spec_helper', __FILE__) + +describe "Fixnum#to_s when given a base" do + it "returns self converted to a String in the given base" do + 12345.to_s(2).should == "11000000111001" + 12345.to_s(8).should == "30071" + 12345.to_s(10).should == "12345" + 12345.to_s(16).should == "3039" + 95.to_s(16).should == "5f" + 12345.to_s(36).should == "9ix" + end + + it "raises an ArgumentError if the base is less than 2 or higher than 36" do + lambda { 123.to_s(-1) }.should raise_error(ArgumentError) + lambda { 123.to_s(0) }.should raise_error(ArgumentError) + lambda { 123.to_s(1) }.should raise_error(ArgumentError) + lambda { 123.to_s(37) }.should raise_error(ArgumentError) + end +end + +describe "Fixnum#to_s when no base given" do + it "returns self converted to a String using base 10" do + 255.to_s.should == '255' + 3.to_s.should == '3' + 0.to_s.should == '0' + -9002.to_s.should == '-9002' + end +end + +with_feature :encoding do + describe "Fixnum#to_s" do + before :each do + @internal = Encoding.default_internal + end + + after :each do + Encoding.default_internal = @internal + end + + it "returns a String in US-ASCII encoding when Encoding.default_internal is nil" do + Encoding.default_internal = nil + 1.to_s.encoding.should equal(Encoding::US_ASCII) + end + + it "returns a String in US-ASCII encoding when Encoding.default_internal is not nil" do + Encoding.default_internal = Encoding::IBM437 + 1.to_s.encoding.should equal(Encoding::US_ASCII) + end + end +end diff --git a/spec/ruby/core/fixnum/uminus_spec.rb b/spec/ruby/core/fixnum/uminus_spec.rb new file mode 100644 index 0000000000..ac676400d1 --- /dev/null +++ b/spec/ruby/core/fixnum/uminus_spec.rb @@ -0,0 +1,16 @@ +require File.expand_path('../../../spec_helper', __FILE__) + +describe "Fixnum#-@" do + it "returns self as a negative value" do + 2.send(:-@).should == -2 + -2.should == -2 + -268435455.should == -268435455 + (--5).should == 5 + -8.send(:-@).should == 8 + end + + it "negates self at Fixnum/Bignum boundaries" do + fixnum_max.send(:-@).should == (0 - fixnum_max) + fixnum_min.send(:-@).should == (0 - fixnum_min) + end +end diff --git a/spec/ruby/core/fixnum/zero_spec.rb b/spec/ruby/core/fixnum/zero_spec.rb new file mode 100644 index 0000000000..e155f9f5e6 --- /dev/null +++ b/spec/ruby/core/fixnum/zero_spec.rb @@ -0,0 +1,9 @@ +require File.expand_path('../../../spec_helper', __FILE__) + +describe "Fixnum#zero?" do + it "returns true if self is 0" do + 0.zero?.should == true + -1.zero?.should == false + 1.zero?.should == false + end +end |