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