OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [tags/] [start/] [insight/] [tcl/] [tests/] [io.test] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 578 markom
# Functionality covered: operation of all IO commands, and all procedures
2
# defined in generic/tclIO.c.
3
#
4
# This file contains a collection of tests for one or more of the Tcl
5
# built-in commands.  Sourcing this file into Tcl runs the tests and
6
# generates output for errors.  No output means no errors were found.
7
#
8
# Copyright (c) 1991-1994 The Regents of the University of California.
9
# Copyright (c) 1994-1997 Sun Microsystems, Inc.
10
#
11
# See the file "license.terms" for information on usage and redistribution
12
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
13
#
14
# RCS: @(#) $Id: io.test,v 1.1.1.1 2002-01-16 10:25:36 markom Exp $
15
 
16
if {[string compare test [info procs test]] == 1} then {source defs}
17
 
18
if {"[info commands testchannel]" != "testchannel"} {
19
    puts "Skipping io tests. This application does not seem to have the"
20
    puts "testchannel command that is needed to run these tests."
21
    return
22
}
23
 
24
removeFile test1
25
removeFile pipe
26
 
27
# set up a long data file for some of the following tests
28
 
29
set f [open longfile w]
30
fconfigure $f -eofchar {} -translation lf
31
for { set i 0 } { $i < 100 } { incr i} {
32
    puts $f "#123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef
33
\#123456789abcdef01
34
\#"
35
    }
36
close $f
37
 
38
set f [open cat w]
39
puts $f {
40
    if {$argv == {}} {
41
        set argv -
42
    }
43
    foreach name $argv {
44
        if {$name == "-"} {
45
            set f stdin
46
        } elseif {[catch {open $name r} f] != 0} {
47
            puts stderr $f
48
            continue
49
        }
50
        while {[eof $f] == 0} {
51
            puts -nonewline stdout [read $f]
52
        }
53
        if {$f != "stdin"} {
54
            close $f
55
        }
56
    }
57
}
58
close $f
59
 
60
# These tests are disabled until we decide what to do with "unsupported0".
61
#
62
#test io-1.7 {unsupported0 command} {
63
#    removeFile test1
64
#    set f1 [open iocmd.test]
65
#    set f2 [open test1 w]
66
#    unsupported0 $f1 $f2
67
#    close $f1
68
#    catch {close $f2}
69
#    set s1 [file size [info script]]
70
#    set s2 [file size test1]
71
#    set x ok
72
#    if {"$s1" != "$s2"} {
73
#        set x broken
74
#    }
75
#    set x
76
#} ok
77
#test io-1.8 {unsupported0 command} {
78
#    removeFile test1
79
#    set f1 [open [info script]]
80
#    set f2 [open test1 w]
81
#    unsupported0 $f1 $f2 40
82
#    close $f1
83
#    close $f2
84
#    file size test1
85
#} 40
86
#test io-1.9 {unsupported0 command} {
87
#    removeFile test1
88
#    set f1 [open [info script]]
89
#    set f2 [open test1 w]
90
#    unsupported0 $f1 $f2 -1
91
#    close $f1
92
#    close $f2
93
#    set x ok
94
#    set s1 [file size [info script]]
95
#    set s2 [file size test1]
96
#    if {$s1 != $s2} {
97
#        set x broken
98
#    }
99
#    set x
100
#} ok
101
#test io-1.10 {unsupported0 command} {unixOrPc} {
102
#    removeFile pipe
103
#    removeFile test1
104
#    set f1 [open pipe w]
105
#    puts $f1 {puts ready}
106
#    puts $f1 {gets stdin}
107
#    puts $f1 {set f1 [open [info script] r]}
108
#    puts $f1 {puts [read $f1 100]}
109
#    puts $f1 {close $f1}
110
#    close $f1
111
#    set f1 [open "|[list $tcltest pipe]" r+]
112
#    gets $f1
113
#    puts $f1 ready
114
#    flush $f1
115
#    set f2 [open test1 w]
116
#    set c [unsupported0 $f1 $f2 40]
117
#    catch {close $f1}
118
#    close $f2
119
#    set s1 [file size test1]
120
#    set x ok
121
#    if {$s1 != "40"} {
122
#        set x broken
123
#    }
124
#    list $c $x
125
#} {40 ok}
126
 
127
# Test standard handle management. The functions tested are
128
# Tcl_SetStdChannel and Tcl_GetStdChannel. Incidentally we are
129
# also testing channel table management.
130
 
