xref: /openbsd-src/gnu/usr.bin/perl/t/comp/proto.t (revision d13be5d47e4149db2549a9828e244d59dbc43f15)
1#!./perl
2#
3# Contributed by Graham Barr <Graham.Barr@tiuk.ti.com>
4#
5# So far there are tests for the following prototypes.
6# none, () ($) ($@) ($%) ($;$) (&) (&\@) (&@) (%) (\%) (\@)
7#
8# It is impossible to test every prototype that can be specified, but
9# we should test as many as we can.
10#
11
12BEGIN {
13    chdir 't' if -d 't';
14    @INC = '../lib';
15}
16
17# We need this, as in places we're testing the interaction of prototypes with
18# strict
19use strict;
20
21print "1..153\n";
22
23my $i = 1;
24
25sub testing (&$) {
26    my $p = prototype(shift);
27    my $c = shift;
28    my $what = defined $c ? '(' . $p . ')' : 'no prototype';
29    print '#' x 25,"\n";
30    print '# Testing ',$what,"\n";
31    print '#' x 25,"\n";
32    print "not "
33	if((defined($p) && defined($c) && $p ne $c)
34	   || (defined($p) != defined($c)));
35    printf "ok %d\n",$i++;
36}
37
38@_ = qw(a b c d);
39my @array;
40my %hash;
41
42##
43##
44##
45
46testing \&no_proto, undef;
47
48sub no_proto {
49    print "# \@_ = (",join(",",@_),")\n";
50    scalar(@_)
51}
52
53print "not " unless 0 == no_proto();
54printf "ok %d\n",$i++;
55
56print "not " unless 1 == no_proto(5);
57printf "ok %d\n",$i++;
58
59print "not " unless 4 == &no_proto;
60printf "ok %d\n",$i++;
61
62print "not " unless 1 == no_proto +6;
63printf "ok %d\n",$i++;
64
65print "not " unless 4 == no_proto(@_);
66printf "ok %d\n",$i++;
67
68##
69##
70##
71
72
73testing \&no_args, '';
74
75sub no_args () {
76    print "# \@_ = (",join(",",@_),")\n";
77    scalar(@_)
78}
79
80print "not " unless 0 == no_args();
81printf "ok %d\n",$i++;
82
83print "not " unless 0 == no_args;
84printf "ok %d\n",$i++;
85
86print "not " unless 5 == no_args +5;
87printf "ok %d\n",$i++;
88
89print "not " unless 4 == &no_args;
90printf "ok %d\n",$i++;
91
92print "not " unless 2 == &no_args(1,2);
93printf "ok %d\n",$i++;
94
95eval "no_args(1)";
96print "not " unless $@;
97printf "ok %d\n",$i++;
98
99##
100##
101##
102
103testing \&one_args, '$';
104
105sub one_args ($) {
106    print "# \@_ = (",join(",",@_),")\n";
107    scalar(@_)
108}
109
110print "not " unless 1 == one_args(1);
111printf "ok %d\n",$i++;
112
113print "not " unless 1 == one_args +5;
114printf "ok %d\n",$i++;
115
116print "not " unless 4 == &one_args;
117printf "ok %d\n",$i++;
118
119print "not " unless 2 == &one_args(1,2);
120printf "ok %d\n",$i++;
121
122eval "one_args(1,2)";
123print "not " unless $@;
124printf "ok %d\n",$i++;
125
126eval "one_args()";
127print "not " unless $@;
128printf "ok %d\n",$i++;
129
130sub one_a_args ($) {
131    print "# \@_ = (",join(",",@_),")\n";
132    print "not " unless @_ == 1 && $_[0] == 4;
133    printf "ok %d\n",$i++;
134}
135
136one_a_args(@_);
137
138##
139##
140##
141
142testing \&over_one_args, '$@';
143
144sub over_one_args ($@) {
145    print "# \@_ = (",join(",",@_),")\n";
146    scalar(@_)
147}
148
149print "not " unless 1 == over_one_args(1);
150printf "ok %d\n",$i++;
151
152print "not " unless 2 == over_one_args(1,2);
153printf "ok %d\n",$i++;
154
155print "not " unless 1 == over_one_args +5;
156printf "ok %d\n",$i++;
157
158print "not " unless 4 == &over_one_args;
159printf "ok %d\n",$i++;
160
161print "not " unless 2 == &over_one_args(1,2);
162printf "ok %d\n",$i++;
163
164print "not " unless 5 == &over_one_args(1,@_);
165printf "ok %d\n",$i++;
166
167eval "over_one_args()";
168print "not " unless $@;
169printf "ok %d\n",$i++;
170
171sub over_one_a_args ($@) {
172    print "# \@_ = (",join(",",@_),")\n";
173    print "not " unless @_ >= 1 && $_[0] == 4;
174    printf "ok %d\n",$i++;
175}
176
177over_one_a_args(@_);
178over_one_a_args(@_,1);
179over_one_a_args(@_,1,2);
180over_one_a_args(@_,@_);
181
182##
183##
184##
185
186testing \&scalar_and_hash, '$%';
187
188sub scalar_and_hash ($%) {
189    print "# \@_ = (",join(",",@_),")\n";
190    scalar(@_)
191}
192
193print "not " unless 1 == scalar_and_hash(1);
194printf "ok %d\n",$i++;
195
196print "not " unless 3 == scalar_and_hash(1,2,3);
197printf "ok %d\n",$i++;
198
199print "not " unless 1 == scalar_and_hash +5;
200printf "ok %d\n",$i++;
201
202print "not " unless 4 == &scalar_and_hash;
203printf "ok %d\n",$i++;
204
205print "not " unless 2 == &scalar_and_hash(1,2);
206printf "ok %d\n",$i++;
207
208print "not " unless 5 == &scalar_and_hash(1,@_);
209printf "ok %d\n",$i++;
210
211eval "scalar_and_hash()";
212print "not " unless $@;
213printf "ok %d\n",$i++;
214
215sub scalar_and_hash_a ($@) {
216    print "# \@_ = (",join(",",@_),")\n";
217    print "not " unless @_ >= 1 && $_[0] == 4;
218    printf "ok %d\n",$i++;
219}
220
221scalar_and_hash_a(@_);
222scalar_and_hash_a(@_,1);
223scalar_and_hash_a(@_,1,2);
224scalar_and_hash_a(@_,@_);
225
226##
227##
228##
229
230testing \&one_or_two, '$;$';
231
232sub one_or_two ($;$) {
233    print "# \@_ = (",join(",",@_),")\n";
234    scalar(@_)
235}
236
237print "not " unless 1 == one_or_two(1);
238printf "ok %d\n",$i++;
239
240print "not " unless 2 == one_or_two(1,3);
241printf "ok %d\n",$i++;
242
243print "not " unless 1 == one_or_two +5;
244printf "ok %d\n",$i++;
245
246print "not " unless 4 == &one_or_two;
247printf "ok %d\n",$i++;
248
249print "not " unless 3 == &one_or_two(1,2,3);
250printf "ok %d\n",$i++;
251
252print "not " unless 5 == &one_or_two(1,@_);
253printf "ok %d\n",$i++;
254
255eval "one_or_two()";
256print "not " unless $@;
257printf "ok %d\n",$i++;
258
259eval "one_or_two(1,2,3)";
260print "not " unless $@;
261printf "ok %d\n",$i++;
262
263sub one_or_two_a ($;$) {
264    print "# \@_ = (",join(",",@_),")\n";
265    print "not " unless @_ >= 1 && $_[0] == 4;
266    printf "ok %d\n",$i++;
267}
268
269one_or_two_a(@_);
270one_or_two_a(@_,1);
271one_or_two_a(@_,@_);
272
273##
274##
275##
276
277testing \&a_sub, '&';
278
279sub a_sub (&) {
280    print "# \@_ = (",join(",",@_),")\n";
281    &{$_[0]};
282}
283
284sub tmp_sub_1 { printf "ok %d\n",$i++ }
285
286a_sub { printf "ok %d\n",$i++ };
287a_sub \&tmp_sub_1;
288
289@array = ( \&tmp_sub_1 );
290eval 'a_sub @array';
291print "not " unless $@;
292printf "ok %d\n",$i++;
293
294##
295##
296##
297
298testing \&a_subx, '\&';
299
300sub a_subx (\&) {
301    print "# \@_ = (",join(",",@_),")\n";
302    &{$_[0]};
303}
304
305sub tmp_sub_2 { printf "ok %d\n",$i++ }
306a_subx &tmp_sub_2;
307
308@array = ( \&tmp_sub_2 );
309eval 'a_subx @array';
310print "not " unless $@;
311printf "ok %d\n",$i++;
312
313##
314##
315##
316
317testing \&sub_aref, '&\@';
318
319sub sub_aref (&\@) {
320    print "# \@_ = (",join(",",@_),")\n";
321    my($sub,$array) = @_;
322    print "not " unless @_ == 2 && @{$array} == 4;
323    print map { &{$sub}($_) } @{$array}
324}
325
326@array = (qw(O K)," ", $i++);
327sub_aref { lc shift } @array;
328print "\n";
329
330##
331##
332##
333
334testing \&sub_array, '&@';
335
336sub sub_array (&@) {
337    print "# \@_ = (",join(",",@_),")\n";
338    print "not " unless @_ == 5;
339    my $sub = shift;
340    print map { &{$sub}($_) } @_
341}
342
343@array = (qw(O K)," ", $i++);
344sub_array { lc shift } @array;
345sub_array { lc shift } ('O', 'K', ' ', $i++);
346print "\n";
347
348##
349##
350##
351
352testing \&a_hash, '%';
353
354sub a_hash (%) {
355    print "# \@_ = (",join(",",@_),")\n";
356    scalar(@_);
357}
358
359print "not " unless 1 == a_hash 'a';
360printf "ok %d\n",$i++;
361
362print "not " unless 2 == a_hash 'a','b';
363printf "ok %d\n",$i++;
364
365##
366##
367##
368
369testing \&a_hash_ref, '\%';
370
371sub a_hash_ref (\%) {
372    print "# \@_ = (",join(",",@_),")\n";
373    print "not " unless ref($_[0]) && $_[0]->{'a'};
374    printf "ok %d\n",$i++;
375    $_[0]->{'b'} = 2;
376}
377
378%hash = ( a => 1);
379a_hash_ref %hash;
380print "not " unless $hash{'b'} == 2;
381printf "ok %d\n",$i++;
382
383##
384##
385##
386
387testing \&array_ref_plus, '\@@';
388
389sub array_ref_plus (\@@) {
390    print "# \@_ = (",join(",",@_),")\n";
391    print "not " unless @_ == 2 && ref($_[0]) && 1 == @{$_[0]} && $_[1] eq 'x';
392    printf "ok %d\n",$i++;
393    @{$_[0]} = (qw(ok)," ",$i++,"\n");
394}
395
396@array = ('a');
397{ my @more = ('x');
398  array_ref_plus @array, @more; }
399print "not " unless @array == 4;
400print @array;
401
402my $p;
403print "not " if defined prototype('CORE::print');
404print "ok ", $i++, "\n";
405
406print "not " if defined prototype('CORE::system');
407print "ok ", $i++, "\n";
408
409print "# CORE::open => ($p)\nnot " if ($p = prototype('CORE::open')) ne '*;$@';
410print "ok ", $i++, "\n";
411
412print "# CORE:Foo => ($p), \$@ => `$@'\nnot "
413    if defined ($p = eval { prototype('CORE::Foo') or 1 }) or $@ !~ /^Can't find an opnumber/;
414print "ok ", $i++, "\n";
415
416# correctly note too-short parameter lists that don't end with '$',
417#  a possible regression.
418
419sub foo1 ($\@);
420eval q{ foo1 "s" };
421print "not " unless $@ =~ /^Not enough/;
422print "ok ", $i++, "\n";
423
424sub foo2 ($\%);
425eval q{ foo2 "s" };
426print "not " unless $@ =~ /^Not enough/;
427print "ok ", $i++, "\n";
428
429sub X::foo3;
430*X::foo3 = sub {'ok'};
431print "# $@not " unless eval {X->foo3} eq 'ok';
432print "ok ", $i++, "\n";
433
434sub X::foo4 ($);
435*X::foo4 = sub ($) {'ok'};
436print "not " unless X->foo4 eq 'ok';
437print "ok ", $i++, "\n";
438
439# test if the (*) prototype allows barewords, constants, scalar expressions,
440# globs and globrefs (just as CORE::open() does), all under stricture
441sub star (*&) { &{$_[1]} }
442sub star2 (**&) { &{$_[2]} }
443sub BAR { "quux" }
444sub Bar::BAZ { "quuz" }
445my $star = 'FOO';
446star FOO, sub {
447    print "not " unless $_[0] eq 'FOO';
448    print "ok $i - star FOO\n";
449}; $i++;
450star(FOO, sub {
451	print "not " unless $_[0] eq 'FOO';
452	print "ok $i - star(FOO)\n";
453    }); $i++;
454star "FOO", sub {
455    print "not " unless $_[0] eq 'FOO';
456    print qq/ok $i - star "FOO"\n/;
457}; $i++;
458star("FOO", sub {
459	print "not " unless $_[0] eq 'FOO';
460	print qq/ok $i - star("FOO")\n/;
461    }); $i++;
462star $star, sub {
463    print "not " unless $_[0] eq 'FOO';
464    print "ok $i - star \$star\n";
465}; $i++;
466star($star, sub {
467	print "not " unless $_[0] eq 'FOO';
468	print "ok $i - star(\$star)\n";
469    }); $i++;
470star *FOO, sub {
471    print "not " unless $_[0] eq \*FOO;
472    print "ok $i - star *FOO\n";
473}; $i++;
474star(*FOO, sub {
475	print "not " unless $_[0] eq \*FOO;
476	print "ok $i - star(*FOO)\n";
477    }); $i++;
478star \*FOO, sub {
479    print "not " unless $_[0] eq \*FOO;
480    print "ok $i - star \\*FOO\n";
481}; $i++;
482star(\*FOO, sub {
483	print "not " unless $_[0] eq \*FOO;
484	print "ok $i - star(\\*FOO)\n";
485    }); $i++;
486star2 FOO, BAR, sub {
487    print "not " unless $_[0] eq 'FOO' and $_[1] eq 'BAR';
488    print "ok $i - star2 FOO, BAR\n";
489}; $i++;
490star2(Bar::BAZ, FOO, sub {
491	print "not " unless $_[0] eq 'Bar::BAZ' and $_[1] eq 'FOO';
492	print "ok $i - star2(Bar::BAZ, FOO)\n"
493    }); $i++;
494star2 BAR(), FOO, sub {
495    print "not " unless $_[0] eq 'quux' and $_[1] eq 'FOO';
496    print "ok $i - star2 BAR(), FOO\n"
497}; $i++;
498star2(FOO, BAR(), sub {
499	print "not " unless $_[0] eq 'FOO' and $_[1] eq 'quux';
500	print "ok $i - star2(FOO, BAR())\n";
501    }); $i++;
502star2 "FOO", "BAR", sub {
503    print "not " unless $_[0] eq 'FOO' and $_[1] eq 'BAR';
504    print qq/ok $i - star2 "FOO", "BAR"\n/;
505}; $i++;
506star2("FOO", "BAR", sub {
507	print "not " unless $_[0] eq 'FOO' and $_[1] eq 'BAR';
508	print qq/ok $i - star2("FOO", "BAR")\n/;
509    }); $i++;
510star2 $star, $star, sub {
511    print "not " unless $_[0] eq 'FOO' and $_[1] eq 'FOO';
512    print "ok $i - star2 \$star, \$star\n";
513}; $i++;
514star2($star, $star, sub {
515	print "not " unless $_[0] eq 'FOO' and $_[1] eq 'FOO';
516	print "ok $i - star2(\$star, \$star)\n";
517    }); $i++;
518star2 *FOO, *BAR, sub {
519    print "not " unless $_[0] eq \*FOO and $_[1] eq \*BAR;
520    print "ok $i - star2 *FOO, *BAR\n";
521}; $i++;
522star2(*FOO, *BAR, sub {
523	print "not " unless $_[0] eq \*FOO and $_[1] eq \*BAR;
524	print "ok $i - star2(*FOO, *BAR)\n";
525    }); $i++;
526star2 \*FOO, \*BAR, sub {
527    no strict 'refs';
528    print "not " unless $_[0] eq \*{'FOO'} and $_[1] eq \*{'BAR'};
529    print "ok $i - star2 \*FOO, \*BAR\n";
530}; $i++;
531star2(\*FOO, \*BAR, sub {
532	no strict 'refs';
533	print "not " unless $_[0] eq \*{'FOO'} and $_[1] eq \*{'BAR'};
534	print "ok $i - star2(\*FOO, \*BAR)\n";
535    }); $i++;
536
537# test scalarref prototype
538sub sreftest (\$$) {
539    print "not " unless ref $_[0];
540    print "ok $_[1] - sreftest\n";
541}
542{
543    no strict 'vars';
544    sreftest my $sref, $i++;
545    sreftest($helem{$i}, $i++);
546    sreftest $aelem[0], $i++;
547}
548
549# test prototypes when they are evaled and there is a syntax error
550# Byacc generates the string "syntax error".  Bison gives the
551# string "parse error".
552#
553for my $p ( "", qw{ () ($) ($@) ($%) ($;$) (&) (&\@) (&@) (%) (\%) (\@) } ) {
554  my $warn = "";
555  local $SIG{__WARN__} = sub {
556    my $thiswarn = join("",@_);
557    return if $thiswarn =~ /^Prototype mismatch: sub main::evaled_subroutine/;
558    $warn .= $thiswarn;
559  };
560  my $eval = "sub evaled_subroutine $p { &void *; }";
561  eval $eval;
562  print "# eval[$eval]\nnot " unless $@ && $@ =~ /(parse|syntax) error/i;
563  print "ok ", $i++, "\n";
564  if ($warn eq '') {
565     print "ok ", $i++, "\n";
566  } else {
567    print "not ok ", $i++, "# $warn \n";
568  }
569}
570
571# Not $$;$;$
572print "not " unless prototype "CORE::substr" eq '$$;$$';
573print "ok ", $i++, "\n";
574
575# recv takes a scalar reference for its second argument
576print "not " unless prototype "CORE::recv" eq '*\\$$$';
577print "ok ", $i++, "\n";
578
579{
580    my $myvar;
581    my @myarray;
582    my %myhash;
583    sub mysub { print "not calling mysub I hope\n" }
584    local *myglob;
585
586    sub myref (\[$@%&*]) { print "# $_[0]\n"; return "$_[0]" }
587
588    print "not " unless myref($myvar)   =~ /^SCALAR\(/;
589    print "ok ", $i++, "\n";
590    print "not " unless myref(@myarray) =~ /^ARRAY\(/;
591    print "ok ", $i++, "\n";
592    print "not " unless myref(%myhash)  =~ /^HASH\(/;
593    print "ok ", $i++, "\n";
594    print "not " unless myref(&mysub)   =~ /^CODE\(/;
595    print "ok ", $i++, "\n";
596    print "not " unless myref(*myglob)  =~ /^GLOB\(/;
597    print "ok ", $i++, "\n";
598
599    eval q/sub multi1 (\[%@]) { 1 } multi1 $myvar;/;
600    print "not "
601	unless $@ =~ /Type of arg 1 to main::multi1 must be one of \[%\@\] /;
602    print "ok ", $i++, "\n";
603    eval q/sub multi2 (\[$*&]) { 1 } multi2 @myarray;/;
604    print "not "
605	unless $@ =~ /Type of arg 1 to main::multi2 must be one of \[\$\*&\] /;
606    print "ok ", $i++, "\n";
607    eval q/sub multi3 (\[$@]) { 1 } multi3 %myhash;/;
608    print "not "
609	unless $@ =~ /Type of arg 1 to main::multi3 must be one of \[\$\@\] /;
610    print "ok ", $i++, "\n";
611    eval q/sub multi4 ($\[%]) { 1 } multi4 1, &mysub;/;
612    print "not "
613	unless $@ =~ /Type of arg 2 to main::multi4 must be one of \[%\] /;
614    print "ok ", $i++, "\n";
615    eval q/sub multi5 (\[$@]$) { 1 } multi5 *myglob;/;
616    print "not "
617	unless $@ =~ /Type of arg 1 to main::multi5 must be one of \[\$\@\] /
618	    && $@ =~ /Not enough arguments/;
619    print "ok ", $i++, "\n";
620}
621
622# check that obviously bad prototypes are getting warnings
623{
624  local $^W = 1;
625  my $warn = "";
626  local $SIG{__WARN__} = sub { $warn .= join("",@_) };
627
628  eval 'sub badproto (@bar) { 1; }';
629  print "not " unless $warn =~ /Illegal character in prototype for main::badproto : \@bar/;
630  print "ok ", $i++, "\n";
631
632  eval 'sub badproto2 (bar) { 1; }';
633  print "not " unless $warn =~ /Illegal character in prototype for main::badproto2 : bar/;
634  print "ok ", $i++, "\n";
635
636  eval 'sub badproto3 (&$bar$@) { 1; }';
637  print "not " unless $warn =~ /Illegal character in prototype for main::badproto3 : &\$bar\$\@/;
638  print "ok ", $i++, "\n";
639
640  eval 'sub badproto4 (@ $b ar) { 1; }';
641  print "not " unless $warn =~ /Illegal character in prototype for main::badproto4 : \@\$bar/;
642  print "ok ", $i++, "\n";
643}
644
645# make sure whitespace in prototypes works
646eval "sub good (\$\t\$\n\$) { 1; }";
647print "not " if $@;
648print "ok ", $i++, "\n";
649
650# Ought to fail, doesn't in 5.8.1.
651eval 'sub bug (\[%@]) {  } my $array = [0 .. 1]; bug %$array;';
652print "not " unless $@ =~ /Not a HASH reference/;
653print "ok ", $i++, "\n";
654