📄 io.test
字号:
# Functionality covered: operation of all IO commands, and all procedures# defined in generic/tclIO.c.## This file contains a collection of tests for one or more of the Tcl# built-in commands. Sourcing this file into Tcl runs the tests and# generates output for errors. No output means no errors were found.## Copyright (c) 1991-1994 The Regents of the University of California.# Copyright (c) 1994-1997 Sun Microsystems, Inc.# Copyright (c) 1998-1999 by Scriptics Corporation.## See the file "license.terms" for information on usage and redistribution# of this file, and for a DISCLAIMER OF ALL WARRANTIES.## RCS: @(#) $Id: io.test,v 1.40 2003/02/25 22:03:38 andreas_kupries Exp $if {[catch {package require tcltest 2}]} { puts stderr "Skipping tests in [info script]. tcltest 2 required." return}namespace eval ::tcl::test::io { namespace import ::tcltest::cleanupTests namespace import ::tcltest::interpreter namespace import ::tcltest::makeFile namespace import ::tcltest::removeFile namespace import ::tcltest::test namespace import ::tcltest::testConstraint namespace import ::tcltest::viewFiletestConstraint testchannel [llength [info commands testchannel]]testConstraint exec [llength [info commands exec]]# You need a *very* special environment to do some tests. In# particular, many file systems do not support large-files...testConstraint largefileSupport 0removeFile test1removeFile pipe# set up a long data file for some of the following testsset path(longfile) [makeFile {} longfile]set f [open $path(longfile) w]fconfigure $f -eofchar {} -translation lffor { set i 0 } { $i < 100 } { incr i} { puts $f "#123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef\#123456789abcdef01\#" }close $fset path(cat) [makeFile { set f stdin if {$argv != ""} { set f [open [lindex $argv 0]] } fconfigure $f -encoding binary -translation lf -blocking 0 -eofchar \x1a fconfigure stdout -encoding binary -translation lf -buffering none fileevent $f readable "foo $f" proc foo {f} { set x [read $f] catch {puts -nonewline $x} if {[eof $f]} { close $f exit 0 } } vwait forever} cat]set thisScript [file join [pwd] [info script]]proc contents {file} { set f [open $file] fconfigure $f -translation binary set a [read $f] close $f return $a}test io-1.5 {Tcl_WriteChars: CheckChannelErrors} {emptyTest} { # no test, need to cause an async error.} {}set path(test1) [makeFile {} test1]test io-1.6 {Tcl_WriteChars: WriteBytes} { set f [open $path(test1) w] fconfigure $f -encoding binary puts -nonewline $f "a\u4e4d\0" close $f contents $path(test1)} "a\x4d\x00"test io-1.7 {Tcl_WriteChars: WriteChars} { set f [open $path(test1) w] fconfigure $f -encoding shiftjis puts -nonewline $f "a\u4e4d\0" close $f contents $path(test1)} "a\x93\xe1\x00"set path(test2) [makeFile {} test2]test io-1.8 {Tcl_WriteChars: WriteChars} { # This test written for SF bug #506297. # # Executing this test without the fix for the referenced bug # applied to tcl will cause tcl, more specifically WriteChars, to # go into an infinite loop. set f [open $path(test2) w] fconfigure $f -encoding iso2022-jp puts -nonewline $f [format %s%c [string repeat " " 4] 12399] close $f contents $path(test2)} " \x1b\$B\$O\x1b(B"test io-2.1 {WriteBytes} { # loop until all bytes are written set f [open $path(test1) w] fconfigure $f -encoding binary -buffersize 16 -translation crlf puts $f "abcdefghijklmnopqrstuvwxyz" close $f contents $path(test1)} "abcdefghijklmnopqrstuvwxyz\r\n"test io-2.2 {WriteBytes: savedLF > 0} { # After flushing buffer, there was a \n left over from the last # \n -> \r\n expansion. It gets stuck at beginning of this buffer. set f [open $path(test1) w] fconfigure $f -encoding binary -buffersize 16 -translation crlf puts -nonewline $f "123456789012345\n12" set x [list [contents $path(test1)]] close $f lappend x [contents $path(test1)]} [list "123456789012345\r" "123456789012345\r\n12"]test io-2.3 {WriteBytes: flush on line} { # Tcl "line" buffering has weird behavior: if current buffer contains # a \n, entire buffer gets flushed. Logical behavior would be to flush # only up to the \n. set f [open $path(test1) w] fconfigure $f -encoding binary -buffering line -translation crlf puts -nonewline $f "\n12" set x [contents $path(test1)] close $f set x} "\r\n12"test io-2.4 {WriteBytes: reset sawLF after each buffer} { set f [open $path(test1) w] fconfigure $f -encoding binary -buffering line -translation lf \ -buffersize 16 puts -nonewline $f "abcdefg\nhijklmnopqrstuvwxyz" set x [list [contents $path(test1)]] close $f lappend x [contents $path(test1)]} [list "abcdefg\nhijklmno" "abcdefg\nhijklmnopqrstuvwxyz"]test io-3.1 {WriteChars: compatibility with WriteBytes} { # loop until all bytes are written set f [open $path(test1) w] fconfigure $f -encoding ascii -buffersize 16 -translation crlf puts $f "abcdefghijklmnopqrstuvwxyz" close $f contents $path(test1)} "abcdefghijklmnopqrstuvwxyz\r\n"test io-3.2 {WriteChars: compatibility with WriteBytes: savedLF > 0} { # After flushing buffer, there was a \n left over from the last # \n -> \r\n expansion. It gets stuck at beginning of this buffer. set f [open $path(test1) w] fconfigure $f -encoding ascii -buffersize 16 -translation crlf puts -nonewline $f "123456789012345\n12" set x [list [contents $path(test1)]] close $f lappend x [contents $path(test1)]} [list "123456789012345\r" "123456789012345\r\n12"]test io-3.3 {WriteChars: compatibility with WriteBytes: flush on line} { # Tcl "line" buffering has weird behavior: if current buffer contains # a \n, entire buffer gets flushed. Logical behavior would be to flush # only up to the \n. set f [open $path(test1) w] fconfigure $f -encoding ascii -buffering line -translation crlf puts -nonewline $f "\n12" set x [contents $path(test1)] close $f set x} "\r\n12"test io-3.4 {WriteChars: loop over stage buffer} { # stage buffer maps to more than can be queued at once. set f [open $path(test1) w] fconfigure $f -encoding jis0208 -buffersize 16 puts -nonewline $f "\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\" set x [list [contents $path(test1)]] close $f lappend x [contents $path(test1)]} [list "!)!)!)!)!)!)!)!)" "!)!)!)!)!)!)!)!)!)!)!)!)!)!)!)"]test io-3.5 {WriteChars: saved != 0} { # Bytes produced by UtfToExternal from end of last channel buffer # had to be moved to beginning of next channel buffer to preserve # requested buffersize. set f [open $path(test1) w] fconfigure $f -encoding jis0208 -buffersize 17 puts -nonewline $f "\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\" set x [list [contents $path(test1)]] close $f lappend x [contents $path(test1)]} [list "!)!)!)!)!)!)!)!)!" "!)!)!)!)!)!)!)!)!)!)!)!)!)!)!)"]test io-3.6 {WriteChars: (stageRead + dstWrote == 0)} { # One incomplete UTF-8 character at end of staging buffer. Backup # in src to the beginning of that UTF-8 character and try again. # # Translate the first 16 bytes, produce 14 bytes of output, 2 left over # (first two bytes of \uff21 in UTF-8). Given those two bytes try # translating them again, find that no bytes are read produced, and break # to outer loop where those two bytes will have the remaining 4 bytes # (the last byte of \uff21 plus the all of \uff22) appended. set f [open $path(test1) w] fconfigure $f -encoding shiftjis -buffersize 16 puts -nonewline $f "12345678901234\uff21\uff22" set x [list [contents $path(test1)]] close $f lappend x [contents $path(test1)]} [list "12345678901234\x82\x60" "12345678901234\x82\x60\x82\x61"]test io-3.7 {WriteChars: (bufPtr->nextAdded > bufPtr->length)} { # When translating UTF-8 to external, the produced bytes went past end # of the channel buffer. This is done purpose -- we then truncate the # bytes at the end of the partial character to preserve the requested # blocksize on flush. The truncated bytes are moved to the beginning # of the next channel buffer. set f [open $path(test1) w] fconfigure $f -encoding jis0208 -buffersize 17 puts -nonewline $f "\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\" set x [list [contents $path(test1)]] close $f lappend x [contents $path(test1)]} [list "!)!)!)!)!)!)!)!)!" "!)!)!)!)!)!)!)!)!)!)!)!)!)!)!)"]test io-3.8 {WriteChars: reset sawLF after each buffer} { set f [open $path(test1) w] fconfigure $f -encoding ascii -buffering line -translation lf \ -buffersize 16 puts -nonewline $f "abcdefg\nhijklmnopqrstuvwxyz" set x [list [contents $path(test1)]] close $f lappend x [contents $path(test1)]} [list "abcdefg\nhijklmno" "abcdefg\nhijklmnopqrstuvwxyz"]test io-4.1 {TranslateOutputEOL: lf} { # search for \n set f [open $path(test1) w] fconfigure $f -buffering line -translation lf puts $f "abcde" set x [list [contents $path(test1)]] close $f lappend x [contents $path(test1)]} [list "abcde\n" "abcde\n"]test io-4.2 {TranslateOutputEOL: cr} { # search for \n, replace with \r set f [open $path(test1) w] fconfigure $f -buffering line -translation cr puts $f "abcde" set x [list [contents $path(test1)]] close $f lappend x [contents $path(test1)]} [list "abcde\r" "abcde\r"]test io-4.3 {TranslateOutputEOL: crlf} { # simple case: search for \n, replace with \r set f [open $path(test1) w] fconfigure $f -buffering line -translation crlf puts $f "abcde" set x [list [contents $path(test1)]] close $f lappend x [contents $path(test1)]} [list "abcde\r\n" "abcde\r\n"]test io-4.4 {TranslateOutputEOL: crlf} { # keep storing more bytes in output buffer until output buffer is full. # We have 13 bytes initially that would turn into 18 bytes. Fill # dest buffer while (dstEnd < dstMax). set f [open $path(test1) w] fconfigure $f -translation crlf -buffersize 16 puts -nonewline $f "1234567\n\n\n\n\nA" set x [list [contents $path(test1)]] close $f lappend x [contents $path(test1)]} [list "1234567\r\n\r\n\r\n\r\n\r" "1234567\r\n\r\n\r\n\r\n\r\nA"]test io-4.5 {TranslateOutputEOL: crlf} { # Check for overflow of the destination buffer set f [open $path(test1) w] fconfigure $f -translation crlf -buffersize 12 puts -nonewline $f "12345678901\n456789012345678901234" close $f set x [contents $path(test1)]} "12345678901\r\n456789012345678901234"test io-5.1 {CheckFlush: not full} { set f [open $path(test1) w] fconfigure $f puts -nonewline $f "12345678901234567890" set x [list [contents $path(test1)]] close $f lappend x [contents $path(test1)]} [list "" "12345678901234567890"]test io-5.2 {CheckFlush: full} { set f [open $path(test1) w] fconfigure $f -buffersize 16 puts -nonewline $f "12345678901234567890" set x [list [contents $path(test1)]] close $f lappend x [contents $path(test1)]} [list "1234567890123456" "12345678901234567890"]test io-5.3 {CheckFlush: not line} { set f [open $path(test1) w] fconfigure $f -buffering line puts -nonewline $f "12345678901234567890" set x [list [contents $path(test1)]] close $f lappend x [contents $path(test1)]} [list "" "12345678901234567890"]test io-5.4 {CheckFlush: line} { set f [open $path(test1) w] fconfigure $f -buffering line -translation lf -encoding ascii puts -nonewline $f "1234567890\n1234567890" set x [list [contents $path(test1)]] close $f lappend x [contents $path(test1)]} [list "1234567890\n1234567890" "1234567890\n1234567890"]test io-5.5 {CheckFlush: none} { set f [open $path(test1) w] fconfigure $f -buffering none puts -nonewline $f "1234567890" set x [list [contents $path(test1)]] close $f lappend x [contents $path(test1)]} [list "1234567890" "1234567890"]test io-6.1 {Tcl_GetsObj: working} { set f [open $path(test1) w] puts $f "foo\nboo" close $f set f [open $path(test1)] set x [gets $f] close $f set x} {foo}test io-6.2 {Tcl_GetsObj: CheckChannelErrors() != 0} { # no test, need to cause an async error.} {}test io-6.3 {Tcl_GetsObj: how many have we used?} { # if (bufPtr != NULL) {oldRemoved = bufPtr->nextRemoved} set f [open $path(test1) w] fconfigure $f -translation crlf puts $f "abc\ndefg" close $f set f [open $path(test1)] set x [list [tell $f] [gets $f line] [tell $f] [gets $f line] $line] close $f set x} {0 3 5 4 defg}test io-6.4 {Tcl_GetsObj: encoding == NULL} { set f [open $path(test1) w] fconfigure $f -translation binary puts $f "\x81\u1234\0" close $f set f [open $path(test1)] fconfigure $f -translation binary set x [list [gets $f line] $line] close $f set x} [list 3 "\x81\x34\x00"]test io-6.5 {Tcl_GetsObj: encoding != NULL} { set f [open $path(test1) w] fconfigure $f -translation binary puts $f "\x88\xea\x92\x9a" close $f set f [open $path(test1)] fconfigure $f -encoding shiftjis set x [list [gets $f line] $line] close $f set x} [list 2 "\u4e00\u4e01"]set a "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"append a $aappend a $atest io-6.6 {Tcl_GetsObj: loop test} { # if (dst >= dstEnd) set f [open $path(test1) w] puts $f $a puts $f hi close $f set f [open $path(test1)] set x [list [gets $f line] $line] close $f set x} [list 256 $a]test io-6.7 {Tcl_GetsObj: error in input} {stdio} { # if (FilterInputBytes(chanPtr, &gs) != 0) set f [open "|[list [interpreter] cat]" w+] puts -nonewline $f "hi\nwould" flush $f gets $f fconfigure $f -blocking 0 set x [gets $f line] close $f set x} {-1}test io-6.8 {Tcl_GetsObj: remember if EOF is seen} { set f [open $path(test1) w] puts $f "abcdef\x1aghijk\nwombat" close $f set f [open $path(test1)] fconfigure $f -eofchar \x1a set x [list [gets $f line] $line [gets $f line] $line] close $f set x} {6 abcdef -1 {}}test io-6.9 {Tcl_GetsObj: remember if EOF is seen} { set f [open $path(test1) w] puts $f "abcdefghijk\nwom\u001abat"
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -