diff options
Diffstat (limited to 'test')
34 files changed, 1578 insertions, 468 deletions
diff --git a/test/bigdecimal/test_bigdecimal.rb b/test/bigdecimal/test_bigdecimal.rb new file mode 100644 index 0000000000..99ce108458 --- /dev/null +++ b/test/bigdecimal/test_bigdecimal.rb @@ -0,0 +1,689 @@ +require "test/unit" +require "bigdecimal" + +class TestBigDecimal < Test::Unit::TestCase + def setup + BigDecimal.mode(BigDecimal::EXCEPTION_ALL, true) + BigDecimal.mode(BigDecimal::EXCEPTION_UNDERFLOW, true) + BigDecimal.mode(BigDecimal::EXCEPTION_OVERFLOW, true) + BigDecimal.mode(BigDecimal::ROUND_MODE, BigDecimal::ROUND_HALF_UP) + BigDecimal.limit(0) + end + + def test_version + assert_equal("1.0.1", BigDecimal.ver) + end + + def test_global_new + assert_equal(1, BigDecimal("1")) + assert_equal(1, BigDecimal("1", 1)) + assert_raise(ArgumentError) { BigDecimal("1", -1) } + end + + def test_new + assert_equal(1, BigDecimal.new("1")) + assert_equal(1, BigDecimal.new("1", 1)) + assert_equal(1, BigDecimal.new(" 1 ")) + assert_equal(111, BigDecimal.new("1_1_1_")) + assert_equal(0, BigDecimal.new("_1_1_1")) + + BigDecimal.mode(BigDecimal::EXCEPTION_OVERFLOW, false) + BigDecimal.mode(BigDecimal::EXCEPTION_NaN, false) + assert_equal( 1, BigDecimal.new("Infinity").infinite?) + assert_equal(-1, BigDecimal.new("-Infinity").infinite?) + assert_equal(true, BigDecimal.new("NaN").nan?) + end + + def _test_mode(type) + BigDecimal.mode(type, true) + assert_raise(FloatDomainError) { yield } + + BigDecimal.mode(type, false) + assert_nothing_raised { yield } + end + + def test_mode + assert_raise(TypeError) { BigDecimal.mode(BigDecimal::EXCEPTION_ALL, 1) } + assert_raise(TypeError) { BigDecimal.mode(BigDecimal::ROUND_MODE, 256) } + assert_raise(TypeError) { BigDecimal.mode(0xf000, true) } + end + + def test_exception_nan + _test_mode(BigDecimal::EXCEPTION_NaN) { BigDecimal.new("NaN") } + end + + def test_exception_infinity + _test_mode(BigDecimal::EXCEPTION_INFINITY) { BigDecimal.new("Infinity") } + end + + def test_exception_underflow + _test_mode(BigDecimal::EXCEPTION_UNDERFLOW) do + x = BigDecimal.new("0.1") + 100.times do + x *= x + break if x == false + end + end + end + + def test_exception_overflow + _test_mode(BigDecimal::EXCEPTION_OVERFLOW) do + x = BigDecimal.new("10") + 100.times do + x *= x + break if x == false + end + end + end + + def test_exception_zerodivide + _test_mode(BigDecimal::EXCEPTION_ZERODIVIDE) { 1 / BigDecimal.new("0") } + _test_mode(BigDecimal::EXCEPTION_ZERODIVIDE) { -1 / BigDecimal.new("0") } + end + + def test_round_up + n4 = BigDecimal.new("4") # n4 / 9 = 0.44444... + n5 = BigDecimal.new("5") # n5 / 9 = 0.55555... + n6 = BigDecimal.new("6") # n6 / 9 = 0.66666... + m4, m5, m6 = -n4, -n5, -n6 + n2h = BigDecimal.new("2.5") + n3h = BigDecimal.new("3.5") + m2h, m3h = -n2h, -n3h + + BigDecimal.mode(BigDecimal::ROUND_MODE, BigDecimal::ROUND_UP) + assert_operator(n4, :<, n4 / 9 * 9) + assert_operator(n5, :<, n5 / 9 * 9) + assert_operator(n6, :<, n6 / 9 * 9) + assert_operator(m4, :>, m4 / 9 * 9) + assert_operator(m5, :>, m5 / 9 * 9) + assert_operator(m6, :>, m6 / 9 * 9) + assert_equal(3, n2h.round) + assert_equal(4, n3h.round) + assert_equal(-3, m2h.round) + assert_equal(-4, m3h.round) + + BigDecimal.mode(BigDecimal::ROUND_MODE, BigDecimal::ROUND_DOWN) + assert_operator(n4, :>, n4 / 9 * 9) + assert_operator(n5, :>, n5 / 9 * 9) + assert_operator(n6, :>, n6 / 9 * 9) + assert_operator(m4, :<, m4 / 9 * 9) + assert_operator(m5, :<, m5 / 9 * 9) + assert_operator(m6, :<, m6 / 9 * 9) + assert_equal(2, n2h.round) + assert_equal(3, n3h.round) + assert_equal(-2, m2h.round) + assert_equal(-3, m3h.round) + + BigDecimal.mode(BigDecimal::ROUND_MODE, BigDecimal::ROUND_HALF_UP) + assert_operator(n4, :>, n4 / 9 * 9) + assert_operator(n5, :<, n5 / 9 * 9) + assert_operator(n6, :<, n6 / 9 * 9) + assert_operator(m4, :<, m4 / 9 * 9) + assert_operator(m5, :>, m5 / 9 * 9) + assert_operator(m6, :>, m6 / 9 * 9) + assert_equal(3, n2h.round) + assert_equal(4, n3h.round) + assert_equal(-3, m2h.round) + assert_equal(-4, m3h.round) + + BigDecimal.mode(BigDecimal::ROUND_MODE, BigDecimal::ROUND_HALF_DOWN) + assert_operator(n4, :>, n4 / 9 * 9) + assert_operator(n5, :>, n5 / 9 * 9) + assert_operator(n6, :<, n6 / 9 * 9) + assert_operator(m4, :<, m4 / 9 * 9) + assert_operator(m5, :<, m5 / 9 * 9) + assert_operator(m6, :>, m6 / 9 * 9) + assert_equal(2, n2h.round) + assert_equal(3, n3h.round) + assert_equal(-2, m2h.round) + assert_equal(-3, m3h.round) + + BigDecimal.mode(BigDecimal::ROUND_MODE, BigDecimal::ROUND_HALF_EVEN) + assert_operator(n4, :>, n4 / 9 * 9) + assert_operator(n5, :<, n5 / 9 * 9) + assert_operator(n6, :<, n6 / 9 * 9) + assert_operator(m4, :<, m4 / 9 * 9) + assert_operator(m5, :>, m5 / 9 * 9) + assert_operator(m6, :>, m6 / 9 * 9) + assert_equal(2, n2h.round) + assert_equal(4, n3h.round) + assert_equal(-2, m2h.round) + assert_equal(-4, m3h.round) + + BigDecimal.mode(BigDecimal::ROUND_MODE, BigDecimal::ROUND_CEILING) + assert_operator(n4, :<, n4 / 9 * 9) + assert_operator(n5, :<, n5 / 9 * 9) + assert_operator(n6, :<, n6 / 9 * 9) + assert_operator(m4, :<, m4 / 9 * 9) + assert_operator(m5, :<, m5 / 9 * 9) + assert_operator(m6, :<, m6 / 9 * 9) + assert_equal(3, n2h.round) + assert_equal(4, n3h.round) + assert_equal(-2, m2h.round) + assert_equal(-3, m3h.round) + + BigDecimal.mode(BigDecimal::ROUND_MODE, BigDecimal::ROUND_FLOOR) + assert_operator(n4, :>, n4 / 9 * 9) + assert_operator(n5, :>, n5 / 9 * 9) + assert_operator(n6, :>, n6 / 9 * 9) + assert_operator(m4, :>, m4 / 9 * 9) + assert_operator(m5, :>, m5 / 9 * 9) + assert_operator(m6, :>, m6 / 9 * 9) + assert_equal(2, n2h.round) + assert_equal(3, n3h.round) + assert_equal(-3, m2h.round) + assert_equal(-4, m3h.round) + end + + def test_zero_p + assert_equal(true, BigDecimal.new("0").zero?) + assert_equal(false, BigDecimal.new("1").zero?) + end + + def test_nonzero_p + assert_equal(nil, BigDecimal.new("0").nonzero?) + assert_equal(BigDecimal.new("1"), BigDecimal.new("1").nonzero?) + end + + def test_double_fig + assert_kind_of(Integer, BigDecimal.double_fig) + end + + def test_cmp + n1 = BigDecimal.new("1") + n2 = BigDecimal.new("2") + assert_equal( 0, n1 <=> n1) + assert_equal( 1, n2 <=> n1) + assert_equal(-1, n1 <=> n2) + assert_operator(n1, :==, n1) + assert_operator(n1, :!=, n2) + assert_operator(n1, :<, n2) + assert_operator(n1, :<=, n1) + assert_operator(n1, :<=, n2) + assert_operator(n2, :>, n1) + assert_operator(n2, :>=, n1) + assert_operator(n1, :>=, n1) + + assert_operator(BigDecimal.new("-0"), :==, BigDecimal.new("0")) + assert_operator(BigDecimal.new("0"), :<, BigDecimal.new("1")) + assert_operator(BigDecimal.new("1"), :>, BigDecimal.new("0")) + assert_operator(BigDecimal.new("1"), :>, BigDecimal.new("-1")) + assert_operator(BigDecimal.new("-1"), :<, BigDecimal.new("1")) + assert_operator(BigDecimal.new((2**100).to_s), :>, BigDecimal.new("1")) + assert_operator(BigDecimal.new("1"), :<, BigDecimal.new((2**100).to_s)) + + BigDecimal.mode(BigDecimal::EXCEPTION_OVERFLOW, false) + inf = BigDecimal.new("Infinity") + assert_operator(inf, :>, 1) + assert_operator(1, :<, inf) + end + + def test_cmp_corece + n1 = BigDecimal.new("1") + n2 = BigDecimal.new("2") + o1 = Object.new; def o1.coerce(x); [x, BigDecimal.new("1")]; end + o2 = Object.new; def o2.coerce(x); [x, BigDecimal.new("2")]; end + assert_equal( 0, n1 <=> o1) + assert_equal( 1, n2 <=> o1) + assert_equal(-1, n1 <=> o2) + assert_operator(n1, :==, o1) + assert_operator(n1, :!=, o2) + assert_operator(n1, :<, o2) + assert_operator(n1, :<=, o1) + assert_operator(n1, :<=, o2) + assert_operator(n2, :>, o1) + assert_operator(n2, :>=, o1) + assert_operator(n1, :>=, 1) + end + + def test_cmp_bignum + assert_operator(BigDecimal.new((2**100).to_s), :==, 2**100) + end + + def test_cmp_data + d = Time.now; def d.coerce(x); [x, x]; end + assert_operator(BigDecimal.new((2**100).to_s), :==, d) + end + + def test_precs + a = BigDecimal.new("1").precs + assert_instance_of(Array, a) + assert_equal(2, a.size) + assert_kind_of(Integer, a[0]) + assert_kind_of(Integer, a[1]) + end + + def test_hash + a = [] + x = BigDecimal.new("1") + 10.times { a << x *= 10 } + h = {} + a.each_with_index {|x, i| h[x] = i } + a.each_with_index do |x, i| + assert_equal(i, h[x]) + end + end + + def test_marshal + s = Marshal.dump(BigDecimal("1", 1)) + assert_equal(BigDecimal("1", 1), Marshal.load(s)) + + # corrupt data + s = s.gsub(/BigDecimal.*\z/m) {|x| x.gsub(/\d/m, "-") } + assert_raise(TypeError) { Marshal.load(s) } + end + + def test_finite_infinite_nan + BigDecimal.mode(BigDecimal::EXCEPTION_OVERFLOW, false) + + x = BigDecimal.new("0") + assert_equal(true, x.finite?) + assert_equal(nil, x.infinite?) + assert_equal(false, x.nan?) + y = 1 / x + assert_equal(false, y.finite?) + assert_equal(1, y.infinite?) + assert_equal(false, y.nan?) + y = -1 / x + assert_equal(false, y.finite?) + assert_equal(-1, y.infinite?) + assert_equal(false, y.nan?) + + BigDecimal.mode(BigDecimal::EXCEPTION_NaN, false) + y = 0 / x + assert_equal(false, y.finite?) + assert_equal(nil, y.infinite?) + assert_equal(true, y.nan?) + end + + def test_to_i + BigDecimal.mode(BigDecimal::EXCEPTION_OVERFLOW, false) + BigDecimal.mode(BigDecimal::EXCEPTION_NaN, false) + + x = BigDecimal.new("0") + assert_kind_of(Integer, x.to_i) + assert_equal(0, x.to_i) + assert_nil(( 1 / x).to_i) + assert_nil((-1 / x).to_i) + assert_nil(( 0 / x).to_i) + x = BigDecimal.new("1") + assert_equal(1, x.to_i) + x = BigDecimal.new((2**100).to_s) + assert_equal(2**100, x.to_i) + end + + def test_to_f + BigDecimal.mode(BigDecimal::EXCEPTION_OVERFLOW, false) + BigDecimal.mode(BigDecimal::EXCEPTION_NaN, false) + + x = BigDecimal.new("0") + assert_instance_of(Float, x.to_f) + assert_equal(0.0, x.to_f) + assert_equal( 1.0 / 0.0, ( 1 / x).to_f) + assert_equal(-1.0 / 0.0, (-1 / x).to_f) + assert_equal(true, ( 0 / x).to_f.nan?) + x = BigDecimal.new("1") + assert_equal(1.0, x.to_f) + x = BigDecimal.new((2**100).to_s) + assert_equal((2**100).to_f, x.to_f) + x = BigDecimal.new("1" + "0" * 10000) + assert_equal(0, BigDecimal.new("-0").to_f) + + BigDecimal.mode(BigDecimal::EXCEPTION_OVERFLOW, true) + assert_raise(FloatDomainError) { x.to_f } + BigDecimal.mode(BigDecimal::EXCEPTION_OVERFLOW, false) + assert_kind_of(Float, x .to_f) + assert_kind_of(Float, (-x).to_f) + end + + def test_coerce + a, b = BigDecimal.new("1").coerce(1.0) + assert_instance_of(Float, a) + assert_instance_of(Float, b) + end + + def test_uplus + x = BigDecimal.new("1") + assert_equal(x, x.send(:+@)) + end + + def test_add + x = BigDecimal.new("1") + assert_equal(BigDecimal.new("2"), x + x) + assert_equal(1, BigDecimal.new("0") + 1) + assert_equal(1, x + 0) + + assert_equal(BigDecimal::SIGN_POSITIVE_ZERO, (BigDecimal.new("0") + 0).sign) + assert_equal(BigDecimal::SIGN_POSITIVE_ZERO, (BigDecimal.new("-0") + 0).sign) + assert_equal(BigDecimal::SIGN_NEGATIVE_ZERO, (BigDecimal.new("-0") + BigDecimal.new("-0")).sign) + + x = BigDecimal.new((2**100).to_s) + assert_equal(BigDecimal.new((2**100+1).to_s), x + 1) + end + + def test_sub + x = BigDecimal.new("1") + assert_equal(BigDecimal.new("0"), x - x) + assert_equal(-1, BigDecimal.new("0") - 1) + assert_equal(1, x - 0) + + assert_equal(BigDecimal::SIGN_POSITIVE_ZERO, (BigDecimal.new("0") - 0).sign) + assert_equal(BigDecimal::SIGN_NEGATIVE_ZERO, (BigDecimal.new("-0") - 0).sign) + assert_equal(BigDecimal::SIGN_POSITIVE_ZERO, (BigDecimal.new("-0") - BigDecimal.new("-0")).sign) + + x = BigDecimal.new((2**100).to_s) + assert_equal(BigDecimal.new((2**100-1).to_s), x - 1) + end + + def test_mult + x = BigDecimal.new((2**100).to_s) + assert_equal(BigDecimal.new((2**100 * 3).to_s), (x * 3).to_i) + assert_equal(x, (x * 1).to_i) + assert_equal(x, (BigDecimal("1") * x).to_i) + assert_equal(BigDecimal.new((2**200).to_s), (x * x).to_i) + end + + def test_div + x = BigDecimal.new((2**100).to_s) + assert_equal(BigDecimal.new((2**100 / 3).to_s), (x / 3).to_i) + assert_equal(BigDecimal::SIGN_POSITIVE_ZERO, (BigDecimal.new("0") / 1).sign) + assert_equal(BigDecimal::SIGN_NEGATIVE_ZERO, (BigDecimal.new("-0") / 1).sign) + assert_equal(2, BigDecimal.new("2") / 1) + assert_equal(-2, BigDecimal.new("2") / -1) + end + + def test_mod + x = BigDecimal.new((2**100).to_s) + assert_equal(1, x % 3) + assert_equal(2, (-x) % 3) + assert_equal(-2, x % -3) + assert_equal(-1, (-x) % -3) + end + + def test_remainder + x = BigDecimal.new((2**100).to_s) + assert_equal(1, x.remainder(3)) + assert_equal(-1, (-x).remainder(3)) + assert_equal(1, x.remainder(-3)) + assert_equal(-1, (-x).remainder(-3)) + end + + def test_divmod + x = BigDecimal.new((2**100).to_s) + assert_equal([(x / 3).floor, 1], x.divmod(3)) + assert_equal([(-x / 3).floor, 2], (-x).divmod(3)) + + assert_equal([0, 0], BigDecimal.new("0").divmod(2)) + + BigDecimal.mode(BigDecimal::EXCEPTION_NaN, false) + a, b = BigDecimal.new("0").divmod(0) + assert_equal(true, a.nan?) + assert_equal(true, b.nan?) + end + + def test_add_bigdecimal + x = BigDecimal.new((2**100).to_s) + assert_equal(3000000000000000000000000000000, x.add(x, 1)) + assert_equal(2500000000000000000000000000000, x.add(x, 2)) + assert_equal(2540000000000000000000000000000, x.add(x, 3)) + end + + def test_sub_bigdecimal + x = BigDecimal.new((2**100).to_s) + assert_equal(1000000000000000000000000000000, x.sub(1, 1)) + assert_equal(1300000000000000000000000000000, x.sub(1, 2)) + assert_equal(1270000000000000000000000000000, x.sub(1, 3)) + end + + def test_mult_bigdecimal + x = BigDecimal.new((2**100).to_s) + assert_equal(4000000000000000000000000000000, x.mult(3, 1)) + assert_equal(3800000000000000000000000000000, x.mult(3, 2)) + assert_equal(3800000000000000000000000000000, x.mult(3, 3)) + end + + def test_div_bigdecimal + x = BigDecimal.new((2**100).to_s) + assert_equal(422550200076076467165567735125, x.div(3)) + assert_equal(400000000000000000000000000000, x.div(3, 1)) + assert_equal(420000000000000000000000000000, x.div(3, 2)) + assert_equal(423000000000000000000000000000, x.div(3, 3)) + end + + def test_abs_bigdecimal + x = BigDecimal.new((2**100).to_s) + assert_equal(1267650600228229401496703205376, x.abs) + x = BigDecimal.new("-" + (2**100).to_s) + assert_equal(1267650600228229401496703205376, x.abs) + x = BigDecimal.new("0") + assert_equal(0, x.abs) + BigDecimal.mode(BigDecimal::EXCEPTION_NaN, false) + x = BigDecimal.new("NaN") + assert_equal(true, x.abs.nan?) + end + + def test_sqrt_bigdecimal + x = BigDecimal.new("0.09") + assert_in_delta(0.3, x.sqrt(1), 0.001) + x = BigDecimal.new((2**100).to_s) + assert_equal(1125899906842624, x.sqrt(1)) + assert_equal(1125899906842624, x.sqrt(2)) + assert_equal(1125899906842624, x.sqrt(3)) # I don't understand the meaning of argument... + x = BigDecimal.new("-" + (2**100).to_s) + assert_raise(FloatDomainError) { x.sqrt(1) } + x = BigDecimal.new((2**200).to_s) + assert_equal(2**100, x.sqrt(1)) + + BigDecimal.mode(BigDecimal::EXCEPTION_OVERFLOW, false) + BigDecimal.mode(BigDecimal::EXCEPTION_NaN, false) + assert_raise(FloatDomainError) { BigDecimal.new("NaN").sqrt(1) } + + assert_equal(0, BigDecimal.new("0").sqrt(1)) + assert_equal(1, BigDecimal.new("1").sqrt(1)) + end + + def test_fix + x = BigDecimal.new("1.1") + assert_equal(1, x.fix) + end + + def test_frac + x = BigDecimal.new("1.1") + assert_equal(0.1, x.frac) + assert_equal(0.1, BigDecimal.new("0.1").frac) + BigDecimal.mode(BigDecimal::EXCEPTION_NaN, false) + assert_equal(true, BigDecimal.new("NaN").frac.nan?) + end + + def test_round + assert_equal(3, BigDecimal.new("3.14159").round) + assert_equal(9, BigDecimal.new("8.7").round) + assert_equal(3.142, BigDecimal.new("3.14159").round(3)) + assert_equal(13300.0, BigDecimal.new("13345.234").round(-2)) + + x = BigDecimal.new("111.111") + assert_equal(111 , x.round) + assert_equal(111.1 , x.round(1)) + assert_equal(111.11 , x.round(2)) + assert_equal(111.111, x.round(3)) + assert_equal(111.111, x.round(4)) + assert_equal(110 , x.round(-1)) + assert_equal(100 , x.round(-2)) + assert_equal( 0 , x.round(-3)) + assert_equal( 0 , x.round(-4)) + + x = BigDecimal.new("2.5") + assert_equal(3, x.round(0, BigDecimal::ROUND_UP)) + assert_equal(2, x.round(0, BigDecimal::ROUND_DOWN)) + assert_equal(3, x.round(0, BigDecimal::ROUND_HALF_UP)) + assert_equal(2, x.round(0, BigDecimal::ROUND_HALF_DOWN)) + assert_equal(2, x.round(0, BigDecimal::ROUND_HALF_EVEN)) + assert_equal(3, x.round(0, BigDecimal::ROUND_CEILING)) + assert_equal(2, x.round(0, BigDecimal::ROUND_FLOOR)) + assert_raise(TypeError) { x.round(0, 256) } + end + + def test_truncate + assert_equal(3, BigDecimal.new("3.14159").truncate) + assert_equal(8, BigDecimal.new("8.7").truncate) + assert_equal(3.141, BigDecimal.new("3.14159").truncate(3)) + assert_equal(13300.0, BigDecimal.new("13345.234").truncate(-2)) + end + + def test_floor + assert_equal(3, BigDecimal.new("3.14159").floor) + assert_equal(-10, BigDecimal.new("-9.1").floor) + assert_equal(3.141, BigDecimal.new("3.14159").floor(3)) + assert_equal(13300.0, BigDecimal.new("13345.234").floor(-2)) + end + + def test_ceil + assert_equal(4, BigDecimal.new("3.14159").ceil) + assert_equal(-9, BigDecimal.new("-9.1").ceil) + assert_equal(3.142, BigDecimal.new("3.14159").ceil(3)) + assert_equal(13400.0, BigDecimal.new("13345.234").ceil(-2)) + end + + def test_to_s + assert_equal('-123.45678 90123 45678 9', BigDecimal.new('-123.45678901234567890').to_s('5F')) + assert_equal('+123.45678901 23456789', BigDecimal.new('123.45678901234567890').to_s('+8F')) + assert_equal(' 123.4567890123456789', BigDecimal.new('123.45678901234567890').to_s(' F')) + assert_equal('0.1234567890123456789E3', BigDecimal.new('123.45678901234567890').to_s) + assert_equal('0.12345 67890 12345 6789E3', BigDecimal.new('123.45678901234567890').to_s(5)) + end + + def test_split + x = BigDecimal.new('-123.45678901234567890') + assert_equal([-1, "1234567890123456789", 10, 3], x.split) + assert_equal([1, "0", 10, 0], BigDecimal.new("0").split) + assert_equal([-1, "0", 10, 0], BigDecimal.new("-0").split) + + BigDecimal.mode(BigDecimal::EXCEPTION_OVERFLOW, false) + BigDecimal.mode(BigDecimal::EXCEPTION_NaN, false) + assert_equal([0, "NaN", 10, 0], BigDecimal.new("NaN").split) + assert_equal([1, "Infinity", 10, 0], BigDecimal.new("Infinity").split) + assert_equal([-1, "Infinity", 10, 0], BigDecimal.new("-Infinity").split) + end + + def test_exponent + x = BigDecimal.new('-123.45678901234567890') + assert_equal(3, x.exponent) + end + + def test_inspect + x = BigDecimal.new("1234.5678") + assert_match(/^#<BigDecimal:[0-9a-f]+,'0.12345678E4',8\(12\)>$/, x.inspect) + end + + def test_power + x = BigDecimal.new("3") + assert_equal(81, x ** 4) + assert_equal(1.0/81, x ** -4) + assert_equal(1, x ** 0) + assert_raise(TypeError) { x ** x } + assert_equal(0, BigDecimal.new("0") ** 4) + assert_equal(1, BigDecimal.new("0") ** 0) + BigDecimal.mode(BigDecimal::EXCEPTION_OVERFLOW, false) + assert_equal(BigDecimal.new("Infinity"), BigDecimal.new("0") ** -1) + assert_equal(BigDecimal.new("-Infinity"), BigDecimal.new("-0") ** -1) + BigDecimal.mode(BigDecimal::EXCEPTION_NaN, false) + assert_equal(true, (BigDecimal.new("NaN") ** 1).nan?) + + assert_equal(BigDecimal::SIGN_POSITIVE_INFINITE, (BigDecimal.new("Infinity") ** 2).sign) + assert_equal(BigDecimal::SIGN_POSITIVE_INFINITE, (BigDecimal.new("Infinity") ** 1).sign) + assert_equal(1, BigDecimal.new("Infinity") ** 0) + assert_equal(BigDecimal::SIGN_POSITIVE_ZERO, (BigDecimal.new("Infinity") ** -1).sign) + assert_equal(BigDecimal::SIGN_POSITIVE_ZERO, (BigDecimal.new("Infinity") ** -2).sign) + + assert_equal(BigDecimal::SIGN_POSITIVE_INFINITE, (BigDecimal.new("-Infinity") ** 2).sign) + assert_equal(BigDecimal::SIGN_NEGATIVE_INFINITE, (BigDecimal.new("-Infinity") ** 1).sign) + assert_equal(1, BigDecimal.new("-Infinity") ** 0) + assert_equal(BigDecimal::SIGN_NEGATIVE_ZERO, (BigDecimal.new("-Infinity") ** -1).sign) + assert_equal(BigDecimal::SIGN_POSITIVE_ZERO, (BigDecimal.new("-Infinity") ** -2).sign) + end + + def test_limit + BigDecimal.limit(1) + x = BigDecimal.new("3") + assert_equal(90, x ** 4) # OK? must it be 80? + # 3 * 3 * 3 * 3 = 10 * 3 * 3 = 30 * 3 = 90 ??? + assert_raise(ArgumentError) { BigDecimal.limit(-1) } + end + + def test_sign + BigDecimal.mode(BigDecimal::EXCEPTION_OVERFLOW, false) + BigDecimal.mode(BigDecimal::EXCEPTION_NaN, false) + + assert_equal(BigDecimal::SIGN_POSITIVE_ZERO, BigDecimal.new("0").sign) + assert_equal(BigDecimal::SIGN_NEGATIVE_ZERO, BigDecimal.new("-0").sign) + assert_equal(BigDecimal::SIGN_POSITIVE_FINITE, BigDecimal.new("1").sign) + assert_equal(BigDecimal::SIGN_NEGATIVE_FINITE, BigDecimal.new("-1").sign) + assert_equal(BigDecimal::SIGN_POSITIVE_INFINITE, (BigDecimal.new("1") / 0).sign) + assert_equal(BigDecimal::SIGN_NEGATIVE_INFINITE, (BigDecimal.new("-1") / 0).sign) + assert_equal(BigDecimal::SIGN_NaN, (BigDecimal.new("0") / 0).sign) + end + + def test_inf + BigDecimal.mode(BigDecimal::EXCEPTION_OVERFLOW, false) + BigDecimal.mode(BigDecimal::EXCEPTION_NaN, false) + inf = BigDecimal.new("Infinity") + + assert_equal(inf, inf + inf) + assert_equal(true, (inf + (-inf)).nan?) + assert_equal(true, (inf - inf).nan?) + assert_equal(inf, inf - (-inf)) + assert_equal(inf, inf * inf) + assert_equal(true, (inf / inf).nan?) + + assert_equal(inf, inf + 1) + assert_equal(inf, inf - 1) + assert_equal(inf, inf * 1) + assert_equal(true, (inf * 0).nan?) + assert_equal(inf, inf / 1) + + assert_equal(inf, 1 + inf) + assert_equal(-inf, 1 - inf) + assert_equal(inf, 1 * inf) + assert_equal(-inf, -1 * inf) + assert_equal(true, (0 * inf).nan?) + assert_equal(BigDecimal::SIGN_POSITIVE_ZERO, (1 / inf).sign) + assert_equal(BigDecimal::SIGN_NEGATIVE_ZERO, (-1 / inf).sign) + end + + def test_to_special_string + BigDecimal.mode(BigDecimal::EXCEPTION_OVERFLOW, false) + BigDecimal.mode(BigDecimal::EXCEPTION_NaN, false) + nan = BigDecimal.new("NaN") + assert_equal("NaN", nan.to_s) + inf = BigDecimal.new("Infinity") + assert_equal("Infinity", inf.to_s) + assert_equal(" Infinity", inf.to_s(" ")) + assert_equal("+Infinity", inf.to_s("+")) + assert_equal("-Infinity", (-inf).to_s) + pzero = BigDecimal.new("0") + assert_equal("0.0", pzero.to_s) + assert_equal(" 0.0", pzero.to_s(" ")) + assert_equal("+0.0", pzero.to_s("+")) + assert_equal("-0.0", (-pzero).to_s) + end + + def test_to_string + assert_equal("0.01", BigDecimal("0.01").to_s("F")) + s = "0." + "0" * 100 + "1" + assert_equal(s, BigDecimal(s).to_s("F")) + s = "1" + "0" * 100 + ".0" + assert_equal(s, BigDecimal(s).to_s("F")) + end + + def test_ctov + assert_equal(0.1, BigDecimal.new("1E-1")) + assert_equal(10, BigDecimal.new("1E+1")) + assert_equal(1, BigDecimal.new("+1")) + BigDecimal.mode(BigDecimal::EXCEPTION_OVERFLOW, false) + + assert_equal(BigDecimal::SIGN_POSITIVE_INFINITE, BigDecimal.new("1E1" + "0" * 10000).sign) + assert_equal(BigDecimal::SIGN_NEGATIVE_INFINITE, BigDecimal.new("-1E1" + "0" * 10000).sign) + assert_equal(BigDecimal::SIGN_POSITIVE_ZERO, BigDecimal.new("1E-1" + "0" * 10000).sign) + assert_equal(BigDecimal::SIGN_NEGATIVE_ZERO, BigDecimal.new("-1E-1" + "0" * 10000).sign) + end +end diff --git a/test/csv/tc_csv_parsing.rb b/test/csv/tc_csv_parsing.rb index 965af929f3..635ae90531 100644 --- a/test/csv/tc_csv_parsing.rb +++ b/test/csv/tc_csv_parsing.rb @@ -1,4 +1,5 @@ -#!/usr/local/bin/ruby -w +#!/usr/bin/env ruby -w +# encoding: UTF-8 # tc_csv_parsing.rb # @@ -7,6 +8,7 @@ # under the terms of Ruby's license. require "test/unit" +require "timeout" require "csv" @@ -17,6 +19,8 @@ require "csv" # separator <tt>$/</tt>. # class TestCSVParsing < Test::Unit::TestCase + BIG_DATA = "123456789\n" * 1024 + def test_mastering_regex_example ex = %Q{Ten Thousand,10000, 2710 ,,"10,000","It's ""10 Grand"", baby",10K} assert_equal( [ "Ten Thousand", "10000", " 2710 ", nil, "10,000", @@ -158,7 +162,31 @@ class TestCSVParsing < Test::Unit::TestCase assert_send([csv.lineno, :<, 4]) end rescue CSV::MalformedCSVError - assert_equal("Unclosed quoted field on line 4.", $!.message) + assert_equal("Illegal quoting on line 4.", $!.message) + end + end + + def test_the_parse_fails_fast_when_it_can_for_unquoted_fields + assert_parse_errors_out('valid,fields,bad start"' + BIG_DATA) + end + + def test_the_parse_fails_fast_when_it_can_for_unescaped_quotes + assert_parse_errors_out('valid,fields,"bad start"unescaped' + BIG_DATA) + end + + def test_field_size_limit_controls_lookahead + assert_parse_errors_out( 'valid,fields,"' + BIG_DATA + '"', + :field_size_limit => 2048 ) + end + + private + + def assert_parse_errors_out(*args) + assert_raise(CSV::MalformedCSVError) do + Timeout.timeout(0.2) do + CSV.parse(*args) + fail("Parse didn't error out") + end end end end diff --git a/test/csv/tc_csv_writing.rb b/test/csv/tc_csv_writing.rb index 4677284306..a1ce4de97e 100644 --- a/test/csv/tc_csv_writing.rb +++ b/test/csv/tc_csv_writing.rb @@ -1,4 +1,5 @@ -#!/usr/local/bin/ruby -w +#!/usr/bin/env ruby -w +# encoding: UTF-8 # tc_csv_writing.rb # diff --git a/test/csv/tc_data_converters.rb b/test/csv/tc_data_converters.rb index 24c6b6b76f..acf27a6669 100644 --- a/test/csv/tc_data_converters.rb +++ b/test/csv/tc_data_converters.rb @@ -1,4 +1,5 @@ -#!/usr/local/bin/ruby -w +#!/usr/bin/env ruby -w +# encoding: UTF-8 # tc_data_converters.rb # diff --git a/test/csv/tc_features.rb b/test/csv/tc_features.rb index ae5a8a451b..ad6732a1ec 100644 --- a/test/csv/tc_features.rb +++ b/test/csv/tc_features.rb @@ -1,4 +1,5 @@ -#!/usr/local/bin/ruby -w +#!/usr/bin/env ruby -w +# encoding: UTF-8 # tc_features.rb # @@ -67,18 +68,25 @@ class TestCSVFeatures < Test::Unit::TestCase end end + def test_csv_char_readers + %w[col_sep row_sep quote_char].each do |reader| + csv = CSV.new("abc,def", reader.to_sym => "|") + assert_equal("|", csv.send(reader)) + end + end + def test_row_sep_auto_discovery ["\r\n", "\n", "\r"].each do |line_end| data = "1,2,3#{line_end}4,5#{line_end}" - discovered = CSV.new(data).instance_eval { @row_sep } + discovered = CSV.new(data).row_sep assert_equal(line_end, discovered) end - assert_equal("\n", CSV.new("\n\r\n\r").instance_eval { @row_sep }) + assert_equal("\n", CSV.new("\n\r\n\r").row_sep) - assert_equal($/, CSV.new("").instance_eval { @row_sep }) + assert_equal($/, CSV.new("").row_sep) - assert_equal($/, CSV.new(STDERR).instance_eval { @row_sep }) + assert_equal($/, CSV.new(STDERR).row_sep) end def test_lineno @@ -117,6 +125,51 @@ class TestCSVFeatures < Test::Unit::TestCase assert_equal(3, count) end + def test_csv_behavior_readers + %w[ unconverted_fields return_headers write_headers + skip_blanks force_quotes ].each do |behavior| + assert( !CSV.new("abc,def").send("#{behavior}?"), + "Behavior defaulted to on." ) + csv = CSV.new("abc,def", behavior.to_sym => true) + assert(csv.send("#{behavior}?"), "Behavior change now registered.") + end + end + + def test_converters_reader + # no change + assert_equal( [:integer], + CSV.new("abc,def", :converters => [:integer]).converters ) + + # just one + assert_equal( [:integer], + CSV.new("abc,def", :converters => :integer).converters ) + + # expanded + assert_equal( [:integer, :float], + CSV.new("abc,def", :converters => :numeric).converters ) + + # custom + csv = CSV.new("abc,def", :converters => [:integer, lambda { }]) + assert_equal(2, csv.converters.size) + assert_equal(:integer, csv.converters.first) + assert_instance_of(Proc, csv.converters.last) + end + + def test_header_converters_reader + # no change + hc = :header_converters + assert_equal([:downcase], CSV.new("abc,def", hc => [:downcase]).send(hc)) + + # just one + assert_equal([:downcase], CSV.new("abc,def", hc => :downcase).send(hc)) + + # custom + csv = CSV.new("abc,def", hc => [:symbol, lambda { }]) + assert_equal(2, csv.send(hc).size) + assert_equal(:symbol, csv.send(hc).first) + assert_instance_of(Proc, csv.send(hc).last) + end + # reported by Kev Jackson def test_failing_to_escape_col_sep_bug_fix assert_nothing_raised(Exception) { CSV.new(String.new, :col_sep => "|") } @@ -149,7 +202,7 @@ class TestCSVFeatures < Test::Unit::TestCase ) ) end - assert_equal("\r\n", zipped.instance_eval { @row_sep }) + assert_equal("\r\n", zipped.row_sep) end def test_gzip_writer_bug_fix @@ -168,6 +221,41 @@ class TestCSVFeatures < Test::Unit::TestCase File.unlink(file) end + def test_inspect_is_smart_about_io_types + str = CSV.new("string,data").inspect + assert(str.include?("io_type:StringIO"), "IO type not detected.") + + str = CSV.new($stderr).inspect + assert(str.include?("io_type:$stderr"), "IO type not detected.") + + path = File.join(File.dirname(__FILE__), "temp.csv") + File.open(path, "w") { |csv| csv << "one,two,three\n1,2,3\n" } + str = CSV.open(path) { |csv| csv.inspect } + assert(str.include?("io_type:File"), "IO type not detected.") + File.unlink(path) + end + + def test_inspect_shows_key_attributes + str = @csv.inspect + %w[lineno col_sep row_sep quote_char].each do |attr_name| + assert_match(/\b#{attr_name}:[^\s>]+/, str) + end + end + + def test_inspect_shows_headers_when_available + CSV.new("one,two,three\n1,2,3\n", :headers => true) do |csv| + assert(csv.inspect.include?("headers:true"), "Header hint not shown.") + csv.shift # load headers + assert_match(/headers:\[[^\]]+\]/, csv.inspect) + end + end + + def test_inspect_is_ascii_8bit_encoded + CSV.new("one,two,three\n1,2,3\n".encode("UTF-16BE")) do |csv| + assert_equal("ASCII-8BIT", csv.inspect.encoding.name) + end + end + def test_version assert_not_nil(CSV::VERSION) assert_instance_of(String, CSV::VERSION) diff --git a/test/csv/tc_headers.rb b/test/csv/tc_headers.rb index 74e2f54ad4..e0f544dadf 100644 --- a/test/csv/tc_headers.rb +++ b/test/csv/tc_headers.rb @@ -1,4 +1,5 @@ -#!/usr/local/bin/ruby -w +#!/usr/bin/env ruby -w +# encoding: UTF-8 # tc_headers.rb # @@ -129,6 +130,21 @@ class TestCSVHeaders < Test::Unit::TestCase assert(!row.field_row?) end + def test_csv_header_string_inherits_separators + # parse with custom col_sep + csv = nil + assert_nothing_raised(Exception) do + csv = CSV.parse( @data.tr(",", "|"), :col_sep => "|", + :headers => "my|new|headers" ) + end + + # verify headers were recognized + row = csv[0] + assert_not_nil(row) + assert_instance_of(CSV::Row, row) + assert_equal([%w{my first}, %w{new second}, %w{headers third}], row.to_a) + end + def test_return_headers # activate headers and request they are returned csv = nil @@ -250,6 +266,17 @@ class TestCSVHeaders < Test::Unit::TestCase end end + def test_headers_reader + # no headers + assert_nil(CSV.new(@data).headers) + + # headers + csv = CSV.new(@data, :headers => true) + assert_equal(true, csv.headers) # before headers are read + csv.shift # set headers + assert_equal(%w[first second third], csv.headers) # after headers are read + end + def test_blank_row_bug_fix @data += "\n#{@data}" # add a blank row diff --git a/test/csv/tc_interface.rb b/test/csv/tc_interface.rb index e8cc920f9d..9cacc28b0a 100644 --- a/test/csv/tc_interface.rb +++ b/test/csv/tc_interface.rb @@ -1,4 +1,5 @@ -#!/usr/local/bin/ruby -w +#!/usr/bin/env ruby -w +# encoding: UTF-8 # tc_interface.rb # @@ -42,8 +43,9 @@ class TestCSVInterface < Test::Unit::TestCase csv.close assert(csv.closed?) - ret = CSV.open(@path) do |csv| - assert_instance_of(CSV, csv) + ret = CSV.open(@path) do |new_csv| + csv = new_csv + assert_instance_of(CSV, new_csv) "Return value." end assert(csv.closed?) @@ -161,7 +163,6 @@ class TestCSVInterface < Test::Unit::TestCase lines = [{:a => 1, :b => 2, :c => 3}, {:a => 4, :b => 5, :c => 6}] CSV.open( @path, "w", :headers => true, - :converters => :all, :header_converters => :symbol ) do |csv| csv << lines.first.keys lines.each { |line| csv << line } @@ -173,6 +174,74 @@ class TestCSVInterface < Test::Unit::TestCase end end + def test_write_hash_with_headers_array + File.unlink(@path) + + lines = [{:a => 1, :b => 2, :c => 3}, {:a => 4, :b => 5, :c => 6}] + CSV.open(@path, "w", :headers => [:b, :a, :c]) do |csv| + lines.each { |line| csv << line } + end + + # test writing fields in the correct order + File.open(@path, "r") do |f| + assert_equal("2,1,3", f.gets.strip) + assert_equal("5,4,6", f.gets.strip) + end + + # test reading CSV with headers + CSV.open( @path, "r", :headers => [:b, :a, :c], + :converters => :all ) do |csv| + csv.each { |line| assert_equal(lines.shift, line.to_hash) } + end + end + + def test_write_hash_with_headers_string + File.unlink(@path) + + lines = [{"a" => 1, "b" => 2, "c" => 3}, {"a" => 4, "b" => 5, "c" => 6}] + CSV.open(@path, "w", :headers => "b|a|c", :col_sep => "|") do |csv| + lines.each { |line| csv << line } + end + + # test writing fields in the correct order + File.open(@path, "r") do |f| + assert_equal("2|1|3", f.gets.strip) + assert_equal("5|4|6", f.gets.strip) + end + + # test reading CSV with headers + CSV.open( @path, "r", :headers => "b|a|c", + :col_sep => "|", + :converters => :all ) do |csv| + csv.each { |line| assert_equal(lines.shift, line.to_hash) } + end + end + + def test_write_headers + File.unlink(@path) + + lines = [{"a" => 1, "b" => 2, "c" => 3}, {"a" => 4, "b" => 5, "c" => 6}] + CSV.open( @path, "w", :headers => "b|a|c", + :write_headers => true, + :col_sep => "|" ) do |csv| + lines.each { |line| csv << line } + end + + # test writing fields in the correct order + File.open(@path, "r") do |f| + assert_equal("b|a|c", f.gets.strip) + assert_equal("2|1|3", f.gets.strip) + assert_equal("5|4|6", f.gets.strip) + end + + # test reading CSV with headers + CSV.open( @path, "r", :headers => true, + :col_sep => "|", + :converters => :all ) do |csv| + csv.each { |line| assert_equal(lines.shift, line.to_hash) } + end + end + def test_append # aliased add_row() and puts() File.unlink(@path) @@ -230,6 +299,6 @@ class TestCSVInterface < Test::Unit::TestCase # shortcuts assert_equal(STDOUT, CSV.instance.instance_eval { @io }) - assert_equal(STDOUT, CSV { |csv| csv.instance_eval { @io } }) + assert_equal(STDOUT, CSV { |new_csv| new_csv.instance_eval { @io } }) end end diff --git a/test/csv/tc_row.rb b/test/csv/tc_row.rb index a9b7f042b2..3fa3784bba 100644 --- a/test/csv/tc_row.rb +++ b/test/csv/tc_row.rb @@ -1,4 +1,5 @@ -#!/usr/local/bin/ruby -w +#!/usr/bin/env ruby -w +# encoding: UTF-8 # tc_row.rb # @@ -286,4 +287,24 @@ class TestCSVRow < Test::Unit::TestCase assert_equal([@row.headers.size, @row.fields.size].max, @row.size) end + + def test_inspect_shows_header_field_pairs + str = @row.inspect + @row.each do |header, field| + assert( str.include?("#{header.inspect}:#{field.inspect}"), + "Header field pair not found." ) + end + end + + def test_inspect_is_ascii_8bit_encoded + assert_equal("ASCII-8BIT", @row.inspect.encoding.name) + end + + def test_inspect_shows_symbol_headers_as_bare_attributes + str = CSV::Row.new(@row.headers.map { |h| h.to_sym }, @row.fields).inspect + @row.each do |header, field| + assert( str.include?("#{header}:#{field.inspect}"), + "Header field pair not found." ) + end + end end diff --git a/test/csv/tc_serialization.rb b/test/csv/tc_serialization.rb index d9c37fde21..c8273bdb3c 100644 --- a/test/csv/tc_serialization.rb +++ b/test/csv/tc_serialization.rb @@ -1,4 +1,5 @@ -#!/usr/local/bin/ruby -w +#!/usr/bin/env ruby -w +# encoding: UTF-8 # tc_serialization.rb # diff --git a/test/csv/tc_table.rb b/test/csv/tc_table.rb index 028274d97f..1e572d979c 100644 --- a/test/csv/tc_table.rb +++ b/test/csv/tc_table.rb @@ -1,4 +1,5 @@ -#!/usr/local/bin/ruby -w +#!/usr/bin/env ruby -w +# encoding: UTF-8 # tc_table.rb # @@ -389,4 +390,17 @@ class TestCSVTable < Test::Unit::TestCase assert_equal(@rows.size, @table.size) end + + def test_inspect_shows_current_mode + str = @table.inspect + assert(str.include?("mode:#{@table.mode}"), "Mode not shown.") + + @table.by_col! + str = @table.inspect + assert(str.include?("mode:#{@table.mode}"), "Mode not shown.") + end + + def test_inspect_is_us_ascii_encoded + assert_equal("US-ASCII", @table.inspect.encoding.name) + end end diff --git a/test/csv/ts_all.rb b/test/csv/ts_all.rb index c930523757..d380ab5318 100644 --- a/test/csv/ts_all.rb +++ b/test/csv/ts_all.rb @@ -1,4 +1,5 @@ -#!/usr/local/bin/ruby -w +#!/usr/bin/env ruby -w +# encoding: UTF-8 # ts_all.rb # @@ -17,3 +18,4 @@ require "tc_row" require "tc_table" require "tc_headers" require "tc_serialization" +require "tc_encodings" diff --git a/test/json/fixtures/fail18.json b/test/json/fixtures/fail18.json index d61a345465..e2d130c6eb 100644 --- a/test/json/fixtures/fail18.json +++ b/test/json/fixtures/fail18.json @@ -1 +1 @@ -[[[[[[[[[[[[[[[[[[[["Too deep"]]]]]]]]]]]]]]]]]]]] // No, we don't limit our depth: Moved to pass... +[[[[[[[[[[[[[[[[[[[["Too deep"]]]]]]]]]]]]]]]]]]]] diff --git a/test/json/runner.rb b/test/json/runner.rb index 91bc12a594..9ad04485bf 100755 --- a/test/json/runner.rb +++ b/test/json/runner.rb @@ -23,4 +23,3 @@ class TS_AllTests end end Test::Unit::UI::Console::TestRunner.run(TS_AllTests) - # vim: set et sw=2 ts=2: diff --git a/test/json/test_json.rb b/test/json/test_json.rb index 257b8e0f63..0ee993eb88 100755 --- a/test/json/test_json.rb +++ b/test/json/test_json.rb @@ -3,6 +3,7 @@ require 'test/unit' require 'json' +require 'stringio' class TC_JSON < Test::Unit::TestCase include JSON @@ -61,6 +62,12 @@ class TC_JSON < Test::Unit::TestCase assert_equal_float [3.141], parse('[3141.0E-3]') assert_equal_float [-3.141], parse('[-3141.0e-3]') assert_equal_float [-3.141], parse('[-3141e-3]') + assert_raises(ParserError) { parse('[NaN]') } + assert parse('[NaN]', :allow_nan => true).first.nan? + assert_raises(ParserError) { parse('[Infinity]') } + assert_equal [1.0/0], parse('[Infinity]', :allow_nan => true) + assert_raises(ParserError) { parse('[-Infinity]') } + assert_equal [-1.0/0], parse('[-Infinity]', :allow_nan => true) assert_equal([""], parse('[""]')) assert_equal(["foobar"], parse('["foobar"]')) assert_equal([{}], parse('[{}]')) @@ -238,18 +245,49 @@ EOT end def test_nesting - to_deep = '[[[[[[[[[[[[[[[[[[[["Too deep"]]]]]]]]]]]]]]]]]]]]' - assert_raises(JSON::NestingError) { JSON.parse to_deep } - assert_raises(JSON::NestingError) { JSON.parser.new(to_deep).parse } - assert_raises(JSON::NestingError) { JSON.parse to_deep, :max_nesting => 19 } - ok = JSON.parse to_deep, :max_nesting => 20 - assert_kind_of Array, ok - ok = JSON.parse to_deep, :max_nesting => nil - assert_kind_of Array, ok - ok = JSON.parse to_deep, :max_nesting => false - assert_kind_of Array, ok - ok = JSON.parse to_deep, :max_nesting => 0 - assert_kind_of Array, ok + assert_raises(JSON::NestingError) { JSON.parse '[[]]', :max_nesting => 1 } + assert_raises(JSON::NestingError) { JSON.parser.new('[[]]', :max_nesting => 1).parse } + assert_equal [[]], JSON.parse('[[]]', :max_nesting => 2) + too_deep = '[[[[[[[[[[[[[[[[[[[["Too deep"]]]]]]]]]]]]]]]]]]]]' + too_deep_ary = eval too_deep + assert_raises(JSON::NestingError) { JSON.parse too_deep } + assert_raises(JSON::NestingError) { JSON.parser.new(too_deep).parse } + assert_raises(JSON::NestingError) { JSON.parse too_deep, :max_nesting => 19 } + ok = JSON.parse too_deep, :max_nesting => 20 + assert_equal too_deep_ary, ok + ok = JSON.parse too_deep, :max_nesting => nil + assert_equal too_deep_ary, ok + ok = JSON.parse too_deep, :max_nesting => false + assert_equal too_deep_ary, ok + ok = JSON.parse too_deep, :max_nesting => 0 + assert_equal too_deep_ary, ok + assert_raises(JSON::NestingError) { JSON.generate [[]], :max_nesting => 1 } + assert_equal '[[]]', JSON.generate([[]], :max_nesting => 2) + assert_raises(JSON::NestingError) { JSON.generate too_deep_ary } + assert_raises(JSON::NestingError) { JSON.generate too_deep_ary, :max_nesting => 19 } + ok = JSON.generate too_deep_ary, :max_nesting => 20 + assert_equal too_deep, ok + ok = JSON.generate too_deep_ary, :max_nesting => nil + assert_equal too_deep, ok + ok = JSON.generate too_deep_ary, :max_nesting => false + assert_equal too_deep, ok + ok = JSON.generate too_deep_ary, :max_nesting => 0 + assert_equal too_deep, ok + end + + def test_load_dump + too_deep = '[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]' + assert_equal too_deep, JSON.dump(eval(too_deep)) + assert_kind_of String, Marshal.dump(eval(too_deep)) + assert_raises(ArgumentError) { JSON.dump(eval(too_deep), 19) } + assert_raises(ArgumentError) { Marshal.dump(eval(too_deep), 19) } + assert_equal too_deep, JSON.dump(eval(too_deep), 20) + assert_kind_of String, Marshal.dump(eval(too_deep), 20) + output = StringIO.new + JSON.dump(eval(too_deep), output) + assert_equal too_deep, output.string + output = StringIO.new + JSON.dump(eval(too_deep), output, 20) + assert_equal too_deep, output.string end end - # vim: set et sw=2 ts=2: diff --git a/test/json/test_json_addition.rb b/test/json/test_json_addition.rb index 04c690ce31..cf8a92ae10 100755 --- a/test/json/test_json_addition.rb +++ b/test/json/test_json_addition.rb @@ -31,6 +31,10 @@ class TC_JSONAddition < Test::Unit::TestCase end class B + def self.json_creatable? + false + end + def to_json(*args) { 'json_class' => self.class.name, @@ -50,9 +54,6 @@ class TC_JSONAddition < Test::Unit::TestCase end end - def setup - end - def test_extended_json a = A.new(666) assert A.json_creatable? @@ -77,14 +78,14 @@ class TC_JSONAddition < Test::Unit::TestCase ) end - def test_extended_json_fail + def test_extended_json_fail1 b = B.new assert !B.json_creatable? json = generate(b) - assert_equal({ 'json_class' => B.name }, JSON.parse(json)) + assert_equal({ "json_class"=>"TC_JSONAddition::B" }, JSON.parse(json)) end - def test_extended_json_fail + def test_extended_json_fail2 c = C.new assert !C.json_creatable? json = generate(c) @@ -111,9 +112,11 @@ EOT assert_equal raw, raw_again end + MyJsonStruct = Struct.new 'MyJsonStruct', :foo, :bar + def test_core - t = Time.at(1198254128, 895990) - assert_equal t, JSON(JSON(t)) + t = Time.now + assert_equal t.inspect, JSON(JSON(t)).inspect d = Date.today assert_equal d, JSON(JSON(d)) d = DateTime.civil(2007, 6, 14, 14, 57, 10, Rational(1, 12), 2299161) @@ -122,8 +125,7 @@ EOT assert_equal 1...10, JSON(JSON(1...10)) assert_equal "a".."c", JSON(JSON("a".."c")) assert_equal "a"..."c", JSON(JSON("a"..."c")) - struct = Struct.new 'MyJsonStruct', :foo, :bar - s = struct.new 4711, 'foot' + s = MyJsonStruct.new 4711, 'foot' assert_equal s, JSON(JSON(s)) struct = Struct.new :foo, :bar s = struct.new 4711, 'foot' @@ -137,7 +139,19 @@ EOT assert_equal e.message, e_again.message assert_equal e.backtrace, e_again.backtrace end - assert_equal /foo/, JSON(JSON(/foo/)) - assert_equal /foo/i, JSON(JSON(/foo/i)) + assert_equal(/foo/, JSON(JSON(/foo/))) + assert_equal(/foo/i, JSON(JSON(/foo/i))) + end + + def test_utc_datetime + now = Time.now + d = DateTime.parse(now.to_s) # usual case + assert d, JSON.parse(d.to_json) + d = DateTime.parse(now.utc.to_s) # of = 0 + assert d, JSON.parse(d.to_json) + d = DateTime.civil(2008, 6, 17, 11, 48, 32, 1) # of = 1 / 12 => 1/12 + assert d, JSON.parse(d.to_json) + d = DateTime.civil(2008, 6, 17, 11, 48, 32, 12) # of = 12 / 12 => 12 + assert d, JSON.parse(d.to_json) end end diff --git a/test/json/test_json_generate.rb b/test/json/test_json_generate.rb index e720b2d862..b7e0bc22c6 100644 --- a/test/json/test_json_generate.rb +++ b/test/json/test_json_generate.rb @@ -70,6 +70,7 @@ EOT #assert s.check_circular h = { 1=>2 } h[3] = h + assert_raises(JSON::CircularDatastructure) { generate(h) } assert_raises(JSON::CircularDatastructure) { generate(h, s) } s = JSON.state.new(:check_circular => true) #assert s.check_circular @@ -77,4 +78,22 @@ EOT a << a assert_raises(JSON::CircularDatastructure) { generate(a, s) } end + + def test_allow_nan + assert_raises(GeneratorError) { generate([JSON::NaN]) } + assert_equal '[NaN]', generate([JSON::NaN], :allow_nan => true) + assert_equal '[NaN]', fast_generate([JSON::NaN]) + assert_raises(GeneratorError) { pretty_generate([JSON::NaN]) } + assert_equal "[\n NaN\n]", pretty_generate([JSON::NaN], :allow_nan => true) + assert_raises(GeneratorError) { generate([JSON::Infinity]) } + assert_equal '[Infinity]', generate([JSON::Infinity], :allow_nan => true) + assert_equal '[Infinity]', fast_generate([JSON::Infinity]) + assert_raises(GeneratorError) { pretty_generate([JSON::Infinity]) } + assert_equal "[\n Infinity\n]", pretty_generate([JSON::Infinity], :allow_nan => true) + assert_raises(GeneratorError) { generate([JSON::MinusInfinity]) } + assert_equal '[-Infinity]', generate([JSON::MinusInfinity], :allow_nan => true) + assert_equal '[-Infinity]', fast_generate([JSON::MinusInfinity]) + assert_raises(GeneratorError) { pretty_generate([JSON::MinusInfinity]) } + assert_equal "[\n -Infinity\n]", pretty_generate([JSON::MinusInfinity], :allow_nan => true) + end end diff --git a/test/json/test_json_rails.rb b/test/json/test_json_rails.rb index e44ea606e3..c0447ddaba 100644 --- a/test/json/test_json_rails.rb +++ b/test/json/test_json_rails.rb @@ -31,6 +31,10 @@ class TC_JSONRails < Test::Unit::TestCase end class B + def self.json_creatable? + false + end + def to_json(*args) { 'json_class' => self.class.name, @@ -46,9 +50,6 @@ class TC_JSONRails < Test::Unit::TestCase end end - def setup - end - def test_extended_json a = A.new(666) assert A.json_creatable? @@ -73,14 +74,14 @@ class TC_JSONRails < Test::Unit::TestCase ) end - def test_extended_json_fail + def test_extended_json_fail1 b = B.new assert !B.json_creatable? json = generate(b) assert_equal({ 'json_class' => B.name }, JSON.parse(json)) end - def test_extended_json_fail + def test_extended_json_fail2 c = C.new # with rails addition all objects are theoretically creatable assert C.json_creatable? json = generate(c) diff --git a/test/json/test_json_unicode.rb b/test/json/test_json_unicode.rb index cad93846b5..862c6ea1e0 100755 --- a/test/json/test_json_unicode.rb +++ b/test/json/test_json_unicode.rb @@ -7,9 +7,6 @@ require 'json' class TC_JSONUnicode < Test::Unit::TestCase include JSON - def setup - end - def test_unicode assert_equal '""', ''.to_json assert_equal '"\\b"', "\b".to_json @@ -54,7 +51,7 @@ class TC_JSONUnicode < Test::Unit::TestCase end end assert_raises(JSON::GeneratorError) do - JSON.generate(["" << 0x80]) + JSON.generate(["\x80"]) end assert_equal "\302\200", JSON.parse('["\u0080"]').first end diff --git a/test/matrix/test_matrix.rb b/test/matrix/test_matrix.rb new file mode 100644 index 0000000000..0850117b86 --- /dev/null +++ b/test/matrix/test_matrix.rb @@ -0,0 +1,49 @@ +require 'test/unit' +require 'matrix' + +class TestMatrix < Test::Unit::TestCase + def setup + @m1 = Matrix[[1,2,3], [4,5,6]] + @m2 = Matrix[[1,2,3], [4,5,6]] + @m3 = @m1.clone + @m4 = Matrix[[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]] + @n1 = Matrix[[2,3,4], [5,6,7]] + end + + def test_identity + assert_same @m1, @m1 + assert_not_same @m1, @m2 + assert_not_same @m1, @m3 + assert_not_same @m1, @m4 + assert_not_same @m1, @n1 + end + + def test_equality + assert_equal @m1, @m1 + assert_equal @m1, @m2 + assert_equal @m1, @m3 + assert_equal @m1, @m4 + assert_not_equal @m1, @n1 + end + + def test_hash_equality + assert @m1.eql?(@m1) + assert @m1.eql?(@m2) + assert @m1.eql?(@m3) + assert !@m1.eql?(@m4) + assert !@m1.eql?(@n1) + + hash = { @m1 => :value } + assert hash.key?(@m1) + assert hash.key?(@m2) + assert hash.key?(@m3) + assert !hash.key?(@m4) + assert !hash.key?(@n1) + end + + def test_hash + assert_equal @m1.hash, @m1.hash + assert_equal @m1.hash, @m2.hash + assert_equal @m1.hash, @m3.hash + end +end diff --git a/test/matrix/test_vector.rb b/test/matrix/test_vector.rb new file mode 100644 index 0000000000..95a39693fc --- /dev/null +++ b/test/matrix/test_vector.rb @@ -0,0 +1,49 @@ +require 'test/unit' +require 'matrix' + +class TestVector < Test::Unit::TestCase + def setup + @v1 = Vector[1,2,3] + @v2 = Vector[1,2,3] + @v3 = @v1.clone + @v4 = Vector[1.0, 2.0, 3.0] + @w1 = Vector[2,3,4] + end + + def test_identity + assert_same @v1, @v1 + assert_not_same @v1, @v2 + assert_not_same @v1, @v3 + assert_not_same @v1, @v4 + assert_not_same @v1, @w1 + end + + def test_equality + assert_equal @v1, @v1 + assert_equal @v1, @v2 + assert_equal @v1, @v3 + assert_equal @v1, @v4 + assert_not_equal @v1, @w1 + end + + def test_hash_equality + assert @v1.eql?(@v1) + assert @v1.eql?(@v2) + assert @v1.eql?(@v3) + assert !@v1.eql?(@v4) + assert !@v1.eql?(@w1) + + hash = { @v1 => :value } + assert hash.key?(@v1) + assert hash.key?(@v2) + assert hash.key?(@v3) + assert !hash.key?(@v4) + assert !hash.key?(@w1) + end + + def test_hash + assert_equal @v1.hash, @v1.hash + assert_equal @v1.hash, @v2.hash + assert_equal @v1.hash, @v3.hash + end +end diff --git a/test/ruby/test_complex.rb b/test/ruby/test_complex.rb index ef612e2279..e2417fe14d 100644 --- a/test/ruby/test_complex.rb +++ b/test/ruby/test_complex.rb @@ -4,6 +4,14 @@ class ComplexSub < Complex; end class Complex_Test < Test::Unit::TestCase + def setup + @rational = defined?(Rational) + if @rational + @keiju = Rational.instance_variable_get('@RCS_ID') + end + @unify = defined?(Complex::Unify) + end + def test_compsub c = ComplexSub.__send__(:new, 1) cc = ComplexSub.__send__(:convert, 1) @@ -11,7 +19,7 @@ class Complex_Test < Test::Unit::TestCase assert_kind_of(Numeric, c) assert_kind_of(Numeric, cc) - if defined?(ComplexSub::Unify) + if @unify assert_instance_of(Fixnum, c) assert_instance_of(Fixnum, cc) else @@ -42,7 +50,7 @@ class Complex_Test < Test::Unit::TestCase assert_equal(true, c.eql?(c2)) assert_equal(false, c.eql?(c3)) - if defined?(Complex::Unify) + if @unify assert_equal(true, c.eql?(0)) else assert_equal(false, c.eql?(0)) @@ -51,6 +59,7 @@ class Complex_Test < Test::Unit::TestCase def test_hash assert_instance_of(Fixnum, Complex(1,2).hash) + assert_instance_of(Fixnum, Complex(1.0,2.0).hash) h = {} h[Complex(0)] = 0 @@ -63,12 +72,15 @@ class Complex_Test < Test::Unit::TestCase h[Complex(0,0)] = 9 assert_equal(4, h.size) + + h[Complex(0.0,0.0)] = 9.0 + assert_equal(5, h.size) end def test_freeze c = Complex(1) c.freeze - unless defined?(Complex::Unify) + unless @unify assert_equal(true, c.frozen?) end assert_instance_of(String, c.to_s) @@ -77,45 +89,45 @@ class Complex_Test < Test::Unit::TestCase def test_new_bang # no unify assert_instance_of(Complex, Complex.__send__(:new!, 2,0)) assert_equal([2,0], Complex.__send__(:new!, 2,0). - instance_eval{[real, image]}) + instance_eval{[real, imag]}) assert_equal([2,4], Complex.__send__(:new!, 2,4). - instance_eval{[real, image]}) + instance_eval{[real, imag]}) assert_equal([-2,4], Complex.__send__(:new!, -2,4). - instance_eval{[real, image]}) + instance_eval{[real, imag]}) assert_equal([2,-4], Complex.__send__(:new!, 2,-4). - instance_eval{[real, image]}) + instance_eval{[real, imag]}) assert_equal([-2,-4], Complex.__send__(:new!, -2,-4). - instance_eval{[real, image]}) + instance_eval{[real, imag]}) assert_equal([2,0], Complex.__send__(:new!, Complex(2)). - instance_eval{[real, image]}) + instance_eval{[real, imag]}) assert_equal([2,3], Complex.__send__(:new!, Complex(2), Complex(3)). - instance_eval{[real, image]}) + instance_eval{[real, imag]}) assert_equal([2,3], Complex.__send__(:new!, 2, Complex(3)). - instance_eval{[real, image]}) + instance_eval{[real, imag]}) assert_equal([1.1,0], Complex.__send__(:new!, 1.1). - instance_eval{[real, image]}) + instance_eval{[real, imag]}) assert_equal([-1.1,0], Complex.__send__(:new!, -1.1). - instance_eval{[real, image]}) + instance_eval{[real, imag]}) assert_equal([1,0], Complex.__send__(:new!, '1'). - instance_eval{[real, image]}) + instance_eval{[real, imag]}) assert_equal([0,0], Complex.__send__(:new!, nil). - instance_eval{[real, image]}) + instance_eval{[real, imag]}) end def test_new assert_instance_of(Complex, Complex.__send__(:new, 2,0.0)) - if defined?(Complex::Unify) + if @unify assert_instance_of(Fixnum, Complex.__send__(:new, 2,0)) else assert_instance_of(Complex, Complex.__send__(:new, 2,0)) - assert_equal([2,0], Complex.__send__(:new, 2,0). instance_eval{[real, image]}) + assert_equal([2,0], Complex.__send__(:new, 2,0). instance_eval{[real, imag]}) end - assert_equal([2,4], Complex.__send__(:new, 2,4).instance_eval{[real, image]}) - assert_equal([-2,4], Complex.__send__(:new, -2,4).instance_eval{[real, image]}) - assert_equal([2,-4], Complex.__send__(:new, 2,-4).instance_eval{[real, image]}) - assert_equal([-2,-4], Complex.__send__(:new, -2,-4).instance_eval{[real, image]}) + assert_equal([2,4], Complex.__send__(:new, 2,4).instance_eval{[real, imag]}) + assert_equal([-2,4], Complex.__send__(:new, -2,4).instance_eval{[real, imag]}) + assert_equal([2,-4], Complex.__send__(:new, 2,-4).instance_eval{[real, imag]}) + assert_equal([-2,-4], Complex.__send__(:new, -2,-4).instance_eval{[real, imag]}) assert_raise(ArgumentError){Complex.__send__(:new, Complex(1,2),2)} assert_raise(ArgumentError){Complex.__send__(:new, 2,Complex(1,2))} @@ -134,44 +146,38 @@ class Complex_Test < Test::Unit::TestCase c = Complex(2**32, 2**32) assert_equal(Complex.__send__(:new, 2**32,2**32), c) - assert_equal([2**32,2**32], [c.real,c.image]) + assert_equal([2**32,2**32], [c.real,c.imag]) c = Complex(-2**32, 2**32) assert_equal(Complex.__send__(:new, -2**32,2**32), c) - assert_equal([-2**32,2**32], [c.real,c.image]) + assert_equal([-2**32,2**32], [c.real,c.imag]) c = Complex(2**32, -2**32) assert_equal(Complex.__send__(:new, 2**32,-2**32), c) - assert_equal([2**32,-2**32], [c.real,c.image]) + assert_equal([2**32,-2**32], [c.real,c.imag]) c = Complex(-2**32, -2**32) assert_equal(Complex.__send__(:new, -2**32,-2**32), c) - assert_equal([-2**32,-2**32], [c.real,c.image]) + assert_equal([-2**32,-2**32], [c.real,c.imag]) - c = Complex(Complex(1),0) - assert_equal(Complex.__send__(:new, 1,0), c) + c = Complex(Complex(1,2),2) + assert_equal(Complex.__send__(:new, 1,4), c) - c = Complex(0,Complex(1)) + c = Complex(2,Complex(1,2)) assert_equal(Complex.__send__(:new, 0,1), c) - c = 5.re - assert_equal(Complex.__send__(:new, 5,0), c) - - c = Complex(1,2).re - assert_equal(Complex.__send__(:new, 1,2), c) - - c = 5.im - assert_equal(Complex.__send__(:new, 0,5), c) - - c = Complex(2,0).im - assert_equal(Complex.__send__(:new, 0,2), c) - assert_raise(ArgumentError){Complex(1,2).im} + c = Complex(Complex(1,2),Complex(1,2)) + assert_equal(Complex.__send__(:new, -1,3), c) c = Complex::I assert_equal(Complex.__send__(:new, 0,1), c) assert_equal(Complex.__send__(:new, 1),Complex(1)) assert_equal(Complex.__send__(:new, 1),Complex('1')) + assert_equal(Complex.__send__(:new, 3.0,3.0),Complex('3.0','3.0')) + if @rational && !@keiju + assert_equal(Complex.__send__(:new, 1,1),Complex('3/3','3/3')) + end assert_raise(ArgumentError){Complex(nil)} assert_raise(ArgumentError){Complex(Object.new)} assert_raise(ArgumentError){Complex()} @@ -182,64 +188,63 @@ class Complex_Test < Test::Unit::TestCase c = Complex(4) assert_equal(4, c.real) - assert_equal(0, c.image) + assert_equal(0, c.imag) c = Complex(4,5) assert_equal(4, c.real) - assert_equal(5, c.image) + assert_equal(5, c.imag) if -0.0.to_s == '-0.0' c = Complex(-0.0,-0.0) assert_equal('-0.0', c.real.to_s) - assert_equal('-0.0', c.image.to_s) + assert_equal('-0.0', c.imag.to_s) end c = Complex.__send__(:new, 4) assert_equal(4, c.real) - assert_equal(0, c.image) - assert_equal(c.imag, c.image) + assert_equal(0, c.imag) + assert_equal(c.imag, c.imaginary) c = Complex.__send__(:new, 4,5) assert_equal(4, c.real) - assert_equal(5, c.image) - assert_equal(c.imag, c.image) + assert_equal(5, c.imag) + assert_equal(c.imag, c.imaginary) if -0.0.to_s == '-0.0' c = Complex.__send__(:new, -0.0,-0.0) assert_equal('-0.0', c.real.to_s) - assert_equal('-0.0', c.image.to_s) - assert_equal(c.imag.to_s, c.image.to_s) + assert_equal('-0.0', c.imag.to_s) + assert_equal(c.imag.to_s, c.imaginary.to_s) end c = Complex.__send__(:new!, 4) assert_equal(4, c.real) - assert_equal(c.imag, c.image) - assert_equal(0, c.image) + assert_equal(c.imag, c.imaginary) + assert_equal(0, c.imag) c = Complex.__send__(:new!, 4,5) assert_equal(4, c.real) - assert_equal(5, c.image) - assert_equal(c.imag, c.image) + assert_equal(5, c.imag) + assert_equal(c.imag, c.imaginary) c = Complex.__send__(:new!, -0.0,-0.0) assert_equal('-0.0', c.real.to_s) - assert_equal('-0.0', c.image.to_s) - assert_equal(c.imag.to_s, c.image.to_s) + assert_equal('-0.0', c.imag.to_s) + assert_equal(c.imag.to_s, c.imaginary.to_s) end def test_attr2 c = Complex(1) - if defined?(Complex::Unify) - assert_equal(true, c.scalar?) + if @unify =begin assert_equal(true, c.finite?) assert_equal(false, c.infinite?) @@ -247,13 +252,14 @@ class Complex_Test < Test::Unit::TestCase assert_equal(true, c.integer?) assert_equal(false, c.float?) assert_equal(true, c.rational?) +=end assert_equal(true, c.real?) +=begin assert_equal(false, c.complex?) assert_equal(true, c.exact?) assert_equal(false, c.inexact?) =end else - assert_equal(false, c.scalar?) =begin assert_equal(true, c.finite?) assert_equal(false, c.infinite?) @@ -261,7 +267,9 @@ class Complex_Test < Test::Unit::TestCase assert_equal(false, c.integer?) assert_equal(false, c.float?) assert_equal(false, c.rational?) +=end assert_equal(false, c.real?) +=begin assert_equal(true, c.complex?) assert_equal(true, c.exact?) assert_equal(false, c.inexact?) @@ -303,6 +311,16 @@ class Complex_Test < Test::Unit::TestCase assert_equal(Complex(-1,1), +Complex(-1,1)) assert_equal(Complex(1,-1), +Complex(1,-1)) assert_equal(Complex(-1,-1), +Complex(-1,-1)) + + if -0.0.to_s == '-0.0' + c = +Complex(0.0,0.0) + assert_equal('0.0', c.real.to_s) + assert_equal('0.0', c.imag.to_s) + + c = +Complex(-0.0,-0.0) + assert_equal('-0.0', c.real.to_s) + assert_equal('-0.0', c.imag.to_s) + end end def test_negate @@ -313,6 +331,16 @@ class Complex_Test < Test::Unit::TestCase assert_equal(Complex(-1,1), -Complex(1,-1)) assert_equal(Complex(1,1), -Complex(-1,-1)) + if -0.0.to_s == '-0.0' + c = -Complex(0.0,0.0) + assert_equal('-0.0', c.real.to_s) + assert_equal('-0.0', c.imag.to_s) + + c = -Complex(-0.0,-0.0) + assert_equal('0.0', c.real.to_s) + assert_equal('0.0', c.imag.to_s) + end + =begin assert_equal(0, Complex(0).negate) assert_equal(-2, Complex(2).negate) @@ -329,7 +357,7 @@ class Complex_Test < Test::Unit::TestCase assert_equal(Complex(3,2), c + 2) assert_equal(Complex(3.0,2), c + 2.0) - if defined?(Rational) + if @rational assert_equal(Complex(Rational(3,1),Rational(2)), c + Rational(2)) assert_equal(Complex(Rational(5,3),Rational(2)), c + Rational(2,3)) end @@ -344,7 +372,7 @@ class Complex_Test < Test::Unit::TestCase assert_equal(Complex(-1,2), c - 2) assert_equal(Complex(-1.0,2), c - 2.0) - if defined?(Rational) + if @rational assert_equal(Complex(Rational(-1,1),Rational(2)), c - Rational(2)) assert_equal(Complex(Rational(1,3),Rational(2)), c - Rational(2,3)) end @@ -359,7 +387,7 @@ class Complex_Test < Test::Unit::TestCase assert_equal(Complex(2,4), c * 2) assert_equal(Complex(2.0,4.0), c * 2.0) - if defined?(Rational) + if @rational assert_equal(Complex(Rational(2,1),Rational(4)), c * Rational(2)) assert_equal(Complex(Rational(2,3),Rational(4,3)), c * Rational(2,3)) end @@ -370,12 +398,12 @@ class Complex_Test < Test::Unit::TestCase c = Complex(1,2) c2 = Complex(2,3) - if defined?(Rational) + if @rational assert_equal(Complex(Rational(8,13),Rational(1,13)), c / c2) else r = c / c2 assert_in_delta(0.615, r.real, 0.001) - assert_in_delta(0.076, r.image, 0.001) + assert_in_delta(0.076, r.imag, 0.001) end c = Complex(1.0,2.0) @@ -383,19 +411,19 @@ class Complex_Test < Test::Unit::TestCase r = c / c2 assert_in_delta(0.615, r.real, 0.001) - assert_in_delta(0.076, r.image, 0.001) + assert_in_delta(0.076, r.imag, 0.001) c = Complex(1,2) c2 = Complex(2,3) - if defined?(Rational) + if @rational assert_equal(Complex(Rational(1,2),1), c / 2) else assert_equal(Complex(0.5,1.0), c / 2) end assert_equal(Complex(0.5,1.0), c / 2.0) - if defined?(Rational) + if @rational assert_equal(Complex(Rational(1,2),Rational(1)), c / Rational(2)) assert_equal(Complex(Rational(3,2),Rational(3)), c / Rational(2,3)) end @@ -405,12 +433,12 @@ class Complex_Test < Test::Unit::TestCase c = Complex(1,2) c2 = Complex(2,3) - if defined?(Rational) + if @rational assert_equal(Complex(Rational(8,13),Rational(1,13)), c.quo(c2)) else r = c.quo(c2) assert_in_delta(0.615, r.real, 0.001) - assert_in_delta(0.076, r.image, 0.001) + assert_in_delta(0.076, r.imag, 0.001) end c = Complex(1.0,2.0) @@ -418,19 +446,19 @@ class Complex_Test < Test::Unit::TestCase r = c.quo(c2) assert_in_delta(0.615, r.real, 0.001) - assert_in_delta(0.076, r.image, 0.001) + assert_in_delta(0.076, r.imag, 0.001) c = Complex(1,2) c2 = Complex(2,3) - if defined?(Rational) + if @rational assert_equal(Complex(Rational(1,2),1), c.quo(2)) else assert_equal(Complex(0.5,1.0), c.quo(2)) end assert_equal(Complex(0.5,1.0), c.quo(2.0)) - if defined?(Rational) + if @rational assert_equal(Complex(Rational(1,2),Rational(1)), c / Rational(2)) assert_equal(Complex(Rational(3,2),Rational(3)), c / Rational(2,3)) end @@ -442,14 +470,14 @@ class Complex_Test < Test::Unit::TestCase r = c.fdiv(c2) assert_in_delta(0.615, r.real, 0.001) - assert_in_delta(0.076, r.image, 0.001) + assert_in_delta(0.076, r.imag, 0.001) c = Complex(1.0,2.0) c2 = Complex(2.0,3.0) r = c.fdiv(c2) assert_in_delta(0.615, r.real, 0.001) - assert_in_delta(0.076, r.image, 0.001) + assert_in_delta(0.076, r.imag, 0.001) c = Complex(1,2) c2 = Complex(2,3) @@ -464,25 +492,25 @@ class Complex_Test < Test::Unit::TestCase r = c ** c2 assert_in_delta(-0.015, r.real, 0.001) - assert_in_delta(-0.179, r.image, 0.001) + assert_in_delta(-0.179, r.imag, 0.001) assert_equal(Complex(-3,4), c ** 2) - if defined?(Rational) && !Rational.instance_variable_get('@RCS_ID') + if @rational && !@keiju assert_equal(Complex(Rational(-3,25),Rational(-4,25)), c ** -2) else r = c ** -2 assert_in_delta(-0.12, r.real, 0.001) - assert_in_delta(-0.16, r.image, 0.001) + assert_in_delta(-0.16, r.imag, 0.001) end r = c ** 2.0 assert_in_delta(-3.0, r.real, 0.001) - assert_in_delta(4.0, r.image, 0.001) + assert_in_delta(4.0, r.imag, 0.001) r = c ** -2.0 assert_in_delta(-0.12, r.real, 0.001) - assert_in_delta(-0.16, r.image, 0.001) + assert_in_delta(-0.16, r.imag, 0.001) - if defined?(Rational) && !Rational.instance_variable_get('@RCS_ID') + if @rational && !@keiju assert_equal(Complex(-3,4), c ** Rational(2)) #=begin assert_equal(Complex(Rational(-3,25),Rational(-4,25)), @@ -491,11 +519,11 @@ class Complex_Test < Test::Unit::TestCase r = c ** Rational(2,3) assert_in_delta(1.264, r.real, 0.001) - assert_in_delta(1.150, r.image, 0.001) + assert_in_delta(1.150, r.imag, 0.001) r = c ** Rational(-2,3) assert_in_delta(0.432, r.real, 0.001) - assert_in_delta(-0.393, r.image, 0.001) + assert_in_delta(-0.393, r.imag, 0.001) end end @@ -525,7 +553,7 @@ class Complex_Test < Test::Unit::TestCase end def test_unify - if defined?(Complex::Unify) + if @unify assert_instance_of(Fixnum, Complex(1,2) + Complex(-1,-2)) assert_instance_of(Fixnum, Complex(1,2) - Complex(1,2)) assert_instance_of(Fixnum, Complex(1,2) * 0) @@ -544,9 +572,9 @@ class Complex_Test < Test::Unit::TestCase assert_equal(5, c.abs2) assert_equal(c.abs, Math.sqrt(c * c.conj)) - assert_equal(c.abs, Math.sqrt(c.real**2 + c.image**2)) + assert_equal(c.abs, Math.sqrt(c.real**2 + c.imag**2)) assert_equal(c.abs2, c * c.conj) - assert_equal(c.abs2, c.real**2 + c.image**2) + assert_equal(c.abs2, c.real**2 + c.imag**2) assert_in_delta(1.107, c.arg, 0.001) assert_in_delta(1.107, c.angle, 0.001) @@ -586,7 +614,7 @@ class Complex_Test < Test::Unit::TestCase assert_equal('1.0-2.0i', Complex(1.0,-2.0).to_s) assert_equal('-1.0-2.0i', Complex(-1.0,-2.0).to_s) - if defined?(Rational) && !defined?(Complex::Unify) && !Rational.instance_variable_get('@RCS_ID') + if @rational && !@unify && !@keiju assert_equal('0+2/1i', Complex(0,Rational(2)).to_s) assert_equal('0-2/1i', Complex(0,Rational(-2)).to_s) assert_equal('1+2/1i', Complex(1,Rational(2)).to_s) @@ -614,13 +642,15 @@ class Complex_Test < Test::Unit::TestCase def test_marshal c = Complex(1,2) + c.instance_eval{@ivar = 9} s = Marshal.dump(c) c2 = Marshal.load(s) assert_equal(c, c2) + assert_equal(9, c2.instance_variable_get(:@ivar)) assert_instance_of(Complex, c2) - if defined?(Rational) + if @rational c = Complex(Rational(1,2),Rational(2,3)) s = Marshal.dump(c) @@ -755,7 +785,7 @@ class Complex_Test < Test::Unit::TestCase assert_raise(ArgumentError){ Complex('5+3i_')} assert_raise(ArgumentError){ Complex('5+3ix')} - if defined?(Rational) && defined?(''.to_r) + if @rational && defined?(''.to_r) assert_equal(Complex(Rational(1,5)), '1/5'.to_c) assert_equal(Complex(Rational(-1,5)), '-1/5'.to_c) assert_equal(Complex(Rational(1,5),3), '1/5+3i'.to_c) @@ -821,51 +851,47 @@ class Complex_Test < Test::Unit::TestCase end def test_to_r - if defined?(Rational) && !Rational.instance_variable_get('@RCS_ID') + if @rational && !@keiju assert_equal(Rational(3), Complex(3).to_r) assert_equal(Rational(3), Rational(Complex(3))) assert_raise(RangeError){Complex(3,2).to_r} - assert_raise(RangeError){Rational(Complex(3,2))} +# assert_raise(RangeError){Rational(Complex(3,2))} end end def test_to_c c = nil.to_c - assert_equal([0,0] , [c.real, c.image]) + assert_equal([0,0] , [c.real, c.imag]) c = 0.to_c - assert_equal([0,0] , [c.real, c.image]) + assert_equal([0,0] , [c.real, c.imag]) c = 1.to_c - assert_equal([1,0] , [c.real, c.image]) + assert_equal([1,0] , [c.real, c.imag]) c = 1.1.to_c - assert_equal([1.1, 0], [c.real, c.image]) + assert_equal([1.1, 0], [c.real, c.imag]) - if defined?(Rational) + if @rational c = Rational(1,2).to_c - assert_equal([Rational(1,2), 0], [c.real, c.image]) + assert_equal([Rational(1,2), 0], [c.real, c.imag]) end c = Complex(1,2).to_c - assert_equal([1, 2], [c.real, c.image]) - end - - def test_prec - assert_equal(nil, Complex < Precision) + assert_equal([1, 2], [c.real, c.imag]) end def test_supp - assert_equal(true, 1.scalar?) - assert_equal(true, 1.1.scalar?) + assert_equal(true, 1.real?) + assert_equal(true, 1.1.real?) assert_equal(1, 1.real) - assert_equal(0, 1.image) assert_equal(0, 1.imag) + assert_equal(0, 1.imaginary) assert_equal(1.1, 1.1.real) - assert_equal(0, 1.1.image) assert_equal(0, 1.1.imag) + assert_equal(0, 1.1.imaginary) assert_equal(1, 1.magnitude) assert_equal(1, -1.magnitude) @@ -919,14 +945,14 @@ class Complex_Test < Test::Unit::TestCase assert_equal(1.1, 1.1.conj) assert_equal(-1.1, -1.1.conj) - if defined?(Rational) + if @rational assert_equal(Complex(Rational(1,2),Rational(1)), Complex(1,2).quo(2)) else assert_equal(Complex(0.5,1.0), Complex(1,2).quo(2)) end =begin - if defined?(Rational) && !Rational.instance_variable_get('@RCS_ID') + if @rational && !@keiju assert_equal(Complex(Rational(1,2),Rational(1)), Complex(1,2).quo(2)) end =end @@ -934,7 +960,7 @@ class Complex_Test < Test::Unit::TestCase assert_equal(0.5, 1.fdiv(2)) assert_equal(5000000000.0, 10000000000.fdiv(2)) assert_equal(0.5, 1.0.fdiv(2)) - if defined?(Rational) + if @rational assert_equal(0.25, Rational(1,2).fdiv(2)) end assert_equal(Complex(0.5,1.0), Complex(1,2).quo(2)) @@ -944,7 +970,7 @@ class Complex_Test < Test::Unit::TestCase # assert_equal(true, Math.sqrt(-4.0).inexact?) assert_equal(Complex(0,2), Math.sqrt(-4)) # assert_equal(true, Math.sqrt(-4).exact?) - if defined?(Rational) + if @rational assert_equal(Complex(0,2), Math.sqrt(Rational(-4))) # assert_equal(true, Math.sqrt(Rational(-4)).exact?) end @@ -953,98 +979,98 @@ class Complex_Test < Test::Unit::TestCase # assert_equal(true, Math.sqrt(-9.0).inexact?) assert_equal(Complex(0,3), Math.sqrt(-9)) # assert_equal(true, Math.sqrt(-9).exact?) - if defined?(Rational) + if @rational assert_equal(Complex(0,3), Math.sqrt(Rational(-9))) # assert_equal(true, Math.sqrt(Rational(-9)).exact?) end c = Math.sqrt(Complex(1, 2)) assert_in_delta(1.272, c.real, 0.001) - assert_in_delta(0.786, c.image, 0.001) + assert_in_delta(0.786, c.imag, 0.001) c = Math.sqrt(-9) assert_in_delta(0.0, c.real, 0.001) - assert_in_delta(3.0, c.image, 0.001) + assert_in_delta(3.0, c.imag, 0.001) c = Math.exp(Complex(1, 2)) assert_in_delta(-1.131, c.real, 0.001) - assert_in_delta(2.471, c.image, 0.001) + assert_in_delta(2.471, c.imag, 0.001) c = Math.sin(Complex(1, 2)) assert_in_delta(3.165, c.real, 0.001) - assert_in_delta(1.959, c.image, 0.001) + assert_in_delta(1.959, c.imag, 0.001) c = Math.cos(Complex(1, 2)) assert_in_delta(2.032, c.real, 0.001) - assert_in_delta(-3.051, c.image, 0.001) + assert_in_delta(-3.051, c.imag, 0.001) c = Math.tan(Complex(1, 2)) assert_in_delta(0.033, c.real, 0.001) - assert_in_delta(1.014, c.image, 0.001) + assert_in_delta(1.014, c.imag, 0.001) c = Math.sinh(Complex(1, 2)) assert_in_delta(-0.489, c.real, 0.001) - assert_in_delta(1.403, c.image, 0.001) + assert_in_delta(1.403, c.imag, 0.001) c = Math.cosh(Complex(1, 2)) assert_in_delta(-0.642, c.real, 0.001) - assert_in_delta(1.068, c.image, 0.001) + assert_in_delta(1.068, c.imag, 0.001) c = Math.tanh(Complex(1, 2)) assert_in_delta(1.166, c.real, 0.001) - assert_in_delta(-0.243, c.image, 0.001) + assert_in_delta(-0.243, c.imag, 0.001) c = Math.log(Complex(1, 2)) assert_in_delta(0.804, c.real, 0.001) - assert_in_delta(1.107, c.image, 0.001) + assert_in_delta(1.107, c.imag, 0.001) c = Math.log(Complex(1, 2), Math::E) assert_in_delta(0.804, c.real, 0.001) - assert_in_delta(1.107, c.image, 0.001) + assert_in_delta(1.107, c.imag, 0.001) c = Math.log(-1) assert_in_delta(0.0, c.real, 0.001) - assert_in_delta(Math::PI, c.image, 0.001) + assert_in_delta(Math::PI, c.imag, 0.001) c = Math.log(8, 2) assert_in_delta(3.0, c.real, 0.001) - assert_in_delta(0.0, c.image, 0.001) + assert_in_delta(0.0, c.imag, 0.001) c = Math.log(-8, -2) assert_in_delta(1.092, c.real, 0.001) - assert_in_delta(-0.420, c.image, 0.001) + assert_in_delta(-0.420, c.imag, 0.001) c = Math.log10(Complex(1, 2)) assert_in_delta(0.349, c.real, 0.001) - assert_in_delta(0.480, c.image, 0.001) + assert_in_delta(0.480, c.imag, 0.001) c = Math.asin(Complex(1, 2)) assert_in_delta(0.427, c.real, 0.001) - assert_in_delta(1.528, c.image, 0.001) + assert_in_delta(1.528, c.imag, 0.001) c = Math.acos(Complex(1, 2)) assert_in_delta(1.143, c.real, 0.001) - assert_in_delta(-1.528, c.image, 0.001) + assert_in_delta(-1.528, c.imag, 0.001) c = Math.atan(Complex(1, 2)) assert_in_delta(1.338, c.real, 0.001) - assert_in_delta(0.402, c.image, 0.001) + assert_in_delta(0.402, c.imag, 0.001) c = Math.atan2(Complex(1, 2), 1) assert_in_delta(1.338, c.real, 0.001) - assert_in_delta(0.402, c.image, 0.001) + assert_in_delta(0.402, c.imag, 0.001) c = Math.asinh(Complex(1, 2)) assert_in_delta(1.469, c.real, 0.001) - assert_in_delta(1.063, c.image, 0.001) + assert_in_delta(1.063, c.imag, 0.001) c = Math.acosh(Complex(1, 2)) assert_in_delta(1.528, c.real, 0.001) - assert_in_delta(1.143, c.image, 0.001) + assert_in_delta(1.143, c.imag, 0.001) c = Math.atanh(Complex(1, 2)) assert_in_delta(0.173, c.real, 0.001) - assert_in_delta(1.178, c.image, 0.001) + assert_in_delta(1.178, c.imag, 0.001) end end @@ -1056,7 +1082,7 @@ class Complex_Test < Test::Unit::TestCase end def test_fixed_bug - if defined?(Rational) && !Rational.instance_variable_get('@RCS_ID') + if @rational && !@keiju assert_equal(Complex(1), 1 ** Complex(1)) end assert_equal('-1.0-0.0i', Complex(-1.0, -0.0).to_s) diff --git a/test/ruby/test_fixnum.rb b/test/ruby/test_fixnum.rb index 8fa751ba98..e55e324bd1 100644 --- a/test/ruby/test_fixnum.rb +++ b/test/ruby/test_fixnum.rb @@ -119,15 +119,6 @@ class TestFixnum < Test::Unit::TestCase assert_equal(0x4000000000000000, (-0x4000000000000000).abs) end - def test_induced_from - assert_equal(1, Fixnum.induced_from(1)) - assert_raise(RangeError) do - Fixnum.induced_from(2**31-1) - Fixnum.induced_from(2**63-1) - end - assert_equal(1, Fixnum.induced_from((2**32).coerce(1).first)) - end - def test_to_s assert_equal("1010", 10.to_s(2)) assert_equal("a", 10.to_s(36)) diff --git a/test/ruby/test_float.rb b/test/ruby/test_float.rb index d4bbd688d2..5d7bbb178b 100644 --- a/test/ruby/test_float.rb +++ b/test/ruby/test_float.rb @@ -275,13 +275,6 @@ class TestFloat < Test::Unit::TestCase assert_equal(11100.0, 11111.1.round(-2)) end - def test_induced_from - assert_equal(1.0, Float.induced_from(1)) - assert_equal(1.0, Float.induced_from(1.0)) - assert_raise(TypeError) { Float.induced_from(nil) } - end - - VS = [ 18446744073709551617.0, 18446744073709551616.0, diff --git a/test/ruby/test_integer.rb b/test/ruby/test_integer.rb index e31fb1880d..232a7fae30 100644 --- a/test/ruby/test_integer.rb +++ b/test/ruby/test_integer.rb @@ -122,12 +122,6 @@ class TestInteger < Test::Unit::TestCase assert_raise(RangeError) { 0x100.chr } end - def test_induced_from - assert_equal(1, Integer.induced_from(1)) - assert_equal(1, Integer.induced_from(1.0)) - assert_raise(TypeError) { Integer.induced_from(nil) } - end - def test_upto a = [] 1.upto(3) {|x| a << x } diff --git a/test/ruby/test_io.rb b/test/ruby/test_io.rb index 35f575aa13..4d9bb51fe3 100644 --- a/test/ruby/test_io.rb +++ b/test/ruby/test_io.rb @@ -578,18 +578,6 @@ class TestIO < Test::Unit::TestCase (wt.kill; wt.join) if wt end - def pipe2(&b) - a = [] - a << IO.pipe while true - rescue Errno::EMFILE, Errno::ENFILE, Errno::ENOMEM - yield(*a.last) - ensure - a.each do |r, w| - r.close unless !r || r.closed? - w.close unless !w || w.closed? - end - end - def ruby(*args) args = ['-e', '$>.write($<.read)'] if args.empty? ruby = EnvUtil.rubybin @@ -636,14 +624,16 @@ class TestIO < Test::Unit::TestCase assert_equal("", f2.read) end - proc do - open(__FILE__) # see Bug #493 [ruby-dev:35957] - end.call - - pipe2 do |r, w| - assert_raise(Errno::EMFILE, Errno::ENFILE, Errno::ENOMEM) do - r2, w2 = r.dup, w.dup - end + a = [] + assert_raise(Errno::EMFILE, Errno::ENFILE, Errno::ENOMEM) do + loop {a << IO.pipe} + end + assert_raise(Errno::EMFILE, Errno::ENFILE, Errno::ENOMEM) do + loop {a << [a[-1][0].dup, a[-1][1].dup]} + end + a.each do |r, w| + r.close unless !r || r.closed? + w.close unless !w || w.closed? end end diff --git a/test/ruby/test_io_m17n.rb b/test/ruby/test_io_m17n.rb index 237698b868..3f21ee088e 100644 --- a/test/ruby/test_io_m17n.rb +++ b/test/ruby/test_io_m17n.rb @@ -374,7 +374,7 @@ EOT with_pipe("euc-jp:utf-8") {|r, w| w << "\xa1xyz" w.close - err = assert_raise(Encoding::InvalidByteSequence) { r.getc } + err = assert_raise(Encoding::InvalidByteSequenceError) { r.getc } assert_equal("\xA1".force_encoding("ascii-8bit"), err.error_bytes) assert_equal("xyz", r.read(10)) } @@ -652,7 +652,7 @@ EOT after = "\u{3046}\u{3048}" w << before + invalid + after w.close - err = assert_raise(Encoding::InvalidByteSequence) { r.gets } + err = assert_raise(Encoding::InvalidByteSequenceError) { r.gets } assert_equal(invalid.force_encoding("ascii-8bit"), err.error_bytes) assert_equal(after.encode("euc-jp"), r.gets) } @@ -669,7 +669,7 @@ EOT w.close assert_equal(before1.encode("euc-jp"), r.getc) assert_equal(before2.encode("euc-jp"), r.getc) - err = assert_raise(Encoding::InvalidByteSequence) { r.getc } + err = assert_raise(Encoding::InvalidByteSequenceError) { r.getc } assert_equal(invalid.force_encoding("ascii-8bit"), err.error_bytes) assert_equal(after1.encode("euc-jp"), r.getc) assert_equal(after2.encode("euc-jp"), r.getc) @@ -688,7 +688,7 @@ EOT w.close assert_equal(before1.encode("euc-jp"), r.getc) assert_equal(before2.encode("euc-jp"), r.getc) - err = assert_raise(Encoding::InvalidByteSequence) { r.getc } + err = assert_raise(Encoding::InvalidByteSequenceError) { r.getc } assert_equal(invalid.force_encoding("ascii-8bit"), err.error_bytes) assert_equal(after1.encode("euc-jp"), r.getc) assert_equal(after2.encode("euc-jp"), r.getc) @@ -711,7 +711,7 @@ EOT after = "\u{3046}\u{3048}" w << before + invalid + after w.close - err = assert_raise(Encoding::InvalidByteSequence) { r.read } + err = assert_raise(Encoding::InvalidByteSequenceError) { r.read } assert_equal(invalid.force_encoding("ascii-8bit"), err.error_bytes) assert_equal(after.encode("euc-jp"), r.read) } @@ -809,28 +809,28 @@ EOT def test_set_encoding_binmode assert_raise(ArgumentError) { - open("/dev/null", "rt") {|f| + open(__FILE__, "rt") {|f| f.set_encoding("iso-2022-jp") } } assert_raise(ArgumentError) { - open("/dev/null", "r") {|f| + open(__FILE__, "r") {|f| f.set_encoding("iso-2022-jp") } } assert_nothing_raised { - open("/dev/null", "rb") {|f| + open(__FILE__, "rb") {|f| f.set_encoding("iso-2022-jp") } } assert_nothing_raised { - open("/dev/null", "r") {|f| + open(__FILE__, "r") {|f| f.binmode f.set_encoding("iso-2022-jp") } } assert_nothing_raised { - open("/dev/null", "rt") {|f| + open(__FILE__, "rt") {|f| f.binmode f.set_encoding("iso-2022-jp") } @@ -1549,11 +1549,11 @@ EOT assert_equal("ab", f.read) } open("t.txt", "r:utf-8:euc-jp", :undef => :replace) {|f| - assert_raise(Encoding::InvalidByteSequence) { f.read } + assert_raise(Encoding::InvalidByteSequenceError) { f.read } assert_equal("b", f.read) } open("t.txt", "r:utf-8:euc-jp", :undef => :replace, :replace => "") {|f| - assert_raise(Encoding::InvalidByteSequence) { f.read } + assert_raise(Encoding::InvalidByteSequenceError) { f.read } assert_equal("b", f.read) } } @@ -1569,11 +1569,11 @@ EOT assert_equal("ab", f.read) } open("t.txt", "r:utf-8:euc-jp", :invalid => :replace) {|f| - assert_raise(Encoding::ConversionUndefined) { f.read } + assert_raise(Encoding::ConversionUndefinedError) { f.read } assert_equal("b", f.read) } open("t.txt", "r:utf-8:euc-jp", :invalid => :replace, :replace => "") {|f| - assert_raise(Encoding::ConversionUndefined) { f.read } + assert_raise(Encoding::ConversionUndefinedError) { f.read } assert_equal("b", f.read) } } @@ -1593,10 +1593,10 @@ EOT assert_equal("ab", File.read("t.txt")) open("t.txt", "w:euc-jp", :undef => :replace) {|f| - assert_raise(Encoding::InvalidByteSequence) { f.write invalid_utf8 } + assert_raise(Encoding::InvalidByteSequenceError) { f.write invalid_utf8 } } open("t.txt", "w:euc-jp", :undef => :replace, :replace => "") {|f| - assert_raise(Encoding::InvalidByteSequence) { f.write invalid_utf8 } + assert_raise(Encoding::InvalidByteSequenceError) { f.write invalid_utf8 } } } end @@ -1613,10 +1613,10 @@ EOT } assert_equal("ab", File.read("t.txt")) open("t.txt", "w:euc-jp:utf-8", :invalid => :replace) {|f| - assert_raise(Encoding::ConversionUndefined) { f.write "a\uFFFDb" } + assert_raise(Encoding::ConversionUndefinedError) { f.write "a\uFFFDb" } } open("t.txt", "w:euc-jp:utf-8", :invalid => :replace, :replace => "") {|f| - assert_raise(Encoding::ConversionUndefined) { f.write "a\uFFFDb" } + assert_raise(Encoding::ConversionUndefinedError) { f.write "a\uFFFDb" } } } end @@ -1633,10 +1633,10 @@ EOT } assert_equal("ab", File.read("t.txt")) open("t.txt", "w:iso-2022-jp:utf-8", :invalid => :replace) {|f| - assert_raise(Encoding::ConversionUndefined) { f.write "a\uFFFDb" } + assert_raise(Encoding::ConversionUndefinedError) { f.write "a\uFFFDb" } } open("t.txt", "w:iso-2022-jp:utf-8", :invalid => :replace, :replace => "") {|f| - assert_raise(Encoding::ConversionUndefined) { f.write "a\uFFFDb" } + assert_raise(Encoding::ConversionUndefinedError) { f.write "a\uFFFDb" } } } end diff --git a/test/ruby/test_m17n.rb b/test/ruby/test_m17n.rb index a878227534..a1772f6408 100644 --- a/test/ruby/test_m17n.rb +++ b/test/ruby/test_m17n.rb @@ -146,17 +146,17 @@ class TestM17N < Test::Unit::TestCase # tests start def test_string_ascii_literal - assert_encoding("US-ASCII", eval(a(%{""})).encoding) - assert_encoding("US-ASCII", eval(a(%{"a"})).encoding) + assert_encoding("ASCII-8BIT", eval(a(%{""})).encoding) + assert_encoding("ASCII-8BIT", eval(a(%{"a"})).encoding) end def test_string_eucjp_literal - assert_encoding("US-ASCII", eval(e(%{""})).encoding) - assert_encoding("US-ASCII", eval(e(%{"a"})).encoding) + assert_encoding("EUC-JP", eval(e(%{""})).encoding) + assert_encoding("EUC-JP", eval(e(%{"a"})).encoding) assert_encoding("EUC-JP", eval(e(%{"\xa1\xa1"})).encoding) assert_encoding("EUC-JP", eval(e(%{"\\xa1\\xa1"})).encoding) - assert_encoding("US-ASCII", eval(e(%{"\\x20"})).encoding) - assert_encoding("US-ASCII", eval(e(%{"\\n"})).encoding) + assert_encoding("EUC-JP", eval(e(%{"\\x20"})).encoding) + assert_encoding("EUC-JP", eval(e(%{"\\n"})).encoding) assert_encoding("EUC-JP", eval(e(%{"\\x80"})).encoding) end @@ -746,7 +746,7 @@ class TestM17N < Test::Unit::TestCase #assert_raise(ArgumentError) { s("%c") % 0xc2a1 } assert_strenc("\u{c2a1}", 'UTF-8', u("%c") % 0xc2a1) assert_strenc("\u{c2}", 'UTF-8', u("%c") % 0xc2) - assert_raise(EncodingCompatibilityError) { + assert_raise(Encoding::CompatibilityError) { "%s%s" % [s("\xc2\xa1"), e("\xc2\xa1")] } end @@ -866,22 +866,22 @@ class TestM17N < Test::Unit::TestCase def test_str_aref_substr assert_equal(a("\xa1\xc2"), a("\xc2\xa1\xc2\xa2\xc2\xa3")[a("\xa1\xc2")]) - assert_raise(EncodingCompatibilityError) { a("\xc2\xa1\xc2\xa2\xc2\xa3")[e("\xa1\xc2")] } + assert_raise(Encoding::CompatibilityError) { a("\xc2\xa1\xc2\xa2\xc2\xa3")[e("\xa1\xc2")] } assert_equal(nil, e("\xc2\xa1\xc2\xa2\xc2\xa3")[e("\xa1\xc2")]) - assert_raise(EncodingCompatibilityError) { e("\xc2\xa1\xc2\xa2\xc2\xa3")[s("\xa1\xc2")] } + assert_raise(Encoding::CompatibilityError) { e("\xc2\xa1\xc2\xa2\xc2\xa3")[s("\xa1\xc2")] } assert_equal(s("\xa1\xc2"), s("\xc2\xa1\xc2\xa2\xc2\xa3")[s("\xa1\xc2")]) - assert_raise(EncodingCompatibilityError) { s("\xc2\xa1\xc2\xa2\xc2\xa3")[u("\xa1\xc2")] } + assert_raise(Encoding::CompatibilityError) { s("\xc2\xa1\xc2\xa2\xc2\xa3")[u("\xa1\xc2")] } assert_equal(nil, u("\xc2\xa1\xc2\xa2\xc2\xa3")[u("\xa1\xc2")]) - assert_raise(EncodingCompatibilityError) { u("\xc2\xa1\xc2\xa2\xc2\xa3")[a("\xa1\xc2")] } + assert_raise(Encoding::CompatibilityError) { u("\xc2\xa1\xc2\xa2\xc2\xa3")[a("\xa1\xc2")] } assert_nil(e("\xa1\xa2\xa3\xa4")[e("\xa2\xa3")]) end def test_aset s = e("\xa3\xb0\xa3\xb1\xa3\xb2\xa3\xb3\xa3\xb4") - assert_raise(EncodingCompatibilityError){s["\xb0\xa3"] = "foo"} + assert_raise(Encoding::CompatibilityError){s["\xb0\xa3"] = "foo"} end def test_str_center @@ -917,13 +917,13 @@ class TestM17N < Test::Unit::TestCase def test_count assert_equal(0, e("\xa1\xa2").count("z")) s = e("\xa3\xb0\xa3\xb1\xa3\xb2\xa3\xb3\xa3\xb4") - assert_raise(EncodingCompatibilityError){s.count(a("\xa3\xb0"))} + assert_raise(Encoding::CompatibilityError){s.count(a("\xa3\xb0"))} end def test_delete assert_equal(1, e("\xa1\xa2").delete("z").length) s = e("\xa3\xb0\xa3\xb1\xa3\xb2\xa3\xb3\xa3\xb4") - assert_raise(EncodingCompatibilityError){s.delete(a("\xa3\xb2"))} + assert_raise(Encoding::CompatibilityError){s.delete(a("\xa3\xb2"))} a = "\u3042\u3044\u3046\u3042\u3044\u3046" a.delete!("\u3042\u3044", "^\u3044") @@ -942,7 +942,7 @@ class TestM17N < Test::Unit::TestCase assert_nil(e("\xa1\xa2\xa3\xa4").index(e("\xa3"))) assert_nil(e("\xa1\xa2\xa3\xa4").rindex(e("\xa3"))) s = e("\xa3\xb0\xa3\xb1\xa3\xb2\xa3\xb3\xa3\xb4") - assert_raise(EncodingCompatibilityError){s.rindex(a("\xb1\xa3"))} + assert_raise(Encoding::CompatibilityError){s.rindex(a("\xb1\xa3"))} end def test_next @@ -985,7 +985,7 @@ class TestM17N < Test::Unit::TestCase def test_upto s1 = e("\xa1\xa2") s2 = s("\xa1\xa2") - assert_raise(EncodingCompatibilityError){s1.upto(s2) {|x| break }} + assert_raise(Encoding::CompatibilityError){s1.upto(s2) {|x| break }} end def test_casecmp @@ -1005,12 +1005,12 @@ class TestM17N < Test::Unit::TestCase end def test_plus - assert_raise(EncodingCompatibilityError){u("\xe3\x81\x82") + a("\xa1")} + assert_raise(Encoding::CompatibilityError){u("\xe3\x81\x82") + a("\xa1")} end def test_chomp s = e("\xa3\xb0\xa3\xb1\xa3\xb2\xa3\xb3\xa3\xb4") - assert_raise(EncodingCompatibilityError){s.chomp(s("\xa3\xb4"))} + assert_raise(Encoding::CompatibilityError){s.chomp(s("\xa3\xb4"))} end def test_gsub @@ -1023,7 +1023,7 @@ class TestM17N < Test::Unit::TestCase t = s.gsub(/b/, "\xa1\xa1".force_encoding("euc-jp")) assert_equal(Encoding::ASCII_8BIT, s.encoding) - assert_raise(EncodingCompatibilityError) { + assert_raise(Encoding::CompatibilityError) { "abc".gsub(/[ac]/) { $& == "a" ? "\xc2\xa1".force_encoding("euc-jp") : "\xc2\xa1".force_encoding("utf-8") @@ -1044,7 +1044,7 @@ class TestM17N < Test::Unit::TestCase def test_each_line s = e("\xa3\xb0\xa3\xb1\xa3\xb2\xa3\xb3\xa3\xb4") - assert_raise(EncodingCompatibilityError){s.each_line(a("\xa3\xb1")) {|l| }} + assert_raise(Encoding::CompatibilityError){s.each_line(a("\xa3\xb1")) {|l| }} s = e("\xa4\xa2\nfoo") actual = [] @@ -1280,11 +1280,20 @@ class TestM17N < Test::Unit::TestCase end def test_compatible - assert_raise(TypeError) {Encoding.compatible?("",0)} + assert_nil Encoding.compatible?("",0) + assert_equal(Encoding::UTF_8, Encoding.compatible?(Encoding::UTF_8, Encoding::UTF_8)) + assert_equal(Encoding::UTF_8, Encoding.compatible?(Encoding::UTF_8, Encoding::US_ASCII)) + assert_equal(Encoding::ASCII_8BIT, + Encoding.compatible?(Encoding::ASCII_8BIT, Encoding::US_ASCII)) + assert_nil Encoding.compatible?(Encoding::UTF_8, Encoding::ASCII_8BIT) end def test_force_encoding assert(("".center(1, "\x80".force_encoding("utf-8")); true), "moved from btest/knownbug, [ruby-dev:33807]") end + + def test_combchar_codepoint + assert_equal([0x30BB, 0x309A], "\u30BB\u309A".codepoints.to_a) + end end diff --git a/test/ruby/test_m17n_comb.rb b/test/ruby/test_m17n_comb.rb index 37b1a687a2..50fe8c2233 100644 --- a/test/ruby/test_m17n_comb.rb +++ b/test/ruby/test_m17n_comb.rb @@ -261,7 +261,7 @@ class TestM17NComb < Test::Unit::TestCase def test_str_plus combination(STRINGS, STRINGS) {|s1, s2| if s1.encoding != s2.encoding && !s1.ascii_only? && !s2.ascii_only? - assert_raise(EncodingCompatibilityError) { s1 + s2 } + assert_raise(Encoding::CompatibilityError) { s1 + s2 } else t = enccall(s1, :+, s2) assert(t.valid_encoding?) if s1.valid_encoding? && s2.valid_encoding? @@ -318,10 +318,11 @@ class TestM17NComb < Test::Unit::TestCase def test_str_eq combination(STRINGS, STRINGS) {|s1, s2| desc_eq = "#{encdump s1} == #{encdump s2}" - if s1.ascii_only? && s2.ascii_only? && a(s1) == a(s2) - assert(s1 == s2, desc_eq) - assert(s1.eql?(s2), desc_eq) - elsif s1.encoding == s2.encoding && a(s1) == a(s2) + if a(s1) == a(s2) and + (s1.ascii_only? && s2.ascii_only? or + s1.encoding == s2.encoding or + s1.encoding == (enc = Encoding.find("ASCII-8BIT")) or + s2.encoding == enc) then assert(s1 == s2, desc_eq) assert(!(s1 != s2)) assert_equal(0, s1 <=> s2) @@ -344,7 +345,7 @@ class TestM17NComb < Test::Unit::TestCase assert_equal(a(s), a(s1) + a(s2)) assert_str_enc_propagation(s, s1, s2) else - assert_raise(EncodingCompatibilityError) { s << s2 } + assert_raise(Encoding::CompatibilityError) { s << s2 } end } end @@ -396,7 +397,7 @@ class TestM17NComb < Test::Unit::TestCase end end else - assert_raise(EncodingCompatibilityError) { s1[s2] } + assert_raise(Encoding::CompatibilityError) { s1[s2] } end } end @@ -481,7 +482,7 @@ class TestM17NComb < Test::Unit::TestCase end end else - assert_raise(EncodingCompatibilityError) { t[i] = s2 } + assert_raise(Encoding::CompatibilityError) { t[i] = s2 } end } } @@ -513,7 +514,7 @@ class TestM17NComb < Test::Unit::TestCase end end else - assert_raise(EncodingCompatibilityError) { t[i,len] = s2 } + assert_raise(Encoding::CompatibilityError) { t[i,len] = s2 } end } end @@ -526,7 +527,7 @@ class TestM17NComb < Test::Unit::TestCase !s2.ascii_only? ? s2.encoding : nil, !s3.ascii_only? ? s3.encoding : nil].uniq.compact if 1 < encs.length - assert_raise(EncodingCompatibilityError, IndexError) { t[s2] = s3 } + assert_raise(Encoding::CompatibilityError, IndexError) { t[s2] = s3 } else if encs.empty? encs = [ @@ -565,7 +566,7 @@ class TestM17NComb < Test::Unit::TestCase end end else - assert_raise(EncodingCompatibilityError, RangeError, + assert_raise(Encoding::CompatibilityError, RangeError, "t=#{encdump(s1)};t[#{first}..#{last}]=#{encdump(s2)}") { t[first..last] = s2 } @@ -592,7 +593,7 @@ class TestM17NComb < Test::Unit::TestCase end end else - assert_raise(EncodingCompatibilityError, RangeError, + assert_raise(Encoding::CompatibilityError, RangeError, "t=#{encdump(s1)};t[#{first}...#{last}]=#{encdump(s2)}") { t[first...last] = s2 } @@ -655,7 +656,7 @@ class TestM17NComb < Test::Unit::TestCase next end if !s1.ascii_only? && !s2.ascii_only? && s1.encoding != s2.encoding - assert_raise(EncodingCompatibilityError) { s1.center(width, s2) } + assert_raise(Encoding::CompatibilityError) { s1.center(width, s2) } next end t = enccall(s1, :center, width, s2) @@ -676,7 +677,7 @@ class TestM17NComb < Test::Unit::TestCase next end if !s1.ascii_only? && !s2.ascii_only? && s1.encoding != s2.encoding - assert_raise(EncodingCompatibilityError) { s1.ljust(width, s2) } + assert_raise(Encoding::CompatibilityError) { s1.ljust(width, s2) } next end t = enccall(s1, :ljust, width, s2) @@ -697,7 +698,7 @@ class TestM17NComb < Test::Unit::TestCase next end if !s1.ascii_only? && !s2.ascii_only? && s1.encoding != s2.encoding - assert_raise(EncodingCompatibilityError) { s1.rjust(width, s2) } + assert_raise(Encoding::CompatibilityError) { s1.rjust(width, s2) } next end t = enccall(s1, :rjust, width, s2) @@ -711,7 +712,7 @@ class TestM17NComb < Test::Unit::TestCase combination(STRINGS, STRINGS) {|s1, s2| if !s1.ascii_only? && !s2.ascii_only? && !Encoding.compatible?(s1,s2) if s1.bytesize > s2.bytesize - assert_raise(EncodingCompatibilityError) { s1.chomp(s2) } + assert_raise(Encoding::CompatibilityError) { s1.chomp(s2) } end next end @@ -777,11 +778,11 @@ class TestM17NComb < Test::Unit::TestCase def test_str_count combination(STRINGS, STRINGS) {|s1, s2| if !s1.valid_encoding? || !s2.valid_encoding? - assert_raise(ArgumentError, EncodingCompatibilityError) { s1.count(s2) } + assert_raise(ArgumentError, Encoding::CompatibilityError) { s1.count(s2) } next end if !s1.ascii_only? && !s2.ascii_only? && s1.encoding != s2.encoding - assert_raise(EncodingCompatibilityError) { s1.count(s2) } + assert_raise(Encoding::CompatibilityError) { s1.count(s2) } next end n = enccall(s1, :count, s2) @@ -809,11 +810,11 @@ class TestM17NComb < Test::Unit::TestCase next end if !s1.valid_encoding? || !s2.valid_encoding? - assert_raise(ArgumentError, EncodingCompatibilityError) { s1.delete(s2) } + assert_raise(ArgumentError, Encoding::CompatibilityError) { s1.delete(s2) } next end if !s1.ascii_only? && !s2.ascii_only? && s1.encoding != s2.encoding - assert_raise(EncodingCompatibilityError) { s1.delete(s2) } + assert_raise(Encoding::CompatibilityError) { s1.delete(s2) } next end t = enccall(s1, :delete, s2) @@ -855,11 +856,11 @@ class TestM17NComb < Test::Unit::TestCase def test_str_each_line combination(STRINGS, STRINGS) {|s1, s2| if !s1.valid_encoding? || !s2.valid_encoding? - assert_raise(ArgumentError, EncodingCompatibilityError) { s1.each_line(s2) {} } + assert_raise(ArgumentError, Encoding::CompatibilityError) { s1.each_line(s2) {} } next end if !s1.ascii_only? && !s2.ascii_only? && s1.encoding != s2.encoding - assert_raise(EncodingCompatibilityError) { s1.each_line(s2) {} } + assert_raise(Encoding::CompatibilityError) { s1.each_line(s2) {} } next end lines = [] @@ -908,9 +909,9 @@ class TestM17NComb < Test::Unit::TestCase def test_str_include? combination(STRINGS, STRINGS) {|s1, s2| if !s1.ascii_only? && !s2.ascii_only? && s1.encoding != s2.encoding - assert_raise(EncodingCompatibilityError) { s1.include?(s2) } - assert_raise(EncodingCompatibilityError) { s1.index(s2) } - assert_raise(EncodingCompatibilityError) { s1.rindex(s2) } + assert_raise(Encoding::CompatibilityError) { s1.include?(s2) } + assert_raise(Encoding::CompatibilityError) { s1.index(s2) } + assert_raise(Encoding::CompatibilityError) { s1.rindex(s2) } next end t = enccall(s1, :include?, s2) @@ -941,7 +942,7 @@ class TestM17NComb < Test::Unit::TestCase def test_str_index combination(STRINGS, STRINGS, -2..2) {|s1, s2, pos| if !s1.ascii_only? && !s2.ascii_only? && s1.encoding != s2.encoding - assert_raise(EncodingCompatibilityError) { s1.index(s2) } + assert_raise(Encoding::CompatibilityError) { s1.index(s2) } next end t = enccall(s1, :index, s2, pos) @@ -974,7 +975,7 @@ class TestM17NComb < Test::Unit::TestCase def test_str_rindex combination(STRINGS, STRINGS, -2..2) {|s1, s2, pos| if !s1.ascii_only? && !s2.ascii_only? && s1.encoding != s2.encoding - assert_raise(EncodingCompatibilityError) { s1.rindex(s2) } + assert_raise(Encoding::CompatibilityError) { s1.rindex(s2) } next end t = enccall(s1, :rindex, s2, pos) @@ -1023,11 +1024,11 @@ class TestM17NComb < Test::Unit::TestCase t2 = s1.dup begin t1[nth, 0] = s2 - rescue EncodingCompatibilityError, IndexError => e1 + rescue Encoding::CompatibilityError, IndexError => e1 end begin t2.insert(nth, s2) - rescue EncodingCompatibilityError, IndexError => e2 + rescue Encoding::CompatibilityError, IndexError => e2 end assert_equal(t1, t2, "t=#{encdump s1}; t.insert(#{nth},#{encdump s2}); t") assert_equal(e1.class, e2.class, "begin #{encdump s1}.insert(#{nth},#{encdump s2}); rescue ArgumentError, IndexError => e; e end") @@ -1041,7 +1042,7 @@ class TestM17NComb < Test::Unit::TestCase t1.insert(nth, s2) slen = s2.length assert_equal(t1[nth-slen+1,slen], s2, "t=#{encdump s1}; t.insert(#{nth},#{encdump s2}); t") - rescue EncodingCompatibilityError, IndexError => e + rescue Encoding::CompatibilityError, IndexError => e end } end @@ -1158,11 +1159,11 @@ class TestM17NComb < Test::Unit::TestCase def test_str_split combination(STRINGS, STRINGS) {|s1, s2| if !s2.valid_encoding? - assert_raise(RegexpError) { s1.split(s2) } + assert_raise(ArgumentError, RegexpError) { s1.split(s2) } next end if !s1.ascii_only? && !s2.ascii_only? && s1.encoding != s2.encoding - assert_raise(ArgumentError) { s1.split(s2) } + assert_raise(ArgumentError, Encoding::CompatibilityError) { s1.split(s2) } next end if !s1.valid_encoding? @@ -1186,11 +1187,11 @@ class TestM17NComb < Test::Unit::TestCase def test_str_squeeze combination(STRINGS, STRINGS) {|s1, s2| if !s1.valid_encoding? || !s2.valid_encoding? - assert_raise(ArgumentError, EncodingCompatibilityError, "#{encdump s1}.squeeze(#{encdump s2})") { s1.squeeze(s2) } + assert_raise(ArgumentError, Encoding::CompatibilityError, "#{encdump s1}.squeeze(#{encdump s2})") { s1.squeeze(s2) } next end if !s1.ascii_only? && !s2.ascii_only? && s1.encoding != s2.encoding - assert_raise(EncodingCompatibilityError) { s1.squeeze(s2) } + assert_raise(Encoding::CompatibilityError) { s1.squeeze(s2) } next end t = enccall(s1, :squeeze, s2) @@ -1280,7 +1281,7 @@ class TestM17NComb < Test::Unit::TestCase next end if !str_enc_compatible?(s1, s2, s3) - assert_raise(EncodingCompatibilityError, desc) { s1.tr(s2, s3) } + assert_raise(Encoding::CompatibilityError, desc) { s1.tr(s2, s3) } next end if !s1.valid_encoding? @@ -1309,11 +1310,11 @@ class TestM17NComb < Test::Unit::TestCase next end if !s1.valid_encoding? - assert_raise(ArgumentError, EncodingCompatibilityError, desc) { s1.tr_s(s2, s3) } + assert_raise(ArgumentError, Encoding::CompatibilityError, desc) { s1.tr_s(s2, s3) } next end if !str_enc_compatible?(s1, s2, s3) - assert_raise(EncodingCompatibilityError, desc) { s1.tr(s2, s3) } + assert_raise(Encoding::CompatibilityError, desc) { s1.tr(s2, s3) } next end if s2.empty? @@ -1423,7 +1424,7 @@ class TestM17NComb < Test::Unit::TestCase next end if !str_enc_compatible?(s1.gsub(r2, ''), s3) - assert_raise(EncodingCompatibilityError, desc) { doit.call } + assert_raise(Encoding::CompatibilityError, desc) { doit.call } next end t = nil @@ -1477,7 +1478,7 @@ class TestM17NComb < Test::Unit::TestCase next end if !str_enc_compatible?(s1.gsub(r2, ''), s3) - assert_raise(EncodingCompatibilityError, desc) { doit.call } + assert_raise(Encoding::CompatibilityError, desc) { doit.call } next end t = ret = nil @@ -1538,7 +1539,7 @@ class TestM17NComb < Test::Unit::TestCase combination(STRINGS, STRINGS) {|s1, s2| desc = "#{encdump s1}.end_with?(#{encdump s2})" if !str_enc_compatible?(s1, s2) - assert_raise(EncodingCompatibilityError, desc) { s1.end_with?(s2) } + assert_raise(Encoding::CompatibilityError, desc) { s1.end_with?(s2) } next end if s1.length < s2.length @@ -1557,7 +1558,7 @@ class TestM17NComb < Test::Unit::TestCase combination(STRINGS, STRINGS) {|s1, s2| desc = "#{encdump s1}.start_with?(#{encdump s2})" if !str_enc_compatible?(s1, s2) - assert_raise(EncodingCompatibilityError, desc) { s1.start_with?(s2) } + assert_raise(Encoding::CompatibilityError, desc) { s1.start_with?(s2) } next end s1 = s1.dup.force_encoding("ASCII-8BIT") @@ -1592,7 +1593,7 @@ class TestM17NComb < Test::Unit::TestCase combination(STRINGS, STRINGS) {|s1, s2| desc = "#{encdump s1}.partition(#{encdump s2})" if !str_enc_compatible?(s1, s2) - assert_raise(EncodingCompatibilityError, desc) { s1.partition(s2) } + assert_raise(Encoding::CompatibilityError, desc) { s1.partition(s2) } next end i = enccall(s1, :index, s2) @@ -1608,7 +1609,7 @@ class TestM17NComb < Test::Unit::TestCase combination(STRINGS, STRINGS) {|s1, s2| desc = "#{encdump s1}.rpartition(#{encdump s2})" if !str_enc_compatible?(s1, s2) - assert_raise(EncodingCompatibilityError, desc) { s1.rpartition(s2) } + assert_raise(Encoding::CompatibilityError, desc) { s1.rpartition(s2) } next end i = enccall(s1, :rindex, s2) diff --git a/test/ruby/test_math.rb b/test/ruby/test_math.rb index 55f354664c..d4dbf9dbf0 100644 --- a/test/ruby/test_math.rb +++ b/test/ruby/test_math.rb @@ -113,6 +113,7 @@ class TestMath < Test::Unit::TestCase assert_equal(1.0/0, Math.log(1.0/0)) assert_raise(Errno::EDOM, Errno::ERANGE) { Math.log(0) } assert_raise(Errno::EDOM, Errno::ERANGE) { Math.log(-1) } + assert_raise(TypeError) { Math.log(1,nil) } end def test_log2 diff --git a/test/ruby/test_mixed_unicode_escapes.rb b/test/ruby/test_mixed_unicode_escapes.rb index f274ae7090..982b57e286 100644 --- a/test/ruby/test_mixed_unicode_escapes.rb +++ b/test/ruby/test_mixed_unicode_escapes.rb @@ -1,4 +1,4 @@ -# -*- coding: sjis -*- +# -*- coding: cp932 -*- # This test is in a differnt file than TestUnicodeEscapes # So that we can have a different coding comment above @@ -18,8 +18,8 @@ class TestMixedUnicodeEscape < Test::Unit::TestCase # String interpolation turns into an expression and we get # a different kind of error, but we still can't mix these - assert_raise(EncodingCompatibilityError) { eval %q("\u{1234}#{nil}")} - assert_raise(EncodingCompatibilityError) { eval %q("#{nil}\u1234")} + assert_raise(Encoding::CompatibilityError) { eval %q("\u{1234}#{nil}")} + assert_raise(Encoding::CompatibilityError) { eval %q("#{nil}\u1234")} end end diff --git a/test/ruby/test_numeric.rb b/test/ruby/test_numeric.rb index 3db054fdae..0dd7b2e99c 100644 --- a/test/ruby/test_numeric.rb +++ b/test/ruby/test_numeric.rb @@ -72,8 +72,8 @@ class TestNumeric < Test::Unit::TestCase end end - def test_scalar_p - assert(Numeric.new.scalar?) + def test_real_p + assert(Numeric.new.real?) end def test_integer_p diff --git a/test/ruby/test_prec.rb b/test/ruby/test_prec.rb deleted file mode 100644 index d872242c11..0000000000 --- a/test/ruby/test_prec.rb +++ /dev/null @@ -1,21 +0,0 @@ -require 'test/unit' - -class TestPrecision < Test::Unit::TestCase - def test_prec_i - assert_same(1, 1.0.prec(Integer)) - assert_same(1, 1.0.prec_i) - assert_same(1, Integer.induced_from(1.0)) - end - - def test_prec_f - assert_equal(1.0, 1.prec(Float)) - assert_equal(1.0, 1.prec_f) - assert_equal(1.0, Float.induced_from(1)) - end - - def test_induced_from - m = Module.new - m.instance_eval { include(Precision) } - assert_raise(TypeError) { m.induced_from(0) } - end -end diff --git a/test/ruby/test_rational.rb b/test/ruby/test_rational.rb index 1d280e4361..7f75584e0e 100644 --- a/test/ruby/test_rational.rb +++ b/test/ruby/test_rational.rb @@ -4,6 +4,14 @@ class RationalSub < Rational; end class Rational_Test < Test::Unit::TestCase + def setup + @complex = defined?(Complex) + if @complex + @keiju = Complex.instance_variable_get('@RCS_ID') + end + @unify = defined?(Rational::Unify) + end + def test_ratsub c = RationalSub.__send__(:new, 1) cc = RationalSub.__send__(:convert, 1) @@ -11,7 +19,7 @@ class Rational_Test < Test::Unit::TestCase assert_kind_of(Numeric, c) assert_kind_of(Numeric, cc) - if defined?(RationalSub::Unify) + if @unify assert_instance_of(Fixnum, c) assert_instance_of(Fixnum, cc) else @@ -41,7 +49,7 @@ class Rational_Test < Test::Unit::TestCase assert_equal(true, c.eql?(c2)) assert_equal(false, c.eql?(c3)) - if defined?(Rational::Unify) + if @unify assert_equal(true, c.eql?(0)) else assert_equal(false, c.eql?(0)) @@ -67,7 +75,7 @@ class Rational_Test < Test::Unit::TestCase def test_freeze c = Rational(1) c.freeze - unless defined?(Rational::Unify) + unless @unify assert_equal(true, c.frozen?) end assert_instance_of(String, c.to_s) @@ -108,7 +116,7 @@ class Rational_Test < Test::Unit::TestCase =begin def test_reduce - if defined?(Rational::Unify) + if @unify assert_instance_of(Fixnum, Rational.__send__(:reduce, 2,1)) else assert_instance_of(Rational, Rational.__send__(:reduce, 2,1)) @@ -138,7 +146,7 @@ class Rational_Test < Test::Unit::TestCase =end def test_new - if defined?(Rational::Unify) + if @unify assert_instance_of(Fixnum, Rational.__send__(:new, 2,1)) else assert_instance_of(Rational, Rational.__send__(:new, 2,1)) @@ -164,7 +172,7 @@ class Rational_Test < Test::Unit::TestCase assert_raise(ArgumentError){Rational.__send__(:new, nil)} =begin assert_raise(ArgumentError){Rational.__send__(:new, Rational(1))} - if defined?(Complex) + if @complex assert_raise(ArgumentError){Rational.__send__(:new, Complex(1))} end =end @@ -199,6 +207,17 @@ class Rational_Test < Test::Unit::TestCase c = Rational(Rational(1,2),Rational(1,2)) assert_equal(Rational.__send__(:new, 1), c) + if @complex && !@keiju + c = Rational(Complex(1,2),2) + assert_equal(Complex.__send__(:new, Rational(1,2),1), c) + + c = Rational(2,Complex(1,2)) + assert_equal(Complex.__send__(:new, Rational(2,5),Rational(-4,5)), c) + + c = Rational(Complex(1,2),Complex(1,2)) + assert_equal(Rational.__send__(:new, 1), c) + end + assert_equal(Rational.__send__(:new, 3),Rational(3)) assert_equal(Rational.__send__(:new, 1),Rational(3,3)) assert_equal(3.3.to_r,Rational(3.3)) @@ -248,8 +267,7 @@ class Rational_Test < Test::Unit::TestCase def test_attr2 c = Rational(1) - if defined?(Rational::Unify) - assert_equal(true, c.scalar?) + if @unify =begin assert_equal(true, c.finite?) assert_equal(false, c.infinite?) @@ -257,13 +275,14 @@ class Rational_Test < Test::Unit::TestCase assert_equal(true, c.integer?) assert_equal(false, c.float?) assert_equal(true, c.rational?) +=end assert_equal(true, c.real?) +=begin assert_equal(false, c.complex?) assert_equal(true, c.exact?) assert_equal(false, c.inexact?) =end else - assert_equal(true, c.scalar?) =begin assert_equal(true, c.finite?) assert_equal(false, c.infinite?) @@ -271,7 +290,9 @@ class Rational_Test < Test::Unit::TestCase assert_equal(false, c.integer?) assert_equal(false, c.float?) assert_equal(true, c.rational?) +=end assert_equal(true, c.real?) +=begin assert_equal(false, c.complex?) assert_equal(true, c.exact?) assert_equal(false, c.inexact?) @@ -406,7 +427,7 @@ class Rational_Test < Test::Unit::TestCase assert_equal(-2, (-c).div(c2)) assert_equal(1, (-c).div(-c2)) - unless defined?(Rational::Unify) + unless @unify c = Rational(11) c2 = Rational(3) @@ -441,7 +462,7 @@ class Rational_Test < Test::Unit::TestCase assert_equal(Rational(99,100), (-c).modulo(c2)) assert_equal(Rational(-101,100), (-c).modulo(-c2)) - unless defined?(Rational::Unify) + unless @unify c = Rational(11) c2 = Rational(3) @@ -476,7 +497,7 @@ class Rational_Test < Test::Unit::TestCase assert_equal([-2, Rational(99,100)], (-c).divmod(c2)) assert_equal([1, Rational(-101,100)], (-c).divmod(-c2)) - unless defined?(Rational::Unify) + unless @unify c = Rational(11) c2 = Rational(3) @@ -512,7 +533,7 @@ class Rational_Test < Test::Unit::TestCase assert_equal(-1, (-c).quot(c2)) assert_equal(1, (-c).quot(-c2)) - unless defined?(Rational::Unify) + unless @unify c = Rational(11) c2 = Rational(3) @@ -548,7 +569,7 @@ class Rational_Test < Test::Unit::TestCase assert_equal(Rational(-101,100), (-c).remainder(c2)) assert_equal(Rational(-101,100), (-c).remainder(-c2)) - unless defined?(Rational::Unify) + unless @unify c = Rational(11) c2 = Rational(3) @@ -584,7 +605,7 @@ class Rational_Test < Test::Unit::TestCase assert_equal([-1, Rational(-101,100)], (-c).quotrem(c2)) assert_equal([1, Rational(-101,100)], (-c).quotrem(-c2)) - unless defined?(Rational::Unify) + unless @unify c = Rational(11) c2 = Rational(3) @@ -641,7 +662,7 @@ class Rational_Test < Test::Unit::TestCase # p ** p x = 2 ** Rational(2) assert_equal(Rational(4), x) - unless defined?(Rational::Unify) + unless @unify assert_instance_of(Rational, x) end assert_equal(4, x.numerator) @@ -649,7 +670,7 @@ class Rational_Test < Test::Unit::TestCase x = Rational(2) ** 2 assert_equal(Rational(4), x) - unless defined?(Rational::Unify) + unless @unify assert_instance_of(Rational, x) end assert_equal(4, x.numerator) @@ -657,7 +678,7 @@ class Rational_Test < Test::Unit::TestCase x = Rational(2) ** Rational(2) assert_equal(Rational(4), x) - unless defined?(Rational::Unify) + unless @unify assert_instance_of(Rational, x) end assert_equal(4, x.numerator) @@ -666,7 +687,7 @@ class Rational_Test < Test::Unit::TestCase # -p ** p x = (-2) ** Rational(2) assert_equal(Rational(4), x) - unless defined?(Rational::Unify) + unless @unify assert_instance_of(Rational, x) end assert_equal(4, x.numerator) @@ -674,7 +695,7 @@ class Rational_Test < Test::Unit::TestCase x = Rational(-2) ** 2 assert_equal(Rational(4), x) - unless defined?(Rational::Unify) + unless @unify assert_instance_of(Rational, x) end assert_equal(4, x.numerator) @@ -682,7 +703,7 @@ class Rational_Test < Test::Unit::TestCase x = Rational(-2) ** Rational(2) assert_equal(Rational(4), x) - unless defined?(Rational::Unify) + unless @unify assert_instance_of(Rational, x) end assert_equal(4, x.numerator) @@ -726,7 +747,7 @@ class Rational_Test < Test::Unit::TestCase assert_equal(1, x.numerator) assert_equal(4, x.denominator) - unless defined?(Rational::Unify) # maybe bug mathn + unless @unify # maybe bug mathn assert_raise(ZeroDivisionError){0 ** -1} end end @@ -787,7 +808,7 @@ class Rational_Test < Test::Unit::TestCase end def test_unify - if defined?(Rational::Unify) + if @unify assert_instance_of(Fixnum, Rational(1,2) + Rational(1,2)) assert_instance_of(Fixnum, Rational(1,2) - Rational(1,2)) assert_instance_of(Fixnum, Rational(1,2) * 2) @@ -801,7 +822,7 @@ class Rational_Test < Test::Unit::TestCase def test_math assert_equal(Rational(1,2), Rational(1,2).abs) assert_equal(Rational(1,2), Rational(-1,2).abs) - if defined?(Complex) && !Complex.instance_variable_get('@RCS_ID') + if @complex && !@keiju assert_equal(Rational(1,2), Rational(1,2).magnitude) assert_equal(Rational(1,2), Rational(-1,2).magnitude) end @@ -831,7 +852,7 @@ class Rational_Test < Test::Unit::TestCase assert_instance_of(String, c.to_s) assert_equal('1/2', c.to_s) - if defined?(Rational::Unify) + if @unify assert_equal('0', Rational(0,2).to_s) assert_equal('0', Rational(0,-2).to_s) else @@ -854,10 +875,12 @@ class Rational_Test < Test::Unit::TestCase def test_marshal c = Rational(1,2) + c.instance_eval{@ivar = 9} s = Marshal.dump(c) c2 = Marshal.load(s) assert_equal(c, c2) + assert_equal(9, c2.instance_variable_get(:@ivar)) assert_instance_of(Rational, c2) assert_raise(ZeroDivisionError){ @@ -987,8 +1010,8 @@ class Rational_Test < Test::Unit::TestCase end def test_to_c - if defined?(Complex) && !Complex.instance_variable_get('@RCS_ID') - if defined?(Rational::Unify) + if @complex && !@keiju + if @unify assert_equal(Rational(3,2), Rational(3,2).to_c) assert_equal(Rational(3,2), Complex(Rational(3,2))) else @@ -1015,8 +1038,8 @@ class Rational_Test < Test::Unit::TestCase c = Rational(1,2).to_r assert_equal([1,2] , [c.numerator, c.denominator]) - if defined?(Complex) - if Complex.instance_variable_get('@RCS_ID') + if @complex + if @keiju assert_raise(NoMethodError){Complex(1,2).to_r} else assert_raise(RangeError){Complex(1,2).to_r} @@ -1024,16 +1047,6 @@ class Rational_Test < Test::Unit::TestCase end end - def test_prec - assert_equal(true, Rational < Precision) - - c = Rational(3,2) - - assert_eql(1, c.prec(Integer)) - assert_eql(1.5, c.prec(Float)) - assert_eql(c, c.prec(Rational)) - end - def test_gcdlcm assert_equal(7, 91.gcd(-49)) assert_equal(5, 5.gcd(0)) @@ -1052,8 +1065,8 @@ class Rational_Test < Test::Unit::TestCase end def test_supp - assert_equal(true, 1.scalar?) - assert_equal(true, 1.1.scalar?) + assert_equal(true, 1.real?) + assert_equal(true, 1.1.real?) assert_equal(1, 1.numerator) assert_equal(9, 9.numerator) @@ -1089,7 +1102,7 @@ class Rational_Test < Test::Unit::TestCase end def test_fixed_bug - if defined?(Rational::Unify) + if @unify assert_instance_of(Fixnum, Rational(1,2) ** 0) # mathn's bug end diff --git a/test/ruby/test_transcode.rb b/test/ruby/test_transcode.rb index 5abeed88ee..389deb48b1 100644 --- a/test/ruby/test_transcode.rb +++ b/test/ruby/test_transcode.rb @@ -21,13 +21,13 @@ class TestTranscode < Test::Unit::TestCase def test_errors assert_raise(ArgumentError) { 'abc'.encode } assert_raise(ArgumentError) { 'abc'.encode! } - assert_raise(Encoding::NoConverter) { 'abc'.encode('foo', 'bar') } - assert_raise(Encoding::NoConverter) { 'abc'.encode!('foo', 'bar') } - assert_raise(Encoding::NoConverter) { 'abc'.force_encoding('utf-8').encode('foo') } - assert_raise(Encoding::NoConverter) { 'abc'.force_encoding('utf-8').encode!('foo') } - assert_raise(Encoding::ConversionUndefined) { "\x80".encode('utf-8','ASCII-8BIT') } - assert_raise(Encoding::InvalidByteSequence) { "\x80".encode('utf-8','US-ASCII') } - assert_raise(Encoding::ConversionUndefined) { "\xA5".encode('utf-8','iso-8859-3') } + assert_raise(Encoding::NoConverterError) { 'abc'.encode('foo', 'bar') } + assert_raise(Encoding::NoConverterError) { 'abc'.encode!('foo', 'bar') } + assert_raise(Encoding::NoConverterError) { 'abc'.force_encoding('utf-8').encode('foo') } + assert_raise(Encoding::NoConverterError) { 'abc'.force_encoding('utf-8').encode!('foo') } + assert_raise(Encoding::ConversionUndefinedError) { "\x80".encode('utf-8','ASCII-8BIT') } + assert_raise(Encoding::InvalidByteSequenceError) { "\x80".encode('utf-8','US-ASCII') } + assert_raise(Encoding::ConversionUndefinedError) { "\xA5".encode('utf-8','iso-8859-3') } end def test_arguments @@ -355,24 +355,24 @@ class TestTranscode < Test::Unit::TestCase check_both_ways("\u71FC", "\xE0\x9E", 'shift_jis') # 燼 check_both_ways("\u71F9", "\xE0\x9F", 'shift_jis') # 燹 check_both_ways("\u73F1", "\xE0\xFC", 'shift_jis') # 珱 - assert_raise(Encoding::ConversionUndefined) { "\xEF\x40".encode("utf-8", 'shift_jis') } - assert_raise(Encoding::ConversionUndefined) { "\xEF\x7E".encode("utf-8", 'shift_jis') } - assert_raise(Encoding::ConversionUndefined) { "\xEF\x80".encode("utf-8", 'shift_jis') } - assert_raise(Encoding::ConversionUndefined) { "\xEF\x9E".encode("utf-8", 'shift_jis') } - assert_raise(Encoding::ConversionUndefined) { "\xEF\x9F".encode("utf-8", 'shift_jis') } - assert_raise(Encoding::ConversionUndefined) { "\xEF\xFC".encode("utf-8", 'shift_jis') } - assert_raise(Encoding::ConversionUndefined) { "\xF0\x40".encode("utf-8", 'shift_jis') } - assert_raise(Encoding::ConversionUndefined) { "\xF0\x7E".encode("utf-8", 'shift_jis') } - assert_raise(Encoding::ConversionUndefined) { "\xF0\x80".encode("utf-8", 'shift_jis') } - assert_raise(Encoding::ConversionUndefined) { "\xF0\x9E".encode("utf-8", 'shift_jis') } - assert_raise(Encoding::ConversionUndefined) { "\xF0\x9F".encode("utf-8", 'shift_jis') } - assert_raise(Encoding::ConversionUndefined) { "\xF0\xFC".encode("utf-8", 'shift_jis') } + assert_raise(Encoding::ConversionUndefinedError) { "\xEF\x40".encode("utf-8", 'shift_jis') } + assert_raise(Encoding::ConversionUndefinedError) { "\xEF\x7E".encode("utf-8", 'shift_jis') } + assert_raise(Encoding::ConversionUndefinedError) { "\xEF\x80".encode("utf-8", 'shift_jis') } + assert_raise(Encoding::ConversionUndefinedError) { "\xEF\x9E".encode("utf-8", 'shift_jis') } + assert_raise(Encoding::ConversionUndefinedError) { "\xEF\x9F".encode("utf-8", 'shift_jis') } + assert_raise(Encoding::ConversionUndefinedError) { "\xEF\xFC".encode("utf-8", 'shift_jis') } + assert_raise(Encoding::ConversionUndefinedError) { "\xF0\x40".encode("utf-8", 'shift_jis') } + assert_raise(Encoding::ConversionUndefinedError) { "\xF0\x7E".encode("utf-8", 'shift_jis') } + assert_raise(Encoding::ConversionUndefinedError) { "\xF0\x80".encode("utf-8", 'shift_jis') } + assert_raise(Encoding::ConversionUndefinedError) { "\xF0\x9E".encode("utf-8", 'shift_jis') } + assert_raise(Encoding::ConversionUndefinedError) { "\xF0\x9F".encode("utf-8", 'shift_jis') } + assert_raise(Encoding::ConversionUndefinedError) { "\xF0\xFC".encode("utf-8", 'shift_jis') } #check_both_ways("\u9ADC", "\xFC\x40", 'shift_jis') # 髜 (IBM extended) - assert_raise(Encoding::ConversionUndefined) { "\xFC\x7E".encode("utf-8", 'shift_jis') } - assert_raise(Encoding::ConversionUndefined) { "\xFC\x80".encode("utf-8", 'shift_jis') } - assert_raise(Encoding::ConversionUndefined) { "\xFC\x9E".encode("utf-8", 'shift_jis') } - assert_raise(Encoding::ConversionUndefined) { "\xFC\x9F".encode("utf-8", 'shift_jis') } - assert_raise(Encoding::ConversionUndefined) { "\xFC\xFC".encode("utf-8", 'shift_jis') } + assert_raise(Encoding::ConversionUndefinedError) { "\xFC\x7E".encode("utf-8", 'shift_jis') } + assert_raise(Encoding::ConversionUndefinedError) { "\xFC\x80".encode("utf-8", 'shift_jis') } + assert_raise(Encoding::ConversionUndefinedError) { "\xFC\x9E".encode("utf-8", 'shift_jis') } + assert_raise(Encoding::ConversionUndefinedError) { "\xFC\x9F".encode("utf-8", 'shift_jis') } + assert_raise(Encoding::ConversionUndefinedError) { "\xFC\xFC".encode("utf-8", 'shift_jis') } check_both_ways("\u677E\u672C\u884C\u5F18", "\x8f\xbc\x96\x7b\x8d\x73\x8d\x4f", 'shift_jis') # 松本行弘 check_both_ways("\u9752\u5C71\u5B66\u9662\u5927\u5B66", "\x90\xC2\x8E\x52\x8A\x77\x89\x40\x91\xE5\x8A\x77", 'shift_jis') # 青山学院大学 check_both_ways("\u795E\u6797\u7FA9\u535A", "\x90\x5F\x97\xD1\x8B\x60\x94\x8E", 'shift_jis') # 神林義博 @@ -392,34 +392,34 @@ class TestTranscode < Test::Unit::TestCase check_both_ways("\u00F7", "\xA1\xE0", 'euc-jp') # ÷ check_both_ways("\u25C7", "\xA1\xFE", 'euc-jp') # ◇ check_both_ways("\u25C6", "\xA2\xA1", 'euc-jp') # ◆ - assert_raise(Encoding::ConversionUndefined) { "\xA2\xAF".encode("utf-8", 'euc-jp') } - assert_raise(Encoding::ConversionUndefined) { "\xA2\xB9".encode("utf-8", 'euc-jp') } - assert_raise(Encoding::ConversionUndefined) { "\xA2\xC2".encode("utf-8", 'euc-jp') } - assert_raise(Encoding::ConversionUndefined) { "\xA2\xC9".encode("utf-8", 'euc-jp') } - assert_raise(Encoding::ConversionUndefined) { "\xA2\xD1".encode("utf-8", 'euc-jp') } - assert_raise(Encoding::ConversionUndefined) { "\xA2\xDB".encode("utf-8", 'euc-jp') } - assert_raise(Encoding::ConversionUndefined) { "\xA2\xEB".encode("utf-8", 'euc-jp') } - assert_raise(Encoding::ConversionUndefined) { "\xA2\xF1".encode("utf-8", 'euc-jp') } - assert_raise(Encoding::ConversionUndefined) { "\xA2\xFA".encode("utf-8", 'euc-jp') } - assert_raise(Encoding::ConversionUndefined) { "\xA2\xFD".encode("utf-8", 'euc-jp') } + assert_raise(Encoding::ConversionUndefinedError) { "\xA2\xAF".encode("utf-8", 'euc-jp') } + assert_raise(Encoding::ConversionUndefinedError) { "\xA2\xB9".encode("utf-8", 'euc-jp') } + assert_raise(Encoding::ConversionUndefinedError) { "\xA2\xC2".encode("utf-8", 'euc-jp') } + assert_raise(Encoding::ConversionUndefinedError) { "\xA2\xC9".encode("utf-8", 'euc-jp') } + assert_raise(Encoding::ConversionUndefinedError) { "\xA2\xD1".encode("utf-8", 'euc-jp') } + assert_raise(Encoding::ConversionUndefinedError) { "\xA2\xDB".encode("utf-8", 'euc-jp') } + assert_raise(Encoding::ConversionUndefinedError) { "\xA2\xEB".encode("utf-8", 'euc-jp') } + assert_raise(Encoding::ConversionUndefinedError) { "\xA2\xF1".encode("utf-8", 'euc-jp') } + assert_raise(Encoding::ConversionUndefinedError) { "\xA2\xFA".encode("utf-8", 'euc-jp') } + assert_raise(Encoding::ConversionUndefinedError) { "\xA2\xFD".encode("utf-8", 'euc-jp') } check_both_ways("\u25EF", "\xA2\xFE", 'euc-jp') # ◯ - assert_raise(Encoding::ConversionUndefined) { "\xA3\xAF".encode("utf-8", 'euc-jp') } - assert_raise(Encoding::ConversionUndefined) { "\xA3\xBA".encode("utf-8", 'euc-jp') } - assert_raise(Encoding::ConversionUndefined) { "\xA3\xC0".encode("utf-8", 'euc-jp') } - assert_raise(Encoding::ConversionUndefined) { "\xA3\xDB".encode("utf-8", 'euc-jp') } - assert_raise(Encoding::ConversionUndefined) { "\xA3\xE0".encode("utf-8", 'euc-jp') } - assert_raise(Encoding::ConversionUndefined) { "\xA3\xFB".encode("utf-8", 'euc-jp') } - assert_raise(Encoding::ConversionUndefined) { "\xA4\xF4".encode("utf-8", 'euc-jp') } - assert_raise(Encoding::ConversionUndefined) { "\xA5\xF7".encode("utf-8", 'euc-jp') } - assert_raise(Encoding::ConversionUndefined) { "\xA6\xB9".encode("utf-8", 'euc-jp') } - assert_raise(Encoding::ConversionUndefined) { "\xA6\xC0".encode("utf-8", 'euc-jp') } - assert_raise(Encoding::ConversionUndefined) { "\xA6\xD9".encode("utf-8", 'euc-jp') } - assert_raise(Encoding::ConversionUndefined) { "\xA7\xC2".encode("utf-8", 'euc-jp') } - assert_raise(Encoding::ConversionUndefined) { "\xA7\xD0".encode("utf-8", 'euc-jp') } - assert_raise(Encoding::ConversionUndefined) { "\xA7\xF2".encode("utf-8", 'euc-jp') } - assert_raise(Encoding::ConversionUndefined) { "\xA8\xC1".encode("utf-8", 'euc-jp') } - assert_raise(Encoding::ConversionUndefined) { "\xCF\xD4".encode("utf-8", 'euc-jp') } - assert_raise(Encoding::ConversionUndefined) { "\xCF\xFE".encode("utf-8", 'euc-jp') } + assert_raise(Encoding::ConversionUndefinedError) { "\xA3\xAF".encode("utf-8", 'euc-jp') } + assert_raise(Encoding::ConversionUndefinedError) { "\xA3\xBA".encode("utf-8", 'euc-jp') } + assert_raise(Encoding::ConversionUndefinedError) { "\xA3\xC0".encode("utf-8", 'euc-jp') } + assert_raise(Encoding::ConversionUndefinedError) { "\xA3\xDB".encode("utf-8", 'euc-jp') } + assert_raise(Encoding::ConversionUndefinedError) { "\xA3\xE0".encode("utf-8", 'euc-jp') } + assert_raise(Encoding::ConversionUndefinedError) { "\xA3\xFB".encode("utf-8", 'euc-jp') } + assert_raise(Encoding::ConversionUndefinedError) { "\xA4\xF4".encode("utf-8", 'euc-jp') } + assert_raise(Encoding::ConversionUndefinedError) { "\xA5\xF7".encode("utf-8", 'euc-jp') } + assert_raise(Encoding::ConversionUndefinedError) { "\xA6\xB9".encode("utf-8", 'euc-jp') } + assert_raise(Encoding::ConversionUndefinedError) { "\xA6\xC0".encode("utf-8", 'euc-jp') } + assert_raise(Encoding::ConversionUndefinedError) { "\xA6\xD9".encode("utf-8", 'euc-jp') } + assert_raise(Encoding::ConversionUndefinedError) { "\xA7\xC2".encode("utf-8", 'euc-jp') } + assert_raise(Encoding::ConversionUndefinedError) { "\xA7\xD0".encode("utf-8", 'euc-jp') } + assert_raise(Encoding::ConversionUndefinedError) { "\xA7\xF2".encode("utf-8", 'euc-jp') } + assert_raise(Encoding::ConversionUndefinedError) { "\xA8\xC1".encode("utf-8", 'euc-jp') } + assert_raise(Encoding::ConversionUndefinedError) { "\xCF\xD4".encode("utf-8", 'euc-jp') } + assert_raise(Encoding::ConversionUndefinedError) { "\xCF\xFE".encode("utf-8", 'euc-jp') } check_both_ways("\u6A97", "\xDD\xA1", 'euc-jp') # 檗 check_both_ways("\u6BEF", "\xDD\xDF", 'euc-jp') # 毯 check_both_ways("\u9EBE", "\xDD\xE0", 'euc-jp') # 麾 @@ -432,7 +432,7 @@ class TestTranscode < Test::Unit::TestCase check_both_ways("\u71FC", "\xDF\xFE", 'euc-jp') # 燼 check_both_ways("\u71F9", "\xE0\xA1", 'euc-jp') # 燹 check_both_ways("\u73F1", "\xE0\xFE", 'euc-jp') # 珱 - assert_raise(Encoding::ConversionUndefined) { "\xF4\xA7".encode("utf-8", 'euc-jp') } + assert_raise(Encoding::ConversionUndefinedError) { "\xF4\xA7".encode("utf-8", 'euc-jp') } #check_both_ways("\u9ADC", "\xFC\xE3", 'euc-jp') # 髜 (IBM extended) check_both_ways("\u677E\u672C\u884C\u5F18", "\xBE\xBE\xCB\xDC\xB9\xD4\xB9\xB0", 'euc-jp') # 松本行弘 @@ -491,33 +491,33 @@ class TestTranscode < Test::Unit::TestCase end def test_eucjp_sjis_undef - assert_raise(Encoding::ConversionUndefined) { "\x8e\xe0".encode("Shift_JIS", "EUC-JP") } - assert_raise(Encoding::ConversionUndefined) { "\x8e\xfe".encode("Shift_JIS", "EUC-JP") } - assert_raise(Encoding::ConversionUndefined) { "\x8f\xa1\xa1".encode("Shift_JIS", "EUC-JP") } - assert_raise(Encoding::ConversionUndefined) { "\x8f\xa1\xfe".encode("Shift_JIS", "EUC-JP") } - assert_raise(Encoding::ConversionUndefined) { "\x8f\xfe\xa1".encode("Shift_JIS", "EUC-JP") } - assert_raise(Encoding::ConversionUndefined) { "\x8f\xfe\xfe".encode("Shift_JIS", "EUC-JP") } - - assert_raise(Encoding::ConversionUndefined) { "\xf0\x40".encode("EUC-JP", "Shift_JIS") } - assert_raise(Encoding::ConversionUndefined) { "\xf0\x7e".encode("EUC-JP", "Shift_JIS") } - assert_raise(Encoding::ConversionUndefined) { "\xf0\x80".encode("EUC-JP", "Shift_JIS") } - assert_raise(Encoding::ConversionUndefined) { "\xf0\xfc".encode("EUC-JP", "Shift_JIS") } - assert_raise(Encoding::ConversionUndefined) { "\xfc\x40".encode("EUC-JP", "Shift_JIS") } - assert_raise(Encoding::ConversionUndefined) { "\xfc\x7e".encode("EUC-JP", "Shift_JIS") } - assert_raise(Encoding::ConversionUndefined) { "\xfc\x80".encode("EUC-JP", "Shift_JIS") } - assert_raise(Encoding::ConversionUndefined) { "\xfc\xfc".encode("EUC-JP", "Shift_JIS") } + assert_raise(Encoding::ConversionUndefinedError) { "\x8e\xe0".encode("Shift_JIS", "EUC-JP") } + assert_raise(Encoding::ConversionUndefinedError) { "\x8e\xfe".encode("Shift_JIS", "EUC-JP") } + assert_raise(Encoding::ConversionUndefinedError) { "\x8f\xa1\xa1".encode("Shift_JIS", "EUC-JP") } + assert_raise(Encoding::ConversionUndefinedError) { "\x8f\xa1\xfe".encode("Shift_JIS", "EUC-JP") } + assert_raise(Encoding::ConversionUndefinedError) { "\x8f\xfe\xa1".encode("Shift_JIS", "EUC-JP") } + assert_raise(Encoding::ConversionUndefinedError) { "\x8f\xfe\xfe".encode("Shift_JIS", "EUC-JP") } + + assert_raise(Encoding::ConversionUndefinedError) { "\xf0\x40".encode("EUC-JP", "Shift_JIS") } + assert_raise(Encoding::ConversionUndefinedError) { "\xf0\x7e".encode("EUC-JP", "Shift_JIS") } + assert_raise(Encoding::ConversionUndefinedError) { "\xf0\x80".encode("EUC-JP", "Shift_JIS") } + assert_raise(Encoding::ConversionUndefinedError) { "\xf0\xfc".encode("EUC-JP", "Shift_JIS") } + assert_raise(Encoding::ConversionUndefinedError) { "\xfc\x40".encode("EUC-JP", "Shift_JIS") } + assert_raise(Encoding::ConversionUndefinedError) { "\xfc\x7e".encode("EUC-JP", "Shift_JIS") } + assert_raise(Encoding::ConversionUndefinedError) { "\xfc\x80".encode("EUC-JP", "Shift_JIS") } + assert_raise(Encoding::ConversionUndefinedError) { "\xfc\xfc".encode("EUC-JP", "Shift_JIS") } end def test_iso_2022_jp - assert_raise(Encoding::InvalidByteSequence) { "\x1b(A".encode("utf-8", "iso-2022-jp") } - assert_raise(Encoding::InvalidByteSequence) { "\x1b$(A".encode("utf-8", "iso-2022-jp") } - assert_raise(Encoding::InvalidByteSequence) { "\x1b$C".encode("utf-8", "iso-2022-jp") } - assert_raise(Encoding::InvalidByteSequence) { "\x0e".encode("utf-8", "iso-2022-jp") } - assert_raise(Encoding::InvalidByteSequence) { "\x80".encode("utf-8", "iso-2022-jp") } - assert_raise(Encoding::InvalidByteSequence) { "\x1b$(Dd!\x1b(B".encode("utf-8", "iso-2022-jp") } - assert_raise(Encoding::ConversionUndefined) { "\u9299".encode("iso-2022-jp") } - assert_raise(Encoding::ConversionUndefined) { "\uff71\uff72\uff73\uff74\uff75".encode("iso-2022-jp") } - assert_raise(Encoding::InvalidByteSequence) { "\x1b(I12345\x1b(B".encode("utf-8", "iso-2022-jp") } + assert_raise(Encoding::InvalidByteSequenceError) { "\x1b(A".encode("utf-8", "iso-2022-jp") } + assert_raise(Encoding::InvalidByteSequenceError) { "\x1b$(A".encode("utf-8", "iso-2022-jp") } + assert_raise(Encoding::InvalidByteSequenceError) { "\x1b$C".encode("utf-8", "iso-2022-jp") } + assert_raise(Encoding::InvalidByteSequenceError) { "\x0e".encode("utf-8", "iso-2022-jp") } + assert_raise(Encoding::InvalidByteSequenceError) { "\x80".encode("utf-8", "iso-2022-jp") } + assert_raise(Encoding::InvalidByteSequenceError) { "\x1b$(Dd!\x1b(B".encode("utf-8", "iso-2022-jp") } + assert_raise(Encoding::ConversionUndefinedError) { "\u9299".encode("iso-2022-jp") } + assert_raise(Encoding::ConversionUndefinedError) { "\uff71\uff72\uff73\uff74\uff75".encode("iso-2022-jp") } + assert_raise(Encoding::InvalidByteSequenceError) { "\x1b(I12345\x1b(B".encode("utf-8", "iso-2022-jp") } assert_equal("\xA1\xA1".force_encoding("euc-jp"), "\e$B!!\e(B".encode("EUC-JP", "ISO-2022-JP")) assert_equal("\e$B!!\e(B".force_encoding("ISO-2022-JP"), @@ -550,11 +550,11 @@ class TestTranscode < Test::Unit::TestCase assert_equal("\u005C", "\e(J\x5C\e(B".encode("UTF-8", "ISO-2022-JP")) assert_equal("\u005C", "\x5C".encode("stateless-ISO-2022-JP", "ISO-2022-JP")) assert_equal("\u005C", "\e(J\x5C\e(B".encode("stateless-ISO-2022-JP", "ISO-2022-JP")) - assert_raise(Encoding::ConversionUndefined) { "\u00A5".encode("Shift_JIS") } - assert_raise(Encoding::ConversionUndefined) { "\u00A5".encode("Windows-31J") } - assert_raise(Encoding::ConversionUndefined) { "\u00A5".encode("EUC-JP") } - assert_raise(Encoding::ConversionUndefined) { "\u00A5".encode("eucJP-ms") } - assert_raise(Encoding::ConversionUndefined) { "\u00A5".encode("CP51932") } + assert_raise(Encoding::ConversionUndefinedError) { "\u00A5".encode("Shift_JIS") } + assert_raise(Encoding::ConversionUndefinedError) { "\u00A5".encode("Windows-31J") } + assert_raise(Encoding::ConversionUndefinedError) { "\u00A5".encode("EUC-JP") } + assert_raise(Encoding::ConversionUndefinedError) { "\u00A5".encode("eucJP-ms") } + assert_raise(Encoding::ConversionUndefinedError) { "\u00A5".encode("CP51932") } # FULLWIDTH REVERSE SOLIDUS check_both_ways("\uFF3C", "\x81\x5F", "Shift_JIS") @@ -575,11 +575,17 @@ class TestTranscode < Test::Unit::TestCase assert_equal("\u007E", "\e(J\x7E\e(B".encode("UTF-8", "ISO-2022-JP")) assert_equal("\u007E", "\x7E".encode("stateless-ISO-2022-JP", "ISO-2022-JP")) assert_equal("\u007E", "\e(J\x7E\e(B".encode("stateless-ISO-2022-JP", "ISO-2022-JP")) - assert_raise(Encoding::ConversionUndefined) { "\u203E".encode("Shift_JIS") } - assert_raise(Encoding::ConversionUndefined) { "\u203E".encode("Windows-31J") } - assert_raise(Encoding::ConversionUndefined) { "\u203E".encode("EUC-JP") } - assert_raise(Encoding::ConversionUndefined) { "\u203E".encode("eucJP-ms") } - assert_raise(Encoding::ConversionUndefined) { "\u203E".encode("CP51932") } + assert_raise(Encoding::ConversionUndefinedError) { "\u203E".encode("Shift_JIS") } + assert_raise(Encoding::ConversionUndefinedError) { "\u203E".encode("Windows-31J") } + assert_raise(Encoding::ConversionUndefinedError) { "\u203E".encode("EUC-JP") } + assert_raise(Encoding::ConversionUndefinedError) { "\u203E".encode("eucJP-ms") } + assert_raise(Encoding::ConversionUndefinedError) { "\u203E".encode("CP51932") } end + def test_nothing_changed + a = "James".force_encoding("US-ASCII") + b = a.encode("Shift_JIS") + assert_equal(Encoding::US_ASCII, a.encoding) + assert_equal(Encoding::Shift_JIS, b.encoding) + end end |