131
if {$tcl_platform(platform) == "macintosh"} {
132
    set consoleFileNames [list console0 console1 console2]
133
} else {
134
    set consoleFileNames [lsort [testchannel open]]
135
}
136
test io-1.1 {Tcl_SetStdChannel and Tcl_GetStdChannel} {
137
    set l ""
138
    lappend l [fconfigure stdin -buffering]
139
    lappend l [fconfigure stdout -buffering]
140
    lappend l [fconfigure stderr -buffering]
141
    lappend l [lsort [testchannel open]]
142
    set l
143
} [list line line none $consoleFileNames]
144
test io-1.2 {Tcl_SetStdChannel and Tcl_GetStdChannel} {
145
    interp create x
146
    set l ""
147
    lappend l [x eval {fconfigure stdin -buffering}]
148
    lappend l [x eval {fconfigure stdout -buffering}]
149
    lappend l [x eval {fconfigure stderr -buffering}]
150
    interp delete x
151
    set l
152
} {line line none}
153
test io-1.3 {Tcl_SetStdChannel & Tcl_GetStdChannel} {stdio} {
154
    set f [open test1 w]
155
    puts $f {
156
        close stdin
157
        close stdout
158
        close stderr
159
        set f [open test1 r]
160
        set f2 [open test2 w]
161
        set f3 [open test3 w]
162
        puts stdout [gets stdin]
163
        puts stdout out
164
        puts stderr err
165
        close $f
166
        close $f2
167
        close $f3
168
    }
169
    close $f
170
    set result [exec $tcltest test1]
171
    set f [open test2 r]
172
    set f2 [open test3 r]
173
    lappend result [read $f] [read $f2]
174
    close $f
175
    close $f2
176
    set result
177
} {{
178
out
179
} {err
180
}}
181
# This test relies on the fact that the smallest available fd is used first.
182
test io-1.4 {Tcl_SetStdChannel & Tcl_GetStdChannel} {unixOnly} {
183
    set f [open test1 w]
184
    puts $f { close stdin
185
        close stdout
186
        close stderr
187
        set f [open test1 r]
188
        set f2 [open test2 w]
189
        set f3 [open test3 w]
190
        puts stdout [gets stdin]
191
        puts stdout $f2
192
        puts stderr $f3
193
        close $f
194
        close $f2
195
        close $f3
196
    }
197
    close $f
198
    set result [exec $tcltest test1]
199
    set f [open test2 r]
200
    set f2 [open test3 r]
201
    lappend result [read $f] [read $f2]
202
    close $f
203
    close $f2
204
    set result
205
} {{ close stdin
206
file1
207
} {file2
208
}}
209
catch {interp delete z}
210
test io-1.5 {Tcl_GetChannel: stdio name translation} {
211
    interp create z
212
    eof stdin
213
    catch {z eval flush stdin} msg1
214
    catch {z eval close stdin} msg2
215
    catch {z eval flush stdin} msg3
216
    set result [list $msg1 $msg2 $msg3]
217
    interp delete z
218
    set result
219
} {{channel "stdin" wasn't opened for writing} {} {can not find channel named "stdin"}}
220
test io-1.6 {Tcl_GetChannel: stdio name translation} {
221
    interp create z
222
    eof stdout
223
    catch {z eval flush stdout} msg1
224
    catch {z eval close stdout} msg2
225
    catch {z eval flush stdout} msg3
226
    set result [list $msg1 $msg2 $msg3]
227
    interp delete z
228
    set result
229
} {{} {} {can not find channel named "stdout"}}
230
test io-1.7 {Tcl_GetChannel: stdio name translation} {
231
    interp create z
232
    eof stderr
233
    catch {z eval flush stderr} msg1
234
    catch {z eval close stderr} msg2
235
    catch {z eval flush stderr} msg3
236
    set result [list $msg1 $msg2 $msg3]
237
    interp delete z
238
    set result
239
} {{} {} {can not find channel named "stderr"}}
240
test io-1.8 {reuse of stdio special channels} {unixOnly} {
241
    removeFile script
242
    removeFile test1
243
    set f [open script w]
244
    puts $f {
245
        close stderr
246
        set f [open test1 w]
247
        puts stderr hello
248
        close $f
249
        set f [open test1 r]
250
        puts [gets $f]
251
    }
252
    close $f
253
    set f [open "|[list $tcltest script]" r]
254
    set c [gets $f]
255
    close $f
256
    set c
257
} hello
258
test io-1.9 {reuse of stdio special channels} {stdio} {
259
    removeFile script
260
    removeFile test1
261
    set f [open script w]
262
    puts $f {
263
        set f [open test1 w]
264
        puts $f hello
265
        close $f
266
        close stderr
267
        set f [open "|[list [info nameofexecutable] cat test1]" r]
268
        puts [gets $f]
269
    }
270
    close $f
271
    set f [open "|[list $tcltest script]" r]
272
    set c [gets $f]
273
    close $f
274
    set c
275
} hello
276
 
277
# Must add test function for testing Tcl_CreateCloseHandler and
278
# Tcl_DeleteCloseHandler.
279
 
280
# Test channel table management. The functions tested are
281
# GetChannelTable, DeleteChannelTable, Tcl_RegisterChannel,
282
# Tcl_UnregisterChannel, Tcl_GetChannel and Tcl_CreateChannel.
283
#
284
# These functions use "eof stdin" to ensure that the standard
285
# channels are added to the channel table of the interpreter.
286
 
287
#
288
# CYGNUS LOCAL:
289
# I open tclConfig.sh to get the correct paths if I am not in the install
290
# directory.  This increments the refcount on the stdin WHEN the interpreter
291
# is created, not when you call eof stdin in the child.  Because of this, I
292
# had to change the first value in the results for tests 2.1, 2.2 & 2.3 from
293
# 0 to 1.  This is really a side issue, and does not affect what the tests
294
# were supposed to be looking for, however.
295
 
296
test io-2.1 {GetChannelTable, DeleteChannelTable on std handles} {
297
    set l1 [testchannel refcount stdin]
298
    eof stdin
299
    interp create x
300
    set l ""
301
    lappend l [expr [testchannel refcount stdin] - $l1]
302
    x eval {eof stdin}
303
    lappend l [expr [testchannel refcount stdin] - $l1]
304
    interp delete x
305
    lappend l [expr [testchannel refcount stdin] - $l1]
306
    set l
307
} {1 1 0}
308
test io-2.2 {GetChannelTable, DeleteChannelTable on std handles} {
309
    set l1 [testchannel refcount stdout]
310
    eof stdin
311
    interp create x
312
    set l ""
313
    lappend l [expr [testchannel refcount stdout] - $l1]
314
    x eval {eof stdout}
315
    lappend l [expr [testchannel refcount stdout] - $l1]
316
    interp delete x
317
    lappend l [expr [testchannel refcount stdout] - $l1]
318
    set l
319
} {1 1 0}
320
test io-2.3 {GetChannelTable, DeleteChannelTable on std handles} {
321
    set l1 [testchannel refcount stderr]
322
    eof stdin
323
    interp create x
324
    set l ""
325
    lappend l [expr [testchannel refcount stderr] - $l1]
326
    x eval {eof stderr}
327
    lappend l [expr [testchannel refcount stderr] - $l1]
328
    interp delete x
329
    lappend l [expr [testchannel refcount stderr] - $l1]
330
    set l
331
} {1 1 0}
332
test io-2.4 {Tcl_RegisterChannel, Tcl_UnregisterChannel} {
333
    removeFile test1
334
    set l ""
335
    set f [open test1 w]
336
    lappend l [lindex [testchannel info $f] 15]
337
    close $f
338
    if {[catch {lindex [testchannel info $f] 15} msg]} {
339
        lappend l $msg
340
    } else {
341
        lappend l "very broken: $f found after being closed"
342
    }
343
    string compare [string tolower $l] \
344
        [list 1 [format "can not find channel named \"%s\"" $f]]
345
} 0
346
test io-2.5 {Tcl_RegisterChannel, Tcl_UnregisterChannel} {
347
    removeFile test1
348
    set l ""
349
    set f [open test1 w]
350
    lappend l [lindex [testchannel info $f] 15]
351
    interp create x
352
    interp share "" $f x
353
    lappend l [lindex [testchannel info $f] 15]
354
    x eval close $f
355
    lappend l [lindex [testchannel info $f] 15]
356
    interp delete x
357
    lappend l [lindex [testchannel info $f] 15]
358
    close $f
359
    if {[catch {lindex [testchannel info $f] 15} msg]} {
360
        lappend l $msg
361
    } else {
362
        lappend l "very broken: $f found after being closed"
363
    }
364
    string compare [string tolower $l] \
365
        [list 1 2 1 1 [format "can not find channel named \"%s\"" $f]]
366
} 0
367
test io-2.6 {Tcl_RegisterChannel, Tcl_UnregisterChannel} {
368
    removeFile test1
369
    set l ""
370
    set f [open test1 w]
371
    lappend l [lindex [testchannel info $f] 15]
372
    interp create x
373
    interp share "" $f x
374
    lappend l [lindex [testchannel info $f] 15]
375
    interp delete x
376
    lappend l [lindex [testchannel info $f] 15]
377
    close $f
378
    if {[catch {lindex [testchannel info $f] 15} msg]} {
379
        lappend l $msg
380
    } else {
381
        lappend l "very broken: $f found after being closed"
382
    }
383
    string compare [string tolower $l] \
384
        [list 1 2 1 [format "can not find channel named \"%s\"" $f]]
385
} 0
386
test io-2.7 {Tcl_GetChannel->Tcl_GetStdChannel, standard handles} {
387
    eof stdin
388
} 0
389
test io-2.8 {testing Tcl_GetChannel, user opened handle} {
390
    removeFile test1
391
    set f [open test1 w]
392
    set x [eof $f]
393
    close $f
394
    set x
395
} 0
396
test io-2.9 {Tcl_GetChannel, channel not found} {
397
    list [catch {eof file34} msg] $msg
398
} {1 {can not find channel named "file34"}}
399
test io-2.10 {Tcl_CreateChannel, insertion into channel table} {
400
    removeFile test1
401
    set f [open test1 w]
402
    set l ""
403
    lappend l [eof $f]
404
    close $f
405
    if {[catch {lindex [testchannel info $f] 15} msg]} {
406
        lappend l $msg
407
    } else {
408
        lappend l "very broken: $f found after being closed"
409
    }
410
    string compare [string tolower $l] \
411
        [list 0 [format "can not find channel named \"%s\"" $f]]
412
} 0
413
 
414
# Test management of attributes associated with a channel, such as
415
# its default translation, its name and type, etc. The functions
416
# tested in this group are Tcl_GetChannelName,
417
# Tcl_GetChannelType and Tcl_GetChannelFile. Tcl_GetChannelInstanceData
418
# not tested because files do not use the instance data.
419
 
420
test io-3.1 {Tcl_GetChannelName} {
421
    removeFile test1
422
    set f [open test1 w]
423
    set n [testchannel name $f]
424
    close $f
425
    string compare $n $f
426
} 0
427
test io-3.2 {Tcl_GetChannelType} {
428
    removeFile test1
429
    set f [open test1 w]
430
    set t [testchannel type $f]
431
    close $f
432
    string compare $t file
433
} 0
434
test io-3.3 {Tcl_GetChannelFile, input} {
435
    set f [open test1 w]
436
    fconfigure $f -translation lf -eofchar {}
437
    puts $f "1234567890\n098765432"
438
    close $f
439
    set f [open test1 r]
440
    gets $f
441
    set l ""
442
    lappend l [testchannel inputbuffered $f]
443
    lappend l [tell $f]
444
    close $f
445
    set l
446
} {10 11}
447
test io-3.4 {Tcl_GetChannelFile, output} {
448
    removeFile test1
449
    set f [open test1 w]
450
    fconfigure $f -translation lf
451
    puts $f hello
452
    set l ""
453
    lappend l [testchannel outputbuffered $f]
454
    lappend l [tell $f]
455
    flush $f
456
    lappend l [testchannel outputbuffered $f]
457
    lappend l [tell $f]
458
    close $f
459
    removeFile test1
460
    set l
461
} {6 6 0 6}
462
 
463
# Test flushing. The functions tested here are FlushChannel.
464
 
465
test io-4.1 {FlushChannel, no output buffered} {
466
    removeFile test1
467
    set f [open test1 w]
468
    flush $f
469
    set s [file size test1]
470
    close $f
471
    set s
472
} 0
473
test io-4.2 {FlushChannel, some output buffered} {
474
    removeFile test1
475
    set f [open test1 w]
476
    fconfigure $f -translation lf -eofchar {}
477
    set l ""
478
    puts $f hello
479
    lappend l [file size test1]
480
    flush $f
481
    lappend l [file size test1]
482
    close $f
483
    lappend l [file size test1]
484
    set l
485
} {0 6 6}
486
test io-4.3 {FlushChannel, implicit flush on close} {
487
    removeFile test1
488
    set f [open test1 w]
489
    fconfigure $f -translation lf -eofchar {}
490
    set l ""
491
    puts $f hello
492
    lappend l [file size test1]
493
    close $f
494
    lappend l [file size test1]
495
    set l
496
} {0 6}
497
test io-4.4 {FlushChannel, implicit flush when buffer fills} {
498
    removeFile test1
499
    set f [open test1 w]
500
    fconfigure $f -translation lf -eofchar {}
501
    fconfigure $f -buffersize 60
502
    set l ""
503
    lappend l [file size test1]
504
    for {set i 0} {$i < 12} {incr i} {
505
        puts $f hello
506
    }
507
    lappend l [file size test1]
508
    flush $f
509
    lappend l [file size test1]
510
    close $f
511
    set l
512
} {0 60 72}
513
test io-4.5 {FlushChannel, implicit flush when buffer fills and on close} {unixOrPc} {
514
    removeFile test1
515
    set f [open test1 w]
516
    fconfigure $f -translation lf -buffersize 60 -eofchar {}
517
    set l ""
518
    lappend l [file size test1]
519
    for {set i 0} {$i < 12} {incr i} {
520
        puts $f hello
521
    }
522
    lappend l [file size test1]
523
    close $f
524
    lappend l [file size test1]
525
    set l
526
} {0 60 72}
527
test io-4.6 {FlushChannel, async flushing, async close} {stdio && asyncPipeClose} {
528
    removeFile pipe
529
    removeFile output
530
    set f [open pipe w]
531
    puts $f {
532
        set f [open output w]
533
        fconfigure $f -translation lf -buffering none -eofchar {}
534
        while {![eof stdin]} {
535
            after 20
536
            puts -nonewline $f [read stdin 1024]
537
        }
538
        close $f
539
    }
540
    close $f
541
    set x 01234567890123456789012345678901
542
    for {set i 0} {$i < 11} {incr i} {
543
        set x "$x$x"
544
    }
545
    set f [open output w]
546
    close $f
547
    set f [open "|[list $tcltest pipe]" w]
548
    fconfigure $f -blocking off
549
    puts -nonewline $f $x
550
    close $f
551
    set counter 0
552
    while {([file size output] < 65536) && ($counter < 1000)} {
553
        incr counter
554
        after 20
555
        update
556
    }
557
    if {$counter == 1000} {
558
        set result probably_broken
559
    } else {
560
        set result ok
561
    }
562
} ok
563
 
564
# Tests closing a channel. The functions tested are CloseChannel and Tcl_Close.
565
 
566
test io-5.1 {CloseChannel called when all references are dropped} {
567
    removeFile test1
568
    set f [open test1 w]
569
    interp create x
570
    interp share "" $f x
571
    set l ""
572
    lappend l [testchannel refcount $f]
573
    x eval close $f
574
    interp delete x
575
    lappend l [testchannel refcount $f]
576
    close $f
577
    set l
578
} {2 1}
579
test io-5.2 {CloseChannel called when all references are dropped} {
580
    removeFile test1
581
    set f [open test1 w]
582
    interp create x
583
    interp share "" $f x
584
    puts -nonewline $f abc
585
    close $f
586
    x eval puts $f def
587
    x eval close $f
588
    interp delete x
589
    set f [open test1 r]
590
    set l [gets $f]
591
    close $f
592
    set l
593
} abcdef
594
test io-5.3 {CloseChannel, not called before output queue is empty} {unixOrPc asyncPipeClose nonPortable tempNotPc} {
595
    removeFile pipe
596
    removeFile output
597
    set f [open pipe w]
598
    puts $f {
599
 
600
        # Need to not have eof char appended on close, because the other
601
        # side of the pipe already closed, so that writing would cause an
602
        # error "invalid file".
603
 
604
        fconfigure stdout -eofchar {}
605
        fconfigure stderr -eofchar {}
606
 
607
        set f [open output w]
608
        fconfigure $f -translation lf -buffering none
609
        for {set x 0} {$x < 20} {incr x} {
610
            after 20
611
            puts -nonewline $f [read stdin 1024]
612
        }
613
        close $f
614
    }
615
    close $f
616
    set x 01234567890123456789012345678901
617
    for {set i 0} {$i < 11} {incr i} {
618
        set x "$x$x"
619
    }
620
    set f [open output w]
621
    close $f
622
    set f [open "|[list $tcltest pipe]" r+]
623
    fconfigure $f -blocking off -eofchar {}
624
 
625
    # Under windows, the first 24576 bytes of $x are copied to $f, and
626
    # then the writing fails.
627
 
628
    puts -nonewline $f $x
629
    close $f
630
    set counter 0
631
    while {([file size output] < 20480) && ($counter < 1000)} {
632
        incr counter
633
        after 20
634
        update
635
    }
636
    if {$counter == 1000} {
637
        set result probably_broken
638
    } else {
639
        set result ok
640
    }
641
} ok
642
test io-5.4 {Tcl_Close} {
643
    removeFile test1
644
    set l ""
645
    lappend l [lsort [testchannel open]]
646
    set f [open test1 w]
647
    lappend l [lsort [testchannel open]]
648
    close $f
649
    lappend l [lsort [testchannel open]]
650
    set x [list $consoleFileNames \
651
                [lsort [eval list $consoleFileNames $f]] \
652
                $consoleFileNames]
653
    string compare $l $x
654
} 0
655
test io-5.5 {Tcl_Close vs standard handles} {unixOnly} {
656
    removeFile script
657
    set f [open script w]
658
    puts $f {
659
        close stdin
660
        puts [testchannel open]
661
    }
662
    close $f
663
    set f [open "|[list $tcltest script]" r]
664
    set l [gets $f]
665
    close $f
666
    set l
667
} {file1 file2}
668
 
669
# Test output on channels. The functions tested are Tcl_Write
670
# and Tcl_Flush.
671
 
672
test io-6.1 {Tcl_Write, channel not writable} {
673
    list [catch {puts stdin hello} msg] $msg
674
} {1 {channel "stdin" wasn't opened for writing}}
675
test io-6.2 {Tcl_Write, empty string} {
676
    removeFile test1
677
    set f [open test1 w]
678
    fconfigure $f -eofchar {}
679
    puts -nonewline $f ""
680
    close $f
681
    file size test1
682
} 0
683
test io-6.3 {Tcl_Write, nonempty string} {
684
    removeFile test1
685
    set f [open test1 w]
686
    fconfigure $f -eofchar {}
687
    puts -nonewline $f hello
688
    close $f
689
    file size test1
690
} 5
691
test io-6.4 {Tcl_Write, buffering in full buffering mode} {
692
    removeFile test1
693
    set f [open test1 w]
694
    fconfigure $f -translation lf -buffering full -eofchar {}
695
    puts $f hello
696
    set l ""
697
    lappend l [testchannel outputbuffered $f]
698
    lappend l [file size test1]
699
    flush $f
700
    lappend l [testchannel outputbuffered $f]
701
    lappend l [file size test1]
702
    close $f
703
    set l
704
} {6 0 0 6}
705
test io-6.5 {Tcl_Write, buffering in line buffering mode} {
706
    removeFile test1
707
    set f [open test1 w]
708
    fconfigure $f -translation lf -buffering line -eofchar {}
709
    puts -nonewline $f hello
710
    set l ""
711
    lappend l [testchannel outputbuffered $f]
712
    lappend l [file size test1]
713
    puts $f hello
714
    lappend l [testchannel outputbuffered $f]
715
    lappend l [file size test1]
716
    close $f
717
    set l
718
} {5 0 0 11}
719
test io-6.6 {Tcl_Write, buffering in no buffering mode} {
720
    removeFile test1
721
    set f [open test1 w]
722
    fconfigure $f -translation lf -buffering none -eofchar {}
723
    puts -nonewline $f hello
724
    set l ""
725
    lappend l [testchannel outputbuffered $f]
726
    lappend l [file size test1]
727
    puts $f hello
728
    lappend l [testchannel outputbuffered $f]
729
    lappend l [file size test1]
730
    close $f
731
    set l
732
} {0 5 0 11}
733
test io-6.7 {Tcl_Flush, full buffering} {
734
    removeFile test1
735
    set f [open test1 w]
736
    fconfigure $f -translation lf -buffering full -eofchar {}
737
    puts -nonewline $f hello
738
    set l ""
739
    lappend l [testchannel outputbuffered $f]
740
    lappend l [file size test1]
741
    puts $f hello
742
    lappend l [testchannel outputbuffered $f]
743
    lappend l [file size test1]
744
    flush $f
745
    lappend l [testchannel outputbuffered $f]
746
    lappend l [file size test1]
747
    close $f
748
    set l
749
} {5 0 11 0 0 11}
750
test io-6.8 {Tcl_Flush, full buffering} {
751
    removeFile test1
752
    set f [open test1 w]
753
    fconfigure $f -translation lf -buffering line
754
    puts -nonewline $f hello
755
    set l ""
756
    lappend l [testchannel outputbuffered $f]
757
    lappend l [file size test1]
758
    flush $f
759
    lappend l [testchannel outputbuffered $f]
760
    lappend l [file size test1]
761
    puts $f hello
762
    lappend l [testchannel outputbuffered $f]
763
    lappend l [file size test1]
764
    flush $f
765
    lappend l [testchannel outputbuffered $f]
766
    lappend l [file size test1]
767
    close $f
768
    set l
769
} {5 0 0 5 0 11 0 11}
770
test io-6.9 {Tcl_Flush, channel not writable} {
771
    list [catch {flush stdin} msg] $msg
772
} {1 {channel "stdin" wasn't opened for writing}}
773
test io-6.10 {Tcl_Write, looping and buffering} {
774
    removeFile test1
775
    set f1 [open test1 w]
776
    fconfigure $f1 -translation lf -eofchar {}
777
    set f2 [open longfile r]
778
    for {set x 0} {$x < 10} {incr x} {
779
        puts $f1 [gets $f2]
780
    }
781
    close $f2
782
    close $f1
783
    file size test1
784
} 387
785
test io-6.11 {Tcl_Write, no newline, implicit flush} {
786
    removeFile test1
787
    set f1 [open test1 w]
788
    fconfigure $f1 -eofchar {}
789
    set f2 [open longfile r]
790
    for {set x 0} {$x < 10} {incr x} {
791
        puts -nonewline $f1 [gets $f2]
792
    }
793
    close $f1
794
    close $f2
795
    file size test1
796
} 377
797
test io-6.12 {Tcl_Write on a pipe} {stdio} {
798
    removeFile test1
799
    removeFile pipe
800
    set f1 [open pipe w]
801
    puts $f1 {
802
        set f1 [open longfile r]
803
        for {set x 0} {$x < 10} {incr x} {
804
            puts [gets $f1]
805
        }
806
    }
807
    close $f1
808
    set f1 [open "|[list $tcltest pipe]" r]
809
    set f2 [open longfile r]
810
    set y ok
811
    for {set x 0} {$x < 10} {incr x} {
812
        set l1 [gets $f1]
813
        set l2 [gets $f2]
814
        if {"$l1" != "$l2"} {
815
            set y broken
816
        }
817
    }
818
    close $f1
819
    close $f2
820
    set y
821
} ok
822
test io-6.13 {Tcl_Write to a pipe, line buffered} {stdio} {
823
    removeFile test1
824
    removeFile pipe
825
    set f1 [open pipe w]
826
    puts $f1 {
827
        puts [gets stdin]
828
        puts [gets stdin]
829
    }
830
    close $f1
831
    set y ok
832
    set f1 [open "|[list $tcltest pipe]" r+]
833
    fconfigure $f1 -buffering line
834
    set f2 [open longfile r]
835
    set line [gets $f2]
836
    puts $f1 $line
837
    set backline [gets $f1]
838
    if {"$line" != "$backline"} {
839
        set y broken
840
    }
841
    set line [gets $f2]
842
    puts $f1 $line
843
    set backline [gets $f1]
844
    if {"$line" != "$backline"} {
845
        set y broken
846
    }
847
    close $f1
848
    close $f2
849
    set y
850
} ok
851
test io-6.14 {Tcl_Write, buffering and implicit flush at close} {
852
    removeFile test3
853
    set f [open test3 w]
854
    puts -nonewline $f "Text1"
855
    puts -nonewline $f " Text 2"
856
    puts $f " Text 3"
857
    close $f
858
    set f [open test3 r]
859
    set x [gets $f]
860
    close $f
861
    set x
862
} {Text1 Text 2 Text 3}
863
test io-6.15 {Tcl_Flush, channel not open for writing} {
864
    removeFile test1
865
    set fd [open test1 w]
866
    close $fd
867
    set fd [open test1 r]
868
    set x [list [catch {flush $fd} msg] $msg]
869
    close $fd
870
    string compare $x \
871
        [list 1 "channel \"$fd\" wasn't opened for writing"]
872
} 0
873
test io-6.16 {Tcl_Flush on pipe opened only for reading} {stdio} {
874
    set fd [open "|[list $tcltest cat longfile]" r]
875
    set x [list [catch {flush $fd} msg] $msg]
876
    catch {close $fd}
877
    string compare $x \
878
        [list 1 "channel \"$fd\" wasn't opened for writing"]
879
} 0
880
test io-6.17 {Tcl_Write buffers, then Tcl_Flush flushes} {
881
    removeFile test1
882
    set f1 [open test1 w]
883
    fconfigure $f1 -translation lf
884
    puts $f1 hello
885
    puts $f1 hello
886
    puts $f1 hello
887
    flush $f1
888
    set x [file size test1]
889
    close $f1
890
    set x
891
} 18
892
test io-6.18 {Tcl_Write and Tcl_Flush intermixed} {
893
    removeFile test1
894
    set x ""
895
    set f1 [open test1 w]
896
    fconfigure $f1 -translation lf
897
    puts $f1 hello
898
    puts $f1 hello
899
    puts $f1 hello
900
    flush $f1
901
    lappend x [file size test1]
902
    puts $f1 hello
903
    flush $f1
904
    lappend x [file size test1]
905
    puts $f1 hello
906
    flush $f1
907
    lappend x [file size test1]
908
    close $f1
909
    set x
910
} {18 24 30}
911
test io-6.19 {Explicit and implicit flushes} {
912
    removeFile test1
913
    set f1 [open test1 w]
914
    fconfigure $f1 -translation lf -eofchar {}
915
    set x ""
916
    puts $f1 hello
917
    puts $f1 hello
918
    puts $f1 hello
919
    flush $f1
920
    lappend x [file size test1]
921
    puts $f1 hello
922
    flush $f1
923
    lappend x [file size test1]
924
    puts $f1 hello
925
    close $f1
926
    lappend x [file size test1]
927
    set x
928
} {18 24 30}
929
test io-6.20 {Implicit flush when buffer is full} {
930
    removeFile test1
931
    set f1 [open test1 w]
932
    fconfigure $f1 -translation lf -eofchar {}
933
    set line "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
934
    for {set x 0} {$x < 100} {incr x} {
935
      puts $f1 $line
936
    }
937
    set z ""
938
    lappend z [file size test1]
939
    for {set x 0} {$x < 100} {incr x} {
940
        puts $f1 $line
941
    }
942
    lappend z [file size test1]
943
    close $f1
944
    lappend z [file size test1]
945
    set z
946
} {4096 12288 12600}
947
test io-6.21 {Tcl_Flush to pipe} {stdio} {
948
    removeFile pipe
949
    set f1 [open pipe w]
950
    puts $f1 {set x [read stdin 6]}
951
    puts $f1 {set cnt [string length $x]}
952
    puts $f1 {puts "read $cnt characters"}
953
    close $f1
954
    set f1 [open "|[list $tcltest pipe]" r+]
955
    puts $f1 hello
956
    flush $f1
957
    set x [gets $f1]
958
    catch {close $f1}
959
    set x
960
} "read 6 characters"
961
test io-6.22 {Tcl_Flush called at other end of pipe} {stdio} {
962
    removeFile pipe
963
    set f1 [open pipe w]
964
    puts $f1 {
965
        fconfigure stdout -buffering full
966
        puts hello
967
        puts hello
968
        flush stdout
969
        gets stdin
970
        puts bye
971
        flush stdout
972
    }
973
    close $f1
974
    set f1 [open "|[list $tcltest pipe]" r+]
975
    set x ""
976
    lappend x [gets $f1]
977
    lappend x [gets $f1]
978
    puts $f1 hello
979
    flush $f1
980
    lappend x [gets $f1]
981
    close $f1
982
    set x
983
} {hello hello bye}
984
test io-6.23 {Tcl_Flush and line buffering at end of pipe} {stdio} {
985
    removeFile pipe
986
    set f1 [open pipe w]
987
    puts $f1 {
988
        puts hello
989
        puts hello
990
        gets stdin
991
        puts bye
992
    }
993
    close $f1
994
    set f1 [open "|[list $tcltest pipe]" r+]
995
    set x ""
996
    lappend x [gets $f1]
997
    lappend x [gets $f1]
998
    puts $f1 hello
999
    flush $f1
1000
    lappend x [gets $f1]
1001
    close $f1
1002
    set x
1003
} {hello hello bye}
1004
test io-6.24 {Tcl_Write and Tcl_Flush move end of file} {
1005
    set f [open test3 w]
1006
    puts $f "Line 1"
1007
    puts $f "Line 2"
1008
    set f2 [open test3]
1009
    set x {}
1010
    lappend x [read -nonewline $f2]
1011
    close $f2
1012
    flush $f
1013
    set f2 [open test3]
1014
    lappend x [read -nonewline $f2]
1015
    close $f2
1016
    close $f
1017
    set x
1018
} {{} {Line 1
1019
Line 2}}
1020
test io-6.25 {Implicit flush with Tcl_Flush to command pipelines} {stdio} {
1021
    removeFile test3
1022
    set f [open "|[list $tcltest cat | $tcltest cat > test3]" w]
1023
    puts $f "Line 1"
1024
    puts $f "Line 2"
1025
    close $f
1026
    after 100
1027
    set f [open test3 r]
1028
    set x [read $f]
1029
    close $f
1030
    set x
1031
} {Line 1
1032
Line 2
1033
}
1034
test io-6.26 {Tcl_Flush, Tcl_Write on bidirectional pipelines} {unixOrPc && unixExecs && tempNotPc} {
1035
    set f [open "|[list cat -u]" r+]
1036
    puts $f "Line1"
1037
    flush $f
1038
    set x [gets $f]
1039
    close $f
1040
    set x
1041
} {Line1}
1042
test io-6.27 {Tcl_Flush on closed pipeline} {stdio && tempNotPc} {
1043
    removeFile pipe
1044
    set f [open pipe w]
1045
    puts $f {exit}
1046
    close $f
1047
    set f [open "|[list $tcltest pipe]" r+]
1048
    gets $f
1049
    puts $f output
1050
    after 50
1051
    #
1052
    # The flush below will get a SIGPIPE. This is an expected part of
1053
    # test and indicates that the test operates correctly. If you run
1054
    # this test under a debugger, the signal will by intercepted unless
1055
    # you disable the debugger's signal interception.
1056
    #
1057
    if {[catch {flush $f} msg]} {
1058
        set x [list 1 $msg $errorCode]
1059
        catch {close $f}
1060
    } else {
1061
        if {[catch {close $f} msg]} {
1062
            set x [list 1 $msg $errorCode]
1063
        } else {
1064
            set x {this was supposed to fail and did not}
1065
        }
1066
    }
1067
    regsub {".*":} $x {"":} x
1068
    string tolower $x
1069
} {1 {error flushing "": broken pipe} {posix epipe {broken pipe}}}
1070
test io-6.28 {Tcl_Write, lf mode} {
1071
    removeFile test1
1072
    set f [open test1 w]
1073
    fconfigure $f -translation lf -eofchar {}
1074
    puts $f hello\nthere\nand\nhere
1075
    flush $f
1076
    set s [file size test1]
1077
    close $f
1078
    set s
1079
} 21
1080
test io-6.29 {Tcl_Write, cr mode} {
1081
    removeFile test1
1082
    set f [open test1 w]
1083
    fconfigure $f -translation cr -eofchar {}
1084
    puts $f hello\nthere\nand\nhere
1085
    close $f
1086
    file size test1
1087
} 21
1088
test io-6.30 {Tcl_Write, crlf mode} {
1089
    removeFile test1
1090
    set f [open test1 w]
1091
    fconfigure $f -translation crlf -eofchar {}
1092
    puts $f hello\nthere\nand\nhere
1093
    close $f
1094
    file size test1
1095
} 25
1096
test io-6.31 {Tcl_Write, background flush} {stdio} {
1097
    removeFile pipe
1098
    removeFile output
1099
    set f [open pipe w]
1100
    puts $f {set f [open output w]}
1101
    puts $f {fconfigure $f -translation lf}
1102
    set x [list while {![eof stdin]}]
1103
    set x "$x {"
1104
    puts $f $x
1105
    puts $f {  puts -nonewline $f [read stdin 4096]}
1106
    puts $f {  flush $f}
1107
    puts $f "}"
1108
    puts $f {close $f}
1109
    close $f
1110
    set x 01234567890123456789012345678901
1111
    for {set i 0} {$i < 11} {incr i} {
1112
        set x "$x$x"
1113
    }
1114
    set f [open output w]
1115
    close $f
1116
    set f [open "|[list $tcltest pipe]" r+]
1117
    fconfigure $f -blocking off
1118
    puts -nonewline $f $x
1119
    close $f
1120
    set counter 0
1121
    while {([file size output] < 65536) && ($counter < 1000)} {
1122
        incr counter
1123
        after 5
1124
        update
1125
    }
1126
    if {$counter == 1000} {
1127
        set result probably_broken
1128
    } else {
1129
        set result ok
1130
    }
1131
} ok
1132
test io-6.32 {Tcl_Write, background flush to slow reader} {stdio && asyncPipeClose} {
1133
    removeFile pipe
1134
    removeFile output
1135
    set f [open pipe w]
1136
    puts $f {set f [open output w]}
1137
    puts $f {fconfigure $f -translation lf}
1138
    set x [list while {![eof stdin]}]
1139
    set x "$x {"
1140
    puts $f $x
1141
    puts $f {  after 20}
1142
    puts $f {  puts -nonewline $f [read stdin 1024]}
1143
    puts $f {  flush $f}
1144
    puts $f "}"
1145
    puts $f {close $f}
1146
    close $f
1147
    set x 01234567890123456789012345678901
1148
    for {set i 0} {$i < 11} {incr i} {
1149
        set x "$x$x"
1150
    }
1151
    set f [open output w]
1152
    close $f
1153
    set f [open "|[list $tcltest pipe]" r+]
1154
    fconfigure $f -blocking off
1155
    puts -nonewline $f $x
1156
    close $f
1157
    set counter 0
1158
    while {([file size output] < 65536) && ($counter < 1000)} {
1159
        incr counter
1160
        after 20
1161
        update
1162
    }
1163
    if {$counter == 1000} {
1164
        set result probably_broken
1165
    } else {
1166
        set result ok
1167
    }
1168
} ok
1169
test io-6.33 {Tcl_Flush, implicit flush on exit} {stdio} {
1170
    set f [open script w]
1171
    puts $f {
1172
        set f [open test1 w]
1173
        fconfigure $f -translation lf
1174
        puts $f hello
1175
        puts $f bye
1176
        puts $f strange
1177
    }
1178
    close $f
1179
    exec $tcltest script
1180
    set f [open test1 r]
1181
    set r [read $f]
1182
    close $f
1183
    set r
1184
} {hello
1185
bye
1186
strange
1187
}
1188
 
1189
test io-6.34 {Tcl_Close, async flush on close, using sockets} {socket tempNotMac} {
1190
    set c 0
1191
    set x running
1192
    set l abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz
1193
    proc writelots {s l} {
1194
        for {set i 0} {$i < 2000} {incr i} {
1195
            puts $s $l
1196
        }
1197
    }
1198
    proc accept {s a p} {
1199
        global x
1200
        fileevent $s readable [list readit $s]
1201
        fconfigure $s -blocking off
1202
        set x accepted
1203
    }
1204
    proc readit {s} {
1205
        global c x
1206
        set l [gets $s]
1207
 
1208
        if {[eof $s]} {
1209
            close $s
1210
            set x done
1211
        } elseif {([string length $l] > 0) || ![fblocked $s]} {
1212
            incr c
1213
        }
1214
    }
1215
    set ss [socket -server accept 2828]
1216
    set cs [socket [info hostname] 2828]
1217
    vwait x
1218
    fconfigure $cs -blocking off
1219
    writelots $cs $l
1220
    close $cs
1221
    close $ss
1222
    vwait x
1223
    set c
1224
} 2000
1225
test io-6.35 {Tcl_Close vs fileevent vs multiple interpreters} {socket} {
1226
    catch {interp delete x}
1227
    catch {interp delete y}
1228
    interp create x
1229
    interp create y
1230
    set s [socket -server accept 2828]
1231
    proc accept {s a p} {
1232
        puts $s hello
1233
        close $s
1234
    }
1235
    set c [socket [info hostname] 2828]
1236
    interp share {} $c x
1237
    interp share {} $c y
1238
    close $c
1239
    x eval {
1240
        proc readit {s} {
1241
            gets $s
1242
            if {[eof $s]} {
1243
                close $s
1244
            }
1245
        }
1246
    }
1247
    y eval {
1248
        proc readit {s} {
1249
            gets $s
1250
            if {[eof $s]} {
1251
                close $s
1252
            }
1253
        }
1254
    }
1255
    x eval "fileevent $c readable \{readit $c\}"
1256
    y eval "fileevent $c readable \{readit $c\}"
1257
    y eval [list close $c]
1258
    update
1259
    close $s
1260
    interp delete x
1261
    interp delete y
1262
} ""
1263
 
1264
# Test end of line translations. Procedures tested are Tcl_Write, Tcl_Read.
1265
 
1266
test io-7.1 {Tcl_Write lf, Tcl_Read lf} {
1267
    removeFile test1
1268
    set f [open test1 w]
1269
    fconfigure $f -translation lf
1270
    puts $f hello\nthere\nand\nhere
1271
    close $f
1272
    set f [open test1 r]
1273
    fconfigure $f -translation lf
1274
    set x [read $f]
1275
    close $f
1276
    set x
1277
} "hello\nthere\nand\nhere\n"
1278
test io-7.2 {Tcl_Write lf, Tcl_Read cr} {
1279
    removeFile test1
1280
    set f [open test1 w]
1281
    fconfigure $f -translation lf
1282
    puts $f hello\nthere\nand\nhere
1283
    close $f
1284
    set f [open test1 r]
1285
    fconfigure $f -translation cr
1286
    set x [read $f]
1287
    close $f
1288
    set x
1289
} "hello\nthere\nand\nhere\n"
1290
test io-7.3 {Tcl_Write lf, Tcl_Read crlf} {
1291
    removeFile test1
1292
    set f [open test1 w]
1293
    fconfigure $f -translation lf
1294
    puts $f hello\nthere\nand\nhere
1295
    close $f
1296
    set f [open test1 r]
1297
    fconfigure $f -translation crlf
1298
    set x [read $f]
1299
    close $f
1300
    set x
1301
} "hello\nthere\nand\nhere\n"
1302
test io-7.4 {Tcl_Write cr, Tcl_Read cr} {
1303
    removeFile test1
1304
    set f [open test1 w]
1305
    fconfigure $f -translation cr
1306
    puts $f hello\nthere\nand\nhere
1307
    close $f
1308
    set f [open test1 r]
1309
    fconfigure $f -translation cr
1310
    set x [read $f]
1311
    close $f
1312
    set x
1313
} "hello\nthere\nand\nhere\n"
1314
test io-7.5 {Tcl_Write cr, Tcl_Read lf} {
1315
    removeFile test1
1316
    set f [open test1 w]
1317
    fconfigure $f -translation cr
1318
    puts $f hello\nthere\nand\nhere
1319
    close $f
1320
    set f [open test1 r]
1321
    fconfigure $f -translation lf
1322
    set x [read $f]
1323
    close $f
1324
    set x
1325
} "hello\rthere\rand\rhere\r"
1326
test io-7.6 {Tcl_Write cr, Tcl_Read crlf} {
1327
    removeFile test1
1328
    set f [open test1 w]
1329
    fconfigure $f -translation cr
1330
    puts $f hello\nthere\nand\nhere
1331
    close $f
1332
    set f [open test1 r]
1333
    fconfigure $f -translation crlf
1334
    set x [read $f]
1335
    close $f
1336
    set x
1337
} "hello\rthere\rand\rhere\r"
1338
test io-7.7 {Tcl_Write crlf, Tcl_Read crlf} {
1339
    removeFile test1
1340
    set f [open test1 w]
1341
    fconfigure $f -translation crlf
1342
    puts $f hello\nthere\nand\nhere
1343
    close $f
1344
    set f [open test1 r]
1345
    fconfigure $f -translation crlf
1346
    set x [read $f]
1347
    close $f
1348
    set x
1349
} "hello\nthere\nand\nhere\n"
1350
test io-7.8 {Tcl_Write crlf, Tcl_Read lf} {
1351
    removeFile test1
1352
    set f [open test1 w]
1353
    fconfigure $f -translation crlf
1354
    puts $f hello\nthere\nand\nhere
1355
    close $f
1356
    set f [open test1 r]
1357
    fconfigure $f -translation lf
1358
    set x [read $f]
1359
    close $f
1360
    set x
1361
} "hello\r\nthere\r\nand\r\nhere\r\n"
1362
test io-7.9 {Tcl_Write crlf, Tcl_Read cr} {
1363
    removeFile test1
1364
    set f [open test1 w]
1365
    fconfigure $f -translation crlf
1366
    puts $f hello\nthere\nand\nhere
1367
    close $f
1368
    set f [open test1 r]
1369
    fconfigure $f -translation cr
1370
    set x [read $f]
1371
    close $f
1372
    set x
1373
} "hello\n\nthere\n\nand\n\nhere\n\n"
1374
test io-7.10 {Tcl_Write lf, Tcl_Read auto} {
1375
    removeFile test1
1376
    set f [open test1 w]
1377
    fconfigure $f -translation lf
1378
    puts $f hello\nthere\nand\nhere
1379
    close $f
1380
    set f [open test1 r]
1381
    set c [read $f]
1382
    set x [fconfigure $f -translation]
1383
    close $f
1384
    list $c $x
1385
} {{hello
1386
there
1387
and
1388
here
1389
} auto}
1390
test io-7.11 {Tcl_Write cr, Tcl_Read auto} {
1391
    removeFile test1
1392
    set f [open test1 w]
1393
    fconfigure $f -translation cr
1394
    puts $f hello\nthere\nand\nhere
1395
    close $f
1396
    set f [open test1 r]
1397
    set c [read $f]
1398
    set x [fconfigure $f -translation]
1399
    close $f
1400
    list $c $x
1401
} {{hello
1402
there
1403
and
1404
here
1405
} auto}
1406
test io-7.12 {Tcl_Write crlf, Tcl_Read auto} {
1407
    removeFile test1
1408
    set f [open test1 w]
1409
    fconfigure $f -translation crlf
1410
    puts $f hello\nthere\nand\nhere
1411
    close $f
1412
    set f [open test1 r]
1413
    set c [read $f]
1414
    set x [fconfigure $f -translation]
1415
    close $f
1416
    list $c $x
1417
} {{hello
1418
there
1419
and
1420
here
1421
} auto}
1422
 
1423
test io-7.13 {Tcl_Write crlf on block boundary, Tcl_Read auto} {
1424
    removeFile test1
1425
    set f [open test1 w]
1426
    fconfigure $f -translation crlf
1427
    set line "123456789ABCDE"   ;# 14 char plus crlf
1428
    puts -nonewline $f x        ;# shift crlf across block boundary
1429
    for {set i 0} {$i < 700} {incr i} {
1430
        puts $f $line
1431
    }
1432
    close $f
1433
    set f [open test1 r]
1434
    fconfigure $f -translation auto
1435
    set c [read $f]
1436
    close $f
1437
    string length $c
1438
} [expr 700*15+1]
1439
 
1440
test io-7.14 {Tcl_Write crlf on block boundary, Tcl_Read crlf} {
1441
    removeFile test1
1442
    set f [open test1 w]
1443
    fconfigure $f -translation crlf
1444
    set line "123456789ABCDE"   ;# 14 char plus crlf
1445
    puts -nonewline $f x        ;# shift crlf across block boundary
1446
    for {set i 0} {$i < 700} {incr i} {
1447
        puts $f $line
1448
    }
1449
    close $f
1450
    set f [open test1 r]
1451
    fconfigure $f -translation crlf
1452
    set c [read $f]
1453
    close $f
1454
    string length $c
1455
} [expr 700*15+1]
1456
 
1457
test io-7.15 {Tcl_Write mixed, Tcl_Read auto} {
1458
    removeFile test1
1459
    set f [open test1 w]
1460
    fconfigure $f -translation lf
1461
    puts $f hello\nthere\nand\rhere
1462
    close $f
1463
    set f [open test1 r]
1464
    fconfigure $f -translation auto
1465
    set c [read $f]
1466
    close $f
1467
    set c
1468
} {hello
1469
there
1470
and
1471
here
1472
}
1473
test io-7.16 {Tcl_Write ^Z at end, Tcl_Read auto} {
1474
    removeFile test1
1475
    set f [open test1 w]
1476
    fconfigure $f -translation lf
1477
    puts -nonewline $f hello\nthere\nand\rhere\n\x1a
1478
    close $f
1479
    set f [open test1 r]
1480
    fconfigure $f -eofchar \x1a -translation auto
1481
    set c [read $f]
1482
    close $f
1483
    set c
1484
} {hello
1485
there
1486
and
1487
here
1488
}
1489
test io-7.17 {Tcl_Write, implicit ^Z at end, Tcl_Read auto} {pcOnly} {
1490
    removeFile test1
1491
    set f [open test1 w]
1492
    fconfigure $f -eofchar \x1a -translation lf
1493
    puts $f hello\nthere\nand\rhere
1494
    close $f
1495
    set f [open test1 r]
1496
    fconfigure $f -eofchar \x1a -translation auto
1497
    set c [read $f]
1498
    close $f
1499
    set c
1500
} {hello
1501
there
1502
and
1503
here
1504
}
1505
test io-7.18 {Tcl_Write, ^Z in middle, Tcl_Read auto} {
1506
    removeFile test1
1507
    set f [open test1 w]
1508
    fconfigure $f -translation lf
1509
    set s [format "abc\ndef\n%cghi\nqrs" 26]
1510
    puts $f $s
1511
    close $f
1512
    set f [open test1 r]
1513
    fconfigure $f -eofchar \x1a -translation auto
1514
    set l ""
1515
    lappend l [gets $f]
1516
    lappend l [gets $f]
1517
    lappend l [eof $f]
1518
    lappend l [gets $f]
1519
    lappend l [eof $f]
1520
    lappend l [gets $f]
1521
    lappend l [eof $f]
1522
    close $f
1523
    set l
1524
} {abc def 0 {} 1 {} 1}
1525
test io-7.19 {Tcl_Write, ^Z no newline in middle, Tcl_Read auto} {
1526
    removeFile test1
1527
    set f [open test1 w]
1528
    fconfigure $f -translation lf
1529
    set s [format "abc\ndef\n%cghi\nqrs" 26]
1530
    puts $f $s
1531
    close $f
1532
    set f [open test1 r]
1533
    fconfigure $f -eofchar \x1a -translation auto
1534
    set l ""
1535
    lappend l [gets $f]
1536
    lappend l [gets $f]
1537
    lappend l [eof $f]
1538
    lappend l [gets $f]
1539
    lappend l [eof $f]
1540
    lappend l [gets $f]
1541
    lappend l [eof $f]
1542
    close $f
1543
    set l
1544
} {abc def 0 {} 1 {} 1}
1545
test io-7.20 {Tcl_Write, ^Z in middle ignored, Tcl_Read lf} {
1546
    removeFile test1
1547
    set f [open test1 w]
1548
    fconfigure $f -translation lf -eofchar {}
1549
    set s [format "abc\ndef\n%cghi\nqrs" 26]
1550
    puts $f $s
1551
    close $f
1552
    set f [open test1 r]
1553
    fconfigure $f -translation lf -eofchar {}
1554
    set l ""
1555
    lappend l [gets $f]
1556
    lappend l [gets $f]
1557
    lappend l [eof $f]
1558
    lappend l [gets $f]
1559
    lappend l [eof $f]
1560
    lappend l [gets $f]
1561
    lappend l [eof $f]
1562
    lappend l [gets $f]
1563
    lappend l [eof $f]
1564
    close $f
1565
    set l
1566
} "abc def 0 \x1aghi 0 qrs 0 {} 1"
1567
test io-7.21 {Tcl_Write, ^Z in middle ignored, Tcl_Read cr} {
1568
    removeFile test1
1569
    set f [open test1 w]
1570
    fconfigure $f -translation lf -eofchar {}
1571
    set s [format "abc\ndef\n%cghi\nqrs" 26]
1572
    puts $f $s
1573
    close $f
1574
    set f [open test1 r]
1575
    fconfigure $f -translation cr -eofchar {}
1576
    set l ""
1577
    set x [gets $f]
1578
    lappend l [string compare $x "abc\ndef\n\x1aghi\nqrs"]
1579
    lappend l [eof $f]
1580
    lappend l [gets $f]
1581
    lappend l [eof $f]
1582
    close $f
1583
    set l
1584
} {0 1 {} 1}
1585
test io-7.22 {Tcl_Write, ^Z in middle ignored, Tcl_Read crlf} {
1586
    removeFile test1
1587
    set f [open test1 w]
1588
    fconfigure $f -translation lf -eofchar {}
1589
    set s [format "abc\ndef\n%cghi\nqrs" 26]
1590
    puts $f $s
1591
    close $f
1592
    set f [open test1 r]
1593
    fconfigure $f -translation crlf -eofchar {}
1594
    set l ""
1595
    set x [gets $f]
1596
    lappend l [string compare $x "abc\ndef\n\x1aghi\nqrs"]
1597
    lappend l [eof $f]
1598
    lappend l [gets $f]
1599
    lappend l [eof $f]
1600
    close $f
1601
    set l
1602
} {0 1 {} 1}
1603
test io-7.23 {Tcl_Write lf, ^Z in middle, Tcl_Read auto} {
1604
    removeFile test1
1605
    set f [open test1 w]
1606
    fconfigure $f -translation lf
1607
    set c [format abc\ndef\n%cqrs\ntuv 26]
1608
    puts $f $c
1609
    close $f
1610
    set f [open test1 r]
1611
    fconfigure $f -translation auto -eofchar \x1a
1612
    set c [string length [read $f]]
1613
    set e [eof $f]
1614
    close $f
1615
    list $c $e
1616
} {8 1}
1617
test io-7.24 {Tcl_Write lf, ^Z in middle, Tcl_Read lf} {
1618
    removeFile test1
1619
    set f [open test1 w]
1620
    fconfigure $f -translation lf
1621
    set c [format abc\ndef\n%cqrs\ntuv 26]
1622
    puts $f $c
1623
    close $f
1624
    set f [open test1 r]
1625
    fconfigure $f -translation lf -eofchar \x1a
1626
    set c [string length [read $f]]
1627
    set e [eof $f]
1628
    close $f
1629
    list $c $e
1630
} {8 1}
1631
test io-7.25 {Tcl_Write cr, ^Z in middle, Tcl_Read auto} {
1632
    removeFile test1
1633
    set f [open test1 w]
1634
    fconfigure $f -translation cr
1635
    set c [format abc\ndef\n%cqrs\ntuv 26]
1636
    puts $f $c
1637
    close $f
1638
    set f [open test1 r]
1639
    fconfigure $f -translation auto -eofchar \x1a
1640
    set c [string length [read $f]]
1641
    set e [eof $f]
1642
    close $f
1643
    list $c $e
1644
} {8 1}
1645
test io-7.26 {Tcl_Write cr, ^Z in middle, Tcl_Read cr} {
1646
    removeFile test1
1647
    set f [open test1 w]
1648
    fconfigure $f -translation cr
1649
    set c [format abc\ndef\n%cqrs\ntuv 26]
1650
    puts $f $c
1651
    close $f
1652
    set f [open test1 r]
1653
    fconfigure $f -translation cr -eofchar \x1a
1654
    set c [string length [read $f]]
1655
    set e [eof $f]
1656
    close $f
1657
    list $c $e
1658
} {8 1}
1659
test io-7.27 {Tcl_Write crlf, ^Z in middle, Tcl_Read auto} {
1660
    removeFile test1
1661
    set f [open test1 w]
1662
    fconfigure $f -translation crlf
1663
    set c [format abc\ndef\n%cqrs\ntuv 26]
1664
    puts $f $c
1665
    close $f
1666
    set f [open test1 r]
1667
    fconfigure $f -translation auto -eofchar \x1a
1668
    set c [string length [read $f]]
1669
    set e [eof $f]
1670
    close $f
1671
    list $c $e
1672
} {8 1}
1673
test io-7.28 {Tcl_Write crlf, ^Z in middle, Tcl_Read crlf} {
1674
    removeFile test1
1675
    set f [open test1 w]
1676
    fconfigure $f -translation crlf
1677
    set c [format abc\ndef\n%cqrs\ntuv 26]
1678
    puts $f $c
1679
    close $f
1680
    set f [open test1 r]
1681
    fconfigure $f -translation crlf -eofchar \x1a
1682
    set c [string length [read $f]]
1683
    set e [eof $f]
1684
    close $f
1685
    list $c $e
1686
} {8 1}
1687
 
1688
# Test end of line translations. Functions tested are Tcl_Write and Tcl_Gets.
1689
 
1690
test io-8.1 {Tcl_Write lf, Tcl_Gets auto} {
1691
    removeFile test1
1692
    set f [open test1 w]
1693
    fconfigure $f -translation lf
1694
    puts $f hello\nthere\nand\nhere
1695
    close $f
1696
    set f [open test1 r]
1697
    set l ""
1698
    lappend l [gets $f]
1699
    lappend l [tell $f]
1700
    lappend l [fconfigure $f -translation]
1701
    lappend l [gets $f]
1702
    lappend l [tell $f]
1703
    lappend l [fconfigure $f -translation]
1704
    close $f
1705
    set l
1706
} {hello 6 auto there 12 auto}
1707
test io-8.2 {Tcl_Write cr, Tcl_Gets auto} {
1708
    removeFile test1
1709
    set f [open test1 w]
1710
    fconfigure $f -translation cr
1711
    puts $f hello\nthere\nand\nhere
1712
    close $f
1713
    set f [open test1 r]
1714
    set l ""
1715
    lappend l [gets $f]
1716
    lappend l [tell $f]
1717
    lappend l [fconfigure $f -translation]
1718
    lappend l [gets $f]
1719
    lappend l [tell $f]
1720
    lappend l [fconfigure $f -translation]
1721
    close $f
1722
    set l
1723
} {hello 6 auto there 12 auto}
1724
test io-8.3 {Tcl_Write crlf, Tcl_Gets auto} {
1725
    removeFile test1
1726
    set f [open test1 w]
1727
    fconfigure $f -translation crlf
1728
    puts $f hello\nthere\nand\nhere
1729
    close $f
1730
    set f [open test1 r]
1731
    set l ""
1732
    lappend l [gets $f]
1733
    lappend l [tell $f]
1734
    lappend l [fconfigure $f -translation]
1735
    lappend l [gets $f]
1736
    lappend l [tell $f]
1737
    lappend l [fconfigure $f -translation]
1738
    close $f
1739
    set l
1740
} {hello 7 auto there 14 auto}
1741
test io-8.4 {Tcl_Write lf, Tcl_Gets lf} {
1742
    removeFile test1
1743
    set f [open test1 w]
1744
    fconfigure $f -translation lf
1745
    puts $f hello\nthere\nand\nhere
1746
    close $f
1747
    set f [open test1 r]
1748
    fconfigure $f -translation lf
1749
    set l ""
1750
    lappend l [gets $f]
1751
    lappend l [tell $f]
1752
    lappend l [fconfigure $f -translation]
1753
    lappend l [gets $f]
1754
    lappend l [tell $f]
1755
    lappend l [fconfigure $f -translation]
1756
    close $f
1757
    set l
1758
} {hello 6 lf there 12 lf}
1759
test io-8.5 {Tcl_Write lf, Tcl_Gets cr} {
1760
    removeFile test1
1761
    set f [open test1 w]
1762
    fconfigure $f -translation lf
1763
    puts $f hello\nthere\nand\nhere
1764
    close $f
1765
    set f [open test1 r]
1766
    fconfigure $f -translation cr
1767
    set l ""
1768
    lappend l [string length [gets $f]]
1769
    lappend l [tell $f]
1770
    lappend l [fconfigure $f -translation]
1771
    lappend l [eof $f]
1772
    lappend l [gets $f]
1773
    lappend l [tell $f]
1774
    lappend l [fconfigure $f -translation]
1775
    lappend l [eof $f]
1776
    close $f
1777
    set l
1778
} {20 21 cr 1 {} 21 cr 1}
1779
test io-8.6 {Tcl_Write lf, Tcl_Gets crlf} {
1780
    removeFile test1
1781
    set f [open test1 w]
1782
    fconfigure $f -translation lf
1783
    puts $f hello\nthere\nand\nhere
1784
    close $f
1785
    set f [open test1 r]
1786
    fconfigure $f -translation crlf
1787
    set l ""
1788
    lappend l [string length [gets $f]]
1789
    lappend l [tell $f]
1790
    lappend l [fconfigure $f -translation]
1791
    lappend l [eof $f]
1792
    lappend l [gets $f]
1793
    lappend l [tell $f]
1794
    lappend l [fconfigure $f -translation]
1795
    lappend l [eof $f]
1796
    close $f
1797
    set l
1798
} {20 21 crlf 1 {} 21 crlf 1}
1799
test io-8.7 {Tcl_Write cr, Tcl_Gets cr} {
1800
    removeFile test1
1801
    set f [open test1 w]
1802
    fconfigure $f -translation cr
1803
    puts $f hello\nthere\nand\nhere
1804
    close $f
1805
    set f [open test1 r]
1806
    fconfigure $f -translation cr
1807
    set l ""
1808
    lappend l [gets $f]
1809
    lappend l [tell $f]
1810
    lappend l [fconfigure $f -translation]
1811
    lappend l [eof $f]
1812
    lappend l [gets $f]
1813
    lappend l [tell $f]
1814
    lappend l [fconfigure $f -translation]
1815
    lappend l [eof $f]
1816
    close $f
1817
    set l
1818
} {hello 6 cr 0 there 12 cr 0}
1819
test io-8.8 {Tcl_Write cr, Tcl_Gets lf} {
1820
    removeFile test1
1821
    set f [open test1 w]
1822
    fconfigure $f -translation cr
1823
    puts $f hello\nthere\nand\nhere
1824
    close $f
1825
    set f [open test1 r]
1826
    fconfigure $f -translation lf
1827
    set l ""
1828
    lappend l [string length [gets $f]]
1829
    lappend l [tell $f]
1830
    lappend l [fconfigure $f -translation]
1831
    lappend l [eof $f]
1832
    lappend l [gets $f]
1833
    lappend l [tell $f]
1834
    lappend l [fconfigure $f -translation]
1835
    lappend l [eof $f]
1836
    close $f
1837
    set l
1838
} {21 21 lf 1 {} 21 lf 1}
1839
test io-8.9 {Tcl_Write cr, Tcl_Gets crlf} {
1840
    removeFile test1
1841
    set f [open test1 w]
1842
    fconfigure $f -translation cr
1843
    puts $f hello\nthere\nand\nhere
1844
    close $f
1845
    set f [open test1 r]
1846
    fconfigure $f -translation crlf
1847
    set l ""
1848
    lappend l [string length [gets $f]]
1849
    lappend l [tell $f]
1850
    lappend l [fconfigure $f -translation]
1851
    lappend l [eof $f]
1852
    lappend l [gets $f]
1853
    lappend l [tell $f]
1854
    lappend l [fconfigure $f -translation]
1855
    lappend l [eof $f]
1856
    close $f
1857
    set l
1858
} {21 21 crlf 1 {} 21 crlf 1}
1859
test io-8.10 {Tcl_Write crlf, Tcl_Gets crlf} {
1860
    removeFile test1
1861
    set f [open test1 w]
1862
    fconfigure $f -translation crlf
1863
    puts $f hello\nthere\nand\nhere
1864
    close $f
1865
    set f [open test1 r]
1866
    fconfigure $f -translation crlf
1867
    set l ""
1868
    lappend l [gets $f]
1869
    lappend l [tell $f]
1870
    lappend l [fconfigure $f -translation]
1871
    lappend l [eof $f]
1872
    lappend l [gets $f]
1873
    lappend l [tell $f]
1874
    lappend l [fconfigure $f -translation]
1875
    lappend l [eof $f]
1876
    close $f
1877
    set l
1878
} {hello 7 crlf 0 there 14 crlf 0}
1879
test io-8.11 {Tcl_Write crlf, Tcl_Gets cr} {
1880
    removeFile test1
1881
    set f [open test1 w]
1882
    fconfigure $f -translation crlf
1883
    puts $f hello\nthere\nand\nhere
1884
    close $f
1885
    set f [open test1 r]
1886
    fconfigure $f -translation cr
1887
    set l ""
1888
    lappend l [gets $f]
1889
    lappend l [tell $f]
1890
    lappend l [fconfigure $f -translation]
1891
    lappend l [eof $f]
1892
    lappend l [string length [gets $f]]
1893
    lappend l [tell $f]
1894
    lappend l [fconfigure $f -translation]
1895
    lappend l [eof $f]
1896
    close $f
1897
    set l
1898
} {hello 6 cr 0 6 13 cr 0}
1899
test io-8.12 {Tcl_Write crlf, Tcl_Gets lf} {
1900
    removeFile test1
1901
    set f [open test1 w]
1902
    fconfigure $f -translation crlf
1903
    puts $f hello\nthere\nand\nhere
1904
    close $f
1905
    set f [open test1 r]
1906
    fconfigure $f -translation lf
1907
    set l ""
1908
    lappend l [string length [gets $f]]
1909
    lappend l [tell $f]
1910
    lappend l [fconfigure $f -translation]
1911
    lappend l [eof $f]
1912
    lappend l [string length [gets $f]]
1913
    lappend l [tell $f]
1914
    lappend l [fconfigure $f -translation]
1915
    lappend l [eof $f]
1916
    close $f
1917
    set l
1918
} {6 7 lf 0 6 14 lf 0}
1919
test io-8.13 {binary mode is synonym of lf mode} {
1920
    removeFile test1
1921
    set f [open test1 w]
1922
    fconfigure $f -translation binary
1923
    set x [fconfigure $f -translation]
1924
    close $f
1925
    set x
1926
} lf
1927
#
1928
# Test io-9.14 has been removed because "auto" output translation mode is
1929
# not supoprted.
1930
#
1931
test io-8.14 {Tcl_Write mixed, Tcl_Gets auto} {
1932
    removeFile test1
1933
    set f [open test1 w]
1934
    fconfigure $f -translation lf
1935
    puts $f hello\nthere\rand\r\nhere
1936
    close $f
1937
    set f [open test1 r]
1938
    fconfigure $f -translation auto
1939
    set l ""
1940
    lappend l [gets $f]
1941
    lappend l [gets $f]
1942
    lappend l [gets $f]
1943
    lappend l [gets $f]
1944
    lappend l [eof $f]
1945
    lappend l [gets $f]
1946
    lappend l [eof $f]
1947
    close $f
1948
    set l
1949
} {hello there and here 0 {} 1}
1950
test io-8.15 {Tcl_Write mixed, Tcl_Gets auto} {
1951
    removeFile test1
1952
    set f [open test1 w]
1953
    fconfigure $f -translation lf
1954
    puts -nonewline $f hello\nthere\rand\r\nhere\r
1955
    close $f
1956
    set f [open test1 r]
1957
    fconfigure $f -translation auto
1958
    set l ""
1959
    lappend l [gets $f]
1960
    lappend l [gets $f]
1961
    lappend l [gets $f]
1962
    lappend l [gets $f]
1963
    lappend l [eof $f]
1964
    lappend l [gets $f]
1965
    lappend l [eof $f]
1966
    close $f
1967
    set l
1968
} {hello there and here 0 {} 1}
1969
test io-8.16 {Tcl_Write mixed, Tcl_Gets auto} {
1970
    removeFile test1
1971
    set f [open test1 w]
1972
    fconfigure $f -translation lf
1973
    puts -nonewline $f hello\nthere\rand\r\nhere\n
1974
    close $f
1975
    set f [open test1 r]
1976
    set l ""
1977
    lappend l [gets $f]
1978
    lappend l [gets $f]
1979
    lappend l [gets $f]
1980
    lappend l [gets $f]
1981
    lappend l [eof $f]
1982
    lappend l [gets $f]
1983
    lappend l [eof $f]
1984
    close $f
1985
    set l
1986
} {hello there and here 0 {} 1}
1987
test io-8.17 {Tcl_Write mixed, Tcl_Gets auto} {
1988
    removeFile test1
1989
    set f [open test1 w]
1990
    fconfigure $f -translation lf
1991
    puts -nonewline $f hello\nthere\rand\r\nhere\r\n
1992
    close $f
1993
    set f [open test1 r]
1994
    fconfigure $f -translation auto
1995
    set l ""
1996
    lappend l [gets $f]
1997
    lappend l [gets $f]
1998
    lappend l [gets $f]
1999
    lappend l [gets $f]
2000
    lappend l [eof $f]
2001
    lappend l [gets $f]
2002
    lappend l [eof $f]
2003
    close $f
2004
    set l
2005
} {hello there and here 0 {} 1}
2006
test io-8.18 {Tcl_Write ^Z at end, Tcl_Gets auto} {
2007
    removeFile test1
2008
    set f [open test1 w]
2009
    fconfigure $f -translation lf
2010
    set s [format "hello\nthere\nand\rhere\n\%c" 26]
2011
    puts $f $s
2012
    close $f
2013
    set f [open test1 r]
2014
    fconfigure $f -eofchar \x1a -translation auto
2015
    set l ""
2016
    lappend l [gets $f]
2017
    lappend l [gets $f]
2018
    lappend l [gets $f]
2019
    lappend l [gets $f]
2020
    lappend l [eof $f]
2021
    lappend l [gets $f]
2022
    lappend l [eof $f]
2023
    close $f
2024
    set l
2025
} {hello there and here 0 {} 1}
2026
test io-8.19 {Tcl_Write, implicit ^Z at end, Tcl_Gets auto} {
2027
    removeFile test1
2028
    set f [open test1 w]
2029
    fconfigure $f -eofchar \x1a -translation lf
2030
    puts $f hello\nthere\nand\rhere
2031
    close $f
2032
    set f [open test1 r]
2033
    fconfigure $f -eofchar \x1a -translation auto
2034
    set l ""
2035
    lappend l [gets $f]
2036
    lappend l [gets $f]
2037
    lappend l [gets $f]
2038
    lappend l [gets $f]
2039
    lappend l [eof $f]
2040
    lappend l [gets $f]
2041
    lappend l [eof $f]
2042
    close $f
2043
    set l
2044
} {hello there and here 0 {} 1}
2045
test io-8.20 {Tcl_Write, ^Z in middle, Tcl_Gets auto, eofChar} {
2046
    removeFile test1
2047
    set f [open test1 w]
2048
    fconfigure $f -translation lf
2049
    set s [format "abc\ndef\n%cqrs\ntuv" 26]
2050
    puts $f $s
2051
    close $f
2052
    set f [open test1 r]
2053
    fconfigure $f -eofchar \x1a
2054
    fconfigure $f -translation auto
2055
    set l ""
2056
    lappend l [gets $f]
2057
    lappend l [gets $f]
2058
    lappend l [eof $f]
2059
    lappend l [gets $f]
2060
    lappend l [eof $f]
2061
    close $f
2062
    set l
2063
} {abc def 0 {} 1}
2064
test io-8.21 {Tcl_Write, no newline ^Z in middle, Tcl_Gets auto, eofChar} {
2065
    removeFile test1
2066
    set f [open test1 w]
2067
    fconfigure $f -translation lf
2068
    set s [format "abc\ndef\n%cqrs\ntuv" 26]
2069
    puts $f $s
2070
    close $f
2071
    set f [open test1 r]
2072
    fconfigure $f -eofchar \x1a -translation auto
2073
    set l ""
2074
    lappend l [gets $f]
2075
    lappend l [gets $f]
2076
    lappend l [eof $f]
2077
    lappend l [gets $f]
2078
    lappend l [eof $f]
2079
    close $f
2080
    set l
2081
} {abc def 0 {} 1}
2082
test io-8.22 {Tcl_Write, ^Z in middle ignored, Tcl_Gets lf} {
2083
    removeFile test1
2084
    set f [open test1 w]
2085
    fconfigure $f -translation lf -eofchar {}
2086
    set s [format "abc\ndef\n%cqrs\ntuv" 26]
2087
    puts $f $s
2088
    close $f
2089
    set f [open test1 r]
2090
    fconfigure $f -translation lf -eofchar {}
2091
    set l ""
2092
    lappend l [gets $f]
2093
    lappend l [gets $f]
2094
    lappend l [eof $f]
2095
    lappend l [gets $f]
2096
    lappend l [eof $f]
2097
    lappend l [gets $f]
2098
    lappend l [eof $f]
2099
    lappend l [gets $f]
2100
    lappend l [eof $f]
2101
    close $f
2102
    set l
2103
} "abc def 0 \x1aqrs 0 tuv 0 {} 1"
2104
test io-8.23 {Tcl_Write, ^Z in middle ignored, Tcl_Gets cr} {
2105
    removeFile test1
2106
    set f [open test1 w]
2107
    fconfigure $f -translation cr -eofchar {}
2108
    set s [format "abc\ndef\n%cqrs\ntuv" 26]
2109
    puts $f $s
2110
    close $f
2111
    set f [open test1 r]
2112
    fconfigure $f -translation cr -eofchar {}
2113
    set l ""
2114
    lappend l [gets $f]
2115
    lappend l [gets $f]
2116
    lappend l [eof $f]
2117
    lappend l [gets $f]
2118
    lappend l [eof $f]
2119
    lappend l [gets $f]
2120
    lappend l [eof $f]
2121
    lappend l [gets $f]
2122
    lappend l [eof $f]
2123
    close $f
2124
    set l
2125
} "abc def 0 \x1aqrs 0 tuv 0 {} 1"
2126
test io-8.24 {Tcl_Write, ^Z in middle ignored, Tcl_Gets crlf} {
2127
    removeFile test1
2128
    set f [open test1 w]
2129
    fconfigure $f -translation crlf -eofchar {}
2130
    set s [format "abc\ndef\n%cqrs\ntuv" 26]
2131
    puts $f $s
2132
    close $f
2133
    set f [open test1 r]
2134
    fconfigure $f -translation crlf -eofchar {}
2135
    set l ""
2136
    lappend l [gets $f]
2137
    lappend l [gets $f]
2138
    lappend l [eof $f]
2139
    lappend l [gets $f]
2140
    lappend l [eof $f]
2141
    lappend l [gets $f]
2142
    lappend l [eof $f]
2143
    lappend l [gets $f]
2144
    lappend l [eof $f]
2145
    close $f
2146
    set l
2147
} "abc def 0 \x1aqrs 0 tuv 0 {} 1"
2148
test io-8.25 {Tcl_Write lf, ^Z in middle, Tcl_Gets auto} {
2149
    removeFile test1
2150
    set f [open test1 w]
2151
    fconfigure $f -translation lf
2152
    set s [format "abc\ndef\n%cqrs\ntuv" 26]
2153
    puts $f $s
2154
    close $f
2155
    set f [open test1 r]
2156
    fconfigure $f -translation auto -eofchar \x1a
2157
    set l ""
2158
    lappend l [gets $f]
2159
    lappend l [gets $f]
2160
    lappend l [eof $f]
2161
    lappend l [gets $f]
2162
    lappend l [eof $f]
2163
    close $f
2164
    set l
2165
} {abc def 0 {} 1}
2166
test io-8.26 {Tcl_Write lf, ^Z in middle, Tcl_Gets lf} {
2167
    removeFile test1
2168
    set f [open test1 w]
2169
    fconfigure $f -translation lf
2170
    set s [format "abc\ndef\n%cqrs\ntuv" 26]
2171
    puts $f $s
2172
    close $f
2173
    set f [open test1 r]
2174
    fconfigure $f -translation lf -eofchar \x1a
2175
    set l ""
2176
    lappend l [gets $f]
2177
    lappend l [gets $f]
2178
    lappend l [eof $f]
2179
    lappend l [gets $f]
2180
    lappend l [eof $f]
2181
    close $f
2182
    set l
2183
} {abc def 0 {} 1}
2184
test io-8.27 {Tcl_Write cr, ^Z in middle, Tcl_Gets auto} {
2185
    removeFile test1
2186
    set f [open test1 w]
2187
    fconfigure $f -translation cr -eofchar {}
2188
    set s [format "abc\ndef\n%cqrs\ntuv" 26]
2189
    puts $f $s
2190
    close $f
2191
    set f [open test1 r]
2192
    fconfigure $f -translation auto -eofchar \x1a
2193
    set l ""
2194
    lappend l [gets $f]
2195
    lappend l [gets $f]
2196
    lappend l [eof $f]
2197
    lappend l [gets $f]
2198
    lappend l [eof $f]
2199
    close $f
2200
    set l
2201
} {abc def 0 {} 1}
2202
test io-8.28 {Tcl_Write cr, ^Z in middle, Tcl_Gets cr} {
2203
    removeFile test1
2204
    set f [open test1 w]
2205
    fconfigure $f -translation cr -eofchar {}
2206
    set s [format "abc\ndef\n%cqrs\ntuv" 26]
2207
    puts $f $s
2208
    close $f
2209
    set f [open test1 r]
2210
    fconfigure $f -translation cr -eofchar \x1a
2211
    set l ""
2212
    lappend l [gets $f]
2213
    lappend l [gets $f]
2214
    lappend l [eof $f]
2215
    lappend l [gets $f]
2216
    lappend l [eof $f]
2217
    close $f
2218
    set l
2219
} {abc def 0 {} 1}
2220
test io-8.29 {Tcl_Write crlf, ^Z in middle, Tcl_Gets auto} {
2221
    removeFile test1
2222
    set f [open test1 w]
2223
    fconfigure $f -translation crlf -eofchar {}
2224
    set s [format "abc\ndef\n%cqrs\ntuv" 26]
2225
    puts $f $s
2226
    close $f
2227
    set f [open test1 r]
2228
    fconfigure $f -translation auto -eofchar \x1a
2229
    set l ""
2230
    lappend l [gets $f]
2231
    lappend l [gets $f]
2232
    lappend l [eof $f]
2233
    lappend l [gets $f]
2234
    lappend l [eof $f]
2235
    close $f
2236
    set l
2237
} {abc def 0 {} 1}
2238
test io-8.30 {Tcl_Write crlf, ^Z in middle, Tcl_Gets crlf} {
2239
    removeFile test1
2240
    set f [open test1 w]
2241
    fconfigure $f -translation crlf -eofchar {}
2242
    set s [format "abc\ndef\n%cqrs\ntuv" 26]
2243
    puts $f $s
2244
    close $f
2245
    set f [open test1 r]
2246
    fconfigure $f -translation crlf -eofchar \x1a
2247
    set l ""
2248
    lappend l [gets $f]
2249
    lappend l [gets $f]
2250
    lappend l [eof $f]
2251
    lappend l [gets $f]
2252
    lappend l [eof $f]
2253
    close $f
2254
    set l
2255
} {abc def 0 {} 1}
2256
test io-8.31 {Tcl_Write crlf on block boundary, Tcl_Gets auto} {
2257
    removeFile test1
2258
    set f [open test1 w]
2259
    fconfigure $f -translation crlf
2260
    set line "123456789ABCDE"   ;# 14 char plus crlf
2261
    puts -nonewline $f x        ;# shift crlf across block boundary
2262
    for {set i 0} {$i < 700} {incr i} {
2263
        puts $f $line
2264
    }
2265
    close $f
2266
    set f [open test1 r]
2267
    fconfigure $f -translation auto
2268
    set c ""
2269
    while {[gets $f line] >= 0} {
2270
        append c $line\n
2271
    }
2272
    close $f
2273
    string length $c
2274
} [expr 700*15+1]
2275
test io-8.32 {Tcl_Write crlf on block boundary, Tcl_Gets auto} {
2276
    removeFile test1
2277
    set f [open test1 w]
2278
    fconfigure $f -translation crlf
2279
    set line "123456789ABCDE"   ;# 14 char plus crlf
2280
    puts -nonewline $f x        ;# shift crlf across block boundary
2281
    for {set i 0} {$i < 256} {incr i} {
2282
        puts $f $line
2283
    }
2284
    close $f
2285
    set f [open test1 r]
2286
    fconfigure $f -translation auto
2287
    set c ""
2288
    while {[gets $f line] >= 0} {
2289
        append c $line\n
2290
    }
2291
    close $f
2292
    string length $c
2293
} [expr 256*15+1]
2294
 
2295
 
2296
# Test Tcl_Read and buffering.
2297
 
2298
test io-9.1 {Tcl_Read, channel not readable} {
2299
    list [catch {read stdout} msg] $msg
2300
} {1 {channel "stdout" wasn't opened for reading}}
2301
test io-9.2 {Tcl_Read, zero byte count} {
2302
    read stdin 0
2303
} ""
2304
test io-9.3 {Tcl_Read, negative byte count} {
2305
    set f [open longfile r]
2306
    set l [list [catch {read $f -1} msg] $msg]
2307
    close $f
2308
    set l
2309
} {1 {bad argument "-1": should be "nonewline"}}
2310
test io-9.4 {Tcl_Read, positive byte count} {
2311
    set f [open longfile r]
2312
    set x [read $f 1024]
2313
    set s [string length $x]
2314
    unset x
2315
    close $f
2316
    set s
2317
} 1024
2318
test io-9.5 {Tcl_Read, multiple buffers} {
2319
    set f [open longfile r]
2320
    fconfigure $f -buffersize 100
2321
    set x [read $f 1024]
2322
    set s [string length $x]
2323
    unset x
2324
    close $f
2325
    set s
2326
} 1024
2327
test io-9.6 {Tcl_Read, very large read} {
2328
    set f1 [open longfile r]
2329
    set z [read $f1 1000000]
2330
    close $f1
2331
    set l [string length $z]
2332
    set x ok
2333
    set z [file size longfile]
2334
    if {$z != $l} {
2335
        set x broken
2336
    }
2337
    set x
2338
} ok
2339
test io-9.7 {Tcl_Read, nonblocking, file} {nonBlockFiles} {
2340
    set f1 [open longfile r]
2341
    fconfigure $f1 -blocking off
2342
    set z [read $f1 20]
2343
    close $f1
2344
    set l [string length $z]
2345
    set x ok
2346
    if {$l != 20} {
2347
        set x broken
2348
    }
2349
    set x
2350
} ok
2351
test io-9.8 {Tcl_Read, nonblocking, file} {nonBlockFiles} {
2352
    set f1 [open longfile r]
2353
    fconfigure $f1 -blocking off
2354
    set z [read $f1 1000000]
2355
    close $f1
2356
    set x ok
2357
    set l [string length $z]]
2358
    set z [file size longfile]]
2359
    if {$z != $l} {
2360
        set x broken
2361
    }
2362
  set x
2363
} ok
2364
test io-9.9 {Tcl_Read, read to end of file} {
2365
    set f1 [open longfile r]
2366
    set z [read $f1]
2367
    close $f1
2368
    set l [string length $z]
2369
    set x ok
2370
    set z [file size longfile]
2371
    if {$z != $l} {
2372
        set x broken
2373
    }
2374
    set x
2375
} ok
2376
test io-9.10 {Tcl_Read from a pipe} {stdio} {
2377
    removeFile pipe
2378
    set f1 [open pipe w]
2379
    puts $f1 {puts [gets stdin]}
2380
    close $f1
2381
    set f1 [open "|[list $tcltest pipe]" r+]
2382
    puts $f1 hello
2383
    flush $f1
2384
    set x [read $f1]
2385
    close $f1
2386
    set x
2387
} "hello\n"
2388
test io-9.11 {Tcl_Read from a pipe} {stdio} {
2389
    removeFile pipe
2390
    set f1 [open pipe w]
2391
    puts $f1 {puts [gets stdin]}
2392
    puts $f1 {puts [gets stdin]}
2393
    close $f1
2394
    set f1 [open "|[list $tcltest pipe]" r+]
2395
    puts $f1 hello
2396
    flush $f1
2397
    set x ""
2398
    lappend x [read $f1 6]
2399
    puts $f1 hello
2400
    flush $f1
2401
    lappend x [read $f1]
2402
    close $f1
2403
    set x
2404
} {{hello
2405
} {hello
2406
}}
2407
test io-9.12 {Tcl_Read, -nonewline} {
2408
    removeFile test1
2409
    set f1 [open test1 w]
2410
    puts $f1 hello
2411
    puts $f1 bye
2412
    close $f1
2413
    set f1 [open test1 r]
2414
    set c [read -nonewline $f1]
2415
    close $f1
2416
    set c
2417
} {hello
2418
bye}
2419
test io-9.13 {Tcl_Read, -nonewline} {
2420
    removeFile test1
2421
    set f1 [open test1 w]
2422
    puts $f1 hello
2423
    puts $f1 bye
2424
    close $f1
2425
    set f1 [open test1 r]
2426
    set c [read -nonewline $f1]
2427
    close $f1
2428
    list [string length $c] $c
2429
} {9 {hello
2430
bye}}
2431
test io-9.14 {Tcl_Read, reading in small chunks} {
2432
    removeFile test1
2433
    set f [open test1 w]
2434
    puts $f "Two lines: this one"
2435
    puts $f "and this one"
2436
    close $f
2437
    set f [open test1]
2438
    set x [list [read $f 1] [read $f 2] [read $f]]
2439
    close $f
2440
    set x
2441
} {T wo { lines: this one
2442
and this one
2443
}}
2444
test io-9.15 {Tcl_Read, asking for more input than available} {
2445
    removeFile test1
2446
    set f [open test1 w]
2447
    puts $f "Two lines: this one"
2448
    puts $f "and this one"
2449
    close $f
2450
    set f [open test1]
2451
    set x [read $f 100]
2452
    close $f
2453
    set x
2454
} {Two lines: this one
2455
and this one
2456
}
2457
test io-9.16 {Tcl_Read, read to end of file with -nonewline} {
2458
    removeFile test1
2459
    set f [open test1 w]
2460
    puts $f "Two lines: this one"
2461
    puts $f "and this one"
2462
    close $f
2463
    set f [open test1]
2464
    set x [read -nonewline $f]
2465
    close $f
2466
    set x
2467
} {Two lines: this one
2468
and this one}
2469
 
2470
# Test Tcl_Gets.
2471
 
2472
test io-10.1 {Tcl_Gets, reading what was written} {
2473
    removeFile test1
2474
    set f1 [open test1 w]
2475
    set y "first line"
2476
    puts $f1 $y
2477
    close $f1
2478
    set f1 [open test1 r]
2479
    set x [gets $f1]
2480
    set z ok
2481
    if {"$x" != "$y"} {
2482
        set z broken
2483
    }
2484
    close $f1
2485
    set z
2486
} ok
2487
test io-10.2 {Tcl_Gets into variable} {
2488
    set f1 [open longfile r]
2489
    set c [gets $f1 x]
2490
    set l [string length x]
2491
    set z ok
2492
    if {$l != $l} {
2493
        set z broken
2494
    }
2495
    close $f1
2496
    set z
2497
} ok
2498
test io-10.3 {Tcl_Gets from pipe} {stdio} {
2499
    removeFile pipe
2500
    set f1 [open pipe w]
2501
    puts $f1 {puts [gets stdin]}
2502
    close $f1
2503
    set f1 [open "|[list $tcltest pipe]" r+]
2504
    puts $f1 hello
2505
    flush $f1
2506
    set x [gets $f1]
2507
    close $f1
2508
    set z ok
2509
    if {"$x" != "hello"} {
2510
        set z broken
2511
    }
2512
    set z
2513
} ok
2514
test io-10.4 {Tcl_Gets with long line} {
2515
    removeFile test3
2516
    set f [open test3 w]
2517
    puts -nonewline $f "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
2518
    puts -nonewline $f "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
2519
    puts -nonewline $f "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
2520
    puts -nonewline $f "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
2521
    puts $f "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
2522
    close $f
2523
    set f [open test3]
2524
    set x [gets $f]
2525
    close $f
2526
    set x
2527
} {abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ}
2528
test io-10.5 {Tcl_Gets with long line} {
2529
    set f [open test3]
2530
    set x [gets $f y]
2531
    close $f
2532
    list $x $y
2533
} {260 abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ}
2534
test io-10.6 {Tcl_Gets and end of file} {
2535
    removeFile test3
2536
    set f [open test3 w]
2537
    puts -nonewline $f "Test1\nTest2"
2538
    close $f
2539
    set f [open test3]
2540
    set x {}
2541
    set y {}
2542
    lappend x [gets $f y] $y
2543
    set y {}
2544
    lappend x [gets $f y] $y
2545
    set y {}
2546
    lappend x [gets $f y] $y
2547
    close $f
2548
    set x
2549
} {5 Test1 5 Test2 -1 {}}
2550
test io-10.7 {Tcl_Gets and bad variable} {
2551
    set f [open test3 w]
2552
    puts $f "Line 1"
2553
    puts $f "Line 2"
2554
    close $f
2555
    catch {unset x}
2556
    set x 24
2557
    set f [open test3 r]
2558
    set result [list [catch {gets $f x(0)} msg] $msg]
2559
    close $f
2560
    set result
2561
} {1 {can't set "x(0)": variable isn't array}}
2562
test io-10.8 {Tcl_Gets, exercising double buffering} {
2563
    set f [open test3 w]
2564
    fconfigure $f -translation lf -eofchar {}
2565
    set x ""
2566
    for {set y 0} {$y < 99} {incr y} {set x "a$x"}
2567
    for {set y 0} {$y < 100} {incr y} {puts $f $x}
2568
    close $f
2569
    set f [open test3 r]
2570
    fconfigure $f -translation lf
2571
    for {set y 0} {$y < 100} {incr y} {gets $f}
2572
    close $f
2573
    set y
2574
} 100
2575
test io-10.9 {Tcl_Gets, exercising double buffering} {
2576
    set f [open test3 w]
2577
    fconfigure $f -translation lf -eofchar {}
2578
    set x ""
2579
    for {set y 0} {$y < 99} {incr y} {set x "a$x"}
2580
    for {set y 0} {$y < 200} {incr y} {puts $f $x}
2581
    close $f
2582
    set f [open test3 r]
2583
    fconfigure $f -translation lf
2584
    for {set y 0} {$y < 200} {incr y} {gets $f}
2585
    close $f
2586
    set y
2587
} 200
2588
test io-10.10 {Tcl_Gets, exercising double buffering} {
2589
    set f [open test3 w]
2590
    fconfigure $f -translation lf -eofchar {}
2591
    set x ""
2592
    for {set y 0} {$y < 99} {incr y} {set x "a$x"}
2593
    for {set y 0} {$y < 300} {incr y} {puts $f $x}
2594
    close $f
2595
    set f [open test3 r]
2596
    fconfigure $f -translation lf
2597
    for {set y 0} {$y < 300} {incr y} {gets $f}
2598
    close $f
2599
    set y
2600
} 300
2601
 
2602
# Test Tcl_Seek and Tcl_Tell.
2603
 
2604
test io-11.1 {Tcl_Seek to current position at start of file} {
2605
    set f1 [open longfile r]
2606
    seek $f1 0 current
2607
    set c [tell $f1]
2608
    close $f1
2609
    set c
2610
} 0
2611
test io-11.2 {Tcl_Seek to offset from start} {
2612
    removeFile test1
2613
    set f1 [open test1 w]
2614
    fconfigure $f1 -translation lf -eofchar {}
2615
    puts $f1 "abcdefghijklmnopqrstuvwxyz"
2616
    puts $f1 "abcdefghijklmnopqrstuvwxyz"
2617
    close $f1
2618
    set f1 [open test1 r]
2619
    seek $f1 10 start
2620
    set c [tell $f1]
2621
    close $f1
2622
    set c
2623
} 10
2624
test io-11.3 {Tcl_Seek to end of file} {
2625
    removeFile test1
2626
    set f1 [open test1 w]
2627
    fconfigure $f1 -translation lf -eofchar {}
2628
    puts $f1 "abcdefghijklmnopqrstuvwxyz"
2629
    puts $f1 "abcdefghijklmnopqrstuvwxyz"
2630
    close $f1
2631
    set f1 [open test1 r]
2632
    seek $f1 0 end
2633
    set c [tell $f1]
2634
    close $f1
2635
    set c
2636
} 54
2637
test io-11.4 {Tcl_Seek to offset from end of file} {
2638
    removeFile test1
2639
    set f1 [open test1 w]
2640
    fconfigure $f1 -translation lf -eofchar {}
2641
    puts $f1 "abcdefghijklmnopqrstuvwxyz"
2642
    puts $f1 "abcdefghijklmnopqrstuvwxyz"
2643
    close $f1
2644
    set f1 [open test1 r]
2645
    seek $f1 -10 end
2646
    set c [tell $f1]
2647
    close $f1
2648
    set c
2649
} 44
2650
test io-11.5 {Tcl_Seek to offset from current position} {
2651
    removeFile test1
2652
    set f1 [open test1 w]
2653
    fconfigure $f1 -translation lf -eofchar {}
2654
    puts $f1 "abcdefghijklmnopqrstuvwxyz"
2655
    puts $f1 "abcdefghijklmnopqrstuvwxyz"
2656
    close $f1
2657
    set f1 [open test1 r]
2658
    seek $f1 10 current
2659
    seek $f1 10 current
2660
    set c [tell $f1]
2661
    close $f1
2662
    set c
2663
} 20
2664
test io-11.6 {Tcl_Seek to offset from end of file} {
2665
    removeFile test1
2666
    set f1 [open test1 w]
2667
    fconfigure $f1 -translation lf -eofchar {}
2668
    puts $f1 "abcdefghijklmnopqrstuvwxyz"
2669
    puts $f1 "abcdefghijklmnopqrstuvwxyz"
2670
    close $f1
2671
    set f1 [open test1 r]
2672
    seek $f1 -10 end
2673
    set c [tell $f1]
2674
    set r [read $f1]
2675
    close $f1
2676
    list $c $r
2677
} {44 {rstuvwxyz
2678
}}
2679
test io-11.7 {Tcl_Seek to offset from end of file, then to current position} {
2680
    removeFile test1
2681
    set f1 [open test1 w]
2682
    fconfigure $f1 -translation lf -eofchar {}
2683
    puts $f1 "abcdefghijklmnopqrstuvwxyz"
2684
    puts $f1 "abcdefghijklmnopqrstuvwxyz"
2685
    close $f1
2686
    set f1 [open test1 r]
2687
    seek $f1 -10 end
2688
    set c1 [tell $f1]
2689
    set r1 [read $f1 5]
2690
    seek $f1 0 current
2691
    set c2 [tell $f1]
2692
    close $f1
2693
    list $c1 $r1 $c2
2694
} {44 rstuv 49}
2695
test io-11.8 {Tcl_Seek on pipes: not supported} {stdio} {
2696
    set f1 [open "|[list $tcltest]" r+]
2697
    set x [list [catch {seek $f1 0 current} msg] $msg]
2698
    close $f1
2699
    regsub {".*":} $x {"":} x
2700
    string tolower $x
2701
} {1 {error during seek on "": invalid argument}}
2702
test io-11.9 {Tcl_Seek, testing buffered input flushing} {
2703
    removeFile test3
2704
    set f [open test3 w]
2705
    fconfigure $f -eofchar {}
2706
    puts -nonewline $f "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
2707
    close $f
2708
    set f [open test3 RDWR]
2709
    set x [read $f 1]
2710
    seek $f 3
2711
    lappend x [read $f 1]
2712
    seek $f 0 start
2713
    lappend x [read $f 1]
2714
    seek $f 10 current
2715
    lappend x [read $f 1]
2716
    seek $f -2 end
2717
    lappend x [read $f 1]
2718
    seek $f 50 end
2719
    lappend x [read $f 1]
2720
    seek $f 1
2721
    lappend x [read $f 1]
2722
    close $f
2723
    set x
2724
} {a d a l Y {} b}
2725
test io-11.10 {Tcl_Seek testing flushing of buffered input} {
2726
    set f [open test3 w]
2727
    fconfigure $f -translation lf
2728
    puts $f xyz\n123
2729
    close $f
2730
    set f [open test3 r+]
2731
    fconfigure $f -translation lf
2732
    set x [gets $f]
2733
    seek $f 0 current
2734
    puts $f 456
2735
    close $f
2736
    list $x [viewFile test3]
2737
} "xyz {xyz
2738
456}"
2739
test io-11.11 {Tcl_Seek testing flushing of buffered output} {
2740
    set f [open test3 w]
2741
    puts $f xyz\n123
2742
    close $f
2743
    set f [open test3 w+]
2744
    puts $f xyzzy
2745
    seek $f 2
2746
    set x [gets $f]
2747
    close $f
2748
    list $x [viewFile test3]
2749
} "zzy xyzzy"
2750
test io-11.12 {Tcl_Seek testing combination of write, seek back and read} {
2751
    set f [open test3 w]
2752
    fconfigure $f -translation lf -eofchar {}
2753
    puts $f xyz\n123
2754
    close $f
2755
    set f [open test3 a+]
2756
    fconfigure $f -translation lf -eofchar {}
2757
    puts $f xyzzy
2758
    flush $f
2759
    set x [tell $f]
2760
    seek $f -4 cur
2761
    set y [gets $f]
2762
    close $f
2763
    list $x [viewFile test3] $y
2764
} {14 {xyz
2765
123
2766
xyzzy} zzy}
2767
test io-11.13 {Tcl_Tell at start of file} {
2768
    removeFile test1
2769
    set f1 [open test1 w]
2770
    set p [tell $f1]
2771
    close $f1
2772
    set p
2773
} 0
2774
test io-11.14 {Tcl_Tell after seek to end of file} {
2775
    removeFile test1
2776
    set f1 [open test1 w]
2777
    fconfigure $f1 -translation lf -eofchar {}
2778
    puts $f1 "abcdefghijklmnopqrstuvwxyz"
2779
    puts $f1 "abcdefghijklmnopqrstuvwxyz"
2780
    close $f1
2781
    set f1 [open test1 r]
2782
    seek $f1 0 end
2783
    set c1 [tell $f1]
2784
    close $f1
2785
    set c1
2786
} 54
2787
test io-11.15 {Tcl_Tell combined with seeking} {
2788
    removeFile test1
2789
    set f1 [open test1 w]
2790
    fconfigure $f1 -translation lf -eofchar {}
2791
    puts $f1 "abcdefghijklmnopqrstuvwxyz"
2792
    puts $f1 "abcdefghijklmnopqrstuvwxyz"
2793
    close $f1
2794
    set f1 [open test1 r]
2795
    seek $f1 10 start
2796
    set c1 [tell $f1]
2797
    seek $f1 10 current
2798
    set c2 [tell $f1]
2799
    close $f1
2800
    list $c1 $c2
2801
} {10 20}
2802
test io-11.16 {Tcl_tell on pipe: always -1} {stdio} {
2803
    set f1 [open "|[list $tcltest]" r+]
2804
    set c [tell $f1]
2805
    close $f1
2806
    set c
2807
} -1
2808
test io-11.17 {Tcl_Tell on pipe: always -1} {stdio} {
2809
    set f1 [open "|[list $tcltest]" r+]
2810
    puts $f1 {puts hello}
2811
    flush $f1
2812
    set c [tell $f1]
2813
    gets $f1
2814
    close $f1
2815
    set c
2816
} -1
2817
test io-11.18 {Tcl_Tell combined with seeking and reading} {
2818
    removeFile test2
2819
    set f [open test2 w]
2820
    fconfigure $f -translation lf -eofchar {}
2821
    puts -nonewline $f "line1\nline2\nline3\nline4\nline5\n"
2822
    close $f
2823
    set f [open test2]
2824
    fconfigure $f -translation lf
2825
    set x [tell $f]
2826
    read $f 3
2827
    lappend x [tell $f]
2828
    seek $f 2
2829
    lappend x [tell $f]
2830
    seek $f 10 current
2831
    lappend x [tell $f]
2832
    seek $f 0 end
2833
    lappend x [tell $f]
2834
    close $f
2835
    set x
2836
} {0 3 2 12 30}
2837
test io-11.19 {Tcl_Tell combined with opening in append mode} {
2838
    set f [open test3 w]
2839
    fconfigure $f -translation lf -eofchar {}
2840
    puts $f "abcdefghijklmnopqrstuvwxyz"
2841
    puts $f "abcdefghijklmnopqrstuvwxyz"
2842
    close $f
2843
    set f [open test3 a]
2844
    set c [tell $f]
2845
    close $f
2846
    set c
2847
} 54
2848
test io-11.20 {Tcl_Tell combined with writing} {
2849
    set f [open test3 w]
2850
    set l ""
2851
    seek $f 29 start
2852
    lappend l [tell $f]
2853
    puts -nonewline $f a
2854
    seek $f 39 start
2855
    lappend l [tell $f]
2856
    puts -nonewline $f a
2857
    lappend l [tell $f]
2858
    seek $f 407 end
2859
    lappend l [tell $f]
2860
    close $f
2861
    set l
2862
} {29 39 40 447}
2863
 
2864
# Test Tcl_Eof
2865
 
2866
test io-12.1 {Tcl_Eof} {
2867
    removeFile test1
2868
    set f [open test1 w]
2869
    puts $f hello
2870
    puts $f hello
2871
    close $f
2872
    set f [open test1]
2873
    set x [eof $f]
2874
    lappend x [eof $f]
2875
    gets $f
2876
    lappend x [eof $f]
2877
    gets $f
2878
    lappend x [eof $f]
2879
    gets $f
2880
    lappend x [eof $f]
2881
    lappend x [eof $f]
2882
    close $f
2883
    set x
2884
} {0 0 0 0 1 1}
2885
test io-12.2 {Tcl_Eof with pipe} {stdio} {
2886
    removeFile pipe
2887
    set f1 [open pipe w]
2888
    puts $f1 {gets stdin}
2889
    puts $f1 {puts hello}
2890
    close $f1
2891
    set f1 [open "|[list $tcltest pipe]" r+]
2892
    puts $f1 hello
2893
    set x [eof $f1]
2894
    flush $f1
2895
    lappend x [eof $f1]
2896
    gets $f1
2897
    lappend x [eof $f1]
2898
    gets $f1
2899
    lappend x [eof $f1]
2900
    close $f1
2901
    set x
2902
} {0 0 0 1}
2903
test io-12.3 {Tcl_Eof with pipe} {stdio} {
2904
    removeFile pipe
2905
    set f1 [open pipe w]
2906
    puts $f1 {gets stdin}
2907
    puts $f1 {puts hello}
2908
    close $f1
2909
    set f1 [open "|[list $tcltest pipe]" r+]
2910
    puts $f1 hello
2911
    set x [eof $f1]
2912
    flush $f1
2913
    lappend x [eof $f1]
2914
    gets $f1
2915
    lappend x [eof $f1]
2916
    gets $f1
2917
    lappend x [eof $f1]
2918
    gets $f1
2919
    lappend x [eof $f1]
2920
    gets $f1
2921
    lappend x [eof $f1]
2922
    close $f1
2923
    set x
2924
} {0 0 0 1 1 1}
2925
test io-12.4 {Tcl_Eof, eof detection on nonblocking file} {nonBlockFiles} {
2926
    removeFile test1
2927
    set f [open test1 w]
2928
    close $f
2929
    set f [open test1 r]
2930
    fconfigure $f -blocking off
2931
    set l ""
2932
    lappend l [gets $f]
2933
    lappend l [eof $f]
2934
    close $f
2935
    set l
2936
} {{} 1}
2937
test io-12.5 {Tcl_Eof, eof detection on nonblocking pipe} {stdio} {
2938
    removeFile pipe
2939
    set f [open pipe w]
2940
    puts $f {
2941
        exit
2942
    }
2943
    close $f
2944
    set f [open "|[list $tcltest pipe]" r]
2945
    set l ""
2946
    lappend l [gets $f]
2947
    lappend l [eof $f]
2948
    close $f
2949
    set l
2950
} {{} 1}
2951
test io-12.6 {Tcl_Eof, eof char, lf write, auto read} {
2952
    removeFile test1
2953
    set f [open test1 w]
2954
    fconfigure $f -translation lf -eofchar \x1a
2955
    puts $f abc\ndef
2956
    close $f
2957
    set s [file size test1]
2958
    set f [open test1 r]
2959
    fconfigure $f -translation auto -eofchar \x1a
2960
    set l [string length [read $f]]
2961
    set e [eof $f]
2962
    close $f
2963
    list $s $l $e
2964
} {9 8 1}
2965
test io-12.7 {Tcl_Eof, eof char, lf write, lf read} {
2966
    removeFile test1
2967
    set f [open test1 w]
2968
    fconfigure $f -translation lf -eofchar \x1a
2969
    puts $f abc\ndef
2970
    close $f
2971
    set s [file size test1]
2972
    set f [open test1 r]
2973
    fconfigure $f -translation lf -eofchar \x1a
2974
    set l [string length [read $f]]
2975
    set e [eof $f]
2976
    close $f
2977
    list $s $l $e
2978
} {9 8 1}
2979
test io-12.8 {Tcl_Eof, eof char, cr write, auto read} {
2980
    removeFile test1
2981
    set f [open test1 w]
2982
    fconfigure $f -translation cr -eofchar \x1a
2983
    puts $f abc\ndef
2984
    close $f
2985
    set s [file size test1]
2986
    set f [open test1 r]
2987
    fconfigure $f -translation auto -eofchar \x1a
2988
    set l [string length [read $f]]
2989
    set e [eof $f]
2990
    close $f
2991
    list $s $l $e
2992
} {9 8 1}
2993
test io-12.9 {Tcl_Eof, eof char, cr write, cr read} {
2994
    removeFile test1
2995
    set f [open test1 w]
2996
    fconfigure $f -translation cr -eofchar \x1a
2997
    puts $f abc\ndef
2998
    close $f
2999
    set s [file size test1]
3000
    set f [open test1 r]
3001
    fconfigure $f -translation cr -eofchar \x1a
3002
    set l [string length [read $f]]
3003
    set e [eof $f]
3004
    close $f
3005
    list $s $l $e
3006
} {9 8 1}
3007
test io-12.10 {Tcl_Eof, eof char, crlf write, auto read} {
3008
    removeFile test1
3009
    set f [open test1 w]
3010
    fconfigure $f -translation crlf -eofchar \x1a
3011
    puts $f abc\ndef
3012
    close $f
3013
    set s [file size test1]
3014
    set f [open test1 r]
3015
    fconfigure $f -translation auto -eofchar \x1a
3016
    set l [string length [read $f]]
3017
    set e [eof $f]
3018
    close $f
3019
    list $s $l $e
3020
} {11 8 1}
3021
test io-12.11 {Tcl_Eof, eof char, crlf write, crlf read} {
3022
    removeFile test1
3023
    set f [open test1 w]
3024
    fconfigure $f -translation crlf -eofchar \x1a
3025
    puts $f abc\ndef
3026
    close $f
3027
    set s [file size test1]
3028
    set f [open test1 r]
3029
    fconfigure $f -translation crlf -eofchar \x1a
3030
    set l [string length [read $f]]
3031
    set e [eof $f]
3032
    close $f
3033
    list $s $l $e
3034
} {11 8 1}
3035
test io-12.12 {Tcl_Eof, eof char in middle, lf write, auto read} {
3036
    removeFile test1
3037
    set f [open test1 w]
3038
    fconfigure $f -translation lf -eofchar {}
3039
    set i [format abc\ndef\n%cqrs\nuvw 26]
3040
    puts $f $i
3041
    close $f
3042
    set c [file size test1]
3043
    set f [open test1 r]
3044
    fconfigure $f -translation auto -eofchar \x1a
3045
    set l [string length [read $f]]
3046
    set e [eof $f]
3047
    close $f
3048
    list $c $l $e
3049
} {17 8 1}
3050
test io-12.13 {Tcl_Eof, eof char in middle, lf write, lf read} {
3051
    removeFile test1
3052
    set f [open test1 w]
3053
    fconfigure $f -translation lf -eofchar {}
3054
    set i [format abc\ndef\n%cqrs\nuvw 26]
3055
    puts $f $i
3056
    close $f
3057
    set c [file size test1]
3058
    set f [open test1 r]
3059
    fconfigure $f -translation lf -eofchar \x1a
3060
    set l [string length [read $f]]
3061
    set e [eof $f]
3062
    close $f
3063
    list $c $l $e
3064
} {17 8 1}
3065
test io-12.14 {Tcl_Eof, eof char in middle, cr write, auto read} {
3066
    removeFile test1
3067
    set f [open test1 w]
3068
    fconfigure $f -translation cr -eofchar {}
3069
    set i [format abc\ndef\n%cqrs\nuvw 26]
3070
    puts $f $i
3071
    close $f
3072
    set c [file size test1]
3073
    set f [open test1 r]
3074
    fconfigure $f -translation auto -eofchar \x1a
3075
    set l [string length [read $f]]
3076
    set e [eof $f]
3077
    close $f
3078
    list $c $l $e
3079
} {17 8 1}
3080
test io-12.15 {Tcl_Eof, eof char in middle, cr write, cr read} {
3081
    removeFile test1
3082
    set f [open test1 w]
3083
    fconfigure $f -translation cr -eofchar {}
3084
    set i [format abc\ndef\n%cqrs\nuvw 26]
3085
    puts $f $i
3086
    close $f
3087
    set c [file size test1]
3088
    set f [open test1 r]
3089
    fconfigure $f -translation cr -eofchar \x1a
3090
    set l [string length [read $f]]
3091
    set e [eof $f]
3092
    close $f
3093
    list $c $l $e
3094
} {17 8 1}
3095
test io-12.16 {Tcl_Eof, eof char in middle, crlf write, auto read} {
3096
    removeFile test1
3097
    set f [open test1 w]
3098
    fconfigure $f -translation crlf -eofchar {}
3099
    set i [format abc\ndef\n%cqrs\nuvw 26]
3100
    puts $f $i
3101
    close $f
3102
    set c [file size test1]
3103
    set f [open test1 r]
3104
    fconfigure $f -translation auto -eofchar \x1a
3105
    set l [string length [read $f]]
3106
    set e [eof $f]
3107
    close $f
3108
    list $c $l $e
3109
} {21 8 1}
3110
test io-12.17 {Tcl_Eof, eof char in middle, crlf write, crlf read} {
3111
    removeFile test1
3112
    set f [open test1 w]
3113
    fconfigure $f -translation crlf -eofchar {}
3114
    set i [format abc\ndef\n%cqrs\nuvw 26]
3115
    puts $f $i
3116
    close $f
3117
    set c [file size test1]
3118
    set f [open test1 r]
3119
    fconfigure $f -translation crlf -eofchar \x1a
3120
    set l [string length [read $f]]
3121
    set e [eof $f]
3122
    close $f
3123
    list $c $l $e
3124
} {21 8 1}
3125
 
3126
# Test Tcl_InputBlocked
3127
 
3128
test io-13.1 {Tcl_InputBlocked on nonblocking pipe} {unixOrPc tempNotPc} {
3129
    set f1 [open "|[list $tcltest]" r+]
3130
    puts $f1 {puts hello_from_pipe}
3131
    flush $f1
3132
    gets $f1
3133
    fconfigure $f1 -blocking off -buffering full
3134
    puts $f1 {puts hello}
3135
    set x ""
3136
    lappend x [gets $f1]
3137
    lappend x [fblocked $f1]
3138
    flush $f1
3139
    after 200
3140
    lappend x [gets $f1]
3141
    lappend x [fblocked $f1]
3142
    lappend x [gets $f1]
3143
    lappend x [fblocked $f1]
3144
    close $f1
3145
    set x
3146
} {{} 1 hello 0 {} 1}
3147
test io-13.2 {Tcl_InputBlocked on blocking pipe} {unixOrPc tempNotPc} {
3148
    set f1 [open "|[list $tcltest]" r+]
3149
    fconfigure $f1 -buffering line
3150
    puts $f1 {puts hello_from_pipe}
3151
    set x ""
3152
    lappend x [gets $f1]
3153
    lappend x [fblocked $f1]
3154
    puts $f1 {exit}
3155
    lappend x [gets $f1]
3156
    lappend x [fblocked $f1]
3157
    lappend x [eof $f1]
3158
    close $f1
3159
    set x
3160
} {hello_from_pipe 0 {} 0 1}
3161
test io-13.3 {Tcl_InputBlocked vs files, short read} {
3162
    removeFile test1
3163
    set f [open test1 w]
3164
    puts $f abcdefghijklmnop
3165
    close $f
3166
    set f [open test1 r]
3167
    set l ""
3168
    lappend l [fblocked $f]
3169
    lappend l [read $f 3]
3170
    lappend l [fblocked $f]
3171
    lappend l [read -nonewline $f]
3172
    lappend l [fblocked $f]
3173
    lappend l [eof $f]
3174
    close $f
3175
    set l
3176
} {0 abc 0 defghijklmnop 0 1}
3177
test io-13.4 {Tcl_InputBlocked vs files, event driven read} {
3178
    proc in {f} {
3179
        global l x
3180
        lappend l [read $f 3]
3181
        if {[eof $f]} {lappend l eof; close $f; set x done}
3182
    }
3183
    removeFile test1
3184
    set f [open test1 w]
3185
    puts $f abcdefghijklmnop
3186
    close $f
3187
    set f [open test1 r]
3188
    set l ""
3189
    fileevent $f readable [list in $f]
3190
    vwait x
3191
    set l
3192
} {abc def ghi jkl mno {p
3193
} eof}
3194
test io-13.5 {Tcl_InputBlocked vs files, short read, nonblocking} {nonBlockFiles} {
3195
    removeFile test1
3196
    set f [open test1 w]
3197
    puts $f abcdefghijklmnop
3198
    close $f
3199
    set f [open test1 r]
3200
    fconfigure $f -blocking off
3201
    set l ""
3202
    lappend l [fblocked $f]
3203
    lappend l [read $f 3]
3204
    lappend l [fblocked $f]
3205
    lappend l [read -nonewline $f]
3206
    lappend l [fblocked $f]
3207
    lappend l [eof $f]
3208
    close $f
3209
    set l
3210
} {0 abc 0 defghijklmnop 0 1}
3211
test io-13.6 {Tcl_InputBlocked vs files, event driven read} {nonBlockFiles} {
3212
    proc in {f} {
3213
        global l x
3214
        lappend l [read $f 3]
3215
        if {[eof $f]} {lappend l eof; close $f; set x done}
3216
    }
3217
    removeFile test1
3218
    set f [open test1 w]
3219
    puts $f abcdefghijklmnop
3220
    close $f
3221
    set f [open test1 r]
3222
    fconfigure $f -blocking off
3223
    set l ""
3224
    fileevent $f readable [list in $f]
3225
    vwait x
3226
    set l
3227
} {abc def ghi jkl mno {p
3228
} eof}
3229
 
3230
# Test Tcl_InputBuffered
3231
 
3232
test io-14.1 {Tcl_InputBuffered} {
3233
    set f [open longfile r]
3234
    fconfigure $f -buffersize 4096
3235
    read $f 3
3236
    set l ""
3237
    lappend l [testchannel inputbuffered $f]
3238
    lappend l [tell $f]
3239
    close $f
3240
    set l
3241
} {4093 3}
3242
test io-14.2 {Tcl_InputBuffered, test input flushing on seek} {
3243
    set f [open longfile r]
3244
    fconfigure $f -buffersize 4096
3245
    read $f 3
3246
    set l ""
3247
    lappend l [testchannel inputbuffered $f]
3248
    lappend l [tell $f]
3249
    seek $f 0 current
3250
    lappend l [testchannel inputbuffered $f]
3251
    lappend l [tell $f]
3252
    close $f
3253
    set l
3254
} {4093 3 0 3}
3255
 
3256
# Test Tcl_SetChannelBufferSize, Tcl_GetChannelBufferSize
3257
 
3258
test io-15.1 {Tcl_GetChannelBufferSize, default buffer size} {
3259
    set f [open longfile r]
3260
    set s [fconfigure $f -buffersize]
3261
    close $f
3262
    set s
3263
} 4096
3264
test io-15.2 {Tcl_SetChannelBufferSize, Tcl_GetChannelBufferSize} {
3265
    set f [open longfile r]
3266
    set l ""
3267
    lappend l [fconfigure $f -buffersize]
3268
    fconfigure $f -buffersize 10000
3269
    lappend l [fconfigure $f -buffersize]
3270
    fconfigure $f -buffersize 1
3271
    lappend l [fconfigure $f -buffersize]
3272
    fconfigure $f -buffersize -1
3273
    lappend l [fconfigure $f -buffersize]
3274
    fconfigure $f -buffersize 0
3275
    lappend l [fconfigure $f -buffersize]
3276
    fconfigure $f -buffersize 100000
3277
    lappend l [fconfigure $f -buffersize]
3278
    fconfigure $f -buffersize 10000000
3279
    lappend l [fconfigure $f -buffersize]
3280
    close $f
3281
    set l
3282
} {4096 10000 4096 4096 4096 100000 4096}
3283
 
3284
# Test Tcl_SetChannelOption, Tcl_GetChannelOption
3285
 
3286
test io-16.1 {Tcl_GetChannelOption} {
3287
    removeFile test1
3288
    set f1 [open test1 w]
3289
    set x [fconfigure $f1 -blocking]
3290
    close $f1
3291
    set x
3292
} 1
3293
#
3294
# Test 17.2 was removed.
3295
#
3296
test io-16.2 {Tcl_GetChannelOption} {
3297
    removeFile test1
3298
    set f1 [open test1 w]
3299
    set x [fconfigure $f1 -buffering]
3300
    close $f1
3301
    set x
3302
} full
3303
test io-16.3 {Tcl_GetChannelOption} {
3304
    removeFile test1
3305
    set f1 [open test1 w]
3306
    fconfigure $f1 -buffering line
3307
    set x [fconfigure $f1 -buffering]
3308
    close $f1
3309
    set x
3310
} line
3311
test io-16.4 {Tcl_GetChannelOption, Tcl_SetChannelOption} {
3312
    removeFile test1
3313
    set f1 [open test1 w]
3314
    set l ""
3315
    lappend l [fconfigure $f1 -buffering]
3316
    fconfigure $f1 -buffering line
3317
    lappend l [fconfigure $f1 -buffering]
3318
    fconfigure $f1 -buffering none
3319
    lappend l [fconfigure $f1 -buffering]
3320
    fconfigure $f1 -buffering line
3321
    lappend l [fconfigure $f1 -buffering]
3322
    fconfigure $f1 -buffering full
3323
    lappend l [fconfigure $f1 -buffering]
3324
    close $f1
3325
    set l
3326
} {full line none line full}
3327
test io-16.5 {Tcl_GetChannelOption, invariance} {
3328
    removeFile test1
3329
    set f1 [open test1 w]
3330
    set l ""
3331
    lappend l [fconfigure $f1 -buffering]
3332
    lappend l [list [catch {fconfigure $f1 -buffering green} msg] $msg]
3333
    lappend l [fconfigure $f1 -buffering]
3334
    close $f1
3335
    set l
3336
} {full {1 {bad value for -buffering: must be one of full, line, or none}} full}
3337
test io-16.6 {Tcl_SetChannelOption, multiple options} {
3338
    removeFile test1
3339
    set f1 [open test1 w]
3340
    fconfigure $f1 -translation lf -buffering line
3341
    puts $f1 hello
3342
    puts $f1 bye
3343
    set x [file size test1]
3344
    close $f1
3345
    set x
3346
} 10
3347
test io-16.7 {Tcl_SetChannelOption, buffering, translation} {
3348
    removeFile test1
3349
    set f1 [open test1 w]
3350
    fconfigure $f1 -translation lf
3351
    puts $f1 hello
3352
    puts $f1 bye
3353
    set x ""
3354
    fconfigure $f1 -buffering line
3355
    lappend x [file size test1]
3356
    puts $f1 really_bye
3357
    lappend x [file size test1]
3358
    close $f1
3359
    set x
3360
} {0 21}
3361
test io-16.8 {Tcl_SetChannelOption, different buffering options} {
3362
    removeFile test1
3363
    set f1 [open test1 w]
3364
    set l ""
3365
    fconfigure $f1 -translation lf -buffering none -eofchar {}
3366
    puts -nonewline $f1 hello
3367
    lappend l [file size test1]
3368
    puts -nonewline $f1 hello
3369
    lappend l [file size test1]
3370
    fconfigure $f1 -buffering full
3371
    puts -nonewline $f1 hello
3372
    lappend l [file size test1]
3373
    fconfigure $f1 -buffering none
3374
    lappend l [file size test1]
3375
    puts -nonewline $f1 hello
3376
    lappend l [file size test1]
3377
    close $f1
3378
    lappend l [file size test1]
3379
    set l
3380
} {5 10 10 10 20 20}
3381
test io-16.9 {Tcl_SetChannelOption, blocking mode} {nonBlockFiles} {
3382
    removeFile test1
3383
    set f1 [open test1 w]
3384
    close $f1
3385
    set f1 [open test1 r]
3386
    set x ""
3387
    lappend x [fconfigure $f1 -blocking]
3388
    fconfigure $f1 -blocking off
3389
    lappend x [fconfigure $f1 -blocking]
3390
    lappend x [gets $f1]
3391
    lappend x [read $f1 1000]
3392
    lappend x [fblocked $f1]
3393
    lappend x [eof $f1]
3394
    close $f1
3395
    set x
3396
} {1 0 {} {} 0 1}
3397
test io-16.10 {Tcl_SetChannelOption, blocking mode} {unixOrPc tempNotPc} {
3398
    removeFile pipe
3399
    set f1 [open pipe w]
3400
    puts $f1 {gets stdin}
3401
    puts $f1 {after 100}
3402
    puts $f1 {puts hi}
3403
    puts $f1 {gets stdin}
3404
    close $f1
3405
    set x ""
3406
    set f1 [open "|[list $tcltest pipe]" r+]
3407
    fconfigure $f1 -blocking off -buffering line
3408
    lappend x [fconfigure $f1 -blocking]
3409
    lappend x [gets $f1]
3410
    lappend x [fblocked $f1]
3411
    puts $f1 hello
3412
    lappend x [gets $f1]
3413
    lappend x [fblocked $f1]
3414
    puts $f1 bye
3415
    lappend x [gets $f1]
3416
    lappend x [fblocked $f1]
3417
    fconfigure $f1 -blocking on
3418
    lappend x [fconfigure $f1 -blocking]
3419
    lappend x [gets $f1]
3420
    lappend x [fblocked $f1]
3421
    lappend x [eof $f1]
3422
    lappend x [gets $f1]
3423
    lappend x [eof $f1]
3424
    close $f1
3425
    set x
3426
} {0 {} 1 {} 1 {} 1 1 hi 0 0 {} 1}
3427
test io-16.11 {Tcl_SetChannelOption, Tcl_GetChannelOption, buffer size} {
3428
    removeFile test1
3429
    set f [open test1 w]
3430
    fconfigure $f -buffersize -10
3431
    set x [fconfigure $f -buffersize]
3432
    close $f
3433
    set x
3434
} 4096
3435
test io-16.12 {Tcl_SetChannelOption, Tcl_GetChannelOption buffer size} {
3436
    removeFile test1
3437
    set f [open test1 w]
3438
    fconfigure $f -buffersize 10000000
3439
    set x [fconfigure $f -buffersize]
3440
    close $f
3441
    set x
3442
} 4096
3443
test io-16.13 {Tcl_SetChannelOption, Tcl_GetChannelOption, buffer size} {
3444
    removeFile test1
3445
    set f [open test1 w]
3446
    fconfigure $f -buffersize 40000
3447
    set x [fconfigure $f -buffersize]
3448
    close $f
3449
    set x
3450
} 40000
3451
test io-16.14 {Tcl_SetChannelOption, setting read mode independently} \
3452
        {socket} {
3453
    proc accept {s a p} {close $s}
3454
    set s1 [socket -server accept 0]
3455
    set port [lindex [fconfigure $s1 -sockname] 2]
3456
    set s2 [socket localhost $port]
3457
    update
3458
    fconfigure $s2 -translation {auto lf}
3459
    set modes [fconfigure $s2 -translation]
3460
    close $s1
3461
    close $s2
3462
    set modes
3463
} {auto lf}
3464
test io-16.15 {Tcl_SetChannelOption, setting read mode independently} \
3465
        {socket} {
3466
    proc accept {s a p} {close $s}
3467
    set s1 [socket -server accept 0]
3468
    set port [lindex [fconfigure $s1 -sockname] 2]
3469
    set s2 [socket localhost $port]
3470
    update
3471
    fconfigure $s2 -translation {auto crlf}
3472
    set modes [fconfigure $s2 -translation]
3473
    close $s1
3474
    close $s2
3475
    set modes
3476
} {auto crlf}
3477
test io-16.16 {Tcl_SetChannelOption, setting read mode independently} \
3478
        {socket} {
3479
    proc accept {s a p} {close $s}
3480
    set s1 [socket -server accept 0]
3481
    set port [lindex [fconfigure $s1 -sockname] 2]
3482
    set s2 [socket localhost $port]
3483
    update
3484
    fconfigure $s2 -translation {auto cr}
3485
    set modes [fconfigure $s2 -translation]
3486
    close $s1
3487
    close $s2
3488
    set modes
3489
} {auto cr}
3490
test io-16.17 {Tcl_SetChannelOption, setting read mode independently} \
3491
        {socket} {
3492
    proc accept {s a p} {close $s}
3493
    set s1 [socket -server accept 0]
3494
    set port [lindex [fconfigure $s1 -sockname] 2]
3495
    set s2 [socket localhost $port]
3496
    update
3497
    fconfigure $s2 -translation {auto auto}
3498
    set modes [fconfigure $s2 -translation]
3499
    close $s1
3500
    close $s2
3501
    set modes
3502
} {auto crlf}
3503
 
3504
test io-17.1 {POSIX open access modes: RDWR} {
3505
    removeFile test3
3506
    set f [open test3 w]
3507
    puts $f xyzzy
3508
    close $f
3509
    set f [open test3 RDWR]
3510
    puts -nonewline $f "ab"
3511
    seek $f 0 current
3512
    set x [gets $f]
3513
    close $f
3514
    set f [open test3 r]
3515
    lappend x [gets $f]
3516
    close $f
3517
    set x
3518
} {zzy abzzy}
3519
test io-17.2 {POSIX open access modes: CREAT} {unixOnly} {
3520
    removeFile test3
3521
    set f [open test3 {WRONLY CREAT} 0600]
3522
    file stat test3 stats
3523
    set x [format "0%o" [expr $stats(mode)&0777]]
3524
    puts $f "line 1"
3525
    close $f
3526
    set f [open test3 r]
3527
    lappend x [gets $f]
3528
    close $f
3529
    set x
3530
} {0600 {line 1}}
3531
test io-17.3 {POSIX open access modes: CREAT} {$testConfig(unix) && ([exec umask] == 2)} {
3532
    # This test only works if your umask is 2, like ouster's.
3533
    removeFile test3
3534
    set f [open test3 {WRONLY CREAT}]
3535
    close $f
3536
    file stat test3 stats
3537
    format "0%o" [expr $stats(mode)&0777]
3538
} 0664
3539
test io-17.4 {POSIX open access modes: CREAT} {
3540
    removeFile test3
3541
    set f [open test3 w]
3542
    fconfigure $f -eofchar {}
3543
    puts $f xyzzy
3544
    close $f
3545
    set f [open test3 {WRONLY CREAT}]
3546
    fconfigure $f -eofchar {}
3547
    puts -nonewline $f "ab"
3548
    close $f
3549
    set f [open test3 r]
3550
    set x [gets $f]
3551
    close $f
3552
    set x
3553
} abzzy
3554
test io-17.5 {POSIX open access modes: APPEND} {
3555
    removeFile test3
3556
    set f [open test3 w]
3557
    fconfigure $f -translation lf -eofchar {}
3558
    puts $f xyzzy
3559
    close $f
3560
    set f [open test3 {WRONLY APPEND}]
3561
    fconfigure $f -translation lf
3562
    puts $f "new line"
3563
    seek $f 0
3564
    puts $f "abc"
3565
    close $f
3566
    set f [open test3 r]
3567
    fconfigure $f -translation lf
3568
    set x ""
3569
    seek $f 6 current
3570
    lappend x [gets $f]
3571
    lappend x [gets $f]
3572
    close $f
3573
    set x
3574
} {{new line} abc}
3575
test io-17.6 {POSIX open access modes: EXCL} {
3576
    removeFile test3
3577
    set f [open test3 w]
3578
    puts $f xyzzy
3579
    close $f
3580
    set msg [list [catch {open test3 {WRONLY CREAT EXCL}} msg] $msg]
3581
    regsub " already " $msg " " msg
3582
    string tolower $msg
3583
} {1 {couldn't open "test3": file exists}}
3584
test io-17.7 {POSIX open access modes: EXCL} {
3585
    removeFile test3
3586
    set f [open test3 {WRONLY CREAT EXCL}]
3587
    fconfigure $f -eofchar {}
3588
    puts $f "A test line"
3589
    close $f
3590
    viewFile test3
3591
} {A test line}
3592
test io-17.8 {POSIX open access modes: TRUNC} {
3593
    removeFile test3
3594
    set f [open test3 w]
3595
    puts $f xyzzy
3596
    close $f
3597
    set f [open test3 {WRONLY TRUNC}]
3598
    puts $f abc
3599
    close $f
3600
    set f [open test3 r]
3601
    set x [gets $f]
3602
    close $f
3603
    set x
3604
} abc
3605
test io-17.9 {POSIX open access modes: NONBLOCK} {nonPortable macOrUnix} {
3606
    removeFile test3
3607
    set f [open test3 {WRONLY NONBLOCK CREAT}]
3608
    puts $f "NONBLOCK test"
3609
    close $f
3610
    set f [open test3 r]
3611
    set x [gets $f]
3612
    close $f
3613
    set x
3614
} {NONBLOCK test}
3615
test io-17.10 {POSIX open access modes: RDONLY} {
3616
    set f [open test1 w]
3617
    puts $f "two lines: this one"
3618
    puts $f "and this"
3619
    close $f
3620
    set f [open test1 RDONLY]
3621
    set x [list [gets $f] [catch {puts $f Test} msg] $msg]
3622
    close $f
3623
    string compare [string tolower $x] \
3624
        [list {two lines: this one} 1 \
3625
                [format "channel \"%s\" wasn't opened for writing" $f]]
3626
} 0
3627
test io-17.11 {POSIX open access modes: RDONLY} {
3628
    removeFile test3
3629
    string tolower [list [catch {open test3 RDONLY} msg] $msg]
3630
} {1 {couldn't open "test3": no such file or directory}}
3631
test io-17.12 {POSIX open access modes: WRONLY} {
3632
    removeFile test3
3633
    string tolower [list [catch {open test3 WRONLY} msg] $msg]
3634
} {1 {couldn't open "test3": no such file or directory}}
3635
test io-17.13 {POSIX open access modes: WRONLY} {
3636
    makeFile xyzzy test3
3637
    set f [open test3 WRONLY]
3638
    fconfigure $f -eofchar {}
3639
    puts -nonewline $f "ab"
3640
    seek $f 0 current
3641
    set x [list [catch {gets $f} msg] $msg]
3642
    close $f
3643
    lappend x [viewFile test3]
3644
    string compare [string tolower $x] \
3645
        [list 1 "channel \"$f\" wasn't opened for reading" abzzy]
3646
} 0
3647
test io-17.14 {POSIX open access modes: RDWR} {
3648
    removeFile test3
3649
    string tolower [list [catch {open test3 RDWR} msg] $msg]
3650
} {1 {couldn't open "test3": no such file or directory}}
3651
test io-17.15 {POSIX open access modes: RDWR} {
3652
    makeFile xyzzy test3
3653
    set f [open test3 RDWR]
3654
    puts -nonewline $f "ab"
3655
    seek $f 0 current
3656
    set x [gets $f]
3657
    close $f
3658
    lappend x [viewFile test3]
3659
} {zzy abzzy}
3660
if {![file exists ~/_test_] && [file writable ~]} {
3661
    test io-17.16 {tilde substitution in open} {
3662
        set f [open ~/_test_ w]
3663
        puts $f "Some text"
3664
        close $f
3665
        set x [file exists [file join $env(HOME) _test_]]
3666
        removeFile [file join $env(HOME) _test_]
3667
        set x
3668
    } 1
3669
}
3670
test io-17.17 {tilde substitution in open} {
3671
    set home $env(HOME)
3672
    unset env(HOME)
3673
    set x [list [catch {open ~/foo} msg] $msg]
3674
    set env(HOME) $home
3675
    set x
3676
} {1 {couldn't find HOME environment variable to expand path}}
3677
 
3678
test io-18.1 {Tcl_FileeventCmd: errors} {
3679
    list [catch {fileevent foo} msg] $msg
3680
} {1 {wrong # args: must be "fileevent channelId event ?script?}}
3681
test io-18.2 {Tcl_FileeventCmd: errors} {
3682
    list [catch {fileevent foo bar baz q} msg] $msg
3683
} {1 {wrong # args: must be "fileevent channelId event ?script?}}
3684
test io-18.3 {Tcl_FileeventCmd: errors} {
3685
    list [catch {fileevent gorp readable} msg] $msg
3686
} {1 {can not find channel named "gorp"}}
3687
test io-18.4 {Tcl_FileeventCmd: errors} {
3688
    list [catch {fileevent gorp writable} msg] $msg
3689
} {1 {can not find channel named "gorp"}}
3690
test io-18.5 {Tcl_FileeventCmd: errors} {
3691
    list [catch {fileevent gorp who-knows} msg] $msg
3692
} {1 {bad event name "who-knows": must be readable or writable}}
3693
 
3694
#
3695
# Test fileevent on a file
3696
#
3697
 
3698
set f [open foo w+]
3699
 
3700
test io-19.1 {Tcl_FileeventCmd: creating, deleting, querying} {
3701
    list [fileevent $f readable] [fileevent $f writable]
3702
} {{} {}}
3703
test io-19.2 {Tcl_FileeventCmd: replacing} {
3704
    set result {}
3705
    fileevent $f r "first script"
3706
    lappend result [fileevent $f readable]
3707
    fileevent $f r "new script"
3708
    lappend result [fileevent $f readable]
3709
    fileevent $f r "yet another"
3710
    lappend result [fileevent $f readable]
3711
    fileevent $f r ""
3712
    lappend result [fileevent $f readable]
3713
} {{first script} {new script} {yet another} {}}
3714
 
3715
#
3716
# Test fileevent on a pipe
3717
#
3718
 
3719
if {($tcl_platform(platform) != "macintosh") && \
3720
        ($testConfig(unixExecs) == 1)} {
3721
 
3722
catch {set f2 [open "|[list cat -u]" r+]}
3723
catch {set f3 [open "|[list cat -u]" r+]}
3724
 
3725
test io-20.1 {Tcl_FileeventCmd: creating, deleting, querying} {
3726
    set result {}
3727
    fileevent $f readable "script 1"
3728
    lappend result [fileevent $f readable] [fileevent $f writable]
3729
    fileevent $f writable "write script"
3730
    lappend result [fileevent $f readable] [fileevent $f writable]
3731
    fileevent $f readable {}
3732
    lappend result [fileevent $f readable] [fileevent $f writable]
3733
    fileevent $f writable {}
3734
    lappend result [fileevent $f readable] [fileevent $f writable]
3735
} {{script 1} {} {script 1} {write script} {} {write script} {} {}}
3736
test io-20.2 {Tcl_FileeventCmd: deleting when many present} {
3737
    set result {}
3738
    lappend result [fileevent $f r] [fileevent $f2 r] [fileevent $f3 r]
3739
    fileevent $f r "read f"
3740
    fileevent $f2 r "read f2"
3741
    fileevent $f3 r "read f3"
3742
    lappend result [fileevent $f r] [fileevent $f2 r] [fileevent $f3 r]
3743
    fileevent $f2 r {}
3744
    lappend result [fileevent $f r] [fileevent $f2 r] [fileevent $f3 r]
3745
    fileevent $f3 r {}
3746
    lappend result [fileevent $f r] [fileevent $f2 r] [fileevent $f3 r]
3747
    fileevent $f r {}
3748
    lappend result [fileevent $f r] [fileevent $f2 r] [fileevent $f3 r]
3749
} {{} {} {} {read f} {read f2} {read f3} {read f} {} {read f3} {read f} {} {} {} {} {}}
3750
 
3751
test io-21.1 {FileEventProc procedure: normal read event} {
3752
    fileevent $f2 readable {
3753
        set x [gets $f2]; fileevent $f2 readable {}
3754
    }
3755
    puts $f2 text; flush $f2
3756
    set x initial
3757
    vwait x
3758
    set x
3759
} {text}
3760
test io-21.2 {FileEventProc procedure: error in read event} {
3761
    proc bgerror args {
3762
        global x
3763
        set x $args
3764
    }
3765
    fileevent $f2 readable {error bogus}
3766
    puts $f2 text; flush $f2
3767
    set x initial
3768
    vwait x
3769
    rename bgerror {}
3770
    list $x [fileevent $f2 readable]
3771
} {bogus {}}
3772
test io-21.3 {FileEventProc procedure: normal write event} {
3773
    fileevent $f2 writable {
3774
        lappend x "triggered"
3775
        incr count -1
3776
        if {$count <= 0} {
3777
            fileevent $f2 writable {}
3778
        }
3779
    }
3780
    set x initial
3781
    set count 3
3782
    vwait x
3783
    vwait x
3784
    vwait x
3785
    set x
3786
} {initial triggered triggered triggered}
3787
test io-21.4 {FileEventProc procedure: eror in write event} {
3788
    proc bgerror args {
3789
        global x
3790
        set x $args
3791
    }
3792
    fileevent $f2 writable {error bad-write}
3793
    set x initial
3794
    vwait x
3795
    rename bgerror {}
3796
    list $x [fileevent $f2 writable]
3797
} {bad-write {}}
3798
test io-21.5 {FileEventProc procedure: end of file} {unixOrPc} {
3799
    set f4 [open "|[list $tcltest cat << foo]" r]
3800
    fileevent $f4 readable {
3801
        if {[gets $f4 line] < 0} {
3802
            lappend x eof
3803
            fileevent $f4 readable {}
3804
        } else {
3805
            lappend x $line
3806
        }
3807
    }
3808
    set x initial
3809
    vwait x
3810
    vwait x
3811
    close $f4
3812
    set x
3813
} {initial foo eof}
3814
 
3815
catch {close $f2}
3816
catch {close $f3}
3817
 
3818
}
3819
        # Closes if {($platform(platform) != "macintosh") && \
3820
        #               ($testConfig(unixExecs) == 1)} clause
3821
 
3822
close $f
3823
makeFile "foo bar" foo
3824
test io-22.1 {DeleteFileEvent, cleanup on close} {
3825
    set f [open foo r]
3826
    fileevent $f readable {
3827
        lappend x "binding triggered: \"[gets $f]\""
3828
        fileevent $f readable {}
3829
    }
3830
    close $f
3831
    set x initial
3832
    after 100 { set y done }
3833
    vwait y
3834
    set x
3835
} {initial}
3836
test io-22.2 {DeleteFileEvent, cleanup on close} {
3837
    set f [open foo r]
3838
    set f2 [open foo r]
3839
    fileevent $f readable {
3840
            lappend x "f triggered: \"[gets $f]\""
3841
            fileevent $f readable {}
3842
        }
3843
    fileevent $f2 readable {
3844
        lappend x "f2 triggered: \"[gets $f2]\""
3845
        fileevent $f2 readable {}
3846
    }
3847
    close $f
3848
    set x initial
3849
    vwait x
3850
    close $f2
3851
    set x
3852
} {initial {f2 triggered: "foo bar"}}
3853
test io-22.3 {DeleteFileEvent, cleanup on close} {
3854
    set f [open foo r]
3855
    set f2 [open foo r]
3856
    set f3 [open foo r]
3857
    fileevent $f readable {f script}
3858
    fileevent $f2 readable {f2 script}
3859
    fileevent $f3 readable {f3 script}
3860
    set x {}
3861
    close $f2
3862
    lappend x [catch {fileevent $f readable} msg] $msg \
3863
            [catch {fileevent $f2 readable}] \
3864
            [catch {fileevent $f3 readable} msg] $msg
3865
    close $f3
3866
    lappend x [catch {fileevent $f readable} msg] $msg \
3867
            [catch {fileevent $f2 readable}] \
3868
            [catch {fileevent $f3 readable}]
3869
    close $f
3870
    lappend x [catch {fileevent $f readable}] \
3871
            [catch {fileevent $f2 readable}] \
3872
            [catch {fileevent $f3 readable}]
3873
} {0 {f script} 1 0 {f3 script} 0 {f script} 1 1 1 1 1}
3874
 
3875
# Execute these tests only if the "testfevent" command is present.
3876
 
3877
if {[info commands testfevent] == "testfevent"} {
3878
 
3879
test io-23.1 {Tcl event loop vs multiple interpreters} {
3880
    testfevent create
3881
    testfevent cmd {
3882
        set f [open foo r]
3883
        set x "no event"
3884
        fileevent $f readable {
3885
            set x "f triggered: [gets $f]"
3886
            fileevent $f readable {}
3887
        }
3888
    }
3889
    after 1     ;# We must delay because Windows takes a little time to notice
3890
    update
3891
    testfevent cmd {close $f}
3892
    list [testfevent cmd {set x}] [testfevent cmd {info commands after}]
3893
} {{f triggered: foo bar} after}
3894
test io-23.2 {Tcl event loop vs multiple interpreters} {
3895
    testfevent create
3896
    testfevent cmd {
3897
        set x 0
3898
        after 100 {set x triggered}
3899
        vwait x
3900
        set x
3901
    }
3902
} {triggered}
3903
test io-23.3 {Tcl event loop vs multiple interpreters} {
3904
    testfevent create
3905
    testfevent cmd {
3906
        set x 0
3907
        after 10 {lappend x timer}
3908
        after 30
3909
        set result $x
3910
        update idletasks
3911
        lappend result $x
3912
        update
3913
        lappend result $x
3914
    }
3915
} {0 0 {0 timer}}
3916
 
3917
test io-24.1 {fileevent vs multiple interpreters} {
3918
    set f [open foo r]
3919
    set f2 [open foo r]
3920
    set f3 [open foo r]
3921
    fileevent $f readable {script 1}
3922
    testfevent create
3923
    testfevent share $f2
3924
    testfevent cmd "fileevent $f2 readable {script 2}"
3925
    fileevent $f3 readable {sript 3}
3926
    set x {}
3927
    lappend x [fileevent $f2 readable]
3928
    testfevent delete
3929
    lappend x [fileevent $f readable] [fileevent $f2 readable] \
3930
        [fileevent $f3 readable]
3931
    close $f
3932
    close $f2
3933
    close $f3
3934
    set x
3935
} {{} {script 1} {} {sript 3}}
3936
test io-24.2 {deleting fileevent on interpreter delete} {
3937
    set f [open foo r]
3938
    set f2 [open foo r]
3939
    set f3 [open foo r]
3940
    set f4 [open foo r]
3941
    fileevent $f readable {script 1}
3942
    testfevent create
3943
    testfevent share $f2
3944
    testfevent share $f3
3945
    testfevent cmd "fileevent $f2 readable {script 2}
3946
        fileevent $f3 readable {script 3}"
3947
    fileevent $f4 readable {script 4}
3948
    testfevent delete
3949
    set x [list [fileevent $f readable] [fileevent $f2 readable] \
3950
                [fileevent $f3 readable] [fileevent $f4 readable]]
3951
    close $f
3952
    close $f2
3953
    close $f3
3954
    close $f4
3955
    set x
3956
} {{script 1} {} {} {script 4}}
3957
test io-24.3 {deleting fileevent on interpreter delete} {
3958
    set f [open foo r]
3959
    set f2 [open foo r]
3960
    set f3 [open foo r]
3961
    set f4 [open foo r]
3962
    testfevent create
3963
    testfevent share $f3
3964
    testfevent share $f4
3965
    fileevent $f readable {script 1}
3966
    fileevent $f2 readable {script 2}
3967
    testfevent cmd "fileevent $f3 readable {script 3}
3968
      fileevent $f4 readable {script 4}"
3969
    testfevent delete
3970
    set x [list [fileevent $f readable] [fileevent $f2 readable] \
3971
                [fileevent $f3 readable] [fileevent $f4 readable]]
3972
    close $f
3973
    close $f2
3974
    close $f3
3975
    close $f4
3976
    set x
3977
} {{script 1} {script 2} {} {}}
3978
test io-24.4 {file events on shared files and multiple interpreters} {
3979
    set f [open foo r]
3980
    set f2 [open foo r]
3981
    testfevent create
3982
    testfevent share $f
3983
    testfevent cmd "fileevent $f readable {script 1}"
3984
    fileevent $f readable {script 2}
3985
    fileevent $f2 readable {script 3}
3986
    set x [list [fileevent $f2 readable] \
3987
                [testfevent cmd "fileevent $f readable"] \
3988
                [fileevent $f readable]]
3989
    testfevent delete
3990
    close $f
3991
    close $f2
3992
    set x
3993
} {{script 3} {script 1} {script 2}}
3994
test io-24.5 {file events on shared files, deleting file events} {
3995
    set f [open foo r]
3996
    testfevent create
3997
    testfevent share $f
3998
    testfevent cmd "fileevent $f readable {script 1}"
3999
    fileevent $f readable {script 2}
4000
    testfevent cmd "fileevent $f readable {}"
4001
    set x [list [testfevent cmd "fileevent $f readable"] \
4002
                [fileevent $f readable]]
4003
    testfevent delete
4004
    close $f
4005
    set x
4006
} {{} {script 2}}
4007
test io-24.6 {file events on shared files, deleting file events} {
4008
    set f [open foo r]
4009
    testfevent create
4010
    testfevent share $f
4011
    testfevent cmd "fileevent $f readable {script 1}"
4012
    fileevent $f readable {script 2}
4013
    fileevent $f readable {}
4014
    set x [list [testfevent cmd "fileevent $f readable"] \
4015
                [fileevent $f readable]]
4016
    testfevent delete
4017
    close $f
4018
    set x
4019
} {{script 1} {}}
4020
 
4021
}
4022
 
4023
# The above curly closes the test for presence of the "testfevent" command.
4024
 
4025
test io-25.1 {testing readability conditions} {
4026
    set f [open bar w]
4027
    puts $f abcdefg
4028
    puts $f abcdefg
4029
    puts $f abcdefg
4030
    puts $f abcdefg
4031
    puts $f abcdefg
4032
    close $f
4033
    set f [open bar r]
4034
    fileevent $f readable [list consume $f]
4035
    proc consume {f} {
4036
        global x l
4037
        lappend l called
4038
        if {[eof $f]} {
4039
            close $f
4040
            set x done
4041
        } else {
4042
            gets $f
4043
        }
4044
    }
4045
    set l ""
4046
    set x not_done
4047
    vwait x
4048
    list $x $l
4049
} {done {called called called called called called called}}
4050
test io-25.2 {testing readability conditions} {nonBlockFiles} {
4051
    set f [open bar w]
4052
    puts $f abcdefg
4053
    puts $f abcdefg
4054
    puts $f abcdefg
4055
    puts $f abcdefg
4056
    puts $f abcdefg
4057
    close $f
4058
    set f [open bar r]
4059
    fileevent $f readable [list consume $f]
4060
    fconfigure $f -blocking off
4061
    proc consume {f} {
4062
        global x l
4063
        lappend l called
4064
        if {[eof $f]} {
4065
            close $f
4066
            set x done
4067
        } else {
4068
            gets $f
4069
        }
4070
    }
4071
    set l ""
4072
    set x not_done
4073
    vwait x
4074
    list $x $l
4075
} {done {called called called called called called called}}
4076
test io-25.3 {testing readability conditions} {unixOnly nonBlockFiles} {
4077
    set f [open bar w]
4078
    puts $f abcdefg
4079
    puts $f abcdefg
4080
    puts $f abcdefg
4081
    puts $f abcdefg
4082
    puts $f abcdefg
4083
    close $f
4084
    set f [open my_script w]
4085
    puts $f {
4086
        proc copy_slowly {f} {
4087
            while {![eof $f]} {
4088
                puts [gets $f]
4089
                after 200
4090
            }
4091
            close $f
4092
        }
4093
    }
4094
    close $f
4095
    set f [open "|[list $tcltest]" r+]
4096
    fileevent $f readable [list consume $f]
4097
    fconfigure $f -buffering line
4098
    fconfigure $f -blocking off
4099
    proc consume {f} {
4100
        global x l
4101
        if {[eof $f]} {
4102
            set x done
4103
        } else {
4104
            gets $f
4105
            lappend l [fblocked $f]
4106
            gets $f
4107
            lappend l [fblocked $f]
4108
        }
4109
    }
4110
    set l ""
4111
    set x not_done
4112
    puts $f {source my_script}
4113
    puts $f {set f [open bar r]}
4114
    puts $f {copy_slowly $f}
4115
    puts $f {exit}
4116
    vwait x
4117
    close $f
4118
    list $x $l
4119
} {done {0 1 0 1 0 1 0 1 0 1 0 1 0 0}}
4120
test io-25.4 {lf write, testing readability, ^Z termination, auto read mode} {
4121
    removeFile test1
4122
    set f [open test1 w]
4123
    fconfigure $f -translation lf
4124
    set c [format "abc\ndef\n%c" 26]
4125
    puts -nonewline $f $c
4126
    close $f
4127
    proc consume {f} {
4128
        global c x l
4129
        if {[eof $f]} {
4130
           set x done
4131
           close $f
4132
        } else {
4133
           lappend l [gets $f]
4134
           incr c
4135
        }
4136
    }
4137
    set c 0
4138
    set l ""
4139
    set f [open test1 r]
4140
    fconfigure $f -translation auto -eofchar \x1a
4141
    fileevent $f readable [list consume $f]
4142
    vwait x
4143
    list $c $l
4144
} {3 {abc def {}}}
4145
test io-25.5 {lf write, testing readability, ^Z in middle, auto read mode} {
4146
    removeFile test1
4147
    set f [open test1 w]
4148
    fconfigure $f -translation lf
4149
    set c [format "abc\ndef\n%cfoo\nbar\n" 26]
4150
    puts -nonewline $f $c
4151
    close $f
4152
    proc consume {f} {
4153
        global c x l
4154
        if {[eof $f]} {
4155
           set x done
4156
           close $f
4157
        } else {
4158
           lappend l [gets $f]
4159
           incr c
4160
        }
4161
    }
4162
    set c 0
4163
    set l ""
4164
    set f [open test1 r]
4165
    fconfigure $f -eofchar \x1a -translation auto
4166
    fileevent $f readable [list consume $f]
4167
    vwait x
4168
    list $c $l
4169
} {3 {abc def {}}}
4170
test io-25.6 {cr write, testing readability, ^Z termination, auto read mode} {
4171
    removeFile test1
4172
    set f [open test1 w]
4173
    fconfigure $f -translation cr
4174
    set c [format "abc\ndef\n%c" 26]
4175
    puts -nonewline $f $c
4176
    close $f
4177
    proc consume {f} {
4178
        global c x l
4179
        if {[eof $f]} {
4180
           set x done
4181
           close $f
4182
        } else {
4183
           lappend l [gets $f]
4184
           incr c
4185
        }
4186
    }
4187
    set c 0
4188
    set l ""
4189
    set f [open test1 r]
4190
    fconfigure $f -translation auto -eofchar \x1a
4191
    fileevent $f readable [list consume $f]
4192
    vwait x
4193
    list $c $l
4194
} {3 {abc def {}}}
4195
test io-25.7 {cr write, testing readability, ^Z in middle, auto read mode} {
4196
    removeFile test1
4197
    set f [open test1 w]
4198
    fconfigure $f -translation cr
4199
    set c [format "abc\ndef\n%cfoo\nbar\n" 26]
4200
    puts -nonewline $f $c
4201
    close $f
4202
    proc consume {f} {
4203
        global c x l
4204
        if {[eof $f]} {
4205
           set x done
4206
           close $f
4207
        } else {
4208
           lappend l [gets $f]
4209
           incr c
4210
        }
4211
    }
4212
    set c 0
4213
    set l ""
4214
    set f [open test1 r]
4215
    fconfigure $f -eofchar \x1a -translation auto
4216
    fileevent $f readable [list consume $f]
4217
    vwait x
4218
    list $c $l
4219
} {3 {abc def {}}}
4220
test io-25.8 {crlf write, testing readability, ^Z termination, auto read mode} {
4221
    removeFile test1
4222
    set f [open test1 w]
4223
    fconfigure $f -translation crlf
4224
    set c [format "abc\ndef\n%c" 26]
4225
    puts -nonewline $f $c
4226
    close $f
4227
    proc consume {f} {
4228
        global c x l
4229
        if {[eof $f]} {
4230
           set x done
4231
           close $f
4232
        } else {
4233
           lappend l [gets $f]
4234
           incr c
4235
        }
4236
    }
4237
    set c 0
4238
    set l ""
4239
    set f [open test1 r]
4240
    fconfigure $f -translation auto -eofchar \x1a
4241
    fileevent $f readable [list consume $f]
4242
    vwait x
4243
    list $c $l
4244
} {3 {abc def {}}}
4245
test io-25.9 {crlf write, testing readability, ^Z in middle, auto read mode} {
4246
    removeFile test1
4247
    set f [open test1 w]
4248
    fconfigure $f -translation crlf
4249
    set c [format "abc\ndef\n%cfoo\nbar\n" 26]
4250
    puts -nonewline $f $c
4251
    close $f
4252
    proc consume {f} {
4253
        global c x l
4254
        if {[eof $f]} {
4255
           set x done
4256
           close $f
4257
        } else {
4258
           lappend l [gets $f]
4259
           incr c
4260
        }
4261
    }
4262
    set c 0
4263
    set l ""
4264
    set f [open test1 r]
4265
    fconfigure $f -eofchar \x1a -translation auto
4266
    fileevent $f readable [list consume $f]
4267
    vwait x
4268
    list $c $l
4269
} {3 {abc def {}}}
4270
test io-25.10 {lf write, testing readability, ^Z in middle, lf read mode} {
4271
    removeFile test1
4272
    set f [open test1 w]
4273
    fconfigure $f -translation lf
4274
    set c [format "abc\ndef\n%cfoo\nbar\n" 26]
4275
    puts -nonewline $f $c
4276
    close $f
4277
    proc consume {f} {
4278
        global c x l
4279
        if {[eof $f]} {
4280
           set x done
4281
           close $f
4282
        } else {
4283
           lappend l [gets $f]
4284
           incr c
4285
        }
4286
    }
4287
    set c 0
4288
    set l ""
4289
    set f [open test1 r]
4290
    fconfigure $f -eofchar \x1a -translation lf
4291
    fileevent $f readable [list consume $f]
4292
    vwait x
4293
    list $c $l
4294
} {3 {abc def {}}}
4295
test io-25.11 {lf write, testing readability, ^Z termination, lf read mode} {
4296
    removeFile test1
4297
    set f [open test1 w]
4298
    fconfigure $f -translation lf
4299
    set c [format "abc\ndef\n%c" 26]
4300
    puts -nonewline $f $c
4301
    close $f
4302
    proc consume {f} {
4303
        global c x l
4304
        if {[eof $f]} {
4305
           set x done
4306
           close $f
4307
        } else {
4308
           lappend l [gets $f]
4309
           incr c
4310
        }
4311
    }
4312
    set c 0
4313
    set l ""
4314
    set f [open test1 r]
4315
    fconfigure $f -translation lf -eofchar \x1a
4316
    fileevent $f readable [list consume $f]
4317
    vwait x
4318
    list $c $l
4319
} {3 {abc def {}}}
4320
test io-25.12 {cr write, testing readability, ^Z in middle, cr read mode} {
4321
    removeFile test1
4322
    set f [open test1 w]
4323
    fconfigure $f -translation cr
4324
    set c [format "abc\ndef\n%cfoo\nbar\n" 26]
4325
    puts -nonewline $f $c
4326
    close $f
4327
    proc consume {f} {
4328
        global c x l
4329
        if {[eof $f]} {
4330
           set x done
4331
           close $f
4332
        } else {
4333
           lappend l [gets $f]
4334
           incr c
4335
        }
4336
    }
4337
    set c 0
4338
    set l ""
4339
    set f [open test1 r]
4340
    fconfigure $f -eofchar \x1a -translation cr
4341
    fileevent $f readable [list consume $f]
4342
    vwait x
4343
    list $c $l
4344
} {3 {abc def {}}}
4345
test io-25.13 {cr write, testing readability, ^Z termination, cr read mode} {
4346
    removeFile test1
4347
    set f [open test1 w]
4348
    fconfigure $f -translation cr
4349
    set c [format "abc\ndef\n%c" 26]
4350
    puts -nonewline $f $c
4351
    close $f
4352
    proc consume {f} {
4353
        global c x l
4354
        if {[eof $f]} {
4355
           set x done
4356
           close $f
4357
        } else {
4358
           lappend l [gets $f]
4359
           incr c
4360
        }
4361
    }
4362
    set c 0
4363
    set l ""
4364
    set f [open test1 r]
4365
    fconfigure $f -translation cr -eofchar \x1a
4366
    fileevent $f readable [list consume $f]
4367
    vwait x
4368
    list $c $l
4369
} {3 {abc def {}}}
4370
test io-25.14 {crlf write, testing readability, ^Z in middle, crlf read mode} {
4371
    removeFile test1
4372
    set f [open test1 w]
4373
    fconfigure $f -translation crlf
4374
    set c [format "abc\ndef\n%cfoo\nbar\n" 26]
4375
    puts -nonewline $f $c
4376
    close $f
4377
    proc consume {f} {
4378
        global c x l
4379
        if {[eof $f]} {
4380
           set x done
4381
           close $f
4382
        } else {
4383
           lappend l [gets $f]
4384
           incr c
4385
        }
4386
    }
4387
    set c 0
4388
    set l ""
4389
    set f [open test1 r]
4390
    fconfigure $f -eofchar \x1a -translation crlf
4391
    fileevent $f readable [list consume $f]
4392
    vwait x
4393
    list $c $l
4394
} {3 {abc def {}}}
4395
test io-25.15 {crlf write, testing readability, ^Z termi, crlf read mode} {
4396
    removeFile test1
4397
    set f [open test1 w]
4398
    fconfigure $f -translation crlf
4399
    set c [format "abc\ndef\n%c" 26]
4400
    puts -nonewline $f $c
4401
    close $f
4402
    proc consume {f} {
4403
        global c x l
4404
        if {[eof $f]} {
4405
           set x done
4406
           close $f
4407
        } else {
4408
           lappend l [gets $f]
4409
           incr c
4410
        }
4411
    }
4412
    set c 0
4413
    set l ""
4414
    set f [open test1 r]
4415
    fconfigure $f -translation crlf -eofchar \x1a
4416
    fileevent $f readable [list consume $f]
4417
    vwait x
4418
    list $c $l
4419
} {3 {abc def {}}}
4420
 
4421
test io-26.1 {testing crlf reading, leftover cr disgorgment} {
4422
    removeFile test1
4423
    set f [open test1 w]
4424
    fconfigure $f -translation lf
4425
    puts -nonewline $f "a\rb\rc\r\n"
4426
    close $f
4427
    set f [open test1 r]
4428
    set l ""
4429
    lappend l [file size test1]
4430
    fconfigure $f -translation crlf
4431
    lappend l [read $f 1]
4432
    lappend l [tell $f]
4433
    lappend l [read $f 1]
4434
    lappend l [tell $f]
4435
    lappend l [read $f 1]
4436
    lappend l [tell $f]
4437
    lappend l [read $f 1]
4438
    lappend l [tell $f]
4439
    lappend l [read $f 1]
4440
    lappend l [tell $f]
4441
    lappend l [read $f 1]
4442
    lappend l [tell $f]
4443
    lappend l [eof $f]
4444
    lappend l [read $f 1]
4445
    lappend l [eof $f]
4446
    close $f
4447
    set l
4448
} "7 a 1 [list \r] 2 b 3 [list \r] 4 c 5 {
4449
} 7 0 {} 1"
4450
test io-26.2 {testing crlf reading, leftover cr disgorgment} {
4451
    removeFile test1
4452
    set f [open test1 w]
4453
    fconfigure $f -translation lf
4454
    puts -nonewline $f "a\rb\rc\r\n"
4455
    close $f
4456
    set f [open test1 r]
4457
    set l ""
4458
    lappend l [file size test1]
4459
    fconfigure $f -translation crlf
4460
    lappend l [read $f 2]
4461
    lappend l [tell $f]
4462
    lappend l [read $f 2]
4463
    lappend l [tell $f]
4464
    lappend l [read $f 2]
4465
    lappend l [tell $f]
4466
    lappend l [eof $f]
4467
    lappend l [read $f 2]
4468
    lappend l [tell $f]
4469
    lappend l [eof $f]
4470
    close $f
4471
    set l
4472
} "7 [list a\r] 2 [list b\r] 4 [list c\n] 7 0 {} 7 1"
4473
test io-26.3 {testing crlf reading, leftover cr disgorgment} {
4474
    removeFile test1
4475
    set f [open test1 w]
4476
    fconfigure $f -translation lf
4477
    puts -nonewline $f "a\rb\rc\r\n"
4478
    close $f
4479
    set f [open test1 r]
4480
    set l ""
4481
    lappend l [file size test1]
4482
    fconfigure $f -translation crlf
4483
    lappend l [read $f 3]
4484
    lappend l [tell $f]
4485
    lappend l [read $f 3]
4486
    lappend l [tell $f]
4487
    lappend l [eof $f]
4488
    lappend l [read $f 3]
4489
    lappend l [tell $f]
4490
    lappend l [eof $f]
4491
    close $f
4492
    set l
4493
} "7 [list a\rb] 3 [list \rc\n] 7 0 {} 7 1"
4494
test io-26.4 {testing crlf reading, leftover cr disgorgment} {
4495
    removeFile test1
4496
    set f [open test1 w]
4497
    fconfigure $f -translation lf
4498
    puts -nonewline $f "a\rb\rc\r\n"
4499
    close $f
4500
    set f [open test1 r]
4501
    set l ""
4502
    lappend l [file size test1]
4503
    fconfigure $f -translation crlf
4504
    lappend l [read $f 3]
4505
    lappend l [tell $f]
4506
    lappend l [gets $f]
4507
    lappend l [tell $f]
4508
    lappend l [eof $f]
4509
    lappend l [gets $f]
4510
    lappend l [tell $f]
4511
    lappend l [eof $f]
4512
    close $f
4513
    set l
4514
} "7 [list a\rb] 3 [list \rc] 7 0 {} 7 1"
4515
test io-26.5 {testing crlf reading, leftover cr disgorgment} {
4516
    removeFile test1
4517
    set f [open test1 w]
4518
    fconfigure $f -translation lf
4519
    puts -nonewline $f "a\rb\rc\r\n"
4520
    close $f
4521
    set f [open test1 r]
4522
    set l ""
4523
    lappend l [file size test1]
4524
    fconfigure $f -translation crlf
4525
    lappend l [set x [gets $f]]
4526
    lappend l [tell $f]
4527
    lappend l [gets $f]
4528
    lappend l [tell $f]
4529
    lappend l [eof $f]
4530
    close $f
4531
    set l
4532
} [list 7 a\rb\rc 7 {} 7 1]
4533
 
4534
test io-27.1 {testing handler deletion} {
4535
    removeFile test1
4536
    set f [open test1 w]
4537
    close $f
4538
    set f [open test1 r]
4539
    testchannelevent $f add readable [list delhandler $f]
4540
    proc delhandler {f} {
4541
        global z
4542
        set z called
4543
        testchannelevent $f delete 0
4544
    }
4545
    set z not_called
4546
    update
4547
    close $f
4548
    set z
4549
} called
4550
test io-27.2 {testing handler deletion with multiple handlers} {
4551
    removeFile test1
4552
    set f [open test1 w]
4553
    close $f
4554
    set f [open test1 r]
4555
    testchannelevent $f add readable [list delhandler $f 1]
4556
    testchannelevent $f add readable [list delhandler $f 0]
4557
    proc delhandler {f i} {
4558
        global z
4559
        lappend z "called delhandler $f $i"
4560
        testchannelevent $f delete 0
4561
    }
4562
    set z ""
4563
    update
4564
    close $f
4565
    string compare [string tolower $z] \
4566
        [list [list called delhandler $f 0] [list called delhandler $f 1]]
4567
} 0
4568
test io-27.3 {testing handler deletion with multiple handlers} {
4569
    removeFile test1
4570
    set f [open test1 w]
4571
    close $f
4572
    set f [open test1 r]
4573
    testchannelevent $f add readable [list notcalled $f 1]
4574
    testchannelevent $f add readable [list delhandler $f 0]
4575
    set z ""
4576
    proc notcalled {f i} {
4577
        global z
4578
        lappend z "notcalled was called!! $f $i"
4579
    }
4580
    proc delhandler {f i} {
4581
        global z
4582
        testchannelevent $f delete 1
4583
        lappend z "delhandler $f $i called"
4584
        testchannelevent $f delete 0
4585
        lappend z "delhandler $f $i deleted myself"
4586
    }
4587
    set z ""
4588
    update
4589
    close $f
4590
    string compare [string tolower $z] \
4591
        [list [list delhandler $f 0 called] \
4592
              [list delhandler $f 0 deleted myself]]
4593
} 0
4594
test io-27.4 {testing handler deletion vs reentrant calls} {
4595
    removeFile test1
4596
    set f [open test1 w]
4597
    close $f
4598
    set f [open test1 r]
4599
    testchannelevent $f add readable [list delrecursive $f]
4600
    proc delrecursive {f} {
4601
        global z u
4602
        if {"$u" == "recursive"} {
4603
            testchannelevent $f delete 0
4604
            lappend z "delrecursive deleting recursive"
4605
        } else {
4606
            lappend z "delrecursive calling recursive"
4607
            set u recursive
4608
            update
4609
        }
4610
    }
4611
    set u toplevel
4612
    set z ""
4613
    update
4614
    close $f
4615
    string compare [string tolower $z] \
4616
        {{delrecursive calling recursive} {delrecursive deleting recursive}}
4617
} 0
4618
test io-27.5 {testing handler deletion vs reentrant calls} {
4619
    removeFile test1
4620
    set f [open test1 w]
4621
    close $f
4622
    set f [open test1 r]
4623
    testchannelevent $f add readable [list notcalled $f]
4624
    testchannelevent $f add readable [list del $f]
4625
    proc notcalled {f} {
4626
        global z
4627
        lappend z "notcalled was called!! $f"
4628
    }
4629
    proc del {f} {
4630
        global z u
4631
        if {"$u" == "recursive"} {
4632
            testchannelevent $f delete 1
4633
            testchannelevent $f delete 0
4634
            lappend z "del deleted notcalled"
4635
            lappend z "del deleted myself"
4636
        } else {
4637
            set u recursive
4638
            lappend z "del calling recursive"
4639
            update
4640
            lappend z "del after update"
4641
        }
4642
    }
4643
    set z ""
4644
    set u toplevel
4645
    update
4646
    close $f
4647
    string compare [string tolower $z] \
4648
        [list {del calling recursive} {del deleted notcalled} \
4649
              {del deleted myself} {del after update}]
4650
} 0
4651
test io-27.6 {testing handler deletion vs reentrant calls} {
4652
    removeFile test1
4653
    set f [open test1 w]
4654
    close $f
4655
    set f [open test1 r]
4656
    testchannelevent $f add readable [list second $f]
4657
    testchannelevent $f add readable [list first $f]
4658
    proc first {f} {
4659
        global u z
4660
        if {"$u" == "toplevel"} {
4661
            lappend z "first called"
4662
            set u first
4663
            update
4664
            lappend z "first after update"
4665
        } else {
4666
            lappend z "first called not toplevel"
4667
        }
4668
    }
4669
    proc second {f} {
4670
        global u z
4671
        if {"$u" == "first"} {
4672
            lappend z "second called, first time"
4673
            set u second
4674
            testchannelevent $f delete 0
4675
        } elseif {"$u" == "second"} {
4676
            lappend z "second called, second time"
4677
            testchannelevent $f delete 0
4678
        } else {
4679
            lappend z "second called, cannot happen!"
4680
            testchannelevent $f removeall
4681
        }
4682
    }
4683
    set z ""
4684
    set u toplevel
4685
    update
4686
    close $f
4687
    string compare [string tolower $z] \
4688
        [list {first called} {first called not toplevel} \
4689
              {second called, first time} {second called, second time} \
4690
              {first after update}]
4691
} 0
4692
 
4693
test io-28.1 {Test old socket deletion on Macintosh} {socket} {
4694
    set x 0
4695
    set result ""
4696
    proc accept {s a p} {
4697
        global x wait
4698
        fconfigure $s -blocking off
4699
        puts $s "sock[incr x]"
4700
        close $s
4701
        set wait done
4702
    }
4703
    set ss [socket -server accept 2831]
4704
    set wait ""
4705
    set cs [socket [info hostname] 2831]
4706
    vwait wait
4707
    lappend result [gets $cs]
4708
    close $cs
4709
 
4710
    set wait ""
4711
    set cs [socket [info hostname] 2831]
4712
    vwait wait
4713
    lappend result [gets $cs]
4714
    close $cs
4715
 
4716
    set wait ""
4717
    set cs [socket [info hostname] 2831]
4718
    vwait wait
4719
    lappend result [gets $cs]
4720
    close $cs
4721
 
4722
    set wait ""
4723
    set cs [socket [info hostname] 2831]
4724
    vwait wait
4725
    lappend result [gets $cs]
4726
    close $cs
4727
    close $ss
4728
    set result
4729
} {sock1 sock2 sock3 sock4}
4730
 
4731
test io-29.1 {TclCopyChannel} {
4732
    removeFile test1
4733
    set f1 [open [info script]]
4734
    set f2 [open test1 w]
4735
    fcopy $f1 $f2 -command { # }
4736
    catch { fcopy $f1 $f2 } msg
4737
    close $f1
4738
    close $f2
4739
    string compare $msg "channel \"$f1\" is busy"
4740
} {0}
4741
test io-29.2 {TclCopyChannel} {
4742
    removeFile test1
4743
    set f1 [open [info script]]
4744
    set f2 [open test1 w]
4745
    set f3 [open [info script]]
4746
    fcopy $f1 $f2 -command { # }
4747
    catch { fcopy $f3 $f2 } msg
4748
    close $f1
4749
    close $f2
4750
    close $f3
4751
    string compare $msg "channel \"$f2\" is busy"
4752
} {0}
4753
test io-29.3 {TclCopyChannel} {
4754
    removeFile test1
4755
    set f1 [open [info script]]
4756
    set f2 [open test1 w]
4757
    fconfigure $f1 -translation lf -blocking 0
4758
    fconfigure $f2 -translation cr -blocking 0
4759
    set s0 [fcopy $f1 $f2]
4760
    set result [list [fconfigure $f1 -blocking] [fconfigure $f2 -blocking]]
4761
    close $f1
4762
    close $f2
4763
    set s1 [file size [info script]]
4764
    set s2 [file size test1]
4765
    if {("$s1" == "$s2") && ($s0 == $s1)} {
4766
        lappend result ok
4767
    }
4768
    set result
4769
} {0 0 ok}
4770
test io-29.4 {TclCopyChannel} {
4771
    removeFile test1
4772
    set f1 [open [info script]]
4773
    set f2 [open test1 w]
4774
    fconfigure $f1 -translation lf -blocking 0
4775
    fconfigure $f2 -translation cr -blocking 0
4776
    fcopy $f1 $f2 -size 40
4777
    set result [list [fconfigure $f1 -blocking] [fconfigure $f2 -blocking]]
4778
    close $f1
4779
    close $f2
4780
    lappend result [file size test1]
4781
} {0 0 40}
4782
test io-29.5 {TclCopyChannel} {
4783
    removeFile test1
4784
    set f1 [open [info script]]
4785
    set f2 [open test1 w]
4786
    fconfigure $f1 -translation lf -blocking 0
4787
    fconfigure $f2 -translation lf -blocking 0
4788
    fcopy $f1 $f2 -size -1
4789
    set result [list [fconfigure $f1 -blocking] [fconfigure $f2 -blocking]]
4790
    close $f1
4791
    close $f2
4792
    set s1 [file size [info script]]
4793
    set s2 [file size test1]
4794
    if {"$s1" == "$s2"} {
4795
        lappend result ok
4796
    }
4797
    set result
4798
} {0 0 ok}
4799
test io-29.6 {TclCopyChannel} {
4800
    removeFile test1
4801
    set f1 [open [info script]]
4802
    set f2 [open test1 w]
4803
    fconfigure $f1 -translation lf -blocking 0
4804
    fconfigure $f2 -translation lf -blocking 0
4805
    set s0 [fcopy $f1 $f2 -size [expr [file size [info script]] + 5]]
4806
    set result [list [fconfigure $f1 -blocking] [fconfigure $f2 -blocking]]
4807
    close $f1
4808
    close $f2
4809
    set s1 [file size [info script]]
4810
    set s2 [file size test1]
4811
    if {("$s1" == "$s2") && ($s0 == $s1)} {
4812
        lappend result ok
4813
    }
4814
    set result
4815
} {0 0 ok}
4816
test io-29.7 {TclCopyChannel} {
4817
    removeFile test1
4818
    set f1 [open [info script]]
4819
    set f2 [open test1 w]
4820
    fconfigure $f1 -translation lf -blocking 0
4821
    fconfigure $f2 -translation lf -blocking 0
4822
    fcopy $f1 $f2
4823
    set result [list [fconfigure $f1 -blocking] [fconfigure $f2 -blocking]]
4824
    set s1 [file size [info script]]
4825
    set s2 [file size test1]
4826
    close $f1
4827
    close $f2
4828
    if {"$s1" == "$s2"} {
4829
        lappend result ok
4830
    }
4831
    set result
4832
} {0 0 ok}
4833
test io-29.8 {TclCopyChannel} {stdio} {
4834
    removeFile test1
4835
    removeFile pipe
4836
    set f1 [open pipe w]
4837
    fconfigure $f1 -translation lf
4838
    puts $f1 {
4839
        puts ready
4840
        gets stdin
4841
        set f1 [open [info script] r]
4842
        fconfigure $f1 -translation lf
4843
        puts [read $f1 100]
4844
        close $f1
4845
    }
4846
    close $f1
4847
    set f1 [open "|[list $tcltest pipe]" r+]
4848
    fconfigure $f1 -translation lf
4849
    gets $f1
4850
    puts $f1 ready
4851
    flush $f1
4852
    set f2 [open test1 w]
4853
    fconfigure $f2 -translation lf
4854
    set s0 [fcopy $f1 $f2 -size 40]
4855
    catch {close $f1}
4856
    close $f2
4857
    list $s0 [file size test1]
4858
} {40 40}
4859
 
4860
test io-30.1 {CopyData} {
4861
    removeFile test1
4862
    set f1 [open [info script]]
4863
    set f2 [open test1 w]
4864
    fconfigure $f1 -translation lf -blocking 0
4865
    fconfigure $f2 -translation cr -blocking 0
4866
    fcopy $f1 $f2 -size 0
4867
    set result [list [fconfigure $f1 -blocking] [fconfigure $f2 -blocking]]
4868
    close $f1
4869
    close $f2
4870
    lappend result [file size test1]
4871
} {0 0 0}
4872
test io-30.2 {CopyData} {
4873
    removeFile test1
4874
    set f1 [open [info script]]
4875
    set f2 [open test1 w]
4876
    fconfigure $f1 -translation lf -blocking 0
4877
    fconfigure $f2 -translation cr -blocking 0
4878
    fcopy $f1 $f2 -command {set s0}
4879
    set result [list [fconfigure $f1 -blocking] [fconfigure $f2 -blocking]]
4880
    vwait s0
4881
    close $f1
4882
    close $f2
4883
    set s1 [file size [info script]]
4884
    set s2 [file size test1]
4885
    if {("$s1" == "$s2") && ($s0 == $s1)} {
4886
        lappend result ok
4887
    }
4888
    set result
4889
} {0 0 ok}
4890
test io-30.3 {CopyData: background read underflow} {unixOnly} {
4891
    removeFile test1
4892
    removeFile pipe
4893
    set f1 [open pipe w]
4894
    puts $f1 {
4895
        puts ready
4896
        flush stdout                            ;# Don't assume line buffered!
4897
        fcopy stdin stdout -command { set x }
4898
        vwait x
4899
        set f [open test1 w]
4900
        fconfigure $f -translation lf
4901
        puts $f "done"
4902
        close $f
4903
    }
4904
    close $f1
4905
    set f1 [open "|[list $tcltest pipe]" r+]
4906
    set result [gets $f1]
4907
    puts $f1 line1
4908
    flush $f1
4909
    lappend result [gets $f1]
4910
    puts $f1 line2
4911
    flush $f1
4912
    lappend result [gets $f1]
4913
    close $f1
4914
    after 500
4915
    set f [open test1]
4916
    lappend result [read $f]
4917
    close $f
4918
    set result
4919
} "ready line1 line2 {done\n}"
4920
test io-30.4 {CopyData: background write overflow} {unixOnly} {
4921
    set big aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n
4922
    for {set x 0} {$x < 12} {incr x} {
4923
        append big $big
4924
    }
4925
    removeFile test1
4926
    removeFile pipe
4927
    set f1 [open pipe w]
4928
    puts $f1 {
4929
        puts ready
4930
        fcopy stdin stdout -command { set x }
4931
        vwait x
4932
        set f [open test1 w]
4933
        fconfigure $f -translation lf
4934
        puts $f "done"
4935
        close $f
4936
    }
4937
    close $f1
4938
    set f1 [open "|[list $tcltest pipe]" r+]
4939
    set result [gets $f1]
4940
    fconfigure $f1 -blocking 0
4941
    puts $f1 $big
4942
    flush $f1
4943
    after 500
4944
    set result ""
4945
    fileevent $f1 read {
4946
        append result [read $f1 1024]
4947
        if {[string length $result] >= [string length $big]} {
4948
            set x done
4949
        }
4950
    }
4951
    vwait x
4952
    close $f1
4953
    set big {}
4954
    set x
4955
} done
4956
 
4957
proc FcopyTestAccept {sock args} {
4958
    after 1000 "close $sock"
4959
}
4960
proc FcopyTestDone {bytes {error {}}} {
4961
    global fcopyTestDone
4962
    if {[string length $error]} {
4963
        set fcopyTestDone 1
4964
    } else {
4965
        set fcopyTestDone 0
4966
    }
4967
}
4968
if [catch {socket -server FcopyTestAccept 2828} listen] {
4969
    puts stderr "Skipping fcopy error test"
4970
} else {
4971
    test io-30.5 {CopyData: error during fcopy} {
4972
        set in [open [info script]]     ;# 126 K
4973
        set out [socket localhost 2828]
4974
        catch {unset fcopyTestDone}
4975
        close $listen   ;# This means the socket open never really succeeds
4976
        fcopy $in $out -command FcopyTestDone
4977
        if ![info exists fcopyTestDone] {
4978
            vwait fcopyTestDone         ;# The error occurs here in the b.g.
4979
        }
4980
        close $in
4981
        close $out
4982
        set fcopyTestDone       ;# 1 for error condition
4983
    } 1
4984
}
4985
test io-30.6 {CopyData: error during fcopy} {stdio} {
4986
    removeFile pipe
4987
    removeFile test1
4988
    catch {unset fcopyTestDone}
4989
    set f1 [open pipe w]
4990
    puts $f1 "exit 1"
4991
    close $f1
4992
    set in [open "|[list $tcltest pipe]" r+]
4993
    set out [open test1 w]
4994
    fcopy $in $out -command [list FcopyTestDone]
4995
    if ![info exists fcopyTestDone] {
4996
        vwait fcopyTestDone
4997
    }
4998
    catch {close $in}
4999
    close $out
5000
    set fcopyTestDone   ;# 0 for plain end of file
5001
} {0}
5002
 
5003
test io-31.1 {Recursive channel events} {socket} {
5004
    # This test checks to see if file events are delivered during recursive
5005
    # event loops when there is buffered data on the channel.
5006
 
5007
    proc accept {s a p} {
5008
        global as
5009
        fconfigure $s -translation lf
5010
        puts $s "line 1\nline2\nline3"
5011
        flush $s
5012
        set as $s
5013
    }
5014
    proc readit {s next} {
5015
        global result x
5016
        lappend result $next
5017
        if {$next == 1} {
5018
            fileevent $s readable [list readit $s 2]
5019
            vwait x
5020
        }
5021
        incr x
5022
    }
5023
    set ss [socket -server accept 2828]
5024
 
5025
    # We need to delay on some systems until the creation of the
5026
    # server socket completes.
5027
 
5028
    set done 0
5029
    for {set i 0} {$i < 10} {incr i} {
5030
        if {![catch {set cs [socket [info hostname] 2828]}]} {
5031
            set done 1
5032
            break
5033
        }
5034
        after 100
5035
    }
5036
    if {$done == 0} {
5037
        close $ss
5038
        error "failed to connect to server"
5039
    }
5040
    set result {}
5041
    set x 0
5042
    vwait as
5043
    fconfigure $cs -translation lf
5044
    lappend result [gets $cs]
5045
    fconfigure $cs -blocking off
5046
    fileevent $cs readable [list readit $cs 1]
5047
    set a [after 2000 { set x failure }]
5048
    vwait x
5049
    after cancel $a
5050
    close $as
5051
    close $ss
5052
    close $cs
5053
    list $result $x
5054
} {{{line 1} 1 2} 2}
5055
test io-31.2 {Testing for busy-wait in recursive channel events} {socket} {
5056
    set s [socket -server accept 3939]
5057
    proc accept {s a p} {
5058
        global counter
5059
 
5060
        set counter 0
5061
        fconfigure $s -blocking off -buffering line -translation lf
5062
        fileevent $s readable "doit $s"
5063
    }
5064
    proc doit {s} {
5065
        global counter
5066
 
5067
        incr counter
5068
        set l [gets $s]
5069
        if {"$l" == ""} {
5070
            fileevent $s readable "doit1 $s"
5071
            after 1000 newline
5072
        }
5073
    }
5074
    proc doit1 {s} {
5075
        global counter
5076
 
5077
        incr counter
5078
        set l [gets $s]
5079
        close $s
5080
    }
5081
    proc producer {} {
5082
        global writer
5083
 
5084
        set writer [socket localhost 3939]
5085
        fconfigure $writer -buffering line
5086
        puts -nonewline $writer hello
5087
        flush $writer
5088
    }
5089
    proc newline {} {
5090
        global writer done
5091
 
5092
        puts $writer hello
5093
        flush $writer
5094
        set done 1
5095
    }
5096
    producer
5097
    vwait done
5098
    close $writer
5099
    close $s
5100
    set counter
5101
} 1
5102
test io-32.1 {ChannelEventScriptInvoker: deletion} {
5103
    proc eventScript {fd} {
5104
        close $fd
5105
        error "planned error"
5106
        set ::x whoops
5107
    }
5108
    proc bgerror {args} {
5109
        set ::x got_error
5110
    }
5111
    set f [open fooBar w]
5112
    fileevent $f writable [list eventScript $f]
5113
    set x not_done
5114
    vwait x
5115
    set x
5116
} {got_error}
5117
 
5118
test io-33.1 {ChannelTimerProc} {
5119
    set f [open fooBar w]
5120
    puts $f "this is a test"
5121
    close $f
5122
    set f [open fooBar r]
5123
    testchannelevent $f add readable {
5124
        read $f 1
5125
        incr x
5126
    }
5127
    set x 0
5128
    vwait x
5129
    vwait x
5130
    set result $x
5131
    testchannelevent $f set 0 none
5132
    after idle {set y done}
5133
    vwait y
5134
    close $f
5135
    lappend result $y
5136
} {2 done}
5137
 
5138
test io-34.1 {buffered data and file events, gets} {
5139
    proc accept {sock args} {
5140
        set ::s2 $sock
5141
    }
5142
    set server [socket -server accept 4040]
5143
    set s [socket localhost 4040]
5144
    vwait s2
5145
    update
5146
    fileevent $s2 readable {lappend result readable}
5147
    puts $s "12\n34567890"
5148
    flush $s
5149
    set result [gets $s2]
5150
    after 1000 {lappend result timer}
5151
    vwait result
5152
    lappend result [gets $s2]
5153
    vwait result
5154
    close $s
5155
    close $s2
5156
    close $server
5157
    set result
5158
} {12 readable 34567890 timer}
5159
test io-34.2 {buffered data and file events, read} {
5160
    proc accept {sock args} {
5161
        set ::s2 $sock
5162
    }
5163
    set server [socket -server accept 4041]
5164
    set s [socket localhost 4041]
5165
    vwait s2
5166
    update
5167
    fileevent $s2 readable {lappend result readable}
5168
    puts -nonewline $s "1234567890"
5169
    flush $s
5170
    set result [read $s2 1]
5171
    after 1000 {lappend result timer}
5172
    vwait result
5173
    lappend result [read $s2 9]
5174
    vwait result
5175
    close $s
5176
    close $s2
5177
    close $server
5178
    set result
5179
} {1 readable 234567890 timer}
5180
 
5181
test io-35.1 {Tcl_NotifyChannel and error when closing} {unixOrPc} {
5182
    set out [open script w]
5183
    puts $out {
5184
        puts "normal message from pipe"
5185
        puts stderr "error message from pipe"
5186
        exit 1
5187
    }
5188
    proc readit {pipe} {
5189
        global x result
5190
        if {[eof $pipe]} {
5191
            set x [catch {close $pipe} line]
5192
            lappend result catch $line
5193
        } else {
5194
            gets $pipe line
5195
            lappend result gets $line
5196
        }
5197
    }
5198
    close $out
5199
    set pipe [open "|[list $tcltest] script" r]
5200
    fileevent $pipe readable [list readit $pipe]
5201
    set x ""
5202
    set result ""
5203
    vwait x
5204
    list $x $result
5205
} {1 {gets {normal message from pipe} gets {} catch {error message from pipe}}}
5206
 
5207
 
5208
removeFile fooBar
5209
removeFile longfile
5210
removeFile script
5211
removeFile output
5212
removeFile test1
5213
removeFile pipe
5214
removeFile my_script
5215
removeFile foo
5216
removeFile bar
5217
removeFile test2
5218
removeFile test3
5219
 
5220
file delete cat
5221
 
5222
set x ""
5223
unset x

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.