2015-12-16 00:07:31 -05:00
|
|
|
# frozen_string_literal: false
|
2011-11-13 22:45:47 -05:00
|
|
|
require 'test/unit'
|
2016-01-10 04:43:47 -05:00
|
|
|
require '-test-/num2int'
|
2016-05-22 03:27:02 -04:00
|
|
|
require '-test-/integer'
|
2017-01-30 05:12:18 -05:00
|
|
|
require 'rbconfig/sizeof'
|
2011-11-13 22:45:47 -05:00
|
|
|
|
|
|
|
class TestNum2int < Test::Unit::TestCase
|
2017-04-05 22:10:40 -04:00
|
|
|
l = RbConfig::LIMITS
|
2017-01-30 05:12:18 -05:00
|
|
|
|
|
|
|
SHRT_MIN = l["SHRT_MIN"]
|
|
|
|
SHRT_MAX = l["SHRT_MAX"]
|
|
|
|
USHRT_MAX = l["USHRT_MAX"]
|
|
|
|
|
|
|
|
INT_MIN = l["INT_MIN"]
|
|
|
|
INT_MAX = l["INT_MAX"]
|
|
|
|
UINT_MAX = l["UINT_MAX"]
|
|
|
|
|
|
|
|
LONG_MAX = l["LONG_MAX"]
|
|
|
|
LONG_MIN = l["LONG_MIN"]
|
|
|
|
ULONG_MAX = l["ULONG_MAX"]
|
2011-11-13 22:45:47 -05:00
|
|
|
|
2017-01-30 05:12:18 -05:00
|
|
|
LLONG_MAX = l["LLONG_MAX"]
|
|
|
|
LLONG_MIN = l["LLONG_MIN"]
|
|
|
|
ULLONG_MAX = l["ULLONG_MAX"]
|
2011-11-13 22:47:21 -05:00
|
|
|
|
2017-01-30 05:12:18 -05:00
|
|
|
FIXNUM_MAX = l["FIXNUM_MAX"]
|
|
|
|
FIXNUM_MIN = l["FIXNUM_MIN"]
|
2011-11-13 22:45:47 -05:00
|
|
|
|
2013-04-01 08:22:56 -04:00
|
|
|
def fix2big(n)
|
|
|
|
10000000000000000000000000000.coerce(n)[0]
|
|
|
|
end
|
|
|
|
|
2013-03-28 05:23:51 -04:00
|
|
|
def assert_num2i_success_internal(exp, func, arg)
|
|
|
|
mesg = "#{func}(#{arg.inspect})"
|
2013-04-01 07:58:55 -04:00
|
|
|
out = nil
|
2013-03-28 05:23:51 -04:00
|
|
|
assert_nothing_raised(mesg) {
|
2013-04-02 06:26:02 -04:00
|
|
|
out = Num2int.send(func, arg)
|
2013-03-28 05:23:51 -04:00
|
|
|
}
|
|
|
|
assert_equal(exp, out, mesg)
|
|
|
|
end
|
|
|
|
|
2013-03-27 13:13:57 -04:00
|
|
|
def assert_num2i_success(type, num, result=num)
|
2013-03-28 05:23:51 -04:00
|
|
|
func = "NUM2#{type}".upcase
|
|
|
|
assert_num2i_success_internal(result.to_s, func, num)
|
2013-04-01 08:22:56 -04:00
|
|
|
assert_num2i_success_internal(result.to_s, func, fix2big(num))
|
2013-04-01 18:41:13 -04:00
|
|
|
assert_num2i_success_internal(result.to_s, func, Rational(num, 1))
|
2013-03-27 09:12:27 -04:00
|
|
|
if num.to_f.to_i == num
|
2013-03-28 05:23:51 -04:00
|
|
|
assert_num2i_success_internal(result.to_s, func, num.to_f)
|
2013-03-27 09:12:27 -04:00
|
|
|
end
|
2013-03-31 03:51:35 -04:00
|
|
|
# The conversion functions such as NUM2INT uses (conceptually) to_int.
|
2013-04-01 18:41:13 -04:00
|
|
|
if (arg = num.to_f + 0.5) != num.to_f && arg.to_int == num
|
2013-03-31 03:51:35 -04:00
|
|
|
assert_num2i_success_internal(result.to_s, func, arg)
|
|
|
|
end
|
2013-04-01 18:41:13 -04:00
|
|
|
if (arg = num.to_f - 0.5) != num.to_f && arg.to_int == num
|
|
|
|
assert_num2i_success_internal(result.to_s, func, arg)
|
|
|
|
end
|
|
|
|
if (arg = num + Rational(1,2)) && arg.to_int == num
|
|
|
|
assert_num2i_success_internal(result.to_s, func, arg)
|
|
|
|
end
|
|
|
|
if (arg = num - Rational(1,2)) && arg.to_int == num
|
2013-03-31 03:51:35 -04:00
|
|
|
assert_num2i_success_internal(result.to_s, func, arg)
|
2013-03-31 00:51:29 -04:00
|
|
|
end
|
2013-03-27 08:01:40 -04:00
|
|
|
end
|
|
|
|
|
2013-03-28 05:23:51 -04:00
|
|
|
def assert_num2i_error_internal(func, arg)
|
|
|
|
assert_raise(RangeError, "#{func}(#{arg.inspect})") {
|
2013-04-02 06:26:02 -04:00
|
|
|
Num2int.send(func, arg)
|
2013-03-28 05:23:51 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2013-03-27 13:13:57 -04:00
|
|
|
def assert_num2i_error(type, num)
|
2013-03-28 05:23:51 -04:00
|
|
|
func = "NUM2#{type}".upcase
|
|
|
|
assert_num2i_error_internal(func, num)
|
2013-04-01 08:22:56 -04:00
|
|
|
assert_num2i_error_internal(func, fix2big(num))
|
2013-04-01 18:41:13 -04:00
|
|
|
assert_num2i_error_internal(func, Rational(num, 1))
|
2013-03-27 09:12:27 -04:00
|
|
|
if num.to_f.to_i == num
|
2013-03-28 05:23:51 -04:00
|
|
|
assert_num2i_error_internal(func, num.to_f)
|
2013-03-27 09:12:27 -04:00
|
|
|
end
|
2013-03-31 03:51:35 -04:00
|
|
|
# The conversion functions such as NUM2INT uses (conceptually) to_int.
|
2013-04-01 18:41:13 -04:00
|
|
|
if (arg = num.to_f + 0.5) != num.to_f && arg.to_int == num
|
|
|
|
assert_num2i_error_internal(func, arg)
|
|
|
|
end
|
|
|
|
if (arg = num.to_f - 0.5) != num.to_f && arg.to_int == num
|
|
|
|
assert_num2i_error_internal(func, arg)
|
|
|
|
end
|
|
|
|
if (arg = num + Rational(1,2)) && arg.to_int == num
|
2013-03-31 03:51:35 -04:00
|
|
|
assert_num2i_error_internal(func, arg)
|
|
|
|
end
|
2013-04-01 18:41:13 -04:00
|
|
|
if (arg = num - Rational(1,2)) && arg.to_int == num
|
2013-03-31 03:51:35 -04:00
|
|
|
assert_num2i_error_internal(func, arg)
|
|
|
|
end
|
2011-11-13 22:54:34 -05:00
|
|
|
end
|
|
|
|
|
2013-03-31 12:14:15 -04:00
|
|
|
def assert_fix2i_success_internal(exp, func, arg)
|
|
|
|
mesg = "#{func}(#{arg.inspect})"
|
2013-04-01 07:58:55 -04:00
|
|
|
out = nil
|
2013-03-31 12:14:15 -04:00
|
|
|
assert_nothing_raised(mesg) {
|
2013-04-02 06:26:02 -04:00
|
|
|
out = Num2int.send(func, arg)
|
2013-03-31 12:14:15 -04:00
|
|
|
}
|
|
|
|
assert_equal(exp, out, mesg)
|
|
|
|
end
|
|
|
|
|
|
|
|
def assert_fix2i_success(type, num, result=num)
|
2016-05-17 02:53:48 -04:00
|
|
|
return if !num.fixnum?
|
2013-03-31 12:14:15 -04:00
|
|
|
func = "FIX2#{type}".upcase
|
|
|
|
assert_fix2i_success_internal(result.to_s, func, num)
|
|
|
|
end
|
|
|
|
|
|
|
|
def assert_fix2i_error_internal(func, arg)
|
|
|
|
assert_raise(RangeError, "#{func}(#{arg.inspect})") {
|
2013-04-02 06:26:02 -04:00
|
|
|
Num2int.send(func, arg)
|
2013-03-31 12:14:15 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def assert_fix2i_error(type, num)
|
2016-05-17 02:53:48 -04:00
|
|
|
return if !num.fixnum?
|
2013-03-31 12:14:15 -04:00
|
|
|
func = "FIX2#{type}".upcase
|
|
|
|
assert_num2i_error_internal(func, num)
|
|
|
|
end
|
|
|
|
|
2013-03-27 08:01:40 -04:00
|
|
|
def test_num2short
|
2013-03-27 13:13:57 -04:00
|
|
|
assert_num2i_success(:short, SHRT_MIN)
|
2013-04-21 23:13:50 -04:00
|
|
|
assert_num2i_success(:short, SHRT_MIN+1)
|
2013-03-27 13:13:57 -04:00
|
|
|
assert_num2i_success(:short, SHRT_MAX)
|
|
|
|
assert_num2i_error(:short, SHRT_MIN-1)
|
|
|
|
assert_num2i_error(:short, SHRT_MAX+1)
|
2013-03-27 08:01:40 -04:00
|
|
|
end
|
|
|
|
|
2011-11-13 22:54:34 -05:00
|
|
|
def test_num2ushort
|
2013-03-27 13:13:57 -04:00
|
|
|
assert_num2i_success(:ushort, 0)
|
|
|
|
assert_num2i_success(:ushort, USHRT_MAX)
|
|
|
|
assert_num2i_success(:ushort, -1, USHRT_MAX)
|
|
|
|
assert_num2i_success(:ushort, SHRT_MIN, SHRT_MAX+1)
|
2013-04-21 23:13:50 -04:00
|
|
|
assert_num2i_success(:ushort, SHRT_MIN+1, SHRT_MAX+2)
|
2013-03-27 13:13:57 -04:00
|
|
|
assert_num2i_error(:ushort, SHRT_MIN-1)
|
|
|
|
assert_num2i_error(:ushort, USHRT_MAX+1)
|
2011-11-13 22:54:34 -05:00
|
|
|
end
|
|
|
|
|
2011-11-13 22:45:47 -05:00
|
|
|
def test_num2int
|
2013-03-27 13:13:57 -04:00
|
|
|
assert_num2i_success(:int, INT_MIN)
|
2013-04-21 23:13:50 -04:00
|
|
|
assert_num2i_success(:int, INT_MIN+1)
|
2013-03-27 13:13:57 -04:00
|
|
|
assert_num2i_success(:int, INT_MAX)
|
|
|
|
assert_num2i_error(:int, INT_MIN-1)
|
|
|
|
assert_num2i_error(:int, INT_MAX+1)
|
2011-11-13 22:45:47 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_num2uint
|
2013-03-27 13:13:57 -04:00
|
|
|
assert_num2i_success(:uint, 0)
|
|
|
|
assert_num2i_success(:uint, UINT_MAX)
|
|
|
|
assert_num2i_success(:uint, -1, UINT_MAX)
|
|
|
|
assert_num2i_success(:uint, INT_MIN, INT_MAX+1)
|
2013-04-21 23:13:50 -04:00
|
|
|
assert_num2i_success(:uint, INT_MIN+1, INT_MAX+2)
|
2013-03-27 13:13:57 -04:00
|
|
|
assert_num2i_error(:uint, INT_MIN-1)
|
|
|
|
assert_num2i_error(:uint, UINT_MAX+1)
|
2011-11-13 22:45:47 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_num2long
|
2013-03-27 13:13:57 -04:00
|
|
|
assert_num2i_success(:long, LONG_MIN)
|
2013-04-21 23:13:50 -04:00
|
|
|
assert_num2i_success(:long, LONG_MIN+1)
|
2013-03-27 13:13:57 -04:00
|
|
|
assert_num2i_success(:long, LONG_MAX)
|
|
|
|
assert_num2i_error(:long, LONG_MIN-1)
|
|
|
|
assert_num2i_error(:long, LONG_MAX+1)
|
|
|
|
assert_num2i_success(:long, FIXNUM_MIN)
|
2013-04-21 23:13:50 -04:00
|
|
|
assert_num2i_success(:long, FIXNUM_MIN+1)
|
2013-03-27 13:13:57 -04:00
|
|
|
assert_num2i_success(:long, FIXNUM_MIN-1)
|
|
|
|
assert_num2i_success(:long, FIXNUM_MAX)
|
|
|
|
assert_num2i_success(:long, FIXNUM_MAX+1)
|
2011-11-13 22:45:47 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_num2ulong
|
2013-03-27 13:13:57 -04:00
|
|
|
assert_num2i_success(:ulong, 0)
|
|
|
|
assert_num2i_success(:ulong, ULONG_MAX)
|
|
|
|
assert_num2i_success(:ulong, -1, ULONG_MAX)
|
|
|
|
assert_num2i_success(:ulong, LONG_MIN, LONG_MAX+1)
|
2013-04-21 23:13:50 -04:00
|
|
|
assert_num2i_success(:ulong, LONG_MIN+1, LONG_MAX+2)
|
2013-03-27 13:13:57 -04:00
|
|
|
assert_num2i_error(:ulong, LONG_MIN-1)
|
|
|
|
assert_num2i_error(:ulong, ULONG_MAX+1)
|
|
|
|
assert_num2i_success(:ulong, FIXNUM_MIN, ULONG_MAX-FIXNUM_MAX)
|
2013-04-21 23:13:50 -04:00
|
|
|
assert_num2i_success(:ulong, FIXNUM_MIN+1, ULONG_MAX-FIXNUM_MAX+1)
|
2013-03-27 13:13:57 -04:00
|
|
|
assert_num2i_success(:ulong, FIXNUM_MIN-1, ULONG_MAX-FIXNUM_MAX-1)
|
|
|
|
assert_num2i_success(:ulong, FIXNUM_MAX, FIXNUM_MAX)
|
|
|
|
assert_num2i_success(:ulong, FIXNUM_MAX+1, FIXNUM_MAX+1)
|
2011-11-13 22:45:47 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_num2ll
|
2013-03-27 13:13:57 -04:00
|
|
|
assert_num2i_success(:ll, LLONG_MIN)
|
2013-04-21 23:13:50 -04:00
|
|
|
assert_num2i_success(:ll, LLONG_MIN+1)
|
2013-03-27 13:13:57 -04:00
|
|
|
assert_num2i_success(:ll, LLONG_MAX)
|
|
|
|
assert_num2i_error(:ll, LLONG_MIN-1)
|
|
|
|
assert_num2i_error(:ll, LLONG_MAX+1)
|
|
|
|
assert_num2i_success(:ll, FIXNUM_MIN)
|
2013-04-21 23:13:50 -04:00
|
|
|
assert_num2i_success(:ll, FIXNUM_MIN+1)
|
2013-03-27 13:13:57 -04:00
|
|
|
assert_num2i_success(:ll, FIXNUM_MIN-1)
|
|
|
|
assert_num2i_success(:ll, FIXNUM_MAX)
|
|
|
|
assert_num2i_success(:ll, FIXNUM_MAX+1)
|
2013-04-02 06:26:02 -04:00
|
|
|
end if defined?(Num2int.NUM2LL)
|
2011-11-13 22:45:47 -05:00
|
|
|
|
|
|
|
def test_num2ull
|
2013-03-27 13:13:57 -04:00
|
|
|
assert_num2i_success(:ull, 0)
|
|
|
|
assert_num2i_success(:ull, ULLONG_MAX)
|
|
|
|
assert_num2i_success(:ull, -1, ULLONG_MAX)
|
|
|
|
assert_num2i_success(:ull, LLONG_MIN, LLONG_MAX+1)
|
2013-04-21 23:13:50 -04:00
|
|
|
assert_num2i_success(:ull, LLONG_MIN+1, LLONG_MAX+2)
|
2013-03-27 13:13:57 -04:00
|
|
|
assert_num2i_error(:ull, LLONG_MIN-1)
|
|
|
|
assert_num2i_error(:ull, ULLONG_MAX+1)
|
|
|
|
assert_num2i_success(:ull, FIXNUM_MIN, ULLONG_MAX-FIXNUM_MAX)
|
2013-04-21 23:13:50 -04:00
|
|
|
assert_num2i_success(:ull, FIXNUM_MIN+1, ULLONG_MAX-FIXNUM_MAX+1)
|
2013-03-27 13:13:57 -04:00
|
|
|
assert_num2i_success(:ull, FIXNUM_MIN-1, ULLONG_MAX-FIXNUM_MAX-1)
|
|
|
|
assert_num2i_success(:ull, FIXNUM_MAX)
|
|
|
|
assert_num2i_success(:ull, FIXNUM_MAX+1)
|
2013-04-02 06:26:02 -04:00
|
|
|
end if defined?(Num2int.NUM2ULL)
|
2013-03-31 12:14:15 -04:00
|
|
|
|
|
|
|
def test_fix2short
|
|
|
|
assert_fix2i_success(:short, 0)
|
|
|
|
assert_fix2i_success(:short, SHRT_MAX)
|
|
|
|
assert_fix2i_success(:short, SHRT_MIN)
|
2013-04-21 23:13:50 -04:00
|
|
|
assert_fix2i_success(:short, SHRT_MIN+1)
|
2013-03-31 12:14:15 -04:00
|
|
|
assert_fix2i_error(:short, SHRT_MAX+1)
|
|
|
|
assert_fix2i_error(:short, SHRT_MIN-1)
|
|
|
|
assert_fix2i_error(:short, FIXNUM_MAX)
|
|
|
|
assert_fix2i_error(:short, FIXNUM_MIN)
|
2013-04-21 23:13:50 -04:00
|
|
|
assert_fix2i_error(:short, FIXNUM_MIN+1)
|
2013-03-31 12:14:15 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_fix2int
|
|
|
|
assert_fix2i_success(:int, 0)
|
|
|
|
assert_fix2i_success(:int, INT_MAX)
|
|
|
|
assert_fix2i_success(:int, INT_MIN)
|
2013-04-21 23:13:50 -04:00
|
|
|
assert_fix2i_success(:int, INT_MIN+1)
|
2013-03-31 12:14:15 -04:00
|
|
|
assert_fix2i_error(:int, INT_MAX+1)
|
|
|
|
assert_fix2i_error(:int, INT_MIN-1)
|
|
|
|
assert_fix2i_error(:int, FIXNUM_MAX) if INT_MAX < FIXNUM_MAX
|
|
|
|
assert_fix2i_error(:int, FIXNUM_MIN) if FIXNUM_MIN < INT_MIN
|
2013-04-21 23:13:50 -04:00
|
|
|
assert_fix2i_error(:int, FIXNUM_MIN+1) if FIXNUM_MIN+1 < INT_MIN
|
2013-03-31 12:14:15 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_fix2uint
|
|
|
|
assert_fix2i_success(:uint, 0)
|
|
|
|
assert_fix2i_success(:uint, UINT_MAX)
|
|
|
|
assert_fix2i_success(:uint, INT_MAX)
|
|
|
|
assert_fix2i_success(:uint, INT_MIN, INT_MAX+1)
|
2013-04-21 23:13:50 -04:00
|
|
|
assert_fix2i_success(:uint, INT_MIN+1, INT_MAX+2)
|
2013-03-31 12:14:15 -04:00
|
|
|
assert_fix2i_error(:uint, UINT_MAX+1)
|
|
|
|
assert_fix2i_error(:uint, INT_MIN-1)
|
|
|
|
assert_fix2i_error(:uint, FIXNUM_MAX) if UINT_MAX < FIXNUM_MAX
|
|
|
|
assert_fix2i_error(:uint, FIXNUM_MIN) if FIXNUM_MIN < INT_MIN
|
2013-04-21 23:13:50 -04:00
|
|
|
assert_fix2i_error(:uint, FIXNUM_MIN+1) if FIXNUM_MIN+1 < INT_MIN
|
2013-03-31 12:14:15 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_fix2long
|
|
|
|
assert_fix2i_success(:long, 0)
|
|
|
|
assert_fix2i_success(:long, FIXNUM_MAX)
|
|
|
|
assert_fix2i_success(:long, FIXNUM_MIN)
|
2013-04-21 23:13:50 -04:00
|
|
|
assert_fix2i_success(:long, FIXNUM_MIN+1)
|
2013-03-31 12:14:15 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_fix2ulong
|
|
|
|
assert_fix2i_success(:ulong, 0)
|
|
|
|
assert_fix2i_success(:ulong, FIXNUM_MAX)
|
|
|
|
assert_fix2i_success(:ulong, -1, ULONG_MAX)
|
|
|
|
end
|
|
|
|
|
2011-11-13 22:45:47 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
|