[279] | 1 | # pack & unpack 'm' (base64)
|
---|
| 2 | assert('[""].pack("m")') do
|
---|
| 3 | ary = ""
|
---|
| 4 | str = ""
|
---|
| 5 | [ary].pack("m") == str and
|
---|
| 6 | str.unpack("m") == [ary]
|
---|
| 7 | end
|
---|
| 8 |
|
---|
| 9 | assert('["\0"].pack("m")') do
|
---|
| 10 | ary = "\0"
|
---|
| 11 | str = "AA==\n"
|
---|
| 12 | [ary].pack("m") == str and
|
---|
| 13 | str.unpack("m") == [ary]
|
---|
| 14 | end
|
---|
| 15 |
|
---|
| 16 | assert('["\0\0"].pack("m")') do
|
---|
| 17 | ary = "\0\0"
|
---|
| 18 | str = "AAA=\n"
|
---|
| 19 | [ary].pack("m") == str and
|
---|
| 20 | str.unpack("m") == [ary]
|
---|
| 21 | end
|
---|
| 22 |
|
---|
| 23 | assert('["\0\0\0"].pack("m")') do
|
---|
| 24 | ary = "\0\0\0"
|
---|
| 25 | str = "AAAA\n"
|
---|
| 26 | [ary].pack("m") == str and
|
---|
| 27 | str.unpack("m") == [ary]
|
---|
| 28 | end
|
---|
| 29 |
|
---|
| 30 | assert('["abc..xyzABC..XYZ"].pack("m")') do
|
---|
| 31 | ["abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"].pack("m") == "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNERUZHSElKS0xNTk9QUVJT\nVFVWV1hZWg==\n"
|
---|
| 32 | end
|
---|
| 33 |
|
---|
| 34 | assert('"YWJ...".unpack("m") should "abc..xyzABC..XYZ"') do
|
---|
| 35 | str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
---|
| 36 | "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNERUZHSElKS0xNTk9QUVJT\nVFVWV1hZWg==\n".unpack("m") == [str] and
|
---|
| 37 | "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWg==\n".unpack("m") == [str]
|
---|
| 38 | end
|
---|
| 39 |
|
---|
| 40 | # pack & unpack 'H'
|
---|
| 41 | assert('["3031"].pack("H*")') do
|
---|
| 42 | ary = "3031"
|
---|
| 43 | str = "01"
|
---|
| 44 | [ary].pack("H*") == str and
|
---|
| 45 | str.unpack("H*") == [ary]
|
---|
| 46 | end
|
---|
| 47 |
|
---|
| 48 | assert('["10"].pack("H*")') do
|
---|
| 49 | ary = "10"
|
---|
| 50 | str = "\020"
|
---|
| 51 | [ary].pack("H*") == str and
|
---|
| 52 | str.unpack("H*") == [ary]
|
---|
| 53 | end
|
---|
| 54 |
|
---|
| 55 | assert('[0,1,127,128,255].pack("C*")') do
|
---|
| 56 | ary = [ 0, 1, 127, 128, 255 ]
|
---|
| 57 | str = "\x00\x01\x7F\x80\xFF"
|
---|
| 58 | ary.pack("C*") == str and str.unpack("C*") == ary
|
---|
| 59 | end
|
---|
| 60 |
|
---|
| 61 | # pack "a"
|
---|
| 62 | assert('["abc"].pack("a")') do
|
---|
| 63 | ["abc"].pack("a") == "a" and
|
---|
| 64 | ["abc"].pack("a*") == "abc" and
|
---|
| 65 | ["abc"].pack("a4") == "abc\0"
|
---|
| 66 | end
|
---|
| 67 |
|
---|
| 68 | # upack "a"
|
---|
| 69 | assert('["abc"].pack("a")') do
|
---|
| 70 | "abc\0".unpack("a4") == ["abc\0"] and
|
---|
| 71 | "abc ".unpack("a4") == ["abc "]
|
---|
| 72 | end
|
---|
| 73 |
|
---|
| 74 | # pack "A"
|
---|
| 75 | assert('["abc"].pack("A")') do
|
---|
| 76 | ["abc"].pack("A") == "a" and
|
---|
| 77 | ["abc"].pack("A*") == "abc" and
|
---|
| 78 | ["abc"].pack("A4") == "abc "
|
---|
| 79 | end
|
---|
| 80 |
|
---|
| 81 | # upack "A"
|
---|
| 82 | assert('["abc"].pack("A")') do
|
---|
| 83 | "abc\0".unpack("A4") == ["abc"] and
|
---|
| 84 | "abc ".unpack("A4") == ["abc"]
|
---|
| 85 | end
|
---|
| 86 |
|
---|
| 87 | # regression tests
|
---|
| 88 | assert('issue #1') do
|
---|
| 89 | [1, 2].pack("nn") == "\000\001\000\002"
|
---|
| 90 | end
|
---|
| 91 |
|
---|
| 92 | def assert_pack tmpl, packed, unpacked
|
---|
| 93 | assert_equal packed, unpacked.pack(tmpl)
|
---|
| 94 | assert_equal unpacked, packed.unpack(tmpl)
|
---|
| 95 | end
|
---|
| 96 |
|
---|
| 97 | PACK_IS_LITTLE_ENDIAN = "\x01\00".unpack('S')[0] == 0x01
|
---|
| 98 |
|
---|
| 99 | assert 'pack float' do
|
---|
| 100 | assert_pack 'e', "\x00\x00@@", [3.0]
|
---|
| 101 | assert_pack 'g', "@@\x00\x00", [3.0]
|
---|
| 102 |
|
---|
| 103 | if PACK_IS_LITTLE_ENDIAN
|
---|
| 104 | assert_pack 'f', "\x00\x00@@", [3.0]
|
---|
| 105 | assert_pack 'F', "\x00\x00@@", [3.0]
|
---|
| 106 | else
|
---|
| 107 | assert_pack 'f', "@@\x00\x00", [3.0]
|
---|
| 108 | assert_pack 'F', "@@\x00\x00", [3.0]
|
---|
| 109 | end
|
---|
| 110 | end
|
---|
| 111 |
|
---|
| 112 | assert 'pack double' do
|
---|
| 113 | assert_pack 'E', "\x00\x00\x00\x00\x00\x00\b@", [3.0]
|
---|
| 114 | assert_pack 'G', "@\b\x00\x00\x00\x00\x00\x00", [3.0]
|
---|
| 115 |
|
---|
| 116 | if PACK_IS_LITTLE_ENDIAN
|
---|
| 117 | assert_pack 'd', "\x00\x00\x00\x00\x00\x00\b@", [3.0]
|
---|
| 118 | assert_pack 'D', "\x00\x00\x00\x00\x00\x00\b@", [3.0]
|
---|
| 119 | else
|
---|
| 120 | assert_pack 'd', "@\b\x00\x00\x00\x00\x00\x00", [3.0]
|
---|
| 121 | assert_pack 'D', "@\b\x00\x00\x00\x00\x00\x00", [3.0]
|
---|
| 122 | end
|
---|
| 123 | end
|
---|
| 124 |
|
---|
| 125 | assert 'pack/unpack "i"' do
|
---|
| 126 | int_size = [0].pack('i').size
|
---|
| 127 | raise "pack('i').size is too small (#{int_size})" if int_size < 2
|
---|
| 128 |
|
---|
| 129 | if PACK_IS_LITTLE_ENDIAN
|
---|
| 130 | str = "\xC7\xCF" + "\xFF" * (int_size-2)
|
---|
| 131 | else
|
---|
| 132 | str = "\xFF" * (int_size-2) + "\xC7\xCF"
|
---|
| 133 | end
|
---|
| 134 | assert_pack 'i', str, [-12345]
|
---|
| 135 | end
|
---|
| 136 |
|
---|
| 137 | assert 'pack/unpack "I"' do
|
---|
| 138 | uint_size = [0].pack('I').size
|
---|
| 139 | raise "pack('I').size is too small (#{uint_size})" if uint_size < 2
|
---|
| 140 |
|
---|
| 141 | if PACK_IS_LITTLE_ENDIAN
|
---|
| 142 | str = "\x39\x30" + "\0" * (uint_size-2)
|
---|
| 143 | else
|
---|
| 144 | str = "\0" * (uint_size-2) + "\x39\x30"
|
---|
| 145 | end
|
---|
| 146 | assert_pack 'I', str, [12345]
|
---|
| 147 | end
|
---|