(Retracted by #8264) More join() testing which was good because
[p5sagit/p5-mst-13.2.git] / t / comp / proto.t
CommitLineData
28757baa 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.
44a8e56a 10#
11
0e57f998 12# XXX known to leak scalars
13$ENV{PERL_DESTRUCT_LEVEL} = 0 unless $ENV{PERL_DESTRUCT_LEVEL} > 3;
14
44a8e56a 15BEGIN {
16 chdir 't' if -d 't';
20822f61 17 @INC = '../lib';
44a8e56a 18}
28757baa 19
20use strict;
21
36a5d4ba 22print "1..124\n";
28757baa 23
24my $i = 1;
25
26sub testing (&$) {
27 my $p = prototype(shift);
28 my $c = shift;
29 my $what = defined $c ? '(' . $p . ')' : 'no prototype';
30 print '#' x 25,"\n";
31 print '# Testing ',$what,"\n";
32 print '#' x 25,"\n";
33 print "not "
34 if((defined($p) && defined($c) && $p ne $c)
35 || (defined($p) != defined($c)));
36 printf "ok %d\n",$i++;
37}
38
39@_ = qw(a b c d);
40my @array;
41my %hash;
42
43##
44##
45##
46
47testing \&no_proto, undef;
48
49sub no_proto {
50 print "# \@_ = (",join(",",@_),")\n";
51 scalar(@_)
52}
53
54print "not " unless 0 == no_proto();
55printf "ok %d\n",$i++;
56
57print "not " unless 1 == no_proto(5);
58printf "ok %d\n",$i++;
59
60print "not " unless 4 == &no_proto;
61printf "ok %d\n",$i++;
62
63print "not " unless 1 == no_proto +6;
64printf "ok %d\n",$i++;
65
66print "not " unless 4 == no_proto(@_);
67printf "ok %d\n",$i++;
68
69##
70##
71##
72
73
74testing \&no_args, '';
75
76sub no_args () {
77 print "# \@_ = (",join(",",@_),")\n";
78 scalar(@_)
79}
80
81print "not " unless 0 == no_args();
82printf "ok %d\n",$i++;
83
84print "not " unless 0 == no_args;
85printf "ok %d\n",$i++;
86
87print "not " unless 5 == no_args +5;
88printf "ok %d\n",$i++;
89
90print "not " unless 4 == &no_args;
91printf "ok %d\n",$i++;
92
93print "not " unless 2 == &no_args(1,2);
94printf "ok %d\n",$i++;
95
96eval "no_args(1)";
97print "not " unless $@;
98printf "ok %d\n",$i++;
99
100##
101##
102##
103
104testing \&one_args, '$';
105
106sub one_args ($) {
107 print "# \@_ = (",join(",",@_),")\n";
108 scalar(@_)
109}
110
111print "not " unless 1 == one_args(1);
112printf "ok %d\n",$i++;
113
114print "not " unless 1 == one_args +5;
115printf "ok %d\n",$i++;
116
117print "not " unless 4 == &one_args;
118printf "ok %d\n",$i++;
119
120print "not " unless 2 == &one_args(1,2);
121printf "ok %d\n",$i++;
122
123eval "one_args(1,2)";
124print "not " unless $@;
125printf "ok %d\n",$i++;
126
127eval "one_args()";
128print "not " unless $@;
129printf "ok %d\n",$i++;
130
131sub one_a_args ($) {
132 print "# \@_ = (",join(",",@_),")\n";
133 print "not " unless @_ == 1 && $_[0] == 4;
134 printf "ok %d\n",$i++;
135}
136
137one_a_args(@_);
138
139##
140##
141##
142
143testing \&over_one_args, '$@';
144
145sub over_one_args ($@) {
146 print "# \@_ = (",join(",",@_),")\n";
147 scalar(@_)
148}
149
150print "not " unless 1 == over_one_args(1);
151printf "ok %d\n",$i++;
152
153print "not " unless 2 == over_one_args(1,2);
154printf "ok %d\n",$i++;
155
156print "not " unless 1 == over_one_args +5;
157printf "ok %d\n",$i++;
158
159print "not " unless 4 == &over_one_args;
160printf "ok %d\n",$i++;
161
162print "not " unless 2 == &over_one_args(1,2);
163printf "ok %d\n",$i++;
164
165print "not " unless 5 == &over_one_args(1,@_);
166printf "ok %d\n",$i++;
167
168eval "over_one_args()";
169print "not " unless $@;
170printf "ok %d\n",$i++;
171
172sub over_one_a_args ($@) {
173 print "# \@_ = (",join(",",@_),")\n";
174 print "not " unless @_ >= 1 && $_[0] == 4;
175 printf "ok %d\n",$i++;
176}
177
178over_one_a_args(@_);
179over_one_a_args(@_,1);
180over_one_a_args(@_,1,2);
181over_one_a_args(@_,@_);
182
183##
184##
185##
186
187testing \&scalar_and_hash, '$%';
188
189sub scalar_and_hash ($%) {
190 print "# \@_ = (",join(",",@_),")\n";
191 scalar(@_)
192}
193
194print "not " unless 1 == scalar_and_hash(1);
195printf "ok %d\n",$i++;
196
197print "not " unless 3 == scalar_and_hash(1,2,3);
198printf "ok %d\n",$i++;
199
200print "not " unless 1 == scalar_and_hash +5;
201printf "ok %d\n",$i++;
202
203print "not " unless 4 == &scalar_and_hash;
204printf "ok %d\n",$i++;
205
206print "not " unless 2 == &scalar_and_hash(1,2);
207printf "ok %d\n",$i++;
208
209print "not " unless 5 == &scalar_and_hash(1,@_);
210printf "ok %d\n",$i++;
211
212eval "scalar_and_hash()";
213print "not " unless $@;
214printf "ok %d\n",$i++;
215
216sub scalar_and_hash_a ($@) {
217 print "# \@_ = (",join(",",@_),")\n";
218 print "not " unless @_ >= 1 && $_[0] == 4;
219 printf "ok %d\n",$i++;
220}
221
222scalar_and_hash_a(@_);
223scalar_and_hash_a(@_,1);
224scalar_and_hash_a(@_,1,2);
225scalar_and_hash_a(@_,@_);
226
227##
228##
229##
230
231testing \&one_or_two, '$;$';
232
233sub one_or_two ($;$) {
234 print "# \@_ = (",join(",",@_),")\n";
235 scalar(@_)
236}
237
238print "not " unless 1 == one_or_two(1);
239printf "ok %d\n",$i++;
240
241print "not " unless 2 == one_or_two(1,3);
242printf "ok %d\n",$i++;
243
244print "not " unless 1 == one_or_two +5;
245printf "ok %d\n",$i++;
246
247print "not " unless 4 == &one_or_two;
248printf "ok %d\n",$i++;
249
250print "not " unless 3 == &one_or_two(1,2,3);
251printf "ok %d\n",$i++;
252
253print "not " unless 5 == &one_or_two(1,@_);
254printf "ok %d\n",$i++;
255
256eval "one_or_two()";
257print "not " unless $@;
258printf "ok %d\n",$i++;
259
260eval "one_or_two(1,2,3)";
261print "not " unless $@;
262printf "ok %d\n",$i++;
263
264sub one_or_two_a ($;$) {
265 print "# \@_ = (",join(",",@_),")\n";
266 print "not " unless @_ >= 1 && $_[0] == 4;
267 printf "ok %d\n",$i++;
268}
269
270one_or_two_a(@_);
271one_or_two_a(@_,1);
272one_or_two_a(@_,@_);
273
274##
275##
276##
277
278testing \&a_sub, '&';
279
280sub a_sub (&) {
281 print "# \@_ = (",join(",",@_),")\n";
282 &{$_[0]};
283}
284
285sub tmp_sub_1 { printf "ok %d\n",$i++ }
286
287a_sub { printf "ok %d\n",$i++ };
288a_sub \&tmp_sub_1;
289
290@array = ( \&tmp_sub_1 );
291eval 'a_sub @array';
292print "not " unless $@;
293printf "ok %d\n",$i++;
294
295##
296##
297##
298
75fc29ea 299testing \&a_subx, '\&';
300
301sub a_subx (\&) {
302 print "# \@_ = (",join(",",@_),")\n";
303 &{$_[0]};
304}
305
306sub tmp_sub_2 { printf "ok %d\n",$i++ }
307a_subx &tmp_sub_2;
308
309@array = ( \&tmp_sub_2 );
310eval 'a_subx @array';
311print "not " unless $@;
312printf "ok %d\n",$i++;
313
314##
315##
316##
317
28757baa 318testing \&sub_aref, '&\@';
319
320sub sub_aref (&\@) {
321 print "# \@_ = (",join(",",@_),")\n";
322 my($sub,$array) = @_;
323 print "not " unless @_ == 2 && @{$array} == 4;
324 print map { &{$sub}($_) } @{$array}
325}
326
327@array = (qw(O K)," ", $i++);
328sub_aref { lc shift } @array;
329print "\n";
330
331##
332##
333##
334
335testing \&sub_array, '&@';
336
337sub sub_array (&@) {
338 print "# \@_ = (",join(",",@_),")\n";
339 print "not " unless @_ == 5;
340 my $sub = shift;
341 print map { &{$sub}($_) } @_
342}
343
344@array = (qw(O K)," ", $i++);
345sub_array { lc shift } @array;
36a5d4ba 346sub_array { lc shift } ('O', 'K', ' ', $i++);
28757baa 347print "\n";
348
349##
350##
351##
352
353testing \&a_hash, '%';
354
355sub a_hash (%) {
356 print "# \@_ = (",join(",",@_),")\n";
357 scalar(@_);
358}
359
360print "not " unless 1 == a_hash 'a';
361printf "ok %d\n",$i++;
362
363print "not " unless 2 == a_hash 'a','b';
364printf "ok %d\n",$i++;
365
366##
367##
368##
369
370testing \&a_hash_ref, '\%';
371
372sub a_hash_ref (\%) {
373 print "# \@_ = (",join(",",@_),")\n";
374 print "not " unless ref($_[0]) && $_[0]->{'a'};
375 printf "ok %d\n",$i++;
376 $_[0]->{'b'} = 2;
377}
378
379%hash = ( a => 1);
380a_hash_ref %hash;
381print "not " unless $hash{'b'} == 2;
382printf "ok %d\n",$i++;
383
384##
385##
386##
387
69dcf70c 388testing \&array_ref_plus, '\@@';
28757baa 389
69dcf70c 390sub array_ref_plus (\@@) {
28757baa 391 print "# \@_ = (",join(",",@_),")\n";
69dcf70c 392 print "not " unless @_ == 2 && ref($_[0]) && 1 == @{$_[0]} && $_[1] eq 'x';
28757baa 393 printf "ok %d\n",$i++;
394 @{$_[0]} = (qw(ok)," ",$i++,"\n");
395}
396
397@array = ('a');
69dcf70c 398{ my @more = ('x');
399 array_ref_plus @array, @more; }
28757baa 400print "not " unless @array == 4;
401print @array;
fb73857a 402
b6c543e3 403my $p;
404print "not " if defined prototype('CORE::print');
405print "ok ", $i++, "\n";
406
407print "not " if defined prototype('CORE::system');
408print "ok ", $i++, "\n";
409
1c1fc3ea 410print "# CORE::open => ($p)\nnot " if ($p = prototype('CORE::open')) ne '*;$@';
b6c543e3 411print "ok ", $i++, "\n";
412
413print "# CORE:Foo => ($p), \$@ => `$@'\nnot "
ba5aeb3a 414 if defined ($p = eval { prototype('CORE::Foo') or 1 }) or $@ !~ /^Can't find an opnumber/;
b6c543e3 415print "ok ", $i++, "\n";
416
fb73857a 417# correctly note too-short parameter lists that don't end with '$',
418# a possible regression.
419
420sub foo1 ($\@);
421eval q{ foo1 "s" };
422print "not " unless $@ =~ /^Not enough/;
423print "ok ", $i++, "\n";
424
425sub foo2 ($\%);
426eval q{ foo2 "s" };
427print "not " unless $@ =~ /^Not enough/;
428print "ok ", $i++, "\n";
57ff9a15 429
430sub X::foo3;
431*X::foo3 = sub {'ok'};
432print "# $@not " unless eval {X->foo3} eq 'ok';
433print "ok ", $i++, "\n";
434
435sub X::foo4 ($);
436*X::foo4 = sub ($) {'ok'};
437print "not " unless X->foo4 eq 'ok';
438print "ok ", $i++, "\n";
2ba6ecf4 439
440# test if the (*) prototype allows barewords, constants, scalar expressions,
441# globs and globrefs (just as CORE::open() does), all under stricture
442sub star (*&) { &{$_[1]} }
18228614 443sub star2 (**&) { &{$_[2]} }
444sub BAR { "quux" }
2692f720 445sub Bar::BAZ { "quuz" }
2ba6ecf4 446my $star = 'FOO';
447star FOO, sub { print "ok $i\n" if $_[0] eq 'FOO' }; $i++;
18228614 448star(FOO, sub { print "ok $i\n" if $_[0] eq 'FOO' }); $i++;
2ba6ecf4 449star "FOO", sub { print "ok $i\n" if $_[0] eq 'FOO' }; $i++;
18228614 450star("FOO", sub { print "ok $i\n" if $_[0] eq 'FOO' }); $i++;
2ba6ecf4 451star $star, sub { print "ok $i\n" if $_[0] eq 'FOO' }; $i++;
18228614 452star($star, sub { print "ok $i\n" if $_[0] eq 'FOO' }); $i++;
2ba6ecf4 453star *FOO, sub { print "ok $i\n" if $_[0] eq \*FOO }; $i++;
18228614 454star(*FOO, sub { print "ok $i\n" if $_[0] eq \*FOO }); $i++;
2ba6ecf4 455star \*FOO, sub { print "ok $i\n" if $_[0] eq \*FOO }; $i++;
18228614 456star(\*FOO, sub { print "ok $i\n" if $_[0] eq \*FOO }); $i++;
457star2 FOO, BAR, sub { print "ok $i\n"
458 if $_[0] eq 'FOO' and $_[1] eq 'BAR' }; $i++;
2692f720 459star2(Bar::BAZ, FOO, sub { print "ok $i\n"
460 if $_[0] eq 'Bar::BAZ' and $_[1] eq 'FOO' }); $i++;
18228614 461star2 BAR(), FOO, sub { print "ok $i\n"
462 if $_[0] eq 'quux' and $_[1] eq 'FOO' }; $i++;
463star2(FOO, BAR(), sub { print "ok $i\n"
464 if $_[0] eq 'FOO' and $_[1] eq 'quux' }); $i++;
465star2 "FOO", "BAR", sub { print "ok $i\n"
466 if $_[0] eq 'FOO' and $_[1] eq 'BAR' }; $i++;
467star2("FOO", "BAR", sub { print "ok $i\n"
468 if $_[0] eq 'FOO' and $_[1] eq 'BAR' }); $i++;
469star2 $star, $star, sub { print "ok $i\n"
470 if $_[0] eq 'FOO' and $_[1] eq 'FOO' }; $i++;
471star2($star, $star, sub { print "ok $i\n"
472 if $_[0] eq 'FOO' and $_[1] eq 'FOO' }); $i++;
473star2 *FOO, *BAR, sub { print "ok $i\n"
1c01eb51 474 if $_[0] eq \*FOO and $_[1] eq \*BAR }; $i++;
18228614 475star2(*FOO, *BAR, sub { print "ok $i\n"
1c01eb51 476 if $_[0] eq \*FOO and $_[1] eq \*BAR }); $i++;
18228614 477star2 \*FOO, \*BAR, sub { no strict 'refs'; print "ok $i\n"
1c01eb51 478 if $_[0] eq \*{'FOO'} and $_[1] eq \*{'BAR'} }; $i++;
18228614 479star2(\*FOO, \*BAR, sub { no strict 'refs'; print "ok $i\n"
1c01eb51 480 if $_[0] eq \*{'FOO'} and $_[1] eq \*{'BAR'} }); $i++;
18228614 481
1c01eb51 482# test scalarref prototype
483sub sreftest (\$$) {
484 print "ok $_[1]\n" if ref $_[0];
485}
486{
487 no strict 'vars';
488 sreftest my $sref, $i++;
489 sreftest($helem{$i}, $i++);
490 sreftest $aelem[0], $i++;
491}
c2b35b10 492
493# test prototypes when they are evaled and there is a syntax error
5279fd7b 494#
c2b35b10 495for my $p ( "", qw{ () ($) ($@) ($%) ($;$) (&) (&\@) (&@) (%) (\%) (\@) } ) {
496 no warnings 'redefine';
497 my $eval = "sub evaled_subroutine $p { &void *; }";
498 eval $eval;
499 print "# eval[$eval]\nnot " unless $@ && $@ =~ /syntax error/;
500 print "ok ", $i++, "\n";
501}
337449a8 502
503# Not $$;$;$
504print "not " unless prototype "CORE::substr" eq '$$;$$';
505print "ok ", $i++, "\n";