xref: /openbsd/gnu/usr.bin/perl/t/op/defins.t (revision d89ec533)
1#!./perl -w
2
3#
4# test auto defined() test insertion
5#
6
7BEGIN {
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
14plan( tests => 27 );
15
16my $unix_mode = 1;
17
18if ($^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
49cmp_ok($warns,'==',0,'no warns at start');
50
51ok(open(FILE,">$saved_filename"),'created work file');
52print FILE "0\n";
53print FILE "1\n";
54close(FILE);
55
56open(FILE,"<$saved_filename");
57ok(defined(FILE),'opened work file');
58my $seen = 0;
59my $dummy;
60while (my $name = <FILE>)
61 {
62  chomp($name);
63  $seen++ if $name eq '0';
64 }
65cmp_ok($seen,'==',1,'seen in while()');
66
67seek(FILE,0,0);
68$seen = 0;
69my $line = '';
70do
71 {
72  chomp($line);
73  $seen++ if $line eq '0';
74 } while ($line = <FILE>);
75cmp_ok($seen,'==',1,'seen in do/while');
76
77seek(FILE,0,0);
78$seen = 0;
79while (($seen ? $dummy : $name) = <FILE> )
80 {
81  chomp($name);
82  $seen++ if $name eq '0';
83 }
84cmp_ok($seen,'==',2,'seen in while() ternary');
85
86seek(FILE,0,0);
87$seen = 0;
88my %where;
89while ($where{$seen} = <FILE>)
90 {
91  chomp($where{$seen});
92  $seen++ if $where{$seen} eq '0';
93 }
94cmp_ok($seen,'==',1,'seen in hash while()');
95close FILE;
96
97opendir(DIR,'.');
98ok(defined(DIR),'opened current directory');
99$seen = 0;
100while (my $name = readdir(DIR))
101 {
102  $seen++ if $name eq $wanted_filename;
103 }
104cmp_ok($seen,'==',1,'saw work file once');
105
106rewinddir(DIR);
107$seen = 0;
108$dummy = '';
109while (($seen ? $dummy : $name) = readdir(DIR))
110 {
111  $seen++ if $name eq $wanted_filename;
112 }
113cmp_ok($seen,'>',0,'saw file in while() ternary');
114
115rewinddir(DIR);
116$seen = 0;
117while ($where{$seen} = readdir(DIR))
118 {
119  $seen++ if $where{$seen} eq $wanted_filename;
120 }
121cmp_ok($seen,'==',1,'saw file in hash while()');
122
123rewinddir(DIR);
124$seen = 0;
125$_ = 'not 0';
126while (readdir(DIR))
127 {
128  $seen++ if $_ eq $wanted_filename;
129 }
130cmp_ok($seen,'==',1,'saw file in bare while(readdir){...}');
131
132rewinddir(DIR);
133$seen = 0;
134$_ = 'not 0';
135
136$_ eq $wanted_filename && $seen++ while readdir(DIR);
137cmp_ok($seen,'==',1,'saw file in bare "... while readdir"');
138
139rewinddir(DIR);
140$seen = 0;
141$_ = "";  # suppress uninit warning
142do
143 {
144  $seen++ if $_ eq $wanted_filename;
145 } while (readdir(DIR));
146cmp_ok($seen,'==',1,'saw file in bare do{...}while(readdir)');
147
148$seen = 0;
149while (my $name = glob('*'))
150 {
151  $seen++ if $name eq $wanted_filename;
152 }
153cmp_ok($seen,'==',1,'saw file in glob while()');
154
155$seen = 0;
156$dummy = '';
157while (($seen ? $dummy : $name) = glob('*'))
158 {
159  $seen++ if $name eq $wanted_filename;
160 }
161cmp_ok($seen,'>',0,'saw file in glob hash while() ternary');
162
163$seen = 0;
164while ($where{$seen} = glob('*'))
165 {
166  $seen++ if $where{$seen} eq $wanted_filename;
167 }
168cmp_ok($seen,'==',1,'seen in glob hash while()');
169
170unlink($saved_filename);
171ok(!(-f $saved_filename),'work file unlinked');
172
173my %hash = (0 => 1, 1 => 2);
174my @array = 1;
175my $neg_sum= 0;
176
177$seen = 0;
178
179while (my $name = each %hash)
180 {
181  $neg_sum = $name - $neg_sum;
182  $seen++ if $name eq '0';
183 }
184cmp_ok(abs($neg_sum),'==',1,'abs(neg_sum) should equal 1');
185cmp_ok($seen,'==',1,'seen in each');
186
187$seen = 0;
188$dummy = '';
189while (($seen ? $dummy : $name) = each %hash)
190 {
191  $seen++ if $name eq '0';
192 }
193cmp_ok($seen,'==',$neg_sum < 0 ? 1 : 2,'seen in each ternary');
194
195$seen = 0;
196while ($where{$seen} = each %hash)
197 {
198  $seen++ if $where{$seen} eq '0';
199 }
200cmp_ok($seen,'==',1,'seen in each hash');
201
202$seen = 0;
203undef $_;
204while (each %hash)
205 {
206  $seen++ if $_ eq '0';
207 }
208cmp_ok($seen,'==',1,'0 seen in $_ in while(each %hash)');
209
210$seen = 0;
211undef $_;
212while (each @array)
213 {
214  $seen++ if $_ eq '0';
215 }
216cmp_ok($seen,'==',1,'0 seen in $_ in while(each @array)');
217
218$seen = 0;
219undef $_;
220$_ eq '0' and $seen++ while each %hash;
221cmp_ok($seen,'==',1,'0 seen in $_ in while(each %hash) as stm mod');
222
223$seen = 0;
224undef $_;
225$_ eq '0' and $seen++ while each @array;
226cmp_ok($seen,'==',1,'0 seen in $_ in while(each @array) as stm mod');
227
228cmp_ok($warns,'==',0,'no warns at finish');
229