# Commands covered: open, close, gets, read, puts, seek, tell, eof, flush,
#		    fblocked, fconfigure, open, channel
#
# This file contains a collection of tests for one or more of the Tcl
# built-in commands.  Sourcing this file into Tcl runs the tests and
# generates output for errors.  No output means no errors were found.
#
# Copyright (c) 1991-1994 The Regents of the University of California.
# Copyright (c) 1994-1996 Sun Microsystems, Inc.
#
# See the file "license.terms" for information on usage and redistribution
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
#
# RCS: @(#) $Id: ioCmd.test,v 1.2 1998/11/04 23:48:39 hylands Exp $

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

removeFile test1
removeFile pipe

set executable [list [info nameofexecutable]]

#test iocmd-1.0 {copyfile command} {
#    list [catch {copyfile a b c d e f} msg] $msg
#} {1 {wrong # args: should be "copyfile inChanId outChanId ?chunkSize?"}}
#test iocmd-1.1 {copyfile command} {
#    list [catch {copyfile f1} msg] $msg
#} {1 {wrong # args: should be "copyfile inChanId outChanId ?chunkSize?"}}
#test iocmd-1.2 {copyfile command} {
#    list [catch {copyfile f1 f2} msg] $msg
#} {1 {can not find channel named "f1"}}
#test iocmd-1.3 {copyfile command} {
#    list [catch {copyfile stdin f2} msg] $msg
#} {1 {can not find channel named "f2"}}
#test iocmd-1.4 {copyfile command} {
#    list [catch {copyfile stdin stdout booboo} msg] $msg
#} {1 {expected integer but got "booboo"}}
#test iocmd-1.5 {copyfile command} {
#    list [catch {copyfile stdout stdin} msg] $msg
#} {1 {channel "stdout" wasn't opened for reading}}
#test iocmd-1.6 {copyfile command} {
#    list [catch {copyfile stdin stdin} msg] $msg
#} {1 {channel "stdin" wasn't opened for writing}}

