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