summaryrefslogtreecommitdiff
path: root/test/-ext-/bignum
diff options
context:
space:
mode:
authorakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-06-06 21:20:04 +0000
committerakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-06-06 21:20:04 +0000
commitc9c67d2fe0d8d502d444c477d3272c4092513a53 (patch)
tree81a21632cffe836b8e768ed733eedcbfd999426a /test/-ext-/bignum
parent64ed7df5434bc2978d5a92a1e750bab39b11edfa (diff)
* bignum.c (rb_int_import): New function.
(int_import_push_bits): Ditto. * internal.h (rb_int_import): Declared. * pack.c (pack_unpack): Use rb_int_import for BER compressed integer. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@41111 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'test/-ext-/bignum')
-rw-r--r--test/-ext-/bignum/test_import.rb54
1 files changed, 54 insertions, 0 deletions
diff --git a/test/-ext-/bignum/test_import.rb b/test/-ext-/bignum/test_import.rb
new file mode 100644
index 00000000000..f9b8b28de5c
--- /dev/null
+++ b/test/-ext-/bignum/test_import.rb
@@ -0,0 +1,54 @@
+# coding: ASCII-8BIT
+
+require 'test/unit'
+require "-test-/bignum"
+
+class TestBignum < Test::Unit::TestCase
+ class TestExport < Test::Unit::TestCase
+ def test_import_zero
+ assert_equal(0, Integer.test_import(0, "", 1, 1, 1, 1, 0))
+ end
+
+ def test_argument_check
+ assert_raise(ArgumentError) { Integer.test_import(1, "x", 1, 0, 1, 1, 0) }
+ assert_raise(ArgumentError) { Integer.test_import(1, "x", 1, 1, 1, 2, 0) }
+ assert_raise(ArgumentError) { Integer.test_import(1, "x", 1, 1, 0, 1, 0) }
+ assert_raise(ArgumentError) { Integer.test_import(1, "x", 1, 1, 1, 1, 8) }
+
+ # assume sizeof(ssize_t) == sizeof(intptr_t)
+ assert_raise(ArgumentError) { Integer.test_import(1, "x", 1, 1, 1 << ([""].pack("p").length * 8 - 1), 1, 0) }
+ end
+
+ def test_import_wordsize
+ assert_equal(1, Integer.test_import(1, "\x01", 1, 1, 1, 1, 0))
+ assert_equal(1, Integer.test_import(1, "\x00\x01", 1, 1, 2, 1, 0))
+ assert_equal(1, Integer.test_import(1, "\x00\x00\x01", 1, 1, 3, 1, 0))
+ assert_equal(1, Integer.test_import(1, "\x01", 1, 1, 1, -1, 0))
+ assert_equal(1, Integer.test_import(1, "\x01\x00", 1, 1, 2, -1, 0))
+ assert_equal(1, Integer.test_import(1, "\x01\x00\x00", 1, 1, 3, -1, 0))
+ end
+
+ def test_import_wordorder_and_endian
+ assert_equal(0x01020304, Integer.test_import(1, "\x01\x02\x03\x04", 2, 1, 2, 1, 0))
+ assert_equal(0x02010403, Integer.test_import(1, "\x01\x02\x03\x04", 2, 1, 2, -1, 0))
+ assert_equal(0x03040102, Integer.test_import(1, "\x01\x02\x03\x04", 2, -1, 2, 1, 0))
+ assert_equal(0x04030201, Integer.test_import(1, "\x01\x02\x03\x04", 2, -1, 2, -1, 0))
+ end
+
+ def test_import_native_endian
+ assert_equal("\x12\x34".unpack("S!")[0], Integer.test_import(1, "\x12\x34", 1, 1, 2, 0, 0))
+ end
+
+ def test_import_nail
+ assert_equal(0b100011, Integer.test_import(1, "\x01\x00\x00\x00\x01\x01", 6, 1, 1, 1, 7))
+ assert_equal(0x12345678, Integer.test_import(1, "\x01\x02\x03\x04\x05\x06\x07\x08", 8, 1, 1, 1, 4))
+ assert_equal(0x12345678, Integer.test_import(1, "\x00\x12\x00\x34\x00\x56\x00\x78", 4, 1, 2, 1, 8))
+ end
+
+ def test_import_sign
+ assert_equal(-1, Integer.test_import(-1, "\x01", 1, 1, 1, 1, 0))
+ assert_equal(-0x8070605040302010, Integer.test_import(-1, "\x80\x70\x60\x50\x40\x30\x20\x10", 8, 1, 1, 1, 0))
+ end
+
+ end
+end