This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
refactoring of flip.t
[perl5.git] / t / op / while_readdir.t
CommitLineData
114c60ec
BG
1#!./perl
2
3BEGIN {
4 chdir 't' if -d 't';
5 @INC = '../lib';
6 require './test.pl';
7}
8
9use strict;
10use warnings;
11
d1c21cec
RGS
12open my $fh, ">", "0" or die "Can't open '0' for writing: $!\n";
13print $fh <<'FILE0';
14This file is here for testing
114c60ec 15
d1c21cec
RGS
16while(readdir $dir){...}
17... while readdir $dir
114c60ec 18
d1c21cec
RGS
19etc
20FILE0
21close $fh;
114c60ec 22
d1c21cec 23plan 10;
114c60ec 24
d1c21cec
RGS
25ok(-f '0', "'0' file is here");
26
27opendir my $dirhandle, '.'
28 or die "Failed test: unable to open directory: $!\n";
29
30my @dir = readdir $dirhandle;
31rewinddir $dirhandle;
114c60ec
BG
32
33{
34 my @list;
35 while(readdir $dirhandle){
36 push @list, $_;
37 }
38 ok( eq_array( \@dir, \@list ), 'while(readdir){push}' );
39 rewinddir $dirhandle;
40}
41
42{
43 my @list;
44 push @list, $_ while readdir $dirhandle;
45 ok( eq_array( \@dir, \@list ), 'push while readdir' );
46 rewinddir $dirhandle;
47}
48
49{
50 my $tmp;
51 my @list;
52 push @list, $tmp while $tmp = readdir $dirhandle;
53 ok( eq_array( \@dir, \@list ), 'push $dir while $dir = readdir' );
54 rewinddir $dirhandle;
55}
56
57{
58 my @list;
59 while( my $dir = readdir $dirhandle){
60 push @list, $dir;
61 }
62 ok( eq_array( \@dir, \@list ), 'while($dir=readdir){push}' );
63 rewinddir $dirhandle;
64}
65
66
67{
68 my @list;
69 my $sub = sub{
70 push @list, $_;
71 };
72 $sub->($_) while readdir $dirhandle;
73 ok( eq_array( \@dir, \@list ), '$sub->($_) while readdir' );
74 rewinddir $dirhandle;
75}
76
d1c21cec
RGS
77{
78 my $works = 0;
79 while(readdir $dirhandle){
80 if( defined $_ && $_ eq '0'){
81 $works = 1;
82 last;
83 }
114c60ec 84 }
d1c21cec
RGS
85 ok( $works, 'while(readdir){} with file named "0"' );
86 rewinddir $dirhandle;
87}
114c60ec 88
d1c21cec
RGS
89{
90 my $works = 0;
91 my $sub = sub{
92 if( defined $_ && $_ eq '0' ){
93 $works = 1;
94 }
95 };
96 $sub->($_) while readdir $dirhandle;
97 ok( $works, '$sub->($_) while readdir; with file named "0"' );
98 rewinddir $dirhandle;
99}
100
101{
102 my $works = 0;
103 while( my $dir = readdir $dirhandle ){
104 if( defined $dir && $dir eq '0'){
105 $works = 1;
106 last;
107 }
114c60ec 108 }
d1c21cec
RGS
109 ok( $works, 'while($dir=readdir){} with file named "0"');
110 rewinddir $dirhandle;
111}
114c60ec 112
d1c21cec
RGS
113{
114 my $tmp;
115 my $ok;
116 my @list;
117 defined($tmp)&& !$tmp && ($ok=1) while $tmp = readdir $dirhandle;
118 ok( $ok, '$dir while $dir = readdir; with file named "0"' );
119 rewinddir $dirhandle;
114c60ec
BG
120}
121
122closedir $dirhandle;
d1c21cec
RGS
123
124END { 1 while unlink "0" }