aboutsummaryrefslogtreecommitdiff
path: root/tests/pack.test
blob: 0746e445a0b076ff89d50bc2955bae1faa3b18b1 (plain)
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
source [file dirname [info script]]/testing.tcl

needs cmd pack

test pack-1.1 {pack usage} -body {
	pack
} -returnCodes error -result {wrong # args: should be "pack varName value -intle|-intbe|-floatle|-floatbe|-str bitwidth ?bitoffset?"}

test pack-1.2 {pack invalid type} -body {
	pack a 1 -badopt 8
} -returnCodes error -result {bad option "-badopt": must be -floatbe, -floatle, -intbe, -intle, or -str}

test pack-1.3 {pack bad width} -body {
	pack a 1 -intbe badint
} -returnCodes error -match glob -result {expected integer *but got "badint"}

test pack-1.4 {pack bad width} -body {
	pack a 1 -intbe -5
} -returnCodes error -result {bad bitwidth: -5}

test pack-1.5 {pack bad offset} -body {
	pack a 1 -intbe 5 badint
} -returnCodes error -match glob -result {expected integer *but got "badint"}

test pack-1.6 {pack bad offset} -body {
	pack a 1 -intbe 5 -6
} -returnCodes error -result {bad bitoffset: -6}

test pack-2.1 {pack basic} {
	unset -nocomplain a
	pack a 65 -intle 8
	set a
} {A}

test pack-2.2 {pack append} {
	pack a 66 -intle 8 8
	set a
} {AB}

test pack-2.3 {pack after end pads with null} {
	pack a 67 -intle 8 24
	set a
} "AB\x00C"

test pack-2.4 {pack replace} {
	pack a 68 -intle 8 16
	set a
} "ABDC"

test pack-2.5 {pack str after end pads with null} {
	pack a ghi -str 24 40
	set a
} "ABDC\x00ghi"

test pack-2.6 {pack str width > string length} {
	set a {}
	pack a ab -str 32
	set a
} "ab\x00\x00"

set badvar {a}

test pack-2.7 {pack bad set} -body {
	pack badvar(a) 32 -intle 8
} -returnCodes error -result {can't set "badvar(a)": variable isn't array}

test pack-2.8 {pack bad set} -body {
	pack bad\x00var 32 -intle 8
} -returnCodes ok -result {8}

test unpack-1.1 {unpack usage} -body {
	unpack
} -returnCodes error -result {wrong # args: should be "unpack binvalue -intbe|-intle|-uintbe|-uintle|-floatbe|-floatle|-str bitpos bitwidth"}

test unpack-1.2 {unpack invalid type} -body {
	unpack abc -badopt 0 8
} -returnCodes error -result {bad option "-badopt": must be -floatbe, -floatle, -intbe, -intle, -str, -uintbe, or -uintle}

test unpack-1.3 {unpack bad width} -body {
	unpack abc -intle 0 badint
} -returnCodes error -match glob -result {expected integer *but got "badint"}

test unpack-1.4 {unpack bad width} -body {
	unpack abc -intle 0 -5
} -returnCodes error -result {bad bitwidth: -5}

test unpack-1.5 {unpack bad offset} -body {
	unpack abc -intle badint 8
} -returnCodes error -match glob -result {expected integer *but got "badint"}

test unpack-1.6 {unpack bad offset} -body {
	unpack abc -intle -6 8
} -returnCodes error -result {bad bitoffset: -6}

test unpack-1.7 {unpack str not on byte boundary offset} -body {
	unpack abc -str 5 8
} -returnCodes error -result {bad bitoffset: 5}

test unpack-1.8 {unpack float bad width} -body {
	unpack abc -floatbe 0 24
} -returnCodes error -result {bad bitwidth: 24}

test unpack-2.1 {unpack str width past end} -body {
	unpack abc -str 16 16
} -result c

test unpack-2.2 {unpack intle} -body {
	format 0x%04x [unpack \x01\x02\x03 -intle 8 16]
} -result 0x0302

test unpack-2.3 {unpack int width past end} -body {
	unpack \x01\x02\x03 -intle 16 16
} -result 3


testreport