summaryrefslogtreecommitdiff
path: root/test/ruby/test_integer.rb
diff options
context:
space:
mode:
Diffstat (limited to 'test/ruby/test_integer.rb')
-rw-r--r--test/ruby/test_integer.rb386
1 files changed, 317 insertions, 69 deletions
diff --git a/test/ruby/test_integer.rb b/test/ruby/test_integer.rb
index 3dbf365a7c..1b485760e3 100644
--- a/test/ruby/test_integer.rb
+++ b/test/ruby/test_integer.rb
@@ -1,3 +1,4 @@
+# frozen_string_literal: false
require 'test/unit'
class TestInteger < Test::Unit::TestCase
@@ -97,6 +98,22 @@ class TestInteger < Test::Unit::TestCase
assert_raise(Encoding::CompatibilityError, bug6192) {Integer("0".encode("utf-32be"))}
assert_raise(Encoding::CompatibilityError, bug6192) {Integer("0".encode("utf-32le"))}
assert_raise(Encoding::CompatibilityError, bug6192) {Integer("0".encode("iso-2022-jp"))}
+
+ assert_raise_with_message(ArgumentError, /\u{1f4a1}/) {Integer("\u{1f4a1}")}
+
+ obj = Struct.new(:s).new(%w[42 not-an-integer])
+ def obj.to_str; s.shift; end
+ assert_equal(42, Integer(obj, 10))
+
+ assert_separately([], "#{<<-"begin;"}\n#{<<-"end;"}")
+ begin;
+ class Float
+ undef to_int
+ def to_int; raise "conversion failed"; end
+ end
+ assert_equal (1 << 100), Integer((1 << 100).to_f)
+ assert_equal 1, Integer(1.0)
+ end;
end
def test_int_p
@@ -104,42 +121,8 @@ class TestInteger < Test::Unit::TestCase
assert_predicate(1, :integer?)
end
- def test_odd_p_even_p
- Fixnum.class_eval do
- alias odd_bak odd?
- alias even_bak even?
- remove_method :odd?, :even?
- end
-
- assert_predicate(1, :odd?)
- assert_not_predicate(2, :odd?)
- assert_not_predicate(1, :even?)
- assert_predicate(2, :even?)
-
- ensure
- Fixnum.class_eval do
- alias odd? odd_bak
- alias even? even_bak
- remove_method :odd_bak, :even_bak
- end
- end
-
def test_succ
assert_equal(2, 1.send(:succ))
-
- Fixnum.class_eval do
- alias succ_bak succ
- remove_method :succ
- end
-
- assert_equal(2, 1.succ)
- assert_equal(4294967297, 4294967296.succ)
-
- ensure
- Fixnum.class_eval do
- alias succ succ_bak
- remove_method :succ_bak
- end
end
def test_chr
@@ -184,61 +167,238 @@ class TestInteger < Test::Unit::TestCase
end
end
+ def assert_int_equal(expected, result, mesg = nil)
+ assert_kind_of(Integer, result, mesg)
+ assert_equal(expected, result, mesg)
+ end
+
+ def assert_float_equal(expected, result, mesg = nil)
+ assert_kind_of(Float, result, mesg)
+ assert_equal(expected, result, mesg)
+ end
+
def test_round
- assert_equal(11111, 11111.round)
- assert_equal(Fixnum, 11111.round.class)
- assert_equal(11111, 11111.round(0))
- assert_equal(Fixnum, 11111.round(0).class)
+ assert_int_equal(11111, 11111.round)
+ assert_int_equal(11111, 11111.round(0))
+
+ assert_int_equal(11111, 11111.round(1))
+ assert_int_equal(11111, 11111.round(2))
+
+ assert_int_equal(11110, 11111.round(-1))
+ assert_int_equal(11100, 11111.round(-2))
+ assert_int_equal(+200, +249.round(-2))
+ assert_int_equal(+300, +250.round(-2))
+ assert_int_equal(-200, -249.round(-2))
+ assert_int_equal(+200, +249.round(-2, half: :even))
+ assert_int_equal(+200, +250.round(-2, half: :even))
+ assert_int_equal(+300, +349.round(-2, half: :even))
+ assert_int_equal(+400, +350.round(-2, half: :even))
+ assert_int_equal(+200, +249.round(-2, half: :up))
+ assert_int_equal(+300, +250.round(-2, half: :up))
+ assert_int_equal(+300, +349.round(-2, half: :up))
+ assert_int_equal(+400, +350.round(-2, half: :up))
+ assert_int_equal(+200, +249.round(-2, half: :down))
+ assert_int_equal(+200, +250.round(-2, half: :down))
+ assert_int_equal(+300, +349.round(-2, half: :down))
+ assert_int_equal(+300, +350.round(-2, half: :down))
+ assert_int_equal(-300, -250.round(-2))
+ assert_int_equal(-200, -249.round(-2, half: :even))
+ assert_int_equal(-200, -250.round(-2, half: :even))
+ assert_int_equal(-300, -349.round(-2, half: :even))
+ assert_int_equal(-400, -350.round(-2, half: :even))
+ assert_int_equal(-200, -249.round(-2, half: :up))
+ assert_int_equal(-300, -250.round(-2, half: :up))
+ assert_int_equal(-300, -349.round(-2, half: :up))
+ assert_int_equal(-400, -350.round(-2, half: :up))
+ assert_int_equal(-200, -249.round(-2, half: :down))
+ assert_int_equal(-200, -250.round(-2, half: :down))
+ assert_int_equal(-300, -349.round(-2, half: :down))
+ assert_int_equal(-300, -350.round(-2, half: :down))
+ assert_int_equal(+30 * 10**70, (+25 * 10**70).round(-71))
+ assert_int_equal(-30 * 10**70, (-25 * 10**70).round(-71))
+ assert_int_equal(+20 * 10**70, (+25 * 10**70 - 1).round(-71))
+ assert_int_equal(-20 * 10**70, (-25 * 10**70 + 1).round(-71))
+ assert_int_equal(+40 * 10**70, (+35 * 10**70).round(-71))
+ assert_int_equal(-40 * 10**70, (-35 * 10**70).round(-71))
+ assert_int_equal(+30 * 10**70, (+35 * 10**70 - 1).round(-71))
+ assert_int_equal(-30 * 10**70, (-35 * 10**70 + 1).round(-71))
+ assert_int_equal(+20 * 10**70, (+25 * 10**70).round(-71, half: :even))
+ assert_int_equal(-20 * 10**70, (-25 * 10**70).round(-71, half: :even))
+ assert_int_equal(+20 * 10**70, (+25 * 10**70 - 1).round(-71, half: :even))
+ assert_int_equal(-20 * 10**70, (-25 * 10**70 + 1).round(-71, half: :even))
+ assert_int_equal(+40 * 10**70, (+35 * 10**70).round(-71, half: :even))
+ assert_int_equal(-40 * 10**70, (-35 * 10**70).round(-71, half: :even))
+ assert_int_equal(+30 * 10**70, (+35 * 10**70 - 1).round(-71, half: :even))
+ assert_int_equal(-30 * 10**70, (-35 * 10**70 + 1).round(-71, half: :even))
+ assert_int_equal(+30 * 10**70, (+25 * 10**70).round(-71, half: :up))
+ assert_int_equal(-30 * 10**70, (-25 * 10**70).round(-71, half: :up))
+ assert_int_equal(+20 * 10**70, (+25 * 10**70 - 1).round(-71, half: :up))
+ assert_int_equal(-20 * 10**70, (-25 * 10**70 + 1).round(-71, half: :up))
+ assert_int_equal(+40 * 10**70, (+35 * 10**70).round(-71, half: :up))
+ assert_int_equal(-40 * 10**70, (-35 * 10**70).round(-71, half: :up))
+ assert_int_equal(+30 * 10**70, (+35 * 10**70 - 1).round(-71, half: :up))
+ assert_int_equal(-30 * 10**70, (-35 * 10**70 + 1).round(-71, half: :up))
+ assert_int_equal(+20 * 10**70, (+25 * 10**70).round(-71, half: :down))
+ assert_int_equal(-20 * 10**70, (-25 * 10**70).round(-71, half: :down))
+ assert_int_equal(+20 * 10**70, (+25 * 10**70 - 1).round(-71, half: :down))
+ assert_int_equal(-20 * 10**70, (-25 * 10**70 + 1).round(-71, half: :down))
+ assert_int_equal(+30 * 10**70, (+35 * 10**70).round(-71, half: :down))
+ assert_int_equal(-30 * 10**70, (-35 * 10**70).round(-71, half: :down))
+ assert_int_equal(+30 * 10**70, (+35 * 10**70 - 1).round(-71, half: :down))
+ assert_int_equal(-30 * 10**70, (-35 * 10**70 + 1).round(-71, half: :down))
+
+ assert_int_equal(1111_1111_1111_1111_1111_1111_1111_1110, 1111_1111_1111_1111_1111_1111_1111_1111.round(-1))
+ assert_int_equal(-1111_1111_1111_1111_1111_1111_1111_1110, (-1111_1111_1111_1111_1111_1111_1111_1111).round(-1))
+
+ assert_int_equal(1111_1111_1111_1111_1111_1111_1111_1111, 1111_1111_1111_1111_1111_1111_1111_1111.round(1))
+ assert_int_equal(10**400, (10**400).round(1))
+ end
- assert_equal(11111.0, 11111.round(1))
- assert_equal(Float, 11111.round(1).class)
- assert_equal(11111.0, 11111.round(2))
- assert_equal(Float, 11111.round(2).class)
+ def test_floor
+ assert_int_equal(11111, 11111.floor)
+ assert_int_equal(11111, 11111.floor(0))
+
+ assert_int_equal(11111, 11111.floor(1))
+ assert_int_equal(11111, 11111.floor(2))
+
+ assert_int_equal(11110, 11110.floor(-1))
+ assert_int_equal(11110, 11119.floor(-1))
+ assert_int_equal(11100, 11100.floor(-2))
+ assert_int_equal(11100, 11199.floor(-2))
+ assert_int_equal(0, 11111.floor(-5))
+ assert_int_equal(+200, +299.floor(-2))
+ assert_int_equal(+300, +300.floor(-2))
+ assert_int_equal(-300, -299.floor(-2))
+ assert_int_equal(-300, -300.floor(-2))
+ assert_int_equal(+20 * 10**70, (+25 * 10**70).floor(-71))
+ assert_int_equal(-30 * 10**70, (-25 * 10**70).floor(-71))
+ assert_int_equal(+20 * 10**70, (+25 * 10**70 - 1).floor(-71))
+ assert_int_equal(-30 * 10**70, (-25 * 10**70 + 1).floor(-71))
+
+ assert_int_equal(1111_1111_1111_1111_1111_1111_1111_1110, 1111_1111_1111_1111_1111_1111_1111_1111.floor(-1))
+ assert_int_equal(-1111_1111_1111_1111_1111_1111_1111_1120, (-1111_1111_1111_1111_1111_1111_1111_1111).floor(-1))
+
+ assert_int_equal(1111_1111_1111_1111_1111_1111_1111_1111, 1111_1111_1111_1111_1111_1111_1111_1111.floor(1))
+ assert_int_equal(10**400, (10**400).floor(1))
+ end
- assert_equal(11110, 11111.round(-1))
- assert_equal(Fixnum, 11111.round(-1).class)
- assert_equal(11100, 11111.round(-2))
- assert_equal(Fixnum, 11111.round(-2).class)
+ def test_ceil
+ assert_int_equal(11111, 11111.ceil)
+ assert_int_equal(11111, 11111.ceil(0))
+
+ assert_int_equal(11111, 11111.ceil(1))
+ assert_int_equal(11111, 11111.ceil(2))
+
+ assert_int_equal(11110, 11110.ceil(-1))
+ assert_int_equal(11120, 11119.ceil(-1))
+ assert_int_equal(11200, 11101.ceil(-2))
+ assert_int_equal(11200, 11200.ceil(-2))
+ assert_int_equal(100000, 11111.ceil(-5))
+ assert_int_equal(300, 299.ceil(-2))
+ assert_int_equal(300, 300.ceil(-2))
+ assert_int_equal(-200, -299.ceil(-2))
+ assert_int_equal(-300, -300.ceil(-2))
+ assert_int_equal(+30 * 10**70, (+25 * 10**70).ceil(-71))
+ assert_int_equal(-20 * 10**70, (-25 * 10**70).ceil(-71))
+ assert_int_equal(+30 * 10**70, (+25 * 10**70 - 1).ceil(-71))
+ assert_int_equal(-20 * 10**70, (-25 * 10**70 + 1).ceil(-71))
+
+ assert_int_equal(1111_1111_1111_1111_1111_1111_1111_1120, 1111_1111_1111_1111_1111_1111_1111_1111.ceil(-1))
+ assert_int_equal(-1111_1111_1111_1111_1111_1111_1111_1110, (-1111_1111_1111_1111_1111_1111_1111_1111).ceil(-1))
+
+ assert_int_equal(1111_1111_1111_1111_1111_1111_1111_1111, 1111_1111_1111_1111_1111_1111_1111_1111.ceil(1))
+ assert_int_equal(10**400, (10**400).ceil(1))
+ end
- assert_equal(1111_1111_1111_1111_1111_1111_1111_1110, 1111_1111_1111_1111_1111_1111_1111_1111.round(-1))
- assert_equal(Bignum, 1111_1111_1111_1111_1111_1111_1111_1111.round(-1).class)
- assert_equal(-1111_1111_1111_1111_1111_1111_1111_1110, (-1111_1111_1111_1111_1111_1111_1111_1111).round(-1))
- assert_equal(Bignum, (-1111_1111_1111_1111_1111_1111_1111_1111).round(-1).class)
+ def test_truncate
+ assert_int_equal(11111, 11111.truncate)
+ assert_int_equal(11111, 11111.truncate(0))
+
+ assert_int_equal(11111, 11111.truncate(1))
+ assert_int_equal(11111, 11111.truncate(2))
+
+ assert_int_equal(11110, 11110.truncate(-1))
+ assert_int_equal(11110, 11119.truncate(-1))
+ assert_int_equal(11100, 11100.truncate(-2))
+ assert_int_equal(11100, 11199.truncate(-2))
+ assert_int_equal(0, 11111.truncate(-5))
+ assert_int_equal(+200, +299.truncate(-2))
+ assert_int_equal(+300, +300.truncate(-2))
+ assert_int_equal(-200, -299.truncate(-2))
+ assert_int_equal(-300, -300.truncate(-2))
+ assert_int_equal(+20 * 10**70, (+25 * 10**70).truncate(-71))
+ assert_int_equal(-20 * 10**70, (-25 * 10**70).truncate(-71))
+ assert_int_equal(+20 * 10**70, (+25 * 10**70 - 1).truncate(-71))
+ assert_int_equal(-20 * 10**70, (-25 * 10**70 + 1).truncate(-71))
+
+ assert_int_equal(1111_1111_1111_1111_1111_1111_1111_1110, 1111_1111_1111_1111_1111_1111_1111_1111.truncate(-1))
+ assert_int_equal(-1111_1111_1111_1111_1111_1111_1111_1110, (-1111_1111_1111_1111_1111_1111_1111_1111).truncate(-1))
+
+ assert_int_equal(1111_1111_1111_1111_1111_1111_1111_1111, 1111_1111_1111_1111_1111_1111_1111_1111.truncate(1))
+ assert_int_equal(10**400, (10**400).truncate(1))
end
- def test_bitwise_and_with_integer_mimic_object
- def (obj = Object.new).to_int
- 10
+ MimicInteger = Struct.new(:to_int)
+ module CoercionToInt
+ def coerce(other)
+ [other, to_int]
end
- assert_raise(TypeError, '[ruby-core:39491]') { 3 & obj }
+ end
- def obj.coerce(other)
- [other, 10]
- end
+ def test_bitwise_and_with_integer_mimic_object
+ obj = MimicInteger.new(10)
+ assert_raise(TypeError, '[ruby-core:39491]') { 3 & obj }
+ obj.extend(CoercionToInt)
assert_equal(3 & 10, 3 & obj)
end
def test_bitwise_or_with_integer_mimic_object
- def (obj = Object.new).to_int
- 10
- end
+ obj = MimicInteger.new(10)
assert_raise(TypeError, '[ruby-core:39491]') { 3 | obj }
-
- def obj.coerce(other)
- [other, 10]
- end
+ obj.extend(CoercionToInt)
assert_equal(3 | 10, 3 | obj)
end
def test_bitwise_xor_with_integer_mimic_object
- def (obj = Object.new).to_int
- 10
- end
+ obj = MimicInteger.new(10)
assert_raise(TypeError, '[ruby-core:39491]') { 3 ^ obj }
+ obj.extend(CoercionToInt)
+ assert_equal(3 ^ 10, 3 ^ obj)
+ end
- def obj.coerce(other)
- [other, 10]
+ module CoercionToSelf
+ def coerce(other)
+ [self.class.new(other), self]
end
+ end
+
+ def test_bitwise_and_with_integer_coercion
+ obj = Struct.new(:value) do
+ include(CoercionToSelf)
+ def &(other)
+ self.value & other.value
+ end
+ end.new(10)
+ assert_equal(3 & 10, 3 & obj)
+ end
+
+ def test_bitwise_or_with_integer_coercion
+ obj = Struct.new(:value) do
+ include(CoercionToSelf)
+ def |(other)
+ self.value | other.value
+ end
+ end.new(10)
+ assert_equal(3 | 10, 3 | obj)
+ end
+
+ def test_bitwise_xor_with_integer_coercion
+ obj = Struct.new(:value) do
+ include(CoercionToSelf)
+ def ^(other)
+ self.value ^ other.value
+ end
+ end.new(10)
assert_equal(3 ^ 10, 3 ^ obj)
end
@@ -277,4 +437,92 @@ class TestInteger < Test::Unit::TestCase
assert_equal(i+1, (n+1).bit_length, "#{n+1}.bit_length")
}
end
+
+ def test_digits
+ assert_equal([0], 0.digits)
+ assert_equal([1], 1.digits)
+ assert_equal([0, 9, 8, 7, 6, 5, 4, 3, 2, 1], 1234567890.digits)
+ assert_equal([90, 78, 56, 34, 12], 1234567890.digits(100))
+ assert_equal([10, 5, 6, 8, 0, 10, 8, 6, 1], 1234567890.digits(13))
+ end
+
+ def test_digits_for_negative_numbers
+ assert_raise(Math::DomainError) { -1.digits }
+ assert_raise(Math::DomainError) { -1234567890.digits }
+ assert_raise(Math::DomainError) { -1234567890.digits(100) }
+ assert_raise(Math::DomainError) { -1234567890.digits(13) }
+ end
+
+ def test_digits_for_invalid_base_numbers
+ assert_raise(ArgumentError) { 10.digits(-1) }
+ assert_raise(ArgumentError) { 10.digits(0) }
+ assert_raise(ArgumentError) { 10.digits(1) }
+ end
+
+ def test_digits_for_non_integral_base_numbers
+ assert_equal([1], 1.digits(10r))
+ assert_equal([1], 1.digits(10.0))
+ assert_raise(RangeError) { 10.digits(10+1i) }
+ end
+
+ def test_digits_for_non_numeric_base_argument
+ assert_raise(TypeError) { 10.digits("10") }
+ assert_raise(TypeError) { 10.digits("a") }
+
+ class << (o = Object.new)
+ def to_int
+ 10
+ end
+ end
+ assert_equal([0, 1], 10.digits(o))
+ end
+
+ def test_square_root
+ assert_raise(TypeError) {Integer.sqrt("x")}
+ assert_raise(Math::DomainError) {Integer.sqrt(-1)}
+ assert_equal(0, Integer.sqrt(0))
+ (1...4).each {|i| assert_equal(1, Integer.sqrt(i))}
+ (4...9).each {|i| assert_equal(2, Integer.sqrt(i))}
+ (9...16).each {|i| assert_equal(3, Integer.sqrt(i))}
+ (1..40).each do |i|
+ mesg = "10**#{i}"
+ s = Integer.sqrt(n = 10**i)
+ if i.even?
+ assert_equal(10**(i/2), Integer.sqrt(n), mesg)
+ else
+ assert_include((s**2)...(s+1)**2, n, mesg)
+ end
+ end
+ 50.step(400, 10) do |i|
+ exact = 10**(i/2)
+ x = 10**i
+ assert_equal(exact, Integer.sqrt(x), "10**#{i}")
+ assert_equal(exact, Integer.sqrt(x+1), "10**#{i}+1")
+ assert_equal(exact-1, Integer.sqrt(x-1), "10**#{i}-1")
+ end
+
+ bug13440 = '[ruby-core:80696] [Bug #13440]'
+ failures = []
+ 0.step(to: 50, by: 0.05) do |i|
+ n = (10**i).to_i
+ root = Integer.sqrt(n)
+ failures << n unless root*root <= n && (root+1)*(root+1) > n
+ end
+ assert_empty(failures, bug13440)
+ end
+
+ def test_fdiv
+ assert_equal(1.0, 1.fdiv(1))
+ assert_equal(0.5, 1.fdiv(2))
+ end
+
+ def test_obj_fdiv
+ o = Object.new
+ def o.coerce(x); [x, 0.5]; end
+ assert_equal(2.0, 1.fdiv(o))
+ o = Object.new
+ def o.coerce(x); [self, x]; end
+ def o.fdiv(x); 1; end
+ assert_equal(1.0, 1.fdiv(o))
+ end
end