aboutsummaryrefslogtreecommitdiff
path: root/tests/pack.test
diff options
context:
space:
mode:
authorSteve Bennett <steveb@workware.net.au>2020-04-18 09:34:25 +1000
committerSteve Bennett <steveb@workware.net.au>2020-05-04 21:57:34 +1000
commitda82368c816c8d06f425aa3f25a2a918fdba1df1 (patch)
treee1dc05358910d168edc982ed05523d0b30ad24d5 /tests/pack.test
parent8a5861eb51c32e41d638181188c256c1dbb93c96 (diff)
downloadjimtcl-da82368c816c8d06f425aa3f25a2a918fdba1df1.zip
jimtcl-da82368c816c8d06f425aa3f25a2a918fdba1df1.tar.gz
jimtcl-da82368c816c8d06f425aa3f25a2a918fdba1df1.tar.bz2
tests: Add many new additional tests for code coverage
readdir, tty, utf8, signal, alarm, kill, file, jimsh, posix, aio, history, interp, pack, unpack, eventloop, exec, load, package, regexp, regsub Signed-off-by: Steve Bennett <steveb@workware.net.au>
Diffstat (limited to 'tests/pack.test')
-rw-r--r--tests/pack.test118
1 files changed, 118 insertions, 0 deletions
diff --git a/tests/pack.test b/tests/pack.test
new file mode 100644
index 0000000..a01669a
--- /dev/null
+++ b/tests/pack.test
@@ -0,0 +1,118 @@
+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 -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 -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 error -result {variable name contains embedded null}
+
+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 -result {expected integer but got "badint"}
+
+test unpack-1.4 {unpack bad width} -body {
+ # Poor message
+ unpack abc -intle 0 -5
+} -returnCodes error -result {int field is too wide: -5}
+
+test unpack-1.5 {unpack bad offset} -body {
+ unpack abc -intle badint 8
+} -returnCodes error -result {expected integer but got "badint"}
+
+test unpack-1.6 {unpack bad offset} {
+ # Should be an error
+ unpack abc -intle -6 8
+} 0
+
+test unpack-1.7 {unpack str not on byte boundary offset} -body {
+ unpack abc -str 5 8
+} -returnCodes error -result {string field is not on a byte boundary}
+
+test unpack-1.8 {unpack float bad width} -body {
+ unpack abc -floatbe 0 24
+} -returnCodes error -result {float field has 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