Clean up test files left behind (on Win32, at least) by Archive-Extract
[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
21use_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';
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
93foreach 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
b54b4831 98{
99 my %hash;
100 XS::APItest::Hash::rot13_hash(\%hash);
101 $hash{a}++; @hash{qw(p i e)} = (2, 4, 8);
102
103 my @keys = sort keys %hash;
104 is("@keys", join(' ', sort(rot13(qw(a p i e)))),
105 "uvar magic called exactly once on store");
106
107 is($hash{i}, 4);
108}
109
3128e575 110exit;
0314122a 111
3128e575 112################################ The End ################################
0314122a 113
3128e575 114sub main_tests {
115 my ($keys, $testkeys, $description) = @_;
116 foreach my $key (@$testkeys) {
117 my $lckey = ($key eq chr 198) ? chr 230 : lc $key;
118 my $unikey = $key;
119 utf8::encode $unikey;
0314122a 120
3128e575 121 utf8::downgrade $key, 1;
122 utf8::downgrade $lckey, 1;
123 utf8::downgrade $unikey, 1;
124 main_test_inner ($key, $lckey, $unikey, $keys, $description);
0314122a 125
3128e575 126 utf8::upgrade $key;
127 utf8::upgrade $lckey;
128 utf8::upgrade $unikey;
129 main_test_inner ($key, $lckey, $unikey, $keys,
130 $description . ' [key utf8 on]');
131 }
0314122a 132
3128e575 133 # hv_exists was buggy for tied hashes, in that the raw utf8 key was being
134 # used - the utf8 flag was being lost.
135 perform_test (\&test_absent, (chr 258), $keys, '');
0314122a 136
3128e575 137 perform_test (\&test_fetch_absent, (chr 258), $keys, '');
138 perform_test (\&test_delete_absent, (chr 258), $keys, '');
0314122a 139}
140
3128e575 141sub main_test_inner {
142 my ($key, $lckey, $unikey, $keys, $description) = @_;
143 perform_test (\&test_present, $key, $keys, $description);
144 perform_test (\&test_fetch_present, $key, $keys, $description);
145 perform_test (\&test_delete_present, $key, $keys, $description);
b60cf05a 146
3128e575 147 perform_test (\&test_store, $key, $keys, $description, [a=>'cheat']);
148 perform_test (\&test_store, $key, $keys, $description, []);
b60cf05a 149
3128e575 150 perform_test (\&test_absent, $lckey, $keys, $description);
151 perform_test (\&test_fetch_absent, $lckey, $keys, $description);
152 perform_test (\&test_delete_absent, $lckey, $keys, $description);
b60cf05a 153
3128e575 154 return if $unikey eq $key;
155
156 perform_test (\&test_absent, $unikey, $keys, $description);
157 perform_test (\&test_fetch_absent, $unikey, $keys, $description);
158 perform_test (\&test_delete_absent, $unikey, $keys, $description);
b60cf05a 159}
160
3128e575 161sub perform_test {
162 my ($test_sub, $key, $keys, $message, @other) = @_;
b60cf05a 163 my $printable = join ',', map {ord} split //, $key;
164
3128e575 165 my (%hash, %tiehash);
166 tie %tiehash, 'Tie::StdHash';
b60cf05a 167
3128e575 168 @hash{@$keys} = @$keys;
169 @tiehash{@$keys} = @$keys;
b60cf05a 170
3128e575 171 &$test_sub (\%hash, $key, $printable, $message, @other);
172 &$test_sub (\%tiehash, $key, $printable, "$message tie", @other);
b60cf05a 173}
174
3128e575 175sub test_present {
176 my ($hash, $key, $printable, $message) = @_;
177
178 ok (exists $hash->{$key}, "hv_exists_ent present$message $printable");
179 ok (XS::APItest::Hash::exists ($hash, $key),
180 "hv_exists present$message $printable");
b60cf05a 181}
182
3128e575 183sub test_absent {
184 my ($hash, $key, $printable, $message) = @_;
858117f8 185
3128e575 186 ok (!exists $hash->{$key}, "hv_exists_ent absent$message $printable");
187 ok (!XS::APItest::Hash::exists ($hash, $key),
188 "hv_exists absent$message $printable");
b60cf05a 189}
190
3128e575 191sub test_delete_present {
192 my ($hash, $key, $printable, $message) = @_;
b60cf05a 193
3128e575 194 my $copy = {};
195 my $class = tied %$hash;
196 if (defined $class) {
197 tie %$copy, ref $class;
198 }
199 $copy = {%$hash};
8829b5e2 200 ok (brute_force_exists ($copy, $key),
201 "hv_delete_ent present$message $printable");
3128e575 202 is (delete $copy->{$key}, $key, "hv_delete_ent present$message $printable");
8829b5e2 203 ok (!brute_force_exists ($copy, $key),
204 "hv_delete_ent present$message $printable");
3128e575 205 $copy = {%$hash};
8829b5e2 206 ok (brute_force_exists ($copy, $key),
207 "hv_delete present$message $printable");
3128e575 208 is (XS::APItest::Hash::delete ($copy, $key), $key,
209 "hv_delete present$message $printable");
8829b5e2 210 ok (!brute_force_exists ($copy, $key),
211 "hv_delete present$message $printable");
b60cf05a 212}
213
3128e575 214sub test_delete_absent {
215 my ($hash, $key, $printable, $message) = @_;
b60cf05a 216
3128e575 217 my $copy = {};
218 my $class = tied %$hash;
219 if (defined $class) {
220 tie %$copy, ref $class;
221 }
222 $copy = {%$hash};
223 is (delete $copy->{$key}, undef, "hv_delete_ent absent$message $printable");
224 $copy = {%$hash};
225 is (XS::APItest::Hash::delete ($copy, $key), undef,
226 "hv_delete absent$message $printable");
b60cf05a 227}
228
3128e575 229sub test_store {
230 my ($hash, $key, $printable, $message, $defaults) = @_;
231 my $HV_STORE_IS_CRAZY = 1;
b60cf05a 232
3128e575 233 # We are cheating - hv_store returns NULL for a store into an empty
234 # tied hash. This isn't helpful here.
0314122a 235
3128e575 236 my $class = tied %$hash;
0314122a 237
3128e575 238 my %h1 = @$defaults;
239 my %h2 = @$defaults;
240 if (defined $class) {
241 tie %h1, ref $class;
242 tie %h2, ref $class;
1baaf5d7 243 $HV_STORE_IS_CRAZY = undef;
3128e575 244 }
1baaf5d7 245 is (XS::APItest::Hash::store_ent(\%h1, $key, 1), $HV_STORE_IS_CRAZY,
3128e575 246 "hv_store_ent$message $printable");
247 ok (brute_force_exists (\%h1, $key), "hv_store_ent$message $printable");
248 is (XS::APItest::Hash::store(\%h2, $key, 1), $HV_STORE_IS_CRAZY,
249 "hv_store$message $printable");
250 ok (brute_force_exists (\%h2, $key), "hv_store$message $printable");
251}
0314122a 252
3128e575 253sub test_fetch_present {
254 my ($hash, $key, $printable, $message) = @_;
b60cf05a 255
3128e575 256 is ($hash->{$key}, $key, "hv_fetch_ent present$message $printable");
257 is (XS::APItest::Hash::fetch ($hash, $key), $key,
258 "hv_fetch present$message $printable");
0314122a 259}
260
3128e575 261sub test_fetch_absent {
262 my ($hash, $key, $printable, $message) = @_;
b60cf05a 263
3128e575 264 is ($hash->{$key}, undef, "hv_fetch_ent absent$message $printable");
265 is (XS::APItest::Hash::fetch ($hash, $key), undef,
266 "hv_fetch absent$message $printable");
267}
b60cf05a 268
3128e575 269sub brute_force_exists {
270 my ($hash, $key) = @_;
271 foreach (keys %$hash) {
272 return 1 if $key eq $_;
273 }
274 return 0;
b60cf05a 275}
b54b4831 276
277sub rot13 {
278 my @results = map {my $a = $_; $a =~ tr/A-Za-z/N-ZA-Mn-za-m/; $a} @_;
279 wantarray ? @results : $results[0];
280}