From f31aed6255a67be4f7c83d231ab4697f4f92821d Mon Sep 17 00:00:00 2001 From: akr Date: Fri, 7 Jun 2013 15:02:39 +0000 Subject: * bignum.c (rb_integer_pack): Arguments changed. Use flags to specify word order and byte order. (rb_integer_unpack): Ditto. (validate_integer_format): Follow the above change. (integer_format_loop_setup): Ditto. * pack.c: Ditto. * internal.h: Ditto. (INTEGER_PACK_MSWORD_FIRST): Defined. (INTEGER_PACK_LSWORD_FIRST): Ditto. (INTEGER_PACK_MSBYTE_FIRST): Ditto. (INTEGER_PACK_LSBYTE_FIRST): Ditto. (INTEGER_PACK_NATIVE_BYTE_ORDER): Ditto. (INTEGER_PACK_LITTLE_ENDIAN): Ditto. (INTEGER_PACK_BIG_ENDIAN): Ditto. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@41151 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 19 +++++++ bignum.c | 100 +++++++++++++++++++--------------- ext/-test-/bignum/pack.c | 21 ++++--- internal.h | 19 ++++++- pack.c | 4 +- test/-ext-/bignum/test_pack.rb | 121 ++++++++++++++++++++++------------------- 6 files changed, 174 insertions(+), 110 deletions(-) diff --git a/ChangeLog b/ChangeLog index eae83b7e2b..7d8ed49dec 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,22 @@ +Fri Jun 7 23:58:06 2013 Tanaka Akira + + * bignum.c (rb_integer_pack): Arguments changed. Use flags to + specify word order and byte order. + (rb_integer_unpack): Ditto. + (validate_integer_format): Follow the above change. + (integer_format_loop_setup): Ditto. + + * pack.c: Ditto. + + * internal.h: Ditto. + (INTEGER_PACK_MSWORD_FIRST): Defined. + (INTEGER_PACK_LSWORD_FIRST): Ditto. + (INTEGER_PACK_MSBYTE_FIRST): Ditto. + (INTEGER_PACK_LSBYTE_FIRST): Ditto. + (INTEGER_PACK_NATIVE_BYTE_ORDER): Ditto. + (INTEGER_PACK_LITTLE_ENDIAN): Ditto. + (INTEGER_PACK_BIG_ENDIAN): Ditto. + Fri Jun 7 22:10:50 2013 NARUSE, Yui * lib/rubygems/specification.rb (Gem::Specification#to_yaml): diff --git a/bignum.c b/bignum.c index 20e9804e4c..13ac5ed53c 100644 --- a/bignum.c +++ b/bignum.c @@ -537,13 +537,26 @@ rb_absint_size_in_word(VALUE val, size_t word_numbits_arg, size_t *number_of_lea return numwords; } +#define INTEGER_PACK_WORDORDER_MASK \ + (INTEGER_PACK_MSWORD_FIRST | \ + INTEGER_PACK_LSWORD_FIRST) +#define INTEGER_PACK_BYTEORDER_MASK \ + (INTEGER_PACK_MSBYTE_FIRST | \ + INTEGER_PACK_LSBYTE_FIRST | \ + INTEGER_PACK_NATIVE_BYTE_ORDER) + static void -validate_integer_format(int wordorder, size_t wordsize, int endian, size_t nails) -{ - if (wordorder != 1 && wordorder != -1) - rb_raise(rb_eArgError, "unexpected wordorder: %d", wordorder); - if (endian != 1 && endian != -1 && endian != 0) - rb_raise(rb_eArgError, "unexpected endian: %d", endian); +validate_integer_format(size_t wordsize, size_t nails, int flags) +{ + int wordorder_bits = flags & INTEGER_PACK_WORDORDER_MASK; + int byteorder_bits = flags & INTEGER_PACK_BYTEORDER_MASK; + if (wordorder_bits != INTEGER_PACK_MSWORD_FIRST && + wordorder_bits != INTEGER_PACK_LSWORD_FIRST) + rb_raise(rb_eArgError, "unexpected wordorder"); + if (byteorder_bits != INTEGER_PACK_MSBYTE_FIRST && + byteorder_bits != INTEGER_PACK_LSBYTE_FIRST && + byteorder_bits != INTEGER_PACK_NATIVE_BYTE_ORDER) + rb_raise(rb_eArgError, "unexpected endian"); if (wordsize == 0) rb_raise(rb_eArgError, "invalid wordsize: %"PRI_SIZE_PREFIX"u", wordsize); if (SSIZE_MAX < wordsize) @@ -553,7 +566,8 @@ validate_integer_format(int wordorder, size_t wordsize, int endian, size_t nails } static void -integer_format_loop_setup(size_t wordcount, int wordorder, size_t wordsize, int endian, size_t nails, +integer_format_loop_setup( + size_t numwords, size_t wordsize, size_t nails, int flags, size_t *word_num_fullbytes_ret, int *word_num_partialbits_ret, size_t *word_num_nailbytes_ret, @@ -563,6 +577,8 @@ integer_format_loop_setup(size_t wordcount, int wordorder, size_t wordsize, int size_t *byte_start_ret, int *byte_step_ret) { + int wordorder_bits = flags & INTEGER_PACK_WORDORDER_MASK; + int byteorder_bits = flags & INTEGER_PACK_BYTEORDER_MASK; size_t word_num_fullbytes; int word_num_partialbits; size_t word_num_nailbytes; @@ -584,25 +600,25 @@ integer_format_loop_setup(size_t wordcount, int wordorder, size_t wordsize, int word_num_nailbytes = wordsize - word_num_fullbytes; } - if (wordorder == 1) { - word_start = wordsize*(wordcount-1); + if (wordorder_bits == INTEGER_PACK_MSWORD_FIRST) { + word_start = wordsize*(numwords-1); word_step = -(ssize_t)wordsize; word_last = 0; } else { word_start = 0; word_step = wordsize; - word_last = wordsize*(wordcount-1); + word_last = wordsize*(numwords-1); } - if (endian == 0) { + if (byteorder_bits == INTEGER_PACK_NATIVE_BYTE_ORDER) { #ifdef WORDS_BIGENDIAN - endian = 1; + byteorder_bits = INTEGER_PACK_MSBYTE_FIRST; #else - endian = -1; + byteorder_bits = INTEGER_PACK_LSBYTE_FIRST; #endif } - if (endian == 1) { + if (byteorder_bits == INTEGER_PACK_MSBYTE_FIRST) { byte_step = -1; byte_start = wordsize-1; } @@ -653,21 +669,20 @@ int_export_take_lowbits(int n, BDIGIT_DBL *ddp, int *numbits_in_dd_p) * 0 for zero. * -1 for negative without overflow. 1 for positive without overflow. * -2 for negative overflow. 2 for positive overflow. - * [wordcount_allocated] the number of words allocated is returned in *wordcount_allocated if it is not NULL. + * [numwords_allocated] the number of words allocated is returned in *numwords_allocated if it is not NULL. * It is not modified if words is not NULL. * [words] buffer to export abs(val). allocated by xmalloc if it is NULL. - * [wordcount] the size of given buffer as number of words (only meaningful when words is not NULL). - * [wordorder] order of words: 1 for most significant word first. -1 for least significant word first. + * [numwords] the size of given buffer as number of words (only meaningful when words is not NULL). * [wordsize] the size of word as number of bytes. - * [endian] order of bytes in a word: 1 for most significant byte first. -1 for least significant byte first. 0 for native endian. * [nails] number of padding bits in a word. Most significant nails bits of each word are filled by zero. + * [flags] bitwise or of constants which name starts "INTEGER_PACK_". It specifies word order and byte order. * * This function returns words or the allocated buffer if words is NULL. * */ void * -rb_integer_pack(VALUE val, int *signp, size_t *wordcount_allocated, void *words, size_t wordcount, int wordorder, size_t wordsize, int endian, size_t nails) +rb_integer_pack(VALUE val, int *signp, size_t *numwords_allocated, void *words, size_t numwords, size_t wordsize, size_t nails, int flags) { int sign; BDIGIT *dp; @@ -677,9 +692,9 @@ rb_integer_pack(VALUE val, int *signp, size_t *wordcount_allocated, void *words, val = rb_to_int(val); - validate_integer_format(wordorder, wordsize, endian, nails); - if (words && SIZE_MAX / wordsize < wordcount) - rb_raise(rb_eArgError, "too big count * wordsize: %"PRI_SIZE_PREFIX"u * %"PRI_SIZE_PREFIX"u", wordcount, wordsize); + validate_integer_format(wordsize, nails, flags); + if (words && SIZE_MAX / wordsize < numwords) + rb_raise(rb_eArgError, "too big count * wordsize: %"PRI_SIZE_PREFIX"u * %"PRI_SIZE_PREFIX"u", numwords, wordsize); if (FIXNUM_P(val)) { long v = FIX2LONG(val); @@ -717,15 +732,15 @@ rb_integer_pack(VALUE val, int *signp, size_t *wordcount_allocated, void *words, if (words) { buf = words; - bufend = buf + wordcount * wordsize; + bufend = buf + numwords * wordsize; } else { /* * val_numbits = (de - dp) * SIZEOF_BDIGITS * CHAR_BIT - nlz(de[-1]) * word_numbits = wordsize * CHAR_BIT - nails - * wordcount = (val_numbits + word_numbits - 1) / word_numbits + * numwords = (val_numbits + word_numbits - 1) / word_numbits */ - VALUE val_numbits, word_numbits, wordcountv; + VALUE val_numbits, word_numbits, numwordsv; val_numbits = SIZET2NUM((de - dp) * SIZEOF_BDIGITS); val_numbits = rb_funcall(val_numbits, '*', 1, LONG2FIX(CHAR_BIT)); if (dp != de) @@ -734,12 +749,12 @@ rb_integer_pack(VALUE val, int *signp, size_t *wordcount_allocated, void *words, word_numbits = rb_funcall(word_numbits, '*', 1, LONG2FIX(CHAR_BIT)); if (nails != 0) word_numbits = rb_funcall(word_numbits, '-', 1, SIZET2NUM(nails)); - wordcountv = rb_funcall(val_numbits, '+', 1, word_numbits); - wordcountv = rb_funcall(wordcountv, '-', 1, LONG2FIX(1)); - wordcountv = rb_funcall(wordcountv, rb_intern("div"), 1, word_numbits); - wordcount = NUM2SIZET(wordcountv); - buf = xmalloc(wordcount * wordsize); - bufend = buf + wordcount * wordsize; + numwordsv = rb_funcall(val_numbits, '+', 1, word_numbits); + numwordsv = rb_funcall(numwordsv, '-', 1, LONG2FIX(1)); + numwordsv = rb_funcall(numwordsv, rb_intern("div"), 1, word_numbits); + numwords = NUM2SIZET(numwordsv); + buf = xmalloc(numwords * wordsize); + bufend = buf + numwords * wordsize; } if (buf == bufend) { @@ -763,7 +778,7 @@ rb_integer_pack(VALUE val, int *signp, size_t *wordcount_allocated, void *words, BDIGIT_DBL dd; int numbits_in_dd; - integer_format_loop_setup(wordcount, wordorder, wordsize, endian, nails, + integer_format_loop_setup(numwords, wordsize, nails, flags, &word_num_fullbytes, &word_num_partialbits, &word_num_nailbytes, &word_start, &word_step, &word_last, &byte_start, &byte_step); @@ -811,8 +826,8 @@ rb_integer_pack(VALUE val, int *signp, size_t *wordcount_allocated, void *words, if (signp) *signp = sign; - if (!words && wordcount_allocated) - *wordcount_allocated = wordcount; + if (!words && numwords_allocated) + *numwords_allocated = numwords; return buf; #undef FILL_DD @@ -837,16 +852,15 @@ int_import_push_bits(int data, int numbits, BDIGIT_DBL *ddp, int *numbits_in_dd_ * [sign] signedness of the value. * -1 for non-positive. 0 or 1 for non-negative. * [words] buffer to import. - * [wordcount] the size of given buffer as number of words. - * [wordorder] order of words: 1 for most significant word first. -1 for least significant word first. + * [numwords] the size of given buffer as number of words. * [wordsize] the size of word as number of bytes. - * [endian] order of bytes in a word: 1 for most significant byte first. -1 for least significant byte first. 0 for native endian. * [nails] number of padding bits in a word. Most significant nails bits of each word are filled by zero. + * [flags] bitwise or of constants which name starts "INTEGER_PACK_". It specifies word order and byte order. * * This function returns the imported integer as Fixnum or Bignum. */ VALUE -rb_integer_unpack(int sign, const void *words, size_t wordcount, int wordorder, size_t wordsize, int endian, size_t nails) +rb_integer_unpack(int sign, const void *words, size_t numwords, size_t wordsize, size_t nails, int flags) { VALUE num_bits, num_bdigits; VALUE result; @@ -868,9 +882,9 @@ rb_integer_unpack(int sign, const void *words, size_t wordcount, int wordorder, BDIGIT_DBL dd; int numbits_in_dd; - validate_integer_format(wordorder, wordsize, endian, nails); - if (SIZE_MAX / wordsize < wordcount) - rb_raise(rb_eArgError, "too big wordcount * wordsize: %"PRI_SIZE_PREFIX"u * %"PRI_SIZE_PREFIX"u", wordcount, wordsize); + validate_integer_format(wordsize, nails, flags); + if (SIZE_MAX / wordsize < numwords) + rb_raise(rb_eArgError, "too big numwords * wordsize: %"PRI_SIZE_PREFIX"u * %"PRI_SIZE_PREFIX"u", numwords, wordsize); if (sign != 1 && sign != 0 && sign != -1) rb_raise(rb_eArgError, "unexpected sign: %d", sign); @@ -878,7 +892,7 @@ rb_integer_unpack(int sign, const void *words, size_t wordcount, int wordorder, num_bits = SIZET2NUM(wordsize); num_bits = rb_funcall(num_bits, '*', 1, LONG2FIX(CHAR_BIT)); num_bits = rb_funcall(num_bits, '-', 1, SIZET2NUM(nails)); - num_bits = rb_funcall(num_bits, '*', 1, SIZET2NUM(wordcount)); + num_bits = rb_funcall(num_bits, '*', 1, SIZET2NUM(numwords)); if (num_bits == LONG2FIX(0)) return LONG2FIX(0); @@ -892,7 +906,7 @@ rb_integer_unpack(int sign, const void *words, size_t wordcount, int wordorder, dp = BDIGITS(result); de = dp + RBIGNUM_LEN(result); - integer_format_loop_setup(wordcount, wordorder, wordsize, endian, nails, + integer_format_loop_setup(numwords, wordsize, nails, flags, &word_num_fullbytes, &word_num_partialbits, NULL, &word_start, &word_step, &word_last, &byte_start, &byte_step); diff --git a/ext/-test-/bignum/pack.c b/ext/-test-/bignum/pack.c index 5307db6f4b..34c7a4728f 100644 --- a/ext/-test-/bignum/pack.c +++ b/ext/-test-/bignum/pack.c @@ -2,7 +2,7 @@ #include "internal.h" static VALUE -rb_integer_pack_m(VALUE val, VALUE buf, VALUE wordorder, VALUE wordsize_arg, VALUE endian, VALUE nails) +rb_integer_pack_m(VALUE val, VALUE buf, VALUE wordsize_arg, VALUE nails, VALUE flags) { int sign; size_t count = 0; @@ -17,24 +17,31 @@ rb_integer_pack_m(VALUE val, VALUE buf, VALUE wordorder, VALUE wordsize_arg, VAL ret = rb_integer_pack(val, &sign, &count, NIL_P(buf) ? NULL : RSTRING_PTR(buf), count, - NUM2INT(wordorder), wordsize, NUM2INT(endian), NUM2INT(nails)); + wordsize, NUM2SIZET(nails), NUM2INT(flags)); return rb_ary_new_from_args(3, INT2NUM(sign), ret ? rb_str_new(ret, wordsize * count) : Qnil, SIZET2NUM(count)); } static VALUE -rb_integer_unpack_m(VALUE klass, VALUE sign, VALUE buf, VALUE wordcount, VALUE wordorder, VALUE wordsize, VALUE endian, VALUE nails) +rb_integer_unpack_m(VALUE klass, VALUE sign, VALUE buf, VALUE wordcount, VALUE wordsize, VALUE nails, VALUE flags) { StringValue(buf); return rb_integer_unpack(NUM2INT(sign), RSTRING_PTR(buf), - NUM2SIZET(wordcount), NUM2INT(wordorder), NUM2SIZET(wordsize), - NUM2INT(endian), NUM2SIZET(nails)); + NUM2SIZET(wordcount), NUM2SIZET(wordsize), + NUM2SIZET(nails), NUM2INT(flags)); } void Init_pack(VALUE klass) { - rb_define_method(rb_cInteger, "test_pack", rb_integer_pack_m, 5); - rb_define_singleton_method(rb_cInteger, "test_unpack", rb_integer_unpack_m, 7); + rb_define_method(rb_cInteger, "test_pack", rb_integer_pack_m, 4); + rb_define_singleton_method(rb_cInteger, "test_unpack", rb_integer_unpack_m, 6); + rb_define_const(rb_cInteger, "INTEGER_PACK_MSWORD_FIRST", INT2NUM(INTEGER_PACK_MSWORD_FIRST)); + rb_define_const(rb_cInteger, "INTEGER_PACK_LSWORD_FIRST", INT2NUM(INTEGER_PACK_LSWORD_FIRST)); + rb_define_const(rb_cInteger, "INTEGER_PACK_MSBYTE_FIRST", INT2NUM(INTEGER_PACK_MSBYTE_FIRST)); + rb_define_const(rb_cInteger, "INTEGER_PACK_LSBYTE_FIRST", INT2NUM(INTEGER_PACK_LSBYTE_FIRST)); + rb_define_const(rb_cInteger, "INTEGER_PACK_NATIVE_BYTE_ORDER", INT2NUM(INTEGER_PACK_NATIVE_BYTE_ORDER)); + rb_define_const(rb_cInteger, "INTEGER_PACK_LITTLE_ENDIAN", INT2NUM(INTEGER_PACK_LITTLE_ENDIAN)); + rb_define_const(rb_cInteger, "INTEGER_PACK_BIG_ENDIAN", INT2NUM(INTEGER_PACK_BIG_ENDIAN)); } diff --git a/internal.h b/internal.h index 31fdc90a5e..7c01d65308 100644 --- a/internal.h +++ b/internal.h @@ -53,6 +53,20 @@ extern "C" { #define MUL_OVERFLOW_LONG_P(a, b) MUL_OVERFLOW_SIGNED_INTEGER_P(a, b, LONG_MIN, LONG_MAX) #define MUL_OVERFLOW_INT_P(a, b) MUL_OVERFLOW_SIGNED_INTEGER_P(a, b, INT_MIN, INT_MAX) +/* "MS" in MSWORD and MSBYTE means "most significant" */ +/* "LS" in LSWORD and LSBYTE means "least significant" */ +#define INTEGER_PACK_MSWORD_FIRST 0x01 +#define INTEGER_PACK_LSWORD_FIRST 0x02 +#define INTEGER_PACK_MSBYTE_FIRST 0x10 +#define INTEGER_PACK_LSBYTE_FIRST 0x20 +#define INTEGER_PACK_NATIVE_BYTE_ORDER 0x40 +#define INTEGER_PACK_LITTLE_ENDIAN \ + (INTEGER_PACK_LSWORD_FIRST | \ + INTEGER_PACK_LSBYTE_FIRST) +#define INTEGER_PACK_BIG_ENDIAN \ + (INTEGER_PACK_MSWORD_FIRST | \ + INTEGER_PACK_MSBYTE_FIRST) + struct rb_deprecated_classext_struct { char conflict[sizeof(VALUE) * 3]; }; @@ -428,8 +442,9 @@ const char *rb_objspace_data_type_name(VALUE obj); VALUE rb_thread_io_blocking_region(rb_blocking_function_t *func, void *data1, int fd); /* bignum.c */ -void *rb_integer_pack(VALUE val, int *signp, size_t *wordcount_allocated, void *words, size_t wordcount, int wordorder, size_t wordsize, int endian, size_t nails); -VALUE rb_integer_unpack(int sign, const void *words, size_t wordcount, int wordorder, size_t wordsize, int endian, size_t nails); + +void *rb_integer_pack(VALUE val, int *signp, size_t *numwords_allocated, void *words, size_t numwords, size_t wordsize, size_t nails, int flags); +VALUE rb_integer_unpack(int sign, const void *words, size_t numwords, size_t wordsize, size_t nails, int flags); /* io.c */ void rb_maygvl_fd_fix_cloexec(int fd); diff --git a/pack.c b/pack.c index 0f6812f07b..8bf15ad468 100644 --- a/pack.c +++ b/pack.c @@ -1023,7 +1023,7 @@ pack_pack(VALUE ary, VALUE fmt) numbytes = 1; buf = rb_str_new(NULL, numbytes); - rb_integer_pack(from, &sign, NULL, RSTRING_PTR(buf), RSTRING_LEN(buf), 1, 1, 1, 1); + rb_integer_pack(from, &sign, NULL, RSTRING_PTR(buf), RSTRING_LEN(buf), 1, 1, INTEGER_PACK_BIG_ENDIAN); if (sign < 0) rb_raise(rb_eArgError, "can't compress negative numbers"); @@ -2142,7 +2142,7 @@ pack_unpack(VALUE str, VALUE fmt) } else { s++; - UNPACK_PUSH(rb_integer_unpack(1, s0, s-s0, 1, 1, 1, 1)); + UNPACK_PUSH(rb_integer_unpack(1, s0, s-s0, 1, 1, INTEGER_PACK_BIG_ENDIAN)); len--; s0 = s; } diff --git a/test/-ext-/bignum/test_pack.rb b/test/-ext-/bignum/test_pack.rb index 3dae027539..17d364c2c9 100644 --- a/test/-ext-/bignum/test_pack.rb +++ b/test/-ext-/bignum/test_pack.rb @@ -5,107 +5,116 @@ require "-test-/bignum" class TestBignum < Test::Unit::TestCase class TestPack < Test::Unit::TestCase + + MSWORD_FIRST = Integer::INTEGER_PACK_MSWORD_FIRST + LSWORD_FIRST = Integer::INTEGER_PACK_LSWORD_FIRST + MSBYTE_FIRST = Integer::INTEGER_PACK_MSBYTE_FIRST + LSBYTE_FIRST = Integer::INTEGER_PACK_LSBYTE_FIRST + NATIVE_BYTE_ORDER = Integer::INTEGER_PACK_NATIVE_BYTE_ORDER + LITTLE_ENDIAN = Integer::INTEGER_PACK_LITTLE_ENDIAN + BIG_ENDIAN = Integer::INTEGER_PACK_BIG_ENDIAN + def test_pack_zero - assert_equal([0, "", 0], 0.test_pack(nil, 1, 1, 1, 0)) + assert_equal([0, "", 0], 0.test_pack(nil, 1, 0, BIG_ENDIAN)) end - def test_argument_check - assert_raise(ArgumentError) { 0.test_pack(nil, 0, 1, 1, 0) } - assert_raise(ArgumentError) { 0.test_pack(nil, 1, 1, 2, 0) } - assert_raise(ArgumentError) { 0.test_pack(nil, 1, 0, 1, 0) } - assert_raise(ArgumentError) { 0.test_pack(nil, 1, 1, 1, 8) } + def test_pack_argument_check + assert_raise(ArgumentError) { 0.test_pack(nil, 1, 0, MSBYTE_FIRST) } + assert_raise(ArgumentError) { 0.test_pack(nil, 1, 0, MSWORD_FIRST) } + assert_raise(ArgumentError) { 0.test_pack(nil, 0, 0, BIG_ENDIAN) } + assert_raise(ArgumentError) { 0.test_pack(nil, 1, 8, BIG_ENDIAN) } # assume sizeof(ssize_t) == sizeof(intptr_t) - assert_raise(ArgumentError) { 0.test_pack(nil, 1, 1 << ([""].pack("p").length * 8 - 1), 1, 0) } + assert_raise(ArgumentError) { 0.test_pack(nil, 1 << ([""].pack("p").length * 8 - 1), 0, BIG_ENDIAN) } end def test_pack_wordsize - assert_equal([1, "\x01", 1], 1.test_pack(nil, 1, 1, 1, 0)) - assert_equal([1, "\x00\x01", 1], 1.test_pack(nil, 1, 2, 1, 0)) - assert_equal([1, "\x00\x00\x01", 1], 1.test_pack(nil, 1, 3, 1, 0)) - assert_equal([1, "\x01", 1], 1.test_pack(nil, 1, 1, -1, 0)) - assert_equal([1, "\x01\x00", 1], 1.test_pack(nil, 1, 2, -1, 0)) - assert_equal([1, "\x01\x00\x00", 1], 1.test_pack(nil, 1, 3, -1, 0)) + assert_equal([1, "\x01", 1], 1.test_pack(nil, 1, 0, BIG_ENDIAN)) + assert_equal([1, "\x00\x01", 1], 1.test_pack(nil, 2, 0, BIG_ENDIAN)) + assert_equal([1, "\x00\x00\x01", 1], 1.test_pack(nil, 3, 0, BIG_ENDIAN)) + assert_equal([1, "\x01", 1], 1.test_pack(nil, 1, 0, LITTLE_ENDIAN)) + assert_equal([1, "\x01\x00", 1], 1.test_pack(nil, 2, 0, LITTLE_ENDIAN)) + assert_equal([1, "\x01\x00\x00", 1], 1.test_pack(nil, 3, 0, LITTLE_ENDIAN)) end def test_pack_fixed_buffer - assert_equal([0, "\x00\x00", 2], 0.test_pack("xx", 1, 1, 1, 0)) - assert_equal([1, "\x00\x01", 2], 0x01.test_pack("xx", 1, 1, 1, 0)) - assert_equal([1, "\x02\x01", 2], 0x0201.test_pack("xx", 1, 1, 1, 0)) - assert_equal([2, "\x02\x01", 2], 0x030201.test_pack("xx", 1, 1, 1, 0)) - assert_equal([2, "\x02\x01", 2], 0x04030201.test_pack("xx", 1, 1, 1, 0)) - assert_equal([0, "\x00\x00", 2], 0.test_pack("xx", -1, 1, 1, 0)) - assert_equal([1, "\x01\x00", 2], 0x01.test_pack("xx", -1, 1, 1, 0)) - assert_equal([1, "\x01\x02", 2], 0x0201.test_pack("xx", -1, 1, 1, 0)) - assert_equal([2, "\x01\x02", 2], 0x030201.test_pack("xx", -1, 1, 1, 0)) - assert_equal([2, "\x01\x02", 2], 0x04030201.test_pack("xx", -1, 1, 1, 0)) + 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)) end def test_pack_wordorder_and_endian - assert_equal([1, "\x12\x34\x56\x78", 2], 0x12345678.test_pack(nil, 1, 2, 1, 0)) - assert_equal([1, "\x34\x12\x78\x56", 2], 0x12345678.test_pack(nil, 1, 2, -1, 0)) - assert_equal([1, "\x56\x78\x12\x34", 2], 0x12345678.test_pack(nil, -1, 2, 1, 0)) - assert_equal([1, "\x78\x56\x34\x12", 2], 0x12345678.test_pack(nil, -1, 2, -1, 0)) + assert_equal([1, "\x12\x34\x56\x78", 2], 0x12345678.test_pack(nil, 2, 0, MSWORD_FIRST|MSBYTE_FIRST)) + assert_equal([1, "\x34\x12\x78\x56", 2], 0x12345678.test_pack(nil, 2, 0, MSWORD_FIRST|LSBYTE_FIRST)) + assert_equal([1, "\x56\x78\x12\x34", 2], 0x12345678.test_pack(nil, 2, 0, LSWORD_FIRST|MSBYTE_FIRST)) + assert_equal([1, "\x78\x56\x34\x12", 2], 0x12345678.test_pack(nil, 2, 0, LSWORD_FIRST|LSBYTE_FIRST)) end def test_pack_native_endian - assert_equal([1, [0x1234].pack("S!"), 1], 0x1234.test_pack(nil, 1, 2, 0, 0)) + assert_equal([1, [0x1234].pack("S!"), 1], 0x1234.test_pack(nil, 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(nil, 1, 1, 1, 7)) - assert_equal([1, "\x01\x02\x03\x04\x05\x06\x07\x08", 8], 0x12345678.test_pack(nil, 1, 1, 1, 4)) - assert_equal([1, "\x00\x12\x00\x34\x00\x56\x00\x78", 4], 0x12345678.test_pack(nil, 1, 2, 1, 8)) + assert_equal([1, "\x01\x00\x00\x00\x01\x01", 6], 0b100011.test_pack(nil, 1, 7, BIG_ENDIAN)) + assert_equal([1, "\x01\x02\x03\x04\x05\x06\x07\x08", 8], 0x12345678.test_pack(nil, 1, 4, BIG_ENDIAN)) + assert_equal([1, "\x00\x12\x00\x34\x00\x56\x00\x78", 4], 0x12345678.test_pack(nil, 2, 8, BIG_ENDIAN)) end def test_pack_sign - assert_equal([-1, "\x01", 1], (-1).test_pack(nil, 1, 1, 1, 0)) - assert_equal([-1, "\x80\x70\x60\x50\x40\x30\x20\x10", 8], (-0x8070605040302010).test_pack(nil, 1, 1, 1, 0)) + assert_equal([-1, "\x01", 1], (-1).test_pack(nil, 1, 0, BIG_ENDIAN)) + assert_equal([-1, "\x80\x70\x60\x50\x40\x30\x20\x10", 8], (-0x8070605040302010).test_pack(nil, 1, 0, BIG_ENDIAN)) end def test_unpack_zero - assert_equal(0, Integer.test_unpack(0, "", 1, 1, 1, 1, 0)) + assert_equal(0, Integer.test_unpack(0, "", 0, 1, 0, BIG_ENDIAN)) end - def test_argument_check - assert_raise(ArgumentError) { Integer.test_unpack(1, "x", 1, 0, 1, 1, 0) } - assert_raise(ArgumentError) { Integer.test_unpack(1, "x", 1, 1, 1, 2, 0) } - assert_raise(ArgumentError) { Integer.test_unpack(1, "x", 1, 1, 0, 1, 0) } - assert_raise(ArgumentError) { Integer.test_unpack(1, "x", 1, 1, 1, 1, 8) } + def test_unpack_argument_check + assert_raise(ArgumentError) { Integer.test_unpack(1, "x", 1, 1, 0, MSBYTE_FIRST) } + assert_raise(ArgumentError) { Integer.test_unpack(1, "x", 1, 1, 0, MSWORD_FIRST) } + assert_raise(ArgumentError) { Integer.test_unpack(1, "x", 1, 0, 0, BIG_ENDIAN) } + assert_raise(ArgumentError) { Integer.test_unpack(1, "x", 1, 1, 8, BIG_ENDIAN) } # assume sizeof(ssize_t) == sizeof(intptr_t) - assert_raise(ArgumentError) { Integer.test_unpack(1, "x", 1, 1, 1 << ([""].pack("p").length * 8 - 1), 1, 0) } + assert_raise(ArgumentError) { Integer.test_unpack(1, "x", 1, 1 << ([""].pack("p").length * 8 - 1), 0, BIG_ENDIAN) } end def test_unpack_wordsize - assert_equal(1, Integer.test_unpack(1, "\x01", 1, 1, 1, 1, 0)) - assert_equal(1, Integer.test_unpack(1, "\x00\x01", 1, 1, 2, 1, 0)) - assert_equal(1, Integer.test_unpack(1, "\x00\x00\x01", 1, 1, 3, 1, 0)) - assert_equal(1, Integer.test_unpack(1, "\x01", 1, 1, 1, -1, 0)) - assert_equal(1, Integer.test_unpack(1, "\x01\x00", 1, 1, 2, -1, 0)) - assert_equal(1, Integer.test_unpack(1, "\x01\x00\x00", 1, 1, 3, -1, 0)) + assert_equal(1, Integer.test_unpack(1, "\x01", 1, 1, 0, BIG_ENDIAN)) + assert_equal(1, Integer.test_unpack(1, "\x00\x01", 1, 2, 0, BIG_ENDIAN)) + assert_equal(1, Integer.test_unpack(1, "\x00\x00\x01", 1, 3, 0, BIG_ENDIAN)) + assert_equal(1, Integer.test_unpack(1, "\x01", 1, 1, 0, LITTLE_ENDIAN)) + assert_equal(1, Integer.test_unpack(1, "\x01\x00", 1, 2, 0, LITTLE_ENDIAN)) + assert_equal(1, Integer.test_unpack(1, "\x01\x00\x00", 1, 3, 0, LITTLE_ENDIAN)) end def test_unpack_wordorder_and_endian - assert_equal(0x01020304, Integer.test_unpack(1, "\x01\x02\x03\x04", 2, 1, 2, 1, 0)) - assert_equal(0x02010403, Integer.test_unpack(1, "\x01\x02\x03\x04", 2, 1, 2, -1, 0)) - assert_equal(0x03040102, Integer.test_unpack(1, "\x01\x02\x03\x04", 2, -1, 2, 1, 0)) - assert_equal(0x04030201, Integer.test_unpack(1, "\x01\x02\x03\x04", 2, -1, 2, -1, 0)) + assert_equal(0x01020304, Integer.test_unpack(1, "\x01\x02\x03\x04", 2, 2, 0, MSWORD_FIRST|MSBYTE_FIRST)) + assert_equal(0x02010403, Integer.test_unpack(1, "\x01\x02\x03\x04", 2, 2, 0, MSWORD_FIRST|LSBYTE_FIRST)) + assert_equal(0x03040102, Integer.test_unpack(1, "\x01\x02\x03\x04", 2, 2, 0, LSWORD_FIRST|MSBYTE_FIRST)) + assert_equal(0x04030201, Integer.test_unpack(1, "\x01\x02\x03\x04", 2, 2, 0, LSWORD_FIRST|LSBYTE_FIRST)) end def test_unpack_native_endian - assert_equal("\x12\x34".unpack("S!")[0], Integer.test_unpack(1, "\x12\x34", 1, 1, 2, 0, 0)) + assert_equal("\x12\x34".unpack("S!")[0], Integer.test_unpack(1, "\x12\x34", 1, 2, 0, MSWORD_FIRST|NATIVE_BYTE_ORDER)) end def test_unpack_nail - assert_equal(0b100011, Integer.test_unpack(1, "\x01\x00\x00\x00\x01\x01", 6, 1, 1, 1, 7)) - assert_equal(0x12345678, Integer.test_unpack(1, "\x01\x02\x03\x04\x05\x06\x07\x08", 8, 1, 1, 1, 4)) - assert_equal(0x12345678, Integer.test_unpack(1, "\x00\x12\x00\x34\x00\x56\x00\x78", 4, 1, 2, 1, 8)) + assert_equal(0b100011, Integer.test_unpack(1, "\x01\x00\x00\x00\x01\x01", 6, 1, 7, BIG_ENDIAN)) + assert_equal(0x12345678, Integer.test_unpack(1, "\x01\x02\x03\x04\x05\x06\x07\x08", 8, 1, 4, BIG_ENDIAN)) + assert_equal(0x12345678, Integer.test_unpack(1, "\x00\x12\x00\x34\x00\x56\x00\x78", 4, 2, 8, BIG_ENDIAN)) end def test_unpack_sign - assert_equal(-1, Integer.test_unpack(-1, "\x01", 1, 1, 1, 1, 0)) - assert_equal(-0x8070605040302010, Integer.test_unpack(-1, "\x80\x70\x60\x50\x40\x30\x20\x10", 8, 1, 1, 1, 0)) + assert_equal(-1, Integer.test_unpack(-1, "\x01", 1, 1, 0, BIG_ENDIAN)) + assert_equal(-0x8070605040302010, Integer.test_unpack(-1, "\x80\x70\x60\x50\x40\x30\x20\x10", 8, 1, 0, BIG_ENDIAN)) end end -- cgit v1.2.3