1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
|
#!perl -w
BEGIN {
chdir 't' if -d 't';
@INC = '../lib';
push @INC, "::lib:$MacPerl::Architecture:" if $^O eq 'MacOS';
require Config; import Config;
if ($Config{'extensions'} !~ /\bXS\/APItest\b/) {
# Look, I'm using this fully-qualified variable more than once!
my $arch = $MacPerl::Architecture;
print "1..0 # Skip: XS::APItest was not built\n";
exit 0;
}
}
use strict;
use utf8;
use Tie::Hash;
use Test::More 'no_plan';
BEGIN {use_ok('XS::APItest')};
sub preform_test;
sub test_present;
sub test_absent;
sub test_delete_present;
sub test_delete_absent;
sub brute_force_exists;
sub test_store;
sub test_fetch_present;
sub test_fetch_absent;
my $utf8_for_258 = chr 258;
utf8::encode $utf8_for_258;
my @testkeys = ('N', chr 198, chr 256);
my @keys = (@testkeys, $utf8_for_258);
foreach (@keys) {
utf8::downgrade $_, 1;
}
main_tests (\@keys, \@testkeys, '');
foreach (@keys) {
utf8::upgrade $_;
}
main_tests (\@keys, \@testkeys, ' [utf8 hash]');
{
my %h = (a=>'cheat');
tie %h, 'Tie::StdHash';
is (XS::APItest::Hash::store(\%h, chr 258, 1), undef);
ok (!exists $h{$utf8_for_258},
"hv_store doesn't insert a key with the raw utf8 on a tied hash");
}
{
my $strtab = strtab();
is (ref $strtab, 'HASH', "The shared string table quacks like a hash");
my $wibble = "\0";
eval {
$strtab->{$wibble}++;
};
my $prefix = "Cannot modify shared string table in hv_";
my $what = $prefix . 'fetch';
like ($@, qr/^$what/,$what);
eval {
XS::APItest::Hash::store($strtab, 'Boom!', 1)
};
$what = $prefix . 'store';
like ($@, qr/^$what/, $what);
if (0) {
A::B->method();
}
# DESTROY should be in there.
eval {
delete $strtab->{DESTROY};
};
$what = $prefix . 'delete';
like ($@, qr/^$what/, $what);
# I can't work out how to get to the code that flips the wasutf8 flag on
# the hash key without some ikcy XS
}
{
is_deeply([&XS::APItest::Hash::test_hv_free_ent], [2,2,1,1],
"hv_free_ent frees the value immediately");
is_deeply([&XS::APItest::Hash::test_hv_delayfree_ent], [2,2,2,1],
"hv_delayfree_ent keeps the value around until FREETMPS");
}
foreach my $in ("", "N", "a\0b") {
my $got = XS::APItest::Hash::test_share_unshare_pvn($in);
is ($got, $in, "test_share_unshare_pvn");
}
if ($] > 5.009) {
my %hash;
XS::APItest::Hash::rot13_hash(\%hash);
$hash{a}++; @hash{qw(p i e)} = (2, 4, 8);
my @keys = sort keys %hash;
is("@keys", join(' ', sort(rot13(qw(a p i e)))),
"uvar magic called exactly once on store");
is($hash{i}, 4);
is(delete $hash{a}, 1);
is(keys %hash, 3);
@keys = sort keys %hash;
is("@keys", join(' ', sort(rot13(qw(p i e)))));
is (XS::APItest::Hash::delete_ent (\%hash, 'p',
XS::APItest::HV_DISABLE_UVAR_XKEY),
undef, "Deleting a known key with conversion disabled fails (ent)");
is(keys %hash, 3);
is (XS::APItest::Hash::delete_ent (\%hash, 'p', 0),
2, "Deleting a known key with conversion enabled works (ent)");
is(keys %hash, 2);
@keys = sort keys %hash;
is("@keys", join(' ', sort(rot13(qw(i e)))));
is (XS::APItest::Hash::delete (\%hash, 'i',
XS::APItest::HV_DISABLE_UVAR_XKEY),
undef, "Deleting a known key with conversion disabled fails");
is(keys %hash, 2);
is (XS::APItest::Hash::delete (\%hash, 'i', 0),
4, "Deleting a known key with conversion enabled works");
is(keys %hash, 1);
@keys = sort keys %hash;
is("@keys", join(' ', sort(rot13(qw(e)))));
}
exit;
################################ The End ################################
sub main_tests {
my ($keys, $testkeys, $description) = @_;
foreach my $key (@$testkeys) {
my $lckey = ($key eq chr 198) ? chr 230 : lc $key;
my $unikey = $key;
utf8::encode $unikey;
utf8::downgrade $key, 1;
utf8::downgrade $lckey, 1;
utf8::downgrade $unikey, 1;
main_test_inner ($key, $lckey, $unikey, $keys, $description);
utf8::upgrade $key;
utf8::upgrade $lckey;
utf8::upgrade $unikey;
main_test_inner ($key, $lckey, $unikey, $keys,
$description . ' [key utf8 on]');
}
# hv_exists was buggy for tied hashes, in that the raw utf8 key was being
# used - the utf8 flag was being lost.
perform_test (\&test_absent, (chr 258), $keys, '');
perform_test (\&test_fetch_absent, (chr 258), $keys, '');
perform_test (\&test_delete_absent, (chr 258), $keys, '');
}
sub main_test_inner {
my ($key, $lckey, $unikey, $keys, $description) = @_;
perform_test (\&test_present, $key, $keys, $description);
perform_test (\&test_fetch_present, $key, $keys, $description);
perform_test (\&test_delete_present, $key, $keys, $description);
perform_test (\&test_store, $key, $keys, $description, [a=>'cheat']);
perform_test (\&test_store, $key, $keys, $description, []);
perform_test (\&test_absent, $lckey, $keys, $description);
perform_test (\&test_fetch_absent, $lckey, $keys, $description);
perform_test (\&test_delete_absent, $lckey, $keys, $description);
return if $unikey eq $key;
perform_test (\&test_absent, $unikey, $keys, $description);
perform_test (\&test_fetch_absent, $unikey, $keys, $description);
perform_test (\&test_delete_absent, $unikey, $keys, $description);
}
sub perform_test {
my ($test_sub, $key, $keys, $message, @other) = @_;
my $printable = join ',', map {ord} split //, $key;
my (%hash, %tiehash);
tie %tiehash, 'Tie::StdHash';
@hash{@$keys} = @$keys;
@tiehash{@$keys} = @$keys;
&$test_sub (\%hash, $key, $printable, $message, @other);
&$test_sub (\%tiehash, $key, $printable, "$message tie", @other);
}
sub test_present {
my ($hash, $key, $printable, $message) = @_;
ok (exists $hash->{$key}, "hv_exists_ent present$message $printable");
ok (XS::APItest::Hash::exists ($hash, $key),
"hv_exists present$message $printable");
}
sub test_absent {
my ($hash, $key, $printable, $message) = @_;
ok (!exists $hash->{$key}, "hv_exists_ent absent$message $printable");
ok (!XS::APItest::Hash::exists ($hash, $key),
"hv_exists absent$message $printable");
}
sub test_delete_present {
my ($hash, $key, $printable, $message) = @_;
my $copy = {};
my $class = tied %$hash;
if (defined $class) {
tie %$copy, ref $class;
}
$copy = {%$hash};
ok (brute_force_exists ($copy, $key),
"hv_delete_ent present$message $printable");
is (delete $copy->{$key}, $key, "hv_delete_ent present$message $printable");
ok (!brute_force_exists ($copy, $key),
"hv_delete_ent present$message $printable");
$copy = {%$hash};
ok (brute_force_exists ($copy, $key),
"hv_delete present$message $printable");
is (XS::APItest::Hash::delete ($copy, $key), $key,
"hv_delete present$message $printable");
ok (!brute_force_exists ($copy, $key),
"hv_delete present$message $printable");
}
sub test_delete_absent {
my ($hash, $key, $printable, $message) = @_;
my $copy = {};
my $class = tied %$hash;
if (defined $class) {
tie %$copy, ref $class;
}
$copy = {%$hash};
is (delete $copy->{$key}, undef, "hv_delete_ent absent$message $printable");
$copy = {%$hash};
is (XS::APItest::Hash::delete ($copy, $key), undef,
"hv_delete absent$message $printable");
}
sub test_store {
my ($hash, $key, $printable, $message, $defaults) = @_;
my $HV_STORE_IS_CRAZY = 1;
# We are cheating - hv_store returns NULL for a store into an empty
# tied hash. This isn't helpful here.
my $class = tied %$hash;
my %h1 = @$defaults;
my %h2 = @$defaults;
if (defined $class) {
tie %h1, ref $class;
tie %h2, ref $class;
$HV_STORE_IS_CRAZY = undef;
}
is (XS::APItest::Hash::store_ent(\%h1, $key, 1), $HV_STORE_IS_CRAZY,
"hv_store_ent$message $printable");
ok (brute_force_exists (\%h1, $key), "hv_store_ent$message $printable");
is (XS::APItest::Hash::store(\%h2, $key, 1), $HV_STORE_IS_CRAZY,
"hv_store$message $printable");
ok (brute_force_exists (\%h2, $key), "hv_store$message $printable");
}
sub test_fetch_present {
my ($hash, $key, $printable, $message) = @_;
is ($hash->{$key}, $key, "hv_fetch_ent present$message $printable");
is (XS::APItest::Hash::fetch ($hash, $key), $key,
"hv_fetch present$message $printable");
}
sub test_fetch_absent {
my ($hash, $key, $printable, $message) = @_;
is ($hash->{$key}, undef, "hv_fetch_ent absent$message $printable");
is (XS::APItest::Hash::fetch ($hash, $key), undef,
"hv_fetch absent$message $printable");
}
sub brute_force_exists {
my ($hash, $key) = @_;
foreach (keys %$hash) {
return 1 if $key eq $_;
}
return 0;
}
sub rot13 {
my @results = map {my $a = $_; $a =~ tr/A-Za-z/N-ZA-Mn-za-m/; $a} @_;
wantarray ? @results : $results[0];
}
|