1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
|
# pack & unpack 'm' (base64)
assert('[""].pack("m")') do
ary = ""
str = ""
[ary].pack("m") == str and
str.unpack("m") == [ary]
end
assert('["\0"].pack("m")') do
ary = "\0"
str = "AA==\n"
[ary].pack("m") == str and
str.unpack("m") == [ary]
end
assert('["\0\0"].pack("m")') do
ary = "\0\0"
str = "AAA=\n"
[ary].pack("m") == str and
str.unpack("m") == [ary]
end
assert('["\0\0\0"].pack("m")') do
ary = "\0\0\0"
str = "AAAA\n"
[ary].pack("m") == str and
str.unpack("m") == [ary]
end
assert('["abc..xyzABC..XYZ"].pack("m")') do
["abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"].pack("m") == "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNERUZHSElKS0xNTk9QUVJT\nVFVWV1hZWg==\n"
end
assert('"YWJ...".unpack("m") should "abc..xyzABC..XYZ"') do
str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
"YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNERUZHSElKS0xNTk9QUVJT\nVFVWV1hZWg==\n".unpack("m") == [str] and
"YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWg==\n".unpack("m") == [str]
end
# pack & unpack 'H'
assert('["3031"].pack("H*")') do
ary = "3031"
str = "01"
[ary].pack("H*") == str and
str.unpack("H*") == [ary]
end
assert('["10"].pack("H*")') do
ary = "10"
str = "\020"
[ary].pack("H*") == str and
str.unpack("H*") == [ary]
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
end
# pack "a"
assert('["abc"].pack("a")') do
["abc"].pack("a") == "a" and
["abc"].pack("a*") == "abc" and
["abc"].pack("a4") == "abc\0"
end
# upack "a"
assert('["abc"].pack("a")') do
"abc\0".unpack("a4") == ["abc\0"] and
"abc ".unpack("a4") == ["abc "]
end
# pack "A"
assert('["abc"].pack("A")') do
["abc"].pack("A") == "a" and
["abc"].pack("A*") == "abc" and
["abc"].pack("A4") == "abc "
end
# upack "A"
assert('["abc"].pack("A")') do
"abc\0".unpack("A4") == ["abc"] and
"abc ".unpack("A4") == ["abc"]
end
# regression tests
assert('issue #1') do
[1, 2].pack("nn") == "\000\001\000\002"
end
assert 'pack float' do
assert_equal "\x00\x00@@", [3.0].pack('e')
assert_equal "@@\x00\x00", [3.0].pack('g')
native = [3.0].pack 'f'
assert_true native == "\x00\x00@@" or native == "@@\x00\x00"
native = [3.0].pack 'F'
assert_true native == "\x00\x00@@" or native == "@@\x00\x00"
end
assert 'pack double' do
assert_equal "\x00\x00\x00\x00\x00\x00\b@", [3.0].pack('E')
assert_equal "@\b\x00\x00\x00\x00\x00\x00", [3.0].pack('G')
native = [3.0].pack 'd'
assert_true native == "\x00\x00\x00\x00\x00\x00\b@" or native == "@\b\x00\x00\x00\x00\x00\x00"
native = [3.0].pack 'D'
assert_true native == "\x00\x00\x00\x00\x00\x00\b@" or native == "@\b\x00\x00\x00\x00\x00\x00"
end
|