3 # Contributed by Graham Barr <Graham.Barr@tiuk.ti.com>
5 # So far there are tests for the following prototypes.
6 # none, () ($) ($@) ($%) ($;$) (&) (&\@) (&@) (%) (\%) (\@)
8 # It is impossible to test every prototype that can be specified, but
9 # we should test as many as we can.
18 my $p = prototype(shift);
20 my $what = defined $c ? '(' . $p . ')' : 'no prototype';
22 print '# Testing ',$what,"\n";
25 if((defined($p) && defined($c) && $p ne $c)
26 || (defined($p) != defined($c)));
27 printf "ok %d\n",$i++;
38 testing \&no_proto, undef;
41 print "# \@_ = (",join(",",@_),")\n";
45 print "not " unless 0 == no_proto();
46 printf "ok %d\n",$i++;
48 print "not " unless 1 == no_proto(5);
49 printf "ok %d\n",$i++;
51 print "not " unless 4 == &no_proto;
52 printf "ok %d\n",$i++;
54 print "not " unless 1 == no_proto +6;
55 printf "ok %d\n",$i++;
57 print "not " unless 4 == no_proto(@_);
58 printf "ok %d\n",$i++;
65 testing \&no_args, '';
68 print "# \@_ = (",join(",",@_),")\n";
72 print "not " unless 0 == no_args();
73 printf "ok %d\n",$i++;
75 print "not " unless 0 == no_args;
76 printf "ok %d\n",$i++;
78 print "not " unless 5 == no_args +5;
79 printf "ok %d\n",$i++;
81 print "not " unless 4 == &no_args;
82 printf "ok %d\n",$i++;
84 print "not " unless 2 == &no_args(1,2);
85 printf "ok %d\n",$i++;
88 print "not " unless $@;
89 printf "ok %d\n",$i++;
95 testing \&one_args, '$';
98 print "# \@_ = (",join(",",@_),")\n";
102 print "not " unless 1 == one_args(1);
103 printf "ok %d\n",$i++;
105 print "not " unless 1 == one_args +5;
106 printf "ok %d\n",$i++;
108 print "not " unless 4 == &one_args;
109 printf "ok %d\n",$i++;
111 print "not " unless 2 == &one_args(1,2);
112 printf "ok %d\n",$i++;
114 eval "one_args(1,2)";
115 print "not " unless $@;
116 printf "ok %d\n",$i++;
119 print "not " unless $@;
120 printf "ok %d\n",$i++;
123 print "# \@_ = (",join(",",@_),")\n";
124 print "not " unless @_ == 1 && $_[0] == 4;
125 printf "ok %d\n",$i++;
134 testing \&over_one_args, '$@';
136 sub over_one_args ($@) {
137 print "# \@_ = (",join(",",@_),")\n";
141 print "not " unless 1 == over_one_args(1);
142 printf "ok %d\n",$i++;
144 print "not " unless 2 == over_one_args(1,2);
145 printf "ok %d\n",$i++;
147 print "not " unless 1 == over_one_args +5;
148 printf "ok %d\n",$i++;
150 print "not " unless 4 == &over_one_args;
151 printf "ok %d\n",$i++;
153 print "not " unless 2 == &over_one_args(1,2);
154 printf "ok %d\n",$i++;
156 print "not " unless 5 == &over_one_args(1,@_);
157 printf "ok %d\n",$i++;
159 eval "over_one_args()";
160 print "not " unless $@;
161 printf "ok %d\n",$i++;
163 sub over_one_a_args ($@) {
164 print "# \@_ = (",join(",",@_),")\n";
165 print "not " unless @_ >= 1 && $_[0] == 4;
166 printf "ok %d\n",$i++;
170 over_one_a_args(@_,1);
171 over_one_a_args(@_,1,2);
172 over_one_a_args(@_,@_);
178 testing \&scalar_and_hash, '$%';
180 sub scalar_and_hash ($%) {
181 print "# \@_ = (",join(",",@_),")\n";
185 print "not " unless 1 == scalar_and_hash(1);
186 printf "ok %d\n",$i++;
188 print "not " unless 3 == scalar_and_hash(1,2,3);
189 printf "ok %d\n",$i++;
191 print "not " unless 1 == scalar_and_hash +5;
192 printf "ok %d\n",$i++;
194 print "not " unless 4 == &scalar_and_hash;
195 printf "ok %d\n",$i++;
197 print "not " unless 2 == &scalar_and_hash(1,2);
198 printf "ok %d\n",$i++;
200 print "not " unless 5 == &scalar_and_hash(1,@_);
201 printf "ok %d\n",$i++;
203 eval "scalar_and_hash()";
204 print "not " unless $@;
205 printf "ok %d\n",$i++;
207 sub scalar_and_hash_a ($@) {
208 print "# \@_ = (",join(",",@_),")\n";
209 print "not " unless @_ >= 1 && $_[0] == 4;
210 printf "ok %d\n",$i++;
213 scalar_and_hash_a(@_);
214 scalar_and_hash_a(@_,1);
215 scalar_and_hash_a(@_,1,2);
216 scalar_and_hash_a(@_,@_);
222 testing \&one_or_two, '$;$';
224 sub one_or_two ($;$) {
225 print "# \@_ = (",join(",",@_),")\n";
229 print "not " unless 1 == one_or_two(1);
230 printf "ok %d\n",$i++;
232 print "not " unless 2 == one_or_two(1,3);
233 printf "ok %d\n",$i++;
235 print "not " unless 1 == one_or_two +5;
236 printf "ok %d\n",$i++;
238 print "not " unless 4 == &one_or_two;
239 printf "ok %d\n",$i++;
241 print "not " unless 3 == &one_or_two(1,2,3);
242 printf "ok %d\n",$i++;
244 print "not " unless 5 == &one_or_two(1,@_);
245 printf "ok %d\n",$i++;
248 print "not " unless $@;
249 printf "ok %d\n",$i++;
251 eval "one_or_two(1,2,3)";
252 print "not " unless $@;
253 printf "ok %d\n",$i++;
255 sub one_or_two_a ($;$) {
256 print "# \@_ = (",join(",",@_),")\n";
257 print "not " unless @_ >= 1 && $_[0] == 4;
258 printf "ok %d\n",$i++;
269 testing \&a_sub, '&';
272 print "# \@_ = (",join(",",@_),")\n";
276 sub tmp_sub_1 { printf "ok %d\n",$i++ }
278 a_sub { printf "ok %d\n",$i++ };
281 @array = ( \&tmp_sub_1 );
283 print "not " unless $@;
284 printf "ok %d\n",$i++;
290 testing \&sub_aref, '&\@';
293 print "# \@_ = (",join(",",@_),")\n";
294 my($sub,$array) = @_;
295 print "not " unless @_ == 2 && @{$array} == 4;
296 print map { &{$sub}($_) } @{$array}
299 @array = (qw(O K)," ", $i++);
300 sub_aref { lc shift } @array;
307 testing \&sub_array, '&@';
310 print "# \@_ = (",join(",",@_),")\n";
311 print "not " unless @_ == 5;
313 print map { &{$sub}($_) } @_
316 @array = (qw(O K)," ", $i++);
317 sub_array { lc shift } @array;
324 testing \&a_hash, '%';
327 print "# \@_ = (",join(",",@_),")\n";
331 print "not " unless 1 == a_hash 'a';
332 printf "ok %d\n",$i++;
334 print "not " unless 2 == a_hash 'a','b';
335 printf "ok %d\n",$i++;
341 testing \&a_hash_ref, '\%';
343 sub a_hash_ref (\%) {
344 print "# \@_ = (",join(",",@_),")\n";
345 print "not " unless ref($_[0]) && $_[0]->{'a'};
346 printf "ok %d\n",$i++;
352 print "not " unless $hash{'b'} == 2;
353 printf "ok %d\n",$i++;
359 testing \&an_array_ref, '\@';
361 sub an_array_ref (\@) {
362 print "# \@_ = (",join(",",@_),")\n";
363 print "not " unless ref($_[0]) && 1 == @{$_[0]};
364 printf "ok %d\n",$i++;
365 @{$_[0]} = (qw(ok)," ",$i++,"\n");
370 print "not " unless @array == 4;