source: EcnlProtoTool/trunk/mrbgems/mruby-pack/test/pack.rb@ 279

Last change on this file since 279 was 279, checked in by coas-nagasima, 7 years ago

ファイルを追加、更新。

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-ruby
File size: 3.5 KB
Line 
1# pack & unpack 'm' (base64)
2assert('[""].pack("m")') do
3 ary = ""
4 str = ""
5 [ary].pack("m") == str and
6 str.unpack("m") == [ary]
7end
8
9assert('["\0"].pack("m")') do
10 ary = "\0"
11 str = "AA==\n"
12 [ary].pack("m") == str and
13 str.unpack("m") == [ary]
14end
15
16assert('["\0\0"].pack("m")') do
17 ary = "\0\0"
18 str = "AAA=\n"
19 [ary].pack("m") == str and
20 str.unpack("m") == [ary]
21end
22
23assert('["\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]
28end
29
30assert('["abc..xyzABC..XYZ"].pack("m")') do
31 ["abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"].pack("m") == "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNERUZHSElKS0xNTk9QUVJT\nVFVWV1hZWg==\n"
32end
33
34assert('"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]
38end
39
40# pack & unpack 'H'
41assert('["3031"].pack("H*")') do
42 ary = "3031"
43 str = "01"
44 [ary].pack("H*") == str and
45 str.unpack("H*") == [ary]
46end
47
48assert('["10"].pack("H*")') do
49 ary = "10"
50 str = "\020"
51 [ary].pack("H*") == str and
52 str.unpack("H*") == [ary]
53end
54
55assert('[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
59end
60
61# pack "a"
62assert('["abc"].pack("a")') do
63 ["abc"].pack("a") == "a" and
64 ["abc"].pack("a*") == "abc" and
65 ["abc"].pack("a4") == "abc\0"
66end
67
68# upack "a"
69assert('["abc"].pack("a")') do
70 "abc\0".unpack("a4") == ["abc\0"] and
71 "abc ".unpack("a4") == ["abc "]
72end
73
74# pack "A"
75assert('["abc"].pack("A")') do
76 ["abc"].pack("A") == "a" and
77 ["abc"].pack("A*") == "abc" and
78 ["abc"].pack("A4") == "abc "
79end
80
81# upack "A"
82assert('["abc"].pack("A")') do
83 "abc\0".unpack("A4") == ["abc"] and
84 "abc ".unpack("A4") == ["abc"]
85end
86
87# regression tests
88assert('issue #1') do
89 [1, 2].pack("nn") == "\000\001\000\002"
90end
91
92def assert_pack tmpl, packed, unpacked
93 assert_equal packed, unpacked.pack(tmpl)
94 assert_equal unpacked, packed.unpack(tmpl)
95end
96
97PACK_IS_LITTLE_ENDIAN = "\x01\00".unpack('S')[0] == 0x01
98
99assert '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
110end
111
112assert '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
123end
124
125assert '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]
135end
136
137assert '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]
147end
Note: See TracBrowser for help on using the repository browser.