ruby-changes:29179
From: akr <ko1@a...>
Date: Tue, 11 Jun 2013 22:16:22 +0900 (JST)
Subject: [ruby-changes:29179] akr:r41231 (trunk): rb_integer_pack tests refined.
akr 2013-06-11 22:15:56 +0900 (Tue, 11 Jun 2013) New Revision: 41231 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=41231 Log: rb_integer_pack tests refined. Modified files: trunk/ext/-test-/bignum/pack.c trunk/test/-ext-/bignum/test_pack.rb Index: ext/-test-/bignum/pack.c =================================================================== --- ext/-test-/bignum/pack.c (revision 41230) +++ ext/-test-/bignum/pack.c (revision 41231) @@ -2,20 +2,37 @@ https://github.com/ruby/ruby/blob/trunk/ext/-test-/bignum/pack.c#L2 #include "internal.h" static VALUE -rb_integer_pack_m(VALUE val, VALUE buf, VALUE wordsize_arg, VALUE nails, VALUE flags) +rb_integer_pack_raw_m(VALUE val, VALUE buf, VALUE numwords_arg, VALUE wordsize_arg, VALUE nails, VALUE flags) { int sign; - size_t count = 0; + size_t numwords = 0; size_t wordsize = NUM2SIZET(wordsize_arg); StringValue(buf); rb_str_modify(buf); - count = wordsize == 0 ? 0 : RSTRING_LEN(buf) / wordsize; sign = rb_integer_pack(val, - RSTRING_PTR(buf), count, + RSTRING_PTR(buf), NUM2SIZET(numwords_arg), + NUM2SIZET(wordsize_arg), NUM2SIZET(nails), NUM2INT(flags)); + + return rb_ary_new_from_args(2, INT2NUM(sign), rb_str_new(RSTRING_PTR(buf), wordsize * numwords)); +} + +static VALUE +rb_integer_pack_m(VALUE val, VALUE numwords_arg, VALUE wordsize_arg, VALUE nails, VALUE flags) +{ + int sign; + size_t numwords = NUM2SIZET(numwords_arg); + size_t wordsize = NUM2SIZET(wordsize_arg); + VALUE buf; + + if (numwords != 0 && wordsize != 0 && LONG_MAX / wordsize < numwords) + rb_raise(rb_eArgError, "too big numwords * wordsize"); + buf = rb_str_new(NULL, numwords * wordsize); + sign = rb_integer_pack(val, + RSTRING_PTR(buf), numwords, wordsize, NUM2SIZET(nails), NUM2INT(flags)); - return rb_ary_new_from_args(3, INT2NUM(sign), rb_str_new(RSTRING_PTR(buf), wordsize * count), SIZET2NUM(count)); + return rb_assoc_new(INT2NUM(sign), buf); } static VALUE @@ -37,18 +54,19 @@ rb_integer_pack_2comp_m(VALUE val, VALUE https://github.com/ruby/ruby/blob/trunk/ext/-test-/bignum/pack.c#L54 } static VALUE -rb_integer_unpack_m(VALUE klass, VALUE sign, VALUE buf, VALUE wordcount, VALUE wordsize, VALUE nails, VALUE flags) +rb_integer_unpack_m(VALUE klass, VALUE sign, VALUE buf, VALUE numwords, VALUE wordsize, VALUE nails, VALUE flags) { StringValue(buf); return rb_integer_unpack(NUM2INT(sign), RSTRING_PTR(buf), - NUM2SIZET(wordcount), NUM2SIZET(wordsize), + NUM2SIZET(numwords), NUM2SIZET(wordsize), NUM2SIZET(nails), NUM2INT(flags)); } void Init_pack(VALUE klass) { + rb_define_method(rb_cInteger, "test_pack_raw", rb_integer_pack_raw_m, 5); rb_define_method(rb_cInteger, "test_pack", rb_integer_pack_m, 4); rb_define_method(rb_cInteger, "test_pack_2comp", rb_integer_pack_2comp_m, 4); rb_define_singleton_method(rb_cInteger, "test_unpack", rb_integer_unpack_m, 6); Index: test/-ext-/bignum/test_pack.rb =================================================================== --- test/-ext-/bignum/test_pack.rb (revision 41230) +++ test/-ext-/bignum/test_pack.rb (revision 41231) @@ -15,61 +15,61 @@ class TestBignum < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/-ext-/bignum/test_pack.rb#L15 BIG_ENDIAN = Integer::INTEGER_PACK_BIG_ENDIAN def test_pack_zero - assert_equal([0, "", 0], 0.test_pack("", 1, 0, BIG_ENDIAN)) + assert_equal([0, ""], 0.test_pack(0, 1, 0, BIG_ENDIAN)) end def test_pack_argument_check - assert_raise(ArgumentError) { 0.test_pack("", 1, 0, MSBYTE_FIRST) } - assert_raise(ArgumentError) { 0.test_pack("", 1, 0, MSWORD_FIRST) } - assert_raise(ArgumentError) { 0.test_pack("", 0, 0, BIG_ENDIAN) } - assert_raise(ArgumentError) { 0.test_pack("", 1, 8, BIG_ENDIAN) } + assert_raise(ArgumentError) { 0.test_pack_raw("", 0, 1, 0, MSBYTE_FIRST) } + assert_raise(ArgumentError) { 0.test_pack_raw("", 0, 1, 0, MSWORD_FIRST) } + assert_raise(ArgumentError) { 0.test_pack_raw("", 0, 0, 0, BIG_ENDIAN) } + assert_raise(ArgumentError) { 0.test_pack_raw("", 0, 1, 8, BIG_ENDIAN) } # assume sizeof(ssize_t) == sizeof(intptr_t) - assert_raise(ArgumentError) { 0.test_pack("", 1 << ([""].pack("p").length * 8 - 1), 0, BIG_ENDIAN) } + assert_raise(ArgumentError) { 0.test_pack_raw("", 1 << ([""].pack("p").length * 8 - 1), 0, BIG_ENDIAN) } end def test_pack_wordsize - assert_equal([1, "\x01", 1], 1.test_pack("x", 1, 0, BIG_ENDIAN)) - assert_equal([1, "\x00\x01", 1], 1.test_pack("xx", 2, 0, BIG_ENDIAN)) - assert_equal([1, "\x00\x00\x01", 1], 1.test_pack("xxx", 3, 0, BIG_ENDIAN)) - assert_equal([1, "\x01", 1], 1.test_pack("x", 1, 0, LITTLE_ENDIAN)) - assert_equal([1, "\x01\x00", 1], 1.test_pack("xx", 2, 0, LITTLE_ENDIAN)) - assert_equal([1, "\x01\x00\x00", 1], 1.test_pack("xxx", 3, 0, LITTLE_ENDIAN)) + assert_equal([1, "\x01"], 1.test_pack(1, 1, 0, BIG_ENDIAN)) + assert_equal([1, "\x00\x01"], 1.test_pack(1, 2, 0, BIG_ENDIAN)) + assert_equal([1, "\x00\x00\x01"], 1.test_pack(1, 3, 0, BIG_ENDIAN)) + assert_equal([1, "\x01"], 1.test_pack(1, 1, 0, LITTLE_ENDIAN)) + assert_equal([1, "\x01\x00"], 1.test_pack(1, 2, 0, LITTLE_ENDIAN)) + assert_equal([1, "\x01\x00\x00"], 1.test_pack(1, 3, 0, LITTLE_ENDIAN)) end def test_pack_fixed_buffer - assert_equal([0, "\x00\x00", 2], 0.test_pack("xx", 1, 0, BIG_ENDIAN)) - assert_equal([1, "\x00\x01", 2], 0x01.test_pack("xx", 1, 0, BIG_ENDIAN)) - assert_equal([1, "\x02\x01", 2], 0x0201.test_pack("xx", 1, 0, BIG_ENDIAN)) - assert_equal([2, "\x02\x01", 2], 0x030201.test_pack("xx", 1, 0, BIG_ENDIAN)) - assert_equal([2, "\x02\x01", 2], 0x04030201.test_pack("xx", 1, 0, BIG_ENDIAN)) - assert_equal([0, "\x00\x00", 2], 0.test_pack("xx", 1, 0, LITTLE_ENDIAN)) - assert_equal([1, "\x01\x00", 2], 0x01.test_pack("xx", 1, 0, LITTLE_ENDIAN)) - assert_equal([1, "\x01\x02", 2], 0x0201.test_pack("xx", 1, 0, LITTLE_ENDIAN)) - assert_equal([2, "\x01\x02", 2], 0x030201.test_pack("xx", 1, 0, LITTLE_ENDIAN)) - assert_equal([2, "\x01\x02", 2], 0x04030201.test_pack("xx", 1, 0, LITTLE_ENDIAN)) + assert_equal([0, "\x00\x00"], 0.test_pack(2, 1, 0, BIG_ENDIAN)) + assert_equal([1, "\x00\x01"], 0x01.test_pack(2, 1, 0, BIG_ENDIAN)) + assert_equal([1, "\x02\x01"], 0x0201.test_pack(2, 1, 0, BIG_ENDIAN)) + assert_equal([2, "\x02\x01"], 0x030201.test_pack(2, 1, 0, BIG_ENDIAN)) + assert_equal([2, "\x02\x01"], 0x04030201.test_pack(2, 1, 0, BIG_ENDIAN)) + assert_equal([0, "\x00\x00"], 0.test_pack(2, 1, 0, LITTLE_ENDIAN)) + assert_equal([1, "\x01\x00"], 0x01.test_pack(2, 1, 0, LITTLE_ENDIAN)) + assert_equal([1, "\x01\x02"], 0x0201.test_pack(2, 1, 0, LITTLE_ENDIAN)) + assert_equal([2, "\x01\x02"], 0x030201.test_pack(2, 1, 0, LITTLE_ENDIAN)) + assert_equal([2, "\x01\x02"], 0x04030201.test_pack(2, 1, 0, LITTLE_ENDIAN)) end def test_pack_wordorder_and_endian - assert_equal([1, "\x12\x34\x56\x78", 2], 0x12345678.test_pack("xxxx", 2, 0, MSWORD_FIRST|MSBYTE_FIRST)) - assert_equal([1, "\x34\x12\x78\x56", 2], 0x12345678.test_pack("xxxx", 2, 0, MSWORD_FIRST|LSBYTE_FIRST)) - assert_equal([1, "\x56\x78\x12\x34", 2], 0x12345678.test_pack("xxxx", 2, 0, LSWORD_FIRST|MSBYTE_FIRST)) - assert_equal([1, "\x78\x56\x34\x12", 2], 0x12345678.test_pack("xxxx", 2, 0, LSWORD_FIRST|LSBYTE_FIRST)) + assert_equal([1, "\x12\x34\x56\x78"], 0x12345678.test_pack(2, 2, 0, MSWORD_FIRST|MSBYTE_FIRST)) + assert_equal([1, "\x34\x12\x78\x56"], 0x12345678.test_pack(2, 2, 0, MSWORD_FIRST|LSBYTE_FIRST)) + assert_equal([1, "\x56\x78\x12\x34"], 0x12345678.test_pack(2, 2, 0, LSWORD_FIRST|MSBYTE_FIRST)) + assert_equal([1, "\x78\x56\x34\x12"], 0x12345678.test_pack(2, 2, 0, LSWORD_FIRST|LSBYTE_FIRST)) end def test_pack_native_endian - assert_equal([1, [0x1234].pack("S!"), 1], 0x1234.test_pack("xx", 2, 0, MSWORD_FIRST|NATIVE_BYTE_ORDER)) + assert_equal([1, [0x1234].pack("S!")], 0x1234.test_pack(1, 2, 0, MSWORD_FIRST|NATIVE_BYTE_ORDER)) end def test_pack_nail - assert_equal([1, "\x01\x00\x00\x00\x01\x01", 6], 0b100011.test_pack("xxxxxx", 1, 7, BIG_ENDIAN)) - assert_equal([1, "\x01\x02\x03\x04\x05\x06\x07\x08", 8], 0x12345678.test_pack("xxxxxxxx", 1, 4, BIG_ENDIAN)) - assert_equal([1, "\x00\x12\x00\x34\x00\x56\x00\x78", 4], 0x12345678.test_pack("xxxxxxxx", 2, 8, BIG_ENDIAN)) + assert_equal([1, "\x01\x00\x00\x00\x01\x01"], 0b100011.test_pack(6, 1, 7, BIG_ENDIAN)) + assert_equal([1, "\x01\x02\x03\x04\x05\x06\x07\x08"], 0x12345678.test_pack(8, 1, 4, BIG_ENDIAN)) + assert_equal([1, "\x00\x12\x00\x34\x00\x56\x00\x78"], 0x12345678.test_pack(4, 2, 8, BIG_ENDIAN)) end def test_pack_sign - assert_equal([-1, "\x01", 1], (-1).test_pack("x", 1, 0, BIG_ENDIAN)) - assert_equal([-1, "\x80\x70\x60\x50\x40\x30\x20\x10", 8], (-0x8070605040302010).test_pack("xxxxxxxx", 1, 0, BIG_ENDIAN)) + assert_equal([-1, "\x01"], (-1).test_pack(1, 1, 0, BIG_ENDIAN)) + assert_equal([-1, "\x80\x70\x60\x50\x40\x30\x20\x10"], (-0x8070605040302010).test_pack(8, 1, 0, BIG_ENDIAN)) end def test_pack2comp_zero -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/