Provide a macro version of S_new_body to inline it within the hot code.
[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';
52 is (XS::APItest::Hash::store(\%h, chr 258, 1), 1);
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}
3128e575 85exit;
0314122a 86
3128e575 87################################ The End ################################
0314122a 88
3128e575 89sub main_tests {
90 my ($keys, $testkeys, $description) = @_;
91 foreach my $key (@$testkeys) {
92 my $lckey = ($key eq chr 198) ? chr 230 : lc $key;
93 my $unikey = $key;
94 utf8::encode $unikey;
0314122a 95
3128e575 96 utf8::downgrade $key, 1;
97 utf8::downgrade $lckey, 1;
98 utf8::downgrade $unikey, 1;
99 main_test_inner ($key, $lckey, $unikey, $keys, $description);
0314122a 100
3128e575 101 utf8::upgrade $key;
102 utf8::upgrade $lckey;
103 utf8::upgrade $unikey;
104 main_test_inner ($key, $lckey, $unikey, $keys,
105 $description . ' [key utf8 on]');
106 }
0314122a 107
3128e575 108 # hv_exists was buggy for tied hashes, in that the raw utf8 key was being
109 # used - the utf8 flag was being lost.
110 perform_test (\&test_absent, (chr 258), $keys, '');
0314122a 111
3128e575 112 perform_test (\&test_fetch_absent, (chr 258), $keys, '');
113 perform_test (\&test_delete_absent, (chr 258), $keys, '');
0314122a 114}
115
3128e575 116sub main_test_inner {
117 my ($key, $lckey, $unikey, $keys, $description) = @_;
118 perform_test (\&test_present, $key, $keys, $description);
119 perform_test (\&test_fetch_present, $key, $keys, $description);
120 perform_test (\&test_delete_present, $key, $keys, $description);
b60cf05a 121
3128e575 122 perform_test (\&test_store, $key, $keys, $description, [a=>'cheat']);
123 perform_test (\&test_store, $key, $keys, $description, []);
b60cf05a 124
3128e575 125 perform_test (\&test_absent, $lckey, $keys, $description);
126 perform_test (\&test_fetch_absent, $lckey, $keys, $description);
127 perform_test (\&test_delete_absent, $lckey, $keys, $description);
b60cf05a 128
3128e575 129 return if $unikey eq $key;
130
131 perform_test (\&test_absent, $unikey, $keys, $description);
132 perform_test (\&test_fetch_absent, $unikey, $keys, $description);
133 perform_test (\&test_delete_absent, $unikey, $keys, $description);
b60cf05a 134}
135
3128e575 136sub perform_test {
137 my ($test_sub, $key, $keys, $message, @other) = @_;
b60cf05a 138 my $printable = join ',', map {ord} split //, $key;
139
3128e575 140 my (%hash, %tiehash);
141 tie %tiehash, 'Tie::StdHash';
b60cf05a 142
3128e575 143 @hash{@$keys} = @$keys;
144 @tiehash{@$keys} = @$keys;
b60cf05a 145
3128e575 146 &$test_sub (\%hash, $key, $printable, $message, @other);
147 &$test_sub (\%tiehash, $key, $printable, "$message tie", @other);
b60cf05a 148}
149
3128e575 150sub test_present {
151 my ($hash, $key, $printable, $message) = @_;
152
153 ok (exists $hash->{$key}, "hv_exists_ent present$message $printable");
154 ok (XS::APItest::Hash::exists ($hash, $key),
155 "hv_exists present$message $printable");
b60cf05a 156}
157
3128e575 158sub test_absent {
159 my ($hash, $key, $printable, $message) = @_;
858117f8 160
3128e575 161 ok (!exists $hash->{$key}, "hv_exists_ent absent$message $printable");
162 ok (!XS::APItest::Hash::exists ($hash, $key),
163 "hv_exists absent$message $printable");
b60cf05a 164}
165
3128e575 166sub test_delete_present {
167 my ($hash, $key, $printable, $message) = @_;
b60cf05a 168
3128e575 169 my $copy = {};
170 my $class = tied %$hash;
171 if (defined $class) {
172 tie %$copy, ref $class;
173 }
174 $copy = {%$hash};
8829b5e2 175 ok (brute_force_exists ($copy, $key),
176 "hv_delete_ent present$message $printable");
3128e575 177 is (delete $copy->{$key}, $key, "hv_delete_ent present$message $printable");
8829b5e2 178 ok (!brute_force_exists ($copy, $key),
179 "hv_delete_ent present$message $printable");
3128e575 180 $copy = {%$hash};
8829b5e2 181 ok (brute_force_exists ($copy, $key),
182 "hv_delete present$message $printable");
3128e575 183 is (XS::APItest::Hash::delete ($copy, $key), $key,
184 "hv_delete present$message $printable");
8829b5e2 185 ok (!brute_force_exists ($copy, $key),
186 "hv_delete present$message $printable");
b60cf05a 187}
188
3128e575 189sub test_delete_absent {
190 my ($hash, $key, $printable, $message) = @_;
b60cf05a 191
3128e575 192 my $copy = {};
193 my $class = tied %$hash;
194 if (defined $class) {
195 tie %$copy, ref $class;
196 }
197 $copy = {%$hash};
198 is (delete $copy->{$key}, undef, "hv_delete_ent absent$message $printable");
199 $copy = {%$hash};
200 is (XS::APItest::Hash::delete ($copy, $key), undef,
201 "hv_delete absent$message $printable");
b60cf05a 202}
203
3128e575 204sub test_store {
205 my ($hash, $key, $printable, $message, $defaults) = @_;
206 my $HV_STORE_IS_CRAZY = 1;
b60cf05a 207
3128e575 208 # We are cheating - hv_store returns NULL for a store into an empty
209 # tied hash. This isn't helpful here.
0314122a 210
3128e575 211 my $class = tied %$hash;
0314122a 212
3128e575 213 my %h1 = @$defaults;
214 my %h2 = @$defaults;
215 if (defined $class) {
216 tie %h1, ref $class;
217 tie %h2, ref $class;
218 $HV_STORE_IS_CRAZY = undef unless @$defaults;
219 }
220 is (XS::APItest::Hash::store_ent(\%h1, $key, 1), 1,
221 "hv_store_ent$message $printable");
222 ok (brute_force_exists (\%h1, $key), "hv_store_ent$message $printable");
223 is (XS::APItest::Hash::store(\%h2, $key, 1), $HV_STORE_IS_CRAZY,
224 "hv_store$message $printable");
225 ok (brute_force_exists (\%h2, $key), "hv_store$message $printable");
226}
0314122a 227
3128e575 228sub test_fetch_present {
229 my ($hash, $key, $printable, $message) = @_;
b60cf05a 230
3128e575 231 is ($hash->{$key}, $key, "hv_fetch_ent present$message $printable");
232 is (XS::APItest::Hash::fetch ($hash, $key), $key,
233 "hv_fetch present$message $printable");
0314122a 234}
235
3128e575 236sub test_fetch_absent {
237 my ($hash, $key, $printable, $message) = @_;
b60cf05a 238
3128e575 239 is ($hash->{$key}, undef, "hv_fetch_ent absent$message $printable");
240 is (XS::APItest::Hash::fetch ($hash, $key), undef,
241 "hv_fetch absent$message $printable");
242}
b60cf05a 243
3128e575 244sub brute_force_exists {
245 my ($hash, $key) = @_;
246 foreach (keys %$hash) {
247 return 1 if $key eq $_;
248 }
249 return 0;
b60cf05a 250}