1 | # encoding: ascii
|
---|
2 |
|
---|
3 | # a = Array, s = String, t = Template
|
---|
4 |
|
---|
5 | def packtest(a, s, t)
|
---|
6 | begin
|
---|
7 | r = a.pack(t)
|
---|
8 | return if r == s
|
---|
9 | puts "#{a.inspect}.pack(#{t.inspect}) -> #{r.inspect} should be #{s.inspect}"
|
---|
10 | rescue => r
|
---|
11 | unless r.is_a? s
|
---|
12 | puts "#{a.inspect}.pack(#{t.inspect}) -> #{r.inspect} should be #{s.inspect}"
|
---|
13 | end
|
---|
14 | end
|
---|
15 | end
|
---|
16 |
|
---|
17 | def unpacktest(a, s, t)
|
---|
18 | r = s.unpack(t)
|
---|
19 | return if r == a
|
---|
20 | puts "#{s.inspect}.unpack(#{t.inspect}) -> #{r.inspect} should be #{a.inspect}"
|
---|
21 | end
|
---|
22 |
|
---|
23 | def pptest(a, s, t)
|
---|
24 | packtest(a, s, t)
|
---|
25 | unpacktest(a, s, t)
|
---|
26 | end
|
---|
27 |
|
---|
28 | pptest [1], "\x01", "C"
|
---|
29 |
|
---|
30 | packtest [1.1], "\x01", "C"
|
---|
31 | packtest [-1], "\xff", "C"
|
---|
32 | packtest [1,2], "\x01\x02", "C2"
|
---|
33 | #packtest [1], "X", ArgumentError
|
---|
34 |
|
---|
35 | unpacktest [48, nil], "0", "CC"
|
---|
36 | unpacktest [160, -96], "\xa0\xa0", "Cc"
|
---|
37 | unpacktest [49, 50, 51], "123", "C*"
|
---|
38 |
|
---|
39 | pptest [12849], "12", "S"
|
---|
40 | unpacktest [nil], "0", "S"
|
---|
41 | unpacktest [12849, nil], "123", "SS"
|
---|
42 | unpacktest [12849], "123", "S*"
|
---|
43 |
|
---|
44 | pptest [10000], "\x27\x10", "s>"
|
---|
45 | pptest [-10000], "\xd8\xf0", "s>"
|
---|
46 | pptest [50000], "\xc3\x50", "S>"
|
---|
47 |
|
---|
48 | pptest [10000], "\x10\x27", "s<"
|
---|
49 | pptest [-10000], "\xf0\xd8", "s<"
|
---|
50 | pptest [50000], "\x50\xc3", "S<"
|
---|
51 |
|
---|
52 | pptest [1000000000], "\x3b\x9a\xca\x00", "l>"
|
---|
53 | pptest [-1000000000], "\xc4\x65\x36\x00", "l>"
|
---|
54 |
|
---|
55 | pptest [1], "\x01\x00\x00\x00", "L<"
|
---|
56 | pptest [258], "\x02\x01\x00\x00", "L<"
|
---|
57 | pptest [66051], "\x03\x02\x01\x00", "L<"
|
---|
58 | pptest [16909060], "\x04\x03\x02\x01", "L<"
|
---|
59 | pptest [16909060], "\x01\x02\x03\x04", "L>"
|
---|
60 |
|
---|
61 | packtest [-1], "\xff\xff\xff\xff", "L<"
|
---|
62 |
|
---|
63 | pptest [1000000000], "\x00\x00\x00\x00\x3b\x9a\xca\x00", "q>"
|
---|
64 | pptest [-1000000000], "\xff\xff\xff\xff\xc4\x65\x36\x00", "q>"
|
---|
65 |
|
---|
66 | if (2**33).is_a? Fixnum
|
---|
67 | pptest [81985529216486895], "\x01\x23\x45\x67\x89\xab\xcd\xef", "q>"
|
---|
68 | pptest [-1167088121787636991], "\x01\x23\x45\x67\x89\xab\xcd\xef", "q<"
|
---|
69 | end
|
---|
70 |
|
---|
71 | pptest [16909060], "\x01\x02\x03\x04", "N"
|
---|
72 | pptest [258], "\x01\x02", "n"
|
---|
73 | pptest [32769], "\x80\x01", "n"
|
---|
74 |
|
---|
75 | pptest [16909060], "\x04\x03\x02\x01", "V"
|
---|
76 | pptest [258], "\x02\x01", "v"
|
---|
77 |
|
---|
78 | packtest [""], "", "m"
|
---|
79 | packtest ["a"], "YQ==\n", "m"
|
---|
80 | packtest ["ab"], "YWI=\n", "m"
|
---|
81 | packtest ["abc"], "YWJj\n", "m"
|
---|
82 | packtest ["abcd"], "YWJjZA==\n", "m"
|
---|
83 |
|
---|
84 | unpacktest [""], "", "m"
|
---|
85 | unpacktest ["a"], "YQ==\n", "m"
|
---|
86 | unpacktest ["ab"], "YWI=\n", "m"
|
---|
87 | unpacktest ["abc"], "YWJj\n", "m"
|
---|
88 | unpacktest ["abcd"], "YWJjZA==\n", "m"
|
---|
89 |
|
---|
90 | packtest [""], "\0", "H"
|
---|
91 | packtest ["3"], "0", "H"
|
---|
92 | packtest ["34"], "", "H0"
|
---|
93 | packtest ["34"], "0", "H"
|
---|
94 | packtest ["34"], "4", "H2"
|
---|
95 | packtest ["34"], "4\0", "H3"
|
---|
96 | packtest ["3456"], "4P", "H3"
|
---|
97 | packtest ["34563"], "4V0", "H*"
|
---|
98 | packtest ["5a"], "Z", "H*"
|
---|
99 | packtest ["5A"], "Z", "H*"
|
---|
100 |
|
---|
101 | unpacktest [""], "", "H"
|
---|
102 | unpacktest [""], "0", "H0"
|
---|
103 | unpacktest ["3"], "0", "H"
|
---|
104 | unpacktest ["30"], "0", "H2"
|
---|
105 | unpacktest ["30"], "0", "H3"
|
---|
106 | unpacktest ["303"], "01", "H3"
|
---|
107 | unpacktest ["303132"], "012", "H*"
|
---|
108 | unpacktest ["3031", 50], "012", "H4C"
|
---|
109 | unpacktest ["5a"], "Z", "H*"
|
---|
110 |
|
---|
111 | packtest [""], "\0", "h"
|
---|
112 | packtest ["3"], "\03", "h"
|
---|
113 | packtest ["34"], "", "h0"
|
---|
114 | packtest ["34"], "\03", "h"
|
---|
115 | packtest ["34"], "C", "h2"
|
---|
116 | packtest ["34"], "C\0", "h3"
|
---|
117 | packtest ["3456"], "C\05", "h3"
|
---|
118 | packtest ["34563"], "Ce\03", "h*"
|
---|
119 |
|
---|
120 | packtest [""], " ", "A"
|
---|
121 | unpacktest [""], "", "A"
|
---|
122 | pptest ["1"], "1", "A"
|
---|
123 | pptest ["1"], "1 ", "A2"
|
---|
124 | unpacktest ["1"], "1", "A2"
|
---|
125 | unpacktest ["1"], "1 ", "A2"
|
---|
126 | unpacktest ["1"], "1\0", "A2"
|
---|
127 | packtest ["12"], "1", "A"
|
---|
128 | unpacktest ["1"], "12", "A"
|
---|
129 | pptest ["123"], "123", "A*"
|
---|
130 | packtest ["1","2"], "2", "A0A"
|
---|
131 | unpacktest ["","2"], "2", "A0A"
|
---|
132 |
|
---|
133 | packtest [""], "\0", "a"
|
---|
134 | unpacktest [""], "", "a"
|
---|
135 | pptest ["1"], "1", "a"
|
---|
136 | pptest ["1 "], "1 ", "a2"
|
---|
137 | pptest ["1\0"], "1\0", "a2"
|
---|
138 | packtest ["1"], "1\0", "a2"
|
---|
139 | pptest ["123"], "123", "a*"
|
---|
140 |
|
---|
141 | packtest [""], "\0", "Z"
|
---|
142 | unpacktest [""], "", "Z"
|
---|
143 | pptest ["1"], "1", "Z"
|
---|
144 | pptest ["1"], "1\0", "Z2"
|
---|
145 | pptest ["1 "], "1 ", "Z2"
|
---|
146 | pptest ["123"], "123\0", "Z*"
|
---|
147 | pptest ["1","2"], "12", "ZZ"
|
---|
148 | pptest ["1","2"], "1\0002", "Z*Z"
|
---|
149 | unpacktest ["1","3"], "1\00023", "Z3Z"
|
---|
150 |
|
---|
151 | packtest [1, 2], "\x01\x02", "CyC"
|
---|
152 |
|
---|
153 | packtest [65], "A", 'U'
|
---|
154 | packtest [59411], "\xEE\xA0\x93", 'U'
|
---|
155 |
|
---|
156 | pptest [1], "\x00\x01", "xC"
|
---|
157 | unpacktest [2], "\xcc\x02", "xC"
|
---|