13 $h{'jkl','mno'} = "JKL\034MNO";
14 $h{'a',2,3,4,5} = join("\034",'A',2,3,4,5);
45 is ($#keys, 29, "keys");
46 is ($#values, 29, "values");
48 $i = 0; # stop -w complaints
50 while (($key,$value) = each(%h)) {
51 if ($key eq $keys[$i] && $value eq $values[$i]
52 && (('a' lt 'A' && $key lt $value) || $key gt $value)) {
54 $i++ if $key eq $value;
58 is ($i, 30, "each count");
60 @keys = ('blurfl', keys(%h), 'dyick');
61 is ($#keys, 31, "added a key");
63 $size = ((split('/',scalar %h))[1]);
65 $newsize = ((split('/',scalar %h))[1]);
66 is ($newsize, $size * 8, "resize");
68 $size = ((split('/',scalar %h))[1]);
69 is ($size, $newsize, "same size");
71 $size = ((split('/',scalar %h))[1]);
72 is ($size, $newsize, "still same size");
75 $size = ((split('/',scalar %h))[1]);
76 is ($size, 8, "size 8");
81 $total += $key while $key = each %hash;
82 is ($total, 100, "test scalar each");
84 for (1..3) { @foo = each %hash }
87 $total += $key while $key = each %hash;
88 is ($total, 100, "test scalar keys resets iterator");
90 for (1..3) { @foo = each %hash }
92 $total += $key while $key = each %hash;
93 isnt ($total, 100, "test iterator of each is being maintained");
95 for (1..3) { @foo = each %hash }
98 $total += $key while $key = each %hash;
99 is ($total, 100, "test values keys resets iterator");
101 $size = (split('/', scalar %hash))[1];
102 keys(%hash) = $size / 2;
103 is ($size, (split('/', scalar %hash))[1]);
104 keys(%hash) = $size + 100;
105 isnt ($size, (split('/', scalar %hash))[1]);
107 is (keys(%hash), 10, "keys (%hash)");
109 @tests = (&next_test, &next_test, &next_test);
112 sub DESTROY { print "ok $::tests[1] # DESTROY called\n"; }
114 my $h = { A => bless [], __PACKAGE__ };
115 while (my($k,$v) = each %$h) {
116 print "ok $::tests[0]\n" if $k eq 'A' and ref($v) eq 'Obj';
119 print "ok $::tests[2]\n";
122 # Check for Unicode hash keys.
123 %u = ("\x{12}", "f", "\x{123}", "fo", "\x{1234}", "foo");
124 $u{"\x{12345}"} = "bar";
125 @u{"\x{10FFFD}"} = "zap";
129 is (length(), 1, "Check length of " . _qq $_);
132 ok (eq_hash(\%u, \%u2), "copied unicode hash keys correctly?");
134 $a = "\xe3\x81\x82"; $A = "\x{3042}";
135 %b = ( $a => "non-utf8");
136 %u = ( $A => "utf8");
138 is (exists $b{$A}, '', "utf8 key in bytes hash");
139 is (exists $u{$a}, '', "bytes key in utf8 hash");
140 print "# $b{$_}\n" for keys %b; # Used to core dump before change #8056.
141 pass ("if we got here change 8056 worked");
142 print "# $u{$_}\n" for keys %u; # Used to core dump before change #8056.
143 pass ("change 8056 is thanks to Inaba Hiroto");
145 # on EBCDIC chars are mapped differently so pick something that needs encoding
147 $d = pack("U*", 0xe3, 0x81, 0xAF);
148 { use bytes; $ol = bytes::length($d) }
149 cmp_ok ($ol, '>', 3, "check encoding on EBCDIC");
150 %u = ($d => "downgrade");
152 is (length, 3, "check length");
153 is ($_, pack("U*", 0xe3, 0x81, 0xAF), "check value");
156 { use bytes; is (bytes::length($d), $ol) }
161 my $u0 = pack("U0U", 0x00FF);
162 my $b0 = "\xC3\xBF"; # 0xCB 0xBF is U+00FF in UTF-8
163 my $u1 = pack("U0U", 0x0100);
164 my $b1 = "\xC4\x80"; # 0xC4 0x80 is U+0100 in UTF-8
171 is(scalar keys %u, 4, "four different Unicode keys");
172 is($u{$u0}, 1, "U+00FF -> 1");
173 is($u{$b0}, 2, "U+00C3 U+00BF -> 2");
174 is($u{$u1}, 3, "U+0100 -> 3 ");
175 is($u{$b1}, 4, "U+00C4 U+0080 -> 4");
178 # test for syntax errors
179 for my $k (qw(each keys values)) {
181 like($@, qr/^Not enough arguments for $k/, "$k demands argument");
188 my ($k1,$v1)=each(%foo);
191 my ($k2,$v2)=each(%foo);
193 while (each(%foo)) {$rest++};
194 is($yes,1,"if(%foo) was true");
195 isnt($k1,$k2,"if(%foo) didnt mess with each (key)");
196 isnt($v1,$v2,"if(%foo) didnt mess with each (value)");
197 is($rest,3,"Got the expect number of keys");
199 like($hsv,qr[/],"Got bucket stats from %foo in scalar assignment context");
201 is(@arr,10,"Got expected number of elements in list context");
207 my ($k1,$v1)=each(%foo);
210 my ($k2,$v2)=each(%foo);
212 while (each(%foo)) {$rest++};
213 is($yes,1,"if(%foo) was true");
214 isnt($k1,$k2,"if(%foo) didnt mess with each (key)");
215 isnt($v1,$v2,"if(%foo) didnt mess with each (value)");
216 is($rest,3,"Got the expect number of keys");
218 like($hsv,qr[/],"Got bucket stats from %foo in scalar assignment context");
220 is(@arr,10,"Got expected number of elements in list context");
223 # make sure a deleted active iterator gets freed timely, even if the
224 # hash is otherwise empty
229 sub DESTROY { $c++ };
232 my %h = ("a" => bless []);
233 my ($k,$v) = each %h;
235 ::is($c, 0, "single key not yet freed");
237 ::is($c, 1, "single key now freed");
241 # Make sure each() does not leave the iterator in an inconsistent state
242 # (RITER set to >= 0, with EITER null) if the active iterator is
243 # deleted, leaving the hash apparently empty.
250 is join ("-", each %h), '1-2',
251 'each on apparently empty hash does not leave RITER set';
255 local $SIG{__WARN__}= sub {
256 /\QUse of each() on hash after insertion without resetting hash iterator results in undefined behavior\E/
257 and $warned++ for @_;
259 my %h= map { $_ => $_ } "A".."F";
260 while (my ($k, $v)= each %h) {
263 ok($warned,"each() after insert produces warnings");
264 no warnings 'internal';
266 %h= map { $_ => $_ } "A".."F";
267 while (my ($k, $v)= each %h) {
270 ok(!$warned, "no warnings 'internal' silences each() after insert warnings");