From 9a5fd6098d6185b8f415be06cb3af11dd29c7363 Mon Sep 17 00:00:00 2001 From: mame Date: Thu, 31 Jan 2008 14:08:14 +0000 Subject: [PATCH] * test/ruby/test_bignum.rb: suppress warnings during test. * test/ruby/test_enum.rb: ditto. * test/ruby/test_integer.rb: add tests to achieve over 90% test coverage of numeric.c. * test/ruby/test_float.rb: ditto. * test/ruby/test_fixnum.rb: ditto. * test/ruby/test_numeric.rb: ditto. * test/ruby/test_pack.rb: add tests to achieve over 90% test coverage of pack.c. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@15360 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 12 ++ test/ruby/test_bignum.rb | 9 + test/ruby/test_enum.rb | 6 + test/ruby/test_fixnum.rb | 124 +++++++++++++ test/ruby/test_float.rb | 164 +++++++++++++++++ test/ruby/test_integer.rb | 113 ++++++++++++ test/ruby/test_pack.rb | 360 ++++++++++++++++++++++++++++++++++++++ 7 files changed, 788 insertions(+) diff --git a/ChangeLog b/ChangeLog index c51c8fa5e0..ed9511f49d 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,15 @@ +Thu Jan 31 23:06:42 2008 Yusuke Endoh + + * test/ruby/test_bignum.rb: suppress warnings during test. + * test/ruby/test_enum.rb: ditto. + * test/ruby/test_integer.rb: add tests to achieve over 90% test + coverage of numeric.c. + * test/ruby/test_float.rb: ditto. + * test/ruby/test_fixnum.rb: ditto. + * test/ruby/test_numeric.rb: ditto. + * test/ruby/test_pack.rb: add tests to achieve over 90% test coverage + of pack.c. + Thu Jan 31 17:30:42 2008 NAKAMURA Usaku * marshal.c (r_object0): no need to call r_entry for immediate values. diff --git a/test/ruby/test_bignum.rb b/test/ruby/test_bignum.rb index c395e3b71a..a08519238b 100644 --- a/test/ruby/test_bignum.rb +++ b/test/ruby/test_bignum.rb @@ -1,6 +1,15 @@ require 'test/unit' class TestBignum < Test::Unit::TestCase + def setup + @verbose = $VERBOSE + $VERBOSE = nil + end + + def teardown + $VERBOSE = @verbose + end + def fact(n) return 1 if n == 0 f = 1 diff --git a/test/ruby/test_enum.rb b/test/ruby/test_enum.rb index 3e8c3c678d..74f6e4e466 100644 --- a/test/ruby/test_enum.rb +++ b/test/ruby/test_enum.rb @@ -14,6 +14,12 @@ class TestEnumerable < Test::Unit::TestCase yield 2 end end + @verbose = $VERBOSE + $VERBOSE = nil + end + + def teardown + $VERBOSE = @verbose end def test_grep diff --git a/test/ruby/test_fixnum.rb b/test/ruby/test_fixnum.rb index 81a091eb40..d7ec6a3396 100644 --- a/test/ruby/test_fixnum.rb +++ b/test/ruby/test_fixnum.rb @@ -118,4 +118,128 @@ class TestFixnum < Test::Unit::TestCase assert_equal(0x40000000, (-0x40000000).abs) assert_equal(0x4000000000000000, (-0x4000000000000000).abs) end + + def test_induced_from + assert_equal(1, Fixnum.induced_from(1)) + assert_raise(RangeError) { Fixnum.induced_from(2**31-1) } + 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)) + assert_raise(ArgumentError) { 10.to_s(1) } + end + + def test_plus + assert_equal(2, 1 + 1) + assert_equal(4294967297, 1 + 2**32) + assert_equal(2.0, 1 + 1.0) + assert_raise(TypeError) { 1 + nil } + end + + def test_minus + assert_equal(0, 1 - 1) + assert_equal(-4294967295, 1 - 2**32) + assert_equal(0.0, 1 - 1.0) + assert_raise(TypeError) { 1 - nil } + end + + def test_mul + assert_equal(6, 2.send(:*, 3)) + a = 2**30-1 + assert_equal(1152921502459363329, a.send(:*, a)) + + assert_equal(6.0, 2 * 3.0) + assert_raise(TypeError) { 2 * nil } + end + + def test_divide + assert_equal(2.0, 4.quo(2)) + assert_equal(2.0, 4 / 2) + assert_equal(2.0, 4.div(2)) + + assert_equal(0.5**32, 1.quo(2**32)) + assert_equal(0, 1 / (2**32)) + assert_equal(0, 1.div(2**32)) + + assert_equal(0.5, 1.quo(2.0)) + assert_equal(0.5, 1 / 2.0) + assert_equal(0, 1.div(2.0)) + + ### rational changes the behavior of Fixnum#quo + #assert_raise(TypeError) { 2.quo(nil) } + assert_raise(TypeError, NoMethodError) { 2.quo(nil) } + assert_raise(TypeError) { 2 / nil } + assert_raise(TypeError) { 2.div(nil) } + + assert_equal(0, 4.modulo(2)) + assert_equal(1, 1.modulo(2**32)) + assert_equal(1, 1.modulo(2.0)) + assert_raise(TypeError) { 2.modulo(nil) } + + assert_equal([2, 0], 4.divmod(2)) + assert_equal([0, 1], 1.divmod(2**32)) + assert_equal([0, 1], 1.divmod(2.0)) + assert_raise(TypeError) { 2.divmod(nil) } + end + + def test_pow2 + assert_equal(65536, 2**16) + assert_equal(4294967296, 2**32) + assert_equal(0.5**16, 2**-16) + assert_equal(1, (-1)**4294967296) + assert_equal(-1, (-1)**4294967295) + assert_equal(4, 2**((2**32).coerce(2).first)) + assert_equal(2, 4**0.5) + assert_equal(0, 0**0.5) + assert((0**-1.0).infinite?) + ### rational changes the behavior of Fixnum#** + #assert_raise(TypeError) { 1 ** nil } + assert_raise(TypeError, NoMethodError) { 1 ** nil } + end + + def test_cmp + assert(1 != nil) + + assert_equal(0, 1 <=> 1) + assert_equal(-1, 1 <=> 4294967296) + assert_equal(0, 1 <=> 1.0) + assert_nil(1 <=> nil) + + assert(1.send(:>, 0)) + assert(!(1.send(:>, 1))) + assert(!(1.send(:>, 2))) + assert(!(1.send(:>, 4294967296))) + assert(1.send(:>, 0.0)) + assert_raise(ArgumentError) { 1.send(:>, nil) } + + assert(1.send(:>=, 0)) + assert(1.send(:>=, 1)) + assert(!(1.send(:>=, 2))) + assert(!(1.send(:>=, 4294967296))) + assert(1.send(:>=, 0.0)) + assert_raise(ArgumentError) { 1.send(:>=, nil) } + + assert(!(1.send(:<, 0))) + assert(!(1.send(:<, 1))) + assert(1.send(:<, 2)) + assert(1.send(:<, 4294967296)) + assert(!(1.send(:<, 0.0))) + assert_raise(ArgumentError) { 1.send(:<, nil) } + + assert(!(1.send(:<=, 0))) + assert(1.send(:<=, 1)) + assert(1.send(:<=, 2)) + assert(1.send(:<=, 4294967296)) + assert(!(1.send(:<=, 0.0))) + assert_raise(ArgumentError) { 1.send(:<=, nil) } + end + + def test_id2name + assert_equal("foo", :foo.to_i.id2name) + assert_nil(0.id2name) + assert_equal(:foo, :foo.to_i.to_sym) + assert_nil(0.to_sym) + end end diff --git a/test/ruby/test_float.rb b/test/ruby/test_float.rb index 0b06bd9d4d..5742c2d48e 100644 --- a/test/ruby/test_float.rb +++ b/test/ruby/test_float.rb @@ -111,4 +111,168 @@ class TestFloat < Test::Unit::TestCase assert_equal(-3.5, (-11.5).remainder(4)) assert_equal(-3.5, (-11.5).remainder(-4)) end + + def test_to_s + inf = 1.0 / 0.0 + assert_equal("Infinity", inf.to_s) + assert_equal("-Infinity", (-inf).to_s) + assert_equal("NaN", (inf / inf).to_s) + + assert_equal("1.0e+14", 10000_00000_00000.0.to_s) + end + + def test_coerce + assert_equal(Float, 1.0.coerce(1).first.class) + end + + def test_plus + assert_equal(4.0, 2.0.send(:+, 2)) + assert_equal(4.0, 2.0.send(:+, (2**32).coerce(2).first)) + assert_equal(4.0, 2.0.send(:+, 2.0)) + assert_raise(TypeError) { 2.0.send(:+, nil) } + end + + def test_minus + assert_equal(0.0, 2.0.send(:-, 2)) + assert_equal(0.0, 2.0.send(:-, (2**32).coerce(2).first)) + assert_equal(0.0, 2.0.send(:-, 2.0)) + assert_raise(TypeError) { 2.0.send(:-, nil) } + end + + def test_mul + assert_equal(4.0, 2.0.send(:*, 2)) + assert_equal(4.0, 2.0.send(:*, (2**32).coerce(2).first)) + assert_equal(4.0, 2.0.send(:*, 2.0)) + assert_raise(TypeError) { 2.0.send(:*, nil) } + end + + def test_div2 + assert_equal(1.0, 2.0.send(:/, 2)) + assert_equal(1.0, 2.0.send(:/, (2**32).coerce(2).first)) + assert_equal(1.0, 2.0.send(:/, 2.0)) + assert_raise(TypeError) { 2.0.send(:/, nil) } + end + + def test_modulo2 + assert_equal(0.0, 2.0.send(:%, 2)) + assert_equal(0.0, 2.0.send(:%, (2**32).coerce(2).first)) + assert_equal(0.0, 2.0.send(:%, 2.0)) + assert_raise(TypeError) { 2.0.send(:%, nil) } + end + + def test_divmod2 + assert_equal([1.0, 0.0], 2.0.divmod(2)) + assert_equal([1.0, 0.0], 2.0.divmod((2**32).coerce(2).first)) + assert_equal([1.0, 0.0], 2.0.divmod(2.0)) + assert_raise(TypeError) { 2.0.divmod(nil) } + + inf = 1.0 / 0.0 + a, b = inf.divmod(0) + assert(a.infinite?) + assert(b.nan?) + + a, b = (2.0**32).divmod(1.0) + assert_equal(2**32, a) + assert_equal(0, b) + end + + def test_pow + assert_equal(1.0, 1.0 ** (2**32)) + assert_equal(1.0, 1.0 ** 1.0) + assert_raise(TypeError) { 1.0 ** nil } + end + + def test_eql + inf = 1.0 / 0.0 + nan = inf / inf + assert(1.0.eql?(1.0)) + assert(inf.eql?(inf)) + assert(!(nan.eql?(nan))) + assert(!(1.0.eql?(nil))) + + assert(1.0 == 1) + assert(1.0 != 2**32) + assert(1.0 != nan) + assert(1.0 != nil) + end + + def test_cmp + inf = 1.0 / 0.0 + nan = inf / inf + assert_equal(0, 1.0 <=> 1.0) + assert_equal(1, 1.0 <=> 0.0) + assert_equal(-1, 1.0 <=> 2.0) + assert_nil(1.0 <=> nil) + assert_nil(1.0 <=> nan) + assert_nil(nan <=> 1.0) + + assert_equal(0, 1.0 <=> 1) + assert_equal(1, 1.0 <=> 0) + assert_equal(-1, 1.0 <=> 2) + + assert_equal(-1, 1.0 <=> 2**32) + + assert_raise(ArgumentError) { 1.0 > nil } + assert_raise(ArgumentError) { 1.0 >= nil } + assert_raise(ArgumentError) { 1.0 < nil } + assert_raise(ArgumentError) { 1.0 <= nil } + end + + def test_zero_p + assert(0.0.zero?) + assert(!(1.0.zero?)) + end + + def test_infinite_p + inf = 1.0 / 0.0 + assert(1, inf.infinite?) + assert(1, (-inf).infinite?) + assert_nil(1.0.infinite?) + end + + def test_finite_p + inf = 1.0 / 0.0 + assert(!(inf.finite?)) + assert(!((-inf).finite?)) + assert(1.0.finite?) + end + + def test_floor_ceil_round_truncate + assert_equal(1, 1.5.floor) + assert_equal(2, 1.5.ceil) + assert_equal(2, 1.5.round) + assert_equal(1, 1.5.truncate) + + assert_equal(2, 2.0.floor) + assert_equal(2, 2.0.ceil) + assert_equal(2, 2.0.round) + assert_equal(2, 2.0.truncate) + + assert_equal(-2, (-1.5).floor) + assert_equal(-1, (-1.5).ceil) + assert_equal(-2, (-1.5).round) + assert_equal(-1, (-1.5).truncate) + + assert_equal(-2, (-2.0).floor) + assert_equal(-2, (-2.0).ceil) + assert_equal(-2, (-2.0).round) + assert_equal(-2, (-2.0).truncate) + + inf = 1.0/0.0 + assert_raise(FloatDomainError) { inf.floor } + assert_raise(FloatDomainError) { inf.ceil } + assert_raise(FloatDomainError) { inf.round } + assert_raise(FloatDomainError) { inf.truncate } + + assert_equal(1.100, 1.111.round(1)) + assert_equal(1.110, 1.111.round(2)) + assert_equal(11110.0, 11111.1.round(-1)) + 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 end diff --git a/test/ruby/test_integer.rb b/test/ruby/test_integer.rb index 81501a61c3..c48be11adf 100644 --- a/test/ruby/test_integer.rb +++ b/test/ruby/test_integer.rb @@ -675,4 +675,117 @@ class TestInteger < Test::Unit::TestCase } assert_raise(ArgumentError, "[ruby-core:14139]") {Integer("0__3_7_7")} end + + def test_int_p + assert(!(1.0.integer?)) + assert(1.integer?) + end + + def test_odd_p_even_p + Fixnum.class_eval do + alias odd_bak odd? + alias even_bak even? + remove_method :odd?, :even? + end + + assert(1.odd?) + assert(!(2.odd?)) + assert(!(1.even?)) + assert(2.even?) + + ensure + Fixnum.class_eval do + alias odd? odd_bak + alias even? even_bak + remove_method :odd_bak, :even_bak + end + end + + def test_succ + assert_equal(2, 1.send(:succ)) + + Fixnum.class_eval do + alias succ_bak succ + remove_method :succ + end + + assert_equal(2, 1.succ) + assert_equal(4294967297, 4294967296.succ) + + ensure + Fixnum.class_eval do + alias succ succ_bak + remove_method :succ_bak + end + end + + def test_chr + assert_equal("a", "a".ord.chr) + assert_raise(RangeError) { (-1).chr } + 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 } + assert_equal([1, 2, 3], a) + + a = [] + 1.upto(0) {|x| a << x } + assert_equal([], a) + + x = 2**30 - 1 + a = [] + x.upto(x+2) {|x| a << x } + assert_equal([x, x+1, x+2], a) + end + + def test_downto + a = [] + -1.downto(-3) {|x| a << x } + assert_equal([-1, -2, -3], a) + + a = [] + 1.downto(2) {|x| a << x } + assert_equal([], a) + + x = -(2**30) + a = [] + x.downto(x-2) {|x| a << x } + assert_equal([x, x-1, x-2], a) + end + + def test_times + (2**32).times do |i| + break if i == 2 + end + end + + def test_round + assert_equal(11111, 11111.round) + assert_equal(Fixnum, 11111.round.class) + assert_equal(11111, 11111.round(0)) + assert_equal(Fixnum, 11111.round(0).class) + + assert_equal(11111.0, 11111.round(1)) + assert_equal(Float, 11111.round(1).class) + assert_equal(11111.0, 11111.round(2)) + assert_equal(Float, 11111.round(2).class) + + assert_equal(11110, 11111.round(-1)) + assert_equal(Fixnum, 11111.round(-1).class) + assert_equal(11100, 11111.round(-2)) + assert_equal(Fixnum, 11111.round(-2).class) + + assert_equal(1111_1111_1111_1110, 1111_1111_1111_1111.round(-1)) + assert_equal(Bignum, 1111_1111_1111_1111.round(-1).class) + assert_equal(-1111_1111_1111_1110, (-1111_1111_1111_1111).round(-1)) + assert_equal(Bignum, (-1111_1111_1111_1111).round(-1).class) + end end diff --git a/test/ruby/test_pack.rb b/test/ruby/test_pack.rb index f78c48dee6..e6e2c28a5c 100644 --- a/test/ruby/test_pack.rb +++ b/test/ruby/test_pack.rb @@ -71,4 +71,364 @@ class TestPack < Test::Unit::TestCase assert_equal a[0], a.pack("p").unpack("p")[0] assert_equal a, a.pack("p").freeze.unpack("p*") end + + def test_format_string_modified + fmt = "CC" + o = Object.new + class << o; self; end.class_eval do + define_method(:to_int) { fmt.clear; 0 } + end + assert_raise(RuntimeError) do + [o, o].pack(fmt) + end + end + + def test_comment + assert_equal("\0\1", [0,1].pack(" C #foo \n C ")) + assert_equal([0,1], "\0\1".unpack(" C #foo \n C ")) + end + + def test_illegal_bang + assert_raise(ArgumentError) { [].pack("a!") } + assert_raise(ArgumentError) { "".unpack("a!") } + end + + def test_pack_unpack_aA + assert_equal("f", ["foo"].pack("A")) + assert_equal("f", ["foo"].pack("a")) + assert_equal("foo", ["foo"].pack("A*")) + assert_equal("foo", ["foo"].pack("a*")) + assert_equal("fo", ["foo"].pack("A2")) + assert_equal("fo", ["foo"].pack("a2")) + assert_equal("foo ", ["foo"].pack("A4")) + assert_equal("foo\0", ["foo"].pack("a4")) + assert_equal(" ", [nil].pack("A")) + assert_equal("\0", [nil].pack("a")) + assert_equal("", [nil].pack("A*")) + assert_equal("", [nil].pack("a*")) + assert_equal(" ", [nil].pack("A2")) + assert_equal("\0\0", [nil].pack("a2")) + + assert_equal("foo" + "\0" * 27, ["foo"].pack("a30")) + + assert_equal(["f"], "foo\0".unpack("A")) + assert_equal(["f"], "foo\0".unpack("a")) + assert_equal(["foo"], "foo\0".unpack("A4")) + assert_equal(["foo\0"], "foo\0".unpack("a4")) + assert_equal(["foo"], "foo ".unpack("A4")) + assert_equal(["foo "], "foo ".unpack("a4")) + assert_equal(["foo"], "foo".unpack("A4")) + assert_equal(["foo"], "foo".unpack("a4")) + end + + def test_pack_unpack_Z + assert_equal("f", ["foo"].pack("Z")) + assert_equal("foo\0", ["foo"].pack("Z*")) + assert_equal("fo", ["foo"].pack("Z2")) + assert_equal("foo\0\0", ["foo"].pack("Z5")) + assert_equal("\0", [nil].pack("Z")) + assert_equal("\0", [nil].pack("Z*")) + assert_equal("\0\0", [nil].pack("Z2")) + + assert_equal(["f"], "foo\0".unpack("Z")) + assert_equal(["foo"], "foo".unpack("Z*")) + assert_equal(["foo"], "foo\0".unpack("Z*")) + assert_equal(["foo"], "foo".unpack("Z5")) + end + + def test_pack_unpack_bB + assert_equal("\xff\x00", ["1111111100000000"].pack("b*")) + assert_equal("\x01\x02", ["1000000001000000"].pack("b*")) + assert_equal("", ["1"].pack("b0")) + assert_equal("\x01", ["1"].pack("b1")) + assert_equal("\x01\x00", ["1"].pack("b2")) + assert_equal("\x01\x00", ["1"].pack("b3")) + assert_equal("\x01\x00\x00", ["1"].pack("b4")) + assert_equal("\x01\x00\x00", ["1"].pack("b5")) + assert_equal("\x01\x00\x00\x00", ["1"].pack("b6")) + + assert_equal("\xff\x00", ["1111111100000000"].pack("B*")) + assert_equal("\x01\x02", ["0000000100000010"].pack("B*")) + assert_equal("", ["1"].pack("B0")) + assert_equal("\x80", ["1"].pack("B1")) + assert_equal("\x80\x00", ["1"].pack("B2")) + assert_equal("\x80\x00", ["1"].pack("B3")) + assert_equal("\x80\x00\x00", ["1"].pack("B4")) + assert_equal("\x80\x00\x00", ["1"].pack("B5")) + assert_equal("\x80\x00\x00\x00", ["1"].pack("B6")) + + assert_equal(["1111111100000000"], "\xff\x00".unpack("b*")) + assert_equal(["1000000001000000"], "\x01\x02".unpack("b*")) + assert_equal([""], "".unpack("b0")) + assert_equal(["1"], "\x01".unpack("b1")) + assert_equal(["10"], "\x01".unpack("b2")) + assert_equal(["100"], "\x01".unpack("b3")) + + assert_equal(["1111111100000000"], "\xff\x00".unpack("B*")) + assert_equal(["0000000100000010"], "\x01\x02".unpack("B*")) + assert_equal([""], "".unpack("B0")) + assert_equal(["1"], "\x80".unpack("B1")) + assert_equal(["10"], "\x80".unpack("B2")) + assert_equal(["100"], "\x80".unpack("B3")) + end + + def test_pack_unpack_hH + assert_equal("\x01\xfe", ["10ef"].pack("h*")) + assert_equal("", ["10ef"].pack("h0")) + assert_equal("\x01\x0e", ["10ef"].pack("h3")) + assert_equal("\x01\xfe\x0", ["10ef"].pack("h5")) + + assert_equal("\x10\xef", ["10ef"].pack("H*")) + assert_equal("", ["10ef"].pack("H0")) + assert_equal("\x10\xe0", ["10ef"].pack("H3")) + assert_equal("\x10\xef\x0", ["10ef"].pack("H5")) + + assert_equal(["10ef"], "\x01\xfe".unpack("h*")) + assert_equal([""], "\x01\xfe".unpack("h0")) + assert_equal(["1"], "\x01\xfe".unpack("h1")) + assert_equal(["10"], "\x01\xfe".unpack("h2")) + assert_equal(["10e"], "\x01\xfe".unpack("h3")) + assert_equal(["10ef"], "\x01\xfe".unpack("h4")) + assert_equal(["10ef"], "\x01\xfe".unpack("h5")) + + assert_equal(["10ef"], "\x10\xef".unpack("H*")) + assert_equal([""], "\x10\xef".unpack("H0")) + assert_equal(["1"], "\x10\xef".unpack("H1")) + assert_equal(["10"], "\x10\xef".unpack("H2")) + assert_equal(["10e"], "\x10\xef".unpack("H3")) + assert_equal(["10ef"], "\x10\xef".unpack("H4")) + assert_equal(["10ef"], "\x10\xef".unpack("H5")) + end + + def test_pack_unpack_cC + assert_equal("\0\1\xff", [0, 1, 255].pack("c*")) + assert_equal("\0\1\xff", [0, 1, -1].pack("c*")) + + assert_equal("\0\1\xff", [0, 1, 255].pack("C*")) + assert_equal("\0\1\xff", [0, 1, -1].pack("C*")) + + assert_equal([0, 1, -1], "\0\1\xff".unpack("c*")) + + assert_equal([0, 1, 255], "\0\1\xff".unpack("C*")) + end + + def test_pack_unpack_sS + s1 = [513, -514].pack("s*") + s2 = [513, 65022].pack("S*") + assert_equal(s1, s2) + assert_equal([513, -514], s2.unpack("s*")) + assert_equal([513, 65022], s1.unpack("S*")) + + s1 = [513, -514].pack("s!*") + s2 = [513, 65022].pack("S!*") + assert_equal(s1, s2) + assert_equal([513, -514], s2.unpack("s!*")) + assert_equal([513, 65022], s1.unpack("S!*")) + end + + def test_pack_unpack_iI + s1 = [67305985, -50462977].pack("i*") + s2 = [67305985, 4244504319].pack("I*") + assert_equal(s1, s2) + assert_equal([67305985, -50462977], s2.unpack("i*")) + assert_equal([67305985, 4244504319], s1.unpack("I*")) + + s1 = [67305985, -50462977].pack("i!*") + s2 = [67305985, 4244504319].pack("I!*") + assert_equal(s1, s2) + assert_equal([67305985, -50462977], s2.unpack("i!*")) + assert_equal([67305985, 4244504319], s1.unpack("I!*")) + end + + def test_pack_unpack_lL + s1 = [67305985, -50462977].pack("l*") + s2 = [67305985, 4244504319].pack("L*") + assert_equal(s1, s2) + assert_equal([67305985, -50462977], s2.unpack("l*")) + assert_equal([67305985, 4244504319], s1.unpack("L*")) + + s1 = [67305985, -50462977].pack("l!*") + s2 = [67305985, 4244504319].pack("L!*") + assert_equal(s1, s2) + assert_equal([67305985, -50462977], s2.unpack("l!*")) + assert_equal([67305985, 4244504319], s1.unpack("L!*")) + end + + def test_pack_unpack_qQ + s1 = [578437695752307201, -506097522914230529].pack("q*") + s2 = [578437695752307201, 17940646550795321087].pack("Q*") + assert_equal(s1, s2) + assert_equal([578437695752307201, -506097522914230529], s2.unpack("q*")) + assert_equal([578437695752307201, 17940646550795321087], s1.unpack("Q*")) + end + + def test_pack_unpack_nN + assert_equal("\000\000\000\001\377\377\177\377\200\000\377\377", [0,1,-1,32767,-32768,65535].pack("n*")) + assert_equal("\000\000\000\000\000\000\000\001\377\377\377\377", [0,1,-1].pack("N*")) + + assert_equal([0,1,65535,32767,32768,65535], "\000\000\000\001\377\377\177\377\200\000\377\377".unpack("n*")) + assert_equal([0,1,4294967295], "\000\000\000\000\000\000\000\001\377\377\377\377".unpack("N*")) + end + + def test_pack_unpack_vV + assert_equal("\000\000\001\000\377\377\377\177\000\200\377\377", [0,1,-1,32767,-32768,65535].pack("v*")) + assert_equal("\000\000\000\000\001\000\000\000\377\377\377\377", [0,1,-1].pack("V*")) + + assert_equal([0,1,65535,32767,32768,65535], "\000\000\001\000\377\377\377\177\000\200\377\377".unpack("v*")) + assert_equal([0,1,4294967295], "\000\000\000\000\001\000\000\000\377\377\377\377".unpack("V*")) + end + + def test_pack_unpack_fdeEgG + inf = 1.0/0.0 + nan = inf/inf + [0.0, 1.0, 3.0, inf, -inf, nan].each do |x| + %w(f d e E g G).each do |f| + v = [x].pack(f).unpack(f) + if x.nan? + assert(v.first.nan?) + else + assert_equal([x], v) + end + end + end + end + + def test_pack_unpack_x + assert_equal("", [].pack("x0")) + assert_equal("\0", [].pack("x")) + assert_equal("\0" * 30, [].pack("x30")) + + assert_equal([0, 2], "\x00\x00\x02".unpack("CxC")) + assert_raise(ArgumentError) { "".unpack("x") } + end + + def test_pack_unpack_X + assert_equal("\x00\x02", [0, 1, 2].pack("CCXC")) + assert_equal("\x02", [0, 1, 2].pack("CCX2C")) + assert_raise(ArgumentError) { [].pack("X") } + + assert_equal([0, 2, 2], "\x00\x02".unpack("CCXC")) + assert_raise(ArgumentError) { "".unpack("X") } + end + + def test_pack_unpack_atmark + assert_equal("\x01\x00\x00\x02", [1, 2].pack("C@3C")) + assert_equal("\x02", [1, 2].pack("C@0C")) + assert_equal("\x01\x02", [1, 2].pack("C@1C")) + + assert_equal([1, 2], "\x01\x00\x00\x02".unpack("C@3C")) + assert_equal([nil], "\x00".unpack("@1C")) # is it OK? + assert_raise(ArgumentError) { "\x00".unpack("@2C") } + end + + def test_pack_unpack_percent + assert_raise(ArgumentError) { [].pack("%") } + assert_raise(ArgumentError) { "".unpack("%") } + end + + def test_pack_unpack_U + assert_equal([0], [0].pack("U").unpack("U")) + assert_equal([0x80], [0x80].pack("U").unpack("U")) + assert_equal([0x800], [0x800].pack("U").unpack("U")) + assert_equal([0x10000], [0x10000].pack("U").unpack("U")) + assert_equal([0x400000], [0x400000].pack("U").unpack("U")) + + assert_raise(ArgumentError) { "\x80".unpack("U") } + assert_raise(ArgumentError) { "\xff".unpack("U") } + assert_raise(ArgumentError) { "\xfc\x00".unpack("U") } + assert_raise(ArgumentError) { "\xc0\xc0".unpack("U") } + assert_raise(ArgumentError) { "\xe0\x80\x80".unpack("U") } + end + + def test_pack_unpack_u + assert_equal("", [""].pack("u")) + assert_equal("!80``\n", ["a"].pack("u")) + assert_equal("#86)C\n", ["abc"].pack("u")) + assert_equal("$86)C9```\n", ["abcd"].pack("u")) + assert_equal("M86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A\n", ["a"*45].pack("u")) + assert_equal("M86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A\n!80``\n", ["a"*46].pack("u")) + assert_equal("&86)C9&5F\n#9VAI\n", ["abcdefghi"].pack("u6")) + + assert_equal([""], "".unpack("u")) + assert_equal(["a"], "!80``\n".unpack("u")) + assert_equal(["abc"], "#86)C\n".unpack("u")) + assert_equal(["abcd"], "$86)C9```\n".unpack("u")) + assert_equal(["a"*45], "M86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A\n".unpack("u")) + assert_equal(["a"*46], "M86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A\n!80``\n".unpack("u")) + assert_equal(["abcdefghi"], "&86)C9&5F\n#9VAI\n".unpack("u")) + + assert_equal(["\x00"], "\"\n".unpack("u")) + assert_equal(["\x00"], "! \r \n".unpack("u")) + end + + def test_pack_unpack_m + assert_equal("", [""].pack("m")) + assert_equal("AA==\n", ["\0"].pack("m")) + assert_equal("AAA=\n", ["\0\0"].pack("m")) + assert_equal("AAAA\n", ["\0\0\0"].pack("m")) + assert_equal("/w==\n", ["\377"].pack("m")) + assert_equal("//8=\n", ["\377\377"].pack("m")) + assert_equal("////\n", ["\377\377\377"].pack("m")) + + assert_equal([""], "".unpack("m")) + assert_equal(["\0"], "AA==\n".unpack("m")) + assert_equal(["\0\0"], "AAA=\n".unpack("m")) + assert_equal(["\0\0\0"], "AAAA\n".unpack("m")) + assert_equal(["\377"], "/w==\n".unpack("m")) + assert_equal(["\377\377"], "//8=\n".unpack("m")) + assert_equal(["\377\377\377"], "////\n".unpack("m")) + end + + def test_pack_unpack_M + assert_equal("a b c\td =\n\ne=\n", ["a b c\td \ne"].pack("M")) + assert_equal(["a b c\td \ne"], "a b c\td =\n\ne=\n".unpack("M")) + assert_equal("=00=\n", ["\0"].pack("M")) + assert_equal("a"*73+"=\na=\n", ["a"*74].pack("M")) + assert_equal(("a"*73+"=\n")*14+"a=\n", ["a"*1023].pack("M")) + assert_equal(["\0"], "=00=\n".unpack("M")) + assert_equal(["a"*74], ("a"*73+"=\na=\n").unpack("M")) + assert_equal(["a"*1023], (("a"*73+"=\n")*14+"a=\n").unpack("M")) + assert_equal(["\x0a"], "=0a=\n".unpack("M")) + assert_equal(["\x0a"], "=0A=\n".unpack("M")) + assert_equal([""], "=0Z=\n".unpack("M")) + assert_equal([""], "=\r\n".unpack("M")) + end + + def test_pack_unpack_P2 + assert_raise(ArgumentError) { ["abc"].pack("P4") } + assert_raise(ArgumentError) { [""].pack("P") } + + assert_equal([], ".".unpack("P")) + assert_equal([], ".".unpack("p")) + assert_equal([nil], ("\0" * 1024).unpack("P")) + end + + def test_pack_p2 + assert([nil].pack("p") =~ /\A\0*\Z/) + end + + def test_pack_unpack_w + assert_equal("\000", [0].pack("w")) + assert_equal("\001", [1].pack("w")) + assert_equal("\177", [127].pack("w")) + assert_equal("\201\000", [128].pack("w")) + assert_equal("\377\177", [0x3fff].pack("w")) + assert_equal("\201\200\000", [0x4000].pack("w")) + assert_equal("\203\377\377\377\177", [0x3fffffff].pack("w")) + assert_equal("\204\200\200\200\000", [0x40000000].pack("w")) + assert_equal("\217\377\377\377\177", [0xffffffff].pack("w")) + assert_equal("\220\200\200\200\000", [0x100000000].pack("w")) + assert_raise(ArgumentError) { [-1].pack("w") } + + assert_equal([0], "\000".unpack("w")) + assert_equal([1], "\001".unpack("w"), [1]) + assert_equal([127], "\177".unpack("w"), [127]) + assert_equal([128], "\201\000".unpack("w"), [128]) + assert_equal([0x3fff], "\377\177".unpack("w"), [0x3fff]) + assert_equal([0x4000], "\201\200\000".unpack("w"), [0x4000]) + assert_equal([0x3fffffff], "\203\377\377\377\177".unpack("w"), [0x3fffffff]) + assert_equal([0x40000000], "\204\200\200\200\000".unpack("w"), [0x40000000]) + assert_equal([0xffffffff], "\217\377\377\377\177".unpack("w"), [0xffffffff]) + assert_equal([0x100000000], "\220\200\200\200\000".unpack("w"), [0x100000000]) + end end