2008-02-03 07:14:30 -05:00
|
|
|
require 'test/unit'
|
|
|
|
|
|
|
|
class TestNumeric < Test::Unit::TestCase
|
|
|
|
class DummyNumeric < Numeric
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_coerce
|
|
|
|
a, b = 1.coerce(2)
|
|
|
|
assert_equal(Fixnum, a.class)
|
|
|
|
assert_equal(Fixnum, b.class)
|
|
|
|
|
|
|
|
a, b = 1.coerce(2.0)
|
|
|
|
assert_equal(Float, a.class)
|
|
|
|
assert_equal(Float, b.class)
|
|
|
|
|
|
|
|
assert_raise(TypeError) { -Numeric.new }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_dummynumeric
|
|
|
|
a = DummyNumeric.new
|
|
|
|
|
|
|
|
DummyNumeric.class_eval do
|
|
|
|
def coerce(x); nil; end
|
|
|
|
end
|
|
|
|
assert_raise(TypeError) { -a }
|
|
|
|
assert_nil(1 <=> a)
|
|
|
|
assert_raise(ArgumentError) { 1 <= a }
|
|
|
|
|
|
|
|
DummyNumeric.class_eval do
|
2010-10-13 15:17:49 -04:00
|
|
|
remove_method :coerce
|
2008-02-03 07:14:30 -05:00
|
|
|
def coerce(x); 1.coerce(x); end
|
|
|
|
end
|
|
|
|
assert_equal(2, 1 + a)
|
|
|
|
assert_equal(0, 1 <=> a)
|
|
|
|
assert(1 <= a)
|
|
|
|
|
|
|
|
DummyNumeric.class_eval do
|
2010-10-13 15:17:49 -04:00
|
|
|
remove_method :coerce
|
2008-02-03 07:14:30 -05:00
|
|
|
def coerce(x); [x, 1]; end
|
|
|
|
end
|
|
|
|
assert_equal(-1, -a)
|
|
|
|
|
|
|
|
ensure
|
|
|
|
DummyNumeric.class_eval do
|
|
|
|
remove_method :coerce
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_numeric
|
|
|
|
a = Numeric.new
|
|
|
|
assert_raise(TypeError) { def a.foo; end }
|
2009-08-27 05:31:11 -04:00
|
|
|
assert_raise(TypeError) { eval("def a.\u3042; end") }
|
2008-02-03 07:14:30 -05:00
|
|
|
assert_raise(TypeError) { a.dup }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_quo
|
2013-06-06 09:31:22 -04:00
|
|
|
assert_raise(TypeError) {DummyNumeric.new.quo(1)}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_quo_ruby_core_41575
|
|
|
|
x = DummyNumeric.new
|
|
|
|
rat = 84.quo(1)
|
|
|
|
DummyNumeric.class_eval do
|
|
|
|
define_method(:to_r) { rat }
|
|
|
|
end
|
|
|
|
assert_equal(2.quo(1), x.quo(42), '[ruby-core:41575]')
|
|
|
|
ensure
|
|
|
|
DummyNumeric.class_eval do
|
|
|
|
remove_method :to_r
|
|
|
|
end
|
2008-02-03 07:14:30 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_divmod
|
2009-06-20 08:37:13 -04:00
|
|
|
=begin
|
2008-02-03 07:14:30 -05:00
|
|
|
DummyNumeric.class_eval do
|
|
|
|
def /(x); 42.0; end
|
|
|
|
def %(x); :mod; end
|
|
|
|
end
|
|
|
|
|
2008-05-27 08:51:28 -04:00
|
|
|
assert_equal(42, DummyNumeric.new.div(1))
|
|
|
|
assert_equal(:mod, DummyNumeric.new.modulo(1))
|
|
|
|
assert_equal([42, :mod], DummyNumeric.new.divmod(1))
|
2009-06-20 08:37:13 -04:00
|
|
|
=end
|
2008-02-03 07:14:30 -05:00
|
|
|
|
2008-03-08 01:10:22 -05:00
|
|
|
assert_kind_of(Integer, 11.divmod(3.5).first, '[ruby-dev:34006]')
|
|
|
|
|
2009-06-20 08:37:13 -04:00
|
|
|
=begin
|
2008-02-03 07:14:30 -05:00
|
|
|
ensure
|
|
|
|
DummyNumeric.class_eval do
|
|
|
|
remove_method :/, :%
|
|
|
|
end
|
2009-06-20 08:37:13 -04:00
|
|
|
=end
|
2008-02-03 07:14:30 -05:00
|
|
|
end
|
|
|
|
|
2008-09-16 18:04:19 -04:00
|
|
|
def test_real_p
|
|
|
|
assert(Numeric.new.real?)
|
2008-02-03 07:14:30 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_integer_p
|
|
|
|
assert(!Numeric.new.integer?)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_abs
|
|
|
|
a = DummyNumeric.new
|
|
|
|
DummyNumeric.class_eval do
|
|
|
|
def -@; :ok; end
|
|
|
|
def <(x); true; end
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal(:ok, a.abs)
|
|
|
|
|
|
|
|
DummyNumeric.class_eval do
|
2010-10-13 15:17:49 -04:00
|
|
|
remove_method :<
|
2008-02-03 07:14:30 -05:00
|
|
|
def <(x); false; end
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal(a, a.abs)
|
|
|
|
|
|
|
|
ensure
|
|
|
|
DummyNumeric.class_eval do
|
|
|
|
remove_method :-@, :<
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_zero_p
|
|
|
|
DummyNumeric.class_eval do
|
|
|
|
def ==(x); true; end
|
|
|
|
end
|
|
|
|
|
|
|
|
assert(DummyNumeric.new.zero?)
|
|
|
|
|
|
|
|
ensure
|
|
|
|
DummyNumeric.class_eval do
|
|
|
|
remove_method :==
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_to_int
|
|
|
|
DummyNumeric.class_eval do
|
|
|
|
def to_i; :ok; end
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal(:ok, DummyNumeric.new.to_int)
|
|
|
|
|
|
|
|
ensure
|
|
|
|
DummyNumeric.class_eval do
|
|
|
|
remove_method :to_i
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_cmp
|
|
|
|
a = Numeric.new
|
|
|
|
assert_equal(0, a <=> a)
|
|
|
|
assert_nil(a <=> :foo)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_floor_ceil_round_truncate
|
|
|
|
DummyNumeric.class_eval do
|
|
|
|
def to_f; 1.5; end
|
|
|
|
end
|
|
|
|
|
|
|
|
a = DummyNumeric.new
|
|
|
|
assert_equal(1, a.floor)
|
|
|
|
assert_equal(2, a.ceil)
|
|
|
|
assert_equal(2, a.round)
|
|
|
|
assert_equal(1, a.truncate)
|
|
|
|
|
|
|
|
DummyNumeric.class_eval do
|
2010-10-13 15:17:49 -04:00
|
|
|
remove_method :to_f
|
2008-02-03 07:14:30 -05:00
|
|
|
def to_f; 1.4; end
|
|
|
|
end
|
|
|
|
|
|
|
|
a = DummyNumeric.new
|
|
|
|
assert_equal(1, a.floor)
|
|
|
|
assert_equal(2, a.ceil)
|
|
|
|
assert_equal(1, a.round)
|
|
|
|
assert_equal(1, a.truncate)
|
|
|
|
|
|
|
|
DummyNumeric.class_eval do
|
2010-10-13 15:17:49 -04:00
|
|
|
remove_method :to_f
|
2008-02-03 07:14:30 -05:00
|
|
|
def to_f; -1.5; end
|
|
|
|
end
|
|
|
|
|
|
|
|
a = DummyNumeric.new
|
|
|
|
assert_equal(-2, a.floor)
|
|
|
|
assert_equal(-1, a.ceil)
|
|
|
|
assert_equal(-2, a.round)
|
|
|
|
assert_equal(-1, a.truncate)
|
|
|
|
|
|
|
|
ensure
|
|
|
|
DummyNumeric.class_eval do
|
|
|
|
remove_method :to_f
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-09-02 10:56:06 -04:00
|
|
|
def assert_step(expected, (from, *args), inf: false)
|
|
|
|
enum = from.step(*args)
|
|
|
|
size = enum.size
|
|
|
|
xsize = expected.size
|
|
|
|
|
|
|
|
if inf
|
|
|
|
assert_send [size, :infinite?], "step size: +infinity"
|
|
|
|
assert_send [size, :>, 0], "step size: +infinity"
|
|
|
|
|
|
|
|
a = []
|
|
|
|
from.step(*args) { |x| a << x; break if a.size == xsize }
|
|
|
|
assert_equal expected, a, "step"
|
|
|
|
|
|
|
|
a = []
|
|
|
|
enum.each { |x| a << x; break if a.size == xsize }
|
|
|
|
assert_equal expected, a, "step enumerator"
|
|
|
|
else
|
|
|
|
assert_equal expected.size, size, "step size"
|
|
|
|
|
|
|
|
a = []
|
|
|
|
from.step(*args) { |x| a << x }
|
|
|
|
assert_equal expected, a, "step"
|
|
|
|
|
|
|
|
a = []
|
|
|
|
enum.each { |x| a << x }
|
|
|
|
assert_equal expected, a, "step enumerator"
|
|
|
|
end
|
|
|
|
end
|
2008-02-03 07:14:30 -05:00
|
|
|
|
2013-09-02 10:56:06 -04:00
|
|
|
def test_step
|
2008-02-03 07:14:30 -05:00
|
|
|
assert_raise(ArgumentError) { 1.step(10, 1, 0) { } }
|
2013-09-02 10:56:06 -04:00
|
|
|
assert_raise(ArgumentError) { 1.step(10, 1, 0).size }
|
2008-02-03 07:14:30 -05:00
|
|
|
assert_raise(ArgumentError) { 1.step(10, 0) { } }
|
2013-09-02 10:56:06 -04:00
|
|
|
assert_raise(ArgumentError) { 1.step(10, 0).size }
|
2013-09-02 18:39:24 -04:00
|
|
|
assert_raise(TypeError) { 1.step(10, "1") { } }
|
|
|
|
assert_raise(TypeError) { 1.step(10, "1").size }
|
|
|
|
assert_nothing_raised { 1.step(10, nil) { } }
|
|
|
|
assert_nothing_raised { 1.step(10, nil).size }
|
|
|
|
assert_nothing_raised { 1.step(by: 0, to: nil) }
|
|
|
|
assert_nothing_raised { 1.step(by: 0, to: nil).size }
|
2013-09-02 10:56:06 -04:00
|
|
|
assert_nothing_raised { 1.step(by: 0) }
|
|
|
|
assert_nothing_raised { 1.step(by: 0).size }
|
2013-09-02 18:39:24 -04:00
|
|
|
assert_nothing_raised { 1.step(by: nil) }
|
|
|
|
assert_nothing_raised { 1.step(by: nil).size }
|
2013-09-02 10:56:06 -04:00
|
|
|
|
|
|
|
assert_step [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [1, 10]
|
|
|
|
assert_step [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [1, to: 10]
|
2013-09-02 18:39:24 -04:00
|
|
|
assert_step [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [1, to: 10, by: nil]
|
2013-09-02 10:56:06 -04:00
|
|
|
assert_step [1, 3, 5, 7, 9], [1, 10, 2]
|
|
|
|
assert_step [1, 3, 5, 7, 9], [1, to: 10, by: 2]
|
|
|
|
|
|
|
|
assert_step [10, 8, 6, 4, 2], [10, 1, -2]
|
|
|
|
assert_step [10, 8, 6, 4, 2], [10, to: 1, by: -2]
|
|
|
|
assert_step [1.0, 3.0, 5.0, 7.0, 9.0], [1.0, 10.0, 2.0]
|
|
|
|
assert_step [1.0, 3.0, 5.0, 7.0, 9.0], [1.0, to: 10.0, by: 2.0]
|
|
|
|
assert_step [1], [1, 10, 2**32]
|
|
|
|
assert_step [1], [1, to: 10, by: 2**32]
|
|
|
|
|
|
|
|
assert_step [3, 3, 3, 3], [3, by: 0], inf: true
|
|
|
|
assert_step [10], [10, 1, -(2**32)]
|
|
|
|
|
|
|
|
assert_step [], [1, 0, Float::INFINITY]
|
|
|
|
assert_step [], [0, 1, -Float::INFINITY]
|
|
|
|
assert_step [10], [10, to: 1, by: -(2**32)]
|
|
|
|
|
|
|
|
assert_step [10, 11, 12, 13], [10], inf: true
|
|
|
|
assert_step [10, 9, 8, 7], [10, by: -1], inf: true
|
2013-09-02 18:39:24 -04:00
|
|
|
assert_step [10, 9, 8, 7], [10, by: -1, to: nil], inf: true
|
2008-02-03 07:14:30 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_num2long
|
|
|
|
assert_raise(TypeError) { 1 & nil }
|
2008-05-07 09:24:55 -04:00
|
|
|
assert_raise(TypeError) { 1 & 1.0 }
|
|
|
|
assert_raise(TypeError) { 1 & 2147483648.0 }
|
|
|
|
assert_raise(TypeError) { 1 & 9223372036854777856.0 }
|
2008-02-03 07:14:30 -05:00
|
|
|
o = Object.new
|
|
|
|
def o.to_int; 1; end
|
2011-08-29 10:24:31 -04:00
|
|
|
assert_raise(TypeError) { assert_equal(1, 1 & o) }
|
2008-02-03 07:14:30 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_eql
|
|
|
|
assert(1 == 1.0)
|
|
|
|
assert(!(1.eql?(1.0)))
|
|
|
|
assert(!(1.eql?(2)))
|
|
|
|
end
|
|
|
|
end
|