2015-12-16 00:07:31 -05:00
|
|
|
# frozen_string_literal: false
|
2008-02-03 07:14:30 -05:00
|
|
|
require 'test/unit'
|
|
|
|
|
|
|
|
class TestNumeric < Test::Unit::TestCase
|
|
|
|
def test_coerce
|
|
|
|
a, b = 1.coerce(2)
|
2016-05-21 14:09:05 -04:00
|
|
|
assert_kind_of(Integer, a)
|
|
|
|
assert_kind_of(Integer, b)
|
2008-02-03 07:14:30 -05:00
|
|
|
|
|
|
|
a, b = 1.coerce(2.0)
|
|
|
|
assert_equal(Float, a.class)
|
|
|
|
assert_equal(Float, b.class)
|
|
|
|
|
|
|
|
assert_raise(TypeError) { -Numeric.new }
|
2014-01-15 03:16:34 -05:00
|
|
|
|
|
|
|
assert_raise_with_message(TypeError, /can't be coerced into /) {1+:foo}
|
|
|
|
assert_raise_with_message(TypeError, /can't be coerced into /) {1&:foo}
|
|
|
|
assert_raise_with_message(TypeError, /can't be coerced into /) {1|:foo}
|
|
|
|
assert_raise_with_message(TypeError, /can't be coerced into /) {1^:foo}
|
2014-01-15 03:16:37 -05:00
|
|
|
|
2016-04-08 21:25:11 -04:00
|
|
|
assert_raise_with_message(TypeError, /:\u{3042}/) {1+:"\u{3042}"}
|
|
|
|
assert_raise_with_message(TypeError, /:\u{3042}/) {1&:"\u{3042}"}
|
|
|
|
assert_raise_with_message(TypeError, /:\u{3042}/) {1|:"\u{3042}"}
|
|
|
|
assert_raise_with_message(TypeError, /:\u{3042}/) {1^:"\u{3042}"}
|
2017-12-12 18:41:34 -05:00
|
|
|
assert_raise_with_message(TypeError, /:"\\u3042"/) {1+:"\u{3042}"}
|
|
|
|
assert_raise_with_message(TypeError, /:"\\u3042"/) {1&:"\u{3042}"}
|
|
|
|
assert_raise_with_message(TypeError, /:"\\u3042"/) {1|:"\u{3042}"}
|
|
|
|
assert_raise_with_message(TypeError, /:"\\u3042"/) {1^:"\u{3042}"}
|
2015-01-12 04:56:14 -05:00
|
|
|
|
|
|
|
bug10711 = '[ruby-core:67405] [Bug #10711]'
|
2016-05-17 02:53:48 -04:00
|
|
|
exp = "1.2 can't be coerced into Integer"
|
2015-01-12 04:56:14 -05:00
|
|
|
assert_raise_with_message(TypeError, exp, bug10711) { 1 & 1.2 }
|
2008-02-03 07:14:30 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_dummynumeric
|
2015-05-17 01:59:58 -04:00
|
|
|
a = Class.new(Numeric) do
|
2008-02-03 07:14:30 -05:00
|
|
|
def coerce(x); nil; end
|
2015-05-17 01:59:58 -04:00
|
|
|
end.new
|
2008-02-03 07:14:30 -05:00
|
|
|
assert_raise(TypeError) { -a }
|
|
|
|
assert_nil(1 <=> a)
|
|
|
|
assert_raise(ArgumentError) { 1 <= a }
|
|
|
|
|
2015-05-17 01:59:58 -04:00
|
|
|
a = Class.new(Numeric) do
|
2008-02-03 07:14:30 -05:00
|
|
|
def coerce(x); 1.coerce(x); end
|
2015-05-17 01:59:58 -04:00
|
|
|
end.new
|
2008-02-03 07:14:30 -05:00
|
|
|
assert_equal(2, 1 + a)
|
|
|
|
assert_equal(0, 1 <=> a)
|
2013-12-13 04:18:05 -05:00
|
|
|
assert_operator(1, :<=, a)
|
2008-02-03 07:14:30 -05:00
|
|
|
|
2015-05-17 01:59:58 -04:00
|
|
|
a = Class.new(Numeric) do
|
2008-02-03 07:14:30 -05:00
|
|
|
def coerce(x); [x, 1]; end
|
2015-05-17 01:59:58 -04:00
|
|
|
end.new
|
2008-02-03 07:14:30 -05:00
|
|
|
assert_equal(-1, -a)
|
|
|
|
|
2014-06-07 09:16:01 -04:00
|
|
|
bug7688 = '[ruby-core:51389] [Bug #7688]'
|
2015-05-17 01:59:58 -04:00
|
|
|
a = Class.new(Numeric) do
|
2017-04-25 07:42:20 -04:00
|
|
|
def coerce(x); raise StandardError, "my error"; end
|
2015-05-17 01:59:58 -04:00
|
|
|
end.new
|
2017-04-25 07:42:20 -04:00
|
|
|
assert_raise_with_message(StandardError, "my error") { 1 + a }
|
|
|
|
assert_raise_with_message(StandardError, "my error") { 1 < a }
|
2014-06-07 09:16:01 -04:00
|
|
|
|
2015-05-17 01:59:58 -04:00
|
|
|
a = Class.new(Numeric) do
|
2014-06-07 09:16:01 -04:00
|
|
|
def coerce(x); :bad_return_value; end
|
2015-05-17 01:59:58 -04:00
|
|
|
end.new
|
2014-06-07 09:16:01 -04:00
|
|
|
assert_raise_with_message(TypeError, "coerce must return [x, y]") { 1 + a }
|
2017-04-25 07:42:20 -04:00
|
|
|
assert_raise_with_message(TypeError, "coerce must return [x, y]") { 1 < a }
|
2008-02-03 07:14:30 -05:00
|
|
|
end
|
|
|
|
|
2014-01-15 03:16:42 -05:00
|
|
|
def test_singleton_method
|
2008-02-03 07:14:30 -05:00
|
|
|
a = Numeric.new
|
2014-01-15 03:16:40 -05:00
|
|
|
assert_raise_with_message(TypeError, /foo/) { def a.foo; end }
|
|
|
|
assert_raise_with_message(TypeError, /\u3042/) { eval("def a.\u3042; end") }
|
2014-01-15 03:16:42 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_dup
|
|
|
|
a = Numeric.new
|
2017-02-21 21:02:11 -05:00
|
|
|
assert_same a, a.dup
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_clone
|
|
|
|
a = Numeric.new
|
|
|
|
assert_same a, a.clone
|
|
|
|
assert_raise(ArgumentError) {a.clone(freeze: false)}
|
2014-01-15 03:16:42 -05:00
|
|
|
|
2017-02-21 21:02:11 -05:00
|
|
|
c = EnvUtil.labeled_class("\u{1f4a9}", Numeric)
|
|
|
|
assert_raise_with_message(ArgumentError, /\u{1f4a9}/) do
|
|
|
|
c.new.clone(freeze: false)
|
2014-01-15 03:16:42 -05:00
|
|
|
end
|
2008-02-03 07:14:30 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_quo
|
2015-05-17 01:59:58 -04:00
|
|
|
a = Numeric.new
|
|
|
|
assert_raise(TypeError) {a.quo(1)}
|
2013-06-06 09:31:22 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_quo_ruby_core_41575
|
|
|
|
rat = 84.quo(1)
|
2015-05-17 01:59:58 -04:00
|
|
|
x = Class.new(Numeric) do
|
2013-06-06 09:31:22 -04:00
|
|
|
define_method(:to_r) { rat }
|
2015-05-17 01:59:58 -04:00
|
|
|
end.new
|
2013-06-06 09:31:22 -04:00
|
|
|
assert_equal(2.quo(1), x.quo(42), '[ruby-core:41575]')
|
2008-02-03 07:14:30 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_divmod
|
2009-06-20 08:37:13 -04:00
|
|
|
=begin
|
2015-05-17 01:59:58 -04:00
|
|
|
x = Class.new(Numeric) do
|
2008-02-03 07:14:30 -05:00
|
|
|
def /(x); 42.0; end
|
|
|
|
def %(x); :mod; end
|
2015-05-17 01:59:58 -04:00
|
|
|
end.new
|
2008-02-03 07:14:30 -05:00
|
|
|
|
2015-05-17 01:59:58 -04:00
|
|
|
assert_equal(42, x.div(1))
|
|
|
|
assert_equal(:mod, x.modulo(1))
|
|
|
|
assert_equal([42, :mod], x.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]')
|
2008-02-03 07:14:30 -05:00
|
|
|
end
|
|
|
|
|
2008-09-16 18:04:19 -04:00
|
|
|
def test_real_p
|
2013-12-13 04:18:05 -05:00
|
|
|
assert_predicate(Numeric.new, :real?)
|
2008-02-03 07:14:30 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_integer_p
|
2013-12-13 04:18:05 -05:00
|
|
|
assert_not_predicate(Numeric.new, :integer?)
|
2008-02-03 07:14:30 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_abs
|
2015-05-17 01:59:58 -04:00
|
|
|
a = Class.new(Numeric) do
|
2008-02-03 07:14:30 -05:00
|
|
|
def -@; :ok; end
|
|
|
|
def <(x); true; end
|
2015-05-17 01:59:58 -04:00
|
|
|
end.new
|
2008-02-03 07:14:30 -05:00
|
|
|
|
|
|
|
assert_equal(:ok, a.abs)
|
|
|
|
|
2015-05-17 01:59:58 -04:00
|
|
|
a = Class.new(Numeric) do
|
2008-02-03 07:14:30 -05:00
|
|
|
def <(x); false; end
|
2015-05-17 01:59:58 -04:00
|
|
|
end.new
|
2008-02-03 07:14:30 -05:00
|
|
|
|
|
|
|
assert_equal(a, a.abs)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_zero_p
|
2015-05-17 01:59:58 -04:00
|
|
|
a = Class.new(Numeric) do
|
2008-02-03 07:14:30 -05:00
|
|
|
def ==(x); true; end
|
2015-05-17 01:59:58 -04:00
|
|
|
end.new
|
2008-02-03 07:14:30 -05:00
|
|
|
|
2015-05-17 01:59:58 -04:00
|
|
|
assert_predicate(a, :zero?)
|
2008-02-03 07:14:30 -05:00
|
|
|
end
|
|
|
|
|
2016-01-05 07:32:43 -05:00
|
|
|
def test_nonzero_p
|
|
|
|
a = Class.new(Numeric) do
|
|
|
|
def zero?; true; end
|
|
|
|
end.new
|
|
|
|
assert_nil(a.nonzero?)
|
|
|
|
|
|
|
|
a = Class.new(Numeric) do
|
|
|
|
def zero?; false; end
|
|
|
|
end.new
|
|
|
|
assert_equal(a, a.nonzero?)
|
|
|
|
end
|
|
|
|
|
2015-05-17 02:01:47 -04:00
|
|
|
def test_positive_p
|
|
|
|
a = Class.new(Numeric) do
|
|
|
|
def >(x); true; end
|
|
|
|
end.new
|
|
|
|
assert_predicate(a, :positive?)
|
|
|
|
|
|
|
|
a = Class.new(Numeric) do
|
|
|
|
def >(x); false; end
|
|
|
|
end.new
|
|
|
|
assert_not_predicate(a, :positive?)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_negative_p
|
|
|
|
a = Class.new(Numeric) do
|
|
|
|
def <(x); true; end
|
|
|
|
end.new
|
|
|
|
assert_predicate(a, :negative?)
|
|
|
|
|
|
|
|
a = Class.new(Numeric) do
|
|
|
|
def <(x); false; end
|
|
|
|
end.new
|
|
|
|
assert_not_predicate(a, :negative?)
|
|
|
|
end
|
|
|
|
|
2008-02-03 07:14:30 -05:00
|
|
|
def test_to_int
|
2015-05-17 01:59:58 -04:00
|
|
|
a = Class.new(Numeric) do
|
2008-02-03 07:14:30 -05:00
|
|
|
def to_i; :ok; end
|
2015-05-17 01:59:58 -04:00
|
|
|
end.new
|
2008-02-03 07:14:30 -05:00
|
|
|
|
2015-05-17 01:59:58 -04:00
|
|
|
assert_equal(:ok, a.to_int)
|
2008-02-03 07:14:30 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_cmp
|
|
|
|
a = Numeric.new
|
|
|
|
assert_equal(0, a <=> a)
|
|
|
|
assert_nil(a <=> :foo)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_floor_ceil_round_truncate
|
2015-05-17 01:59:58 -04:00
|
|
|
a = Class.new(Numeric) do
|
2008-02-03 07:14:30 -05:00
|
|
|
def to_f; 1.5; end
|
2015-05-17 01:59:58 -04:00
|
|
|
end.new
|
2008-02-03 07:14:30 -05:00
|
|
|
|
|
|
|
assert_equal(1, a.floor)
|
|
|
|
assert_equal(2, a.ceil)
|
|
|
|
assert_equal(2, a.round)
|
|
|
|
assert_equal(1, a.truncate)
|
|
|
|
|
2015-05-17 01:59:58 -04:00
|
|
|
a = Class.new(Numeric) do
|
2008-02-03 07:14:30 -05:00
|
|
|
def to_f; 1.4; end
|
2015-05-17 01:59:58 -04:00
|
|
|
end.new
|
2008-02-03 07:14:30 -05:00
|
|
|
|
|
|
|
assert_equal(1, a.floor)
|
|
|
|
assert_equal(2, a.ceil)
|
|
|
|
assert_equal(1, a.round)
|
|
|
|
assert_equal(1, a.truncate)
|
|
|
|
|
2015-05-17 01:59:58 -04:00
|
|
|
a = Class.new(Numeric) do
|
2008-02-03 07:14:30 -05:00
|
|
|
def to_f; -1.5; end
|
2015-05-17 01:59:58 -04:00
|
|
|
end.new
|
2008-02-03 07:14:30 -05:00
|
|
|
|
|
|
|
assert_equal(-2, a.floor)
|
|
|
|
assert_equal(-1, a.ceil)
|
|
|
|
assert_equal(-2, a.round)
|
|
|
|
assert_equal(-1, a.truncate)
|
|
|
|
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
|
2017-04-05 22:10:40 -04:00
|
|
|
bignum = RbConfig::LIMITS['FIXNUM_MAX'] + 1
|
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 }
|
2017-04-25 07:42:31 -04:00
|
|
|
assert_raise(ArgumentError) { 1.step(10, "1") { } }
|
|
|
|
assert_raise(ArgumentError) { 1.step(10, "1").size }
|
2013-09-02 18:54:58 -04:00
|
|
|
assert_raise(TypeError) { 1.step(10, nil) { } }
|
|
|
|
assert_raise(TypeError) { 1.step(10, nil).size }
|
2013-09-02 18:39:24 -04:00
|
|
|
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
|
|
|
|
2014-05-07 04:24:09 -04:00
|
|
|
bug9811 = '[ruby-dev:48177] [Bug #9811]'
|
|
|
|
assert_raise(ArgumentError, bug9811) { 1.step(10, foo: nil) {} }
|
|
|
|
assert_raise(ArgumentError, bug9811) { 1.step(10, foo: nil).size }
|
|
|
|
assert_raise(ArgumentError, bug9811) { 1.step(10, to: 11) {} }
|
|
|
|
assert_raise(ArgumentError, bug9811) { 1.step(10, to: 11).size }
|
|
|
|
assert_raise(ArgumentError, bug9811) { 1.step(10, 1, by: 11) {} }
|
|
|
|
assert_raise(ArgumentError, bug9811) { 1.step(10, 1, by: 11).size }
|
|
|
|
|
2014-02-27 23:59:49 -05:00
|
|
|
assert_equal(bignum*2+1, (-bignum).step(bignum, 1).size)
|
|
|
|
assert_equal(bignum*2, (-bignum).step(bignum-1, 1).size)
|
|
|
|
|
2014-02-28 00:11:44 -05:00
|
|
|
assert_equal(10+1, (0.0).step(10.0, 1.0).size)
|
|
|
|
|
|
|
|
i, bigflo = 1, bignum.to_f
|
|
|
|
i <<= 1 until (bigflo - i).to_i < bignum
|
|
|
|
bigflo -= i >> 1
|
|
|
|
assert_equal(bigflo.to_i, (0.0).step(bigflo-1.0, 1.0).size)
|
|
|
|
assert_operator((0.0).step(bignum.to_f, 1.0).size, :>=, bignum) # may loose precision
|
|
|
|
|
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]
|
2014-02-27 21:04:03 -05:00
|
|
|
assert_step [1], [1, 10, bignum]
|
|
|
|
assert_step [1], [1, to: 10, by: bignum]
|
2013-09-02 10:56:06 -04:00
|
|
|
|
2014-02-26 22:10:12 -05:00
|
|
|
assert_step [], [2, 1, 3]
|
|
|
|
assert_step [], [-2, -1, -3]
|
2013-09-02 10:56:06 -04:00
|
|
|
assert_step [3, 3, 3, 3], [3, by: 0], inf: true
|
2014-02-27 21:04:59 -05:00
|
|
|
assert_step [3, 3, 3, 3], [3, by: 0, to: 42], inf: true
|
2014-02-27 21:04:03 -05:00
|
|
|
assert_step [10], [10, 1, -bignum]
|
2013-09-02 10:56:06 -04:00
|
|
|
|
|
|
|
assert_step [], [1, 0, Float::INFINITY]
|
|
|
|
assert_step [], [0, 1, -Float::INFINITY]
|
2014-02-27 21:04:03 -05:00
|
|
|
assert_step [10], [10, to: 1, by: -bignum]
|
2013-09-02 10:56:06 -04:00
|
|
|
|
|
|
|
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
|
2014-02-27 21:04:59 -05:00
|
|
|
|
|
|
|
assert_step [42, 42, 42, 42], [42, by: 0, to: -Float::INFINITY], inf: true
|
|
|
|
assert_step [42, 42, 42, 42], [42, by: 0, to: 42.5], inf: true
|
|
|
|
assert_step [4.2, 4.2, 4.2, 4.2], [4.2, by: 0.0], inf: true
|
|
|
|
assert_step [4.2, 4.2, 4.2, 4.2], [4.2, by: -0.0], inf: true
|
|
|
|
assert_step [42.0, 42.0, 42.0, 42.0], [42, by: 0.0, to: 44], inf: true
|
|
|
|
assert_step [42.0, 42.0, 42.0, 42.0], [42, by: 0.0, to: 0], inf: true
|
|
|
|
assert_step [42.0, 42.0, 42.0, 42.0], [42, by: -0.0, to: 44], inf: true
|
|
|
|
|
|
|
|
assert_step [bignum]*4, [bignum, by: 0], inf: true
|
|
|
|
assert_step [bignum]*4, [bignum, by: 0.0], inf: true
|
|
|
|
assert_step [bignum]*4, [bignum, by: 0, to: bignum+1], inf: true
|
|
|
|
assert_step [bignum]*4, [bignum, by: 0, to: 0], 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
|
2013-12-13 04:18:05 -05:00
|
|
|
assert_equal(1, 1.0)
|
|
|
|
assert_not_operator(1, :eql?, 1.0)
|
|
|
|
assert_not_operator(1, :eql?, 2)
|
2008-02-03 07:14:30 -05:00
|
|
|
end
|
2016-02-16 04:25:08 -05:00
|
|
|
|
|
|
|
def test_coerced_remainder
|
|
|
|
assert_separately([], <<-'end;')
|
|
|
|
x = Class.new do
|
|
|
|
def coerce(a) [self, a]; end
|
|
|
|
def %(a) self; end
|
|
|
|
end.new
|
|
|
|
assert_raise(ArgumentError) {1.remainder(x)}
|
|
|
|
end;
|
|
|
|
end
|
2016-10-22 09:33:34 -04:00
|
|
|
|
|
|
|
def test_comparison_comparable
|
|
|
|
bug12864 = '[ruby-core:77713] [Bug #12864]'
|
|
|
|
|
|
|
|
myinteger = Class.new do
|
|
|
|
include Comparable
|
|
|
|
|
|
|
|
def initialize(i)
|
|
|
|
@i = i.to_i
|
|
|
|
end
|
|
|
|
attr_reader :i
|
|
|
|
|
|
|
|
def <=>(other)
|
|
|
|
@i <=> (other.is_a?(self.class) ? other.i : other)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
all_assertions(bug12864) do |a|
|
|
|
|
[5, 2**62, 2**61].each do |i|
|
|
|
|
a.for("%#x"%i) do
|
|
|
|
m = myinteger.new(i)
|
|
|
|
assert_equal(i, m)
|
|
|
|
assert_equal(m, i)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-12-03 21:35:40 -05:00
|
|
|
|
|
|
|
def test_pow
|
2017-12-06 10:02:31 -05:00
|
|
|
assert_equal(2**3, 2.pow(3))
|
|
|
|
assert_equal(2**-1, 2.pow(-1))
|
|
|
|
assert_equal(2**0.5, 2.pow(0.5))
|
|
|
|
assert_equal((-1)**0.5, -1.pow(0.5))
|
2017-12-03 21:35:40 -05:00
|
|
|
assert_equal(3**3 % 8, 3.pow(3, 8))
|
|
|
|
assert_equal(3**3 % -8, 3.pow(3,-8))
|
|
|
|
assert_equal(3**2 % -2, 3.pow(2,-2))
|
|
|
|
assert_equal((-3)**3 % 8, -3.pow(3,8))
|
|
|
|
assert_equal((-3)**3 % -8, -3.pow(3,-8))
|
|
|
|
assert_equal(5**2 % -8, 5.pow(2,-8))
|
|
|
|
assert_equal(4481650795473624846969600733813414725093,
|
|
|
|
2120078484650058507891187874713297895455.
|
|
|
|
pow(5478118174010360425845660566650432540723,
|
|
|
|
5263488859030795548286226023720904036518))
|
2017-12-30 09:50:15 -05:00
|
|
|
|
|
|
|
assert_equal(12, 12.pow(1, 10000000000), '[Bug #14259]')
|
|
|
|
assert_equal(12, 12.pow(1, 10000000001), '[Bug #14259]')
|
|
|
|
assert_equal(12, 12.pow(1, 10000000002), '[Bug #14259]')
|
|
|
|
assert_equal(17298641040, 12.pow(72387894339363242, 243682743764), '[Bug #14259]')
|
2017-12-03 21:35:40 -05:00
|
|
|
end
|
|
|
|
|
2008-02-03 07:14:30 -05:00
|
|
|
end
|