PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/t/comp/proto.t

http://perl5005.googlecode.com/
Perl | 458 lines | 298 code | 110 blank | 50 comment | 86 complexity | 3ae23852d92160972b5b3b1172fea7d8 MD5 | raw file
Possible License(s): AGPL-1.0
  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. #
  11. BEGIN {
  12. chdir 't' if -d 't';
  13. @INC = '../lib';
  14. }
  15. use strict;
  16. print "1..100\n";
  17. my $i = 1;
  18. sub testing (&$) {
  19. my $p = prototype(shift);
  20. my $c = shift;
  21. my $what = defined $c ? '(' . $p . ')' : 'no prototype';
  22. print '#' x 25,"\n";
  23. print '# Testing ',$what,"\n";
  24. print '#' x 25,"\n";
  25. print "not "
  26. if((defined($p) && defined($c) && $p ne $c)
  27. || (defined($p) != defined($c)));
  28. printf "ok %d\n",$i++;
  29. }
  30. @_ = qw(a b c d);
  31. my @array;
  32. my %hash;
  33. ##
  34. ##
  35. ##
  36. testing \&no_proto, undef;
  37. sub no_proto {
  38. print "# \@_ = (",join(",",@_),")\n";
  39. scalar(@_)
  40. }
  41. print "not " unless 0 == no_proto();
  42. printf "ok %d\n",$i++;
  43. print "not " unless 1 == no_proto(5);
  44. printf "ok %d\n",$i++;
  45. print "not " unless 4 == &no_proto;
  46. printf "ok %d\n",$i++;
  47. print "not " unless 1 == no_proto +6;
  48. printf "ok %d\n",$i++;
  49. print "not " unless 4 == no_proto(@_);
  50. printf "ok %d\n",$i++;
  51. ##
  52. ##
  53. ##
  54. testing \&no_args, '';
  55. sub no_args () {
  56. print "# \@_ = (",join(",",@_),")\n";
  57. scalar(@_)
  58. }
  59. print "not " unless 0 == no_args();
  60. printf "ok %d\n",$i++;
  61. print "not " unless 0 == no_args;
  62. printf "ok %d\n",$i++;
  63. print "not " unless 5 == no_args +5;
  64. printf "ok %d\n",$i++;
  65. print "not " unless 4 == &no_args;
  66. printf "ok %d\n",$i++;
  67. print "not " unless 2 == &no_args(1,2);
  68. printf "ok %d\n",$i++;
  69. eval "no_args(1)";
  70. print "not " unless $@;
  71. printf "ok %d\n",$i++;
  72. ##
  73. ##
  74. ##
  75. testing \&one_args, '$';
  76. sub one_args ($) {
  77. print "# \@_ = (",join(",",@_),")\n";
  78. scalar(@_)
  79. }
  80. print "not " unless 1 == one_args(1);
  81. printf "ok %d\n",$i++;
  82. print "not " unless 1 == one_args +5;
  83. printf "ok %d\n",$i++;
  84. print "not " unless 4 == &one_args;
  85. printf "ok %d\n",$i++;
  86. print "not " unless 2 == &one_args(1,2);
  87. printf "ok %d\n",$i++;
  88. eval "one_args(1,2)";
  89. print "not " unless $@;
  90. printf "ok %d\n",$i++;
  91. eval "one_args()";
  92. print "not " unless $@;
  93. printf "ok %d\n",$i++;
  94. sub one_a_args ($) {
  95. print "# \@_ = (",join(",",@_),")\n";
  96. print "not " unless @_ == 1 && $_[0] == 4;
  97. printf "ok %d\n",$i++;
  98. }
  99. one_a_args(@_);
  100. ##
  101. ##
  102. ##
  103. testing \&over_one_args, '$@';
  104. sub over_one_args ($@) {
  105. print "# \@_ = (",join(",",@_),")\n";
  106. scalar(@_)
  107. }
  108. print "not " unless 1 == over_one_args(1);
  109. printf "ok %d\n",$i++;
  110. print "not " unless 2 == over_one_args(1,2);
  111. printf "ok %d\n",$i++;
  112. print "not " unless 1 == over_one_args +5;
  113. printf "ok %d\n",$i++;
  114. print "not " unless 4 == &over_one_args;
  115. printf "ok %d\n",$i++;
  116. print "not " unless 2 == &over_one_args(1,2);
  117. printf "ok %d\n",$i++;
  118. print "not " unless 5 == &over_one_args(1,@_);
  119. printf "ok %d\n",$i++;
  120. eval "over_one_args()";
  121. print "not " unless $@;
  122. printf "ok %d\n",$i++;
  123. sub over_one_a_args ($@) {
  124. print "# \@_ = (",join(",",@_),")\n";
  125. print "not " unless @_ >= 1 && $_[0] == 4;
  126. printf "ok %d\n",$i++;
  127. }
  128. over_one_a_args(@_);
  129. over_one_a_args(@_,1);
  130. over_one_a_args(@_,1,2);
  131. over_one_a_args(@_,@_);
  132. ##
  133. ##
  134. ##
  135. testing \&scalar_and_hash, '$%';
  136. sub scalar_and_hash ($%) {
  137. print "# \@_ = (",join(",",@_),")\n";
  138. scalar(@_)
  139. }
  140. print "not " unless 1 == scalar_and_hash(1);
  141. printf "ok %d\n",$i++;
  142. print "not " unless 3 == scalar_and_hash(1,2,3);
  143. printf "ok %d\n",$i++;
  144. print "not " unless 1 == scalar_and_hash +5;
  145. printf "ok %d\n",$i++;
  146. print "not " unless 4 == &scalar_and_hash;
  147. printf "ok %d\n",$i++;
  148. print "not " unless 2 == &scalar_and_hash(1,2);
  149. printf "ok %d\n",$i++;
  150. print "not " unless 5 == &scalar_and_hash(1,@_);
  151. printf "ok %d\n",$i++;
  152. eval "scalar_and_hash()";
  153. print "not " unless $@;
  154. printf "ok %d\n",$i++;
  155. sub scalar_and_hash_a ($@) {
  156. print "# \@_ = (",join(",",@_),")\n";
  157. print "not " unless @_ >= 1 && $_[0] == 4;
  158. printf "ok %d\n",$i++;
  159. }
  160. scalar_and_hash_a(@_);
  161. scalar_and_hash_a(@_,1);
  162. scalar_and_hash_a(@_,1,2);
  163. scalar_and_hash_a(@_,@_);
  164. ##
  165. ##
  166. ##
  167. testing \&one_or_two, '$;$';
  168. sub one_or_two ($;$) {
  169. print "# \@_ = (",join(",",@_),")\n";
  170. scalar(@_)
  171. }
  172. print "not " unless 1 == one_or_two(1);
  173. printf "ok %d\n",$i++;
  174. print "not " unless 2 == one_or_two(1,3);
  175. printf "ok %d\n",$i++;
  176. print "not " unless 1 == one_or_two +5;
  177. printf "ok %d\n",$i++;
  178. print "not " unless 4 == &one_or_two;
  179. printf "ok %d\n",$i++;
  180. print "not " unless 3 == &one_or_two(1,2,3);
  181. printf "ok %d\n",$i++;
  182. print "not " unless 5 == &one_or_two(1,@_);
  183. printf "ok %d\n",$i++;
  184. eval "one_or_two()";
  185. print "not " unless $@;
  186. printf "ok %d\n",$i++;
  187. eval "one_or_two(1,2,3)";
  188. print "not " unless $@;
  189. printf "ok %d\n",$i++;
  190. sub one_or_two_a ($;$) {
  191. print "# \@_ = (",join(",",@_),")\n";
  192. print "not " unless @_ >= 1 && $_[0] == 4;
  193. printf "ok %d\n",$i++;
  194. }
  195. one_or_two_a(@_);
  196. one_or_two_a(@_,1);
  197. one_or_two_a(@_,@_);
  198. ##
  199. ##
  200. ##
  201. testing \&a_sub, '&';
  202. sub a_sub (&) {
  203. print "# \@_ = (",join(",",@_),")\n";
  204. &{$_[0]};
  205. }
  206. sub tmp_sub_1 { printf "ok %d\n",$i++ }
  207. a_sub { printf "ok %d\n",$i++ };
  208. a_sub \&tmp_sub_1;
  209. @array = ( \&tmp_sub_1 );
  210. eval 'a_sub @array';
  211. print "not " unless $@;
  212. printf "ok %d\n",$i++;
  213. ##
  214. ##
  215. ##
  216. testing \&sub_aref, '&\@';
  217. sub sub_aref (&\@) {
  218. print "# \@_ = (",join(",",@_),")\n";
  219. my($sub,$array) = @_;
  220. print "not " unless @_ == 2 && @{$array} == 4;
  221. print map { &{$sub}($_) } @{$array}
  222. }
  223. @array = (qw(O K)," ", $i++);
  224. sub_aref { lc shift } @array;
  225. print "\n";
  226. ##
  227. ##
  228. ##
  229. testing \&sub_array, '&@';
  230. sub sub_array (&@) {
  231. print "# \@_ = (",join(",",@_),")\n";
  232. print "not " unless @_ == 5;
  233. my $sub = shift;
  234. print map { &{$sub}($_) } @_
  235. }
  236. @array = (qw(O K)," ", $i++);
  237. sub_array { lc shift } @array;
  238. print "\n";
  239. ##
  240. ##
  241. ##
  242. testing \&a_hash, '%';
  243. sub a_hash (%) {
  244. print "# \@_ = (",join(",",@_),")\n";
  245. scalar(@_);
  246. }
  247. print "not " unless 1 == a_hash 'a';
  248. printf "ok %d\n",$i++;
  249. print "not " unless 2 == a_hash 'a','b';
  250. printf "ok %d\n",$i++;
  251. ##
  252. ##
  253. ##
  254. testing \&a_hash_ref, '\%';
  255. sub a_hash_ref (\%) {
  256. print "# \@_ = (",join(",",@_),")\n";
  257. print "not " unless ref($_[0]) && $_[0]->{'a'};
  258. printf "ok %d\n",$i++;
  259. $_[0]->{'b'} = 2;
  260. }
  261. %hash = ( a => 1);
  262. a_hash_ref %hash;
  263. print "not " unless $hash{'b'} == 2;
  264. printf "ok %d\n",$i++;
  265. ##
  266. ##
  267. ##
  268. testing \&array_ref_plus, '\@@';
  269. sub array_ref_plus (\@@) {
  270. print "# \@_ = (",join(",",@_),")\n";
  271. print "not " unless @_ == 2 && ref($_[0]) && 1 == @{$_[0]} && $_[1] eq 'x';
  272. printf "ok %d\n",$i++;
  273. @{$_[0]} = (qw(ok)," ",$i++,"\n");
  274. }
  275. @array = ('a');
  276. { my @more = ('x');
  277. array_ref_plus @array, @more; }
  278. print "not " unless @array == 4;
  279. print @array;
  280. my $p;
  281. print "not " if defined prototype('CORE::print');
  282. print "ok ", $i++, "\n";
  283. print "not " if defined prototype('CORE::system');
  284. print "ok ", $i++, "\n";
  285. print "# CORE::open => ($p)\nnot " if ($p = prototype('CORE::open')) ne '*;$';
  286. print "ok ", $i++, "\n";
  287. print "# CORE:Foo => ($p), \$@ => `$@'\nnot "
  288. if defined ($p = eval { prototype('CORE::Foo') or 1 }) or $@ !~ /^Cannot find an opnumber/;
  289. print "ok ", $i++, "\n";
  290. # correctly note too-short parameter lists that don't end with '$',
  291. # a possible regression.
  292. sub foo1 ($\@);
  293. eval q{ foo1 "s" };
  294. print "not " unless $@ =~ /^Not enough/;
  295. print "ok ", $i++, "\n";
  296. sub foo2 ($\%);
  297. eval q{ foo2 "s" };
  298. print "not " unless $@ =~ /^Not enough/;
  299. print "ok ", $i++, "\n";
  300. sub X::foo3;
  301. *X::foo3 = sub {'ok'};
  302. print "# $@not " unless eval {X->foo3} eq 'ok';
  303. print "ok ", $i++, "\n";
  304. sub X::foo4 ($);
  305. *X::foo4 = sub ($) {'ok'};
  306. print "not " unless X->foo4 eq 'ok';
  307. print "ok ", $i++, "\n";
  308. # test if the (*) prototype allows barewords, constants, scalar expressions,
  309. # globs and globrefs (just as CORE::open() does), all under stricture
  310. sub star (*&) { &{$_[1]} }
  311. sub star2 (**&) { &{$_[2]} }
  312. sub BAR { "quux" }
  313. sub Bar::BAZ { "quuz" }
  314. my $star = 'FOO';
  315. star FOO, sub { print "ok $i\n" if $_[0] eq 'FOO' }; $i++;
  316. star(FOO, sub { print "ok $i\n" if $_[0] eq 'FOO' }); $i++;
  317. star "FOO", sub { print "ok $i\n" if $_[0] eq 'FOO' }; $i++;
  318. star("FOO", sub { print "ok $i\n" if $_[0] eq 'FOO' }); $i++;
  319. star $star, sub { print "ok $i\n" if $_[0] eq 'FOO' }; $i++;
  320. star($star, sub { print "ok $i\n" if $_[0] eq 'FOO' }); $i++;
  321. star *FOO, sub { print "ok $i\n" if $_[0] eq \*FOO }; $i++;
  322. star(*FOO, sub { print "ok $i\n" if $_[0] eq \*FOO }); $i++;
  323. star \*FOO, sub { print "ok $i\n" if $_[0] eq \*FOO }; $i++;
  324. star(\*FOO, sub { print "ok $i\n" if $_[0] eq \*FOO }); $i++;
  325. star2 FOO, BAR, sub { print "ok $i\n"
  326. if $_[0] eq 'FOO' and $_[1] eq 'BAR' }; $i++;
  327. star2(Bar::BAZ, FOO, sub { print "ok $i\n"
  328. if $_[0] eq 'Bar::BAZ' and $_[1] eq 'FOO' }); $i++;
  329. star2 BAR(), FOO, sub { print "ok $i\n"
  330. if $_[0] eq 'quux' and $_[1] eq 'FOO' }; $i++;
  331. star2(FOO, BAR(), sub { print "ok $i\n"
  332. if $_[0] eq 'FOO' and $_[1] eq 'quux' }); $i++;
  333. star2 "FOO", "BAR", sub { print "ok $i\n"
  334. if $_[0] eq 'FOO' and $_[1] eq 'BAR' }; $i++;
  335. star2("FOO", "BAR", sub { print "ok $i\n"
  336. if $_[0] eq 'FOO' and $_[1] eq 'BAR' }); $i++;
  337. star2 $star, $star, sub { print "ok $i\n"
  338. if $_[0] eq 'FOO' and $_[1] eq 'FOO' }; $i++;
  339. star2($star, $star, sub { print "ok $i\n"
  340. if $_[0] eq 'FOO' and $_[1] eq 'FOO' }); $i++;
  341. star2 *FOO, *BAR, sub { print "ok $i\n"
  342. if $_[0] eq \*FOO and $_[0] eq \*BAR }; $i++;
  343. star2(*FOO, *BAR, sub { print "ok $i\n"
  344. if $_[0] eq \*FOO and $_[0] eq \*BAR }); $i++;
  345. star2 \*FOO, \*BAR, sub { no strict 'refs'; print "ok $i\n"
  346. if $_[0] eq \*{'FOO'} and $_[0] eq \*{'BAR'} }; $i++;
  347. star2(\*FOO, \*BAR, sub { no strict 'refs'; print "ok $i\n"
  348. if $_[0] eq \*{'FOO'} and $_[0] eq \*{'BAR'} }); $i++;