Imported Upstream version 1.46.0
[platform/upstream/nghttp2.git] / third-party / mruby / mrbgems / mruby-pack / test / pack.rb
index 1788c2b..6832adc 100644 (file)
+PACK_IS_LITTLE_ENDIAN = "\x01\00".unpack('S')[0] == 0x01
+
+def assert_pack tmpl, packed, unpacked
+  t = tmpl.inspect
+  assert "assert_pack" do
+    assert_equal packed, unpacked.pack(tmpl), "#{unpacked.inspect}.pack(#{t})"
+    assert_equal unpacked, packed.unpack(tmpl), "#{packed.inspect}.unpack(#{t})"
+  end
+end
+
 # pack & unpack 'm' (base64)
 assert('[""].pack("m")') do
-  ary = ""
-  str = ""
-  [ary].pack("m") == str and
-  str.unpack("m") == [ary]
+  assert_pack "m", "", [""]
 end
 
 assert('["\0"].pack("m")') do
-  ary = "\0"
-  str = "AA==\n"
-  [ary].pack("m") == str and
-  str.unpack("m") == [ary]
+  assert_pack "m", "AA==\n", ["\0"]
 end
 
 assert('["\0\0"].pack("m")') do
-  ary = "\0\0"
-  str = "AAA=\n"
-  [ary].pack("m") == str and
-  str.unpack("m") == [ary]
+  assert_pack "m", "AAA=\n", ["\0\0"]
 end
 
 assert('["\0\0\0"].pack("m")') do
-  ary = "\0\0\0"
-  str = "AAAA\n"
-  [ary].pack("m") == str and
-  str.unpack("m") == [ary]
+  assert_pack "m", "AAAA\n", ["\0\0\0"]
 end
 
 assert('["abc..xyzABC..XYZ"].pack("m")') do
-  ["abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"].pack("m") == "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNERUZHSElKS0xNTk9QUVJT\nVFVWV1hZWg==\n"
+  assert_pack "m", "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNERUZHSElKS0xNTk9QUVJT\nVFVWV1hZWg==\n", ["abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"]
 end
 
 assert('"YWJ...".unpack("m") should "abc..xyzABC..XYZ"') do
-  str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
-  "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNERUZHSElKS0xNTk9QUVJT\nVFVWV1hZWg==\n".unpack("m") == [str] and
-  "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWg==\n".unpack("m") == [str]
+  ary = ["abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"]
+  assert_equal ary, "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNERUZHSElKS0xNTk9QUVJT\nVFVWV1hZWg==\n".unpack("m")
+  assert_equal ary, "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWg==\n".unpack("m")
+end
+
+assert('["A", "B"].pack') do
+  assert_equal "QQ==\n", ["A", "B"].pack("m50")
+  assert_equal ["A"], "QQ==\n".unpack("m50")
+  assert_equal "QQ==Qg==", ["A", "B"].pack("m0 m0")
+  assert_equal ["A", "B"], "QQ==Qg==".unpack("m10 m10")
+end
+
+assert('["abc..xyzABC..XYZ"].pack("m0")') do
+  assert_pack "m0", "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWg==", ["abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"]
 end
 
 # pack & unpack 'H'
 assert('["3031"].pack("H*")') do
-  ary = "3031"
-  str = "01"
-  [ary].pack("H*") == str and
-  str.unpack("H*") == [ary]
+  assert_pack "H*", "01", ["3031"]
 end
 
 assert('["10"].pack("H*")') do
-  ary = "10"
-  str = "\020"
-  [ary].pack("H*") == str and
-  str.unpack("H*") == [ary]
+  assert_pack "H*", "\020", ["10"]
 end
 
 assert('[0,1,127,128,255].pack("C*")') do
- ary = [ 0, 1, 127, 128, 255 ]
- str = "\x00\x01\x7F\x80\xFF"
- ary.pack("C*") == str and str.unpack("C*") == ary
+  assert_pack "C*", "\x00\x01\x7F\x80\xFF", [0, 1, 127, 128, 255]
 end
 
 # pack "a"
 assert('["abc"].pack("a")') do
-  ["abc"].pack("a") == "a" and
-  ["abc"].pack("a*") == "abc" and
-  ["abc"].pack("a4") == "abc\0"
+  assert_equal "a", ["abc"].pack("a")
+  assert_equal "abc", ["abc"].pack("a*")
+  assert_equal "abc\0", ["abc"].pack("a4")
 end
 
 # upack "a"
 assert('["abc"].pack("a")') do
-  "abc\0".unpack("a4") == ["abc\0"] and
-  "abc ".unpack("a4") == ["abc "]
+  assert_equal ["abc\0"], "abc\0".unpack("a4")
+  assert_equal ["abc "], "abc ".unpack("a4")
 end
 
 # pack "A"
 assert('["abc"].pack("A")') do
-  ["abc"].pack("A") == "a" and
-  ["abc"].pack("A*") == "abc" and
-  ["abc"].pack("A4") == "abc "
+  assert_equal "a", ["abc"].pack("A")
+  assert_equal "abc", ["abc"].pack("A*")
+  assert_equal "abc ", ["abc"].pack("A4")
 end
 
 # upack "A"
 assert('["abc"].pack("A")') do
-  "abc\0".unpack("A4") == ["abc"] and
-  "abc ".unpack("A4") == ["abc"]
+  assert_equal ["abc"], "abc\0".unpack("A4")
+  assert_equal ["abc"], "abc ".unpack("A4")
 end
 
 # regression tests
 assert('issue #1') do
-  [1, 2].pack("nn") == "\000\001\000\002"
+  assert_equal "\000\001\000\002", [1, 2].pack("nn")
 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]