File: test_pack.rb

package info (click to toggle)
jruby 1.5.1-1
  • links: PTS, VCS
  • area: non-free
  • in suites: squeeze
  • size: 46,252 kB
  • ctags: 72,039
  • sloc: ruby: 398,155; java: 169,482; yacc: 3,782; xml: 2,469; ansic: 415; sh: 279; makefile: 78; tcl: 40
file content (157 lines) | stat: -rw-r--r-- 4,929 bytes parent folder | download | duplicates (5)
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
require 'test/unit'

class TestPack < Test::Unit::TestCase

  def test_pack_A
    a = %w(cat wombat x yy)
    assert_equal "catwomx  yy ",          a.pack("A3A3A3A3")
    assert_equal "cat",                   a.pack("A*")
    assert_equal "cwx  yy ",              a.pack("A3@1A3@2A3A3")
  end

  def test_pack_a
    a = %w(cat wombat x yy)
    assert_equal "catwomx\000\000yy\000", a.pack("a3a3a3a3")
    assert_equal "cat",                   a.pack("a*")
    assert_equal "ca",                    a.pack("a2")
    assert_equal "cat\000\000",           a.pack("a5")
  end

  def test_pack_B
    assert_equal "\x61",     ["01100001"].pack("B8")
    assert_equal "\x61",     ["01100001"].pack("B*")
    assert_equal "\x61",     ["0110000100110111"].pack("B8")
    assert_equal "\x61\x37", ["0110000100110111"].pack("B16")
    assert_equal "\x61\x37", ["01100001", "00110111"].pack("B8B8")
    assert_equal "\x60",     ["01100001"].pack("B4")
    assert_equal "\x40",     ["01100001"].pack("B2")
  end

  def test_pack_b
    assert_equal "\x86",     ["01100001"].pack("b8")
    assert_equal "\x86",     ["01100001"].pack("b*")
    assert_equal "\x86",     ["0110000100110111"].pack("b8")
    assert_equal "\x86\xec", ["0110000100110111"].pack("b16")
    assert_equal "\x86\xec", ["01100001", "00110111"].pack("b8b8")
    assert_equal "\x06",     ["01100001"].pack("b4")
    assert_equal "\x02",     ["01100001"].pack("b2")
  end

  def test_pack_C
    assert_equal "ABC",      [ 65, 66, 67 ].pack("C3")
    assert_equal "\377BC",   [ -1, 66, 67 ].pack("C*")
  end

  def test_pack_c
    assert_equal "ABC",      [ 65, 66, 67 ].pack("c3")
    assert_equal "\377BC",   [ -1, 66, 67 ].pack("c*")
  end

  def test_pack_H
    assert_equal "AB\n\x10",  ["4142", "0a", "12"].pack("H4H2H1")
    assert_equal "AB\n\x02",  ["1424", "a0", "21"].pack("h4h2h1")
  end

  def test_pack_M
    assert_equal("abc=02def=\ncat=\n=01=\n", 
                 ["abc\002def", "cat", "\001"].pack("M9M3M4"))
  end

  def test_pack_m
    assert_equal "aGVsbG8K\n",  ["hello\n"].pack("m")
  end

  def test_pack_u
    assert_equal ",:&5L;&\\*:&5L;&\\*\n",  ["hello\nhello\n"].pack("u")
  end

  def test_pack_U
    assert_equal "\xc2\xa9B\xe2\x89\xa0", [0xa9, 0x42, 0x2260].pack("U*")
  end

  def test_pack_ugly
    format = "c2x5CCxsdils_l_a6";
    # Need the expression in here to force ary[5] to be numeric.  This avoids
    # test2 failing because ary2 goes str->numeric->str and ary does not.
    ary = [1, -100, 127, 128, 32767, 987.654321098/100.0,
           12345, 123456, -32767, -123456, "abcdef"]
    x    = ary.pack(format)
    ary2 = x.unpack(format)

    assert_equal ary.length, ary2.length
    assert_equal ary.join(':'), ary2.join(':')
    assert_not_nil(x =~ /def/)
  end

  def test_unpack_A
    assert_equal ["cat", "wom", "x", "yy"], "catwomx  yy ".unpack("A3A3A3A3")

    assert_equal ["cat"], "cat  \000\000".unpack("A*")
    assert_equal ["cwx", "wx", "x", "yy"], "cwx  yy ".unpack("A3@1A3@2A3A3")
  end

  def test_unpack_a
    assert_equal ["cat", "wom", "x\000\000", "yy\000"],
                 "catwomx\000\000yy\000".unpack("a3a3a3a3")
    assert_equal ["cat \000\000"], "cat \000\000".unpack("a*")
    assert_equal ["ca"], "catdog".unpack("a2")
    assert_equal ["cat\000\000"], "cat\000\000\000\000\000dog".unpack("a5")
  end

  def test_unpack_B
    assert_equal ["01100001"], "\x61".unpack("B8")
    assert_equal ["01100001"], "\x61".unpack("B*")
    assert_equal ["0110000100110111"], "\x61\x37".unpack("B16")
    assert_equal ["01100001", "00110111"], "\x61\x37".unpack("B8B8")
    assert_equal ["0110"], "\x60".unpack("B4")

    assert_equal ["01"], "\x40".unpack("B2")
  end

  def test_unpack_b
    assert_equal ["01100001"], "\x86".unpack("b8")
    assert_equal ["01100001"], "\x86".unpack("b*")

    assert_equal ["0110000100110111"], "\x86\xec".unpack("b16")
    assert_equal ["01100001", "00110111"], "\x86\xec".unpack("b8b8")

    assert_equal ["0110"], "\x06".unpack("b4")
    assert_equal ["01"], "\x02".unpack("b2")
  end

  def test_unpack_C
    assert_equal [ 65, 66, 67 ],  "ABC".unpack("C3")
    assert_equal [ 255, 66, 67 ], "\377BC".unpack("C*")
  end

  def test_unpack_c
    assert_equal [ 65, 66, 67 ],  "ABC".unpack("c3")
    assert_equal [ -1, 66, 67 ],  "\377BC".unpack("c*")
  end
    
  def test_unpack_H
    assert_equal ["4142", "0a", "1"], "AB\n\x10".unpack("H4H2H1")
  end

  def test_unpack_h
    assert_equal ["1424", "a0", "2"], "AB\n\x02".unpack("h4h2h1")
  end

  def test_unpack_M
    assert_equal ["abc\002defcat\001", "", ""],
                 "abc=02def=\ncat=\n=01=\n".unpack("M9M3M4")
  end

  def test_unpack_m
    assert_equal ["hello\n"], "aGVsbG8K\n".unpack("m")
  end

  def test_unpack_u
    assert_equal ["hello\nhello\n"], ",:&5L;&\\*:&5L;&\\*\n".unpack("u")
  end

  def test_unpack_U
    assert_equal [0xa9, 0x42, 0x2260], "\xc2\xa9B\xe2\x89\xa0".unpack("U*")
  end

end