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