summaryrefslogtreecommitdiff
path: root/test/ruby/test_sprintf.rb
diff options
context:
space:
mode:
Diffstat (limited to 'test/ruby/test_sprintf.rb')
-rw-r--r--test/ruby/test_sprintf.rb20
1 files changed, 19 insertions, 1 deletions
diff --git a/test/ruby/test_sprintf.rb b/test/ruby/test_sprintf.rb
index 8cf2c63a20..bbbe6e7ec3 100644
--- a/test/ruby/test_sprintf.rb
+++ b/test/ruby/test_sprintf.rb
@@ -1,5 +1,6 @@
# frozen_string_literal: false
require 'test/unit'
+require 'rbconfig/sizeof'
class TestSprintf < Test::Unit::TestCase
def test_positional
@@ -227,6 +228,10 @@ class TestSprintf < Test::Unit::TestCase
bug11766 = '[ruby-core:71806] [Bug #11766]'
assert_equal("x"*10+" 1.0", sprintf("x"*10+"%8.1f", 1r), bug11766)
+
+ require 'rbconfig/sizeof'
+ fmin, fmax = RbConfig::LIMITS.values_at("FIXNUM_MIN", "FIXNUM_MAX")
+ assert_match(/\A-\d+\.\d+\z/, sprintf("%f", Rational(fmin, fmax)))
end
def test_rational_precision
@@ -235,7 +240,7 @@ class TestSprintf < Test::Unit::TestCase
def test_hash
options = {:capture=>/\d+/}
- assert_equal("with options {:capture=>/\\d+/}", sprintf("with options %p" % options))
+ assert_equal("with options #{options.inspect}", sprintf("with options %p" % options))
end
def test_inspect
@@ -535,6 +540,11 @@ class TestSprintf < Test::Unit::TestCase
def test_width_underflow
bug = 'https://github.com/mruby/mruby/issues/3347'
assert_equal("!", sprintf("%*c", 0, ?!.ord), bug)
+
+ int_max = RbConfig::LIMITS["INT_MAX"]
+ assert_raise_with_message(ArgumentError, /width too big/) {
+ sprintf "%*c", int_max, 0x80
+ }
end
def test_negative_width_overflow
@@ -542,4 +552,12 @@ class TestSprintf < Test::Unit::TestCase
sprintf("%*s", RbConfig::LIMITS["INT_MIN"], "")
end
end
+
+ def test_binary_format_coderange
+ 1.upto(500) do |i|
+ str = sprintf("%*s".b, i, "\xe2".b)
+ refute_predicate str, :ascii_only?
+ assert_equal i, str.bytesize
+ end
+ end
end