This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
The state() implementation is not yet perfect. Check in a new todo test
[perl5.git] / t / io / tell.t
CommitLineData
8d063cd8
LW
1#!./perl
2
58c59f01
NC
3BEGIN {
4 chdir 't' if -d 't';
5 @INC = '../lib';
6}
7
c2fcde81 8print "1..28\n";
8d063cd8 9
3fb41248 10$TST = 'TST';
378cc40b 11
2986a63f 12$Is_Dosish = ($^O eq 'MSWin32' or $^O eq 'NetWare' or $^O eq 'dos' or
cebd85e6
JH
13 $^O eq 'os2' or $^O eq 'mint' or $^O eq 'cygwin' or
14 $^O =~ /^uwin/);
8736538c 15
2eb25c99 16open($TST, 'harness') || (die "Can't open harness");
8736538c 17binmode $TST if $Is_Dosish;
3fb41248 18if (eof(TST)) { print "not ok 1\n"; } else { print "ok 1\n"; }
8d063cd8 19
378cc40b 20$firstline = <$TST>;
8d063cd8
LW
21$secondpos = tell;
22
23$x = 0;
3fb41248 24while (<TST>) {
8d063cd8
LW
25 if (eof) {$x++;}
26}
27if ($x == 1) { print "ok 2\n"; } else { print "not ok 2\n"; }
28
29$lastpos = tell;
30
31unless (eof) { print "not ok 3\n"; } else { print "ok 3\n"; }
32
378cc40b 33if (seek($TST,0,0)) { print "ok 4\n"; } else { print "not ok 4\n"; }
8d063cd8
LW
34
35if (eof) { print "not ok 5\n"; } else { print "ok 5\n"; }
36
3fb41248 37if ($firstline eq <TST>) { print "ok 6\n"; } else { print "not ok 6\n"; }
8d063cd8
LW
38
39if ($secondpos == tell) { print "ok 7\n"; } else { print "not ok 7\n"; }
40
3fb41248 41if (seek(TST,0,1)) { print "ok 8\n"; } else { print "not ok 8\n"; }
8d063cd8 42
378cc40b 43if (eof($TST)) { print "not ok 9\n"; } else { print "ok 9\n"; }
8d063cd8
LW
44
45if ($secondpos == tell) { print "ok 10\n"; } else { print "not ok 10\n"; }
46
3fb41248 47if (seek(TST,0,2)) { print "ok 11\n"; } else { print "not ok 11\n"; }
8d063cd8
LW
48
49if ($lastpos == tell) { print "ok 12\n"; } else { print "not ok 12\n"; }
50
51unless (eof) { print "not ok 13\n"; } else { print "ok 13\n"; }
455ece5e
AD
52
53if ($. == 0) { print "not ok 14\n"; } else { print "ok 14\n"; }
54
55$curline = $.;
3fb41248
SP
56open(OTHER, 'harness') || (die "Can't open harness: $!");
57binmode OTHER if (($^O eq 'MSWin32') || ($^O eq 'NetWare'));
455ece5e
AD
58
59{
60 local($.);
61
62 if ($. == 0) { print "not ok 15\n"; } else { print "ok 15\n"; }
63
3fb41248 64 tell OTHER;
455ece5e
AD
65 if ($. == 0) { print "ok 16\n"; } else { print "not ok 16\n"; }
66
67 $. = 5;
3fb41248 68 scalar <OTHER>;
455ece5e
AD
69 if ($. == 6) { print "ok 17\n"; } else { print "not ok 17\n"; }
70}
71
72if ($. == $curline) { print "ok 18\n"; } else { print "not ok 18\n"; }
73
74{
75 local($.);
76
3fb41248 77 scalar <OTHER>;
455ece5e
AD
78 if ($. == 7) { print "ok 19\n"; } else { print "not ok 19\n"; }
79}
80
81if ($. == $curline) { print "ok 20\n"; } else { print "not ok 20\n"; }
82
83{
84 local($.);
85
3fb41248 86 tell OTHER;
455ece5e
AD
87 if ($. == 7) { print "ok 21\n"; } else { print "not ok 21\n"; }
88}
d26ab924 89
3fb41248
SP
90close(OTHER);
91{
92 no warnings 'closed';
93 if (tell(OTHER) == -1) { print "ok 22\n"; } else { print "not ok 22\n"; }
94}
95{
96 no warnings 'unopened';
97 if (tell(ETHER) == -1) { print "ok 23\n"; } else { print "not ok 23\n"; }
98}
d26ab924
JH
99
100# ftell(STDIN) (or any std streams) is undefined, it can return -1 or
101# something else. ftell() on pipes, fifos, and sockets is defined to
102# return -1.
103
0678cb22
NIS
104my $written = "tell_write.txt";
105
4e7ee149 106END { 1 while unlink($written) }
0678cb22 107
3fb41248 108close($TST);
0678cb22 109open($tst,">$written") || die "Cannot open $written:$!";
7bdbfeb7 110binmode $tst if $Is_Dosish;
0678cb22
NIS
111
112if (tell($tst) == 0) { print "ok 24\n"; } else { print "not ok 24\n"; }
113
114print $tst "fred\n";
115
116if (tell($tst) == 5) { print "ok 25\n"; } else { print "not ok 25\n"; }
117
118print $tst "more\n";
119
120if (tell($tst) == 10) { print "ok 26\n"; } else { print "not ok 26\n"; }
121
122close($tst);
123
124open($tst,"+>>$written") || die "Cannot open $written:$!";
7bdbfeb7 125binmode $tst if $Is_Dosish;
0678cb22
NIS
126
127if (0)
128{
129 # :stdio does not pass these so ignore them for now
130
131if (tell($tst) == 0) { print "ok 27\n"; } else { print "not ok 27\n"; }
132
133$line = <$tst>;
134
135if ($line eq "fred\n") { print "ok 29\n"; } else { print "not ok 29\n"; }
136
137if (tell($tst) == 5) { print "ok 30\n"; } else { print "not ok 30\n"; }
138
139}
140
141print $tst "xxxx\n";
142
c6b46866
JH
143if (tell($tst) == 15 ||
144 tell($tst) == 5) # unset PERLIO or PERLIO=stdio (e.g. HP-UX, Solaris)
145{ print "ok 27\n"; } else { print "not ok 27\n"; }
0678cb22
NIS
146
147close($tst);
148
c2fcde81
JH
149open($tst,">$written") || die "Cannot open $written:$!";
150print $tst "foobar";
151close $tst;
152open($tst,">>$written") || die "Cannot open $written:$!";
153
e4fdb327
YST
154# This test makes a questionable assumption that the file pointer will
155# be at eof after opening a file but before seeking, reading, or writing.
156# Only known failure is on cygwin.
92770387
YST
157my $todo = $^O eq "cygwin" && &PerlIO::get_layers($tst) eq 'stdio'
158 && ' # TODO: file pointer not at eof';
e4fdb327 159
c2fcde81 160if (tell($tst) == 6)
92770387 161{ print "ok 28$todo\n"; } else { print "not ok 28$todo\n"; }
c2fcde81 162close $tst;
0678cb22 163