xref: /openbsd/gnu/usr.bin/perl/t/op/coreamp.t (revision 91f110e0)
1#!./perl
2
3# This file tests the results of calling subroutines in the CORE::
4# namespace with ampersand syntax.  In other words, it tests the bodies of
5# the subroutines themselves, not the ops that they might inline themselves
6# as when called as barewords.
7
8# Other tests for CORE subs are in coresubs.t
9
10BEGIN {
11    chdir 't' if -d 't';
12    @INC = qw(. ../lib);
13    require "test.pl";
14    $^P |= 0x100;
15}
16
17no warnings 'experimental::smartmatch';
18
19sub lis($$;$) {
20  &is(map(@$_ ? "[@{[map $_//'~~u~~', @$_]}]" : 'nought', @_[0,1]), $_[2]);
21}
22
23package hov {
24  use overload '%{}' => sub { +{} }
25}
26package sov {
27  use overload '${}' => sub { \my $x }
28}
29
30my %op_desc = (
31 evalbytes=> 'eval "string"',
32 join     => 'join or string',
33 pos      => 'match position',
34 prototype=> 'subroutine prototype',
35 readline => '<HANDLE>',
36 readpipe => 'quoted execution (``, qx)',
37 reset    => 'symbol reset',
38 ref      => 'reference-type operator',
39 undef    => 'undef operator',
40);
41sub op_desc($) {
42  return $op_desc{$_[0]} || $_[0];
43}
44
45
46# This tests that the &{} syntax respects the number of arguments implied
47# by the prototype, plus some extra tests for the (_) prototype.
48sub test_proto {
49  my($o) = shift;
50
51  # Create an alias, for the caller’s convenience.
52  *{"my$o"} = \&{"CORE::$o"};
53
54  my $p = prototype "CORE::$o";
55  $p = '$;$' if $p eq '$_';
56
57  if ($p eq '') {
58    $tests ++;
59
60    eval " &CORE::$o(1) ";
61    like $@, qr/^Too many arguments for $o at /, "&$o with too many args";
62
63  }
64  elsif ($p =~ /^_;?\z/) {
65    $tests ++;
66
67    eval " &CORE::$o(1,2) ";
68    my $desc = quotemeta op_desc($o);
69    like $@, qr/^Too many arguments for $desc at /,
70      "&$o with too many args";
71
72    if (!@_) { return }
73
74    $tests += 6;
75
76    my($in,$out) = @_; # for testing implied $_
77
78    # Since we have $in and $out values, we might as well test basic amper-
79    # sand calls, too.
80
81    is &{"CORE::$o"}($in), $out, "&$o";
82    lis [&{"CORE::$o"}($in)], [$out], "&$o in list context";
83
84    $_ = $in;
85    is &{"CORE::$o"}(), $out, "&$o with no args";
86
87    # Since there is special code to deal with lexical $_, make sure it
88    # works in all cases.
89    undef $_;
90    {
91      no warnings 'experimental::lexical_topic';
92      my $_ = $in;
93      is &{"CORE::$o"}(), $out, "&$o with no args uses lexical \$_";
94    }
95    # Make sure we get the right pad under recursion
96    my $r;
97    $r = sub {
98      if($_[0]) {
99        no warnings 'experimental::lexical_topic';
100        my $_ = $in;
101        is &{"CORE::$o"}(), $out,
102           "&$o with no args uses the right lexical \$_ under recursion";
103      }
104      else {
105        &$r(1)
106      }
107    };
108    &$r(0);
109    no warnings 'experimental::lexical_topic';
110    my $_ = $in;
111    eval {
112       is "CORE::$o"->(), $out, "&$o with the right lexical \$_ in an eval"
113    };
114  }
115  elsif ($p =~ '^;([$*]+)\z') { # ;$ ;* ;$$ etc.
116    my $maxargs = length $1;
117    $tests += 1;
118    eval " &CORE::$o((1)x($maxargs+1)) ";
119    my $desc = quotemeta op_desc($o);
120    like $@, qr/^Too many arguments for $desc at /,
121        "&$o with too many args";
122  }
123  elsif ($p =~ '^([$*]+);?\z') { # Fixed-length $$$ or ***
124    my $args = length $1;
125    $tests += 2;
126    my $desc = quotemeta op_desc($o);
127    eval " &CORE::$o((1)x($args-1)) ";
128    like $@, qr/^Not enough arguments for $desc at /, "&$o w/too few args";
129    eval " &CORE::$o((1)x($args+1)) ";
130    like $@, qr/^Too many arguments for $desc at /, "&$o w/too many args";
131  }
132  elsif ($p =~ '^([$*]+);([$*]+)\z') { # Variable-length $$$ or ***
133    my $minargs = length $1;
134    my $maxargs = $minargs + length $2;
135    $tests += 2;
136    eval " &CORE::$o((1)x($minargs-1)) ";
137    like $@, qr/^Not enough arguments for $o at /, "&$o with too few args";
138    eval " &CORE::$o((1)x($maxargs+1)) ";
139    like $@, qr/^Too many arguments for $o at /, "&$o with too many args";
140  }
141  elsif ($p eq '_;$') {
142    $tests += 1;
143
144    eval " &CORE::$o(1,2,3) ";
145    like $@, qr/^Too many arguments for $o at /, "&$o with too many args";
146  }
147  elsif ($p eq '@') {
148    # Do nothing, as we cannot test for too few or too many arguments.
149  }
150  elsif ($p =~ '^[$*;]+@\z') {
151    $tests ++;
152    $p =~ ';@';
153    my $minargs = $-[0];
154    eval " &CORE::$o((1)x($minargs-1)) ";
155    my $desc = quotemeta op_desc($o);
156    like $@, qr/^Not enough arguments for $desc at /,
157       "&$o with too few args";
158  }
159  elsif ($p =~ /^\*\\\$\$(;?)\$\z/) { #  *\$$$ and *\$$;$
160    $tests += 5;
161
162    eval "&CORE::$o(1,1,1,1,1)";
163    like $@, qr/^Too many arguments for $o at /,
164         "&$o with too many args";
165    eval " &CORE::$o((1)x(\$1?2:3)) ";
166    like $@, qr/^Not enough arguments for $o at /,
167         "&$o with too few args";
168    eval " &CORE::$o(1,[],1,1) ";
169    like $@, qr/^Type of arg 2 to &CORE::$o must be scalar reference at /,
170        "&$o with array ref arg";
171    eval " &CORE::$o(1,1,1,1) ";
172    like $@, qr/^Type of arg 2 to &CORE::$o must be scalar reference at /,
173        "&$o with scalar arg";
174    eval " &CORE::$o(1,bless([], 'sov'),1,1) ";
175    like $@, qr/^Type of arg 2 to &CORE::$o must be scalar reference at /,
176        "&$o with non-scalar arg w/scalar overload (which does not count)";
177  }
178  elsif ($p =~ /^\\%\$*\z/) { #  \% and \%$$
179    $tests += 5;
180
181    eval "&CORE::$o(" . join(",", (1) x length $p) . ")";
182    like $@, qr/^Too many arguments for $o at /,
183         "&$o with too many args";
184    eval " &CORE::$o(" . join(",", (1) x (length($p)-2)) . ") ";
185    like $@, qr/^Not enough arguments for $o at /,
186         "&$o with too few args";
187    my $moreargs = ",1" x (length($p) - 2);
188    eval " &CORE::$o([]$moreargs) ";
189    like $@, qr/^Type of arg 1 to &CORE::$o must be hash reference at /,
190        "&$o with array ref arg";
191    eval " &CORE::$o(*foo$moreargs) ";
192    like $@, qr/^Type of arg 1 to &CORE::$o must be hash reference at /,
193        "&$o with typeglob arg";
194    eval " &CORE::$o(bless([], 'hov')$moreargs) ";
195    like $@, qr/^Type of arg 1 to &CORE::$o must be hash reference at /,
196        "&$o with non-hash arg with hash overload (which does not count)";
197  }
198  elsif ($p =~ /^(;)?\\\[(\$\@%&?\*)](\$\@)?\z/) {
199    $tests += 3;
200
201    unless ($3) {
202      $tests ++;
203      eval " &CORE::$o(1,2) ";
204      like $@, qr/^Too many arguments for ${\op_desc($o)} at /,
205        "&$o with too many args";
206    }
207    unless ($1) {
208      $tests ++;
209      eval { &{"CORE::$o"}($3 ? 1 : ()) };
210      like $@, qr/^Not enough arguments for $o at /,
211         "&$o with too few args";
212    }
213    my $more_args = $3 ? ',1' : '';
214    eval " &CORE::$o(2$more_args) ";
215    like $@, qr/^Type of arg 1 to &CORE::$o must be reference to one of(?x:
216                ) \[\Q$2\E] at /,
217        "&$o with non-ref arg";
218    eval " &CORE::$o(*STDOUT{IO}$more_args) ";
219    like $@, qr/^Type of arg 1 to &CORE::$o must be reference to one of(?x:
220                ) \[\Q$2\E] at /,
221        "&$o with ioref arg";
222    my $class = ref *DATA{IO};
223    eval " &CORE::$o(bless(*DATA{IO}, 'hov')$more_args) ";
224    like $@, qr/^Type of arg 1 to &CORE::$o must be reference to one of(?x:
225                ) \[\Q$2\E] at /,
226        "&$o with ioref arg with hash overload (which does not count)";
227    bless *DATA{IO}, $class;
228    if (do {$2 !~ /&/}) {
229      $tests++;
230      eval " &CORE::$o(\\&scriggle$more_args) ";
231      like $@, qr/^Type of arg 1 to &CORE::$o must be reference to one (?x:
232                  )of \[\Q$2\E] at /,
233        "&$o with coderef arg";
234    }
235  }
236  elsif ($p eq ';\[$*]') {
237    $tests += 4;
238
239    my $desc = quotemeta op_desc($o);
240    eval " &CORE::$o(1,2) ";
241    like $@, qr/^Too many arguments for $desc at /,
242        "&$o with too many args";
243    eval " &CORE::$o([]) ";
244    like $@, qr/^Type of arg 1 to &CORE::$o must be scalar reference at /,
245        "&$o with array ref arg";
246    eval " &CORE::$o(1) ";
247    like $@, qr/^Type of arg 1 to &CORE::$o must be scalar reference at /,
248        "&$o with scalar arg";
249    eval " &CORE::$o(bless([], 'sov')) ";
250    like $@, qr/^Type of arg 1 to &CORE::$o must be scalar reference at /,
251        "&$o with non-scalar arg w/scalar overload (which does not count)";
252  }
253
254  else {
255    die "Please add tests for the $p prototype";
256  }
257}
258
259# Test that &CORE::foo calls without parentheses (no new @_) can handle the
260# total absence of any @_ without crashing.
261undef *_;
262&CORE::wantarray;
263$tests++;
264pass('no crash with &CORE::foo when *_{ARRAY} is undef');
265
266test_proto '__FILE__';
267test_proto '__LINE__';
268test_proto '__PACKAGE__';
269test_proto '__SUB__';
270
271is file(), 'frob'    , '__FILE__ does check its caller'   ; ++ $tests;
272is line(),  5        , '__LINE__ does check its caller'   ; ++ $tests;
273is pakg(), 'stribble', '__PACKAGE__ does check its caller'; ++ $tests;
274sub __SUB__test { &my__SUB__ }
275is __SUB__test, \&__SUB__test, '&__SUB__';                  ++ $tests;
276
277test_proto 'abs', -5, 5;
278
279test_proto 'accept';
280$tests += 6; eval q{
281  is &CORE::accept(qw{foo bar}), undef, "&accept";
282  lis [&{"CORE::accept"}(qw{foo bar})], [undef], "&accept in list context";
283
284  &myaccept(my $foo, my $bar);
285  is ref $foo, 'GLOB', 'CORE::accept autovivifies its first argument';
286  is $bar, undef, 'CORE::accept does not autovivify its second argument';
287  use strict;
288  undef $foo;
289  eval { 'myaccept'->($foo, $bar) };
290  like $@, qr/^Can't use an undefined value as a symbol reference at/,
291      'CORE::accept will not accept undef 2nd arg under strict';
292  is ref $foo, 'GLOB', 'CORE::accept autovivs its first arg under strict';
293};
294
295test_proto 'alarm';
296test_proto 'atan2';
297
298test_proto 'bind';
299$tests += 3;
300is &CORE::bind('foo', 'bear'), undef, "&bind";
301lis [&CORE::bind('foo', 'bear')], [undef], "&bind in list context";
302eval { &mybind(my $foo, "bear") };
303like $@, qr/^Bad symbol for filehandle at/,
304     'CORE::bind dies with undef first arg';
305
306test_proto 'binmode';
307$tests += 3;
308is &CORE::binmode(qw[foo bar]), undef, "&binmode";
309lis [&CORE::binmode(qw[foo bar])], [undef], "&binmode in list context";
310is &mybinmode(foo), undef, '&binmode with one arg';
311
312test_proto 'bless';
313$tests += 3;
314like &CORE::bless([],'parcel'), qr/^parcel=ARRAY/, "&bless";
315like join(" ", &CORE::bless([],'parcel')),
316     qr/^parcel=ARRAY(?!.* )/, "&bless in list context";
317like &mybless([]), qr/^main=ARRAY/, '&bless with one arg';
318
319test_proto 'break';
320{ $tests ++;
321  my $tmp;
322  CORE::given(1) {
323    CORE::when(1) {
324      &mybreak;
325      $tmp = 'bad';
326    }
327  }
328  is $tmp, undef, '&break';
329}
330
331test_proto 'caller';
332$tests += 4;
333sub caller_test {
334    is scalar &CORE::caller, 'hadhad', '&caller';
335    is scalar &CORE::caller(1), 'main', '&caller(1)';
336    lis [&CORE::caller], [caller], '&caller in list context';
337    # The last element of caller in list context is a hint hash, which
338    # may be a different hash for caller vs &CORE::caller, so an eq com-
339    # parison (which lis() uses for convenience) won’t work.  So just
340    # pop the last element, since the rest are sufficient to prove that
341    # &CORE::caller works.
342    my @ampcaller = &CORE::caller(1);
343    my @caller    = caller(1);
344    pop @ampcaller; pop @caller;
345    lis \@ampcaller, \@caller, '&caller(1) in list context';
346}
347sub {
348   package hadhad;
349   ::caller_test();
350}->();
351
352test_proto 'chmod';
353$tests += 3;
354is &CORE::chmod(), 0, '&chmod with no args';
355is &CORE::chmod(0666), 0, '&chmod';
356lis [&CORE::chmod(0666)], [0], '&chmod in list context';
357
358test_proto 'chown';
359$tests += 4;
360is &CORE::chown(), 0, '&chown with no args';
361is &CORE::chown(1), 0, '&chown with 1 arg';
362is &CORE::chown(1,2), 0, '&chown';
363lis [&CORE::chown(1,2)], [0], '&chown in list context';
364
365test_proto 'chr', 5, "\5";
366test_proto 'chroot';
367
368test_proto 'close';
369{
370  last if is_miniperl;
371  $tests += 3;
372
373  open my $fh, ">", \my $buffalo;
374  print $fh 'an address in the outskirts of Jersey';
375  ok &CORE::close($fh), '&CORE::close retval';
376  print $fh 'lalala';
377  is $buffalo, 'an address in the outskirts of Jersey',
378     'effect of &CORE::close';
379  # This has to be a separate variable from $fh, as re-using the same
380  # variable can cause the tests to pass by accident.  That actually hap-
381  # pened during developement, because the second close() was reading
382  # beyond the end of the stack and finding a $fh left over from before.
383  open my $fh2, ">", \($buffalo = '');
384  select+(select($fh2), do {
385     print "Nasusiro Tokasoni";
386     &CORE::close();
387     print "jfd";
388     is $buffalo, "Nasusiro Tokasoni", '&CORE::close with no args';
389  })[0];
390}
391lis [&CORE::close('tototootot')], [''], '&close in list context'; ++$tests;
392
393test_proto 'closedir';
394$tests += 2;
395is &CORE::closedir(foo), undef, '&CORE::closedir';
396lis [&CORE::closedir(foo)], [undef], '&CORE::closedir in list context';
397
398test_proto 'connect';
399$tests += 2;
400is &CORE::connect('foo','bar'), undef, '&connect';
401lis [&myconnect('foo','bar')], [undef], '&connect in list context';
402
403test_proto 'continue';
404$tests ++;
405CORE::given(1) {
406  CORE::when(1) {
407    &mycontinue();
408  }
409  pass "&continue";
410}
411
412test_proto 'cos';
413test_proto 'crypt';
414
415test_proto 'dbmclose';
416test_proto 'dbmopen';
417{
418  last unless eval { require AnyDBM_File };
419  $tests ++;
420  my $filename = tempfile();
421  &mydbmopen(\my %db, $filename, 0666);
422  $db{1} = 2; $db{3} = 4;
423  &mydbmclose(\%db);
424  is scalar keys %db, 0, '&dbmopen and &dbmclose';
425}
426
427test_proto 'die';
428eval { dier('quinquangle') };
429is $@, "quinquangle at frob line 6.\n", '&CORE::die'; $tests ++;
430
431test_proto $_ for qw(
432 endgrent endhostent endnetent endprotoent endpwent endservent
433);
434
435test_proto 'evalbytes';
436$tests += 4;
437{
438  chop(my $upgraded = "use utf8; '\xc4\x80'" . chr 256);
439  is &myevalbytes($upgraded), chr 256, '&evalbytes';
440  # Test hints
441  require strict;
442  strict->import;
443  &myevalbytes('
444    is someone, "someone", "run-time hint bits do not leak into &evalbytes"
445  ');
446  use strict;
447  BEGIN { $^H{coreamp} = 42 }
448  $^H{coreamp} = 75;
449  &myevalbytes('
450    BEGIN {
451      is $^H{coreamp}, 42, "compile-time hh propagates into &evalbytes";
452    }
453    ${"frobnicate"}
454  ');
455  like $@, qr/strict/, 'compile-time hint bits propagate into &evalbytes';
456}
457
458test_proto 'exit';
459$tests ++;
460is runperl(prog => '&CORE::exit; END { print qq-ok\n- }'), "ok\n",
461  '&exit with no args';
462
463test_proto 'fork';
464
465test_proto 'formline';
466$tests += 3;
467is &myformline(' @<<< @>>>', 1, 2), 1, '&myformline retval';
468is $^A,        ' 1       2', 'effect of &myformline';
469lis [&myformline('@')], [1], '&myformline in list context';
470
471test_proto 'exp';
472
473test_proto 'fc';
474$tests += 2;
475{
476  my $sharp_s = "\xdf";
477  is &myfc($sharp_s), $sharp_s, '&fc, no unicode_strings';
478  use feature 'unicode_strings';
479  is &myfc($sharp_s), "ss", '&fc, unicode_strings';
480}
481
482test_proto 'fcntl';
483
484test_proto 'fileno';
485$tests += 2;
486is &CORE::fileno(\*STDIN), fileno STDIN, '&CORE::fileno';
487lis [&CORE::fileno(\*STDIN)], [fileno STDIN], '&CORE::fileno in list cx';
488
489test_proto 'flock';
490test_proto 'fork';
491
492test_proto 'getc';
493{
494  last if is_miniperl;
495  $tests += 3;
496  local *STDIN;
497  open my $fh, "<", \(my $buf='falo');
498  open STDIN, "<", \(my $buf2 = 'bison');
499  is &mygetc($fh), 'f', '&mygetc';
500  is &mygetc(), 'b', '&mygetc with no args';
501  lis [&mygetc($fh)], ['a'], '&mygetc in list context';
502}
503
504test_proto "get$_" for qw '
505  grent grgid grnam hostbyaddr hostbyname hostent login netbyaddr netbyname
506  netent peername
507';
508
509test_proto 'getpgrp';
510eval {&mygetpgrp()};
511pass '&getpgrp with no args does not crash'; $tests++;
512
513test_proto "get$_" for qw '
514  ppid priority protobyname protobynumber protoent
515  pwent pwnam pwuid servbyname servbyport servent sockname sockopt
516';
517
518# Make sure the following tests test what we think they are testing.
519ok ! $CORE::{glob}, '*CORE::glob not autovivified yet'; $tests ++;
520{
521  # Make sure ck_glob does not respect the override when &CORE::glob is
522  # autovivified (by test_proto).
523  local *CORE::GLOBAL::glob = sub {};
524  test_proto 'glob';
525}
526$_ = "t/*.t";
527@_ = &myglob($_);
528is join($", &myglob()), "@_", '&glob without arguments';
529is join($", &myglob("t/*.t")), "@_", '&glob with an arg';
530$tests += 2;
531
532test_proto 'gmtime';
533&CORE::gmtime;
534pass '&gmtime without args does not crash'; ++$tests;
535
536test_proto 'hex', ff=>255;
537
538test_proto 'index';
539$tests += 3;
540is &myindex("foffooo","o",2),4,'&index';
541lis [&myindex("foffooo","o",2)],[4],'&index in list context';
542is &myindex("foffooo","o"),1,'&index with 2 args';
543
544test_proto 'int', 1.5=>1;
545test_proto 'ioctl';
546
547test_proto 'join';
548$tests += 2;
549is &myjoin('a','b','c'), 'bac', '&join';
550lis [&myjoin('a','b','c')], ['bac'], '&join in list context';
551
552test_proto 'kill'; # set up mykill alias
553if ($^O ne 'riscos') {
554    $tests ++;
555    ok( &mykill(0, $$), '&kill' );
556}
557
558test_proto 'lc', 'A', 'a';
559test_proto 'lcfirst', 'AA', 'aA';
560test_proto 'length', 'aaa', 3;
561test_proto 'link';
562test_proto 'listen';
563
564test_proto 'localtime';
565&CORE::localtime;
566pass '&localtime without args does not crash'; ++$tests;
567
568test_proto 'lock';
569$tests += 6;
570is \&mylock(\$foo), \$foo, '&lock retval when passed a scalar ref';
571lis [\&mylock(\$foo)], [\$foo], '&lock in list context';
572is &mylock(\@foo), \@foo, '&lock retval when passed an array ref';
573is &mylock(\%foo), \%foo, '&lock retval when passed a ash ref';
574is &mylock(\&foo), \&foo, '&lock retval when passed a code ref';
575is \&mylock(\*foo), \*foo, '&lock retval when passed a glob ref';
576
577test_proto 'log';
578
579test_proto 'mkdir';
580# mkdir is tested with implicit $_ at the end, to make the test easier
581
582test_proto "msg$_" for qw( ctl get rcv snd );
583
584test_proto 'not';
585$tests += 2;
586is &mynot(1), !1, '&not';
587lis [&mynot(0)], [!0], '&not in list context';
588
589test_proto 'oct', '666', 438;
590
591test_proto 'open';
592$tests += 5;
593$file = 'test.pl';
594ok &myopen('file'), '&open with 1 arg' or warn "1-arg open: $!";
595like <file>, qr|^#|, 'result of &open with 1 arg';
596close file;
597{
598  ok &myopen(my $fh, "test.pl"), 'two-arg &open';
599  ok $fh, '&open autovivifies';
600  like <$fh>, qr '^#', 'result of &open with 2 args';
601  last if is_miniperl;
602  $tests +=2;
603  ok &myopen(my $fh2, "<", \"sharummbles"), 'retval of 3-arg &open';
604  is <$fh2>, 'sharummbles', 'result of three-arg &open';
605}
606
607test_proto 'opendir';
608test_proto 'ord', chr(64), 64;
609
610test_proto 'pack';
611$tests += 2;
612is &mypack("H*", '5065726c'), 'Perl', '&pack';
613lis [&mypack("H*", '5065726c')], ['Perl'], '&pack in list context';
614
615test_proto 'pipe';
616
617test_proto 'pos';
618$tests += 4;
619$_ = "hello";
620pos = 3;
621is &mypos, 3, 'reading &pos without args';
622&mypos = 4;
623is pos, 4, 'writing to &pos without args';
624{
625  my $x = "gubai";
626  pos $x = 3;
627  is &mypos(\$x), 3, 'reading &pos without args';
628  &mypos(\$x) = 4;
629  is pos $x, 4, 'writing to &pos without args';
630}
631
632test_proto 'prototype';
633$tests++;
634is &myprototype(\&myprototype), prototype("CORE::prototype"), '&prototype';
635
636test_proto 'quotemeta', '$', '\$';
637
638test_proto 'rand';
639$tests += 3;
640like &CORE::rand, qr/^0[.\d+-e]*\z/, '&rand';
641unlike join(" ", &CORE::rand), qr/ /, '&rand in list context';
642&cmp_ok(&CORE::rand(78), qw '< 78', '&rand with 1 arg');
643
644test_proto 'read';
645{
646  last if is_miniperl;
647  $tests += 5;
648  open my $fh, "<", \(my $buff = 'morays have their mores');
649  ok &myread($fh, \my $input, 6), '&read with 3 args';
650  is $input, 'morays', 'value read by 3-arg &read';
651  ok &myread($fh, \$input, 6, 6), '&read with 4 args';
652  is $input, 'morays have ', 'value read by 4-arg &read';
653  is +()=&myread($fh, \$input, 6), 1, '&read in list context';
654}
655
656test_proto 'readdir';
657
658test_proto 'readline';
659{
660  local *ARGV = *DATA;
661  $tests ++;
662  is scalar &myreadline,
663    "I wandered lonely as a cloud\n", '&readline w/no args';
664}
665{
666  last if is_miniperl;
667  $tests += 2;
668  open my $fh, "<", \(my $buff = <<END);
669The Recursive Problem
670---------------------
671I have a problem I cannot solve.
672The problem is that I cannot solve it.
673END
674  is &myreadline($fh), "The Recursive Problem\n",
675    '&readline with 1 arg';
676  lis [&myreadline($fh)], [
677       "---------------------\n",
678       "I have a problem I cannot solve.\n",
679       "The problem is that I cannot solve it.\n",
680      ], '&readline in list context';
681}
682
683test_proto 'readlink';
684test_proto 'readpipe';
685test_proto 'recv';
686
687use if !is_miniperl, File::Spec::Functions, qw "catfile";
688use if !is_miniperl, File::Temp, 'tempdir';
689
690test_proto 'rename';
691{
692    last if is_miniperl;
693    $tests ++;
694    my $dir = tempdir(uc cleanup => 1);
695    my $tmpfilenam = catfile $dir, 'aaa';
696    open my $fh, ">", $tmpfilenam or die "cannot open $tmpfilenam: $!";
697    close $fh or die "cannot close $tmpfilenam: $!";
698    &myrename("$tmpfilenam", $tmpfilenam = catfile $dir,'bbb');
699    ok open(my $fh, '>', $tmpfilenam), '&rename';
700}
701
702test_proto 'ref', [], 'ARRAY';
703
704test_proto 'reset';
705$tests += 2;
706my $oncer = sub { "a" =~ m?a? };
707&$oncer;
708&myreset;
709ok &$oncer, '&reset with no args';
710package resettest {
711  $b = "c";
712  $banana = "cream";
713  &::myreset('b');
714  ::lis [$b,$banana],[(undef)x2], '1-arg &reset';
715}
716
717test_proto 'reverse';
718$tests += 2;
719is &myreverse('reward'), 'drawer', '&reverse';
720lis [&myreverse(qw 'dog bites man')], [qw 'man bites dog'],
721  '&reverse in list context';
722
723test_proto 'rewinddir';
724
725test_proto 'rindex';
726$tests += 3;
727is &myrindex("foffooo","o",2),1,'&rindex';
728lis [&myrindex("foffooo","o",2)],[1],'&rindex in list context';
729is &myrindex("foffooo","o"),6,'&rindex with 2 args';
730
731test_proto 'rmdir';
732
733test_proto 'scalar';
734$tests += 2;
735is &myscalar(3), 3, '&scalar';
736lis [&myscalar(3)], [3], '&scalar in list cx';
737
738test_proto 'seek';
739{
740    last if is_miniperl;
741    $tests += 1;
742    open my $fh, "<", \"misled" or die $!;
743    &myseek($fh, 2, 0);
744    is <$fh>, 'sled', '&seek in action';
745}
746
747test_proto 'seekdir';
748
749# Can’t test_proto, as it has none
750$tests += 8;
751*myselect = \&CORE::select;
752is defined prototype &myselect, defined prototype "CORE::select",
753   'prototype of &select (or lack thereof)';
754is &myselect, select, '&select with no args';
755{
756  my $prev = select;
757  is &myselect(my $fh), $prev, '&select($arg) retval';
758  is lc ref $fh, 'glob', '&select autovivifies';
759  is select=~s/\*//rug, (*$fh."")=~s/\*//rug, '&select selects';
760  select $prev;
761}
762eval { &myselect(1,2) };
763like $@, qr/^Not enough arguments for select system call at /,
764      ,'&myselect($two,$args)';
765eval { &myselect(1,2,3) };
766like $@, qr/^Not enough arguments for select system call at /,
767      ,'&myselect($with,$three,$args)';
768eval { &myselect(1,2,3,4,5) };
769like $@, qr/^Too many arguments for select system call at /,
770      ,'&myselect($a,$total,$of,$five,$args)';
771&myselect((undef)x3,.25);
772# Just have to assume that worked. :-) If we get here, at least it didn’t
773# crash or anything.
774
775test_proto "sem$_" for qw "ctl get op";
776
777test_proto 'send';
778
779test_proto "set$_" for qw '
780  grent hostent netent
781';
782
783test_proto 'setpgrp';
784$tests +=2;
785eval { &mysetpgrp( 0) };
786pass "&setpgrp with one argument";
787eval { &mysetpgrp };
788pass "&setpgrp with no arguments";
789
790test_proto "set$_" for qw '
791  priority protoent pwent servent sockopt
792';
793
794test_proto "shm$_" for qw "ctl get read write";
795test_proto 'shutdown';
796test_proto 'sin';
797test_proto 'sleep';
798test_proto "socket$_" for "", "pair";
799
800test_proto 'sprintf';
801$tests += 2;
802is &mysprintf("%x", 65), '41', '&sprintf';
803lis [&mysprintf("%x", '65')], ['41'], '&sprintf in list context';
804
805test_proto 'sqrt', 4, 2;
806
807test_proto 'srand';
808$tests ++;
809&CORE::srand;
810() = &CORE::srand;
811pass '&srand with no args does not crash';
812
813test_proto 'study';
814
815test_proto 'substr';
816$tests += 5;
817$_ = "abc";
818is &mysubstr($_, 1, 1, "d"), 'b', '4-arg &substr';
819is $_, 'adc', 'what 4-arg &substr does';
820is &mysubstr("abc", 1, 1), 'b', '3-arg &substr';
821is &mysubstr("abc", 1), 'bc', '2-arg &substr';
822&mysubstr($_, 1) = 'long';
823is $_, 'along', 'lvalue &substr';
824
825test_proto 'symlink';
826test_proto 'syscall';
827
828test_proto 'sysopen';
829$tests +=2;
830{
831  &mysysopen(my $fh, 'test.pl', 0);
832  pass '&sysopen does not crash with 3 args';
833  ok $fh, 'sysopen autovivifies';
834}
835
836test_proto 'sysread';
837test_proto 'sysseek';
838test_proto 'syswrite';
839
840test_proto 'tell';
841{
842  $tests += 2;
843  open my $fh, "test.pl" or die "Cannot open test.pl";
844  <$fh>;
845  is &mytell(), tell($fh), '&tell with no args';
846  is &mytell($fh), tell($fh), '&tell with an arg';
847}
848
849test_proto 'telldir';
850
851test_proto 'tie';
852test_proto 'tied';
853$tests += 3;
854{
855  my $fetches;
856  package tier {
857    sub TIESCALAR { bless[] }
858    sub FETCH { ++$fetches }
859  }
860  my $tied;
861  my $obj = &mytie(\$tied, 'tier');
862  is &mytied(\$tied), $obj, '&tie and &tied retvals';
863  () = "$tied";
864  is $fetches, 1, '&tie actually ties';
865  &CORE::untie(\$tied);
866  () = "$tied";
867  is $fetches, 1, '&untie unties';
868}
869
870test_proto 'time';
871$tests += 2;
872like &mytime, '^\d+\z', '&time in scalar context';
873like join('-', &mytime), '^\d+\z', '&time in list context';
874
875test_proto 'times';
876$tests += 2;
877like &mytimes, '^[\d.]+\z', '&times in scalar context';
878like join('-',&mytimes), '^[\d.]+-[\d.]+-[\d.]+-[\d.]+\z',
879   '&times in list context';
880
881test_proto 'uc', 'aa', 'AA';
882test_proto 'ucfirst', 'aa', "Aa";
883
884test_proto 'umask';
885$tests ++;
886is &myumask, umask, '&umask with no args';
887
888test_proto 'undef';
889$tests += 12;
890is &myundef(), undef, '&undef returns undef';
891lis [&myundef()], [undef], '&undef returns undef in list cx';
892lis [&myundef(\$_)], [undef], '&undef(...) returns undef in list cx';
893is \&myundef(), \undef, '&undef returns the right undef';
894$_ = 'anserine questions';
895&myundef(\$_);
896is $_, undef, '&undef(\$_) undefines $_';
897@_ = 1..3;
898&myundef(\@_);
899is @_, 0, '&undef(\@_) undefines @_';
900%_ = 1..4;
901&myundef(\%_);
902ok !%_, '&undef(\%_) undefines %_';
903&myundef(\&utf8::valid); # nobody should be using this :-)
904ok !defined &utf8::valid, '&undef(\&foo) undefines &foo';
905@_ = \*_;
906&myundef;
907is *_{ARRAY}, undef, '@_=\*_, &undef undefines *_';
908@_ = \*_;
909&myundef(\*_);
910is *_{ARRAY}, undef, '&undef(\*_) undefines *_';
911(&myundef(), @_) = 1..10;
912lis \@_, [2..10], 'list assignment to &undef()';
913ok !defined undef, 'list assignment to &undef() does not affect undef';
914undef @_;
915
916test_proto 'unpack';
917$tests += 2;
918$_ = 'abcd';
919is &myunpack("H*"), '61626364', '&unpack with one arg';
920is &myunpack("H*", "bcde"), '62636465', '&unpack with two arg';
921
922
923test_proto 'untie'; # behaviour already tested along with tie(d)
924
925test_proto 'utime';
926$tests += 2;
927is &myutime(undef,undef), 0, '&utime';
928lis [&myutime(undef,undef)], [0], '&utime in list context';
929
930test_proto 'vec';
931$tests += 3;
932is &myvec("foo", 0, 4), 6, '&vec';
933lis [&myvec("foo", 0, 4)], [6], '&vec in list context';
934$tmp = "foo";
935++&myvec($tmp,0,4);
936is $tmp, "goo", 'lvalue &vec';
937
938test_proto 'wait';
939test_proto 'waitpid';
940
941test_proto 'wantarray';
942$tests += 4;
943my $context;
944my $cx_sub = sub {
945  $context = qw[void scalar list][&mywantarray + defined mywantarray()]
946};
947() = &$cx_sub;
948is $context, 'list', '&wantarray with caller in list context';
949scalar &$cx_sub;
950is($context, 'scalar', '&wantarray with caller in scalar context');
951&$cx_sub;
952is($context, 'void', '&wantarray with caller in void context');
953lis [&mywantarray],[wantarray], '&wantarray itself in list context';
954
955test_proto 'warn';
956{ $tests += 3;
957  my $w;
958  local $SIG{__WARN__} = sub { $w = shift };
959  is &mywarn('a'), 1, '&warn retval';
960  is $w, "a at " . __FILE__ . " line " . (__LINE__-1) . ".\n", 'warning';
961  lis [&mywarn()], [1], '&warn retval in list context';
962}
963
964test_proto 'write';
965$tests ++;
966eval {&mywrite};
967like $@, qr'^Undefined format "STDOUT" called',
968   "&write without arguments can handle the null";
969
970# This is just a check to make sure we have tested everything.  If we
971# haven’t, then either the sub needs to be tested or the list in
972# gv.c is wrong.
973{
974  last if is_miniperl;
975  require File::Spec::Functions;
976  my $keywords_file =
977   File::Spec::Functions::catfile(
978      File::Spec::Functions::updir,'regen','keywords.pl'
979   );
980  open my $kh, $keywords_file
981    or die "$0 cannot open $keywords_file: $!";
982  while(<$kh>) {
983    if (m?__END__?..${\0} and /^[-+](.*)/) {
984      my $word = $1;
985      next if
986       $word =~ /^(?:s(?:tate|ort|ay|ub)?|d(?:ef
987                  ault|ump|o)|p(?:rintf?|ackag
988                  e)|e(?:ls(?:if|e)|val|q)|g(?:[et]|iven|oto
989                  |rep)|u(?:n(?:less|til)|se)|l(?:(?:as)?t|ocal|e)|re
990                  (?:quire|turn|do)|__(?:DATA|END)__|for(?:each|mat)?|(?:
991                  AUTOLOA|EN)D|n(?:e(?:xt)?|o)|C(?:HECK|ORE)|wh(?:ile|en)
992                  |(?:ou?|t)r|m(?:ap|y)?|UNITCHECK|q[qrwx]?|x(?:or)?|DEST
993                  ROY|BEGIN|INIT|and|cmp|if|y)\z/x;
994      $tests ++;
995      ok   exists &{"my$word"}
996        || (eval{&{"CORE::$word"}}, $@ =~ /cannot be called directly/),
997     "$word either has been tested or is not ampable";
998    }
999  }
1000}
1001
1002# Add new tests above this line.
1003
1004# This test must come last (before the test count test):
1005
1006{
1007  last if is_miniperl;
1008  require Cwd;
1009  import Cwd;
1010  $tests += 3;
1011  require File::Temp ;
1012  my $dir = File::Temp::tempdir(uc cleanup => 1);
1013  my $cwd = cwd();
1014  chdir($dir);
1015
1016  # Make sure that implicit $_ is not applied to mkdir’s second argument.
1017  local $^W = 1;
1018  my $warnings;
1019  local $SIG{__WARN__} = sub { ++$warnings };
1020
1021  no warnings 'experimental::lexical_topic';
1022  my $_ = 'Phoo';
1023  ok &mymkdir(), '&mkdir';
1024  like <*>, qr/^phoo(.DIR)?\z/i, 'mkdir works with implicit $_';
1025
1026  is $warnings, undef, 'no implicit $_ for second argument to mkdir';
1027
1028  chdir($cwd); # so auto-cleanup can remove $dir
1029}
1030
1031# ------------ END TESTING ----------- #
1032
1033done_testing $tests;
1034
1035#line 3 frob
1036
1037sub file { &CORE::__FILE__ }
1038sub line { &CORE::__LINE__ } # 5
1039sub dier { &CORE::die(@_)  } # 6
1040package stribble;
1041sub main::pakg { &CORE::__PACKAGE__ }
1042
1043# Please do not add new tests here.
1044package main;
1045CORE::__DATA__
1046I wandered lonely as a cloud
1047That floats on high oer vales and hills,
1048And all at once I saw a crowd,
1049A host of golden daffodils!
1050Beside the lake, beneath the trees,
1051Fluttering, dancing, in the breeze.
1052-- Wordsworth
1053