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
