# -*- tcl -*-
# Commands covered:  binio
#
# This file contains a collection of tests for one or more of the trf
# commands of the TRF extension. Sourcing this file into Tcl runs the
# tests and generates output for errors.  No output means no errors were
# found.
#
# Copyright (c) 1997 Andreas Kupries (a.kupries@westend.com)
#
# See the file "license.terms" for information on usage and redistribution
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
#
# $Id: binio.test,v 1.4 1997/05/31 13:51:56 aku Exp $

if {[string compare test [info procs test]] == 1} then {source defs}

if {[catch {binio} msg] && ("$msg" == "invalid command name \"binio\"")} {
    puts stdout "\tSkipping 'binio' tests, as the command is not compiled into the package"
    flush stdout
    return ""
}


# determine endianess of machine running these tests

proc endianess {} {
    set fh [open binio.test r]
    set e  [fconfigure $fh -byteorder]
    close $fh
    return $e
}

proc choose {small big} {
    global endianess
    switch -- $endianess {
	smallendian {return $small}
	bigendian   {return $big}
    }
}


set endianess [endianess]


test binio-1.0 {binio, errors} {
    catch {binio} msg
    set msg
} {wrong # args: should be "binio option channel ?arg arg...?"}

test binio-1.1 {binio, errors} {
    catch {binio blubber a} msg
    set msg
} {binio blubber: channel expected as 2nd argument, got "a"}

test binio-1.2 {binio, errors} {
    catch {binio blubber stdin} msg
    set msg
} {binio: bad option "blubber": should be one of copy, pack or unpack}

test binio-1.3 {binio, errors} {
    catch {binio copy stdin} msg
    set msg
} {wrong # args: should be "binio copy inChannel outChannel ?chunkSize?"}

test binio-1.4 {binio, errors} {
    catch {binio pack stdout} msg
    set msg
} {wrong # args: should be "binio pack outChannel format ?data1 data2 ...?"}

test binio-1.5 {binio, errors} {
    catch {binio unpack stdin} msg
    set msg
} {wrong # args: should be "binio unpack outChannel format ?var1 var2 ...?"}

test binio-1.6 {binio copy, errors} {
    catch {binio copy stdout stdin} msg
    set msg
} {channel "stdout" wasn't opened for reading}

test binio-1.7 {binio copy, errors} {
    catch {binio copy stdin stdin} msg
    set msg
} {channel "stdin" wasn't opened for writing}

test binio-1.8 {binio copy} {
    set input  [open test.binio r]
    set output [memchan]

    fconfigure $input  -translation binary
    fconfigure $output -translation binary

    set n [binio copy $input $output]

    close $output
    close $input
    set n
} {27}

test binio-1.9 {binio unpack, %c} {
    set input  [open test.binio r]
    fconfigure $input -translation binary

    set n ""
    foreach i {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} {
	binio unpack $input %c k
	lappend n $k
    }
    close $input
    set n
} {0 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}


test binio-1.10 {binio unpack, %X, smallendian} {
    set input  [open test.binio r]
    fconfigure $input -translation binary -byteorder smallendian

    set n ""
    foreach i {1 2 3 4 5 6 7 8 9 10 11 12 13} {
	binio unpack $input %X k
	lappend n $k
    }
    close $input
    set n
} {0100 0302 0504 0706 0908 0b0a 0d0c 0f0e 1110 1312 1514 1716 1918}

test binio-1.11 {binio unpack, %X, bigendian} {
    set input  [open test.binio r]
    fconfigure $input -translation binary -byteorder bigendian

    set n ""
    foreach i {1 2 3 4 5 6 7 8 9 10 11 12 13} {
	binio unpack $input %X k
	lappend n $k
    }
    close $input
    set n
} {0001 0203 0405 0607 0809 0a0b 0c0d 0e0f 1011 1213 1415 1617 1819}

test binio-1.12 {binio unpack, %x, smallendian order} {
    set input  [open test.binio r]
    fconfigure $input -translation binary -byteorder smallendian

    set n ""
    foreach i {1 2 3 4 5 6} {
	binio unpack $input %x k
	lappend n $k
    }
    close $input
    set n
} {03020100 07060504 0b0a0908 0f0e0d0c 13121110 17161514}

test binio-1.13 {binio unpack, %x, bigendian order} {
    set input  [open test.binio r]
    fconfigure $input -translation binary -byteorder bigendian

    set n ""
    foreach i {1 2 3 4 5 6} {
	binio unpack $input %x k
	lappend n $k
    }
    close $input
    set n
} {00010203 04050607 08090a0b 0c0d0e0f 10111213 14151617}


unset   input
unset   output
unset   endianess
rename  endianess {}
rename  choose {}
