io.test
上传用户:rrhhcc
上传日期:2015-12-11
资源大小:54129k
文件大小:203k
- set s [format "abcndefn%cqrsntuv" 26]
- puts $f $s
- close $f
- set f [open $path(test1) r]
- fconfigure $f -eofchar x1a
- fconfigure $f -translation auto
- set l ""
- lappend l [gets $f]
- lappend l [gets $f]
- lappend l [eof $f]
- lappend l [gets $f]
- lappend l [eof $f]
- close $f
- set l
- } {abc def 0 {} 1}
- test io-31.21 {Tcl_Write, no newline ^Z in middle, Tcl_Gets auto, eofChar} {
- file delete $path(test1)
- set f [open $path(test1) w]
- fconfigure $f -translation lf
- set s [format "abcndefn%cqrsntuv" 26]
- puts $f $s
- close $f
- set f [open $path(test1) r]
- fconfigure $f -eofchar x1a -translation auto
- set l ""
- lappend l [gets $f]
- lappend l [gets $f]
- lappend l [eof $f]
- lappend l [gets $f]
- lappend l [eof $f]
- close $f
- set l
- } {abc def 0 {} 1}
- test io-31.22 {Tcl_Write, ^Z in middle ignored, Tcl_Gets lf} {
- file delete $path(test1)
- set f [open $path(test1) w]
- fconfigure $f -translation lf -eofchar {}
- set s [format "abcndefn%cqrsntuv" 26]
- puts $f $s
- close $f
- set f [open $path(test1) r]
- fconfigure $f -translation lf -eofchar {}
- set l ""
- lappend l [gets $f]
- lappend l [gets $f]
- lappend l [eof $f]
- lappend l [gets $f]
- lappend l [eof $f]
- lappend l [gets $f]
- lappend l [eof $f]
- lappend l [gets $f]
- lappend l [eof $f]
- close $f
- set l
- } "abc def 0 x1aqrs 0 tuv 0 {} 1"
- test io-31.23 {Tcl_Write, ^Z in middle ignored, Tcl_Gets cr} {
- file delete $path(test1)
- set f [open $path(test1) w]
- fconfigure $f -translation cr -eofchar {}
- set s [format "abcndefn%cqrsntuv" 26]
- puts $f $s
- close $f
- set f [open $path(test1) r]
- fconfigure $f -translation cr -eofchar {}
- set l ""
- lappend l [gets $f]
- lappend l [gets $f]
- lappend l [eof $f]
- lappend l [gets $f]
- lappend l [eof $f]
- lappend l [gets $f]
- lappend l [eof $f]
- lappend l [gets $f]
- lappend l [eof $f]
- close $f
- set l
- } "abc def 0 x1aqrs 0 tuv 0 {} 1"
- test io-31.24 {Tcl_Write, ^Z in middle ignored, Tcl_Gets crlf} {
- file delete $path(test1)
- set f [open $path(test1) w]
- fconfigure $f -translation crlf -eofchar {}
- set s [format "abcndefn%cqrsntuv" 26]
- puts $f $s
- close $f
- set f [open $path(test1) r]
- fconfigure $f -translation crlf -eofchar {}
- set l ""
- lappend l [gets $f]
- lappend l [gets $f]
- lappend l [eof $f]
- lappend l [gets $f]
- lappend l [eof $f]
- lappend l [gets $f]
- lappend l [eof $f]
- lappend l [gets $f]
- lappend l [eof $f]
- close $f
- set l
- } "abc def 0 x1aqrs 0 tuv 0 {} 1"
- test io-31.25 {Tcl_Write lf, ^Z in middle, Tcl_Gets auto} {
- file delete $path(test1)
- set f [open $path(test1) w]
- fconfigure $f -translation lf
- set s [format "abcndefn%cqrsntuv" 26]
- puts $f $s
- close $f
- set f [open $path(test1) r]
- fconfigure $f -translation auto -eofchar x1a
- set l ""
- lappend l [gets $f]
- lappend l [gets $f]
- lappend l [eof $f]
- lappend l [gets $f]
- lappend l [eof $f]
- close $f
- set l
- } {abc def 0 {} 1}
- test io-31.26 {Tcl_Write lf, ^Z in middle, Tcl_Gets lf} {
- file delete $path(test1)
- set f [open $path(test1) w]
- fconfigure $f -translation lf
- set s [format "abcndefn%cqrsntuv" 26]
- puts $f $s
- close $f
- set f [open $path(test1) r]
- fconfigure $f -translation lf -eofchar x1a
- set l ""
- lappend l [gets $f]
- lappend l [gets $f]
- lappend l [eof $f]
- lappend l [gets $f]
- lappend l [eof $f]
- close $f
- set l
- } {abc def 0 {} 1}
- test io-31.27 {Tcl_Write cr, ^Z in middle, Tcl_Gets auto} {
- file delete $path(test1)
- set f [open $path(test1) w]
- fconfigure $f -translation cr -eofchar {}
- set s [format "abcndefn%cqrsntuv" 26]
- puts $f $s
- close $f
- set f [open $path(test1) r]
- fconfigure $f -translation auto -eofchar x1a
- set l ""
- lappend l [gets $f]
- lappend l [gets $f]
- lappend l [eof $f]
- lappend l [gets $f]
- lappend l [eof $f]
- close $f
- set l
- } {abc def 0 {} 1}
- test io-31.28 {Tcl_Write cr, ^Z in middle, Tcl_Gets cr} {
- file delete $path(test1)
- set f [open $path(test1) w]
- fconfigure $f -translation cr -eofchar {}
- set s [format "abcndefn%cqrsntuv" 26]
- puts $f $s
- close $f
- set f [open $path(test1) r]
- fconfigure $f -translation cr -eofchar x1a
- set l ""
- lappend l [gets $f]
- lappend l [gets $f]
- lappend l [eof $f]
- lappend l [gets $f]
- lappend l [eof $f]
- close $f
- set l
- } {abc def 0 {} 1}
- test io-31.29 {Tcl_Write crlf, ^Z in middle, Tcl_Gets auto} {
- file delete $path(test1)
- set f [open $path(test1) w]
- fconfigure $f -translation crlf -eofchar {}
- set s [format "abcndefn%cqrsntuv" 26]
- puts $f $s
- close $f
- set f [open $path(test1) r]
- fconfigure $f -translation auto -eofchar x1a
- set l ""
- lappend l [gets $f]
- lappend l [gets $f]
- lappend l [eof $f]
- lappend l [gets $f]
- lappend l [eof $f]
- close $f
- set l
- } {abc def 0 {} 1}
- test io-31.30 {Tcl_Write crlf, ^Z in middle, Tcl_Gets crlf} {
- file delete $path(test1)
- set f [open $path(test1) w]
- fconfigure $f -translation crlf -eofchar {}
- set s [format "abcndefn%cqrsntuv" 26]
- puts $f $s
- close $f
- set f [open $path(test1) r]
- fconfigure $f -translation crlf -eofchar x1a
- set l ""
- lappend l [gets $f]
- lappend l [gets $f]
- lappend l [eof $f]
- lappend l [gets $f]
- lappend l [eof $f]
- close $f
- set l
- } {abc def 0 {} 1}
- test io-31.31 {Tcl_Write crlf on block boundary, Tcl_Gets crlf} {
- file delete $path(test1)
- set f [open $path(test1) w]
- fconfigure $f -translation crlf
- set line "123456789ABCDE" ;# 14 char plus crlf
- puts -nonewline $f x ;# shift crlf across block boundary
- for {set i 0} {$i < 700} {incr i} {
- puts $f $line
- }
- close $f
- set f [open $path(test1) r]
- fconfigure $f -translation crlf
- set c ""
- while {[gets $f line] >= 0} {
- append c $linen
- }
- close $f
- string length $c
- } [expr 700*15+1]
- test io-31.32 {Tcl_Write crlf on block boundary, Tcl_Gets auto} {
- file delete $path(test1)
- set f [open $path(test1) w]
- fconfigure $f -translation crlf
- set line "123456789ABCDE" ;# 14 char plus crlf
- puts -nonewline $f x ;# shift crlf across block boundary
- for {set i 0} {$i < 700} {incr i} {
- puts $f $line
- }
- close $f
- set f [open $path(test1) r]
- fconfigure $f -translation auto
- set c ""
- while {[gets $f line] >= 0} {
- append c $linen
- }
- close $f
- string length $c
- } [expr 700*15+1]
- # Test Tcl_Read and buffering.
- test io-32.1 {Tcl_Read, channel not readable} {
- list [catch {read stdout} msg] $msg
- } {1 {channel "stdout" wasn't opened for reading}}
- test io-32.2 {Tcl_Read, zero byte count} {
- read stdin 0
- } ""
- test io-32.3 {Tcl_Read, negative byte count} {
- set f [open $path(longfile) r]
- set l [list [catch {read $f -1} msg] $msg]
- close $f
- set l
- } {1 {bad argument "-1": should be "nonewline"}}
- test io-32.4 {Tcl_Read, positive byte count} {
- set f [open $path(longfile) r]
- set x [read $f 1024]
- set s [string length $x]
- unset x
- close $f
- set s
- } 1024
- test io-32.5 {Tcl_Read, multiple buffers} {
- set f [open $path(longfile) r]
- fconfigure $f -buffersize 100
- set x [read $f 1024]
- set s [string length $x]
- unset x
- close $f
- set s
- } 1024
- test io-32.6 {Tcl_Read, very large read} {
- set f1 [open $path(longfile) r]
- set z [read $f1 1000000]
- close $f1
- set l [string length $z]
- set x ok
- set z [file size $path(longfile)]
- if {$z != $l} {
- set x broken
- }
- set x
- } ok
- test io-32.7 {Tcl_Read, nonblocking, file} {nonBlockFiles} {
- set f1 [open $path(longfile) r]
- fconfigure $f1 -blocking off
- set z [read $f1 20]
- close $f1
- set l [string length $z]
- set x ok
- if {$l != 20} {
- set x broken
- }
- set x
- } ok
- test io-32.8 {Tcl_Read, nonblocking, file} {nonBlockFiles} {
- set f1 [open $path(longfile) r]
- fconfigure $f1 -blocking off
- set z [read $f1 1000000]
- close $f1
- set x ok
- set l [string length $z]
- set z [file size $path(longfile)]
- if {$z != $l} {
- set x broken
- }
- set x
- } ok
- test io-32.9 {Tcl_Read, read to end of file} {
- set f1 [open $path(longfile) r]
- set z [read $f1]
- close $f1
- set l [string length $z]
- set x ok
- set z [file size $path(longfile)]
- if {$z != $l} {
- set x broken
- }
- set x
- } ok
- test io-32.10 {Tcl_Read from a pipe} {stdio openpipe} {
- file delete $path(pipe)
- set f1 [open $path(pipe) w]
- puts $f1 {puts [gets stdin]}
- close $f1
- set f1 [open "|[list [interpreter] $path(pipe)]" r+]
- puts $f1 hello
- flush $f1
- set x [read $f1]
- close $f1
- set x
- } "hellon"
- test io-32.11 {Tcl_Read from a pipe} {stdio openpipe} {
- file delete $path(pipe)
- set f1 [open $path(pipe) w]
- puts $f1 {puts [gets stdin]}
- puts $f1 {puts [gets stdin]}
- close $f1
- set f1 [open "|[list [interpreter] $path(pipe)]" r+]
- puts $f1 hello
- flush $f1
- set x ""
- lappend x [read $f1 6]
- puts $f1 hello
- flush $f1
- lappend x [read $f1]
- close $f1
- set x
- } {{hello
- } {hello
- }}
- test io-32.12 {Tcl_Read, -nonewline} {
- file delete $path(test1)
- set f1 [open $path(test1) w]
- puts $f1 hello
- puts $f1 bye
- close $f1
- set f1 [open $path(test1) r]
- set c [read -nonewline $f1]
- close $f1
- set c
- } {hello
- bye}
- test io-32.13 {Tcl_Read, -nonewline} {
- file delete $path(test1)
- set f1 [open $path(test1) w]
- puts $f1 hello
- puts $f1 bye
- close $f1
- set f1 [open $path(test1) r]
- set c [read -nonewline $f1]
- close $f1
- list [string length $c] $c
- } {9 {hello
- bye}}
- test io-32.14 {Tcl_Read, reading in small chunks} {
- file delete $path(test1)
- set f [open $path(test1) w]
- puts $f "Two lines: this one"
- puts $f "and this one"
- close $f
- set f [open $path(test1)]
- set x [list [read $f 1] [read $f 2] [read $f]]
- close $f
- set x
- } {T wo { lines: this one
- and this one
- }}
- test io-32.15 {Tcl_Read, asking for more input than available} {
- file delete $path(test1)
- set f [open $path(test1) w]
- puts $f "Two lines: this one"
- puts $f "and this one"
- close $f
- set f [open $path(test1)]
- set x [read $f 100]
- close $f
- set x
- } {Two lines: this one
- and this one
- }
- test io-32.16 {Tcl_Read, read to end of file with -nonewline} {
- file delete $path(test1)
- set f [open $path(test1) w]
- puts $f "Two lines: this one"
- puts $f "and this one"
- close $f
- set f [open $path(test1)]
- set x [read -nonewline $f]
- close $f
- set x
- } {Two lines: this one
- and this one}
- # Test Tcl_Gets.
- test io-33.1 {Tcl_Gets, reading what was written} {
- file delete $path(test1)
- set f1 [open $path(test1) w]
- set y "first line"
- puts $f1 $y
- close $f1
- set f1 [open $path(test1) r]
- set x [gets $f1]
- set z ok
- if {"$x" != "$y"} {
- set z broken
- }
- close $f1
- set z
- } ok
- test io-33.2 {Tcl_Gets into variable} {
- set f1 [open $path(longfile) r]
- set c [gets $f1 x]
- set l [string length x]
- set z ok
- if {$l != $l} {
- set z broken
- }
- close $f1
- set z
- } ok
- test io-33.3 {Tcl_Gets from pipe} {stdio openpipe} {
- file delete $path(pipe)
- set f1 [open $path(pipe) w]
- puts $f1 {puts [gets stdin]}
- close $f1
- set f1 [open "|[list [interpreter] $path(pipe)]" r+]
- puts $f1 hello
- flush $f1
- set x [gets $f1]
- close $f1
- set z ok
- if {"$x" != "hello"} {
- set z broken
- }
- set z
- } ok
- test io-33.4 {Tcl_Gets with long line} {
- file delete $path(test3)
- set f [open $path(test3) w]
- puts -nonewline $f "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
- puts -nonewline $f "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
- puts -nonewline $f "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
- puts -nonewline $f "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
- puts $f "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
- close $f
- set f [open $path(test3)]
- set x [gets $f]
- close $f
- set x
- } {abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ}
- test io-33.5 {Tcl_Gets with long line} {
- set f [open $path(test3)]
- set x [gets $f y]
- close $f
- list $x $y
- } {260 abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ}
- test io-33.6 {Tcl_Gets and end of file} {
- file delete $path(test3)
- set f [open $path(test3) w]
- puts -nonewline $f "Test1nTest2"
- close $f
- set f [open $path(test3)]
- set x {}
- set y {}
- lappend x [gets $f y] $y
- set y {}
- lappend x [gets $f y] $y
- set y {}
- lappend x [gets $f y] $y
- close $f
- set x
- } {5 Test1 5 Test2 -1 {}}
- test io-33.7 {Tcl_Gets and bad variable} {
- set f [open $path(test3) w]
- puts $f "Line 1"
- puts $f "Line 2"
- close $f
- catch {unset x}
- set x 24
- set f [open $path(test3) r]
- set result [list [catch {gets $f x(0)} msg] $msg]
- close $f
- set result
- } {1 {can't set "x(0)": variable isn't array}}
- test io-33.8 {Tcl_Gets, exercising double buffering} {
- set f [open $path(test3) w]
- fconfigure $f -translation lf -eofchar {}
- set x ""
- for {set y 0} {$y < 99} {incr y} {set x "a$x"}
- for {set y 0} {$y < 100} {incr y} {puts $f $x}
- close $f
- set f [open $path(test3) r]
- fconfigure $f -translation lf
- for {set y 0} {$y < 100} {incr y} {gets $f}
- close $f
- set y
- } 100
- test io-33.9 {Tcl_Gets, exercising double buffering} {
- set f [open $path(test3) w]
- fconfigure $f -translation lf -eofchar {}
- set x ""
- for {set y 0} {$y < 99} {incr y} {set x "a$x"}
- for {set y 0} {$y < 200} {incr y} {puts $f $x}
- close $f
- set f [open $path(test3) r]
- fconfigure $f -translation lf
- for {set y 0} {$y < 200} {incr y} {gets $f}
- close $f
- set y
- } 200
- test io-33.10 {Tcl_Gets, exercising double buffering} {
- set f [open $path(test3) w]
- fconfigure $f -translation lf -eofchar {}
- set x ""
- for {set y 0} {$y < 99} {incr y} {set x "a$x"}
- for {set y 0} {$y < 300} {incr y} {puts $f $x}
- close $f
- set f [open $path(test3) r]
- fconfigure $f -translation lf
- for {set y 0} {$y < 300} {incr y} {gets $f}
- close $f
- set y
- } 300
- # Test Tcl_Seek and Tcl_Tell.
- test io-34.1 {Tcl_Seek to current position at start of file} {
- set f1 [open $path(longfile) r]
- seek $f1 0 current
- set c [tell $f1]
- close $f1
- set c
- } 0
- test io-34.2 {Tcl_Seek to offset from start} {
- file delete $path(test1)
- set f1 [open $path(test1) w]
- fconfigure $f1 -translation lf -eofchar {}
- puts $f1 "abcdefghijklmnopqrstuvwxyz"
- puts $f1 "abcdefghijklmnopqrstuvwxyz"
- close $f1
- set f1 [open $path(test1) r]
- seek $f1 10 start
- set c [tell $f1]
- close $f1
- set c
- } 10
- test io-34.3 {Tcl_Seek to end of file} {
- file delete $path(test1)
- set f1 [open $path(test1) w]
- fconfigure $f1 -translation lf -eofchar {}
- puts $f1 "abcdefghijklmnopqrstuvwxyz"
- puts $f1 "abcdefghijklmnopqrstuvwxyz"
- close $f1
- set f1 [open $path(test1) r]
- seek $f1 0 end
- set c [tell $f1]
- close $f1
- set c
- } 54
- test io-34.4 {Tcl_Seek to offset from end of file} {
- file delete $path(test1)
- set f1 [open $path(test1) w]
- fconfigure $f1 -translation lf -eofchar {}
- puts $f1 "abcdefghijklmnopqrstuvwxyz"
- puts $f1 "abcdefghijklmnopqrstuvwxyz"
- close $f1
- set f1 [open $path(test1) r]
- seek $f1 -10 end
- set c [tell $f1]
- close $f1
- set c
- } 44
- test io-34.5 {Tcl_Seek to offset from current position} {
- file delete $path(test1)
- set f1 [open $path(test1) w]
- fconfigure $f1 -translation lf -eofchar {}
- puts $f1 "abcdefghijklmnopqrstuvwxyz"
- puts $f1 "abcdefghijklmnopqrstuvwxyz"
- close $f1
- set f1 [open $path(test1) r]
- seek $f1 10 current
- seek $f1 10 current
- set c [tell $f1]
- close $f1
- set c
- } 20
- test io-34.6 {Tcl_Seek to offset from end of file} {
- file delete $path(test1)
- set f1 [open $path(test1) w]
- fconfigure $f1 -translation lf -eofchar {}
- puts $f1 "abcdefghijklmnopqrstuvwxyz"
- puts $f1 "abcdefghijklmnopqrstuvwxyz"
- close $f1
- set f1 [open $path(test1) r]
- seek $f1 -10 end
- set c [tell $f1]
- set r [read $f1]
- close $f1
- list $c $r
- } {44 {rstuvwxyz
- }}
- test io-34.7 {Tcl_Seek to offset from end of file, then to current position} {
- file delete $path(test1)
- set f1 [open $path(test1) w]
- fconfigure $f1 -translation lf -eofchar {}
- puts $f1 "abcdefghijklmnopqrstuvwxyz"
- puts $f1 "abcdefghijklmnopqrstuvwxyz"
- close $f1
- set f1 [open $path(test1) r]
- seek $f1 -10 end
- set c1 [tell $f1]
- set r1 [read $f1 5]
- seek $f1 0 current
- set c2 [tell $f1]
- close $f1
- list $c1 $r1 $c2
- } {44 rstuv 49}
- test io-34.8 {Tcl_Seek on pipes: not supported} {stdio openpipe} {
- set f1 [open "|[list [interpreter]]" r+]
- set x [list [catch {seek $f1 0 current} msg] $msg]
- close $f1
- regsub {".*":} $x {"":} x
- string tolower $x
- } {1 {error during seek on "": invalid argument}}
- test io-34.9 {Tcl_Seek, testing buffered input flushing} {
- file delete $path(test3)
- set f [open $path(test3) w]
- fconfigure $f -eofchar {}
- puts -nonewline $f "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
- close $f
- set f [open $path(test3) RDWR]
- set x [read $f 1]
- seek $f 3
- lappend x [read $f 1]
- seek $f 0 start
- lappend x [read $f 1]
- seek $f 10 current
- lappend x [read $f 1]
- seek $f -2 end
- lappend x [read $f 1]
- seek $f 50 end
- lappend x [read $f 1]
- seek $f 1
- lappend x [read $f 1]
- close $f
- set x
- } {a d a l Y {} b}
- set path(test3) [makeFile {} test3]
- test io-34.10 {Tcl_Seek testing flushing of buffered input} {
- set f [open $path(test3) w]
- fconfigure $f -translation lf
- puts $f xyzn123
- close $f
- set f [open $path(test3) r+]
- fconfigure $f -translation lf
- set x [gets $f]
- seek $f 0 current
- puts $f 456
- close $f
- list $x [viewFile test3]
- } "xyz {xyz
- 456}"
- test io-34.11 {Tcl_Seek testing flushing of buffered output} {
- set f [open $path(test3) w]
- puts $f xyzn123
- close $f
- set f [open $path(test3) w+]
- puts $f xyzzy
- seek $f 2
- set x [gets $f]
- close $f
- list $x [viewFile test3]
- } "zzy xyzzy"
- test io-34.12 {Tcl_Seek testing combination of write, seek back and read} {
- set f [open $path(test3) w]
- fconfigure $f -translation lf -eofchar {}
- puts $f xyzn123
- close $f
- set f [open $path(test3) a+]
- fconfigure $f -translation lf -eofchar {}
- puts $f xyzzy
- flush $f
- set x [tell $f]
- seek $f -4 cur
- set y [gets $f]
- close $f
- list $x [viewFile test3] $y
- } {14 {xyz
- 123
- xyzzy} zzy}
- test io-34.13 {Tcl_Tell at start of file} {
- file delete $path(test1)
- set f1 [open $path(test1) w]
- set p [tell $f1]
- close $f1
- set p
- } 0
- test io-34.14 {Tcl_Tell after seek to end of file} {
- file delete $path(test1)
- set f1 [open $path(test1) w]
- fconfigure $f1 -translation lf -eofchar {}
- puts $f1 "abcdefghijklmnopqrstuvwxyz"
- puts $f1 "abcdefghijklmnopqrstuvwxyz"
- close $f1
- set f1 [open $path(test1) r]
- seek $f1 0 end
- set c1 [tell $f1]
- close $f1
- set c1
- } 54
- test io-34.15 {Tcl_Tell combined with seeking} {
- file delete $path(test1)
- set f1 [open $path(test1) w]
- fconfigure $f1 -translation lf -eofchar {}
- puts $f1 "abcdefghijklmnopqrstuvwxyz"
- puts $f1 "abcdefghijklmnopqrstuvwxyz"
- close $f1
- set f1 [open $path(test1) r]
- seek $f1 10 start
- set c1 [tell $f1]
- seek $f1 10 current
- set c2 [tell $f1]
- close $f1
- list $c1 $c2
- } {10 20}
- test io-34.16 {Tcl_tell on pipe: always -1} {stdio openpipe} {
- set f1 [open "|[list [interpreter]]" r+]
- set c [tell $f1]
- close $f1
- set c
- } -1
- test io-34.17 {Tcl_Tell on pipe: always -1} {stdio openpipe} {
- set f1 [open "|[list [interpreter]]" r+]
- puts $f1 {puts hello}
- flush $f1
- set c [tell $f1]
- gets $f1
- close $f1
- set c
- } -1
- test io-34.18 {Tcl_Tell combined with seeking and reading} {
- file delete $path(test2)
- set f [open $path(test2) w]
- fconfigure $f -translation lf -eofchar {}
- puts -nonewline $f "line1nline2nline3nline4nline5n"
- close $f
- set f [open $path(test2)]
- fconfigure $f -translation lf
- set x [tell $f]
- read $f 3
- lappend x [tell $f]
- seek $f 2
- lappend x [tell $f]
- seek $f 10 current
- lappend x [tell $f]
- seek $f 0 end
- lappend x [tell $f]
- close $f
- set x
- } {0 3 2 12 30}
- test io-34.19 {Tcl_Tell combined with opening in append mode} {
- set f [open $path(test3) w]
- fconfigure $f -translation lf -eofchar {}
- puts $f "abcdefghijklmnopqrstuvwxyz"
- puts $f "abcdefghijklmnopqrstuvwxyz"
- close $f
- set f [open $path(test3) a]
- set c [tell $f]
- close $f
- set c
- } 54
- test io-34.20 {Tcl_Tell combined with writing} {
- set f [open $path(test3) w]
- set l ""
- seek $f 29 start
- lappend l [tell $f]
- puts -nonewline $f a
- seek $f 39 start
- lappend l [tell $f]
- puts -nonewline $f a
- lappend l [tell $f]
- seek $f 407 end
- lappend l [tell $f]
- close $f
- set l
- } {29 39 40 447}
- test io-34.21 {Tcl_Seek and Tcl_Tell on large files} {largefileSupport} {
- file delete $path(test3)
- set f [open $path(test3) w]
- fconfigure $f -encoding binary
- set l ""
- lappend l [tell $f]
- puts -nonewline $f abcdef
- lappend l [tell $f]
- flush $f
- lappend l [tell $f]
- # 4GB offset!
- seek $f 0x100000000
- lappend l [tell $f]
- puts -nonewline $f abcdef
- lappend l [tell $f]
- close $f
- lappend l [file size $f]
- # truncate...
- close [open $path(test3) w]
- lappend l [file size $f]
- set l
- } {0 6 6 4294967296 4294967302 4294967302 0}
- # Test Tcl_Eof
- test io-35.1 {Tcl_Eof} {
- file delete $path(test1)
- set f [open $path(test1) w]
- puts $f hello
- puts $f hello
- close $f
- set f [open $path(test1)]
- set x [eof $f]
- lappend x [eof $f]
- gets $f
- lappend x [eof $f]
- gets $f
- lappend x [eof $f]
- gets $f
- lappend x [eof $f]
- lappend x [eof $f]
- close $f
- set x
- } {0 0 0 0 1 1}
- test io-35.2 {Tcl_Eof with pipe} {stdio openpipe} {
- file delete $path(pipe)
- set f1 [open $path(pipe) w]
- puts $f1 {gets stdin}
- puts $f1 {puts hello}
- close $f1
- set f1 [open "|[list [interpreter] $path(pipe)]" r+]
- puts $f1 hello
- set x [eof $f1]
- flush $f1
- lappend x [eof $f1]
- gets $f1
- lappend x [eof $f1]
- gets $f1
- lappend x [eof $f1]
- close $f1
- set x
- } {0 0 0 1}
- test io-35.3 {Tcl_Eof with pipe} {stdio openpipe} {
- file delete $path(pipe)
- set f1 [open $path(pipe) w]
- puts $f1 {gets stdin}
- puts $f1 {puts hello}
- close $f1
- set f1 [open "|[list [interpreter] $path(pipe)]" r+]
- puts $f1 hello
- set x [eof $f1]
- flush $f1
- lappend x [eof $f1]
- gets $f1
- lappend x [eof $f1]
- gets $f1
- lappend x [eof $f1]
- gets $f1
- lappend x [eof $f1]
- gets $f1
- lappend x [eof $f1]
- close $f1
- set x
- } {0 0 0 1 1 1}
- test io-35.4 {Tcl_Eof, eof detection on nonblocking file} {nonBlockFiles} {
- file delete $path(test1)
- set f [open $path(test1) w]
- close $f
- set f [open $path(test1) r]
- fconfigure $f -blocking off
- set l ""
- lappend l [gets $f]
- lappend l [eof $f]
- close $f
- set l
- } {{} 1}
- test io-35.5 {Tcl_Eof, eof detection on nonblocking pipe} {stdio openpipe} {
- file delete $path(pipe)
- set f [open $path(pipe) w]
- puts $f {
- exit
- }
- close $f
- set f [open "|[list [interpreter] $path(pipe)]" r]
- set l ""
- lappend l [gets $f]
- lappend l [eof $f]
- close $f
- set l
- } {{} 1}
- test io-35.6 {Tcl_Eof, eof char, lf write, auto read} {
- file delete $path(test1)
- set f [open $path(test1) w]
- fconfigure $f -translation lf -eofchar x1a
- puts $f abcndef
- close $f
- set s [file size $path(test1)]
- set f [open $path(test1) r]
- fconfigure $f -translation auto -eofchar x1a
- set l [string length [read $f]]
- set e [eof $f]
- close $f
- list $s $l $e
- } {9 8 1}
- test io-35.7 {Tcl_Eof, eof char, lf write, lf read} {
- file delete $path(test1)
- set f [open $path(test1) w]
- fconfigure $f -translation lf -eofchar x1a
- puts $f abcndef
- close $f
- set s [file size $path(test1)]
- set f [open $path(test1) r]
- fconfigure $f -translation lf -eofchar x1a
- set l [string length [read $f]]
- set e [eof $f]
- close $f
- list $s $l $e
- } {9 8 1}
- test io-35.8 {Tcl_Eof, eof char, cr write, auto read} {
- file delete $path(test1)
- set f [open $path(test1) w]
- fconfigure $f -translation cr -eofchar x1a
- puts $f abcndef
- close $f
- set s [file size $path(test1)]
- set f [open $path(test1) r]
- fconfigure $f -translation auto -eofchar x1a
- set l [string length [read $f]]
- set e [eof $f]
- close $f
- list $s $l $e
- } {9 8 1}
- test io-35.9 {Tcl_Eof, eof char, cr write, cr read} {
- file delete $path(test1)
- set f [open $path(test1) w]
- fconfigure $f -translation cr -eofchar x1a
- puts $f abcndef
- close $f
- set s [file size $path(test1)]
- set f [open $path(test1) r]
- fconfigure $f -translation cr -eofchar x1a
- set l [string length [read $f]]
- set e [eof $f]
- close $f
- list $s $l $e
- } {9 8 1}
- test io-35.10 {Tcl_Eof, eof char, crlf write, auto read} {
- file delete $path(test1)
- set f [open $path(test1) w]
- fconfigure $f -translation crlf -eofchar x1a
- puts $f abcndef
- close $f
- set s [file size $path(test1)]
- set f [open $path(test1) r]
- fconfigure $f -translation auto -eofchar x1a
- set l [string length [read $f]]
- set e [eof $f]
- close $f
- list $s $l $e
- } {11 8 1}
- test io-35.11 {Tcl_Eof, eof char, crlf write, crlf read} {
- file delete $path(test1)
- set f [open $path(test1) w]
- fconfigure $f -translation crlf -eofchar x1a
- puts $f abcndef
- close $f
- set s [file size $path(test1)]
- set f [open $path(test1) r]
- fconfigure $f -translation crlf -eofchar x1a
- set l [string length [read $f]]
- set e [eof $f]
- close $f
- list $s $l $e
- } {11 8 1}
- test io-35.12 {Tcl_Eof, eof char in middle, lf write, auto read} {
- file delete $path(test1)
- set f [open $path(test1) w]
- fconfigure $f -translation lf -eofchar {}
- set i [format abcndefn%cqrsnuvw 26]
- puts $f $i
- close $f
- set c [file size $path(test1)]
- set f [open $path(test1) r]
- fconfigure $f -translation auto -eofchar x1a
- set l [string length [read $f]]
- set e [eof $f]
- close $f
- list $c $l $e
- } {17 8 1}
- test io-35.13 {Tcl_Eof, eof char in middle, lf write, lf read} {
- file delete $path(test1)
- set f [open $path(test1) w]
- fconfigure $f -translation lf -eofchar {}
- set i [format abcndefn%cqrsnuvw 26]
- puts $f $i
- close $f
- set c [file size $path(test1)]
- set f [open $path(test1) r]
- fconfigure $f -translation lf -eofchar x1a
- set l [string length [read $f]]
- set e [eof $f]
- close $f
- list $c $l $e
- } {17 8 1}
- test io-35.14 {Tcl_Eof, eof char in middle, cr write, auto read} {
- file delete $path(test1)
- set f [open $path(test1) w]
- fconfigure $f -translation cr -eofchar {}
- set i [format abcndefn%cqrsnuvw 26]
- puts $f $i
- close $f
- set c [file size $path(test1)]
- set f [open $path(test1) r]
- fconfigure $f -translation auto -eofchar x1a
- set l [string length [read $f]]
- set e [eof $f]
- close $f
- list $c $l $e
- } {17 8 1}
- test io-35.15 {Tcl_Eof, eof char in middle, cr write, cr read} {
- file delete $path(test1)
- set f [open $path(test1) w]
- fconfigure $f -translation cr -eofchar {}
- set i [format abcndefn%cqrsnuvw 26]
- puts $f $i
- close $f
- set c [file size $path(test1)]
- set f [open $path(test1) r]
- fconfigure $f -translation cr -eofchar x1a
- set l [string length [read $f]]
- set e [eof $f]
- close $f
- list $c $l $e
- } {17 8 1}
- test io-35.16 {Tcl_Eof, eof char in middle, crlf write, auto read} {
- file delete $path(test1)
- set f [open $path(test1) w]
- fconfigure $f -translation crlf -eofchar {}
- set i [format abcndefn%cqrsnuvw 26]
- puts $f $i
- close $f
- set c [file size $path(test1)]
- set f [open $path(test1) r]
- fconfigure $f -translation auto -eofchar x1a
- set l [string length [read $f]]
- set e [eof $f]
- close $f
- list $c $l $e
- } {21 8 1}
- test io-35.17 {Tcl_Eof, eof char in middle, crlf write, crlf read} {
- file delete $path(test1)
- set f [open $path(test1) w]
- fconfigure $f -translation crlf -eofchar {}
- set i [format abcndefn%cqrsnuvw 26]
- puts $f $i
- close $f
- set c [file size $path(test1)]
- set f [open $path(test1) r]
- fconfigure $f -translation crlf -eofchar x1a
- set l [string length [read $f]]
- set e [eof $f]
- close $f
- list $c $l $e
- } {21 8 1}
- # Test Tcl_InputBlocked
- test io-36.1 {Tcl_InputBlocked on nonblocking pipe} {stdio openpipe} {
- set f1 [open "|[list [interpreter]]" r+]
- puts $f1 {puts hello_from_pipe}
- flush $f1
- gets $f1
- fconfigure $f1 -blocking off -buffering full
- puts $f1 {puts hello}
- set x ""
- lappend x [gets $f1]
- lappend x [fblocked $f1]
- flush $f1
- after 200
- lappend x [gets $f1]
- lappend x [fblocked $f1]
- lappend x [gets $f1]
- lappend x [fblocked $f1]
- close $f1
- set x
- } {{} 1 hello 0 {} 1}
- test io-36.2 {Tcl_InputBlocked on blocking pipe} {stdio openpipe} {
- set f1 [open "|[list [interpreter]]" r+]
- fconfigure $f1 -buffering line
- puts $f1 {puts hello_from_pipe}
- set x ""
- lappend x [gets $f1]
- lappend x [fblocked $f1]
- puts $f1 {exit}
- lappend x [gets $f1]
- lappend x [fblocked $f1]
- lappend x [eof $f1]
- close $f1
- set x
- } {hello_from_pipe 0 {} 0 1}
- test io-36.3 {Tcl_InputBlocked vs files, short read} {
- file delete $path(test1)
- set f [open $path(test1) w]
- puts $f abcdefghijklmnop
- close $f
- set f [open $path(test1) r]
- set l ""
- lappend l [fblocked $f]
- lappend l [read $f 3]
- lappend l [fblocked $f]
- lappend l [read -nonewline $f]
- lappend l [fblocked $f]
- lappend l [eof $f]
- close $f
- set l
- } {0 abc 0 defghijklmnop 0 1}
- test io-36.4 {Tcl_InputBlocked vs files, event driven read} {fileevent} {
- proc in {f} {
- variable l
- variable x
- lappend l [read $f 3]
- if {[eof $f]} {lappend l eof; close $f; set x done}
- }
- file delete $path(test1)
- set f [open $path(test1) w]
- puts $f abcdefghijklmnop
- close $f
- set f [open $path(test1) r]
- set l ""
- fileevent $f readable [namespace code [list in $f]]
- variable x
- vwait [namespace which -variable x]
- set l
- } {abc def ghi jkl mno {p
- } eof}
- test io-36.5 {Tcl_InputBlocked vs files, short read, nonblocking} {nonBlockFiles} {
- file delete $path(test1)
- set f [open $path(test1) w]
- puts $f abcdefghijklmnop
- close $f
- set f [open $path(test1) r]
- fconfigure $f -blocking off
- set l ""
- lappend l [fblocked $f]
- lappend l [read $f 3]
- lappend l [fblocked $f]
- lappend l [read -nonewline $f]
- lappend l [fblocked $f]
- lappend l [eof $f]
- close $f
- set l
- } {0 abc 0 defghijklmnop 0 1}
- test io-36.6 {Tcl_InputBlocked vs files, event driven read} {nonBlockFiles fileevent} {
- proc in {f} {
- variable l
- variable x
- lappend l [read $f 3]
- if {[eof $f]} {lappend l eof; close $f; set x done}
- }
- file delete $path(test1)
- set f [open $path(test1) w]
- puts $f abcdefghijklmnop
- close $f
- set f [open $path(test1) r]
- fconfigure $f -blocking off
- set l ""
- fileevent $f readable [namespace code [list in $f]]
- variable x
- vwait [namespace which -variable x]
- set l
- } {abc def ghi jkl mno {p
- } eof}
- # Test Tcl_InputBuffered
- test io-37.1 {Tcl_InputBuffered} {testchannel} {
- set f [open $path(longfile) r]
- fconfigure $f -buffersize 4096
- read $f 3
- set l ""
- lappend l [testchannel inputbuffered $f]
- lappend l [tell $f]
- close $f
- set l
- } {4093 3}
- test io-37.2 {Tcl_InputBuffered, test input flushing on seek} {testchannel} {
- set f [open $path(longfile) r]
- fconfigure $f -buffersize 4096
- read $f 3
- set l ""
- lappend l [testchannel inputbuffered $f]
- lappend l [tell $f]
- seek $f 0 current
- lappend l [testchannel inputbuffered $f]
- lappend l [tell $f]
- close $f
- set l
- } {4093 3 0 3}
- # Test Tcl_SetChannelBufferSize, Tcl_GetChannelBufferSize
- test io-38.1 {Tcl_GetChannelBufferSize, default buffer size} {
- set f [open $path(longfile) r]
- set s [fconfigure $f -buffersize]
- close $f
- set s
- } 4096
- test io-38.2 {Tcl_SetChannelBufferSize, Tcl_GetChannelBufferSize} {
- set f [open $path(longfile) r]
- set l ""
- lappend l [fconfigure $f -buffersize]
- fconfigure $f -buffersize 10000
- lappend l [fconfigure $f -buffersize]
- fconfigure $f -buffersize 1
- lappend l [fconfigure $f -buffersize]
- fconfigure $f -buffersize -1
- lappend l [fconfigure $f -buffersize]
- fconfigure $f -buffersize 0
- lappend l [fconfigure $f -buffersize]
- fconfigure $f -buffersize 100000
- lappend l [fconfigure $f -buffersize]
- fconfigure $f -buffersize 10000000
- lappend l [fconfigure $f -buffersize]
- close $f
- set l
- } {4096 10000 1 1 1 100000 100000}
- test io-38.3 {Tcl_SetChannelBufferSize, changing buffersize between reads} {
- # This test crashes the interp if Bug #427196 is not fixed
- set chan [open [info script] r]
- fconfigure $chan -buffersize 10
- set var [read $chan 2]
- fconfigure $chan -buffersize 32
- append var [read $chan]
- close $chan
- } {}
- # Test Tcl_SetChannelOption, Tcl_GetChannelOption
- test io-39.1 {Tcl_GetChannelOption} {
- file delete $path(test1)
- set f1 [open $path(test1) w]
- set x [fconfigure $f1 -blocking]
- close $f1
- set x
- } 1
- #
- # Test 17.2 was removed.
- #
- test io-39.2 {Tcl_GetChannelOption} {
- file delete $path(test1)
- set f1 [open $path(test1) w]
- set x [fconfigure $f1 -buffering]
- close $f1
- set x
- } full
- test io-39.3 {Tcl_GetChannelOption} {
- file delete $path(test1)
- set f1 [open $path(test1) w]
- fconfigure $f1 -buffering line
- set x [fconfigure $f1 -buffering]
- close $f1
- set x
- } line
- test io-39.4 {Tcl_GetChannelOption, Tcl_SetChannelOption} {
- file delete $path(test1)
- set f1 [open $path(test1) w]
- set l ""
- lappend l [fconfigure $f1 -buffering]
- fconfigure $f1 -buffering line
- lappend l [fconfigure $f1 -buffering]
- fconfigure $f1 -buffering none
- lappend l [fconfigure $f1 -buffering]
- fconfigure $f1 -buffering line
- lappend l [fconfigure $f1 -buffering]
- fconfigure $f1 -buffering full
- lappend l [fconfigure $f1 -buffering]
- close $f1
- set l
- } {full line none line full}
- test io-39.5 {Tcl_GetChannelOption, invariance} {
- file delete $path(test1)
- set f1 [open $path(test1) w]
- set l ""
- lappend l [fconfigure $f1 -buffering]
- lappend l [list [catch {fconfigure $f1 -buffering green} msg] $msg]
- lappend l [fconfigure $f1 -buffering]
- close $f1
- set l
- } {full {1 {bad value for -buffering: must be one of full, line, or none}} full}
- test io-39.6 {Tcl_SetChannelOption, multiple options} {
- file delete $path(test1)
- set f1 [open $path(test1) w]
- fconfigure $f1 -translation lf -buffering line
- puts $f1 hello
- puts $f1 bye
- set x [file size $path(test1)]
- close $f1
- set x
- } 10
- test io-39.7 {Tcl_SetChannelOption, buffering, translation} {
- file delete $path(test1)
- set f1 [open $path(test1) w]
- fconfigure $f1 -translation lf
- puts $f1 hello
- puts $f1 bye
- set x ""
- fconfigure $f1 -buffering line
- lappend x [file size $path(test1)]
- puts $f1 really_bye
- lappend x [file size $path(test1)]
- close $f1
- set x
- } {0 21}
- test io-39.8 {Tcl_SetChannelOption, different buffering options} {
- file delete $path(test1)
- set f1 [open $path(test1) w]
- set l ""
- fconfigure $f1 -translation lf -buffering none -eofchar {}
- puts -nonewline $f1 hello
- lappend l [file size $path(test1)]
- puts -nonewline $f1 hello
- lappend l [file size $path(test1)]
- fconfigure $f1 -buffering full
- puts -nonewline $f1 hello
- lappend l [file size $path(test1)]
- fconfigure $f1 -buffering none
- lappend l [file size $path(test1)]
- puts -nonewline $f1 hello
- lappend l [file size $path(test1)]
- close $f1
- lappend l [file size $path(test1)]
- set l
- } {5 10 10 10 20 20}
- test io-39.9 {Tcl_SetChannelOption, blocking mode} {nonBlockFiles} {
- file delete $path(test1)
- set f1 [open $path(test1) w]
- close $f1
- set f1 [open $path(test1) r]
- set x ""
- lappend x [fconfigure $f1 -blocking]
- fconfigure $f1 -blocking off
- lappend x [fconfigure $f1 -blocking]
- lappend x [gets $f1]
- lappend x [read $f1 1000]
- lappend x [fblocked $f1]
- lappend x [eof $f1]
- close $f1
- set x
- } {1 0 {} {} 0 1}
- test io-39.10 {Tcl_SetChannelOption, blocking mode} {stdio openpipe} {
- file delete $path(pipe)
- set f1 [open $path(pipe) w]
- puts $f1 {
- gets stdin
- after 100
- puts hi
- gets stdin
- }
- close $f1
- set x ""
- set f1 [open "|[list [interpreter] $path(pipe)]" r+]
- fconfigure $f1 -blocking off -buffering line
- lappend x [fconfigure $f1 -blocking]
- lappend x [gets $f1]
- lappend x [fblocked $f1]
- fconfigure $f1 -blocking on
- puts $f1 hello
- fconfigure $f1 -blocking off
- lappend x [gets $f1]
- lappend x [fblocked $f1]
- fconfigure $f1 -blocking on
- puts $f1 bye
- fconfigure $f1 -blocking off
- lappend x [gets $f1]
- lappend x [fblocked $f1]
- fconfigure $f1 -blocking on
- lappend x [fconfigure $f1 -blocking]
- lappend x [gets $f1]
- lappend x [fblocked $f1]
- lappend x [eof $f1]
- lappend x [gets $f1]
- lappend x [eof $f1]
- close $f1
- set x
- } {0 {} 1 {} 1 {} 1 1 hi 0 0 {} 1}
- test io-39.11 {Tcl_SetChannelOption, Tcl_GetChannelOption, buffer size} {
- file delete $path(test1)
- set f [open $path(test1) w]
- fconfigure $f -buffersize -10
- set x [fconfigure $f -buffersize]
- close $f
- set x
- } 4096
- test io-39.12 {Tcl_SetChannelOption, Tcl_GetChannelOption buffer size} {
- file delete $path(test1)
- set f [open $path(test1) w]
- fconfigure $f -buffersize 10000000
- set x [fconfigure $f -buffersize]
- close $f
- set x
- } 4096
- test io-39.13 {Tcl_SetChannelOption, Tcl_GetChannelOption, buffer size} {
- file delete $path(test1)
- set f [open $path(test1) w]
- fconfigure $f -buffersize 40000
- set x [fconfigure $f -buffersize]
- close $f
- set x
- } 40000
- test io-39.14 {Tcl_SetChannelOption: -encoding, binary & utf-8} {
- file delete $path(test1)
- set f [open $path(test1) w]
- fconfigure $f -encoding {}
- puts -nonewline $f xe7x89xa6
- close $f
- set f [open $path(test1) r]
- fconfigure $f -encoding utf-8
- set x [read $f]
- close $f
- set x
- } u7266
- test io-39.15 {Tcl_SetChannelOption: -encoding, binary & utf-8} {
- file delete $path(test1)
- set f [open $path(test1) w]
- fconfigure $f -encoding binary
- puts -nonewline $f xe7x89xa6
- close $f
- set f [open $path(test1) r]
- fconfigure $f -encoding utf-8
- set x [read $f]
- close $f
- set x
- } u7266
- test io-39.16 {Tcl_SetChannelOption: -encoding, errors} {
- file delete $path(test1)
- set f [open $path(test1) w]
- set result [list [catch {fconfigure $f -encoding foobar} msg] $msg]
- close $f
- set result
- } {1 {unknown encoding "foobar"}}
- test io-39.17 {Tcl_SetChannelOption: -encoding, clearing CHANNEL_NEED_MORE_DATA} {stdio openpipe fileevent} {
- set f [open "|[list [interpreter] $path(cat)]" r+]
- fconfigure $f -encoding binary
- puts -nonewline $f "xe7"
- flush $f
- fconfigure $f -encoding utf-8 -blocking 0
- variable x {}
- fileevent $f readable [namespace code { lappend x [read $f] }]
- vwait [namespace which -variable x]
- after 300 [namespace code { lappend x timeout }]
- vwait [namespace which -variable x]
- fconfigure $f -encoding utf-8
- vwait [namespace which -variable x]
- after 300 [namespace code { lappend x timeout }]
- vwait [namespace which -variable x]
- fconfigure $f -encoding binary
- vwait [namespace which -variable x]
- after 300 [namespace code { lappend x timeout }]
- vwait [namespace which -variable x]
- close $f
- set x
- } "{} timeout {} timeout xe7 timeout"
- test io-39.18 {Tcl_SetChannelOption, setting read mode independently}
- {socket} {
- proc accept {s a p} {close $s}
- set s1 [socket -server [namespace code accept] 0]
- set port [lindex [fconfigure $s1 -sockname] 2]
- set s2 [socket 127.0.0.1 $port]
- update
- fconfigure $s2 -translation {auto lf}
- set modes [fconfigure $s2 -translation]
- close $s1
- close $s2
- set modes
- } {auto lf}
- test io-39.19 {Tcl_SetChannelOption, setting read mode independently}
- {socket} {
- proc accept {s a p} {close $s}
- set s1 [socket -server [namespace code accept] 0]
- set port [lindex [fconfigure $s1 -sockname] 2]
- set s2 [socket 127.0.0.1 $port]
- update
- fconfigure $s2 -translation {auto crlf}
- set modes [fconfigure $s2 -translation]
- close $s1
- close $s2
- set modes
- } {auto crlf}
- test io-39.20 {Tcl_SetChannelOption, setting read mode independently}
- {socket} {
- proc accept {s a p} {close $s}
- set s1 [socket -server [namespace code accept] 0]
- set port [lindex [fconfigure $s1 -sockname] 2]
- set s2 [socket 127.0.0.1 $port]
- update
- fconfigure $s2 -translation {auto cr}
- set modes [fconfigure $s2 -translation]
- close $s1
- close $s2
- set modes
- } {auto cr}
- test io-39.21 {Tcl_SetChannelOption, setting read mode independently}
- {socket} {
- proc accept {s a p} {close $s}
- set s1 [socket -server [namespace code accept] 0]
- set port [lindex [fconfigure $s1 -sockname] 2]
- set s2 [socket 127.0.0.1 $port]
- update
- fconfigure $s2 -translation {auto auto}
- set modes [fconfigure $s2 -translation]
- close $s1
- close $s2
- set modes
- } {auto crlf}
- test io-39.22 {Tcl_SetChannelOption, invariance} {unixOnly} {
- file delete $path(test1)
- set f1 [open $path(test1) w+]
- set l ""
- lappend l [fconfigure $f1 -eofchar]
- fconfigure $f1 -eofchar {ON GO}
- lappend l [fconfigure $f1 -eofchar]
- fconfigure $f1 -eofchar D
- lappend l [fconfigure $f1 -eofchar]
- close $f1
- set l
- } {{{} {}} {O G} {D D}}
- test io-39.22a {Tcl_SetChannelOption, invariance} {
- file delete $path(test1)
- set f1 [open $path(test1) w+]
- set l [list]
- fconfigure $f1 -eofchar {ON GO}
- lappend l [fconfigure $f1 -eofchar]
- fconfigure $f1 -eofchar D
- lappend l [fconfigure $f1 -eofchar]
- lappend l [list [catch {fconfigure $f1 -eofchar {1 2 3}} msg] $msg]
- close $f1
- set l
- } {{O G} {D D} {1 {bad value for -eofchar: should be a list of zero, one, or two elements}}}
- test io-39.23 {Tcl_GetChannelOption, server socket is not readable or
- writeable, it should still have valid -eofchar and -translation options } {
- set l [list]
- set sock [socket -server [namespace code accept] 0]
- lappend l [fconfigure $sock -eofchar] [fconfigure $sock -translation]
- close $sock
- set l
- } {{{}} auto}
- test io-39.24 {Tcl_SetChannelOption, server socket is not readable or
- writable so we can't change -eofchar or -translation } {
- set l [list]
- set sock [socket -server [namespace code accept] 0]
- fconfigure $sock -eofchar D -translation lf
- lappend l [fconfigure $sock -eofchar] [fconfigure $sock -translation]
- close $sock
- set l
- } {{{}} auto}
- test io-40.1 {POSIX open access modes: RDWR} {
- file delete $path(test3)
- set f [open $path(test3) w]
- puts $f xyzzy
- close $f
- set f [open $path(test3) RDWR]
- puts -nonewline $f "ab"
- seek $f 0 current
- set x [gets $f]
- close $f
- set f [open $path(test3) r]
- lappend x [gets $f]
- close $f
- set x
- } {zzy abzzy}
- test io-40.2 {POSIX open access modes: CREAT} {unixOnly} {
- file delete $path(test3)
- set f [open $path(test3) {WRONLY CREAT} 0600]
- file stat $path(test3) stats
- set x [format "0%o" [expr $stats(mode)&0777]]
- puts $f "line 1"
- close $f
- set f [open $path(test3) r]
- lappend x [gets $f]
- close $f
- set x
- } {0600 {line 1}}
- # some tests can only be run is umask is 2
- # if "umask" cannot be run, the tests will be skipped.
- catch {testConstraint umask2 [expr {[exec umask] == 2}]}
- test io-40.3 {POSIX open access modes: CREAT} {unixOnly umask2} {
- # This test only works if your umask is 2, like ouster's.
- file delete $path(test3)
- set f [open $path(test3) {WRONLY CREAT}]
- close $f
- file stat test3 stats
- format "0%o" [expr $stats(mode)&0777]
- } 0664
- test io-40.4 {POSIX open access modes: CREAT} {
- file delete $path(test3)
- set f [open $path(test3) w]
- fconfigure $f -eofchar {}
- puts $f xyzzy
- close $f
- set f [open $path(test3) {WRONLY CREAT}]
- fconfigure $f -eofchar {}
- puts -nonewline $f "ab"
- close $f
- set f [open $path(test3) r]
- set x [gets $f]
- close $f
- set x
- } abzzy
- test io-40.5 {POSIX open access modes: APPEND} {
- file delete $path(test3)
- set f [open $path(test3) w]
- fconfigure $f -translation lf -eofchar {}
- puts $f xyzzy
- close $f
- set f [open $path(test3) {WRONLY APPEND}]
- fconfigure $f -translation lf
- puts $f "new line"
- seek $f 0
- puts $f "abc"
- close $f
- set f [open $path(test3) r]
- fconfigure $f -translation lf
- set x ""
- seek $f 6 current
- lappend x [gets $f]
- lappend x [gets $f]
- close $f
- set x
- } {{new line} abc}
- test io-40.6 {POSIX open access modes: EXCL} -match regexp -body {
- file delete $path(test3)
- set f [open $path(test3) w]
- puts $f xyzzy
- close $f
- open $path(test3) {WRONLY CREAT EXCL}
- } -returnCodes error -result {(?i)couldn't open ".*test3": file (already )?exists}
- test io-40.7 {POSIX open access modes: EXCL} {
- file delete $path(test3)
- set f [open $path(test3) {WRONLY CREAT EXCL}]
- fconfigure $f -eofchar {}
- puts $f "A test line"
- close $f
- viewFile test3
- } {A test line}
- test io-40.8 {POSIX open access modes: TRUNC} {
- file delete $path(test3)
- set f [open $path(test3) w]
- puts $f xyzzy
- close $f
- set f [open $path(test3) {WRONLY TRUNC}]
- puts $f abc
- close $f
- set f [open $path(test3) r]
- set x [gets $f]
- close $f
- set x
- } abc
- test io-40.9 {POSIX open access modes: NONBLOCK} {nonPortable macOrUnix} {
- file delete $path(test3)
- set f [open $path(test3) {WRONLY NONBLOCK CREAT}]
- puts $f "NONBLOCK test"
- close $f
- set f [open $path(test3) r]
- set x [gets $f]
- close $f
- set x
- } {NONBLOCK test}
- test io-40.10 {POSIX open access modes: RDONLY} {
- set f [open $path(test1) w]
- puts $f "two lines: this one"
- puts $f "and this"
- close $f
- set f [open $path(test1) RDONLY]
- set x [list [gets $f] [catch {puts $f Test} msg] $msg]
- close $f
- string compare [string tolower $x]
- [list {two lines: this one} 1
- [format "channel "%s" wasn't opened for writing" $f]]
- } 0
- test io-40.11 {POSIX open access modes: RDONLY} -match regexp -body {
- file delete $path(test3)
- open $path(test3) RDONLY
- } -returnCodes error -result {(?i)couldn't open ".*test3": no such file or directory}
- test io-40.12 {POSIX open access modes: WRONLY} -match regexp -body {
- file delete $path(test3)
- open $path(test3) WRONLY
- } -returnCodes error -result {(?i)couldn't open ".*test3": no such file or directory}
- test io-40.13 {POSIX open access modes: WRONLY} {
- makeFile xyzzy test3
- set f [open $path(test3) WRONLY]
- fconfigure $f -eofchar {}
- puts -nonewline $f "ab"
- seek $f 0 current
- set x [list [catch {gets $f} msg] $msg]
- close $f
- lappend x [viewFile test3]
- string compare [string tolower $x]
- [list 1 "channel "$f" wasn't opened for reading" abzzy]
- } 0
- test io-40.14 {POSIX open access modes: RDWR} -match regexp -body {
- file delete $path(test3)
- open $path(test3) RDWR
- } -returnCodes error -result {(?i)couldn't open ".*test3": no such file or directory}
- test io-40.15 {POSIX open access modes: RDWR} {
- makeFile xyzzy test3
- set f [open $path(test3) RDWR]
- puts -nonewline $f "ab"
- seek $f 0 current
- set x [gets $f]
- close $f
- lappend x [viewFile test3]
- } {zzy abzzy}
- if {![file exists ~/_test_] && [file writable ~]} {
- test io-40.16 {tilde substitution in open} -setup {
- makeFile {Some text} _test_ ~
- } -body {
- file exists [file join $env(HOME) _test_]
- } -cleanup {
- removeFile _test_ ~
- } -result 1
- }
- test io-40.17 {tilde substitution in open} {
- set home $env(HOME)
- unset env(HOME)
- set x [list [catch {open ~/foo} msg] $msg]
- set env(HOME) $home
- set x
- } {1 {couldn't find HOME environment variable to expand path}}
- test io-41.1 {Tcl_FileeventCmd: errors} {fileevent} {
- list [catch {fileevent foo} msg] $msg
- } {1 {wrong # args: should be "fileevent channelId event ?script?"}}
- test io-41.2 {Tcl_FileeventCmd: errors} {fileevent} {
- list [catch {fileevent foo bar baz q} msg] $msg
- } {1 {wrong # args: should be "fileevent channelId event ?script?"}}
- test io-41.3 {Tcl_FileeventCmd: errors} {fileevent} {
- list [catch {fileevent gorp readable} msg] $msg
- } {1 {can not find channel named "gorp"}}
- test io-41.4 {Tcl_FileeventCmd: errors} {fileevent} {
- list [catch {fileevent gorp writable} msg] $msg
- } {1 {can not find channel named "gorp"}}
- test io-41.5 {Tcl_FileeventCmd: errors} {fileevent} {
- list [catch {fileevent gorp who-knows} msg] $msg
- } {1 {bad event name "who-knows": must be readable or writable}}
- #
- # Test fileevent on a file
- #
- set path(foo) [makeFile {} foo]
- set f [open $path(foo) w+]
- test io-42.1 {Tcl_FileeventCmd: creating, deleting, querying} {fileevent} {
- list [fileevent $f readable] [fileevent $f writable]
- } {{} {}}
- test io-42.2 {Tcl_FileeventCmd: replacing} {fileevent} {
- set result {}
- fileevent $f r "first script"
- lappend result [fileevent $f readable]
- fileevent $f r "new script"
- lappend result [fileevent $f readable]
- fileevent $f r "yet another"
- lappend result [fileevent $f readable]
- fileevent $f r ""
- lappend result [fileevent $f readable]
- } {{first script} {new script} {yet another} {}}
- test io-42.3 {Tcl_FileeventCmd: replacing, with NULL chars in script} {fileevent} {
- set result {}
- fileevent $f r "first scr ipt"
- lappend result [string length [fileevent $f readable]]
- fileevent $f r "new scr ipt"
- lappend result [string length [fileevent $f readable]]
- fileevent $f r "yet ano ther"
- lappend result [string length [fileevent $f readable]]
- fileevent $f r ""
- lappend result [fileevent $f readable]
- } {13 11 12 {}}
- test io-43.1 {Tcl_FileeventCmd: creating, deleting, querying} {stdio unixExecs fileevent} {
- set result {}
- fileevent $f readable "script 1"
- lappend result [fileevent $f readable] [fileevent $f writable]
- fileevent $f writable "write script"
- lappend result [fileevent $f readable] [fileevent $f writable]
- fileevent $f readable {}
- lappend result [fileevent $f readable] [fileevent $f writable]
- fileevent $f writable {}
- lappend result [fileevent $f readable] [fileevent $f writable]
- } {{script 1} {} {script 1} {write script} {} {write script} {} {}}
- test io-43.2 {Tcl_FileeventCmd: deleting when many present} -setup {
- set f2 [open "|[list cat -u]" r+]
- set f3 [open "|[list cat -u]" r+]
- } -constraints {stdio unixExecs fileevent openpipe} -body {
- set result {}
- lappend result [fileevent $f r] [fileevent $f2 r] [fileevent $f3 r]
- fileevent $f r "read f"
- fileevent $f2 r "read f2"
- fileevent $f3 r "read f3"
- lappend result [fileevent $f r] [fileevent $f2 r] [fileevent $f3 r]
- fileevent $f2 r {}
- lappend result [fileevent $f r] [fileevent $f2 r] [fileevent $f3 r]
- fileevent $f3 r {}
- lappend result [fileevent $f r] [fileevent $f2 r] [fileevent $f3 r]
- fileevent $f r {}
- lappend result [fileevent $f r] [fileevent $f2 r] [fileevent $f3 r]
- } -cleanup {
- catch {close $f2}
- catch {close $f3}
- } -result {{} {} {} {read f} {read f2} {read f3} {read f} {} {read f3} {read f} {} {} {} {} {}}
- test io-44.1 {FileEventProc procedure: normal read event} -setup {
- set f2 [open "|[list cat -u]" r+]
- set f3 [open "|[list cat -u]" r+]
- } -constraints {stdio unixExecs fileevent openpipe} -body {
- fileevent $f2 readable [namespace code {
- set x [gets $f2]; fileevent $f2 readable {}
- }]
- puts $f2 text; flush $f2
- variable x initial
- vwait [namespace which -variable x]
- set x
- } -cleanup {
- catch {close $f2}
- catch {close $f3}
- } -result {text}
- test io-44.2 {FileEventProc procedure: error in read event} -setup {
- set f2 [open "|[list cat -u]" r+]
- set f3 [open "|[list cat -u]" r+]
- } -constraints {stdio unixExecs fileevent openpipe} -body {
- proc ::bgerror args "set [namespace which -variable x] $args"
- fileevent $f2 readable {error bogus}
- puts $f2 text; flush $f2
- variable x initial
- vwait [namespace which -variable x]
- rename ::bgerror {}
- list $x [fileevent $f2 readable]
- } -cleanup {
- catch {close $f2}
- catch {close $f3}
- } -result {bogus {}}
- test io-44.3 {FileEventProc procedure: normal write event} -setup {
- set f2 [open "|[list cat -u]" r+]
- set f3 [open "|[list cat -u]" r+]
- } -constraints {stdio unixExecs fileevent openpipe} -body {
- fileevent $f2 writable [namespace code {
- lappend x "triggered"
- incr count -1
- if {$count <= 0} {
- fileevent $f2 writable {}
- }
- }]
- variable x initial
- set count 3
- vwait [namespace which -variable x]
- vwait [namespace which -variable x]
- vwait [namespace which -variable x]
- set x
- } -cleanup {
- catch {close $f2}
- catch {close $f3}
- } -result {initial triggered triggered triggered}
- test io-44.4 {FileEventProc procedure: eror in write event} -setup {
- set f2 [open "|[list cat -u]" r+]
- set f3 [open "|[list cat -u]" r+]
- } -constraints {stdio unixExecs fileevent openpipe} -body {
- proc ::bgerror args "set [namespace which -variable x] $args"
- fileevent $f2 writable {error bad-write}
- variable x initial
- vwait [namespace which -variable x]
- rename ::bgerror {}
- list $x [fileevent $f2 writable]
- } -cleanup {
- catch {close $f2}
- catch {close $f3}
- } -result {bad-write {}}
- test io-44.5 {FileEventProc procedure: end of file} {stdio unixExecs openpipe fileevent} {
- set f4 [open "|[list [interpreter] $path(cat) << foo]" r]
- fileevent $f4 readable [namespace code {
- if {[gets $f4 line] < 0} {
- lappend x eof
- fileevent $f4 readable {}
- } else {
- lappend x $line
- }
- }]
- variable x initial
- vwait [namespace which -variable x]
- vwait [namespace which -variable x]
- close $f4
- set x
- } {initial foo eof}
- close $f
- makeFile "foo bar" foo
- test io-45.1 {DeleteFileEvent, cleanup on close} {fileevent} {
- set f [open $path(foo) r]
- fileevent $f readable [namespace code {
- lappend x "binding triggered: "[gets $f]""
- fileevent $f readable {}
- }]
- close $f
- set x initial
- after 100 [namespace code { set y done }]
- variable y
- vwait [namespace which -variable y]
- set x
- } {initial}
- test io-45.2 {DeleteFileEvent, cleanup on close} {fileevent} {
- set f [open $path(foo) r]
- set f2 [open $path(foo) r]
- fileevent $f readable [namespace code {
- lappend x "f triggered: "[gets $f]""
- fileevent $f readable {}
- }]
- fileevent $f2 readable [namespace code {
- lappend x "f2 triggered: "[gets $f2]""
- fileevent $f2 readable {}
- }]
- close $f
- variable x initial
- vwait [namespace which -variable x]
- close $f2
- set x
- } {initial {f2 triggered: "foo bar"}}
- test io-45.3 {DeleteFileEvent, cleanup on close} {fileevent} {
- set f [open $path(foo) r]
- set f2 [open $path(foo) r]
- set f3 [open $path(foo) r]
- fileevent $f readable {f script}
- fileevent $f2 readable {f2 script}
- fileevent $f3 readable {f3 script}
- set x {}
- close $f2
- lappend x [catch {fileevent $f readable} msg] $msg
- [catch {fileevent $f2 readable}]
- [catch {fileevent $f3 readable} msg] $msg
- close $f3
- lappend x [catch {fileevent $f readable} msg] $msg
- [catch {fileevent $f2 readable}]
- [catch {fileevent $f3 readable}]
- close $f
- lappend x [catch {fileevent $f readable}]
- [catch {fileevent $f2 readable}]
- [catch {fileevent $f3 readable}]
- } {0 {f script} 1 0 {f3 script} 0 {f script} 1 1 1 1 1}
- # Execute these tests only if the "testfevent" command is present.
- testConstraint testfevent [llength [info commands testfevent]]
- test io-46.1 {Tcl event loop vs multiple interpreters} {testfevent fileevent} {
- testfevent create
- set script "set f [[list open $path(foo) r]]n"
- append script {
- set x "no event"
- fileevent $f readable [namespace code {
- set x "f triggered: [gets $f]"
- fileevent $f readable {}
- }]
- }
- testfevent cmd $script
- after 1 ;# We must delay because Windows takes a little time to notice
- update
- testfevent cmd {close $f}
- list [testfevent cmd {set x}] [testfevent cmd {info commands after}]
- } {{f triggered: foo bar} after}
- test io-46.2 {Tcl event loop vs multiple interpreters} testfevent {
- testfevent create
- testfevent cmd {
- variable x 0
- after 100 {set x triggered}
- vwait [namespace which -variable x]
- set x
- }
- } {triggered}
- test io-46.3 {Tcl event loop vs multiple interpreters} testfevent {
- testfevent create
- testfevent cmd {
- set x 0
- after 10 {lappend x timer}
- after 30
- set result $x
- update idletasks
- lappend result $x
- update
- lappend result $x
- }
- } {0 0 {0 timer}}
- test io-47.1 {fileevent vs multiple interpreters} {testfevent fileevent} {
- set f [open $path(foo) r]
- set f2 [open $path(foo) r]
- set f3 [open $path(foo) r]
- fileevent $f readable {script 1}
- testfevent create
- testfevent share $f2
- testfevent cmd "fileevent $f2 readable {script 2}"
- fileevent $f3 readable {sript 3}
- set x {}
- lappend x [fileevent $f2 readable]
- testfevent delete
- lappend x [fileevent $f readable] [fileevent $f2 readable]
- [fileevent $f3 readable]
- close $f
- close $f2
- close $f3
- set x
- } {{} {script 1} {} {sript 3}}
- test io-47.2 {deleting fileevent on interpreter delete} {testfevent fileevent} {
- set f [open $path(foo) r]
- set f2 [open $path(foo) r]
- set f3 [open $path(foo) r]
- set f4 [open $path(foo) r]
- fileevent $f readable {script 1}
- testfevent create
- testfevent share $f2
- testfevent share $f3
- testfevent cmd "fileevent $f2 readable {script 2}
- fileevent $f3 readable {script 3}"
- fileevent $f4 readable {script 4}
- testfevent delete
- set x [list [fileevent $f readable] [fileevent $f2 readable]
- [fileevent $f3 readable] [fileevent $f4 readable]]
- close $f
- close $f2
- close $f3
- close $f4
- set x
- } {{script 1} {} {} {script 4}}
- test io-47.3 {deleting fileevent on interpreter delete} {testfevent fileevent} {
- set f [open $path(foo) r]
- set f2 [open $path(foo) r]
- set f3 [open $path(foo) r]
- set f4 [open $path(foo) r]
- testfevent create
- testfevent share $f3
- testfevent share $f4
- fileevent $f readable {script 1}
- fileevent $f2 readable {script 2}
- testfevent cmd "fileevent $f3 readable {script 3}
- fileevent $f4 readable {script 4}"
- testfevent delete
- set x [list [fileevent $f readable] [fileevent $f2 readable]
- [fileevent $f3 readable] [fileevent $f4 readable]]
- close $f
- close $f2
- close $f3
- close $f4
- set x
- } {{script 1} {script 2} {} {}}
- test io-47.4 {file events on shared files and multiple interpreters} {testfevent fileevent} {
- set f [open $path(foo) r]
- set f2 [open $path(foo) r]
- testfevent create
- testfevent share $f
- testfevent cmd "fileevent $f readable {script 1}"
- fileevent $f readable {script 2}
- fileevent $f2 readable {script 3}
- set x [list [fileevent $f2 readable]
- [testfevent cmd "fileevent $f readable"]
- [fileevent $f readable]]
- testfevent delete
- close $f
- close $f2
- set x
- } {{script 3} {script 1} {script 2}}
- test io-47.5 {file events on shared files, deleting file events} {testfevent fileevent} {
- set f [open $path(foo) r]
- testfevent create
- testfevent share $f
- testfevent cmd "fileevent $f readable {script 1}"
- fileevent $f readable {script 2}
- testfevent cmd "fileevent $f readable {}"
- set x [list [testfevent cmd "fileevent $f readable"]
- [fileevent $f readable]]
- testfevent delete
- close $f
- set x
- } {{} {script 2}}
- test io-47.6 {file events on shared files, deleting file events} {testfevent fileevent} {
- set f [open $path(foo) r]
- testfevent create
- testfevent share $f
- testfevent cmd "fileevent $f readable {script 1}"
- fileevent $f readable {script 2}
- fileevent $f readable {}
- set x [list [testfevent cmd "fileevent $f readable"]
- [fileevent $f readable]]
- testfevent delete
- close $f
- set x
- } {{script 1} {}}
- set path(bar) [makeFile {} bar]
- test io-48.1 {testing readability conditions} {fileevent} {
- set f [open $path(bar) w]
- puts $f abcdefg
- puts $f abcdefg
- puts $f abcdefg
- puts $f abcdefg
- puts $f abcdefg
- close $f
- set f [open $path(bar) r]
- fileevent $f readable [namespace code [list consume $f]]
- proc consume {f} {
- variable l
- variable x
- lappend l called
- if {[eof $f]} {
- close $f
- set x done
- } else {
- gets $f
- }
- }
- set l ""
- variable x not_done
- vwait [namespace which -variable x]
- list $x $l
- } {done {called called called called called called called}}
- test io-48.2 {testing readability conditions} {nonBlockFiles fileevent} {
- set f [open $path(bar) w]
- puts $f abcdefg
- puts $f abcdefg
- puts $f abcdefg
- puts $f abcdefg
- puts $f abcdefg
- close $f
- set f [open $path(bar) r]
- fileevent $f readable [namespace code [list consume $f]]
- fconfigure $f -blocking off
- proc consume {f} {
- variable x
- variable l
- lappend l called
- if {[eof $f]} {
- close $f
- set x done
- } else {
- gets $f
- }
- }
- set l ""
- variable x not_done
- vwait [namespace which -variable x]
- list $x $l
- } {done {called called called called called called called}}
- set path(my_script) [makeFile {} my_script]
- test io-48.3 {testing readability conditions} {stdio unixOnly nonBlockFiles openpipe fileevent} {
- set f [open $path(bar) w]
- puts $f abcdefg
- puts $f abcdefg
- puts $f abcdefg
- puts $f abcdefg
- puts $f abcdefg
- close $f
- set f [open $path(my_script) w]
- puts $f {
- proc copy_slowly {f} {
- while {![eof $f]} {
- puts [gets $f]
- after 200
- }
- close $f
- }
- }
- close $f
- set f [open "|[list [interpreter]]" r+]
- fileevent $f readable [namespace code [list consume $f]]
- fconfigure $f -buffering line
- fconfigure $f -blocking off
- proc consume {f} {
- variable l
- variable x
- if {[eof $f]} {
- set x done
- } else {
- gets $f
- lappend l [fblocked $f]
- gets $f
- lappend l [fblocked $f]
- }
- }
- set l ""
- variable x not_done
- puts $f [list source $path(my_script)]
- puts $f "set f [[list open $path(bar) r]]"
- puts $f {copy_slowly $f}
- puts $f {exit}
- vwait [namespace which -variable x]
- close $f
- list $x $l
- } {done {0 1 0 1 0 1 0 1 0 1 0 1 0 0}}
- test io-48.4 {lf write, testing readability, ^Z termination, auto read mode} {fileevent} {
- file delete $path(test1)
- set f [open $path(test1) w]
- fconfigure $f -translation lf
- variable c [format "abcndefn%c" 26]
- puts -nonewline $f $c
- close $f
- proc consume {f} {
- variable l
- variable c
- variable x
- if {[eof $f]} {
- set x done
- close $f
- } else {
- lappend l [gets $f]
- incr c
- }
- }
- set c 0
- set l ""
- set f [open $path(test1) r]
- fconfigure $f -translation auto -eofchar x1a
- fileevent $f readable [namespace code [list consume $f]]
- variable x
- vwait [namespace which -variable x]
- list $c $l
- } {3 {abc def {}}}
- test io-48.5 {lf write, testing readability, ^Z in middle, auto read mode} {fileevent} {
- file delete $path(test1)
- set f [open $path(test1) w]
- fconfigure $f -translation lf
- set c [format "abcndefn%cfoonbarn" 26]
- puts -nonewline $f $c
- close $f
- proc consume {f} {
- variable l
- variable x
- variable c
- if {[eof $f]} {
- set x done
- close $f
- } else {
- lappend l [gets $f]
- incr c
- }
- }
- set c 0
- set l ""
- set f [open $path(test1) r]
- fconfigure $f -eofchar x1a -translation auto
- fileevent $f readable [namespace code [list consume $f]]
- variable x
- vwait [namespace which -variable x]
- list $c $l
- } {3 {abc def {}}}
- test io-48.6 {cr write, testing readability, ^Z termination, auto read mode} {fileevent} {
- file delete $path(test1)
- set f [open $path(test1) w]
- fconfigure $f -translation cr
- set c [format "abcndefn%c" 26]
- puts -nonewline $f $c
- close $f
- proc consume {f} {
- variable l
- variable x
- variable c
- if {[eof $f]} {
- set x done
- close $f
- } else {
- lappend l [gets $f]
- incr c
- }
- }
- set c 0
- set l ""
- set f [open $path(test1) r]
- fconfigure $f -translation auto -eofchar x1a
- fileevent $f readable [namespace code [list consume $f]]
- variable x
- vwait [namespace which -variable x]
- list $c $l
- } {3 {abc def {}}}
- test io-48.7 {cr write, testing readability, ^Z in middle, auto read mode} {fileevent} {
- file delete $path(test1)
- set f [open $path(test1) w]
- fconfigure $f -translation cr
- set c [format "abcndefn%cfoonbarn" 26]
- puts -nonewline $f $c
- close $f
- proc consume {f} {
- variable l
- variable c
- variable x
- if {[eof $f]} {
- set x done
- close $f
- } else {
- lappend l [gets $f]
- incr c
- }
- }
- set c 0
- set l ""
- set f [open $path(test1) r]
- fconfigure $f -eofchar x1a -translation auto
- fileevent $f readable [namespace code [list consume $f]]
- variable x
- vwait [namespace which -variable x]
- list $c $l
- } {3 {abc def {}}}
- test io-48.8 {crlf write, testing readability, ^Z termination, auto read mode} {fileevent} {
- file delete $path(test1)
- set f [open $path(test1) w]
- fconfigure $f -translation crlf
- set c [format "abcndefn%c" 26]
- puts -nonewline $f $c
- close $f
- proc consume {f} {
- variable l
- variable x
- variable c
- if {[eof $f]} {
- set x done
- close $f
- } else {
- lappend l [gets $f]
- incr c
- }
- }
- set c 0
- set l ""
- set f [open $path(test1) r]
- fconfigure $f -translation auto -eofchar x1a
- fileevent $f readable [namespace code [list consume $f]]
- variable x
- vwait [namespace which -variable x]
- list $c $l
- } {3 {abc def {}}}
- test io-48.9 {crlf write, testing readability, ^Z in middle, auto read mode} {fileevent} {
- file delete $path(test1)
- set f [open $path(test1) w]
- fconfigure $f -translation crlf
- set c [format "abcndefn%cfoonbarn" 26]
- puts -nonewline $f $c
- close $f
- proc consume {f} {
- variable l
- variable c
- variable x
- if {[eof $f]} {
- set x done
- close $f
- } else {
- lappend l [gets $f]
- incr c
- }
- }
- set c 0
- set l ""
- set f [open $path(test1) r]
- fconfigure $f -eofchar x1a -translation auto
- fileevent $f readable [namespace code [list consume $f]]
- variable x
- vwait [namespace which -variable x]
- list $c $l
- } {3 {abc def {}}}
- test io-48.10 {lf write, testing readability, ^Z in middle, lf read mode} {fileevent} {
- file delete $path(test1)
- set f [open $path(test1) w]
- fconfigure $f -translation lf
- set c [format "abcndefn%cfoonbarn" 26]
- puts -nonewline $f $c
- close $f
- proc consume {f} {
- variable l
- variable c
- variable x
- if {[eof $f]} {
- set x done
- close $f
- } else {
- lappend l [gets $f]
- incr c
- }
- }
- set c 0
- set l ""
- set f [open $path(test1) r]
- fconfigure $f -eofchar x1a -translation lf
- fileevent $f readable [namespace code [list consume $f]]
- variable x
- vwait [namespace which -variable x]
- list $c $l
- } {3 {abc def {}}}
- test io-48.11 {lf write, testing readability, ^Z termination, lf read mode} {fileevent} {
- file delete $path(test1)
- set f [open $path(test1) w]
- fconfigure $f -translation lf
- set c [format "abcndefn%c" 26]
- puts -nonewline $f $c
- close $f
- proc consume {f} {
- variable l
- variable x
- variable c
- if {[eof $f]} {
- set x done
- close $f
- } else {
- lappend l [gets $f]
- incr c
- }
- }
- set c 0
- set l ""
- set f [open $path(test1) r]
- fconfigure $f -translation lf -eofchar x1a
- fileevent $f readable [namespace code [list consume $f]]
- variable x
- vwait [namespace which -variable x]
- list $c $l
- } {3 {abc def {}}}
- test io-48.12 {cr write, testing readability, ^Z in middle, cr read mode} {fileevent} {
- file delete $path(test1)
- set f [open $path(test1) w]
- fconfigure $f -translation cr
- set c [format "abcndefn%cfoonbarn" 26]
- puts -nonewline $f $c
- close $f
- proc consume {f} {
- variable l
- variable x
- variable c
- if {[eof $f]} {
- set x done
- close $f
- } else {
- lappend l [gets $f]
- incr c
- }
- }
- set c 0
- set l ""
- set f [open $path(test1) r]
- fconfigure $f -eofchar x1a -translation cr
- fileevent $f readable [namespace code [list consume $f]]
- variable x
- vwait [namespace which -variable x]
- list $c $l
- } {3 {abc def {}}}
- test io-48.13 {cr write, testing readability, ^Z termination, cr read mode} {fileevent} {
- file delete $path(test1)
- set f [open $path(test1) w]
- fconfigure $f -translation cr
- set c [format "abcndefn%c" 26]
- puts -nonewline $f $c
- close $f
- proc consume {f} {
- variable c
- variable x
- variable l
- if {[eof $f]} {
- set x done
- close $f
- } else {
- lappend l [gets $f]
- incr c
- }
- }
- set c 0
- set l ""
- set f [open $path(test1) r]
- fconfigure $f -translation cr -eofchar x1a
- fileevent $f readable [namespace code [list consume $f]]
- variable x
- vwait [namespace which -variable x]
- list $c $l
- } {3 {abc def {}}}
- test io-48.14 {crlf write, testing readability, ^Z in middle, crlf read mode} {fileevent} {
- file delete $path(test1)
- set f [open $path(test1) w]
- fconfigure $f -translation crlf
- set c [format "abcndefn%cfoonbarn" 26]
- puts -nonewline $f $c
- close $f
- proc consume {f} {
- variable c
- variable x
- variable l
- if {[eof $f]} {
- set x done
- close $f
- } else {
- lappend l [gets $f]
- incr c
- }
- }
- set c 0
- set l ""
- set f [open $path(test1) r]
- fconfigure $f -eofchar x1a -translation crlf
- fileevent $f readable [namespace code [list consume $f]]
- variable x
- vwait [namespace which -variable x]
- list $c $l
- } {3 {abc def {}}}
- test io-48.15 {crlf write, testing readability, ^Z termi, crlf read mode} {fileevent} {
- file delete $path(test1)
- set f [open $path(test1) w]
- fconfigure $f -translation crlf
- set c [format "abcndefn%c" 26]
- puts -nonewline $f $c
- close $f
- proc consume {f} {
- variable c
- variable x
- variable l
- if {[eof $f]} {
- set x done
- close $f
- } else {
- lappend l [gets $f]
- incr c
- }
- }
- set c 0
- set l ""
- set f [open $path(test1) r]
- fconfigure $f -translation crlf -eofchar x1a
- fileevent $f readable [namespace code [list consume $f]]
- variable x
- vwait [namespace which -variable x]
- list $c $l
- } {3 {abc def {}}}
- test io-49.1 {testing crlf reading, leftover cr disgorgment} {
- file delete $path(test1)
- set f [open $path(test1) w]
- fconfigure $f -translation lf
- puts -nonewline $f "arbrcrn"
- close $f
- set f [open $path(test1) r]
- set l ""
- lappend l [file size $path(test1)]
- fconfigure $f -translation crlf
- lappend l [read $f 1]
- lappend l [tell $f]
- lappend l [read $f 1]
- lappend l [tell $f]
- lappend l [read $f 1]
- lappend l [tell $f]
- lappend l [read $f 1]
- lappend l [tell $f]
- lappend l [read $f 1]
- lappend l [tell $f]
- lappend l [read $f 1]
- lappend l [tell $f]
- lappend l [eof $f]
- lappend l [read $f 1]
- lappend l [eof $f]
- close $f
- set l
- } "7 a 1 [list r] 2 b 3 [list r] 4 c 5 {
- } 7 0 {} 1"
- test io-49.2 {testing crlf reading, leftover cr disgorgment} {
- file delete $path(test1)
- set f [open $path(test1) w]
- fconfigure $f -translation lf
- puts -nonewline $f "arbrcrn"
- close $f
- set f [open $path(test1) r]
- set l ""
- lappend l [file size $path(test1)]
- fconfigure $f -translation crlf
- lappend l [read $f 2]
- lappend l [tell $f]
- lappend l [read $f 2]
- lappend l [tell $f]
- lappend l [read $f 2]
- lappend l [tell $f]
- lappend l [eof $f]
- lappend l [read $f 2]
- lappend l [tell $f]
- lappend l [eof $f]
- close $f
- set l
- } "7 [list ar] 2 [list br] 4 [list cn] 7 0 {} 7 1"
- test io-49.3 {testing crlf reading, leftover cr disgorgment} {
- file delete $path(test1)
- set f [open $path(test1) w]
- fconfigure $f -translation lf
- puts -nonewline $f "arbrcrn"
- close $f
- set f [open $path(test1) r]
- set l ""
- lappend l [file size $path(test1)]
- fconfigure $f -translation crlf
- lappend l [read $f 3]
- lappend l [tell $f]
- lappend l [read $f 3]
- lappend l [tell $f]
- lappend l [eof $f]
- lappend l [read $f 3]
- lappend l [tell $f]
- lappend l [eof $f]
- close $f
- set l
- } "7 [list arb] 3 [list rcn] 7 0 {} 7 1"
- test io-49.4 {testing crlf reading, leftover cr disgorgment} {
- file delete $path(test1)
- set f [open $path(test1) w]
- fconfigure $f -translation lf
- puts -nonewline $f "arbrcrn"
- close $f
- set f [open $path(test1) r]
- set l ""
- lappend l [file size $path(test1)]
- fconfigure $f -translation crlf
- lappend l [read $f 3]
- lappend l [tell $f]
- lappend l [gets $f]
- lappend l [tell $f]
- lappend l [eof $f]
- lappend l [gets $f]
- lappend l [tell $f]
- lappend l [eof $f]
- close $f
- set l
- } "7 [list arb] 3 [list rc] 7 0 {} 7 1"
- test io-49.5 {testing crlf reading, leftover cr disgorgment} {
- file delete $path(test1)
- set f [open $path(test1) w]
- fconfigure $f -translation lf
- puts -nonewline $f "arbrcrn"
- close $f
- set f [open $path(test1) r]
- set l ""
- lappend l [file size $path(test1)]
- fconfigure $f -translation crlf
- lappend l [set x [gets $f]]
- lappend l [tell $f]
- lappend l [gets $f]
- lappend l [tell $f]
- lappend l [eof $f]
- close $f
- set l
- } [list 7 arbrc 7 {} 7 1]
-
- testConstraint testchannelevent [llength [info commands testchannelevent]]
- test io-50.1 {testing handler deletion} {testchannelevent} {
- file delete $path(test1)
- set f [open $path(test1) w]
- close $f
- set f [open $path(test1) r]
- testchannelevent $f add readable [namespace code [list delhandler $f]]
- proc delhandler {f} {
- variable z
- set z called
- testchannelevent $f delete 0
- }
- set z not_called
- update
- close $f
- set z
- } called
- test io-50.2 {testing handler deletion with multiple handlers} {testchannelevent} {
- file delete $path(test1)
- set f [open $path(test1) w]
- close $f
- set f [open $path(test1) r]
- testchannelevent $f add readable [namespace code [list delhandler $f 1]]
- testchannelevent $f add readable [namespace code [list delhandler $f 0]]
- proc delhandler {f i} {
- variable z
- lappend z "called delhandler $f $i"
- testchannelevent $f delete 0
- }
- set z ""
- update
- close $f
- string compare [string tolower $z]
- [list [list called delhandler $f 0] [list called delhandler $f 1]]
- } 0
- test io-50.3 {testing handler deletion with multiple handlers} {testchannelevent} {
- file delete $path(test1)
- set f [open $path(test1) w]
- close $f
- set f [open $path(test1) r]
- testchannelevent $f add readable [namespace code [list notcalled $f 1]]
- testchannelevent $f add readable [namespace code [list delhandler $f 0]]
- set z ""
- proc notcalled {f i} {
- variable z
- lappend z "notcalled was called!! $f $i"
- }
- proc delhandler {f i} {
- variable z
- testchannelevent $f delete 1
- lappend z "delhandler $f $i called"
- testchannelevent $f delete 0
- lappend z "delhandler $f $i deleted myself"
- }
- set z ""
- update
- close $f
- string compare [string tolower $z]
- [list [list delhandler $f 0 called]
- [list delhandler $f 0 deleted myself]]
- } 0
- test io-50.4 {testing handler deletion vs reentrant calls} {testchannelevent} {
- file delete $path(test1)
- set f [open $path(test1) w]
- close $f
- set f [open $path(test1) r]
- testchannelevent $f add readable [namespace code [list delrecursive $f]]
- proc delrecursive {f} {
- variable z
- variable u
- if {"$u" == "recursive"} {
- testchannelevent $f delete 0
- lappend z "delrecursive deleting recursive"
- } else {
- lappend z "delrecursive calling recursive"
- set u recursive
- update
- }
- }
- set u toplevel
- set z ""
- update
- close $f
- string compare [string tolower $z]
- {{delrecursive calling recursive} {delrecursive deleting recursive}}
- } 0
- test io-50.5 {testing handler deletion vs reentrant calls} {testchannelevent} {
- file delete $path(test1)
- set f [open $path(test1) w]
- close $f
- set f [open $path(test1) r]
- testchannelevent $f add readable [namespace code [list notcalled $f]]
- testchannelevent $f add readable [namespace code [list del $f]]
- proc notcalled {f} {
- variable z
- lappend z "notcalled was called!! $f"
- }
- proc del {f} {
- variable u
- variable z
- if {"$u" == "recursive"} {
- testchannelevent $f delete 1
- testchannelevent $f delete 0
- lappend z "del deleted notcalled"
- lappend z "del deleted myself"
- } else {
- set u recursive
- lappend z "del calling recursive"
- update
- lappend z "del after update"
- }
- }
- set z ""
- set u toplevel
- update
- close $f
- string compare [string tolower $z]
- [list {del calling recursive} {del deleted notcalled}
- {del deleted myself} {del after update}]
- } 0
- test io-50.6 {testing handler deletion vs reentrant calls} {testchannelevent} {
- file delete $path(test1)
- set f [open $path(test1) w]
- close $f
- set f [open $path(test1) r]
- testchannelevent $f add readable [namespace code [list second $f]]
- testchannelevent $f add readable [namespace code [list first $f]]
- proc first {f} {
- variable u
- variable z
- if {"$u" == "toplevel"} {
- lappend z "first called"
- set u first
- update
- lappend z "first after update"
- } else {
- lappend z "first called not toplevel"
- }
- }
- proc second {f} {
- variable u
- variable z
- if {"$u" == "first"} {
- lappend z "second called, first time"
- set u second
- testchannelevent $f delete 0
- } elseif {"$u" == "second"} {
- lappend z "second called, second time"
- testchannelevent $f delete 0
- } else {
- lappend z "second called, cannot happen!"
- testchannelevent $f removeall
- }
- }
- set z ""
- set u toplevel
- update
- close $f
- string compare [string tolower $z]
- [list {first called} {first called not toplevel}
- {second called, first time} {second called, second time}
- {first after update}]
- } 0
- test io-51.1 {Test old socket deletion on Macintosh} {socket} {
- set x 0
- set result ""
- proc accept {s a p} {
- variable x
- variable wait
- fconfigure $s -blocking off
- puts $s "sock[incr x]"
- close $s
- set wait done
- }
- set ss [socket -server [namespace code accept] 0]
- variable wait ""
- set cs [socket [info hostname] [lindex [fconfigure $ss -sockname] 2]]
- vwait [namespace which -variable wait]
- lappend result [gets $cs]
- close $cs
- set wait ""
- set cs [socket [info hostname] [lindex [fconfigure $ss -sockname] 2]]
- vwait [namespace which -variable wait]
- lappend result [gets $cs]
- close $cs
- set wait ""
- set cs [socket [info hostname] [lindex [fconfigure $ss -sockname] 2]]
- vwait [namespace which -variable wait]
- lappend result [gets $cs]
- close $cs
- set wait ""
- set cs [socket [info hostname] [lindex [fconfigure $ss -sockname] 2]]
- vwait [namespace which -variable wait]
- lappend result [gets $cs]
- close $cs
- close $ss
- set result
- } {sock1 sock2 sock3 sock4}
- test io-52.1 {TclCopyChannel} {fcopy} {
- file delete $path(test1)
- set f1 [open $thisScript]
- set f2 [open $path(test1) w]
- fcopy $f1 $f2 -command { # }
- catch { fcopy $f1 $f2 } msg
- close $f1
- close $f2
- string compare $msg "channel "$f1" is busy"
- } {0}
- test io-52.2 {TclCopyChannel} {fcopy} {
- file delete $path(test1)
- set f1 [open $thisScript]
- set f2 [open $path(test1) w]
- set f3 [open $thisScript]
- fcopy $f1 $f2 -command { # }
- catch { fcopy $f3 $f2 } msg
- close $f1
- close $f2
- close $f3
- string compare $msg "channel "$f2" is busy"
- } {0}
- test io-52.3 {TclCopyChannel} {fcopy} {
- file delete $path(test1)
- set f1 [open $thisScript]
- set f2 [open $path(test1) w]
- fconfigure $f1 -translation lf -blocking 0
- fconfigure $f2 -translation cr -blocking 0
- set s0 [fcopy $f1 $f2]
- set result [list [fconfigure $f1 -blocking] [fconfigure $f2 -blocking]]
- close $f1
- close $f2
- set s1 [file size $thisScript]
- set s2 [file size $path(test1)]
- if {("$s1" == "$s2") && ($s0 == $s1)} {
- lappend result ok
- }
- set result
- } {0 0 ok}
- test io-52.4 {TclCopyChannel} {fcopy} {
- file delete $path(test1)
- set f1 [open $thisScript]
- set f2 [open $path(test1) w]
- fconfigure $f1 -translation lf -blocking 0
- fconfigure $f2 -translation cr -blocking 0
- fcopy $f1 $f2 -size 40
- set result [list [fconfigure $f1 -blocking] [fconfigure $f2 -blocking]]
- close $f1
- close $f2
- lappend result [file size $path(test1)]
- } {0 0 40}
- test io-52.5 {TclCopyChannel} {fcopy} {
- file delete $path(test1)
- set f1 [open $thisScript]
- set f2 [open $path(test1) w]
- fconfigure $f1 -translation lf -blocking 0
- fconfigure $f2 -translation lf -blocking 0
- fcopy $f1 $f2 -size -1
- set result [list [fconfigure $f1 -blocking] [fconfigure $f2 -blocking]]
- close $f1
- close $f2
- set s1 [file size $thisScript]
- set s2 [file size $path(test1)]
- if {"$s1" == "$s2"} {
- lappend result ok
- }
- set result
- } {0 0 ok}
- test io-52.6 {TclCopyChannel} {fcopy} {
- file delete $path(test1)
- set f1 [open $thisScript]
- set f2 [open $path(test1) w]
- fconfigure $f1 -translation lf -blocking 0
- fconfigure $f2 -translation lf -blocking 0
- set s0 [fcopy $f1 $f2 -size [expr [file size $thisScript] + 5]]
- set result [list [fconfigure $f1 -blocking] [fconfigure $f2 -blocking]]
- close $f1
- close $f2
- set s1 [file size $thisScript]
- set s2 [file size $path(test1)]
- if {("$s1" == "$s2") && ($s0 == $s1)} {
- lappend result ok
- }
- set result
- } {0 0 ok}
- test io-52.7 {TclCopyChannel} {fcopy} {
- file delete $path(test1)
- set f1 [open $thisScript]
- set f2 [open $path(test1) w]
- fconfigure $f1 -translation lf -blocking 0
- fconfigure $f2 -translation lf -blocking 0
- fcopy $f1 $f2
- set result [list [fconfigure $f1 -blocking] [fconfigure $f2 -blocking]]
- set s1 [file size $thisScript]
- set s2 [file size $path(test1)]
- close $f1
- close $f2
- if {"$s1" == "$s2"} {
- lappend result ok
- }
- set result
- } {0 0 ok}
- test io-52.8 {TclCopyChannel} {stdio openpipe fcopy} {
- file delete $path(test1)
- file delete $path(pipe)
- set f1 [open $path(pipe) w]
- fconfigure $f1 -translation lf
- puts $f1 "
- puts ready
- gets stdin
- set f1 [open [list $thisScript] r]
- fconfigure $f1 -translation lf
- puts [read $f1 100]
- close $f1
- "
- close $f1
- set f1 [open "|[list [interpreter] $path(pipe)]" r+]
- fconfigure $f1 -translation lf
- gets $f1
- puts $f1 ready
- flush $f1
- set f2 [open $path(test1) w]
- fconfigure $f2 -translation lf
- set s0 [fcopy $f1 $f2 -size 40]
- catch {close $f1}
- close $f2
- list $s0 [file size $path(test1)]
- } {40 40}
- # Empty files, to register them with the test facility
- set path(kyrillic.txt) [makeFile {} kyrillic.txt]
- set path(utf8-fcopy.txt) [makeFile {} utf8-fcopy.txt]
- set path(utf8-rp.txt) [makeFile {} utf8-rp.txt]
- # Create kyrillic file, use lf translation to avoid os eol issues
- set out [open $path(kyrillic.txt) w]
- fconfigure $out -encoding koi8-r -translation lf
- puts $out "u0410u0410"
- close $out
- test io-52.9 {TclCopyChannel & encodings} {fcopy} {
- # Copy kyrillic to UTF-8, using fcopy.
- set in [open $path(kyrillic.txt) r]
- set out [open $path(utf8-fcopy.txt) w]
- fconfigure $in -encoding koi8-r -translation lf
- fconfigure $out -encoding utf-8 -translation lf
- fcopy $in $out
- close $in
- close $out
- # Do the same again, but differently (read/puts).
- set in [open $path(kyrillic.txt) r]
- set out [open $path(utf8-rp.txt) w]
- fconfigure $in -encoding koi8-r -translation lf
- fconfigure $out -encoding utf-8 -translation lf
- puts -nonewline $out [read $in]
- close $in
- close $out
- list [file size $path(kyrillic.txt)]
- [file size $path(utf8-fcopy.txt)]
- [file size $path(utf8-rp.txt)]
- } {3 5 5}
- test io-52.10 {TclCopyChannel & encodings} {fcopy} {
- # encoding to binary (=> implies that the
- # internal utf-8 is written)
- set in [open $path(kyrillic.txt) r]
- set out [open $path(utf8-fcopy.txt) w]
- fconfigure $in -encoding koi8-r -translation lf
- # -translation binary is also -encoding binary
- fconfigure $out -translation binary
- fcopy $in $out
- close $in
- close $out
- file size $path(utf8-fcopy.txt)
- } 5
- test io-52.11 {TclCopyChannel & encodings} {fcopy} {
- # binary to encoding => the input has to be
- # in utf-8 to make sense to the encoder
- set in [open $path(utf8-fcopy.txt) r]
- set out [open $path(kyrillic.txt) w]
- # -translation binary is also -encoding binary
- fconfigure $in -translation binary
- fconfigure $out -encoding koi8-r -translation lf
- fcopy $in $out
- close $in
- close $out
- file size $path(kyrillic.txt)
- } 3
- test io-53.1 {CopyData} {fcopy} {
- file delete $path(test1)
- set f1 [open $thisScript]
- set f2 [open $path(test1) w]
- fconfigure $f1 -translation lf -blocking 0
- fconfigure $f2 -translation cr -blocking 0
- fcopy $f1 $f2 -size 0
- set result [list [fconfigure $f1 -blocking] [fconfigure $f2 -blocking]]
- close $f1
- close $f2
- lappend result [file size $path(test1)]
- } {0 0 0}
- test io-53.2 {CopyData} {fcopy} {
- file delete $path(test1)
- set f1 [open $thisScript]
- set f2 [open $path(test1) w]
- fconfigure $f1 -translation lf -blocking 0
- fconfigure $f2 -translation cr -blocking 0
- fcopy $f1 $f2 -command [namespace code {set s0}]
- set result [list [fconfigure $f1 -blocking] [fconfigure $f2 -blocking]]
- variable s0
- vwait [namespace which -variable s0]
- close $f1
- close $f2
- set s1 [file size $thisScript]
- set s2 [file size $path(test1)]
- if {("$s1" == "$s2") && ($s0 == $s1)} {
- lappend result ok
- }
- set result
- } {0 0 ok}
- test io-53.3 {CopyData: background read underflow} {stdio unixOnly openpipe fcopy} {
- file delete $path(test1)
- file delete $path(pipe)
- set f1 [open $path(pipe) w]
- puts -nonewline $f1 {
- puts ready
- flush stdout ;# Don't assume line buffered!
- fcopy stdin stdout -command { set x }
- vwait x
- set f [}
- puts $f1 [list open $path(test1) w]]
- puts $f1 {
- fconfigure $f -translation lf
- puts $f "done"
- close $f
- }
- close $f1
- set f1 [open "|[list [interpreter] $path(pipe)]" r+]
- set result [gets $f1]
- puts $f1 line1
- flush $f1
- lappend result [gets $f1]
- puts $f1 line2
- flush $f1
- lappend result [gets $f1]
- close $f1
- after 500
- set f [open $path(test1)]
- lappend result [read $f]
- close $f
- set result
- } "ready line1 line2 {donen}"
- test io-53.4 {CopyData: background write overflow} {stdio unixOnly openpipe fileevent fcopy} {
- set big bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbn
- variable x
- for {set x 0} {$x < 12} {incr x} {
- append big $big
- }
- file delete $path(test1)
- file delete $path(pipe)
- set f1 [open $path(pipe) w]
- puts $f1 {
- puts ready
- fcopy stdin stdout -command { set x }
- vwait x
- set f [open $path(test1) w]
- fconfigure $f -translation lf
- puts $f "done"
- close $f
- }
- close $f1
- set f1 [open "|[list [interpreter] $path(pipe)]" r+]
- set result [gets $f1]
- fconfigure $f1 -blocking 0
- puts $f1 $big
- flush $f1
- after 500
- set result ""
- fileevent $f1 read [namespace code {
- append result [read $f1 1024]
- if {[string length $result] >= [string length $big]} {
- set x done
- }
- }]
- vwait [namespace which -variable x]
- close $f1
- set big {}
- set x
- } done
- set result {}
- proc FcopyTestAccept {sock args} {
- after 1000 "close $sock"
- }
- proc FcopyTestDone {bytes {error {}}} {
- variable fcopyTestDone
- if {[string length $error]} {
- set fcopyTestDone 1
- } else {
- set fcopyTestDone 0
- }
- }
- test io-53.5 {CopyData: error during fcopy} {socket fcopy} {
- variable fcopyTestDone
- set listen [socket -server [namespace code FcopyTestAccept] 0]
- set in [open $thisScript] ;# 126 K
- set out [socket 127.0.0.1 [lindex [fconfigure $listen -sockname] 2]]
- catch {unset fcopyTestDone}
- close $listen ;# This means the socket open never really succeeds
- fcopy $in $out -command [namespace code FcopyTestDone]
- variable fcopyTestDone
- if ![info exists fcopyTestDone] {
- vwait [namespace which -variable fcopyTestDone] ;# The error occurs here in the b.g.
- }
- close $in
- close $out
- set fcopyTestDone ;# 1 for error condition
- } 1
- test io-53.6 {CopyData: error during fcopy} {stdio openpipe fcopy} {
- variable fcopyTestDone
- file delete $path(pipe)
- file delete $path(test1)
- catch {unset fcopyTestDone}
- set f1 [open $path(pipe) w]
- puts $f1 "exit 1"
- close $f1
- set in [open "|[list [interpreter] $path(pipe)]" r+]
- set out [open $path(test1) w]
- fcopy $in $out -command [namespace code FcopyTestDone]
- variable fcopyTestDone
- if ![info exists fcopyTestDone] {
- vwait [namespace which -variable fcopyTestDone]
- }
- catch {close $in}
- close $out
- set fcopyTestDone ;# 0 for plain end of file
- } {0}
- proc doFcopy {in out {bytes 0} {error {}}} {
- variable fcopyTestDone
- variable fcopyTestCount
- incr fcopyTestCount $bytes
- if {[string length $error]} {
- set fcopyTestDone 1
- } elseif {[eof $in]} {
- set fcopyTestDone 0
- } else {
- # Delay next fcopy to wait for size>0 input bytes
- after 100 [list
- fcopy $in $out -size 1000
- -command [namespace code [list doFcopy $in $out]]
- ]
- }
- }
- test io-53.7 {CopyData: Flooding fcopy from pipe} {stdio openpipe fcopy} {
- variable fcopyTestDone
- file delete $path(pipe)
- catch {unset fcopyTestDone}
- set fcopyTestCount 0
- set f1 [open $path(pipe) w]
- puts $f1 {
- # Write 10 bytes / 10 msec
- proc Write {count} {
- puts -nonewline "1234567890"
- if {[incr count -1]} {
- after 10 [list Write $count]
- } else {
- set ::ready 1
- }
- }
- fconfigure stdout -buffering none
- Write 345 ;# 3450 bytes ~3.45 sec
- vwait ready
- exit 0
- }
- close $f1
- set in [open "|[list [interpreter] $path(pipe) &]" r+]
- set out [open $path(test1) w]
- doFcopy $in $out
- variable fcopyTestDone
- if ![info exists fcopyTestDone] {
- vwait [namespace which -variable fcopyTestDone]
- }
- catch {close $in}
- close $out
- # -1=error 0=script error N=number of bytes
- expr ($fcopyTestDone == 0) ? $fcopyTestCount : -1
- } {3450}
- test io-54.1 {Recursive channel events} {socket fileevent} {
- # This test checks to see if file events are delivered during recursive
- # event loops when there is buffered data on the channel.
- proc accept {s a p} {
- variable as
- fconfigure $s -translation lf
- puts $s "line 1nline2nline3"
- flush $s
- set as $s
- }
- proc readit {s next} {
- variable x
- variable result
- lappend result $next
- if {$next == 1} {
- fileevent $s readable [namespace code [list readit $s 2]]
- vwait [namespace which -variable x]
- }
- incr x
- }
- set ss [socket -server [namespace code accept] 0]
- # We need to delay on some systems until the creation of the
- # server socket completes.
- set done 0
- for {set i 0} {$i < 10} {incr i} {
- if {![catch {set cs [socket [info hostname] [lindex [fconfigure $ss -sockname] 2]]}]} {
- set done 1
- break
- }
- after 100
- }
- if {$done == 0} {
- close $ss
- error "failed to connect to server"
- }
- variable result {}
- variable x 0
- variable as
- vwait [namespace which -variable as]
- fconfigure $cs -translation lf
- lappend result [gets $cs]
- fconfigure $cs -blocking off
- fileevent $cs readable [namespace code [list readit $cs 1]]
- set a [after 2000 [namespace code { set x failure }]]
- vwait [namespace which -variable x]
- after cancel $a
- close $as
- close $ss
- close $cs
- list $result $x
- } {{{line 1} 1 2} 2}
- test io-54.2 {Testing for busy-wait in recursive channel events} {socket fileevent} {
- set accept {}
- set after {}
- variable s [socket -server [namespace code accept] 0]
- proc accept {s a p} {
- variable counter
- variable accept
- set accept $s
- set counter 0
- fconfigure $s -blocking off -buffering line -translation lf
- fileevent $s readable [namespace code "doit $s"]
- }
- proc doit {s} {
- variable counter
- variable after
- incr counter
- set l [gets $s]
- if {"$l" == ""} {
- fileevent $s readable [namespace code "doit1 $s"]
- set after [after 1000 [namespace code newline]]
- }
- }
- proc doit1 {s} {
- variable counter
- variable accept
- incr counter
- set l [gets $s]
- close $s
- set accept {}
- }
- proc producer {} {
- variable s
- variable writer
- set writer [socket 127.0.0.1 [lindex [fconfigure $s -sockname] 2]]
- fconfigure $writer -buffering line
- puts -nonewline $writer hello
- flush $writer
- }
- proc newline {} {
- variable done
- variable writer
- puts $writer hello
- flush $writer
- set done 1
- }
- producer
- variable done
- vwait [namespace which -variable done]
- close $writer
- close $s
- after cancel $after
- if {$accept != {}} {close $accept}
- set counter
- } 1
- set path(fooBar) [makeFile {} fooBar]
- test io-55.1 {ChannelEventScriptInvoker: deletion} {fileevent} {
- variable x
- proc eventScript {fd} {
- variable x
- close $fd
- error "planned error"
- set x whoops
- }
- proc ::bgerror {args} "set [namespace which -variable x] got_error"
- set f [open $path(fooBar) w]
- fileevent $f writable [namespace code [list eventScript $f]]
- variable x not_done
- vwait [namespace which -variable x]
- set x
- } {got_error}
- test io-56.1 {ChannelTimerProc} {testchannelevent} {
- set f [open $path(fooBar) w]
- puts $f "this is a test"
- close $f
- set f [open $path(fooBar) r]
- testchannelevent $f add readable [namespace code {
- read $f 1
- incr x
- }]
- variable x 0
- vwait [namespace which -variable x]
- vwait [namespace which -variable x]
- set result $x
- testchannelevent $f set 0 none
- after idle [namespace code {set y done}]
- variable y
- vwait [namespace which -variable y]
- close $f
- lappend result $y
- } {2 done}
- test io-57.1 {buffered data and file events, gets} {fileevent} {
- proc accept {sock args} {
- variable s2
- set s2 $sock
- }
- set server [socket -server [namespace code accept] 0]
- set s [socket 127.0.0.1 [lindex [fconfigure $server -sockname] 2]]
- variable s2
- vwait [namespace which -variable s2]
- update
- fileevent $s2 readable [namespace code {lappend result readable}]
- puts $s "12n34567890"
- flush $s
- variable result [gets $s2]
- after 1000 [namespace code {lappend result timer}]
- vwait [namespace which -variable result]
- lappend result [gets $s2]
- vwait [namespace which -variable result]
- close $s
- close $s2
- close $server
- set result
- } {12 readable 34567890 timer}
- test io-57.2 {buffered data and file events, read} {fileevent} {
- proc accept {sock args} {
- variable s2
- set s2 $sock
- }
- set server [socket -server [namespace code accept] 0]
- set s [socket 127.0.0.1 [lindex [fconfigure $server -sockname] 2]]
- variable s2
- vwait [namespace which -variable s2]
- update
- fileevent $s2 readable [namespace code {lappend result readable}]
- puts -nonewline $s "1234567890"
- flush $s
- variable result [read $s2 1]
- after 1000 [namespace code {lappend result timer}]
- vwait [namespace which -variable result]
- lappend result [read $s2 9]
- vwait [namespace which -variable result]
- close $s
- close $s2
- close $server
- set result
- } {1 readable 234567890 timer}
-
- test io-58.1 {Tcl_NotifyChannel and error when closing} {stdio unixOrPc openpipe fileevent} {
- set out [open $path(script) w]
- puts $out {
- puts "normal message from pipe"
- puts stderr "error message from pipe"
- exit 1
- }
- proc readit {pipe} {
- variable x
- variable result
- if {[eof $pipe]} {
- set x [catch {close $pipe} line]
- lappend result catch $line
- } else {
- gets $pipe line
- lappend result gets $line
- }
- }
- close $out
- set pipe [open "|[list [interpreter] $path(script)]" r]
- fileevent $pipe readable [namespace code [list readit $pipe]]
- variable x ""
- set result ""
- vwait [namespace which -variable x]
- list $x $result
- } {1 {gets {normal message from pipe} gets {} catch {error message from pipe}}}
- testConstraint testmainthread [llength [info commands testmainthread]]
- test io-59.1 {Thread reference of channels} {testmainthread testchannel} {
- # TIP #10
- # More complicated tests (like that the reference changes as a
- # channel is moved from thread to thread) can be done only in the
- # extension which fully implements the moving of channels between
- # threads, i.e. 'Threads'. Or we have to extend [testthread] as well.
- set f [open $path(longfile) r]
- set result [testchannel mthread $f]
- close $f
- string equal $result [testmainthread]
- } {1}
- test io-60.1 {writing illegal utf sequences} {openpipe fileevent} {
- # This test will hang in older revisions of the core.
- set out [open $path(script) w]
- puts $out {
- puts [encoding convertfrom identity xe2]
- exit 1
- }
- proc readit {pipe} {
- variable x
- variable result
- if {[eof $pipe]} {
- set x [catch {close $pipe} line]
- lappend result catch $line
- } else {
- gets $pipe line
- lappend result gets $line
- }
- }
- close $out
- set pipe [open "|[list [interpreter] $path(script)]" r]
- fileevent $pipe readable [namespace code [list readit $pipe]]
- variable x ""
- set result ""
- vwait [namespace which -variable x]
- # cut of the remainder of the error stack, especially the filename
- set result [lreplace $result 3 3 [lindex [split [lindex $result 3] n] 0]]
- list $x $result
- } {1 {gets {} catch {error writing "stdout": invalid argument}}}
- test io-61.1 {Reset eof state after changing the eof char} -setup {
- set datafile [makeFile {} eofchar]
- set f [open $datafile w]
- fconfigure $f -translation binary
- puts -nonewline $f [string repeat "Ho humn" 11]
- puts $f =
- set line [string repeat "Ge gla " 4]
- puts -nonewline $f [string repeat [string trimright $line]n 834]
- close $f
- } -body {
- set f [open $datafile r]
- fconfigure $f -eofchar =
- set res {}
- lappend res [read $f; tell $f]
- fconfigure $f -eofchar {}
- lappend res [read $f 1]
- lappend res [read $f; tell $f]
- # Any seek zaps the internals into a good state.
- #seek $f 0 start
- #seek $f 0 current
- #lappend res [read $f; tell $f]
- close $f
- set res
- } -cleanup {
- removeFile eofchar
- } -result {77 = 23431}
- # cleanup
- foreach file [list fooBar longfile script output test1 pipe my_script foo
- bar test2 test3 cat stdout kyrillic.txt utf8-fcopy.txt utf8-rp.txt] {
- removeFile $file
- }
- cleanupTests
- }
- namespace delete ::tcl::test::io
- return