3 # Contributed by Graham Barr <Graham.Barr@tiuk.ti.com>
5 # So far there are tests for the following prototypes.
6 # none, () ($) ($@) ($%) ($;$) (&) (&\@) (&@) (%) (\%) (\@)
8 # It is impossible to test every prototype that can be specified, but
9 # we should test as many as we can.
12 # XXX known to leak scalars
13 $ENV{PERL_DESTRUCT_LEVEL} = 0 unless $ENV{PERL_DESTRUCT_LEVEL} > 3;
27 my $p = prototype(shift);
29 my $what = defined $c ? '(' . $p . ')' : 'no prototype';
31 print '# Testing ',$what,"\n";
34 if((defined($p) && defined($c) && $p ne $c)
35 || (defined($p) != defined($c)));
36 printf "ok %d\n",$i++;
47 testing \&no_proto, undef;
50 print "# \@_ = (",join(",",@_),")\n";
54 print "not " unless 0 == no_proto();
55 printf "ok %d\n",$i++;
57 print "not " unless 1 == no_proto(5);
58 printf "ok %d\n",$i++;
60 print "not " unless 4 == &no_proto;
61 printf "ok %d\n",$i++;
63 print "not " unless 1 == no_proto +6;
64 printf "ok %d\n",$i++;
66 print "not " unless 4 == no_proto(@_);
67 printf "ok %d\n",$i++;
74 testing \&no_args, '';
77 print "# \@_ = (",join(",",@_),")\n";
81 print "not " unless 0 == no_args();
82 printf "ok %d\n",$i++;
84 print "not " unless 0 == no_args;
85 printf "ok %d\n",$i++;
87 print "not " unless 5 == no_args +5;
88 printf "ok %d\n",$i++;
90 print "not " unless 4 == &no_args;
91 printf "ok %d\n",$i++;
93 print "not " unless 2 == &no_args(1,2);
94 printf "ok %d\n",$i++;
97 print "not " unless $@;
98 printf "ok %d\n",$i++;
104 testing \&one_args, '$';
107 print "# \@_ = (",join(",",@_),")\n";
111 print "not " unless 1 == one_args(1);
112 printf "ok %d\n",$i++;
114 print "not " unless 1 == one_args +5;
115 printf "ok %d\n",$i++;
117 print "not " unless 4 == &one_args;
118 printf "ok %d\n",$i++;
120 print "not " unless 2 == &one_args(1,2);
121 printf "ok %d\n",$i++;
123 eval "one_args(1,2)";
124 print "not " unless $@;
125 printf "ok %d\n",$i++;
128 print "not " unless $@;
129 printf "ok %d\n",$i++;
132 print "# \@_ = (",join(",",@_),")\n";
133 print "not " unless @_ == 1 && $_[0] == 4;
134 printf "ok %d\n",$i++;
143 testing \&over_one_args, '$@';
145 sub over_one_args ($@) {
146 print "# \@_ = (",join(",",@_),")\n";
150 print "not " unless 1 == over_one_args(1);
151 printf "ok %d\n",$i++;
153 print "not " unless 2 == over_one_args(1,2);
154 printf "ok %d\n",$i++;
156 print "not " unless 1 == over_one_args +5;
157 printf "ok %d\n",$i++;
159 print "not " unless 4 == &over_one_args;
160 printf "ok %d\n",$i++;
162 print "not " unless 2 == &over_one_args(1,2);
163 printf "ok %d\n",$i++;
165 print "not " unless 5 == &over_one_args(1,@_);
166 printf "ok %d\n",$i++;
168 eval "over_one_args()";
169 print "not " unless $@;
170 printf "ok %d\n",$i++;
172 sub over_one_a_args ($@) {
173 print "# \@_ = (",join(",",@_),")\n";
174 print "not " unless @_ >= 1 && $_[0] == 4;
175 printf "ok %d\n",$i++;
179 over_one_a_args(@_,1);
180 over_one_a_args(@_,1,2);
181 over_one_a_args(@_,@_);
187 testing \&scalar_and_hash, '$%';
189 sub scalar_and_hash ($%) {
190 print "# \@_ = (",join(",",@_),")\n";
194 print "not " unless 1 == scalar_and_hash(1);
195 printf "ok %d\n",$i++;
197 print "not " unless 3 == scalar_and_hash(1,2,3);
198 printf "ok %d\n",$i++;
200 print "not " unless 1 == scalar_and_hash +5;
201 printf "ok %d\n",$i++;
203 print "not " unless 4 == &scalar_and_hash;
204 printf "ok %d\n",$i++;
206 print "not " unless 2 == &scalar_and_hash(1,2);
207 printf "ok %d\n",$i++;
209 print "not " unless 5 == &scalar_and_hash(1,@_);
210 printf "ok %d\n",$i++;
212 eval "scalar_and_hash()";
213 print "not " unless $@;
214 printf "ok %d\n",$i++;
216 sub scalar_and_hash_a ($@) {
217 print "# \@_ = (",join(",",@_),")\n";
218 print "not " unless @_ >= 1 && $_[0] == 4;
219 printf "ok %d\n",$i++;
222 scalar_and_hash_a(@_);
223 scalar_and_hash_a(@_,1);
224 scalar_and_hash_a(@_,1,2);
225 scalar_and_hash_a(@_,@_);
231 testing \&one_or_two, '$;$';
233 sub one_or_two ($;$) {
234 print "# \@_ = (",join(",",@_),")\n";
238 print "not " unless 1 == one_or_two(1);
239 printf "ok %d\n",$i++;
241 print "not " unless 2 == one_or_two(1,3);
242 printf "ok %d\n",$i++;
244 print "not " unless 1 == one_or_two +5;
245 printf "ok %d\n",$i++;
247 print "not " unless 4 == &one_or_two;
248 printf "ok %d\n",$i++;
250 print "not " unless 3 == &one_or_two(1,2,3);
251 printf "ok %d\n",$i++;
253 print "not " unless 5 == &one_or_two(1,@_);
254 printf "ok %d\n",$i++;
257 print "not " unless $@;
258 printf "ok %d\n",$i++;
260 eval "one_or_two(1,2,3)";
261 print "not " unless $@;
262 printf "ok %d\n",$i++;
264 sub one_or_two_a ($;$) {
265 print "# \@_ = (",join(",",@_),")\n";
266 print "not " unless @_ >= 1 && $_[0] == 4;
267 printf "ok %d\n",$i++;
278 testing \&a_sub, '&';
281 print "# \@_ = (",join(",",@_),")\n";
285 sub tmp_sub_1 { printf "ok %d\n",$i++ }
287 a_sub { printf "ok %d\n",$i++ };
290 @array = ( \&tmp_sub_1 );
292 print "not " unless $@;
293 printf "ok %d\n",$i++;
299 testing \&a_subx, '\&';
302 print "# \@_ = (",join(",",@_),")\n";
306 sub tmp_sub_2 { printf "ok %d\n",$i++ }
309 @array = ( \&tmp_sub_2 );
310 eval 'a_subx @array';
311 print "not " unless $@;
312 printf "ok %d\n",$i++;
318 testing \&sub_aref, '&\@';
321 print "# \@_ = (",join(",",@_),")\n";
322 my($sub,$array) = @_;
323 print "not " unless @_ == 2 && @{$array} == 4;
324 print map { &{$sub}($_) } @{$array}
327 @array = (qw(O K)," ", $i++);
328 sub_aref { lc shift } @array;
335 testing \&sub_array, '&@';
338 print "# \@_ = (",join(",",@_),")\n";
339 print "not " unless @_ == 5;
341 print map { &{$sub}($_) } @_
344 @array = (qw(O K)," ", $i++);
345 sub_array { lc shift } @array;
346 sub_array { lc shift } ('O', 'K', ' ', $i++);
353 testing \&a_hash, '%';
356 print "# \@_ = (",join(",",@_),")\n";
360 print "not " unless 1 == a_hash 'a';
361 printf "ok %d\n",$i++;
363 print "not " unless 2 == a_hash 'a','b';
364 printf "ok %d\n",$i++;
370 testing \&a_hash_ref, '\%';
372 sub a_hash_ref (\%) {
373 print "# \@_ = (",join(",",@_),")\n";
374 print "not " unless ref($_[0]) && $_[0]->{'a'};
375 printf "ok %d\n",$i++;
381 print "not " unless $hash{'b'} == 2;
382 printf "ok %d\n",$i++;
388 testing \&array_ref_plus, '\@@';
390 sub array_ref_plus (\@@) {
391 print "# \@_ = (",join(",",@_),")\n";
392 print "not " unless @_ == 2 && ref($_[0]) && 1 == @{$_[0]} && $_[1] eq 'x';
393 printf "ok %d\n",$i++;
394 @{$_[0]} = (qw(ok)," ",$i++,"\n");
399 array_ref_plus @array, @more; }
400 print "not " unless @array == 4;
404 print "not " if defined prototype('CORE::print');
405 print "ok ", $i++, "\n";
407 print "not " if defined prototype('CORE::system');
408 print "ok ", $i++, "\n";
410 print "# CORE::open => ($p)\nnot " if ($p = prototype('CORE::open')) ne '*;$@';
411 print "ok ", $i++, "\n";
413 print "# CORE:Foo => ($p), \$@ => `$@'\nnot "
414 if defined ($p = eval { prototype('CORE::Foo') or 1 }) or $@ !~ /^Can't find an opnumber/;
415 print "ok ", $i++, "\n";
417 # correctly note too-short parameter lists that don't end with '$',
418 # a possible regression.
422 print "not " unless $@ =~ /^Not enough/;
423 print "ok ", $i++, "\n";
427 print "not " unless $@ =~ /^Not enough/;
428 print "ok ", $i++, "\n";
431 *X::foo3 = sub {'ok'};
432 print "# $@not " unless eval {X->foo3} eq 'ok';
433 print "ok ", $i++, "\n";
436 *X::foo4 = sub ($) {'ok'};
437 print "not " unless X->foo4 eq 'ok';
438 print "ok ", $i++, "\n";
440 # test if the (*) prototype allows barewords, constants, scalar expressions,
441 # globs and globrefs (just as CORE::open() does), all under stricture
442 sub star (*&) { &{$_[1]} }
443 sub star2 (**&) { &{$_[2]} }
445 sub Bar::BAZ { "quuz" }
447 star FOO, sub { print "ok $i\n" if $_[0] eq 'FOO' }; $i++;
448 star(FOO, sub { print "ok $i\n" if $_[0] eq 'FOO' }); $i++;
449 star "FOO", sub { print "ok $i\n" if $_[0] eq 'FOO' }; $i++;
450 star("FOO", sub { print "ok $i\n" if $_[0] eq 'FOO' }); $i++;
451 star $star, sub { print "ok $i\n" if $_[0] eq 'FOO' }; $i++;
452 star($star, sub { print "ok $i\n" if $_[0] eq 'FOO' }); $i++;
453 star *FOO, sub { print "ok $i\n" if $_[0] eq \*FOO }; $i++;
454 star(*FOO, sub { print "ok $i\n" if $_[0] eq \*FOO }); $i++;
455 star \*FOO, sub { print "ok $i\n" if $_[0] eq \*FOO }; $i++;
456 star(\*FOO, sub { print "ok $i\n" if $_[0] eq \*FOO }); $i++;
457 star2 FOO, BAR, sub { print "ok $i\n"
458 if $_[0] eq 'FOO' and $_[1] eq 'BAR' }; $i++;
459 star2(Bar::BAZ, FOO, sub { print "ok $i\n"
460 if $_[0] eq 'Bar::BAZ' and $_[1] eq 'FOO' }); $i++;
461 star2 BAR(), FOO, sub { print "ok $i\n"
462 if $_[0] eq 'quux' and $_[1] eq 'FOO' }; $i++;
463 star2(FOO, BAR(), sub { print "ok $i\n"
464 if $_[0] eq 'FOO' and $_[1] eq 'quux' }); $i++;
465 star2 "FOO", "BAR", sub { print "ok $i\n"
466 if $_[0] eq 'FOO' and $_[1] eq 'BAR' }; $i++;
467 star2("FOO", "BAR", sub { print "ok $i\n"
468 if $_[0] eq 'FOO' and $_[1] eq 'BAR' }); $i++;
469 star2 $star, $star, sub { print "ok $i\n"
470 if $_[0] eq 'FOO' and $_[1] eq 'FOO' }; $i++;
471 star2($star, $star, sub { print "ok $i\n"
472 if $_[0] eq 'FOO' and $_[1] eq 'FOO' }); $i++;
473 star2 *FOO, *BAR, sub { print "ok $i\n"
474 if $_[0] eq \*FOO and $_[1] eq \*BAR }; $i++;
475 star2(*FOO, *BAR, sub { print "ok $i\n"
476 if $_[0] eq \*FOO and $_[1] eq \*BAR }); $i++;
477 star2 \*FOO, \*BAR, sub { no strict 'refs'; print "ok $i\n"
478 if $_[0] eq \*{'FOO'} and $_[1] eq \*{'BAR'} }; $i++;
479 star2(\*FOO, \*BAR, sub { no strict 'refs'; print "ok $i\n"
480 if $_[0] eq \*{'FOO'} and $_[1] eq \*{'BAR'} }); $i++;
482 # test scalarref prototype
484 print "ok $_[1]\n" if ref $_[0];
488 sreftest my $sref, $i++;
489 sreftest($helem{$i}, $i++);
490 sreftest $aelem[0], $i++;
493 # test prototypes when they are evaled and there is a syntax error
494 # Byacc generates the string "syntax error". Bison gives the
495 # string "parse error".
497 for my $p ( "", qw{ () ($) ($@) ($%) ($;$) (&) (&\@) (&@) (%) (\%) (\@) } ) {
498 no warnings 'redefine';
499 my $eval = "sub evaled_subroutine $p { &void *; }";
501 print "# eval[$eval]\nnot " unless $@ && $@ =~ /(parse|syntax) error/i;
502 print "ok ", $i++, "\n";
506 print "not " unless prototype "CORE::substr" eq '$$;$$';
507 print "ok ", $i++, "\n";