test iocmd-2.1 {puts command} {
   list [catch {puts} msg] $msg
} {1 {wrong # args: should be "puts ?-nonewline? ?channelId? string"}}
test iocmd-2.2 {puts command} {
   list [catch {puts a b c d e f g} msg] $msg
} {1 {wrong # args: should be "puts ?-nonewline? ?channelId? string"}}
test iocmd-2.3 {puts command} {
   list [catch {puts froboz -nonewline kablooie} msg] $msg
} {1 {bad argument "kablooie": should be "nonewline"}}
test iocmd-2.4 {puts command} {
   list [catch {puts froboz hello} msg] $msg
} {1 {can not find channel named "froboz"}}
test iocmd-2.5 {puts command} {
   list [catch {puts stdin hello} msg] $msg
} {1 {channel "stdin" wasn't opened for writing}}

test iocmd-3.0 {flush command} {
   list [catch {flush} msg] $msg
} {1 {wrong # args: should be "flush channelId"}}
test iocmd-3.1 {flush command} {
   list [catch {flush a b c d e} msg] $msg
} {1 {wrong # args: should be "flush channelId"}}
test iocmd-3.3 {flush command} {
   list [catch {flush foo} msg] $msg
} {1 {can not find channel named "foo"}}
test iocmd-3.4 {flush command} {
   list [catch {flush stdin} msg] $msg
} {1 {channel "stdin" wasn't opened for writing}}

test iocmd-4.0 {gets command} {
   list [catch {gets} msg] $msg
} {1 {wrong # args: should be "gets channelId ?varName?"}}
test iocmd-4.1 {gets command} {
   list [catch {gets a b c d e f g} msg] $msg
} {1 {wrong # args: should be "gets channelId ?varName?"}}
test iocmd-4.2 {gets command} {
   list [catch {gets aaa} msg] $msg
} {1 {can not find channel named "aaa"}}
test iocmd-4.2 {gets command} {
   list [catch {gets stdout} msg] $msg
} {1 {channel "stdout" wasn't opened for reading}}

test iocmd-5.0 {read command} {
   list [catch {read} msg] $msg
} {1 {wrong # args: should be "read channelId ?numBytes?" or "read ?-nonewline? channelId"}}
test iocmd-5.1 {read command} {
   list [catch {read a b c d e f g h} msg] $msg
} {1 {wrong # args: should be "read channelId ?numBytes?" or "read ?-nonewline? channelId"}}
test iocmd-5.2 {read command} {
   list [catch {read aaa} msg] $msg
} {1 {can not find channel named "aaa"}}
test iocmd-5.3 {read command} {
   list [catch {read -nonewline} msg] $msg
} {1 {wrong # args: should be "read channelId ?numBytes?" or "read ?-nonewline? channelId"}}
test iocmd-5.4 {read command} {
   list [catch {read -nonew file4} msg] $msg $errorCode
} {1 {can not find channel named "-nonew"} NONE}
test iocmd-5.5 {read command} {
   list [catch {read stdout} msg] $msg
} {1 {channel "stdout" wasn't opened for reading}}
test iocmd-5.6 {read command} {
   list [catch {read -nonewline stdout} msg] $msg
} {1 {channel "stdout" wasn't opened for reading}}
test iocmd-5.23 {read command with incorrect combination of arguments} {
    removeFile test1
    set f [open test1 w]
    puts $f "Two lines: this one"
    puts $f "and this one"
    close $f
    set f [open test1]
    set x [list [catch {read -nonewline $f 20 z} msg] $msg $errorCode]
    close $f
    set x
} {1 {wrong # args: should be "read channelId ?numBytes?" or "read ?-nonewline? channelId"} NONE}
test iocmd-5.24 {read command} {
    list [catch {read stdin foo} msg] $msg $errorCode
} {1 {bad argument "foo": should be "nonewline"} NONE}
test iocmd-5.25 {read command} {
    list [catch {read file107} msg] $msg $errorCode
} {1 {can not find channel named "file107"} NONE}
test iocmd-5.26 {read command} {
    set f [open test3 w]
    set x [list [catch {read $f} msg] $msg $errorCode]
    close $f
    string compare [string tolower $x] \
	[list 1 [format "channel \"%s\" wasn't opened for reading" $f] none]
} 0
test iocmd-5.27 {read command} {
    set f [open test1]
    set x [list [catch {read $f 12z} msg] $msg $errorCode]
    close $f
    set x
} {1 {expected integer but got "12z"} NONE}

test iocmd-6.0 {seek command} {
    list [catch {seek} msg] $msg
} {1 {wrong # args: should be "seek channelId offset ?origin?"}}
test iocmd-6.1 {seek command} {
    list [catch {seek a b c d e f g} msg] $msg
} {1 {wrong # args: should be "seek channelId offset ?origin?"}}
test iocmd-6.2 {seek command} {
    list [catch {seek stdin gugu} msg] $msg
} {1 {expected integer but got "gugu"}}
test iocmd-6.3 {seek command} {
    list [catch {seek stdin 100 gugu} msg] $msg
} {1 {bad origin "gugu": should be start, current, or end}}

test iocmd-7.0 {tell command} {
    list [catch {tell} msg] $msg
} {1 {wrong # args: should be "tell channelId"}}
test iocmd-7.1 {tell command} {
    list [catch {tell a b c d e} msg] $msg
} {1 {wrong # args: should be "tell channelId"}}
test iocmd-7.2 {tell command} {
    list [catch {tell aaa} msg] $msg
} {1 {can not find channel named "aaa"}}

test iocmd-8.0 {close command} {
    list [catch {close} msg] $msg
} {1 {wrong # args: should be "close channelId"}}
test iocmd-8.1 {close command} {
    list [catch {close a b c d e} msg] $msg
} {1 {wrong # args: should be "close channelId"}}
test iocmd-8.2 {close command} {
    list [catch {close aaa} msg] $msg
} {1 {can not find channel named "aaa"}}

test iocmd-9.0 {fconfigure command} {
    list [catch {fconfigure} msg] $msg
} {1 {wrong # args: should be "fconfigure channelId ?optionName? ?value? ?optionName value?..."}}
test iocmd-9.1 {fconfigure command} {
    list [catch {fconfigure a b c d e f} msg] $msg
} {1 {wrong # args: should be "fconfigure channelId ?optionName? ?value? ?optionName value?..."}}
test iocmd-9.2 {fconfigure command} {
    list [catch {fconfigure a b} msg] $msg
} {1 {can not find channel named "a"}}
test iocmd-9.3 {fconfigure command} {
    removeFile test1
    set f1 [open test1 w]
    set x [list [catch {fconfigure $f1 froboz} msg] $msg]
    close $f1
    set x
} {1 {bad option "froboz": must be -blocking, -buffering, -buffersize, -eofchar, -translation, or a channel type specific option}}
test iocmd-9.4 {fconfigure command} {
    list [catch {fconfigure stdin -buffering froboz} msg] $msg
} {1 {bad value for -buffering: must be one of full, line, or none}}
test iocmd-9.4 {fconfigure command} {
    list [catch {fconfigure stdin -translation froboz} msg] $msg
} {1 {bad value for -translation: must be one of auto, binary, cr, lf, crlf, or platform}}
test iocmd-9.5 {fconfigure command} {
    removeFile test1
    set f1 [open test1 w]
    fconfigure $f1 -translation lf -eofchar {}
    set x [fconfigure $f1]
    close $f1
    set x
} {-blocking 1 -buffering full -buffersize 4096 -eofchar {} -translation lf}
test iocmd-9.6 {fconfigure command} {
    removeFile test1
    set f1 [open test1 w]
    fconfigure $f1 -translation lf -buffering line -buffersize 3030 \
		-eofchar {}
    set x ""
    lappend x [fconfigure $f1 -buffering]
    lappend x [fconfigure $f1]
    close $f1
    set x
} {line {-blocking 1 -buffering line -buffersize 3030 -eofchar {} -translation lf}}
test iocmd-9.7 {fconfigure command} {
    removeFile test1
    set f1 [open test1 w]
    fconfigure $f1 -translation binary -buffering none -buffersize 4040 \
		-eofchar {}
    set x [fconfigure $f1]
    close $f1
    set x
} {-blocking 1 -buffering none -buffersize 4040 -eofchar {} -translation lf}
test iocmd-9.8 {fconfigure command} {
    list [catch {fconfigure a b} msg] $msg
} {1 {can not find channel named "a"}}
test iocmd-9.9 {fconfigure command} {
    list [catch {fconfigure stdout -froboz blarfo} msg] $msg
} {1 {bad option "-froboz": should be -blocking, -buffering, -buffersize, -eofchar, -translation, or channel type specific option}}
test iocmd-9.10 {fconfigure command} {
    list [catch {fconfigure stdout -b blarfo} msg] $msg
} {1 {bad option "-b": should be -blocking, -buffering, -buffersize, -eofchar, -translation, or channel type specific option}}
test iocmd-9.11 {fconfigure command} {
    list [catch {fconfigure stdout -buffer blarfo} msg] $msg
} {1 {bad option "-buffer": should be -blocking, -buffering, -buffersize, -eofchar, -translation, or channel type specific option}}
test iocmd-9.12 {fconfigure command} {
    fconfigure stdin -buffers
} 4096

test iocmd-10.1 {eof command} {
    list [catch {eof} msg] $msg $errorCode
} {1 {wrong # args: should be "eof channelId"} NONE}
test iocmd-10.2 {eof command} {
    list [catch {eof a b} msg] $msg $errorCode
} {1 {wrong # args: should be "eof channelId"} NONE}
test iocmd-10.3 {eof command} {
    catch {close file100}
    list [catch {eof file100} msg] $msg $errorCode
} {1 {can not find channel named "file100"} NONE}

test iocmd-11.0 {fblocked command} {
    list [catch {fblocked} msg] $msg
} {1 {wrong # args: should be "fblocked channelId"}}
test iocmd-11.1 {fblocked command} {
    list [catch {fblocked a b c d e f g} msg] $msg
} {1 {wrong # args: should be "fblocked channelId"}}
test iocmd-11.2 {fblocked command} {
    list [catch {fblocked file1000} msg] $msg
} {1 {can not find channel named "file1000"}}
test iocmd-11.3 {fblocked command} {
    list [catch {fblocked stdout} msg] $msg
} {1 {channel "stdout" wasn't opened for reading}}
test iocmd-11.4 {fblocked command} {
    fblocked stdin
} 0

test iocmd-12.1 {I/O to command pipelines} {unixOrPc unixExecs} {
    list [catch {open "| cat < test1 > test3" w} msg] $msg $errorCode
} {1 {can't write input to command: standard input was redirected} NONE}
test iocmd-12.2 {I/O to command pipelines} {unixOrPc unixExecs} {
    list [catch {open "| echo > test3" r} msg] $msg $errorCode
} {1 {can't read output from command: standard output was redirected} NONE}
test iocmd-12.3 {I/O to command pipelines} {unixOrPc unixExecs} {
    list [catch {open "| echo > test3" r+} msg] $msg $errorCode
} {1 {can't read output from command: standard output was redirected} NONE}

test iocmd-13.1 {POSIX open access modes: RDONLY} {
    removeFile test1
    set f [open test1 w]
    puts $f "Two lines: this one"
    puts $f "and this one"
    close $f
    set f [open test1 RDONLY]
    set x [list [gets $f] [catch {puts $f Test} msg] $msg]
    close $f
    string compare $x \
	"{Two lines: this one} 1 [list [format "channel \"%s\" wasn't opened for writing" $f]]"
} 0
test iocmd-13.2 {POSIX open access modes: RDONLY} {
    removeFile test3
    string tolower [list [catch {open test3 RDONLY} msg] $msg]
} {1 {couldn't open "test3": no such file or directory}}
test iocmd-13.3 {POSIX open access modes: WRONLY} {
    removeFile test3
    string tolower [list [catch {open test3 WRONLY} msg] $msg]
} {1 {couldn't open "test3": no such file or directory}}
#
# Test 13.4 relies on assigning the same channel name twice.
#
test iocmd-13.4 {POSIX open access modes: WRONLY} {unixOnly} {
    removeFile test3
    set f [open test3 w]
    fconfigure $f -eofchar {}
    puts $f xyzzy
    close $f
    set f [open test3 WRONLY]
    fconfigure $f -eofchar {}
    puts -nonewline $f "ab"
    seek $f 0 current
    set x [list [catch {gets $f} msg] $msg]
    close $f
    set f [open test3 r]
    fconfigure $f -eofchar {}
    lappend x [gets $f]
    close $f
    set y [list 1 [format "channel \"%s\" wasn't opened for reading" $f] abzzy]
    string compare $x $y
} 0
test iocmd-13.5 {POSIX open access modes: RDWR} {
    removeFile test3
    string tolower [list [catch {open test3 RDWR} msg] $msg]
} {1 {couldn't open "test3": no such file or directory}}
test iocmd-13.15 {POSIX open access modes: errors} {
    concat [catch {open test3 "FOO \{BAR BAZ"} msg] $msg\n$errorInfo
} "1 unmatched open brace in list
unmatched open brace in list
    while processing open access modes \"FOO {BAR BAZ\"
    invoked from within
\"open test3 \"FOO \\{BAR BAZ\"\""
test iocmd-13.16 {POSIX open access modes: errors} {
  list [catch {open test3 {FOO BAR BAZ}} msg] $msg
} {1 {invalid access mode "FOO": must be RDONLY, WRONLY, RDWR, APPEND, CREAT EXCL, NOCTTY, NONBLOCK, or TRUNC}}
test iocmd-13.17 {POSIX open access modes: errors} {
    list [catch {open test3 {TRUNC CREAT}} msg] $msg
} {1 {access mode must include either RDONLY, WRONLY, or RDWR}}

test iocmd-14.1 {errors in open command} {
    list [catch {open} msg] $msg
} {1 {wrong # args: should be "open fileName ?access? ?permissions?"}}
test iocmd-14.2 {errors in open command} {
    list [catch {open a b c d} msg] $msg
} {1 {wrong # args: should be "open fileName ?access? ?permissions?"}}
test iocmd-14.3 {errors in open command} {
    list [catch {open test1 x} msg] $msg
} {1 {illegal access mode "x"}}
test iocmd-14.4 {errors in open command} {
    list [catch {open test1 rw} msg] $msg
} {1 {illegal access mode "rw"}}
test iocmd-14.5 {errors in open command} {
    list [catch {open test1 r+1} msg] $msg
} {1 {illegal access mode "r+1"}}
test iocmd-14.6 {errors in open command} {
    string tolower [list [catch {open _non_existent_} msg] $msg $errorCode]
} {1 {couldn't open "_non_existent_": no such file or directory} {posix enoent {no such file or directory}}}

test iocmd-15.1 {file id parsing errors} {
    list [catch {eof gorp} msg] $msg $errorCode
} {1 {can not find channel named "gorp"} NONE}
test iocmd-15.2 {file id parsing errors} {
    list [catch {eof filex} msg] $msg
} {1 {can not find channel named "filex"}}
test iocmd-15.3 {file id parsing errors} {
    list [catch {eof file12a} msg] $msg
} {1 {can not find channel named "file12a"}}
test iocmd-15.4 {file id parsing errors} {
    list [catch {eof file123} msg] $msg
} {1 {can not find channel named "file123"}}
test iocmd-15.5 {file id parsing errors} {
    list [catch {eof stdout} msg] $msg
} {0 0}
test iocmd-15.6 {file id parsing errors} {
    list [catch {eof stdin} msg] $msg
} {0 0}
test iocmd-15.7 {file id parsing errors} {
    list [catch {eof stdout} msg] $msg
} {0 0}
test iocmd-15.8 {file id parsing errors} {
    list [catch {eof stderr} msg] $msg
} {0 0}
test iocmd-15.9 {file id parsing errors} {
    list [catch {eof stderr1} msg] $msg
} {1 {can not find channel named "stderr1"}}
set f [open test1]
close $f
set expect "1 {can not find channel named \"$f\"}"
test iocmd-15.10 {file id parsing errors} {
    list [catch {eof $f} msg] $msg
} $expect

removeFile test1
removeFile test2
removeFile test3
removeFile pipe
removeFile output
set x ""
set x
