Fix typo in code not yet used.
[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
153exit;
154
155################################ The End ################################
156
157sub test_U_hash {
158 my ($hash, $placebo, $new, $mapping, $message) = @_;
159 my @hitlist = keys %$placebo;
160 print "# $message\n";
b54b4831 161
53c40a8f 162 my @keys = sort keys %$hash;
163 is ("@keys", join(' ', sort($mapping->(keys %$placebo))),
164 "uvar magic called exactly once on store");
b54b4831 165
850f5f16 166 is (keys %$hash, keys %$placebo);
55289a74 167
53c40a8f 168 my $victim = shift @hitlist;
169 is (delete $hash->{$victim}, delete $placebo->{$victim});
55289a74 170
850f5f16 171 is (keys %$hash, keys %$placebo);
53c40a8f 172 @keys = sort keys %$hash;
173 is ("@keys", join(' ', sort($mapping->(keys %$placebo))));
55289a74 174
53c40a8f 175 $victim = shift @hitlist;
176 is (XS::APItest::Hash::delete_ent ($hash, $victim,
55289a74 177 XS::APItest::HV_DISABLE_UVAR_XKEY),
178 undef, "Deleting a known key with conversion disabled fails (ent)");
850f5f16 179 is (keys %$hash, keys %$placebo);
55289a74 180
53c40a8f 181 is (XS::APItest::Hash::delete_ent ($hash, $victim, 0),
182 delete $placebo->{$victim},
183 "Deleting a known key with conversion enabled works (ent)");
850f5f16 184 is (keys %$hash, keys %$placebo);
53c40a8f 185 @keys = sort keys %$hash;
186 is ("@keys", join(' ', sort($mapping->(keys %$placebo))));
55289a74 187
53c40a8f 188 $victim = shift @hitlist;
189 is (XS::APItest::Hash::delete ($hash, $victim,
55289a74 190 XS::APItest::HV_DISABLE_UVAR_XKEY),
191 undef, "Deleting a known key with conversion disabled fails");
850f5f16 192 is (keys %$hash, keys %$placebo);
53c40a8f 193
194 is (XS::APItest::Hash::delete ($hash, $victim, 0),
195 delete $placebo->{$victim},
196 "Deleting a known key with conversion enabled works");
850f5f16 197 is (keys %$hash, keys %$placebo);
53c40a8f 198 @keys = sort keys %$hash;
199 is ("@keys", join(' ', sort($mapping->(keys %$placebo))));
200
201 my ($k, $v) = splice @$new, 0, 2;
202 $hash->{$k} = $v;
203 $placebo->{$k} = $v;
850f5f16 204 is (keys %$hash, keys %$placebo);
53c40a8f 205 @keys = sort keys %$hash;
206 is ("@keys", join(' ', sort($mapping->(keys %$placebo))));
207
208 ($k, $v) = splice @$new, 0, 2;
209 is (XS::APItest::Hash::store_ent($hash, $k, $v), $v, "store_ent");
210 $placebo->{$k} = $v;
850f5f16 211 is (keys %$hash, keys %$placebo);
53c40a8f 212 @keys = sort keys %$hash;
213 is ("@keys", join(' ', sort($mapping->(keys %$placebo))));
214
215 ($k, $v) = splice @$new, 0, 2;
216 is (XS::APItest::Hash::store($hash, $k, $v), $v, "store");
53c40a8f 217 $placebo->{$k} = $v;
850f5f16 218 is (keys %$hash, keys %$placebo);
53c40a8f 219 @keys = sort keys %$hash;
220 is ("@keys", join(' ', sort($mapping->(keys %$placebo))));
221
222 @hitlist = keys %$placebo;
223 $victim = shift @hitlist;
224 is (XS::APItest::Hash::fetch_ent($hash, $victim), $placebo->{$victim},
225 "fetch_ent");
226 is (XS::APItest::Hash::fetch_ent($hash, $mapping->($victim)), undef,
bdee33e4 227 "fetch_ent (missing)");
228
53c40a8f 229 $victim = shift @hitlist;
230 is (XS::APItest::Hash::fetch($hash, $victim), $placebo->{$victim},
231 "fetch");
232 is (XS::APItest::Hash::fetch($hash, $mapping->($victim)), undef,
bdee33e4 233 "fetch (missing)");
234
53c40a8f 235 $victim = shift @hitlist;
236 ok (XS::APItest::Hash::exists_ent($hash, $victim), "exists_ent");
237 ok (!XS::APItest::Hash::exists_ent($hash, $mapping->($victim)),
bdee33e4 238 "exists_ent (missing)");
239
53c40a8f 240 $victim = shift @hitlist;
6b4de907 241 die "Need a victim" unless defined $victim;
53c40a8f 242 ok (XS::APItest::Hash::exists($hash, $victim), "exists");
243 ok (!XS::APItest::Hash::exists($hash, $mapping->($victim)),
244 "exists (missing)");
6b4de907 245
246 is (XS::APItest::Hash::common({hv => $hash, keysv => $victim}),
247 $placebo->{$victim}, "common (fetch)");
248 is (XS::APItest::Hash::common({hv => $hash, keypv => $victim}),
249 $placebo->{$victim}, "common (fetch pv)");
250 is (XS::APItest::Hash::common({hv => $hash, keysv => $victim,
251 action => XS::APItest::HV_DISABLE_UVAR_XKEY}),
252 undef, "common (fetch) missing");
253 is (XS::APItest::Hash::common({hv => $hash, keypv => $victim,
254 action => XS::APItest::HV_DISABLE_UVAR_XKEY}),
255 undef, "common (fetch pv) missing");
256 is (XS::APItest::Hash::common({hv => $hash, keysv => $mapping->($victim),
257 action => XS::APItest::HV_DISABLE_UVAR_XKEY}),
258 $placebo->{$victim}, "common (fetch) missing mapped");
259 is (XS::APItest::Hash::common({hv => $hash, keypv => $mapping->($victim),
260 action => XS::APItest::HV_DISABLE_UVAR_XKEY}),
261 $placebo->{$victim}, "common (fetch pv) missing mapped");
b54b4831 262}
263
3128e575 264sub main_tests {
265 my ($keys, $testkeys, $description) = @_;
266 foreach my $key (@$testkeys) {
267 my $lckey = ($key eq chr 198) ? chr 230 : lc $key;
268 my $unikey = $key;
269 utf8::encode $unikey;
0314122a 270
3128e575 271 utf8::downgrade $key, 1;
272 utf8::downgrade $lckey, 1;
273 utf8::downgrade $unikey, 1;
274 main_test_inner ($key, $lckey, $unikey, $keys, $description);
0314122a 275
3128e575 276 utf8::upgrade $key;
277 utf8::upgrade $lckey;
278 utf8::upgrade $unikey;
279 main_test_inner ($key, $lckey, $unikey, $keys,
280 $description . ' [key utf8 on]');
281 }
0314122a 282
3128e575 283 # hv_exists was buggy for tied hashes, in that the raw utf8 key was being
284 # used - the utf8 flag was being lost.
285 perform_test (\&test_absent, (chr 258), $keys, '');
0314122a 286
3128e575 287 perform_test (\&test_fetch_absent, (chr 258), $keys, '');
288 perform_test (\&test_delete_absent, (chr 258), $keys, '');
0314122a 289}
290
3128e575 291sub main_test_inner {
292 my ($key, $lckey, $unikey, $keys, $description) = @_;
293 perform_test (\&test_present, $key, $keys, $description);
294 perform_test (\&test_fetch_present, $key, $keys, $description);
295 perform_test (\&test_delete_present, $key, $keys, $description);
b60cf05a 296
3128e575 297 perform_test (\&test_store, $key, $keys, $description, [a=>'cheat']);
298 perform_test (\&test_store, $key, $keys, $description, []);
b60cf05a 299
3128e575 300 perform_test (\&test_absent, $lckey, $keys, $description);
301 perform_test (\&test_fetch_absent, $lckey, $keys, $description);
302 perform_test (\&test_delete_absent, $lckey, $keys, $description);
b60cf05a 303
3128e575 304 return if $unikey eq $key;
305
306 perform_test (\&test_absent, $unikey, $keys, $description);
307 perform_test (\&test_fetch_absent, $unikey, $keys, $description);
308 perform_test (\&test_delete_absent, $unikey, $keys, $description);
b60cf05a 309}
310
3128e575 311sub perform_test {
312 my ($test_sub, $key, $keys, $message, @other) = @_;
b60cf05a 313 my $printable = join ',', map {ord} split //, $key;
314
3128e575 315 my (%hash, %tiehash);
316 tie %tiehash, 'Tie::StdHash';
b60cf05a 317
3128e575 318 @hash{@$keys} = @$keys;
319 @tiehash{@$keys} = @$keys;
b60cf05a 320
3128e575 321 &$test_sub (\%hash, $key, $printable, $message, @other);
322 &$test_sub (\%tiehash, $key, $printable, "$message tie", @other);
b60cf05a 323}
324
3128e575 325sub test_present {
326 my ($hash, $key, $printable, $message) = @_;
327
328 ok (exists $hash->{$key}, "hv_exists_ent present$message $printable");
329 ok (XS::APItest::Hash::exists ($hash, $key),
330 "hv_exists present$message $printable");
b60cf05a 331}
332
3128e575 333sub test_absent {
334 my ($hash, $key, $printable, $message) = @_;
858117f8 335
3128e575 336 ok (!exists $hash->{$key}, "hv_exists_ent absent$message $printable");
337 ok (!XS::APItest::Hash::exists ($hash, $key),
338 "hv_exists absent$message $printable");
b60cf05a 339}
340
3128e575 341sub test_delete_present {
342 my ($hash, $key, $printable, $message) = @_;
b60cf05a 343
3128e575 344 my $copy = {};
345 my $class = tied %$hash;
346 if (defined $class) {
347 tie %$copy, ref $class;
348 }
349 $copy = {%$hash};
8829b5e2 350 ok (brute_force_exists ($copy, $key),
351 "hv_delete_ent present$message $printable");
3128e575 352 is (delete $copy->{$key}, $key, "hv_delete_ent present$message $printable");
8829b5e2 353 ok (!brute_force_exists ($copy, $key),
354 "hv_delete_ent present$message $printable");
3128e575 355 $copy = {%$hash};
8829b5e2 356 ok (brute_force_exists ($copy, $key),
357 "hv_delete present$message $printable");
3128e575 358 is (XS::APItest::Hash::delete ($copy, $key), $key,
359 "hv_delete present$message $printable");
8829b5e2 360 ok (!brute_force_exists ($copy, $key),
361 "hv_delete present$message $printable");
b60cf05a 362}
363
3128e575 364sub test_delete_absent {
365 my ($hash, $key, $printable, $message) = @_;
b60cf05a 366
3128e575 367 my $copy = {};
368 my $class = tied %$hash;
369 if (defined $class) {
370 tie %$copy, ref $class;
371 }
372 $copy = {%$hash};
373 is (delete $copy->{$key}, undef, "hv_delete_ent absent$message $printable");
374 $copy = {%$hash};
375 is (XS::APItest::Hash::delete ($copy, $key), undef,
376 "hv_delete absent$message $printable");
b60cf05a 377}
378
3128e575 379sub test_store {
380 my ($hash, $key, $printable, $message, $defaults) = @_;
381 my $HV_STORE_IS_CRAZY = 1;
b60cf05a 382
3128e575 383 # We are cheating - hv_store returns NULL for a store into an empty
384 # tied hash. This isn't helpful here.
0314122a 385
3128e575 386 my $class = tied %$hash;
0314122a 387
9568a123 388 # It's important to do this with nice new hashes created each time round
389 # the loop, rather than hashes in the pad, which get recycled, and may have
390 # xhv_array non-NULL
391 my $h1 = {@$defaults};
392 my $h2 = {@$defaults};
3128e575 393 if (defined $class) {
9568a123 394 tie %$h1, ref $class;
395 tie %$h2, ref $class;
396 if ($] > 5.009) {
397 # bug 36327 is fixed
398 $HV_STORE_IS_CRAZY = undef;
399 } else {
400 # HV store_ent returns 1 if there was already underlying hash storage
401 $HV_STORE_IS_CRAZY = undef unless @$defaults;
402 }
3128e575 403 }
9568a123 404 is (XS::APItest::Hash::store_ent($h1, $key, 1), $HV_STORE_IS_CRAZY,
405 "hv_store_ent$message $printable");
406 ok (brute_force_exists ($h1, $key), "hv_store_ent$message $printable");
407 is (XS::APItest::Hash::store($h2, $key, 1), $HV_STORE_IS_CRAZY,
3128e575 408 "hv_store$message $printable");
9568a123 409 ok (brute_force_exists ($h2, $key), "hv_store$message $printable");
3128e575 410}
0314122a 411
3128e575 412sub test_fetch_present {
413 my ($hash, $key, $printable, $message) = @_;
b60cf05a 414
3128e575 415 is ($hash->{$key}, $key, "hv_fetch_ent present$message $printable");
416 is (XS::APItest::Hash::fetch ($hash, $key), $key,
417 "hv_fetch present$message $printable");
0314122a 418}
419
3128e575 420sub test_fetch_absent {
421 my ($hash, $key, $printable, $message) = @_;
b60cf05a 422
3128e575 423 is ($hash->{$key}, undef, "hv_fetch_ent absent$message $printable");
424 is (XS::APItest::Hash::fetch ($hash, $key), undef,
425 "hv_fetch absent$message $printable");
426}
b60cf05a 427
3128e575 428sub brute_force_exists {
429 my ($hash, $key) = @_;
430 foreach (keys %$hash) {
431 return 1 if $key eq $_;
432 }
433 return 0;
b60cf05a 434}
b54b4831 435
436sub rot13 {
437 my @results = map {my $a = $_; $a =~ tr/A-Za-z/N-ZA-Mn-za-m/; $a} @_;
438 wantarray ? @results : $results[0];
439}
53c40a8f 440
441sub bitflip {
442 my @results = map {join '', map {chr(32 ^ ord $_)} split '', $_} @_;
443 wantarray ? @results : $results[0];
444}