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