This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Test case for C<undef %File::Glob::>
[perl5.git] / t / op / each.t
1 #!./perl
2
3 BEGIN {
4     chdir 't' if -d 't';
5     @INC = '.';
6     push @INC, '../lib';
7 }
8
9 print "1..27\n";
10
11 $h{'abc'} = 'ABC';
12 $h{'def'} = 'DEF';
13 $h{'jkl','mno'} = "JKL\034MNO";
14 $h{'a',2,3,4,5} = join("\034",'A',2,3,4,5);
15 $h{'a'} = 'A';
16 $h{'b'} = 'B';
17 $h{'c'} = 'C';
18 $h{'d'} = 'D';
19 $h{'e'} = 'E';
20 $h{'f'} = 'F';
21 $h{'g'} = 'G';
22 $h{'h'} = 'H';
23 $h{'i'} = 'I';
24 $h{'j'} = 'J';
25 $h{'k'} = 'K';
26 $h{'l'} = 'L';
27 $h{'m'} = 'M';
28 $h{'n'} = 'N';
29 $h{'o'} = 'O';
30 $h{'p'} = 'P';
31 $h{'q'} = 'Q';
32 $h{'r'} = 'R';
33 $h{'s'} = 'S';
34 $h{'t'} = 'T';
35 $h{'u'} = 'U';
36 $h{'v'} = 'V';
37 $h{'w'} = 'W';
38 $h{'x'} = 'X';
39 $h{'y'} = 'Y';
40 $h{'z'} = 'Z';
41
42 @keys = keys %h;
43 @values = values %h;
44
45 if ($#keys == 29 && $#values == 29) {print "ok 1\n";} else {print "not ok 1\n";}
46
47 $i = 0;         # stop -w complaints
48
49 while (($key,$value) = each(%h)) {
50     if ($key eq $keys[$i] && $value eq $values[$i]
51         && (('a' lt 'A' && $key lt $value) || $key gt $value)) {
52         $key =~ y/a-z/A-Z/;
53         $i++ if $key eq $value;
54     }
55 }
56
57 if ($i == 30) {print "ok 2\n";} else {print "not ok 2\n";}
58
59 @keys = ('blurfl', keys(%h), 'dyick');
60 if ($#keys == 31) {print "ok 3\n";} else {print "not ok 3\n";}
61
62 $size = ((split('/',scalar %h))[1]);
63 keys %h = $size * 5;
64 $newsize = ((split('/',scalar %h))[1]);
65 if ($newsize == $size * 8) {print "ok 4\n";} else {print "not ok 4\n";}
66 keys %h = 1;
67 $size = ((split('/',scalar %h))[1]);
68 if ($size == $newsize) {print "ok 5\n";} else {print "not ok 5\n";}
69 %h = (1,1);
70 $size = ((split('/',scalar %h))[1]);
71 if ($size == $newsize) {print "ok 6\n";} else {print "not ok 6\n";}
72 undef %h;
73 %h = (1,1);
74 $size = ((split('/',scalar %h))[1]);
75 if ($size == 8) {print "ok 7\n";} else {print "not ok 7\n";}
76
77 # test scalar each
78 %hash = 1..20;
79 $total = 0;
80 $total += $key while $key = each %hash;
81 print "# Scalar each is bad.\nnot " unless $total == 100;
82 print "ok 8\n";
83
84 for (1..3) { @foo = each %hash }
85 keys %hash;
86 $total = 0;
87 $total += $key while $key = each %hash;
88 print "# Scalar keys isn't resetting the iterator.\nnot " if $total != 100;
89 print "ok 9\n";
90
91 for (1..3) { @foo = each %hash }
92 $total = 0;
93 $total += $key while $key = each %hash;
94 print "# Iterator of each isn't being maintained.\nnot " if $total == 100;
95 print "ok 10\n";
96
97 for (1..3) { @foo = each %hash }
98 values %hash;
99 $total = 0;
100 $total += $key while $key = each %hash;
101 print "# Scalar values isn't resetting the iterator.\nnot " if $total != 100;
102 print "ok 11\n";
103
104 $size = (split('/', scalar %hash))[1];
105 keys(%hash) = $size / 2;
106 print "not " if $size != (split('/', scalar %hash))[1];
107 print "ok 12\n";
108 keys(%hash) = $size + 100;
109 print "not " if $size == (split('/', scalar %hash))[1];
110 print "ok 13\n";
111
112 print "not " if keys(%hash) != 10;
113 print "ok 14\n";
114
115 print keys(hash) != 10 ? "not ok 15\n" : "ok 15\n";
116
117 $i = 0;
118 %h = (a => A, b => B, c=> C, d => D, abc => ABC);
119 @keys = keys(h);
120 @values = values(h);
121 while (($key, $value) = each(h)) {
122         if ($key eq $keys[$i] && $value eq $values[$i] && $key eq lc($value)) {
123                 $i++;
124         }
125 }
126 if ($i == 5) { print "ok 16\n" } else { print "not ok\n" }
127
128 {
129     package Obj;
130     sub DESTROY { print "ok 18\n"; }
131     {
132         my $h = { A => bless [], __PACKAGE__ };
133         while (my($k,$v) = each %$h) {
134             print "ok 17\n" if $k eq 'A' and ref($v) eq 'Obj';
135         }
136     }
137     print "ok 19\n";
138 }
139
140 # Check for Unicode hash keys.
141 %u = ("\x{12}", "f", "\x{123}", "fo", "\x{1234}",  "foo");
142 $u{"\x{12345}"}  = "bar";
143 @u{"\x{123456}"} = "zap";
144
145 foreach (keys %u) {
146     unless (length() == 1) {
147         print "not ";
148         last;
149     }
150 }
151 print "ok 20\n";
152
153 $a = "\xe3\x81\x82"; $A = "\x{3042}";
154 %b = ( $a => "non-utf8");
155 %u = ( $A => "utf8");
156
157 print "not " if exists $b{$A};
158 print "ok 21\n";
159 print "not " if exists $u{$a};
160 print "ok 22\n";
161 print "#$b{$_}\n" for keys %b; # Used to core dump before change #8056.
162 print "ok 23\n";
163 print "#$u{$_}\n" for keys %u; # Used to core dump before change #8056.
164 print "ok 24\n";
165
166 use bytes ();
167
168 # on EBCDIC chars are mapped differently so pick something that needs encoding
169 # there too.
170 $d = pack("U*", 0xe3, 0x81, 0xAF);
171 $ol = bytes::length($d);
172 print "not " unless $ol > 3;
173 print "ok 25\n";
174 %u = ($d => "downgrade");
175 for (keys %u) {
176     use bytes;
177     print "not " if length ne 3 or $_ ne "\xe3\x81\xAF";
178     print "ok 26\n";
179 }
180 {
181     use bytes;
182     print "not " if length($d) != $ol;
183     print "ok 27\n";
184 }