This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Bump $VERSION.
[perl5.git] / ext / XS-APItest / APItest.pm
CommitLineData
3e61d65a
JH
1package XS::APItest;
2
83f8bb56 3{ use 5.011001; }
3e61d65a
JH
4use strict;
5use warnings;
6use Carp;
7
8268b43e 8our $VERSION = '0.53';
b7dfc24f 9
b58f046e 10require XSLoader;
3e61d65a
JH
11
12# Export everything since these functions are only used by a test script
e2f1cb34
NC
13# Export subpackages too - in effect, export all their routines into us, then
14# export everything from us.
15sub import {
16 my $package = shift;
17 croak ("Can't export for '$package'") unless $package eq __PACKAGE__;
18 my $exports;
19 @{$exports}{@_} = () if @_;
20
21 my $callpkg = caller;
22
23 my @stashes = ('XS::APItest::', \%XS::APItest::);
24 while (my ($stash_name, $stash) = splice @stashes, 0, 2) {
25 while (my ($sym_name, $glob) = each %$stash) {
26 if ($sym_name =~ /::$/) {
27 # Skip any subpackages that are clearly OO
28 next if *{$glob}{HASH}{'new'};
120b7a08
SM
29 # and any that have AUTOLOAD
30 next if *{$glob}{HASH}{AUTOLOAD};
e2f1cb34
NC
31 push @stashes, "$stash_name$sym_name", *{$glob}{HASH};
32 } elsif (ref $glob eq 'SCALAR' || *{$glob}{CODE}) {
33 if ($exports) {
34 next if !exists $exports->{$sym_name};
35 delete $exports->{$sym_name};
36 }
37 no strict 'refs';
38 *{"$callpkg\::$sym_name"} = \&{"$stash_name$sym_name"};
39 }
40 }
41 }
83f8bb56 42 foreach (keys %{$exports||{}}) {
78cdf107 43 next unless /\A(?:rpn|calcrpn|stufftest|swaptwostmts|looprest|scopelessblock|stmtasexpr|stmtsasexpr|loopblock|blockasexpr|swaplabel|labelconst|arrayfullexpr|arraylistexpr|arraytermexpr|arrayarithexpr|arrayexprflags)\z/;
83f8bb56
Z
44 $^H{"XS::APItest/$_"} = 1;
45 delete $exports->{$_};
46 }
e2f1cb34
NC
47 if ($exports) {
48 my @carp = keys %$exports;
49 if (@carp) {
50 croak(join '',
51 (map "\"$_\" is not exported by the $package module\n", sort @carp),
52 "Can't continue after import errors");
53 }
54 }
55}
3e61d65a 56
84ac5fd7 57use vars '$WARNINGS_ON_BOOTSTRAP';
0932863f
NC
58use vars map "\$${_}_called_PP", qw(BEGIN UNITCHECK CHECK INIT END);
59
9568a123
NC
60BEGIN {
61 # This is arguably a hack, but it disposes of the UNITCHECK block without
62 # needing to preprocess the source code
63 if ($] < 5.009) {
64 eval 'sub UNITCHECK (&) {}; 1' or die $@;
65 }
66}
67
0932863f
NC
68# Do these here to verify that XS code and Perl code get called at the same
69# times
70BEGIN {
71 $BEGIN_called_PP++;
72}
73UNITCHECK {
74 $UNITCHECK_called_PP++;
9568a123 75};
0932863f
NC
76{
77 # Need $W false by default, as some tests run under -w, and under -w we
78 # can get warnings about "Too late to run CHECK" block (and INIT block)
79 no warnings 'void';
80 CHECK {
81 $CHECK_called_PP++;
82 }
83 INIT {
84 $INIT_called_PP++;
85 }
86}
87END {
88 $END_called_PP++;
89}
90
84ac5fd7 91if ($WARNINGS_ON_BOOTSTRAP) {
b58f046e 92 XSLoader::load();
84ac5fd7 93} else {
0932863f 94 # More CHECK and INIT blocks that could warn:
84ac5fd7 95 local $^W;
b58f046e 96 XSLoader::load();
84ac5fd7 97}
3e61d65a 98
fba8e77b
FC
99# This XS function needs the lvalue attr applied.
100eval 'use attributes __PACKAGE__, \\&lv_temp_object, "lvalue"; 1' or die;
101
3e61d65a
JH
1021;
103__END__
104
105=head1 NAME
106
107XS::APItest - Test the perl C API
108
109=head1 SYNOPSIS
110
111 use XS::APItest;
112 print_double(4);
113
83f8bb56
Z
114 use XS::APItest qw(rpn calcrpn);
115 $triangle = rpn($n $n 1 + * 2 /);
116 calcrpn $triangle { $n $n 1 + * 2 / }
117
3e61d65a
JH
118=head1 ABSTRACT
119
d97c33b5
DM
120This module tests the perl C API. Also exposes various bit of the perl
121internals for the use of core test scripts.
3e61d65a
JH
122
123=head1 DESCRIPTION
124
125This module can be used to check that the perl C API is behaving
126correctly. This module provides test functions and an associated
127test script that verifies the output.
128
129This module is not meant to be installed.
130
131=head2 EXPORT
132
133Exports all the test functions:
134
135=over 4
136
137=item B<print_double>
138
139Test that a double-precision floating point number is formatted
140correctly by C<printf>.
141
142 print_double( $val );
143
144Output is sent to STDOUT.
145
146=item B<print_long_double>
147
148Test that a C<long double> is formatted correctly by
149C<printf>. Takes no arguments - the test value is hard-wired
150into the function (as "7").
151
152 print_long_double();
153
154Output is sent to STDOUT.
155
156=item B<have_long_double>
157
158Determine whether a C<long double> is supported by Perl. This should
159be used to determine whether to test C<print_long_double>.
160
161 print_long_double() if have_long_double;
162
163=item B<print_nv>
164
165Test that an C<NV> is formatted correctly by
166C<printf>.
167
168 print_nv( $val );
169
170Output is sent to STDOUT.
171
172=item B<print_iv>
173
174Test that an C<IV> is formatted correctly by
175C<printf>.
176
177 print_iv( $val );
178
179Output is sent to STDOUT.
180
181=item B<print_uv>
182
183Test that an C<UV> is formatted correctly by
184C<printf>.
185
186 print_uv( $val );
187
188Output is sent to STDOUT.
189
190=item B<print_int>
191
192Test that an C<int> is formatted correctly by
193C<printf>.
194
195 print_int( $val );
196
197Output is sent to STDOUT.
198
199=item B<print_long>
200
201Test that an C<long> is formatted correctly by
202C<printf>.
203
204 print_long( $val );
205
206Output is sent to STDOUT.
207
208=item B<print_float>
209
210Test that a single-precision floating point number is formatted
211correctly by C<printf>.
212
213 print_float( $val );
214
215Output is sent to STDOUT.
216
27fcb6ee
FC
217=item B<filter>
218
219Installs a source filter that substitutes "e" for "o" (witheut regard fer
220what it might be medifying).
221
d1f347d7
DM
222=item B<call_sv>, B<call_pv>, B<call_method>
223
224These exercise the C calls of the same names. Everything after the flags
04a9cc25 225arg is passed as the args to the called function. They return whatever
d1f347d7
DM
226the C function itself pushed onto the stack, plus the return value from
227the function; for example
228
229 call_sv( sub { @_, 'c' }, G_ARRAY, 'a', 'b'); # returns 'a', 'b', 'c', 3
230 call_sv( sub { @_ }, G_SCALAR, 'a', 'b'); # returns 'b', 1
231
232=item B<eval_sv>
233
3c4b39be 234Evaluates the passed SV. Result handling is done the same as for
d1f347d7
DM
235C<call_sv()> etc.
236
237=item B<eval_pv>
238
3c4b39be 239Exercises the C function of the same name in scalar context. Returns the
d1f347d7
DM
240same SV that the C function returns.
241
242=item B<require_pv>
243
3c4b39be 244Exercises the C function of the same name. Returns nothing.
d1f347d7 245
3e61d65a
JH
246=back
247
83f8bb56
Z
248=head1 KEYWORDS
249
250These are not supplied by default, but must be explicitly imported.
251They are lexically scoped.
252
253=over
254
255=item rpn(EXPRESSION)
256
257This construct is a Perl expression. I<EXPRESSION> must be an RPN
258arithmetic expression, as described below. The RPN expression is
259evaluated, and its value is returned as the value of the Perl expression.
260
261=item calcrpn VARIABLE { EXPRESSION }
262
263This construct is a complete Perl statement. (No semicolon should
264follow the closing brace.) I<VARIABLE> must be a Perl scalar C<my>
265variable, and I<EXPRESSION> must be an RPN arithmetic expression as
266described below. The RPN expression is evaluated, and its value is
267assigned to the variable.
268
269=back
270
271=head2 RPN expression syntax
272
273Tokens of an RPN expression may be separated by whitespace, but such
274separation is usually not required. It is required only where unseparated
275tokens would look like a longer token. For example, C<12 34 +> can be
276written as C<12 34+>, but not as C<1234 +>.
277
278An RPN expression may be any of:
279
280=over
281
282=item C<1234>
283
284A sequence of digits is an unsigned decimal literal number.
285
286=item C<$foo>
287
288An alphanumeric name preceded by dollar sign refers to a Perl scalar
289variable. Only variables declared with C<my> or C<state> are supported.
290If the variable's value is not a native integer, it will be converted
291to an integer, by Perl's usual mechanisms, at the time it is evaluated.
292
293=item I<A> I<B> C<+>
294
295Sum of I<A> and I<B>.
296
297=item I<A> I<B> C<->
298
299Difference of I<A> and I<B>, the result of subtracting I<B> from I<A>.
300
301=item I<A> I<B> C<*>
302
303Product of I<A> and I<B>.
304
305=item I<A> I<B> C</>
306
307Quotient when I<A> is divided by I<B>, rounded towards zero.
308Division by zero generates an exception.
309
310=item I<A> I<B> C<%>
311
312Remainder when I<A> is divided by I<B> with the quotient rounded towards zero.
313Division by zero generates an exception.
314
315=back
316
317Because the arithmetic operators all have fixed arity and are postfixed,
318there is no need for operator precedence, nor for a grouping operator
319to override precedence. This is half of the point of RPN.
320
321An RPN expression can also be interpreted in another way, as a sequence
322of operations on a stack, one operation per token. A literal or variable
323token pushes a value onto the stack. A binary operator pulls two items
324off the stack, performs a calculation with them, and pushes the result
325back onto the stack. The stack starts out empty, and at the end of the
326expression there must be exactly one value left on the stack.
327
3e61d65a
JH
328=head1 SEE ALSO
329
330L<XS::Typemap>, L<perlapi>.
331
332=head1 AUTHORS
333
334Tim Jenness, E<lt>t.jenness@jach.hawaii.eduE<gt>,
335Christian Soeller, E<lt>csoelle@mph.auckland.ac.nzE<gt>,
83f8bb56
Z
336Hugo van der Sanden E<lt>hv@crypt.compulink.co.ukE<gt>,
337Andrew Main (Zefram) <zefram@fysh.org>
3e61d65a
JH
338
339=head1 COPYRIGHT AND LICENSE
340
d1f347d7 341Copyright (C) 2002,2004 Tim Jenness, Christian Soeller, Hugo van der Sanden.
3e61d65a
JH
342All Rights Reserved.
343
83f8bb56
Z
344Copyright (C) 2009 Andrew Main (Zefram) <zefram@fysh.org>
345
3e61d65a
JH
346This library is free software; you can redistribute it and/or modify
347it under the same terms as Perl itself.
348
349=cut