Precomputing the hash value for a string representable in bytes, but passed in
[p5sagit/p5-mst-13.2.git] / ext / XS / APItest / t / hash.t
CommitLineData
0314122a 1#!perl -w
2
3BEGIN {
4 chdir 't' if -d 't';
5 @INC = '../lib';
6 push @INC, "::lib:$MacPerl::Architecture:" if $^O eq 'MacOS';
7 require Config; import Config;
8 if ($Config{'extensions'} !~ /\bXS\/APItest\b/) {
9 # Look, I'm using this fully-qualified variable more than once!
10 my $arch = $MacPerl::Architecture;
11 print "1..0 # Skip: XS::APItest was not built\n";
12 exit 0;
13 }
14}
15
3128e575 16use strict;
17use utf8;
0314122a 18use Tie::Hash;
3128e575 19use Test::More 'no_plan';
20
55289a74 21BEGIN {use_ok('XS::APItest')};
0314122a 22
3128e575 23sub preform_test;
24sub test_present;
25sub test_absent;
26sub test_delete_present;
27sub test_delete_absent;
28sub brute_force_exists;
29sub test_store;
30sub test_fetch_present;
31sub test_fetch_absent;
0314122a 32
b60cf05a 33my $utf8_for_258 = chr 258;
34utf8::encode $utf8_for_258;
0314122a 35
3128e575 36my @testkeys = ('N', chr 198, chr 256);
b60cf05a 37my @keys = (@testkeys, $utf8_for_258);
0314122a 38
3128e575 39foreach (@keys) {
40 utf8::downgrade $_, 1;
41}
42main_tests (\@keys, \@testkeys, '');
0314122a 43
3128e575 44foreach (@keys) {
45 utf8::upgrade $_;
46}
47main_tests (\@keys, \@testkeys, ' [utf8 hash]');
0314122a 48
3128e575 49{
50 my %h = (a=>'cheat');
51 tie %h, 'Tie::StdHash';
9568a123 52 # is bug 36327 fixed?
53 my $result = ($] > 5.009) ? undef : 1;
54
55 is (XS::APItest::Hash::store(\%h, chr 258, 1), $result);
3128e575 56
57 ok (!exists $h{$utf8_for_258},
58 "hv_store doesn't insert a key with the raw utf8 on a tied hash");
59}
0314122a 60
9568a123 61if ($] > 5.009) {
5d2b1485 62 my $strtab = strtab();
63 is (ref $strtab, 'HASH', "The shared string table quacks like a hash");
8ca60cef 64 my $wibble = "\0";
5d2b1485 65 eval {
8ca60cef 66 $strtab->{$wibble}++;
5d2b1485 67 };
68 my $prefix = "Cannot modify shared string table in hv_";
69 my $what = $prefix . 'fetch';
70 like ($@, qr/^$what/,$what);
71 eval {
72 XS::APItest::Hash::store($strtab, 'Boom!', 1)
73 };
74 $what = $prefix . 'store';
75 like ($@, qr/^$what/, $what);
76 if (0) {
77 A::B->method();
78 }
79 # DESTROY should be in there.
80 eval {
81 delete $strtab->{DESTROY};
82 };
83 $what = $prefix . 'delete';
84 like ($@, qr/^$what/, $what);
85 # I can't work out how to get to the code that flips the wasutf8 flag on
86 # the hash key without some ikcy XS
87}
2dc92170 88
89{
90 is_deeply([&XS::APItest::Hash::test_hv_free_ent], [2,2,1,1],
91 "hv_free_ent frees the value immediately");
92 is_deeply([&XS::APItest::Hash::test_hv_delayfree_ent], [2,2,2,1],
93 "hv_delayfree_ent keeps the value around until FREETMPS");
94}
35ab5632 95
96foreach my $in ("", "N", "a\0b") {
97 my $got = XS::APItest::Hash::test_share_unshare_pvn($in);
98 is ($got, $in, "test_share_unshare_pvn");
99}
100
55289a74 101if ($] > 5.009) {
53c40a8f 102 foreach ([\&XS::APItest::Hash::rot13_hash, \&rot13, "rot 13"],
103 [\&XS::APItest::Hash::bitflip_hash, \&bitflip, "bitflip"],
104 ) {
105 my ($setup, $mapping, $name) = @$_;
106 my %hash;
107 my %placebo = (a => 1, p => 2, i => 4, e => 8);
108 $setup->(\%hash);
109 $hash{a}++; @hash{qw(p i e)} = (2, 4, 8);
110
111 test_U_hash(\%hash, \%placebo, [f => 9, g => 10, h => 11], $mapping,
112 $name);
113 }
850f5f16 114 foreach my $upgrade_o (0, 1) {
115 foreach my $upgrade_n (0, 1) {
116 my (%hash, %placebo);
117 XS::APItest::Hash::bitflip_hash(\%hash);
118 foreach my $new (["7", 65, 67, 80],
119 ["8", 163, 171, 215],
120 ["U", 2603, 2604, 2604],
121 ) {
122 foreach my $code (78, 240, 256, 1336) {
123 my $key = chr $code;
124 # This is the UTF-8 byte sequence for the key.
125 my $key_utf8 = $key;
126 utf8::encode($key_utf8);
127 if ($upgrade_o) {
128 $key .= chr 256;
129 chop $key;
130 }
131 $hash{$key} = $placebo{$key} = $code;
132 $hash{$key_utf8} = $placebo{$key_utf8} = "$code as UTF-8";
133 }
134 my $name = 'bitflip ' . shift @$new;
135 my @new_kv;
136 foreach my $code (@$new) {
137 my $key = chr $code;
138 if ($upgrade_n) {
139 $key .= chr 256;
140 chop $key;
141 }
142 push @new_kv, $key, $_;
143 }
144
145 $name .= ' upgraded(orig) ' if $upgrade_o;
146 $name .= ' upgraded(new) ' if $upgrade_n;
147 test_U_hash(\%hash, \%placebo, \@new_kv, \&bitflip, $name);
148 }
149 }
150 }
53c40a8f 151}
152
527df579 153{
154 my $as_utf8 = "\241" . chr 256;
155 chop $as_utf8;
156 my $as_bytes = "\243";
157 foreach my $key ('N', $as_bytes, $as_utf8, "\x{2623}") {
158 my $ord = ord $key;
159 foreach my $hash_pv (0, 1) {
160 my %hash;
161 is (XS::APItest::Hash::common({hv => \%hash, keypv => $key,
162 val => $ord, hash_pv => $hash_pv,
163 action =>
164 XS::APItest::HV_FETCH_ISSTORE}),
165 $ord, "store $ord \$hash_pv = $hash_pv");
166 is_deeply ([each %hash], [$key, $ord], "First key read is good");
167 is_deeply ([each %hash], [], "No second key good");
168
169 is ($hash{$key}, $ord, "Direct hash read finds $ord");
170 }
171 }
172}
173
53c40a8f 174exit;
175
176################################ The End ################################
177
178sub test_U_hash {
179 my ($hash, $placebo, $new, $mapping, $message) = @_;
180 my @hitlist = keys %$placebo;
181 print "# $message\n";
b54b4831 182
53c40a8f 183 my @keys = sort keys %$hash;
184 is ("@keys", join(' ', sort($mapping->(keys %$placebo))),
185 "uvar magic called exactly once on store");
b54b4831 186
850f5f16 187 is (keys %$hash, keys %$placebo);
55289a74 188
53c40a8f 189 my $victim = shift @hitlist;
190 is (delete $hash->{$victim}, delete $placebo->{$victim});
55289a74 191
850f5f16 192 is (keys %$hash, keys %$placebo);
53c40a8f 193 @keys = sort keys %$hash;
194 is ("@keys", join(' ', sort($mapping->(keys %$placebo))));
55289a74 195
53c40a8f 196 $victim = shift @hitlist;
197 is (XS::APItest::Hash::delete_ent ($hash, $victim,
55289a74 198 XS::APItest::HV_DISABLE_UVAR_XKEY),
199 undef, "Deleting a known key with conversion disabled fails (ent)");
850f5f16 200 is (keys %$hash, keys %$placebo);
55289a74 201
53c40a8f 202 is (XS::APItest::Hash::delete_ent ($hash, $victim, 0),
203 delete $placebo->{$victim},
204 "Deleting a known key with conversion enabled works (ent)");
850f5f16 205 is (keys %$hash, keys %$placebo);
53c40a8f 206 @keys = sort keys %$hash;
207 is ("@keys", join(' ', sort($mapping->(keys %$placebo))));
55289a74 208
53c40a8f 209 $victim = shift @hitlist;
210 is (XS::APItest::Hash::delete ($hash, $victim,
55289a74 211 XS::APItest::HV_DISABLE_UVAR_XKEY),
212 undef, "Deleting a known key with conversion disabled fails");
850f5f16 213 is (keys %$hash, keys %$placebo);
53c40a8f 214
215 is (XS::APItest::Hash::delete ($hash, $victim, 0),
216 delete $placebo->{$victim},
217 "Deleting a known key with conversion enabled works");
850f5f16 218 is (keys %$hash, keys %$placebo);
53c40a8f 219 @keys = sort keys %$hash;
220 is ("@keys", join(' ', sort($mapping->(keys %$placebo))));
221
222 my ($k, $v) = splice @$new, 0, 2;
223 $hash->{$k} = $v;
224 $placebo->{$k} = $v;
850f5f16 225 is (keys %$hash, keys %$placebo);
53c40a8f 226 @keys = sort keys %$hash;
227 is ("@keys", join(' ', sort($mapping->(keys %$placebo))));
228
229 ($k, $v) = splice @$new, 0, 2;
230 is (XS::APItest::Hash::store_ent($hash, $k, $v), $v, "store_ent");
231 $placebo->{$k} = $v;
850f5f16 232 is (keys %$hash, keys %$placebo);
53c40a8f 233 @keys = sort keys %$hash;
234 is ("@keys", join(' ', sort($mapping->(keys %$placebo))));
235
236 ($k, $v) = splice @$new, 0, 2;
237 is (XS::APItest::Hash::store($hash, $k, $v), $v, "store");
53c40a8f 238 $placebo->{$k} = $v;
850f5f16 239 is (keys %$hash, keys %$placebo);
53c40a8f 240 @keys = sort keys %$hash;
241 is ("@keys", join(' ', sort($mapping->(keys %$placebo))));
242
243 @hitlist = keys %$placebo;
244 $victim = shift @hitlist;
245 is (XS::APItest::Hash::fetch_ent($hash, $victim), $placebo->{$victim},
246 "fetch_ent");
247 is (XS::APItest::Hash::fetch_ent($hash, $mapping->($victim)), undef,
bdee33e4 248 "fetch_ent (missing)");
249
53c40a8f 250 $victim = shift @hitlist;
251 is (XS::APItest::Hash::fetch($hash, $victim), $placebo->{$victim},
252 "fetch");
253 is (XS::APItest::Hash::fetch($hash, $mapping->($victim)), undef,
bdee33e4 254 "fetch (missing)");
255
53c40a8f 256 $victim = shift @hitlist;
257 ok (XS::APItest::Hash::exists_ent($hash, $victim), "exists_ent");
258 ok (!XS::APItest::Hash::exists_ent($hash, $mapping->($victim)),
bdee33e4 259 "exists_ent (missing)");
260
53c40a8f 261 $victim = shift @hitlist;
6b4de907 262 die "Need a victim" unless defined $victim;
53c40a8f 263 ok (XS::APItest::Hash::exists($hash, $victim), "exists");
264 ok (!XS::APItest::Hash::exists($hash, $mapping->($victim)),
265 "exists (missing)");
6b4de907 266
267 is (XS::APItest::Hash::common({hv => $hash, keysv => $victim}),
268 $placebo->{$victim}, "common (fetch)");
269 is (XS::APItest::Hash::common({hv => $hash, keypv => $victim}),
270 $placebo->{$victim}, "common (fetch pv)");
271 is (XS::APItest::Hash::common({hv => $hash, keysv => $victim,
272 action => XS::APItest::HV_DISABLE_UVAR_XKEY}),
273 undef, "common (fetch) missing");
274 is (XS::APItest::Hash::common({hv => $hash, keypv => $victim,
275 action => XS::APItest::HV_DISABLE_UVAR_XKEY}),
276 undef, "common (fetch pv) missing");
277 is (XS::APItest::Hash::common({hv => $hash, keysv => $mapping->($victim),
278 action => XS::APItest::HV_DISABLE_UVAR_XKEY}),
279 $placebo->{$victim}, "common (fetch) missing mapped");
280 is (XS::APItest::Hash::common({hv => $hash, keypv => $mapping->($victim),
281 action => XS::APItest::HV_DISABLE_UVAR_XKEY}),
282 $placebo->{$victim}, "common (fetch pv) missing mapped");
b54b4831 283}
284
3128e575 285sub main_tests {
286 my ($keys, $testkeys, $description) = @_;
287 foreach my $key (@$testkeys) {
288 my $lckey = ($key eq chr 198) ? chr 230 : lc $key;
289 my $unikey = $key;
290 utf8::encode $unikey;
0314122a 291
3128e575 292 utf8::downgrade $key, 1;
293 utf8::downgrade $lckey, 1;
294 utf8::downgrade $unikey, 1;
295 main_test_inner ($key, $lckey, $unikey, $keys, $description);
0314122a 296
3128e575 297 utf8::upgrade $key;
298 utf8::upgrade $lckey;
299 utf8::upgrade $unikey;
300 main_test_inner ($key, $lckey, $unikey, $keys,
301 $description . ' [key utf8 on]');
302 }
0314122a 303
3128e575 304 # hv_exists was buggy for tied hashes, in that the raw utf8 key was being
305 # used - the utf8 flag was being lost.
306 perform_test (\&test_absent, (chr 258), $keys, '');
0314122a 307
3128e575 308 perform_test (\&test_fetch_absent, (chr 258), $keys, '');
309 perform_test (\&test_delete_absent, (chr 258), $keys, '');
0314122a 310}
311
3128e575 312sub main_test_inner {
313 my ($key, $lckey, $unikey, $keys, $description) = @_;
314 perform_test (\&test_present, $key, $keys, $description);
315 perform_test (\&test_fetch_present, $key, $keys, $description);
316 perform_test (\&test_delete_present, $key, $keys, $description);
b60cf05a 317
3128e575 318 perform_test (\&test_store, $key, $keys, $description, [a=>'cheat']);
319 perform_test (\&test_store, $key, $keys, $description, []);
b60cf05a 320
3128e575 321 perform_test (\&test_absent, $lckey, $keys, $description);
322 perform_test (\&test_fetch_absent, $lckey, $keys, $description);
323 perform_test (\&test_delete_absent, $lckey, $keys, $description);
b60cf05a 324
3128e575 325 return if $unikey eq $key;
326
327 perform_test (\&test_absent, $unikey, $keys, $description);
328 perform_test (\&test_fetch_absent, $unikey, $keys, $description);
329 perform_test (\&test_delete_absent, $unikey, $keys, $description);
b60cf05a 330}
331
3128e575 332sub perform_test {
333 my ($test_sub, $key, $keys, $message, @other) = @_;
b60cf05a 334 my $printable = join ',', map {ord} split //, $key;
335
3128e575 336 my (%hash, %tiehash);
337 tie %tiehash, 'Tie::StdHash';
b60cf05a 338
3128e575 339 @hash{@$keys} = @$keys;
340 @tiehash{@$keys} = @$keys;
b60cf05a 341
3128e575 342 &$test_sub (\%hash, $key, $printable, $message, @other);
343 &$test_sub (\%tiehash, $key, $printable, "$message tie", @other);
b60cf05a 344}
345
3128e575 346sub test_present {
347 my ($hash, $key, $printable, $message) = @_;
348
349 ok (exists $hash->{$key}, "hv_exists_ent present$message $printable");
350 ok (XS::APItest::Hash::exists ($hash, $key),
351 "hv_exists present$message $printable");
b60cf05a 352}
353
3128e575 354sub test_absent {
355 my ($hash, $key, $printable, $message) = @_;
858117f8 356
3128e575 357 ok (!exists $hash->{$key}, "hv_exists_ent absent$message $printable");
358 ok (!XS::APItest::Hash::exists ($hash, $key),
359 "hv_exists absent$message $printable");
b60cf05a 360}
361
3128e575 362sub test_delete_present {
363 my ($hash, $key, $printable, $message) = @_;
b60cf05a 364
3128e575 365 my $copy = {};
366 my $class = tied %$hash;
367 if (defined $class) {
368 tie %$copy, ref $class;
369 }
370 $copy = {%$hash};
8829b5e2 371 ok (brute_force_exists ($copy, $key),
372 "hv_delete_ent present$message $printable");
3128e575 373 is (delete $copy->{$key}, $key, "hv_delete_ent present$message $printable");
8829b5e2 374 ok (!brute_force_exists ($copy, $key),
375 "hv_delete_ent present$message $printable");
3128e575 376 $copy = {%$hash};
8829b5e2 377 ok (brute_force_exists ($copy, $key),
378 "hv_delete present$message $printable");
3128e575 379 is (XS::APItest::Hash::delete ($copy, $key), $key,
380 "hv_delete present$message $printable");
8829b5e2 381 ok (!brute_force_exists ($copy, $key),
382 "hv_delete present$message $printable");
b60cf05a 383}
384
3128e575 385sub test_delete_absent {
386 my ($hash, $key, $printable, $message) = @_;
b60cf05a 387
3128e575 388 my $copy = {};
389 my $class = tied %$hash;
390 if (defined $class) {
391 tie %$copy, ref $class;
392 }
393 $copy = {%$hash};
394 is (delete $copy->{$key}, undef, "hv_delete_ent absent$message $printable");
395 $copy = {%$hash};
396 is (XS::APItest::Hash::delete ($copy, $key), undef,
397 "hv_delete absent$message $printable");
b60cf05a 398}
399
3128e575 400sub test_store {
401 my ($hash, $key, $printable, $message, $defaults) = @_;
402 my $HV_STORE_IS_CRAZY = 1;
b60cf05a 403
3128e575 404 # We are cheating - hv_store returns NULL for a store into an empty
405 # tied hash. This isn't helpful here.
0314122a 406
3128e575 407 my $class = tied %$hash;
0314122a 408
9568a123 409 # It's important to do this with nice new hashes created each time round
410 # the loop, rather than hashes in the pad, which get recycled, and may have
411 # xhv_array non-NULL
412 my $h1 = {@$defaults};
413 my $h2 = {@$defaults};
3128e575 414 if (defined $class) {
9568a123 415 tie %$h1, ref $class;
416 tie %$h2, ref $class;
417 if ($] > 5.009) {
418 # bug 36327 is fixed
419 $HV_STORE_IS_CRAZY = undef;
420 } else {
421 # HV store_ent returns 1 if there was already underlying hash storage
422 $HV_STORE_IS_CRAZY = undef unless @$defaults;
423 }
3128e575 424 }
9568a123 425 is (XS::APItest::Hash::store_ent($h1, $key, 1), $HV_STORE_IS_CRAZY,
426 "hv_store_ent$message $printable");
427 ok (brute_force_exists ($h1, $key), "hv_store_ent$message $printable");
428 is (XS::APItest::Hash::store($h2, $key, 1), $HV_STORE_IS_CRAZY,
3128e575 429 "hv_store$message $printable");
9568a123 430 ok (brute_force_exists ($h2, $key), "hv_store$message $printable");
3128e575 431}
0314122a 432
3128e575 433sub test_fetch_present {
434 my ($hash, $key, $printable, $message) = @_;
b60cf05a 435
3128e575 436 is ($hash->{$key}, $key, "hv_fetch_ent present$message $printable");
437 is (XS::APItest::Hash::fetch ($hash, $key), $key,
438 "hv_fetch present$message $printable");
0314122a 439}
440
3128e575 441sub test_fetch_absent {
442 my ($hash, $key, $printable, $message) = @_;
b60cf05a 443
3128e575 444 is ($hash->{$key}, undef, "hv_fetch_ent absent$message $printable");
445 is (XS::APItest::Hash::fetch ($hash, $key), undef,
446 "hv_fetch absent$message $printable");
447}
b60cf05a 448
3128e575 449sub brute_force_exists {
450 my ($hash, $key) = @_;
451 foreach (keys %$hash) {
452 return 1 if $key eq $_;
453 }
454 return 0;
b60cf05a 455}
b54b4831 456
457sub rot13 {
458 my @results = map {my $a = $_; $a =~ tr/A-Za-z/N-ZA-Mn-za-m/; $a} @_;
459 wantarray ? @results : $results[0];
460}
53c40a8f 461
462sub bitflip {
463 my @results = map {join '', map {chr(32 ^ ord $_)} split '', $_} @_;
464 wantarray ? @results : $results[0];
465}