2015-12-16 00:07:31 -05:00
|
|
|
# frozen_string_literal: false
|
2007-07-05 03:38:41 -04:00
|
|
|
require 'test/unit'
|
|
|
|
|
|
|
|
class TestFixnum < Test::Unit::TestCase
|
|
|
|
def setup
|
|
|
|
@verbose = $VERBOSE
|
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
|
|
|
$VERBOSE = @verbose
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_pow
|
|
|
|
[1, 2, 2**64, 2**63*3, 2**64*3].each do |y|
|
2007-07-14 10:31:21 -04:00
|
|
|
[-1, 0, 1].each do |x|
|
2007-07-05 03:38:41 -04:00
|
|
|
z1 = x**y
|
|
|
|
z2 = (-x)**y
|
|
|
|
if y % 2 == 1
|
|
|
|
assert_equal(z2, -z1)
|
|
|
|
else
|
|
|
|
assert_equal(z2, z1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2007-07-12 09:23:29 -04:00
|
|
|
|
|
|
|
def test_succ
|
|
|
|
assert_equal(0x40000000, 0x3fffffff.succ, "[ruby-dev:31189]")
|
|
|
|
assert_equal(0x4000000000000000, 0x3fffffffffffffff.succ, "[ruby-dev:31190]")
|
|
|
|
end
|
|
|
|
|
2007-07-13 05:33:17 -04:00
|
|
|
def test_pred
|
|
|
|
assert_equal(-0x40000001, (-0x40000000).pred)
|
|
|
|
assert_equal(-0x4000000000000001, (-0x4000000000000000).pred)
|
|
|
|
end
|
|
|
|
|
2007-07-12 09:23:29 -04:00
|
|
|
def test_plus
|
2007-07-13 05:33:17 -04:00
|
|
|
assert_equal(0x40000000, 0x3fffffff+1)
|
2016-02-15 14:42:59 -05:00
|
|
|
assert_equal(0x7ffffffe, 0x3fffffff+0x3fffffff)
|
2007-07-12 09:23:29 -04:00
|
|
|
assert_equal(0x4000000000000000, 0x3fffffffffffffff+1)
|
2016-02-15 14:42:59 -05:00
|
|
|
assert_equal(0x7ffffffffffffffe, 0x3fffffffffffffff+0x3fffffffffffffff)
|
2007-07-13 05:33:17 -04:00
|
|
|
assert_equal(-0x40000001, (-0x40000000)+(-1))
|
|
|
|
assert_equal(-0x4000000000000001, (-0x4000000000000000)+(-1))
|
2016-02-15 14:42:59 -05:00
|
|
|
assert_equal(-0x7ffffffe, (-0x3fffffff)+(-0x3fffffff))
|
2007-07-13 05:33:17 -04:00
|
|
|
assert_equal(-0x80000000, (-0x40000000)+(-0x40000000))
|
2016-02-15 14:42:59 -05:00
|
|
|
assert_equal(-0x8000000000000000, (-0x4000000000000000)+(-0x4000000000000000))
|
2007-07-13 05:33:17 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_sub
|
|
|
|
assert_equal(0x40000000, 0x3fffffff-(-1))
|
|
|
|
assert_equal(0x4000000000000000, 0x3fffffffffffffff-(-1))
|
|
|
|
assert_equal(-0x40000001, (-0x40000000)-1)
|
|
|
|
assert_equal(-0x4000000000000001, (-0x4000000000000000)-1)
|
|
|
|
assert_equal(-0x80000000, (-0x40000000)-0x40000000)
|
2016-02-15 14:42:59 -05:00
|
|
|
assert_equal(0x7fffffffffffffff, 0x3fffffffffffffff-(-0x4000000000000000))
|
|
|
|
assert_equal(-0x8000000000000000, -0x4000000000000000-0x4000000000000000)
|
2007-07-12 09:23:29 -04:00
|
|
|
end
|
|
|
|
|
2007-07-13 05:33:17 -04:00
|
|
|
def test_mult
|
|
|
|
assert_equal(0x40000000, 0x20000000*2)
|
|
|
|
assert_equal(0x4000000000000000, 0x2000000000000000*2)
|
|
|
|
assert_equal(-0x40000001, 33025*(-32513))
|
|
|
|
assert_equal(-0x4000000000000001, 1380655685*(-3340214413))
|
|
|
|
assert_equal(0x40000000, (-0x40000000)*(-1))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_div
|
2007-07-13 06:21:50 -04:00
|
|
|
assert_equal(2, 5/2)
|
|
|
|
assert_equal(0, 1/2)
|
|
|
|
assert_equal(-1, -1/2)
|
|
|
|
assert_equal(0, -(1/2))
|
|
|
|
assert_equal(-1, (-1)/2)
|
|
|
|
assert_equal(0, (-1)/(-2))
|
|
|
|
assert_equal(-1, 1/(-2))
|
|
|
|
assert_equal(1, -(1/(-2)))
|
|
|
|
assert_equal(0x3fffffff, 0xbffffffd/3)
|
2007-07-13 05:33:17 -04:00
|
|
|
assert_equal(0x40000000, 0xc0000000/3)
|
|
|
|
assert_equal(0x4000000000000000, 0xc000000000000000/3)
|
|
|
|
assert_equal(-0x40000001, 0xc0000003/(-3))
|
|
|
|
assert_equal(-0x4000000000000001, 0xc000000000000003/(-3))
|
|
|
|
assert_equal(0x40000000, (-0x40000000)/(-1), "[ruby-dev:31210]")
|
2007-07-13 05:35:29 -04:00
|
|
|
assert_equal(0x4000000000000000, (-0x4000000000000000)/(-1))
|
2014-08-01 03:35:34 -04:00
|
|
|
assert_raise(FloatDomainError) { 2.div(Float::NAN).nan? }
|
2007-07-13 05:33:17 -04:00
|
|
|
end
|
2007-07-13 06:21:50 -04:00
|
|
|
|
|
|
|
def test_mod
|
|
|
|
assert_equal(2, (-0x40000000) % 3)
|
|
|
|
assert_equal(0, (-0x40000000) % (-1))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_divmod
|
|
|
|
(-5).upto(5) {|a|
|
|
|
|
(-5).upto(5) {|b|
|
|
|
|
next if b == 0
|
|
|
|
q, r = a.divmod(b)
|
|
|
|
assert_equal(a, b*q+r)
|
2013-12-13 04:18:05 -05:00
|
|
|
assert_operator(r.abs, :<, b.abs)
|
|
|
|
if 0 < b
|
|
|
|
assert_operator(r, :>=, 0)
|
|
|
|
assert_operator(r, :<, b)
|
|
|
|
else
|
|
|
|
assert_operator(r, :>, b)
|
|
|
|
assert_operator(r, :<=, 0)
|
|
|
|
end
|
2007-07-13 06:21:50 -04:00
|
|
|
assert_equal(q, a/b)
|
|
|
|
assert_equal(q, a.div(b))
|
|
|
|
assert_equal(r, a%b)
|
|
|
|
assert_equal(r, a.modulo(b))
|
|
|
|
}
|
|
|
|
}
|
2014-08-01 03:35:34 -04:00
|
|
|
assert_raise(FloatDomainError) { 2.divmod(Float::NAN) }
|
2007-07-13 06:21:50 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_not
|
|
|
|
assert_equal(-0x40000000, ~0x3fffffff)
|
|
|
|
assert_equal(0x3fffffff, ~-0x40000000)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_lshift
|
2010-04-29 01:46:04 -04:00
|
|
|
assert_equal(0x40000000, 0x20000000 << 1)
|
|
|
|
assert_equal(-0x40000000, (-0x20000000) << 1)
|
|
|
|
assert_equal(-0x80000000, (-0x40000000) << 1)
|
2007-07-13 06:21:50 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_rshift
|
2010-04-29 01:46:04 -04:00
|
|
|
assert_equal(0x20000000, 0x40000000 >> 1)
|
|
|
|
assert_equal(-0x20000000, (-0x40000000) >> 1)
|
|
|
|
assert_equal(-0x40000000, (-0x80000000) >> 1)
|
2007-07-13 06:21:50 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_abs
|
|
|
|
assert_equal(0x40000000, (-0x40000000).abs)
|
|
|
|
assert_equal(0x4000000000000000, (-0x4000000000000000).abs)
|
|
|
|
end
|
2008-01-31 09:08:14 -05:00
|
|
|
|
|
|
|
def test_to_s
|
|
|
|
assert_equal("1010", 10.to_s(2))
|
|
|
|
assert_equal("a", 10.to_s(36))
|
|
|
|
assert_raise(ArgumentError) { 10.to_s(1) }
|
|
|
|
end
|
|
|
|
|
2008-05-13 11:57:22 -04:00
|
|
|
def test_plus2
|
2008-01-31 09:08:14 -05:00
|
|
|
assert_equal(2, 1 + 1)
|
|
|
|
assert_equal(4294967297, 1 + 2**32)
|
|
|
|
assert_equal(2.0, 1 + 1.0)
|
|
|
|
assert_raise(TypeError) { 1 + nil }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_minus
|
|
|
|
assert_equal(0, 1 - 1)
|
|
|
|
assert_equal(-4294967295, 1 - 2**32)
|
|
|
|
assert_equal(0.0, 1 - 1.0)
|
|
|
|
assert_raise(TypeError) { 1 - nil }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_mul
|
|
|
|
assert_equal(6, 2.send(:*, 3))
|
|
|
|
a = 2**30-1
|
|
|
|
assert_equal(1152921502459363329, a.send(:*, a))
|
|
|
|
|
|
|
|
assert_equal(6.0, 2 * 3.0)
|
|
|
|
assert_raise(TypeError) { 2 * nil }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_divide
|
|
|
|
assert_equal(2.0, 4.quo(2))
|
|
|
|
assert_equal(2.0, 4 / 2)
|
|
|
|
assert_equal(2.0, 4.div(2))
|
|
|
|
|
|
|
|
assert_equal(0.5**32, 1.quo(2**32))
|
|
|
|
assert_equal(0, 1 / (2**32))
|
|
|
|
assert_equal(0, 1.div(2**32))
|
|
|
|
|
2013-06-05 21:04:11 -04:00
|
|
|
assert_kind_of(Float, 1.quo(2.0))
|
2008-01-31 09:08:14 -05:00
|
|
|
assert_equal(0.5, 1.quo(2.0))
|
|
|
|
assert_equal(0.5, 1 / 2.0)
|
|
|
|
assert_equal(0, 1.div(2.0))
|
|
|
|
|
|
|
|
### rational changes the behavior of Fixnum#quo
|
|
|
|
#assert_raise(TypeError) { 2.quo(nil) }
|
|
|
|
assert_raise(TypeError, NoMethodError) { 2.quo(nil) }
|
|
|
|
assert_raise(TypeError) { 2 / nil }
|
|
|
|
assert_raise(TypeError) { 2.div(nil) }
|
|
|
|
|
|
|
|
assert_equal(0, 4.modulo(2))
|
|
|
|
assert_equal(1, 1.modulo(2**32))
|
|
|
|
assert_equal(1, 1.modulo(2.0))
|
|
|
|
assert_raise(TypeError) { 2.modulo(nil) }
|
|
|
|
|
|
|
|
assert_equal([2, 0], 4.divmod(2))
|
|
|
|
assert_equal([0, 1], 1.divmod(2**32))
|
|
|
|
assert_equal([0, 1], 1.divmod(2.0))
|
|
|
|
assert_raise(TypeError) { 2.divmod(nil) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_pow2
|
|
|
|
assert_equal(65536, 2**16)
|
|
|
|
assert_equal(4294967296, 2**32)
|
|
|
|
assert_equal(0.5**16, 2**-16)
|
|
|
|
assert_equal(1, (-1)**4294967296)
|
|
|
|
assert_equal(-1, (-1)**4294967295)
|
|
|
|
assert_equal(4, 2**((2**32).coerce(2).first))
|
|
|
|
assert_equal(2, 4**0.5)
|
|
|
|
assert_equal(0, 0**0.5)
|
2010-04-29 01:46:04 -04:00
|
|
|
assert_equal(1, (0**-1.0).infinite?)
|
2008-01-31 09:08:14 -05:00
|
|
|
### rational changes the behavior of Fixnum#**
|
|
|
|
#assert_raise(TypeError) { 1 ** nil }
|
|
|
|
assert_raise(TypeError, NoMethodError) { 1 ** nil }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_cmp
|
2013-12-13 04:18:05 -05:00
|
|
|
assert_operator(1, :!=, nil)
|
2008-01-31 09:08:14 -05:00
|
|
|
|
|
|
|
assert_equal(0, 1 <=> 1)
|
|
|
|
assert_equal(-1, 1 <=> 4294967296)
|
2016-03-19 08:53:36 -04:00
|
|
|
assert_equal(-1, 1 <=> 1 << 100)
|
2008-01-31 09:08:14 -05:00
|
|
|
assert_equal(0, 1 <=> 1.0)
|
|
|
|
assert_nil(1 <=> nil)
|
|
|
|
|
2013-12-13 04:18:05 -05:00
|
|
|
assert_operator(1, :>, 0)
|
|
|
|
assert_not_operator(1, :>, 1)
|
|
|
|
assert_not_operator(1, :>, 2)
|
|
|
|
assert_not_operator(1, :>, 4294967296)
|
|
|
|
assert_operator(1, :>, 0.0)
|
|
|
|
assert_raise(ArgumentError) { 1 > nil }
|
|
|
|
|
|
|
|
assert_operator(1, :>=, 0)
|
|
|
|
assert_operator(1, :>=, 1)
|
|
|
|
assert_not_operator(1, :>=, 2)
|
|
|
|
assert_not_operator(1, :>=, 4294967296)
|
|
|
|
assert_operator(1, :>=, 0.0)
|
|
|
|
assert_raise(ArgumentError) { 1 >= nil }
|
|
|
|
|
|
|
|
assert_not_operator(1, :<, 0)
|
|
|
|
assert_not_operator(1, :<, 1)
|
|
|
|
assert_operator(1, :<, 2)
|
|
|
|
assert_operator(1, :<, 4294967296)
|
|
|
|
assert_not_operator(1, :<, 0.0)
|
|
|
|
assert_raise(ArgumentError) { 1 < nil }
|
|
|
|
|
|
|
|
assert_not_operator(1, :<=, 0)
|
|
|
|
assert_operator(1, :<=, 1)
|
|
|
|
assert_operator(1, :<=, 2)
|
|
|
|
assert_operator(1, :<=, 4294967296)
|
|
|
|
assert_not_operator(1, :<=, 0.0)
|
|
|
|
assert_raise(ArgumentError) { 1 <= nil }
|
2008-01-31 09:08:14 -05:00
|
|
|
end
|
2011-08-28 21:07:15 -04:00
|
|
|
|
|
|
|
class DummyNumeric < Numeric
|
|
|
|
def to_int
|
|
|
|
1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_and_with_float
|
|
|
|
assert_raise(TypeError) { 1 & 1.5 }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_and_with_rational
|
|
|
|
assert_raise(TypeError, "#1792") { 1 & Rational(3, 2) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_and_with_nonintegral_numeric
|
|
|
|
assert_raise(TypeError, "#1792") { 1 & DummyNumeric.new }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_or_with_float
|
|
|
|
assert_raise(TypeError) { 1 | 1.5 }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_or_with_rational
|
|
|
|
assert_raise(TypeError, "#1792") { 1 | Rational(3, 2) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_or_with_nonintegral_numeric
|
|
|
|
assert_raise(TypeError, "#1792") { 1 | DummyNumeric.new }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_xor_with_float
|
|
|
|
assert_raise(TypeError) { 1 ^ 1.5 }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_xor_with_rational
|
|
|
|
assert_raise(TypeError, "#1792") { 1 ^ Rational(3, 2) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_xor_with_nonintegral_numeric
|
|
|
|
assert_raise(TypeError, "#1792") { 1 ^ DummyNumeric.new }
|
|
|
|
end
|
2012-08-23 07:51:30 -04:00
|
|
|
|
|
|
|
def test_singleton_method
|
|
|
|
assert_raise(TypeError) { a = 1; def a.foo; end }
|
|
|
|
end
|
2012-10-27 20:03:20 -04:00
|
|
|
|
|
|
|
def test_frozen
|
|
|
|
assert_equal(true, 1.frozen?)
|
|
|
|
end
|
2013-02-05 00:39:49 -05:00
|
|
|
|
|
|
|
def assert_eql(a, b, mess)
|
|
|
|
assert a.eql?(b), "expected #{a} & #{b} to be eql? #{mess}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_power_of_1_and_minus_1
|
|
|
|
bug5715 = '[ruby-core:41498]'
|
|
|
|
big = 1 << 66
|
|
|
|
assert_eql 1, 1 ** -big , bug5715
|
|
|
|
assert_eql 1, (-1) ** -big , bug5715
|
2016-02-19 02:48:02 -05:00
|
|
|
assert_eql (-1), (-1) ** -(big+1), bug5715
|
2013-02-05 00:39:49 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_power_of_0
|
|
|
|
bug5713 = '[ruby-core:41494]'
|
|
|
|
big = 1 << 66
|
|
|
|
assert_raise(ZeroDivisionError, bug5713) { 0 ** -big }
|
|
|
|
assert_raise(ZeroDivisionError, bug5713) { 0 ** Rational(-2,3) }
|
|
|
|
end
|
2014-08-01 03:35:34 -04:00
|
|
|
|
|
|
|
def test_remainder
|
|
|
|
assert_equal(1, 5.remainder(4))
|
|
|
|
assert_predicate(4.remainder(Float::NAN), :nan?)
|
|
|
|
end
|
2015-05-17 02:01:47 -04:00
|
|
|
|
|
|
|
def test_zero_p
|
|
|
|
assert_predicate(0, :zero?)
|
|
|
|
assert_not_predicate(1, :zero?)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_positive_p
|
|
|
|
assert_predicate(1, :positive?)
|
|
|
|
assert_not_predicate(0, :positive?)
|
|
|
|
assert_not_predicate(-1, :positive?)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_negative_p
|
|
|
|
assert_predicate(-1, :negative?)
|
|
|
|
assert_not_predicate(0, :negative?)
|
|
|
|
assert_not_predicate(1, :negative?)
|
|
|
|
end
|
2016-07-17 10:53:00 -04:00
|
|
|
|
|
|
|
def test_finite_p
|
|
|
|
assert_predicate(1, :finite?)
|
|
|
|
assert_predicate(0, :finite?)
|
|
|
|
assert_predicate(-1, :finite?)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_infinite_p
|
|
|
|
assert_nil(1.infinite?)
|
|
|
|
assert_nil(0.infinite?)
|
|
|
|
assert_nil(-1.infinite?)
|
|
|
|
end
|
2007-07-05 03:38:41 -04:00
|
|
|
end
|