summaryrefslogtreecommitdiffhomepage
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/pack.rb33
1 files changed, 33 insertions, 0 deletions
diff --git a/test/pack.rb b/test/pack.rb
index 7789fb446..c7899e2d7 100644
--- a/test/pack.rb
+++ b/test/pack.rb
@@ -88,3 +88,36 @@ end
assert('issue #1') do
[1, 2].pack("nn") == "\000\001\000\002"
end
+
+def assert_pack tmpl, packed, unpacked
+ assert_equal packed, unpacked.pack(tmpl)
+ assert_equal unpacked, packed.unpack(tmpl)
+end
+
+PACK_IS_LITTLE_ENDIAN = "\x01\00".unpack('S')[0] == 0x01
+
+assert 'pack float' do
+ assert_pack 'e', "\x00\x00@@", [3.0]
+ assert_pack 'g', "@@\x00\x00", [3.0]
+
+ if PACK_IS_LITTLE_ENDIAN
+ assert_pack 'f', "\x00\x00@@", [3.0]
+ assert_pack 'F', "\x00\x00@@", [3.0]
+ else
+ assert_pack 'f', "@@\x00\x00", [3.0]
+ assert_pack 'F', "@@\x00\x00", [3.0]
+ end
+end
+
+assert 'pack double' do
+ assert_pack 'E', "\x00\x00\x00\x00\x00\x00\b@", [3.0]
+ assert_pack 'G', "@\b\x00\x00\x00\x00\x00\x00", [3.0]
+
+ if PACK_IS_LITTLE_ENDIAN
+ assert_pack 'd', "\x00\x00\x00\x00\x00\x00\b@", [3.0]
+ assert_pack 'D', "\x00\x00\x00\x00\x00\x00\b@", [3.0]
+ else
+ assert_pack 'd', "@\b\x00\x00\x00\x00\x00\x00", [3.0]
+ assert_pack 'D', "@\b\x00\x00\x00\x00\x00\x00", [3.0]
+ end
+end