Win32 PerlIO intermediate state now working as expected.
[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
c2b35b10 22print "1..122\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;
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
69dcf70c 387testing \&array_ref_plus, '\@@';
28757baa 388
69dcf70c 389sub array_ref_plus (\@@) {
28757baa 390 print "# \@_ = (",join(",",@_),")\n";
69dcf70c 391 print "not " unless @_ == 2 && ref($_[0]) && 1 == @{$_[0]} && $_[1] eq 'x';
28757baa 392 printf "ok %d\n",$i++;
393 @{$_[0]} = (qw(ok)," ",$i++,"\n");
394}
395
396@array = ('a');
69dcf70c 397{ my @more = ('x');
398 array_ref_plus @array, @more; }
28757baa 399print "not " unless @array == 4;
400print @array;
fb73857a 401
b6c543e3 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
1c1fc3ea 409print "# CORE::open => ($p)\nnot " if ($p = prototype('CORE::open')) ne '*;$@';
b6c543e3 410print "ok ", $i++, "\n";
411
412print "# CORE:Foo => ($p), \$@ => `$@'\nnot "
ba5aeb3a 413 if defined ($p = eval { prototype('CORE::Foo') or 1 }) or $@ !~ /^Can't find an opnumber/;
b6c543e3 414print "ok ", $i++, "\n";
415
fb73857a 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";
57ff9a15 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";
2ba6ecf4 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]} }
18228614 442sub star2 (**&) { &{$_[2]} }
443sub BAR { "quux" }
2692f720 444sub Bar::BAZ { "quuz" }
2ba6ecf4 445my $star = 'FOO';
446star FOO, sub { print "ok $i\n" if $_[0] eq 'FOO' }; $i++;
18228614 447star(FOO, sub { print "ok $i\n" if $_[0] eq 'FOO' }); $i++;
2ba6ecf4 448star "FOO", sub { print "ok $i\n" if $_[0] eq 'FOO' }; $i++;
18228614 449star("FOO", sub { print "ok $i\n" if $_[0] eq 'FOO' }); $i++;
2ba6ecf4 450star $star, sub { print "ok $i\n" if $_[0] eq 'FOO' }; $i++;
18228614 451star($star, sub { print "ok $i\n" if $_[0] eq 'FOO' }); $i++;
2ba6ecf4 452star *FOO, sub { print "ok $i\n" if $_[0] eq \*FOO }; $i++;
18228614 453star(*FOO, sub { print "ok $i\n" if $_[0] eq \*FOO }); $i++;
2ba6ecf4 454star \*FOO, sub { print "ok $i\n" if $_[0] eq \*FOO }; $i++;
18228614 455star(\*FOO, sub { print "ok $i\n" if $_[0] eq \*FOO }); $i++;
456star2 FOO, BAR, sub { print "ok $i\n"
457 if $_[0] eq 'FOO' and $_[1] eq 'BAR' }; $i++;
2692f720 458star2(Bar::BAZ, FOO, sub { print "ok $i\n"
459 if $_[0] eq 'Bar::BAZ' and $_[1] eq 'FOO' }); $i++;
18228614 460star2 BAR(), FOO, sub { print "ok $i\n"
461 if $_[0] eq 'quux' and $_[1] eq 'FOO' }; $i++;
462star2(FOO, BAR(), sub { print "ok $i\n"
463 if $_[0] eq 'FOO' and $_[1] eq 'quux' }); $i++;
464star2 "FOO", "BAR", sub { print "ok $i\n"
465 if $_[0] eq 'FOO' and $_[1] eq 'BAR' }; $i++;
466star2("FOO", "BAR", sub { print "ok $i\n"
467 if $_[0] eq 'FOO' and $_[1] eq 'BAR' }); $i++;
468star2 $star, $star, sub { print "ok $i\n"
469 if $_[0] eq 'FOO' and $_[1] eq 'FOO' }; $i++;
470star2($star, $star, sub { print "ok $i\n"
471 if $_[0] eq 'FOO' and $_[1] eq 'FOO' }); $i++;
472star2 *FOO, *BAR, sub { print "ok $i\n"
1c01eb51 473 if $_[0] eq \*FOO and $_[1] eq \*BAR }; $i++;
18228614 474star2(*FOO, *BAR, sub { print "ok $i\n"
1c01eb51 475 if $_[0] eq \*FOO and $_[1] eq \*BAR }); $i++;
18228614 476star2 \*FOO, \*BAR, sub { no strict 'refs'; print "ok $i\n"
1c01eb51 477 if $_[0] eq \*{'FOO'} and $_[1] eq \*{'BAR'} }; $i++;
18228614 478star2(\*FOO, \*BAR, sub { no strict 'refs'; print "ok $i\n"
1c01eb51 479 if $_[0] eq \*{'FOO'} and $_[1] eq \*{'BAR'} }); $i++;
18228614 480
1c01eb51 481# test scalarref prototype
482sub sreftest (\$$) {
483 print "ok $_[1]\n" if ref $_[0];
484}
485{
486 no strict 'vars';
487 sreftest my $sref, $i++;
488 sreftest($helem{$i}, $i++);
489 sreftest $aelem[0], $i++;
490}
c2b35b10 491
492# test prototypes when they are evaled and there is a syntax error
5279fd7b 493#
c2b35b10 494for my $p ( "", qw{ () ($) ($@) ($%) ($;$) (&) (&\@) (&@) (%) (\%) (\@) } ) {
495 no warnings 'redefine';
496 my $eval = "sub evaled_subroutine $p { &void *; }";
497 eval $eval;
498 print "# eval[$eval]\nnot " unless $@ && $@ =~ /syntax error/;
499 print "ok ", $i++, "\n";
500}