home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / devel / tcl / tclx7_31.z / tclx7_31 / tcldev / tclX7.3a-p1 / tests / filecmds.test < prev    next >
Encoding:
Text File  |  1994-01-24  |  5.4 KB  |  199 lines

  1. #
  2. # filecmds.test
  3. #
  4. # Tests for the copyfile, pipe, and frename commands.
  5. #---------------------------------------------------------------------------
  6. # Copyright 1992-1993 Karl Lehenbauer and Mark Diekhans.
  7. #
  8. # Permission to use, copy, modify, and distribute this software and its
  9. # documentation for any purpose and without fee is hereby granted, provided
  10. # that the above copyright notice appear in all copies.  Karl Lehenbauer and
  11. # Mark Diekhans make no representations about the suitability of this
  12. # software for any purpose.  It is provided "as is" without express or
  13. # implied warranty.
  14. #------------------------------------------------------------------------------
  15. # $Id: filecmds.test,v 3.3 1994/01/24 04:18:18 markd Exp $
  16. #------------------------------------------------------------------------------
  17. #
  18.  
  19. if {[info procs test] != "test"} then {source testlib.tcl}
  20.  
  21. #
  22. # Fork without exec will not work under Tk, skip this test
  23. #
  24. if ![lempty [info commands button]] {
  25.     puts stderr "*************************************************************"
  26.     puts stderr "File commands tests are constructed in a way that does not"
  27.     puts stderr "work under Tk.  Test skipped."
  28.     puts stderr "*************************************************************"
  29.     puts stderr ""
  30.     return
  31. }
  32.  
  33. # Create a test file
  34.  
  35. catch {unlink [glob IOTEST*.TMP]}
  36.  
  37. set testFH [open IOTEST.TMP w]
  38. set testFileSize 0
  39. for {set cnt 0} {$cnt < 100} {incr cnt} {
  40.      set rec [GenRec $cnt]
  41.      puts $testFH $rec
  42.      incr testFileSize [expr [clength $rec]+1]
  43. }
  44. close $testFH
  45.  
  46. if {$testFileSize != [file size IOTEST.TMP]} {
  47.      error "Wrong file size calculated for IOTEST.TMP"
  48. }
  49.  
  50. Test filecmds-3.1 {copyfile tests} {
  51.     set testFH [open IOTEST.TMP r]
  52.     set testFH2 [open IOTEST2.TMP w]
  53.     copyfile $testFH $testFH2
  54.     close $testFH
  55.     close $testFH2
  56.     system "diff IOTEST.TMP IOTEST2.TMP >/dev/null 2>&1"
  57. } 0 0
  58.  
  59. Test filecmds-3.2 {copyfile tests} {
  60.     set testFH [open IOTEST3.TMP w]
  61.     set testFH2 [open IOTEST2.TMP w]
  62.     set stat [list [catch {copyfile $testFH $testFH2} msg] \
  63.                    [lrange $msg 1 end]]
  64.     close $testFH
  65.     close $testFH2
  66.     set stat
  67. } 0 {1 {wasn't opened for reading}}
  68.  
  69. Test filecmds-3.3 {copyfile tests} {
  70.     set testFH [open IOTEST.TMP r]
  71.     set testFH2 [open IOTEST2.TMP r]
  72.     set stat [list [catch {copyfile $testFH $testFH2} msg] \
  73.                    [lrange $msg 1 end]]
  74.     close $testFH
  75.     close $testFH2
  76.     set stat
  77. } 0 {1 {wasn't opened for writing}}
  78.  
  79. Test filecmds-3.4 {copyfile tests} {
  80.     copyfile $testFH $testFH2
  81. } 1 "file \"$testFH\" isn't open"
  82.  
  83. Test filecmds-3.5 {copyfile tests} {
  84.     copyfile
  85. } 1 {wrong # args: copyfile ?-bytes num|-maxbytes num? fromFileId toFileId}
  86.  
  87. foreach flag {-bytes -maxbytes} {
  88.     Test filecmds-3.6.$flag {copyfile tests} {
  89.         set copySize [expr ($testFileSize*2)/3]
  90.         set testFH [open IOTEST.TMP r]
  91.         set testFH2 [open IOTEST2.TMP w]
  92.         copyfile $flag $copySize $testFH $testFH2
  93.         close $testFH
  94.         close $testFH2
  95.  
  96.         set testFH [open IOTEST.TMP r]
  97.         set testData [read $testFH $copySize]
  98.         close $testFH
  99.  
  100.         set testFH2 [open IOTEST2.TMP r]
  101.         set testData2 [read $testFH2]
  102.         close $testFH2
  103.  
  104.         list [expr [file size IOTEST2.TMP] == $copySize] \
  105.              [string compare $testData $testData2]
  106.     } 0 {1 0}
  107.  
  108.     catch {unset testData testData2}
  109. }
  110.  
  111. set copySize [expr $testFileSize*2]
  112.  
  113. Test filecmds-3.7 {copyfile tests} {
  114.     set testFH [open IOTEST.TMP r]
  115.     set testFH2 [open IOTEST2.TMP w]
  116.     set stat [catch {copyfile -bytes $copySize $testFH $testFH2} msg]
  117.     close $testFH
  118.     close $testFH2
  119.     list $stat $msg
  120. } 0 [list 1 \
  121.           "premature EOF, $copySize bytes expected, $testFileSize bytes actually read"]
  122.  
  123. Test filecmds-3.7 {copyfile tests} {
  124.     set testFH [open IOTEST.TMP r]
  125.     set testFH2 [open IOTEST2.TMP w]
  126.     set stat [catch {copyfile -maxbytes $copySize $testFH $testFH2} msg]
  127.     close $testFH
  128.     close $testFH2
  129.     list $stat $msg
  130. } 0 [list 0 $testFileSize]
  131.  
  132. pipe readPF writePF
  133.  
  134. flush stdout  ;# Not going to exec, must clean up the buffers.
  135. flush stderr
  136. set sonPid [fork]
  137.  
  138. if {$sonPid == 0} {
  139.     for {set cnt 0} {$cnt < 50} {incr cnt} {
  140.         Test filecmds-4.1 {pipe tests} {
  141.             if {![gets $readPF msgBuf]} {
  142.                set msgBuf "Premature eof on pipe"
  143.             }
  144.             set msgBuf
  145.         } 0 [GenRec $cnt]
  146.     }
  147.     close $readPF
  148.     exit 0
  149. }
  150.  
  151. for {set cnt 0} {$cnt < 50} {incr cnt} {
  152.     puts $writePF [GenRec $cnt]
  153. }
  154. flush $writePF
  155. Test filecmds-4.2 {pipe tests} {
  156.     wait $sonPid
  157. } 0 "$sonPid EXIT 0"
  158.  
  159. close $readPF
  160. close $writePF
  161.  
  162. Test filecmds-5.1 {frename tests} {
  163.     frename
  164. } 1 {wrong # args: frename oldPath newPath}
  165.  
  166. Test filecmds-5.2 {frename tests} {
  167.     frename a b c
  168. } 1 {wrong # args: frename oldPath newPath}
  169.  
  170. Test filecmds-5.3 {frename tests} {
  171.     frename IOTEST.TMP IOTEST4.TMP
  172.     file exists IOTEST4.TMP
  173. } 0 1
  174.  
  175. Test filecmds-5.4 {frename tests} {
  176.     unlink -nocomplain IOTEST5.TMP
  177.     list [catch {frename IOTEST5.TMP IOTEST6.TMP} msg] [string tolower $msg]
  178. } 0 {1 {rename "iotest5.tmp" to "iotest6.tmp" failed: no such file or directory}}
  179.  
  180. Test filecmds-6.1 {readdir tests} {
  181.     readdir
  182. } 1 {wrong # args: readdir dirPath}
  183.  
  184. Test filecmds-6.2 {readdir tests} {
  185.     readdir x y
  186. } 1 {wrong # args: readdir dirPath}
  187.  
  188. tcltouch READDIR.TMP/AAA
  189. tcltouch READDIR.TMP/BBB
  190. tcltouch READDIR.TMP/CCC
  191. tcltouch READDIR.TMP/DDD
  192.  
  193. Test filecmds-6.3 {readdir tests} {
  194.     lsort [readdir READDIR.TMP]
  195. } 0 {AAA BBB CCC DDD}
  196.  
  197. system {rm -rf *.TMP}
  198.  
  199.