Small optimisations, by Brandon Black
[p5sagit/p5-mst-13.2.git] / t / op / defins.t
CommitLineData
55d729e4 1#!./perl -w
2
3#
4# test auto defined() test insertion
5#
6
7BEGIN {
8 chdir 't' if -d 't';
7e35a6a8 9 @INC = qw(. ../lib);
55d729e4 10 $SIG{__WARN__} = sub { $warns++; warn $_[0] };
55d729e4 11}
7e35a6a8 12require 'test.pl';
13plan( tests => 19 );
b2b3adea 14
15$wanted_filename = $^O eq 'VMS' ? '0.' : '0';
95e8664e 16$saved_filename = $^O eq 'MacOS' ? ':0' : './0';
7e35a6a8 17
18cmp_ok($warns,'==',0,'no warns at start');
55d729e4 19
95e8664e 20open(FILE,">$saved_filename");
7e35a6a8 21ok(defined(FILE),'created work file');
55d729e4 22print FILE "1\n";
23print FILE "0";
24close(FILE);
25
95e8664e 26open(FILE,"<$saved_filename");
7e35a6a8 27ok(defined(FILE),'opened work file');
55d729e4 28my $seen = 0;
29my $dummy;
30while (my $name = <FILE>)
31 {
32 $seen++ if $name eq '0';
7e35a6a8 33 }
34cmp_ok($seen,'==',1,'seen in while()');
55d729e4 35
36seek(FILE,0,0);
37$seen = 0;
38my $line = '';
7e35a6a8 39do
55d729e4 40 {
41 $seen++ if $line eq '0';
42 } while ($line = <FILE>);
7e35a6a8 43cmp_ok($seen,'==',1,'seen in do/while');
55d729e4 44
45seek(FILE,0,0);
7e35a6a8 46$seen = 0;
47while (($seen ? $dummy : $name) = <FILE> )
55d729e4 48 {
49 $seen++ if $name eq '0';
50 }
7e35a6a8 51cmp_ok($seen,'==',1,'seen in while() ternary');
55d729e4 52
53seek(FILE,0,0);
7e35a6a8 54$seen = 0;
55my %where;
55d729e4 56while ($where{$seen} = <FILE>)
57 {
58 $seen++ if $where{$seen} eq '0';
59 }
7e35a6a8 60cmp_ok($seen,'==',1,'seen in hash while()');
7120fed6 61close FILE;
55d729e4 62
95e8664e 63opendir(DIR,($^O eq 'MacOS' ? ':' : '.'));
7e35a6a8 64ok(defined(DIR),'opened current directory');
55d729e4 65$seen = 0;
66while (my $name = readdir(DIR))
67 {
b2b3adea 68 $seen++ if $name eq $wanted_filename;
7e35a6a8 69 }
70cmp_ok($seen,'==',1,'saw work file once');
55d729e4 71
72rewinddir(DIR);
7e35a6a8 73$seen = 0;
55d729e4 74$dummy = '';
75while (($seen ? $dummy : $name) = readdir(DIR))
76 {
b2b3adea 77 $seen++ if $name eq $wanted_filename;
55d729e4 78 }
7e35a6a8 79cmp_ok($seen,'>',0,'saw file in while() ternary');
55d729e4 80
81rewinddir(DIR);
7e35a6a8 82$seen = 0;
55d729e4 83while ($where{$seen} = readdir(DIR))
84 {
b2b3adea 85 $seen++ if $where{$seen} eq $wanted_filename;
55d729e4 86 }
7e35a6a8 87cmp_ok($seen,'==',1,'saw file in hash while()');
55d729e4 88
89$seen = 0;
90while (my $name = glob('*'))
91 {
b2b3adea 92 $seen++ if $name eq $wanted_filename;
7e35a6a8 93 }
94cmp_ok($seen,'==',1,'saw file in glob while()');
55d729e4 95
7e35a6a8 96$seen = 0;
55d729e4 97$dummy = '';
98while (($seen ? $dummy : $name) = glob('*'))
99 {
b2b3adea 100 $seen++ if $name eq $wanted_filename;
55d729e4 101 }
7e35a6a8 102cmp_ok($seen,'>',0,'saw file in glob hash while() ternary');
55d729e4 103
7e35a6a8 104$seen = 0;
55d729e4 105while ($where{$seen} = glob('*'))
106 {
b2b3adea 107 $seen++ if $where{$seen} eq $wanted_filename;
55d729e4 108 }
7e35a6a8 109cmp_ok($seen,'==',1,'seen in glob hash while()');
55d729e4 110
95e8664e 111unlink($saved_filename);
7e35a6a8 112ok(!(-f $saved_filename),'work file unlinked');
55d729e4 113
114my %hash = (0 => 1, 1 => 2);
115
116$seen = 0;
117while (my $name = each %hash)
118 {
119 $seen++ if $name eq '0';
7e35a6a8 120 }
121cmp_ok($seen,'==',1,'seen in each');
55d729e4 122
7e35a6a8 123$seen = 0;
55d729e4 124$dummy = '';
125while (($seen ? $dummy : $name) = each %hash)
126 {
127 $seen++ if $name eq '0';
128 }
7e35a6a8 129cmp_ok($seen,'==',1,'seen in each ternary');
55d729e4 130
7e35a6a8 131$seen = 0;
55d729e4 132while ($where{$seen} = each %hash)
133 {
134 $seen++ if $where{$seen} eq '0';
135 }
7e35a6a8 136cmp_ok($seen,'==',1,'seen in each hash');
55d729e4 137
7e35a6a8 138cmp_ok($warns,'==',0,'no warns at finish');