#
# copyfile.test
#
# Tests for the copyfile command.
#---------------------------------------------------------------------------
# Copyright 1992-1999 Karl Lehenbauer and Mark Diekhans.
#
# Permission to use, copy, modify, and distribute this software and its
# documentation for any purpose and without fee is hereby granted, provided
# that the above copyright notice appear in all copies.  Karl Lehenbauer and
# Mark Diekhans make no representations about the suitability of this
# software for any purpose.  It is provided "as is" without express or
# implied warranty.
#------------------------------------------------------------------------------
# $Id: copyfile.test,v 1.1.1.1 1999/06/28 20:35:29 damon Exp $
#------------------------------------------------------------------------------
#

if [cequal [info procs Test] {}] {source testlib.tcl}
eval $SAVED_UNKNOWN

#
# Compare the contents of two files.  Return 1 if the same, 0 if different.
#
proc TestCompareFiles {fn1 fn2} {
    set fh1 [open $fn1]
    fconfigure $fh1 -translation binary
    set data1 [read $fh1]
    close $fh1
    set fh2 [open $fn2]
    fconfigure $fh2 -translation binary
    set data2 [read $fh2]
    close $fh2
    return [cequal $data1 $data2]
}


# Create a test file

TestRemove COPYFILE1.TMP COPYFILE2.TMP COPYFILE3.TMP

set testFH [open COPYFILE1.TMP w]
set testFileSize 0
for {set cnt 0} {$cnt < 100} {incr cnt} {
     set rec [GenRec $cnt]
     puts $testFH $rec
     incr testFileSize [expr [clength $rec]+1]
     if [cequal $tcl_platform(platform) windows] {
        incr testFileSize  ;# for <cr>
     }
}
close $testFH

if {$testFileSize != [file size COPYFILE1.TMP]} {
     error "Wrong file size calculated for COPYFILE1.TMP"
}

Test copyfile-1.1 {copyfile tests} {
    set testFH1 [open COPYFILE1.TMP r]
    set testFH2 [open COPYFILE2.TMP w]
    copyfile $testFH1 $testFH2
    close $testFH1
    close $testFH2
    TestCompareFiles COPYFILE1.TMP COPYFILE2.TMP
} 0 1

Test copyfile-1.1.1 {copyfile tests} {
    # Source/target open read-write.
    set testFH [open COPYFILE1.TMP r+]
    set testFH2 [open COPYFILE2.TMP r+]
    seek $testFH2 0
    copyfile $testFH $testFH2
    close $testFH
    close $testFH2
    TestCompareFiles COPYFILE1.TMP COPYFILE2.TMP
} 0 1

Test copyfile-1.2 {copyfile tests} {
    set testFH [open COPYFILE3.TMP w]
    set testFH2 [open COPYFILE2.TMP w]
    set stat [list [catch {copyfile $testFH $testFH2} msg] \
                   [lrange $msg 2 end]]
    close $testFH
    close $testFH2
    set stat
} 0 {1 {wasn't opened for reading}}

Test copyfile-1.3 {copyfile tests} {
    set testFH [open COPYFILE1.TMP r]
    set testFH2 [open COPYFILE2.TMP r]
    set stat [list [catch {copyfile $testFH $testFH2} msg] \
                   [lrange $msg 2 end]]
    close $testFH
    close $testFH2
    set stat
} 0 {1 {wasn't opened for writing}}

Test copyfile-1.4 {copyfile tests} {
    copyfile $testFH $testFH2
} 1 "can not find channel named \"$testFH\""

Test copyfile-1.5 {copyfile tests} {
    copyfile
} 1 {wrong # args: copyfile ?-bytes num|-maxbytes num? ?-translate? fromFileId toFileId}

foreach flag {-bytes -maxbytes} {
    Test copyfile-1.6.$flag {copyfile tests} {
        set copySize [expr ($testFileSize*2)/3]
        set testFH [open COPYFILE1.TMP r]
        set testFH2 [open COPYFILE2.TMP w]
        copyfile $flag $copySize $testFH $testFH2
        close $testFH
        close $testFH2
        list [expr [file size COPYFILE2.TMP] == $copySize] \
             [TestCompareFiles COPYFILE1.TMP COPYFILE2.TMP]
    } 0 {1 0}

    catch {unset testData testData2}
}

set copySize [expr $testFileSize*2]

Test copyfile-1.7 {copyfile tests} {
    set testFH [open COPYFILE1.TMP r]
    set testFH2 [open COPYFILE2.TMP w]
    set stat [catch {copyfile -bytes $copySize $testFH $testFH2} msg]
    close $testFH
    close $testFH2
    list $stat $msg
} 0 [list 1 \
          "premature EOF, $copySize bytes expected, $testFileSize bytes actually read"]

Test copyfile-1.7 {copyfile tests} {
    set testFH [open COPYFILE1.TMP r]
    set testFH2 [open COPYFILE2.TMP w]
    set stat [catch {copyfile -maxbytes $copySize $testFH $testFH2} msg]
    close $testFH
    close $testFH2
    list $stat $msg
} 0 [list 0 $testFileSize]

TestRemove COPYFILE1.TMP COPYFILE2.TMP COPYFILE3.TMP

rename unknown {}

