Commit | Line | Data |
---|---|---|
8d063cd8 LW |
1 | #!./perl |
2 | ||
f9a63242 JH |
3 | BEGIN { |
4 | chdir 't' if -d 't'; | |
677fb045 NC |
5 | @INC = '../lib'; |
6 | require './test.pl'; | |
c4d5f83a | 7 | } |
f9a63242 | 8 | |
00a1a643 | 9 | plan tests => 56; |
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 | 44 | |
59e20782 NC |
45 | is ($#keys, 29, "keys"); |
46 | is ($#values, 29, "values"); | |
8d063cd8 | 47 | |
75039078 | 48 | $i = 0; # stop -w complaints |
49 | ||
50 | while (($key,$value) = each(%h)) { | |
9d116dd7 JH |
51 | if ($key eq $keys[$i] && $value eq $values[$i] |
52 | && (('a' lt 'A' && $key lt $value) || $key gt $value)) { | |
8d063cd8 LW |
53 | $key =~ y/a-z/A-Z/; |
54 | $i++ if $key eq $value; | |
55 | } | |
56 | } | |
57 | ||
59e20782 | 58 | is ($i, 30, "each count"); |
378cc40b | 59 | |
a687059c | 60 | @keys = ('blurfl', keys(%h), 'dyick'); |
59e20782 | 61 | is ($#keys, 31, "added a key"); |
75039078 | 62 | |
63 | $size = ((split('/',scalar %h))[1]); | |
64 | keys %h = $size * 5; | |
65 | $newsize = ((split('/',scalar %h))[1]); | |
59e20782 | 66 | is ($newsize, $size * 8, "resize"); |
75039078 | 67 | keys %h = 1; |
68 | $size = ((split('/',scalar %h))[1]); | |
59e20782 | 69 | is ($size, $newsize, "same size"); |
75039078 | 70 | %h = (1,1); |
71 | $size = ((split('/',scalar %h))[1]); | |
59e20782 | 72 | is ($size, $newsize, "still same size"); |
75039078 | 73 | undef %h; |
74 | %h = (1,1); | |
75 | $size = ((split('/',scalar %h))[1]); | |
59e20782 | 76 | is ($size, 8, "size 8"); |
3524d3b9 TP |
77 | |
78 | # test scalar each | |
79 | %hash = 1..20; | |
80 | $total = 0; | |
81 | $total += $key while $key = each %hash; | |
59e20782 | 82 | is ($total, 100, "test scalar each"); |
3524d3b9 TP |
83 | |
84 | for (1..3) { @foo = each %hash } | |
85 | keys %hash; | |
86 | $total = 0; | |
87 | $total += $key while $key = each %hash; | |
59e20782 | 88 | is ($total, 100, "test scalar keys resets iterator"); |
3524d3b9 TP |
89 | |
90 | for (1..3) { @foo = each %hash } | |
91 | $total = 0; | |
92 | $total += $key while $key = each %hash; | |
59e20782 | 93 | isnt ($total, 100, "test iterator of each is being maintained"); |
3524d3b9 TP |
94 | |
95 | for (1..3) { @foo = each %hash } | |
96 | values %hash; | |
97 | $total = 0; | |
98 | $total += $key while $key = each %hash; | |
59e20782 | 99 | is ($total, 100, "test values keys resets iterator"); |
3524d3b9 TP |
100 | |
101 | $size = (split('/', scalar %hash))[1]; | |
102 | keys(%hash) = $size / 2; | |
59e20782 | 103 | is ($size, (split('/', scalar %hash))[1]); |
3524d3b9 | 104 | keys(%hash) = $size + 100; |
59e20782 | 105 | isnt ($size, (split('/', scalar %hash))[1]); |
3524d3b9 | 106 | |
59e20782 | 107 | is (keys(%hash), 10, "keys (%hash)"); |
3524d3b9 | 108 | |
eb81740c NC |
109 | { |
110 | no warnings 'deprecated'; | |
111 | is (keys(hash), 10, "keys (hash)"); | |
112 | } | |
c6aa4a32 SP |
113 | |
114 | $i = 0; | |
115 | %h = (a => A, b => B, c=> C, d => D, abc => ABC); | |
eb81740c NC |
116 | { |
117 | no warnings 'deprecated'; | |
118 | @keys = keys(h); | |
119 | @values = values(h); | |
120 | while (($key, $value) = each(h)) { | |
c6aa4a32 SP |
121 | if ($key eq $keys[$i] && $value eq $values[$i] && $key eq lc($value)) { |
122 | $i++; | |
123 | } | |
eb81740c | 124 | } |
c6aa4a32 | 125 | } |
59e20782 | 126 | is ($i, 5); |
59af0135 | 127 | |
677fb045 | 128 | @tests = (&next_test, &next_test, &next_test); |
59af0135 GS |
129 | { |
130 | package Obj; | |
677fb045 | 131 | sub DESTROY { print "ok $::tests[1] # DESTROY called\n"; } |
59af0135 GS |
132 | { |
133 | my $h = { A => bless [], __PACKAGE__ }; | |
134 | while (my($k,$v) = each %$h) { | |
677fb045 | 135 | print "ok $::tests[0]\n" if $k eq 'A' and ref($v) eq 'Obj'; |
59af0135 GS |
136 | } |
137 | } | |
677fb045 | 138 | print "ok $::tests[2]\n"; |
59af0135 GS |
139 | } |
140 | ||
f2b0cce7 JH |
141 | # Check for Unicode hash keys. |
142 | %u = ("\x{12}", "f", "\x{123}", "fo", "\x{1234}", "foo"); | |
143 | $u{"\x{12345}"} = "bar"; | |
b851fbc1 | 144 | @u{"\x{10FFFD}"} = "zap"; |
f2b0cce7 | 145 | |
677fb045 | 146 | my %u2; |
f2b0cce7 | 147 | foreach (keys %u) { |
59e20782 | 148 | is (length(), 1, "Check length of " . _qq $_); |
677fb045 | 149 | $u2{$_} = $u{$_}; |
f2b0cce7 | 150 | } |
677fb045 | 151 | ok (eq_hash(\%u, \%u2), "copied unicode hash keys correctly?"); |
ca9dc00c IH |
152 | |
153 | $a = "\xe3\x81\x82"; $A = "\x{3042}"; | |
154 | %b = ( $a => "non-utf8"); | |
155 | %u = ( $A => "utf8"); | |
156 | ||
59e20782 NC |
157 | is (exists $b{$A}, '', "utf8 key in bytes hash"); |
158 | is (exists $u{$a}, '', "bytes key in utf8 hash"); | |
169da838 | 159 | print "# $b{$_}\n" for keys %b; # Used to core dump before change #8056. |
677fb045 | 160 | pass ("if we got here change 8056 worked"); |
169da838 | 161 | print "# $u{$_}\n" for keys %u; # Used to core dump before change #8056. |
677fb045 | 162 | pass ("change 8056 is thanks to Inaba Hiroto"); |
f9a63242 | 163 | |
ffbc6a93 JH |
164 | # on EBCDIC chars are mapped differently so pick something that needs encoding |
165 | # there too. | |
166 | $d = pack("U*", 0xe3, 0x81, 0xAF); | |
169da838 | 167 | { use bytes; $ol = bytes::length($d) } |
59e20782 | 168 | cmp_ok ($ol, '>', 3, "check encoding on EBCDIC"); |
ef9edfd0 | 169 | %u = ($d => "downgrade"); |
f9a63242 | 170 | for (keys %u) { |
59e20782 | 171 | is (length, 3, "check length"); |
9caa78f3 | 172 | is ($_, pack("U*", 0xe3, 0x81, 0xAF), "check value"); |
f9a63242 | 173 | } |
ef9edfd0 | 174 | { |
59e20782 | 175 | { use bytes; is (bytes::length($d), $ol) } |
ef9edfd0 | 176 | } |
3ea3bee8 JH |
177 | |
178 | { | |
179 | my %u; | |
180 | my $u0 = pack("U0U", 0x00FF); | |
181 | my $b0 = "\xC3\xBF"; # 0xCB 0xBF is U+00FF in UTF-8 | |
182 | my $u1 = pack("U0U", 0x0100); | |
183 | my $b1 = "\xC4\x80"; # 0xC4 0x80 is U+0100 in UTF-8 | |
184 | ||
185 | $u{$u0} = 1; | |
186 | $u{$b0} = 2; | |
187 | $u{$u1} = 3; | |
188 | $u{$b1} = 4; | |
189 | ||
190 | is(scalar keys %u, 4, "four different Unicode keys"); | |
191 | is($u{$u0}, 1, "U+00FF -> 1"); | |
192 | is($u{$b0}, 2, "U+00C3 U+00BF -> 2"); | |
193 | is($u{$u1}, 3, "U+0100 -> 3 "); | |
194 | is($u{$b1}, 4, "U+00C4 U+0080 -> 4"); | |
195 | } | |
a916b302 RGS |
196 | |
197 | # test for syntax errors | |
198 | for my $k (qw(each keys values)) { | |
199 | eval $k; | |
200 | like($@, qr/^Not enough arguments for $k/, "$k demands argument"); | |
201 | } | |
867fa1e2 YO |
202 | |
203 | { | |
204 | my %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,'/',"Got bucket stats from %foo in scalar assignment context"); | |
aaf643ce DL |
219 | my @arr=%foo&&%foo; |
220 | is(@arr,10,"Got expected number of elements in list context"); | |
867fa1e2 YO |
221 | } |
222 | { | |
223 | our %foo=(1..10); | |
224 | my ($k,$v); | |
225 | my $count=keys %foo; | |
226 | my ($k1,$v1)=each(%foo); | |
227 | my $yes = 0; | |
228 | if (%foo) { $yes++ } | |
229 | my ($k2,$v2)=each(%foo); | |
230 | my $rest=0; | |
231 | while (each(%foo)) {$rest++}; | |
232 | is($yes,1,"if(%foo) was true"); | |
233 | isnt($k1,$k2,"if(%foo) didnt mess with each (key)"); | |
234 | isnt($v1,$v2,"if(%foo) didnt mess with each (value)"); | |
235 | is($rest,3,"Got the expect number of keys"); | |
236 | my $hsv=1 && %foo; | |
237 | like($hsv,'/',"Got bucket stats from %foo in scalar assignment context"); | |
aaf643ce DL |
238 | my @arr=%foo&&%foo; |
239 | is(@arr,10,"Got expected number of elements in list context"); | |
867fa1e2 | 240 | } |
00a1a643 DM |
241 | { |
242 | # make sure a deleted active iterator gets freed timely, even if the | |
243 | # hash is otherwise empty | |
244 | ||
245 | package Single; | |
246 | ||
247 | my $c = 0; | |
248 | sub DESTROY { $c++ }; | |
249 | ||
250 | { | |
251 | my %h = ("a" => bless []); | |
252 | my ($k,$v) = each %h; | |
253 | delete $h{$k}; | |
254 | ::is($c, 0, "single key not yet freed"); | |
255 | } | |
256 | ::is($c, 1, "single key now freed"); | |
257 | } |