xref: /openbsd-src/gnu/usr.bin/perl/t/run/runenv_randseed.t (revision f2a19305cfc49ea4d1a5feb55cd6c283c6f1e031)
1*f2a19305Safresh1#!./perl
2*f2a19305Safresh1
3*f2a19305Safresh1BEGIN {
4*f2a19305Safresh1    chdir 't' if -d 't';
5*f2a19305Safresh1    @INC = '../lib';
6*f2a19305Safresh1    require './test.pl';
7*f2a19305Safresh1    require Config;
8*f2a19305Safresh1    Config->import;
9*f2a19305Safresh1}
10*f2a19305Safresh1
11*f2a19305Safresh1skip_all_without_config('d_fork');
12*f2a19305Safresh1skip_all("This perl is built with NO_PERL_RAND_SEED")
13*f2a19305Safresh1    if $Config{ccflags} =~ /-DNO_PERL_RAND_SEED\b/;
14*f2a19305Safresh1use strict;
15*f2a19305Safresh1use warnings;
16*f2a19305Safresh1
17*f2a19305Safresh1for (1..2) {
18*f2a19305Safresh1    local $ENV{PERL_RAND_SEED} = 1;
19*f2a19305Safresh1    fresh_perl_is("print map { chr(rand(26)+65) } 1..10",
20*f2a19305Safresh1                  "BLVIOAEZTJ", undef, "Test randomness with PERL_RAND_SEED=1");
21*f2a19305Safresh1}
22*f2a19305Safresh1
23*f2a19305Safresh1for (1..2) {
24*f2a19305Safresh1    local $ENV{PERL_RAND_SEED} = 2;
25*f2a19305Safresh1    fresh_perl_is("print map { chr(rand(26)+65) } 1..10",
26*f2a19305Safresh1                  "XEOUOFRPQZ", undef, "Test randomness with PERL_RAND_SEED=2");
27*f2a19305Safresh1}
28*f2a19305Safresh1
29*f2a19305Safresh1my %got;
30*f2a19305Safresh1for my $try (1..10) {
31*f2a19305Safresh1    local $ENV{PERL_RAND_SEED};
32*f2a19305Safresh1    my ($out,$err)= runperl_and_capture({}, ['-e',"print map { chr(rand(26)+65) } 1..10;"]);
33*f2a19305Safresh1    if ($err) { diag $err }
34*f2a19305Safresh1    $got{$out}++;
35*f2a19305Safresh1}
36*f2a19305Safresh1ok(8 <= keys %got, "Got at least 8 different strings");
37*f2a19305Safresh1for (1..2) {
38*f2a19305Safresh1    local $ENV{PERL_RAND_SEED} = 1;
39*f2a19305Safresh1    my ($out,$err)= runperl_and_capture({}, ['-le',
40*f2a19305Safresh1            <<'EOF_TEST_CODE'
41*f2a19305Safresh1            for my $l ("A".."E") {
42*f2a19305Safresh1                my $pid= fork;
43*f2a19305Safresh1                if ($pid) {
44*f2a19305Safresh1                    push @pids, $pid;
45*f2a19305Safresh1                }
46*f2a19305Safresh1                elsif (!defined $pid) {
47*f2a19305Safresh1                    print "$l:failed fork";
48*f2a19305Safresh1                } elsif (!$pid) {
49*f2a19305Safresh1                    print "$l:", map { chr(rand(26)+65) } 1..10;
50*f2a19305Safresh1                    exit;
51*f2a19305Safresh1                }
52*f2a19305Safresh1            }
53*f2a19305Safresh1            waitpid $_,0 for @pids;
54*f2a19305Safresh1EOF_TEST_CODE
55*f2a19305Safresh1        ]);
56*f2a19305Safresh1    is($err, "", "No exceptions forking.");
57*f2a19305Safresh1    my @parts= sort { $a cmp $b } split /\n/, $out;
58*f2a19305Safresh1    my @want= (
59*f2a19305Safresh1            "A:KNXDITWWJZ",
60*f2a19305Safresh1            "B:WDQJGTBJQS",
61*f2a19305Safresh1            "C:ZGYCCINIHE",
62*f2a19305Safresh1            "D:UGLGAEXFBP",
63*f2a19305Safresh1            "E:MQLTNZGZQB"
64*f2a19305Safresh1    );
65*f2a19305Safresh1    is("@parts","@want","Works as expected with forks.");
66*f2a19305Safresh1}
67*f2a19305Safresh1
68*f2a19305Safresh1done_testing();
69