This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Newer -DLEAKTEST patch
[perl5.git] / t / comp / proto.t
CommitLineData
28757baa
PP
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
PP
10#
11
12BEGIN {
13 chdir 't' if -d 't';
14 @INC = '../lib';
15}
28757baa
PP
16
17use strict;
18
fb73857a 19print "1..76\n";
28757baa
PP
20
21my $i = 1;
22
23sub testing (&$) {
24 my $p = prototype(shift);
25 my $c = shift;
26 my $what = defined $c ? '(' . $p . ')' : 'no prototype';
27 print '#' x 25,"\n";
28 print '# Testing ',$what,"\n";
29 print '#' x 25,"\n";
30 print "not "
31 if((defined($p) && defined($c) && $p ne $c)
32 || (defined($p) != defined($c)));
33 printf "ok %d\n",$i++;
34}
35
36@_ = qw(a b c d);
37my @array;
38my %hash;
39
40##
41##
42##
43
44testing \&no_proto, undef;
45
46sub no_proto {
47 print "# \@_ = (",join(",",@_),")\n";
48 scalar(@_)
49}
50
51print "not " unless 0 == no_proto();
52printf "ok %d\n",$i++;
53
54print "not " unless 1 == no_proto(5);
55printf "ok %d\n",$i++;
56
57print "not " unless 4 == &no_proto;
58printf "ok %d\n",$i++;
59
60print "not " unless 1 == no_proto +6;
61printf "ok %d\n",$i++;
62
63print "not " unless 4 == no_proto(@_);
64printf "ok %d\n",$i++;
65
66##
67##
68##
69
70
71testing \&no_args, '';
72
73sub no_args () {
74 print "# \@_ = (",join(",",@_),")\n";
75 scalar(@_)
76}
77
78print "not " unless 0 == no_args();
79printf "ok %d\n",$i++;
80
81print "not " unless 0 == no_args;
82printf "ok %d\n",$i++;
83
84print "not " unless 5 == no_args +5;
85printf "ok %d\n",$i++;
86
87print "not " unless 4 == &no_args;
88printf "ok %d\n",$i++;
89
90print "not " unless 2 == &no_args(1,2);
91printf "ok %d\n",$i++;
92
93eval "no_args(1)";
94print "not " unless $@;
95printf "ok %d\n",$i++;
96
97##
98##
99##
100
101testing \&one_args, '$';
102
103sub one_args ($) {
104 print "# \@_ = (",join(",",@_),")\n";
105 scalar(@_)
106}
107
108print "not " unless 1 == one_args(1);
109printf "ok %d\n",$i++;
110
111print "not " unless 1 == one_args +5;
112printf "ok %d\n",$i++;
113
114print "not " unless 4 == &one_args;
115printf "ok %d\n",$i++;
116
117print "not " unless 2 == &one_args(1,2);
118printf "ok %d\n",$i++;
119
120eval "one_args(1,2)";
121print "not " unless $@;
122printf "ok %d\n",$i++;
123
124eval "one_args()";
125print "not " unless $@;
126printf "ok %d\n",$i++;
127
128sub one_a_args ($) {
129 print "# \@_ = (",join(",",@_),")\n";
130 print "not " unless @_ == 1 && $_[0] == 4;
131 printf "ok %d\n",$i++;
132}
133
134one_a_args(@_);
135
136##
137##
138##
139
140testing \&over_one_args, '$@';
141
142sub over_one_args ($@) {
143 print "# \@_ = (",join(",",@_),")\n";
144 scalar(@_)
145}
146
147print "not " unless 1 == over_one_args(1);
148printf "ok %d\n",$i++;
149
150print "not " unless 2 == over_one_args(1,2);
151printf "ok %d\n",$i++;
152
153print "not " unless 1 == over_one_args +5;
154printf "ok %d\n",$i++;
155
156print "not " unless 4 == &over_one_args;
157printf "ok %d\n",$i++;
158
159print "not " unless 2 == &over_one_args(1,2);
160printf "ok %d\n",$i++;
161
162print "not " unless 5 == &over_one_args(1,@_);
163printf "ok %d\n",$i++;
164
165eval "over_one_args()";
166print "not " unless $@;
167printf "ok %d\n",$i++;
168
169sub over_one_a_args ($@) {
170 print "# \@_ = (",join(",",@_),")\n";
171 print "not " unless @_ >= 1 && $_[0] == 4;
172 printf "ok %d\n",$i++;
173}
174
175over_one_a_args(@_);
176over_one_a_args(@_,1);
177over_one_a_args(@_,1,2);
178over_one_a_args(@_,@_);
179
180##
181##
182##
183
184testing \&scalar_and_hash, '$%';
185
186sub scalar_and_hash ($%) {
187 print "# \@_ = (",join(",",@_),")\n";
188 scalar(@_)
189}
190
191print "not " unless 1 == scalar_and_hash(1);
192printf "ok %d\n",$i++;
193
194print "not " unless 3 == scalar_and_hash(1,2,3);
195printf "ok %d\n",$i++;
196
197print "not " unless 1 == scalar_and_hash +5;
198printf "ok %d\n",$i++;
199
200print "not " unless 4 == &scalar_and_hash;
201printf "ok %d\n",$i++;
202
203print "not " unless 2 == &scalar_and_hash(1,2);
204printf "ok %d\n",$i++;
205
206print "not " unless 5 == &scalar_and_hash(1,@_);
207printf "ok %d\n",$i++;
208
209eval "scalar_and_hash()";
210print "not " unless $@;
211printf "ok %d\n",$i++;
212
213sub scalar_and_hash_a ($@) {
214 print "# \@_ = (",join(",",@_),")\n";
215 print "not " unless @_ >= 1 && $_[0] == 4;
216 printf "ok %d\n",$i++;
217}
218
219scalar_and_hash_a(@_);
220scalar_and_hash_a(@_,1);
221scalar_and_hash_a(@_,1,2);
222scalar_and_hash_a(@_,@_);
223
224##
225##
226##
227
228testing \&one_or_two, '$;$';
229
230sub one_or_two ($;$) {
231 print "# \@_ = (",join(",",@_),")\n";
232 scalar(@_)
233}
234
235print "not " unless 1 == one_or_two(1);
236printf "ok %d\n",$i++;
237
238print "not " unless 2 == one_or_two(1,3);
239printf "ok %d\n",$i++;
240
241print "not " unless 1 == one_or_two +5;
242printf "ok %d\n",$i++;
243
244print "not " unless 4 == &one_or_two;
245printf "ok %d\n",$i++;
246
247print "not " unless 3 == &one_or_two(1,2,3);
248printf "ok %d\n",$i++;
249
250print "not " unless 5 == &one_or_two(1,@_);
251printf "ok %d\n",$i++;
252
253eval "one_or_two()";
254print "not " unless $@;
255printf "ok %d\n",$i++;
256
257eval "one_or_two(1,2,3)";
258print "not " unless $@;
259printf "ok %d\n",$i++;
260
261sub one_or_two_a ($;$) {
262 print "# \@_ = (",join(",",@_),")\n";
263 print "not " unless @_ >= 1 && $_[0] == 4;
264 printf "ok %d\n",$i++;
265}
266
267one_or_two_a(@_);
268one_or_two_a(@_,1);
269one_or_two_a(@_,@_);
270
271##
272##
273##
274
275testing \&a_sub, '&';
276
277sub a_sub (&) {
278 print "# \@_ = (",join(",",@_),")\n";
279 &{$_[0]};
280}
281
282sub tmp_sub_1 { printf "ok %d\n",$i++ }
283
284a_sub { printf "ok %d\n",$i++ };
285a_sub \&tmp_sub_1;
286
287@array = ( \&tmp_sub_1 );
288eval 'a_sub @array';
289print "not " unless $@;
290printf "ok %d\n",$i++;
291
292##
293##
294##
295
296testing \&sub_aref, '&\@';
297
298sub sub_aref (&\@) {
299 print "# \@_ = (",join(",",@_),")\n";
300 my($sub,$array) = @_;
301 print "not " unless @_ == 2 && @{$array} == 4;
302 print map { &{$sub}($_) } @{$array}
303}
304
305@array = (qw(O K)," ", $i++);
306sub_aref { lc shift } @array;
307print "\n";
308
309##
310##
311##
312
313testing \&sub_array, '&@';
314
315sub sub_array (&@) {
316 print "# \@_ = (",join(",",@_),")\n";
317 print "not " unless @_ == 5;
318 my $sub = shift;
319 print map { &{$sub}($_) } @_
320}
321
322@array = (qw(O K)," ", $i++);
323sub_array { lc shift } @array;
324print "\n";
325
326##
327##
328##
329
330testing \&a_hash, '%';
331
332sub a_hash (%) {
333 print "# \@_ = (",join(",",@_),")\n";
334 scalar(@_);
335}
336
337print "not " unless 1 == a_hash 'a';
338printf "ok %d\n",$i++;
339
340print "not " unless 2 == a_hash 'a','b';
341printf "ok %d\n",$i++;
342
343##
344##
345##
346
347testing \&a_hash_ref, '\%';
348
349sub a_hash_ref (\%) {
350 print "# \@_ = (",join(",",@_),")\n";
351 print "not " unless ref($_[0]) && $_[0]->{'a'};
352 printf "ok %d\n",$i++;
353 $_[0]->{'b'} = 2;
354}
355
356%hash = ( a => 1);
357a_hash_ref %hash;
358print "not " unless $hash{'b'} == 2;
359printf "ok %d\n",$i++;
360
361##
362##
363##
364
69dcf70c 365testing \&array_ref_plus, '\@@';
28757baa 366
69dcf70c 367sub array_ref_plus (\@@) {
28757baa 368 print "# \@_ = (",join(",",@_),")\n";
69dcf70c 369 print "not " unless @_ == 2 && ref($_[0]) && 1 == @{$_[0]} && $_[1] eq 'x';
28757baa
PP
370 printf "ok %d\n",$i++;
371 @{$_[0]} = (qw(ok)," ",$i++,"\n");
372}
373
374@array = ('a');
69dcf70c
MB
375{ my @more = ('x');
376 array_ref_plus @array, @more; }
28757baa
PP
377print "not " unless @array == 4;
378print @array;
fb73857a
PP
379
380# correctly note too-short parameter lists that don't end with '$',
381# a possible regression.
382
383sub foo1 ($\@);
384eval q{ foo1 "s" };
385print "not " unless $@ =~ /^Not enough/;
386print "ok ", $i++, "\n";
387
388sub foo2 ($\%);
389eval q{ foo2 "s" };
390print "not " unless $@ =~ /^Not enough/;
391print "ok ", $i++, "\n";