This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
New test: comp/proto.t
[perl5.git] / t / comp / proto.t
CommitLineData
8c1635e6
GB
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
11use strict;
12
13my $i = 1;
14
15##
16## Something really weird is happening here. Try changing the order
17## of the next three lines, and try moving them to after the definition
18## of testing, some combinations cause the script to fail while
19## running tests on (&\@)
20##
21
22my %hash;
23my @array;
24@_ = qw(a b c d);
25
26print "1..74\n";
27
28sub testing (&$) {
29 my $p = prototype(shift);
30 my $c = shift;
31 my $what = defined $c ? '(' . $p . ')' : 'no prototype';
32 print '#' x 25,"\n";
33 print '# Testing ',$what,"\n";
34 print '#' x 25,"\n";
35 print "not "
36 if((defined($p) && defined($c) && $p ne $c)
37 || (defined($p) != defined($c)));
38 printf "ok %d\n",$i++;
39}
40
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 \&sub_aref, '&\@';
299
300sub sub_aref (&\@) {
301 print "# \@_ = (",join(",",@_),")\n";
302 my($sub,$array) = @_;
303 print "not " unless @_ == 2 && @{$array} == 4;
304 print map { &{$sub}($_) } @{$array}
305}
306
307@array = (qw(O K)," ", $i++);
308sub_aref { lc shift } @array;
309print "\n";
310
311##
312##
313##
314
315testing \&sub_array, '&@';
316
317sub sub_array (&@) {
318 print "# \@_ = (",join(",",@_),")\n";
319 print "not " unless @_ == 5;
320 my $sub = shift;
321 print map { &{$sub}($_) } @_
322}
323
324@array = (qw(O K)," ", $i++);
325sub_array { lc shift } @array;
326print "\n";
327
328##
329##
330##
331
332testing \&a_hash, '%';
333
334sub a_hash (%) {
335 print "# \@_ = (",join(",",@_),")\n";
336 scalar(@_);
337}
338
339print "not " unless 1 == a_hash 'a';
340printf "ok %d\n",$i++;
341
342print "not " unless 2 == a_hash 'a','b';
343printf "ok %d\n",$i++;
344
345##
346##
347##
348
349testing \&a_hash_ref, '\%';
350
351sub a_hash_ref (\%) {
352 print "# \@_ = (",join(",",@_),")\n";
353 print "not " unless ref($_[0]) && $_[0]->{'a'};
354 printf "ok %d\n",$i++;
355 $_[0]->{'b'} = 2;
356}
357
358%hash = ( a => 1);
359a_hash_ref %hash;
360print "not " unless $hash{'b'} == 2;
361printf "ok %d\n",$i++;
362
363##
364##
365##
366
367testing \&an_array_ref, '\@';
368
369sub an_array_ref (\@) {
370 print "# \@_ = (",join(",",@_),")\n";
371 print "not " unless ref($_[0]) && 1 == @{$_[0]};
372 printf "ok %d\n",$i++;
373 @{$_[0]} = (qw(ok)," ",$i++,"\n");
374}
375
376@array = ('a');
377an_array_ref @array;
378print "not " unless @array == 4;
379print @array;