1#
2# lgets.test
3#
4# Tests for the lgets 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: lgets.test,v 1.3 2002/04/04 06:10:30 hobbs Exp $
16#------------------------------------------------------------------------------
17#
18
19if {[cequal [info procs Test] {}]} {
20    source [file join [file dirname [info script]] testlib.tcl]
21}
22
23test lgets-1.1 {lgets command} {
24    list [catch {lgets} msg] $msg
25} {1 {wrong # args: lgets fileId ?varName?}}
26
27test lgets-1.2 {lgets command} {
28    list [catch {lgets a b c} msg] $msg
29} {1 {wrong # args: lgets fileId ?varName?}}
30
31test lgets-1.3 {lgets command} {
32    list [catch {lgets a} msg] $msg
33} {1 {can not find channel named "a"}}
34
35set f [open test2.tmp w]
36test lgets-1.4 {lgets command} {
37    list [catch {lgets $f} msg] $msg
38} [list 1 "channel \"$f\" wasn't opened for reading"]
39catch {close $f}
40
41set test2data \
42        [replicate abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ 100]
43set f [open test2.tmp w]
44puts $f $test2data
45close $f
46
47test lgets-1.5 {lgets command with long line} {
48    set f [open test2.tmp]
49    set x [lgets $f]
50    close $f
51    set x
52} $test2data
53
54test lgets-1.6 {lgets command with long line} {
55    set f [open test2.tmp]
56    set x [lgets $f y]
57    close $f
58    list $x $y
59} [list [clength $test2data] $test2data]
60
61test lgets-1.7 {lgets command with EOF in list element} {
62    set f [open test2.tmp w]
63    puts $f "Test1  \{Test2 " nonewline
64    close $f
65    set f [open test2.tmp]
66    list [catch {lgets $f} msg] $msg
67} {1 {EOF encountered before newline while reading list from channel}}
68
69catch {close $f}
70
71test lgets-1.8 {lgets command with EOF in list} {
72    set f [open test2.tmp w]
73    puts $f "Test1\nTest2" nonewline
74    close $f
75    set f [open test2.tmp]
76    set x {}
77    set y {}
78    lappend x [lgets $f y] $y
79    set y {}
80    lappend x [catch {lgets $f y} r] $r $y
81    close $f
82    set x
83} {5 Test1 1 {EOF encountered before newline while reading list from channel} Test2}
84
85test lgets-2.1 {lgets command} {
86    catch {unset data}
87    set data(0) [list aaa b cccc]
88    set data(1) [list aaaa \" ccc]
89    set data(2) [list aaaaa \" cc]
90    set data(3) [list aaaaa \"\" \} \{ \n c]
91    set data(4) [list bcad \n defg \n nypq]
92    set data(5) [list {BCAD  {AAA
93BBB
94CCC}
95defg
96nypq}]
97    set data(6) [list [replicate ABD 200]  "\n" [replicate XYZ 100]]
98    set data(7) [list [replicate ABD 255]  "\n" [replicate XYZ 100]]
99    set data(8) [list [replicate ABD 256]  "\n" [replicate XYZ 100]]
100    set data(9) [list [replicate "AAA\n \{B \"BB" 100]]
101
102    set fh [open test1.tmp w]
103    foreach idx [lsort -integer [array names data]] {
104        puts $fh $data($idx)
105    }
106    close $fh
107    set fh [open test1.tmp]
108    concat {}
109} {}
110
111test lgets-2.2 {lgets command} {
112    lgets $fh
113} $data(0)
114
115test lgets-2.3 {lgets command} {
116    lgets $fh retvar
117    set retvar
118} $data(1)
119
120test lgets-2.4 {lgets command} {
121    lgets $fh
122} $data(2)
123
124test lgets-2.5 {lgets command} {
125    lgets $fh retvar
126    set retvar
127} $data(3)
128
129test lgets-2.6 {lgets command} {
130    lgets $fh
131} $data(4)
132
133test lgets-2.7 {lgets command} {
134    lgets $fh retvar
135    set retvar
136} $data(5)
137
138test lgets-2.8 {lgets command} {
139    lgets $fh retvar
140    set retvar
141} $data(6)
142
143test lgets-2.9 {lgets command} {
144    lgets $fh retvar
145    set retvar
146} $data(7)
147
148test lgets-2.10 {lgets command} {
149    lgets $fh retvar
150    set retvar
151} $data(8)
152
153test lgets-2.11 {lgets command} {
154    lgets $fh retvar
155    set retvar
156} $data(9)
157
158catch {close $fh}
159unset data
160
161# Make sure odd lists work.
162set data [list ERR_REQUEST_PROCESSOR_FAILURE " {sc ...\""]
163
164test lgets-3.1 {lgets command} {
165    set fh [open test1.tmp w+]
166    puts $fh $data
167    seek $fh 0
168    lgets $fh
169} $data
170
171catch {close $fh}
172
173# Easy test to make sure basic non-blocked channel works, even though we
174# don't actually test the no-data case.
175
176# FIX: Doesn't work right on Win32.
177if [cequal $tcl_platform(platform) windows] {
178    echo "    * lgets tests not completely ported to Win32, some tests skipped"
179    TestRemove test1.tmp test2.tmp
180    return
181}
182
183pipe rpipe wpipe
184fconfigure $rpipe -blocking 0
185
186test lgets-4.1 {lgets on non-blocked channel} {tempNotPc} {
187    puts $wpipe $data
188    flush $wpipe
189    list [catch {lgets $rpipe} msg] $msg
190} {1 {channel is non-blocking; not currently supported by the lgets command}}
191
192test lgets-4.2 {lgets on non-blocked channel} {tempNotPc} {
193    puts $wpipe $data
194    flush $wpipe
195    catch {unset x}
196    list [catch {lgets $rpipe x} msg] $msg
197} {1 {channel is non-blocking; not currently supported by the lgets command}}
198
199catch {close $rpipe}
200catch {close $wpipe}
201unset data
202
203
204#
205# Binary data 
206#
207
208test lgets-5.1 {lgets with binary data} {
209    catch {unset data}
210    set data(0) [list aaa b cc\0cc]
211    set data(1) [list aaaa \" \0\0  ccc]
212    set data(2) [list aaaaa \" cc]
213    set data(3) [list aaaaa \"\" \} [replicate \0\1\0 5] \{ \n c]
214    set data(4) [list bcad \n defg \n nypq]
215    set data(5) [list BCAD  "AAA
216\0
217BBB
218CCC" "
219defg
220\0\0\0" nypq]
221    set data(6) [list [replicate ABD 200]  "\n" [replicate XYZ 100]]
222    set data(7) [list [replicate ABD 255]  "\n" [replicate XYZ 100]]
223    set data(8) [list [replicate ABD 256]  "\n" [replicate XYZ 100]]
224    set data(9) [list [replicate "AAA\n \{B \"BB" 100]]
225
226    set fh [open test1.tmp w]
227    foreach idx [lsort -integer [array names data]] {
228        puts $fh $data($idx)
229    }
230    close $fh
231    set fh [open test1.tmp]
232    concat {}
233} {}
234
235test lgets-5.2 {lgets with binary data} {
236    lgets $fh
237} $data(0)
238
239test lgets-5.3 {lgets with binary data} {
240    lgets $fh retvar
241    set retvar
242} $data(1)
243
244test lgets-5.4 {lgets with binary data} {
245    lgets $fh
246} $data(2)
247
248test lgets-5.5 {lgets with binary data} {
249    lgets $fh retvar
250    set retvar
251} $data(3)
252
253test lgets-5.6 {lgets with binary data} {
254    lgets $fh
255} $data(4)
256
257test lgets-5.7 {lgets with binary data} {
258    lgets $fh retvar
259    set retvar
260} $data(5)
261
262test lgets-5.8 {lgets with binary data} {
263    lgets $fh retvar
264    set retvar
265} $data(6)
266
267test lgets-5.9 {lgets with binary data} {
268    lgets $fh retvar
269    set retvar
270} $data(7)
271
272test lgets-5.10 {lgets with binary data} {
273    lgets $fh retvar
274    set retvar
275} $data(8)
276
277test lgets-5.11 {lgets with binary data} {
278    lgets $fh retvar
279    set retvar
280} $data(9)
281
282catch {close $fh}
283unset data
284
285test lgets-6.0 {lgets corner cases} {
286    set fh [open test2.tmp w+]
287    puts $fh [list {\\server} {\home} {foo\}}]
288    seek $fh 0
289    lgets $fh inlist
290    close $fh
291    set inlist
292} [list {\\server} {\home} {foo\}}]
293
294
295TestRemove test1.tmp test2.tmp
296
297# cleanup
298::tcltest::cleanupTests
299return
300