1#
2# copyfile.test
3#
4# Tests for the copyfile command.
5#---------------------------------------------------------------------------
6# Copyright 1992-1999 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: copyfile.test,v 1.1 2001/10/24 23:31:49 hobbs Exp $
16#------------------------------------------------------------------------------
17#
18
19if [cequal [info procs Test] {}] {source testlib.tcl}
20eval $SAVED_UNKNOWN
21
22#
23# Compare the contents of two files.  Return 1 if the same, 0 if different.
24#
25proc TestCompareFiles {fn1 fn2} {
26    set fh1 [open $fn1]
27    fconfigure $fh1 -translation binary
28    set data1 [read $fh1]
29    close $fh1
30    set fh2 [open $fn2]
31    fconfigure $fh2 -translation binary
32    set data2 [read $fh2]
33    close $fh2
34    return [cequal $data1 $data2]
35}
36
37
38# Create a test file
39
40TestRemove COPYFILE1.TMP COPYFILE2.TMP COPYFILE3.TMP
41
42set testFH [open COPYFILE1.TMP w]
43set testFileSize 0
44for {set cnt 0} {$cnt < 100} {incr cnt} {
45     set rec [GenRec $cnt]
46     puts $testFH $rec
47     incr testFileSize [expr [clength $rec]+1]
48     if [cequal $tcl_platform(platform) windows] {
49        incr testFileSize  ;# for <cr>
50     }
51}
52close $testFH
53
54if {$testFileSize != [file size COPYFILE1.TMP]} {
55     error "Wrong file size calculated for COPYFILE1.TMP"
56}
57
58Test copyfile-1.1 {copyfile tests} {
59    set testFH1 [open COPYFILE1.TMP r]
60    set testFH2 [open COPYFILE2.TMP w]
61    copyfile $testFH1 $testFH2
62    close $testFH1
63    close $testFH2
64    TestCompareFiles COPYFILE1.TMP COPYFILE2.TMP
65} 0 1
66
67Test copyfile-1.1.1 {copyfile tests} {
68    # Source/target open read-write.
69    set testFH [open COPYFILE1.TMP r+]
70    set testFH2 [open COPYFILE2.TMP r+]
71    seek $testFH2 0
72    copyfile $testFH $testFH2
73    close $testFH
74    close $testFH2
75    TestCompareFiles COPYFILE1.TMP COPYFILE2.TMP
76} 0 1
77
78Test copyfile-1.2 {copyfile tests} {
79    set testFH [open COPYFILE3.TMP w]
80    set testFH2 [open COPYFILE2.TMP w]
81    set stat [list [catch {copyfile $testFH $testFH2} msg] \
82                   [lrange $msg 2 end]]
83    close $testFH
84    close $testFH2
85    set stat
86} 0 {1 {wasn't opened for reading}}
87
88Test copyfile-1.3 {copyfile tests} {
89    set testFH [open COPYFILE1.TMP r]
90    set testFH2 [open COPYFILE2.TMP r]
91    set stat [list [catch {copyfile $testFH $testFH2} msg] \
92                   [lrange $msg 2 end]]
93    close $testFH
94    close $testFH2
95    set stat
96} 0 {1 {wasn't opened for writing}}
97
98Test copyfile-1.4 {copyfile tests} {
99    copyfile $testFH $testFH2
100} 1 "can not find channel named \"$testFH\""
101
102Test copyfile-1.5 {copyfile tests} {
103    copyfile
104} 1 {wrong # args: copyfile ?-bytes num|-maxbytes num? ?-translate? fromFileId toFileId}
105
106foreach flag {-bytes -maxbytes} {
107    Test copyfile-1.6.$flag {copyfile tests} {
108        set copySize [expr ($testFileSize*2)/3]
109        set testFH [open COPYFILE1.TMP r]
110        set testFH2 [open COPYFILE2.TMP w]
111        copyfile $flag $copySize $testFH $testFH2
112        close $testFH
113        close $testFH2
114        list [expr [file size COPYFILE2.TMP] == $copySize] \
115             [TestCompareFiles COPYFILE1.TMP COPYFILE2.TMP]
116    } 0 {1 0}
117
118    catch {unset testData testData2}
119}
120
121set copySize [expr $testFileSize*2]
122
123Test copyfile-1.7 {copyfile tests} {
124    set testFH [open COPYFILE1.TMP r]
125    set testFH2 [open COPYFILE2.TMP w]
126    set stat [catch {copyfile -bytes $copySize $testFH $testFH2} msg]
127    close $testFH
128    close $testFH2
129    list $stat $msg
130} 0 [list 1 \
131          "premature EOF, $copySize bytes expected, $testFileSize bytes actually read"]
132
133Test copyfile-1.7 {copyfile tests} {
134    set testFH [open COPYFILE1.TMP r]
135    set testFH2 [open COPYFILE2.TMP w]
136    set stat [catch {copyfile -maxbytes $copySize $testFH $testFH2} msg]
137    close $testFH
138    close $testFH2
139    list $stat $msg
140} 0 [list 0 $testFileSize]
141
142TestRemove COPYFILE1.TMP COPYFILE2.TMP COPYFILE3.TMP
143
144rename unknown {}
145
146