File: testPackUnpack.rb

package info (click to toggle)
jruby1.0 1.0.2-4
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 28,004 kB
  • ctags: 33,860
  • sloc: ruby: 166,858; java: 90,218; yacc: 1,572; xml: 708; sh: 700; makefile: 53; ansic: 19
file content (54 lines) | stat: -rw-r--r-- 3,707 bytes parent folder | download
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
require 'test/minirunit'

test_check "Test pack/unpack:"
test_equal(["cat", "wom", "x", "yy"], %w( cat wombat x yy).pack("A3A3A3A3").unpack("A3A3A3A3"))
test_equal(["cat", "wom", "x", "yy"], %w( cat wombat x yy).pack("a3a3a3a3").unpack("Z3Z3Z3Z3"))
test_equal(["cat", "wom", "x  ", "yy "], %w( cat wombat x yy).pack("A3A3A3A3").unpack("Z3Z3Z3Z3"))
test_equal(["cat", "wom", "x\000\000", "yy\000"], %w( cat wombat x yy).pack("a3a3a3a3").unpack("a3a3a3a3"))
test_equal(["01100001"], ["01100001"].pack("b8").unpack("b8"))
test_equal(["01100001"], ["01100001"].pack("B8").unpack("B8"))
test_equal(["10000110"], ["01100001"].pack("B8").unpack("b8"))
test_equal(["1424", "a0", "2"],  ["1424", "a0", "21"].pack("h4h2h1").unpack("h4h2h1"))
test_equal(["4142", "0a", "1"],  ["4142", "0a", "12"].pack("H4H2H1").unpack("H4H2H1"))
test_equal(["1424", "a0", "0"],  ["4142", "0a", "12"].pack("H4H2H1").unpack("h4h2h1"))
test_equal([ 65, 66, 67 ], [ 65, 66, 67 ].pack("C3").unpack("C3"))
test_equal([ 255, 66, 67 ],[ -1, 66, 67 ].pack("C*").unpack("C*"))
test_equal([ -1, 66, 67 ],[ -1, 66, 67 ].pack("c*").unpack("c*"))
test_equal([-12, 65, 543, -12],[-12, 65, 543, 65524].pack("s*").unpack("s*"))
test_equal([65524, 65, 543, 65524],[-12, 65, 543, 65524].pack("S*").unpack("S*"))
test_equal([-12, 65, 543, -1339724956], [-12, 65, 543, 2955242340].pack('i*').unpack('i*'))
test_equal([4294967284, 65, 543, 2955242340],  [-12, 65, 543, 2955242340].pack('I*').unpack('I*'))
test_equal([-12, 65, 543, -1339724956], [-12, 65, 543, 2955242340].pack('l*').unpack('l*'))
test_equal([4294967284, 65, 543, 2955242340],  [-12, 65, 543, 2955242340].pack('L*').unpack('L*'))
test_equal([4294967284, 65, 543, 2955242340], [-12, 65, 543, 2955242340].pack('N*').unpack('N*'))
test_equal("\377\377\377\364\000\000\000A\000\000\002\037\260%kd" ,[-12, 65, 543, 2955242340].pack('N*'))
test_equal("\377\364\000A\002\037\377\364", [-12, 65, 543, 65524].pack('n*'))
test_equal([65524, 65, 543, 65524],[-12, 65, 543, 65524].pack("n*").unpack("n*"))
test_equal("\364\377\377\377A\000\000\000\037\002\000\000dk%\260" ,[-12, 65, 543, 2955242340].pack('V*'))
test_equal([4294967284, 65, 543, 2955242340],  [-12, 65, 543, 2955242340].pack('V*').unpack('V*'))
test_equal("\364\377A\000\037\002\212W" ,[-12, 65, 543, 295524234].pack('v*'))
test_equal([65524, 65, 543, 22410], [-12, 65, 543, 295524234].pack('v*').unpack('v*'))
test_equal("\xc2\xa9B\xe2\x89\xa0", [0xa9, 0x42, 0x2260].pack("U*"))
test_equal([0xa9, 0x42, 0x2260], [0xa9, 0x42, 0x2260].pack("U*").unpack('U*'))
test_equal([127, 128], [127, 128].pack("xCxC").unpack("xCxC"))
test_equal(["h", "e", "l", "l", "o"], "whole".unpack('xax2aX2aX1aX2a'))
test_equal([987.654321098/100.0], [987.654321098/100.0].pack("d*").unpack("d*"))
test_equal([987.654321098/100.0], [987.654321098/100.0].pack("E*").unpack("E*"))
test_equal([987.654321098/100.0], [987.654321098/100.0].pack("G*").unpack("G*"))
test_equal(["123"], ["123"].pack("m").unpack("m"))
test_equal(["12"], ["12"].pack("m").unpack("m"))
# TODO: Missing more tests for double precision.  
# TODO: Missing all single precision tests.
test_exception(ArgumentError) { ["A"].pack("X2") }
test_exception(ArgumentError) { "A".unpack("X2") }
test_exception(ArgumentError) { "A".unpack("x2") }

test_equal("\000\000\000\000\000\000\000\000", [0].pack('D'))
test_exception(ArgumentError){ ['test'].pack("D") }
test_equal("\000\000\000\000", [0].pack('F'))
test_exception(TypeError){ [0].pack("H") }
test_exception(ArgumentError){ ['test'].pack("d") }
test_exception(TypeError){ [0].pack("h") }
test_exception(TypeError){ [-1, 0, 1, 128].pack("m") }
test_equal(["testingT"], "testing=54".unpack("M*"))
test_equal(["12345"], "12345".unpack("M*"))