This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Perl_sv_vcatpvfn_flags: simpler redundant arg test
[perl5.git] / t / op / defins.t
1 #!./perl -w
2
3 #
4 # test auto defined() test insertion
5 #
6
7 BEGIN {
8     chdir 't' if -d 't';
9     require './test.pl';
10     set_up_inc( qw(. ../lib) );
11     $SIG{__WARN__} = sub { $warns++; warn $_[0] };
12 }
13
14 plan( tests => 27 );
15
16 my $unix_mode = 1;
17
18 if ($^O eq 'VMS') {
19     # We have to know if VMS is in UNIX mode.  In UNIX mode, trailing dots
20     # should not be present.  There are actually two settings that control this.
21
22     $unix_mode = 0;
23     my $unix_rpt = 0;
24     my $drop_dot = 0;
25     if (eval 'require VMS::Feature') {
26         $unix_rpt = VMS::Feature::current('filename_unix_report');
27         $drop_dot = VMS::Feature::current('readdir_dropdotnotype');
28     } else {
29         my $unix_report = $ENV{'DECC$FILENAME_UNIX_REPORT'} || '';
30         $unix_rpt = $unix_report =~ /^[ET1]/i; 
31         my $drop_dot_notype = $ENV{'DECC$READDIR_DROPDOTNOTYPE'} || '';
32         $drop_dot = $drop_dot_notype =~ /^[ET1]/i;
33     }
34     $unix_mode = 1 if $drop_dot && unix_rpt;
35 }
36
37 # $wanted_filename should be 0 for readdir() and glob() tests.
38 # This is because it is the only valid filename that is false in a boolean test.
39
40 # $filename = '0';
41 # print "hi\n" if $filename; # doesn't print
42
43 # In the case of VMS, '0' isn't always the filename that you get.
44 # Which makes those particular tests pointless.
45
46 $wanted_filename = $unix_mode ? '0' : '0.';
47 $saved_filename = './0';
48
49 cmp_ok($warns,'==',0,'no warns at start');
50
51 ok(open(FILE,">$saved_filename"),'created work file');
52 print FILE "0\n";
53 print FILE "1\n";
54 close(FILE);
55
56 open(FILE,"<$saved_filename");
57 ok(defined(FILE),'opened work file');
58 my $seen = 0;
59 my $dummy;
60 while (my $name = <FILE>)
61  {
62   chomp($name);
63   $seen++ if $name eq '0';
64  }
65 cmp_ok($seen,'==',1,'seen in while()');
66
67 seek(FILE,0,0);
68 $seen = 0;
69 my $line = '';
70 do
71  {
72   chomp($line);
73   $seen++ if $line eq '0';
74  } while ($line = <FILE>);
75 cmp_ok($seen,'==',1,'seen in do/while');
76
77 seek(FILE,0,0);
78 $seen = 0;
79 while (($seen ? $dummy : $name) = <FILE> )
80  {
81   chomp($name);
82   $seen++ if $name eq '0';
83  }
84 cmp_ok($seen,'==',2,'seen in while() ternary');
85
86 seek(FILE,0,0);
87 $seen = 0;
88 my %where;
89 while ($where{$seen} = <FILE>)
90  {
91   chomp($where{$seen});
92   $seen++ if $where{$seen} eq '0';
93  }
94 cmp_ok($seen,'==',1,'seen in hash while()');
95 close FILE;
96
97 opendir(DIR,'.');
98 ok(defined(DIR),'opened current directory');
99 $seen = 0;
100 while (my $name = readdir(DIR))
101  {
102   $seen++ if $name eq $wanted_filename;
103  }
104 cmp_ok($seen,'==',1,'saw work file once');
105
106 rewinddir(DIR);
107 $seen = 0;
108 $dummy = '';
109 while (($seen ? $dummy : $name) = readdir(DIR))
110  {
111   $seen++ if $name eq $wanted_filename;
112  }
113 cmp_ok($seen,'>',0,'saw file in while() ternary');
114
115 rewinddir(DIR);
116 $seen = 0;
117 while ($where{$seen} = readdir(DIR))
118  {
119   $seen++ if $where{$seen} eq $wanted_filename;
120  }
121 cmp_ok($seen,'==',1,'saw file in hash while()');
122
123 rewinddir(DIR);
124 $seen = 0;
125 $_ = 'not 0';
126 while (readdir(DIR))
127  {
128   $seen++ if $_ eq $wanted_filename;
129  }
130 cmp_ok($seen,'==',1,'saw file in bare while(readdir){...}');
131
132 rewinddir(DIR);
133 $seen = 0;
134 $_ = 'not 0';
135
136 $_ eq $wanted_filename && $seen++ while readdir(DIR);
137 cmp_ok($seen,'==',1,'saw file in bare "... while readdir"');
138
139 rewinddir(DIR);
140 $seen = 0;
141 $_ = "";  # suppress uninit warning
142 do
143  {
144   $seen++ if $_ eq $wanted_filename;
145  } while (readdir(DIR));
146 cmp_ok($seen,'==',1,'saw file in bare do{...}while(readdir)');
147
148 $seen = 0;
149 while (my $name = glob('*'))
150  {
151   $seen++ if $name eq $wanted_filename;
152  }
153 cmp_ok($seen,'==',1,'saw file in glob while()');
154
155 $seen = 0;
156 $dummy = '';
157 while (($seen ? $dummy : $name) = glob('*'))
158  {
159   $seen++ if $name eq $wanted_filename;
160  }
161 cmp_ok($seen,'>',0,'saw file in glob hash while() ternary');
162
163 $seen = 0;
164 while ($where{$seen} = glob('*'))
165  {
166   $seen++ if $where{$seen} eq $wanted_filename;
167  }
168 cmp_ok($seen,'==',1,'seen in glob hash while()');
169
170 unlink($saved_filename);
171 ok(!(-f $saved_filename),'work file unlinked');
172
173 my %hash = (0 => 1, 1 => 2);
174 my @array = 1;
175 my $neg_sum= 0;
176
177 $seen = 0;
178
179 while (my $name = each %hash)
180  {
181   $neg_sum = $name - $neg_sum;
182   $seen++ if $name eq '0';
183  }
184 cmp_ok(abs($neg_sum),'==',1,'abs(neg_sum) should equal 1');
185 cmp_ok($seen,'==',1,'seen in each');
186
187 $seen = 0;
188 $dummy = '';
189 while (($seen ? $dummy : $name) = each %hash)
190  {
191   $seen++ if $name eq '0';
192  }
193 cmp_ok($seen,'==',$neg_sum < 0 ? 1 : 2,'seen in each ternary');
194
195 $seen = 0;
196 while ($where{$seen} = each %hash)
197  {
198   $seen++ if $where{$seen} eq '0';
199  }
200 cmp_ok($seen,'==',1,'seen in each hash');
201
202 $seen = 0;
203 undef $_;
204 while (each %hash)
205  {
206   $seen++ if $_ eq '0';
207  }
208 cmp_ok($seen,'==',1,'0 seen in $_ in while(each %hash)');
209
210 $seen = 0;
211 undef $_;
212 while (each @array)
213  {
214   $seen++ if $_ eq '0';
215  }
216 cmp_ok($seen,'==',1,'0 seen in $_ in while(each @array)');
217
218 $seen = 0;
219 undef $_;
220 $_ eq '0' and $seen++ while each %hash;
221 cmp_ok($seen,'==',1,'0 seen in $_ in while(each %hash) as stm mod');
222
223 $seen = 0;
224 undef $_;
225 $_ eq '0' and $seen++ while each @array;
226 cmp_ok($seen,'==',1,'0 seen in $_ in while(each @array) as stm mod');
227
228 cmp_ok($warns,'==',0,'no warns at finish');