This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Get t/uni/cache.t working under minitest
[perl5.git] / t / op / each.t
1 #!./perl
2
3 BEGIN {
4     chdir 't' if -d 't';
5     @INC = '../lib';
6     require './test.pl';
7 }
8
9 plan tests => 57;
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 is ($#keys, 29, "keys");
46 is ($#values, 29, "values");
47
48 $i = 0;         # stop -w complaints
49
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)) {
53         $key =~ y/a-z/A-Z/;
54         $i++ if $key eq $value;
55     }
56 }
57
58 is ($i, 30, "each count");
59
60 @keys = ('blurfl', keys(%h), 'dyick');
61 is ($#keys, 31, "added a key");
62
63 $size = ((split('/',scalar %h))[1]);
64 keys %h = $size * 5;
65 $newsize = ((split('/',scalar %h))[1]);
66 is ($newsize, $size * 8, "resize");
67 keys %h = 1;
68 $size = ((split('/',scalar %h))[1]);
69 is ($size, $newsize, "same size");
70 %h = (1,1);
71 $size = ((split('/',scalar %h))[1]);
72 is ($size, $newsize, "still same size");
73 undef %h;
74 %h = (1,1);
75 $size = ((split('/',scalar %h))[1]);
76 is ($size, 8, "size 8");
77
78 # test scalar each
79 %hash = 1..20;
80 $total = 0;
81 $total += $key while $key = each %hash;
82 is ($total, 100, "test scalar each");
83
84 for (1..3) { @foo = each %hash }
85 keys %hash;
86 $total = 0;
87 $total += $key while $key = each %hash;
88 is ($total, 100, "test scalar keys resets iterator");
89
90 for (1..3) { @foo = each %hash }
91 $total = 0;
92 $total += $key while $key = each %hash;
93 isnt ($total, 100, "test iterator of each is being maintained");
94
95 for (1..3) { @foo = each %hash }
96 values %hash;
97 $total = 0;
98 $total += $key while $key = each %hash;
99 is ($total, 100, "test values keys resets iterator");
100
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]);
106
107 is (keys(%hash), 10, "keys (%hash)");
108
109 @tests = (&next_test, &next_test, &next_test);
110 {
111     package Obj;
112     sub DESTROY { print "ok $::tests[1] # DESTROY called\n"; }
113     {
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';
117         }
118     }
119     print "ok $::tests[2]\n";
120 }
121
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";
126
127 my %u2;
128 foreach (keys %u) {
129     is (length(), 1, "Check length of " . _qq $_);
130     $u2{$_} = $u{$_};
131 }
132 ok (eq_hash(\%u, \%u2), "copied unicode hash keys correctly?");
133
134 $a = "\xe3\x81\x82"; $A = "\x{3042}";
135 %b = ( $a => "non-utf8");
136 %u = ( $A => "utf8");
137
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");
144
145 # on EBCDIC chars are mapped differently so pick something that needs encoding
146 # there too.
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");
151 for (keys %u) {
152     is (length, 3, "check length"); 
153     is ($_, pack("U*", 0xe3, 0x81, 0xAF), "check value");
154 }
155 {
156     { use bytes; is (bytes::length($d), $ol) }
157 }
158
159 {
160     my %u;
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
165
166     $u{$u0} = 1;
167     $u{$b0} = 2; 
168     $u{$u1} = 3;
169     $u{$b1} = 4;
170
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");
176 }
177
178 # test for syntax errors
179 for my $k (qw(each keys values)) {
180     eval $k;
181     like($@, qr/^Not enough arguments for $k/, "$k demands argument");
182 }
183
184 {
185     my %foo=(1..10);
186     my ($k,$v);
187     my $count=keys %foo;
188     my ($k1,$v1)=each(%foo);
189     my $yes = 0;
190     if (%foo) { $yes++ }
191     my ($k2,$v2)=each(%foo);
192     my $rest=0;
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");
198     my $hsv=1 && %foo;
199     like($hsv,qr[/],"Got bucket stats from %foo in scalar assignment context");
200     my @arr=%foo&&%foo;
201     is(@arr,10,"Got expected number of elements in list context");
202 }    
203 {
204     our %foo=(1..10);
205     my ($k,$v);
206     my $count=keys %foo;
207     my ($k1,$v1)=each(%foo);
208     my $yes = 0;
209     if (%foo) { $yes++ }
210     my ($k2,$v2)=each(%foo);
211     my $rest=0;
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");
217     my $hsv=1 && %foo;
218     like($hsv,qr[/],"Got bucket stats from %foo in scalar assignment context");
219     my @arr=%foo&&%foo;
220     is(@arr,10,"Got expected number of elements in list context");
221 }    
222 {
223     # make sure a deleted active iterator gets freed timely, even if the
224     # hash is otherwise empty
225
226     package Single;
227
228     my $c = 0;
229     sub DESTROY { $c++ };
230
231     {
232         my %h = ("a" => bless []);
233         my ($k,$v) = each %h;
234         delete $h{$k};
235         ::is($c, 0, "single key not yet freed");
236     }
237     ::is($c, 1, "single key now freed");
238 }
239
240 {
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.
244     my %h;
245     $h{1} = 2;
246     each %h;
247     delete $h{1};
248     each %h;
249     $h{1}=2;
250     is join ("-", each %h), '1-2',
251         'each on apparently empty hash does not leave RITER set';
252 }
253 {
254     my $warned= 0;
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 @_;
258     };
259     my %h= map { $_ => $_ } "A".."F";
260     while (my ($k, $v)= each %h) {
261         $h{"$k$k"}= $v;
262     }
263     ok($warned,"each() after insert produces warnings");
264     no warnings 'internal';
265     $warned= 0;
266     %h= map { $_ => $_ } "A".."F";
267     while (my ($k, $v)= each %h) {
268         $h{"$k$k"}= $v;
269     }
270     ok(!$warned, "no warnings 'internal' silences each() after insert warnings");
271 }