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
1 #!./perl
2
3 BEGIN {
4     chdir 't' if -d 't';
5     @INC = '../lib';
6 }
7
8 print "1..28\n";
9
10 $TST = 'TST';
11
12 $Is_Dosish = ($^O eq 'MSWin32' or $^O eq 'NetWare' or $^O eq 'dos' or
13               $^O eq 'os2' or $^O eq 'mint' or $^O eq 'cygwin' or
14               $^O =~ /^uwin/);
15
16 open($TST, 'harness') || (die "Can't open harness");
17 binmode $TST if $Is_Dosish;
18 if (eof(TST)) { print "not ok 1\n"; } else { print "ok 1\n"; }
19
20 $firstline = <$TST>;
21 $secondpos = tell;
22
23 $x = 0;
24 while (<TST>) {
25     if (eof) {$x++;}
26 }
27 if ($x == 1) { print "ok 2\n"; } else { print "not ok 2\n"; }
28
29 $lastpos = tell;
30
31 unless (eof) { print "not ok 3\n"; } else { print "ok 3\n"; }
32
33 if (seek($TST,0,0)) { print "ok 4\n"; } else { print "not ok 4\n"; }
34
35 if (eof) { print "not ok 5\n"; } else { print "ok 5\n"; }
36
37 if ($firstline eq <TST>) { print "ok 6\n"; } else { print "not ok 6\n"; }
38
39 if ($secondpos == tell) { print "ok 7\n"; } else { print "not ok 7\n"; }
40
41 if (seek(TST,0,1)) { print "ok 8\n"; } else { print "not ok 8\n"; }
42
43 if (eof($TST)) { print "not ok 9\n"; } else { print "ok 9\n"; }
44
45 if ($secondpos == tell) { print "ok 10\n"; } else { print "not ok 10\n"; }
46
47 if (seek(TST,0,2)) { print "ok 11\n"; } else { print "not ok 11\n"; }
48
49 if ($lastpos == tell) { print "ok 12\n"; } else { print "not ok 12\n"; }
50
51 unless (eof) { print "not ok 13\n"; } else { print "ok 13\n"; }
52
53 if ($. == 0) { print "not ok 14\n"; } else { print "ok 14\n"; }
54
55 $curline = $.;
56 open(OTHER, 'harness') || (die "Can't open harness: $!");
57 binmode OTHER if (($^O eq 'MSWin32') || ($^O eq 'NetWare'));
58
59 {
60     local($.);
61
62     if ($. == 0) { print "not ok 15\n"; } else { print "ok 15\n"; }
63
64     tell OTHER;
65     if ($. == 0) { print "ok 16\n"; } else { print "not ok 16\n"; }
66
67     $. = 5;
68     scalar <OTHER>;
69     if ($. == 6) { print "ok 17\n"; } else { print "not ok 17\n"; }
70 }
71
72 if ($. == $curline) { print "ok 18\n"; } else { print "not ok 18\n"; }
73
74 {
75     local($.);
76
77     scalar <OTHER>;
78     if ($. == 7) { print "ok 19\n"; } else { print "not ok 19\n"; }
79 }
80
81 if ($. == $curline) { print "ok 20\n"; } else { print "not ok 20\n"; }
82
83 {
84     local($.);
85
86     tell OTHER;
87     if ($. == 7) { print "ok 21\n"; } else { print "not ok 21\n"; }
88 }
89
90 close(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 }
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
104 my $written = "tell_write.txt";
105
106 END { 1 while unlink($written) }
107
108 close($TST);
109 open($tst,">$written")  || die "Cannot open $written:$!";
110 binmode $tst if $Is_Dosish;
111
112 if (tell($tst) == 0) { print "ok 24\n"; } else { print "not ok 24\n"; }
113
114 print $tst "fred\n";
115
116 if (tell($tst) == 5) { print "ok 25\n"; } else { print "not ok 25\n"; }
117
118 print $tst "more\n";
119
120 if (tell($tst) == 10) { print "ok 26\n"; } else { print "not ok 26\n"; }
121
122 close($tst);
123
124 open($tst,"+>>$written")  || die "Cannot open $written:$!";
125 binmode $tst if $Is_Dosish;
126
127 if (0) 
128 {
129  # :stdio does not pass these so ignore them for now 
130
131 if (tell($tst) == 0) { print "ok 27\n"; } else { print "not ok 27\n"; }
132
133 $line = <$tst>;
134
135 if ($line eq "fred\n") { print "ok 29\n"; } else { print "not ok 29\n"; }
136
137 if (tell($tst) == 5) { print "ok 30\n"; } else { print "not ok 30\n"; }
138
139 }
140
141 print $tst "xxxx\n";
142
143 if (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"; }
146
147 close($tst);
148
149 open($tst,">$written")  || die "Cannot open $written:$!";
150 print $tst "foobar";
151 close $tst;
152 open($tst,">>$written")  || die "Cannot open $written:$!";
153
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.
157 my $todo = $^O eq "cygwin" && &PerlIO::get_layers($tst) eq 'stdio'
158     && ' # TODO: file pointer not at eof';
159
160 if (tell($tst) == 6)
161 { print "ok 28$todo\n"; } else { print "not ok 28$todo\n"; }
162 close $tst;
163