10 # Export everything since these functions are only used by a test script
11 # Export subpackages too - in effect, export all their routines into us, then
12 # export everything from us.
15 croak ("Can't export for '$package'") unless $package eq __PACKAGE__;
17 @{$exports}{@_} = () if @_;
21 my @stashes = ('XS::APItest::', \%XS::APItest::);
22 while (my ($stash_name, $stash) = splice @stashes, 0, 2) {
23 while (my ($sym_name, $glob) = each %$stash) {
24 if ($sym_name =~ /::$/) {
25 # Skip any subpackages that are clearly OO
26 next if *{$glob}{HASH}{'new'};
27 push @stashes, "$stash_name$sym_name", *{$glob}{HASH};
28 } elsif (ref $glob eq 'SCALAR' || *{$glob}{CODE}) {
30 next if !exists $exports->{$sym_name};
31 delete $exports->{$sym_name};
34 *{"$callpkg\::$sym_name"} = \&{"$stash_name$sym_name"};
38 foreach (keys %{$exports||{}}) {
39 next unless /\A(?:rpn|calcrpn|stufftest|swaptwostmts|looprest|scopelessblock|stmtasexpr|stmtsasexpr|loopblock|blockasexpr|swaplabel|labelconst|arrayfullexpr|arraylistexpr|arraytermexpr|arrayarithexpr|arrayexprflags)\z/;
40 $^H{"XS::APItest/$_"} = 1;
41 delete $exports->{$_};
44 my @carp = keys %$exports;
47 (map "\"$_\" is not exported by the $package module\n", sort @carp),
48 "Can't continue after import errors");
53 our $VERSION = '0.27';
55 use vars '$WARNINGS_ON_BOOTSTRAP';
56 use vars map "\$${_}_called_PP", qw(BEGIN UNITCHECK CHECK INIT END);
59 # This is arguably a hack, but it disposes of the UNITCHECK block without
60 # needing to preprocess the source code
62 eval 'sub UNITCHECK (&) {}; 1' or die $@;
66 # Do these here to verify that XS code and Perl code get called at the same
72 $UNITCHECK_called_PP++;
75 # Need $W false by default, as some tests run under -w, and under -w we
76 # can get warnings about "Too late to run CHECK" block (and INIT block)
89 if ($WARNINGS_ON_BOOTSTRAP) {
92 # More CHECK and INIT blocks that could warn:
102 XS::APItest - Test the perl C API
109 use XS::APItest qw(rpn calcrpn);
110 $triangle = rpn($n $n 1 + * 2 /);
111 calcrpn $triangle { $n $n 1 + * 2 / }
115 This module tests the perl C API. Also exposes various bit of the perl
116 internals for the use of core test scripts.
120 This module can be used to check that the perl C API is behaving
121 correctly. This module provides test functions and an associated
122 test script that verifies the output.
124 This module is not meant to be installed.
128 Exports all the test functions:
132 =item B<print_double>
134 Test that a double-precision floating point number is formatted
135 correctly by C<printf>.
137 print_double( $val );
139 Output is sent to STDOUT.
141 =item B<print_long_double>
143 Test that a C<long double> is formatted correctly by
144 C<printf>. Takes no arguments - the test value is hard-wired
145 into the function (as "7").
149 Output is sent to STDOUT.
151 =item B<have_long_double>
153 Determine whether a C<long double> is supported by Perl. This should
154 be used to determine whether to test C<print_long_double>.
156 print_long_double() if have_long_double;
160 Test that an C<NV> is formatted correctly by
165 Output is sent to STDOUT.
169 Test that an C<IV> is formatted correctly by
174 Output is sent to STDOUT.
178 Test that an C<UV> is formatted correctly by
183 Output is sent to STDOUT.
187 Test that an C<int> is formatted correctly by
192 Output is sent to STDOUT.
196 Test that an C<long> is formatted correctly by
201 Output is sent to STDOUT.
205 Test that a single-precision floating point number is formatted
206 correctly by C<printf>.
210 Output is sent to STDOUT.
212 =item B<call_sv>, B<call_pv>, B<call_method>
214 These exercise the C calls of the same names. Everything after the flags
215 arg is passed as the the args to the called function. They return whatever
216 the C function itself pushed onto the stack, plus the return value from
217 the function; for example
219 call_sv( sub { @_, 'c' }, G_ARRAY, 'a', 'b'); # returns 'a', 'b', 'c', 3
220 call_sv( sub { @_ }, G_SCALAR, 'a', 'b'); # returns 'b', 1
224 Evaluates the passed SV. Result handling is done the same as for
229 Exercises the C function of the same name in scalar context. Returns the
230 same SV that the C function returns.
234 Exercises the C function of the same name. Returns nothing.
240 These are not supplied by default, but must be explicitly imported.
241 They are lexically scoped.
245 =item rpn(EXPRESSION)
247 This construct is a Perl expression. I<EXPRESSION> must be an RPN
248 arithmetic expression, as described below. The RPN expression is
249 evaluated, and its value is returned as the value of the Perl expression.
251 =item calcrpn VARIABLE { EXPRESSION }
253 This construct is a complete Perl statement. (No semicolon should
254 follow the closing brace.) I<VARIABLE> must be a Perl scalar C<my>
255 variable, and I<EXPRESSION> must be an RPN arithmetic expression as
256 described below. The RPN expression is evaluated, and its value is
257 assigned to the variable.
261 =head2 RPN expression syntax
263 Tokens of an RPN expression may be separated by whitespace, but such
264 separation is usually not required. It is required only where unseparated
265 tokens would look like a longer token. For example, C<12 34 +> can be
266 written as C<12 34+>, but not as C<1234 +>.
268 An RPN expression may be any of:
274 A sequence of digits is an unsigned decimal literal number.
278 An alphanumeric name preceded by dollar sign refers to a Perl scalar
279 variable. Only variables declared with C<my> or C<state> are supported.
280 If the variable's value is not a native integer, it will be converted
281 to an integer, by Perl's usual mechanisms, at the time it is evaluated.
285 Sum of I<A> and I<B>.
289 Difference of I<A> and I<B>, the result of subtracting I<B> from I<A>.
293 Product of I<A> and I<B>.
297 Quotient when I<A> is divided by I<B>, rounded towards zero.
298 Division by zero generates an exception.
302 Remainder when I<A> is divided by I<B> with the quotient rounded towards zero.
303 Division by zero generates an exception.
307 Because the arithmetic operators all have fixed arity and are postfixed,
308 there is no need for operator precedence, nor for a grouping operator
309 to override precedence. This is half of the point of RPN.
311 An RPN expression can also be interpreted in another way, as a sequence
312 of operations on a stack, one operation per token. A literal or variable
313 token pushes a value onto the stack. A binary operator pulls two items
314 off the stack, performs a calculation with them, and pushes the result
315 back onto the stack. The stack starts out empty, and at the end of the
316 expression there must be exactly one value left on the stack.
320 L<XS::Typemap>, L<perlapi>.
324 Tim Jenness, E<lt>t.jenness@jach.hawaii.eduE<gt>,
325 Christian Soeller, E<lt>csoelle@mph.auckland.ac.nzE<gt>,
326 Hugo van der Sanden E<lt>hv@crypt.compulink.co.ukE<gt>,
327 Andrew Main (Zefram) <zefram@fysh.org>
329 =head1 COPYRIGHT AND LICENSE
331 Copyright (C) 2002,2004 Tim Jenness, Christian Soeller, Hugo van der Sanden.
334 Copyright (C) 2009 Andrew Main (Zefram) <zefram@fysh.org>
336 This library is free software; you can redistribute it and/or modify
337 it under the same terms as Perl itself.