14 $h{'jkl','mno'} = "JKL\034MNO";
15 $h{'a',2,3,4,5} = join("\034",'A',2,3,4,5);
46 is ($#keys, 29, "keys");
47 is ($#values, 29, "values");
49 $i = 0; # stop -w complaints
51 while (($key,$value) = each(%h)) {
52 if ($key eq $keys[$i] && $value eq $values[$i]
53 && (('a' lt 'A' && $key lt $value) || $key gt $value)) {
55 $i++ if $key eq $value;
59 is ($i, 30, "each count");
61 @keys = ('blurfl', keys(%h), 'dyick');
62 is ($#keys, 31, "added a key");
64 $size = Hash::Util::num_buckets(%h);
66 $newsize = Hash::Util::num_buckets(%h);
67 is ($newsize, $size * 8, "resize");
69 $size = Hash::Util::num_buckets(%h);
70 is ($size, $newsize, "same size");
72 $size = Hash::Util::num_buckets(%h);
73 is ($size, $newsize, "still same size");
76 $size = Hash::Util::num_buckets(%h);
77 is ($size, 8, "size 8");
82 $total += $key while $key = each %hash;
83 is ($total, 100, "test scalar each");
85 for (1..3) { @foo = each %hash }
88 $total += $key while $key = each %hash;
89 is ($total, 100, "test scalar keys resets iterator");
91 for (1..3) { @foo = each %hash }
93 $total += $key while $key = each %hash;
94 isnt ($total, 100, "test iterator of each is being maintained");
96 for (1..3) { @foo = each %hash }
99 $total += $key while $key = each %hash;
100 is ($total, 100, "test values keys resets iterator");
102 $size = Hash::Util::num_buckets(%hash);
103 keys(%hash) = $size / 2;
104 is ($size, Hash::Util::num_buckets(%hash),
105 "assign to keys does not shrink hash bucket array");
106 keys(%hash) = $size + 100;
107 isnt ($size, Hash::Util::num_buckets(%hash),
108 "assignment to keys of a number not large enough does not change size");
110 is (keys(%hash), 10, "keys (%hash)");
112 @tests = (&next_test, &next_test, &next_test);
115 sub DESTROY { print "ok $::tests[1] # DESTROY called\n"; }
117 my $h = { A => bless [], __PACKAGE__ };
118 while (my($k,$v) = each %$h) {
119 print "ok $::tests[0]\n" if $k eq 'A' and ref($v) eq 'Obj';
122 print "ok $::tests[2]\n";
125 # Check for Unicode hash keys.
126 %u = ("\x{12}", "f", "\x{123}", "fo", "\x{1234}", "foo");
127 $u{"\x{12345}"} = "bar";
128 @u{"\x{10FFFD}"} = "zap";
132 is (length(), 1, "Check length of " . _qq $_);
135 ok (eq_hash(\%u, \%u2), "copied unicode hash keys correctly?");
137 $a = "\xe3\x81\x82"; $A = "\x{3042}";
138 %b = ( $a => "non-utf8");
139 %u = ( $A => "utf8");
141 is (exists $b{$A}, '', "utf8 key in bytes hash");
142 is (exists $u{$a}, '', "bytes key in utf8 hash");
143 print "# $b{$_}\n" for keys %b; # Used to core dump before change #8056.
144 pass ("if we got here change 8056 worked");
145 print "# $u{$_}\n" for keys %u; # Used to core dump before change #8056.
146 pass ("change 8056 is thanks to Inaba Hiroto");
148 # on EBCDIC chars are mapped differently so pick something that needs encoding
150 $d = pack("U*", 0xe3, 0x81, 0xAF);
151 { use bytes; $ol = bytes::length($d) }
152 cmp_ok ($ol, '>', 3, "check encoding on EBCDIC");
153 %u = ($d => "downgrade");
155 is (length, 3, "check length");
156 is ($_, pack("U*", 0xe3, 0x81, 0xAF), "check value");
159 { use bytes; is (bytes::length($d), $ol) }
164 my $u0 = pack("U0U", 0x00FF);
165 my $b0 = "\xC3\xBF"; # 0xCB 0xBF is U+00FF in UTF-8
166 my $u1 = pack("U0U", 0x0100);
167 my $b1 = "\xC4\x80"; # 0xC4 0x80 is U+0100 in UTF-8
174 is(scalar keys %u, 4, "four different Unicode keys");
175 is($u{$u0}, 1, "U+00FF -> 1");
176 is($u{$b0}, 2, "U+00C3 U+00BF -> 2");
177 is($u{$u1}, 3, "U+0100 -> 3 ");
178 is($u{$b1}, 4, "U+00C4 U+0080 -> 4");
181 # test for syntax errors
182 for my $k (qw(each keys values)) {
184 like($@, qr/^Not enough arguments for $k/, "$k demands argument");
191 my ($k1,$v1)=each(%foo);
194 my ($k2,$v2)=each(%foo);
196 while (each(%foo)) {$rest++};
197 is($yes,1,"if(%foo) was true - my");
198 isnt($k1,$k2,"if(%foo) didnt mess with each (key) - my");
199 isnt($v1,$v2,"if(%foo) didnt mess with each (value) - my");
200 is($rest,3,"Got the expected number of keys - my");
202 is($hsv,$count,"Got the count of keys from %foo in scalar assignment context - my");
204 is(@arr,10,"Got expected number of elements in list context - my");
210 my ($k1,$v1)=each(%foo);
213 my ($k2,$v2)=each(%foo);
215 while (each(%foo)) {$rest++};
216 is($yes,1,"if(%foo) was true - our");
217 isnt($k1,$k2,"if(%foo) didnt mess with each (key) - our");
218 isnt($v1,$v2,"if(%foo) didnt mess with each (value) - our");
219 is($rest,3,"Got the expected number of keys - our");
221 is($hsv,$count,"Got the count of keys from %foo in scalar assignment context - our");
223 is(@arr,10,"Got expected number of elements in list context - our");
226 # make sure a deleted active iterator gets freed timely, even if the
227 # hash is otherwise empty
232 sub DESTROY { $c++ };
235 my %h = ("a" => bless []);
236 my ($k,$v) = each %h;
238 ::is($c, 0, "single key not yet freed");
240 ::is($c, 1, "single key now freed");
244 # Make sure each() does not leave the iterator in an inconsistent state
245 # (RITER set to >= 0, with EITER null) if the active iterator is
246 # deleted, leaving the hash apparently empty.
253 is join ("-", each %h), '1-2',
254 'each on apparently empty hash does not leave RITER set';
258 local $SIG{__WARN__}= sub {
259 /\QUse of each() on hash after insertion without resetting hash iterator results in undefined behavior\E/
260 and $warned++ for @_;
262 my %h= map { $_ => $_ } "A".."F";
263 while (my ($k, $v)= each %h) {
266 ok($warned,"each() after insert produces warnings");
267 no warnings 'internal';
269 %h= map { $_ => $_ } "A".."F";
270 while (my ($k, $v)= each %h) {
273 ok(!$warned, "no warnings 'internal' silences each() after insert warnings");
276 use feature 'refaliasing';
277 no warnings 'experimental::refaliasing';
280 ($a, $b) = (each %h2);
281 is "$a $b", "f 7", 'each in list assignment';
283 ($a, $b) = (3, values %h2);
284 is "$a $b", "3 7", 'values in list assignment';