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
CommitLineData
8d063cd8
LW
1#!./perl
2
f9a63242
JH
3BEGIN {
4 chdir 't' if -d 't';
c4d5f83a 5 @INC = '.';
f9a63242 6 push @INC, '../lib';
c4d5f83a 7}
f9a63242 8
c4d5f83a 9print "1..27\n";
8d063cd8
LW
10
11$h{'abc'} = 'ABC';
12$h{'def'} = 'DEF';
a687059c
LW
13$h{'jkl','mno'} = "JKL\034MNO";
14$h{'a',2,3,4,5} = join("\034",'A',2,3,4,5);
8d063cd8
LW
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
a687059c
LW
42@keys = keys %h;
43@values = values %h;
8d063cd8
LW
44
45if ($#keys == 29 && $#values == 29) {print "ok 1\n";} else {print "not ok 1\n";}
46
75039078 47$i = 0; # stop -w complaints
48
49while (($key,$value) = each(%h)) {
9d116dd7
JH
50 if ($key eq $keys[$i] && $value eq $values[$i]
51 && (('a' lt 'A' && $key lt $value) || $key gt $value)) {
8d063cd8
LW
52 $key =~ y/a-z/A-Z/;
53 $i++ if $key eq $value;
54 }
55}
56
57if ($i == 30) {print "ok 2\n";} else {print "not ok 2\n";}
378cc40b 58
a687059c 59@keys = ('blurfl', keys(%h), 'dyick');
378cc40b 60if ($#keys == 31) {print "ok 3\n";} else {print "not ok 3\n";}
75039078 61
62$size = ((split('/',scalar %h))[1]);
63keys %h = $size * 5;
64$newsize = ((split('/',scalar %h))[1]);
65if ($newsize == $size * 8) {print "ok 4\n";} else {print "not ok 4\n";}
66keys %h = 1;
67$size = ((split('/',scalar %h))[1]);
68if ($size == $newsize) {print "ok 5\n";} else {print "not ok 5\n";}
69%h = (1,1);
70$size = ((split('/',scalar %h))[1]);
71if ($size == $newsize) {print "ok 6\n";} else {print "not ok 6\n";}
72undef %h;
73%h = (1,1);
74$size = ((split('/',scalar %h))[1]);
75if ($size == 8) {print "ok 7\n";} else {print "not ok 7\n";}
3524d3b9
TP
76
77# test scalar each
78%hash = 1..20;
79$total = 0;
80$total += $key while $key = each %hash;
81print "# Scalar each is bad.\nnot " unless $total == 100;
82print "ok 8\n";
83
84for (1..3) { @foo = each %hash }
85keys %hash;
86$total = 0;
87$total += $key while $key = each %hash;
88print "# Scalar keys isn't resetting the iterator.\nnot " if $total != 100;
89print "ok 9\n";
90
91for (1..3) { @foo = each %hash }
92$total = 0;
93$total += $key while $key = each %hash;
94print "# Iterator of each isn't being maintained.\nnot " if $total == 100;
95print "ok 10\n";
96
97for (1..3) { @foo = each %hash }
98values %hash;
99$total = 0;
100$total += $key while $key = each %hash;
101print "# Scalar values isn't resetting the iterator.\nnot " if $total != 100;
102print "ok 11\n";
103
104$size = (split('/', scalar %hash))[1];
105keys(%hash) = $size / 2;
106print "not " if $size != (split('/', scalar %hash))[1];
107print "ok 12\n";
108keys(%hash) = $size + 100;
109print "not " if $size == (split('/', scalar %hash))[1];
110print "ok 13\n";
111
112print "not " if keys(%hash) != 10;
113print "ok 14\n";
114
c6aa4a32
SP
115print 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);
121while (($key, $value) = each(h)) {
122 if ($key eq $keys[$i] && $value eq $values[$i] && $key eq lc($value)) {
123 $i++;
124 }
125}
126if ($i == 5) { print "ok 16\n" } else { print "not ok\n" }
59af0135
GS
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
f2b0cce7
JH
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
145foreach (keys %u) {
146 unless (length() == 1) {
147 print "not ";
148 last;
149 }
150}
151print "ok 20\n";
ca9dc00c
IH
152
153$a = "\xe3\x81\x82"; $A = "\x{3042}";
154%b = ( $a => "non-utf8");
155%u = ( $A => "utf8");
156
157print "not " if exists $b{$A};
158print "ok 21\n";
159print "not " if exists $u{$a};
160print "ok 22\n";
161print "#$b{$_}\n" for keys %b; # Used to core dump before change #8056.
162print "ok 23\n";
163print "#$u{$_}\n" for keys %u; # Used to core dump before change #8056.
164print "ok 24\n";
f9a63242 165
c4d5f83a
NIS
166use bytes ();
167
ffbc6a93
JH
168# on EBCDIC chars are mapped differently so pick something that needs encoding
169# there too.
170$d = pack("U*", 0xe3, 0x81, 0xAF);
c4d5f83a
NIS
171$ol = bytes::length($d);
172print "not " unless $ol > 3;
173print "ok 25\n";
ef9edfd0 174%u = ($d => "downgrade");
f9a63242
JH
175for (keys %u) {
176 use bytes;
ffbc6a93 177 print "not " if length ne 3 or $_ ne "\xe3\x81\xAF";
c4d5f83a 178 print "ok 26\n";
f9a63242 179}
ef9edfd0
JH
180{
181 use bytes;
c4d5f83a
NIS
182 print "not " if length($d) != $ol;
183 print "ok 27\n";
ef9edfd0 184}