File: runenv_randseed.t

package info (click to toggle)
perl 5.42.0-2
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 128,392 kB
  • sloc: perl: 534,963; ansic: 240,563; sh: 72,042; pascal: 6,934; xml: 2,428; yacc: 1,360; makefile: 1,197; cpp: 208; lisp: 1
file content (67 lines) | stat: -rw-r--r-- 1,908 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!./perl

BEGIN {
    chdir 't' if -d 't';
    @INC = '../lib';
    require './test.pl';
}
use Config;

skip_all_without_config('d_fork');
skip_all("This perl is built with NO_PERL_RAND_SEED")
    if $Config{ccflags} =~ /-DNO_PERL_RAND_SEED\b/;
use strict;
use warnings;

for (1..2) {
    local $ENV{PERL_RAND_SEED} = 1;
    fresh_perl_is("print map { chr(utf8::unicode_to_native(rand(26)+65)) } 1..10",
                  "BLVIOAEZTJ", undef, "Test randomness with PERL_RAND_SEED=1");
}

for (1..2) {
    local $ENV{PERL_RAND_SEED} = 2;
    fresh_perl_is("print map { chr(utf8::unicode_to_native(rand(26)+65)) } 1..10",
                  "XEOUOFRPQZ", undef, "Test randomness with PERL_RAND_SEED=2");
}

my %got;
for my $try (1..10) {
    local $ENV{PERL_RAND_SEED};
    my ($out,$err)= runperl_and_capture({}, ['-e',"print map { chr(rand(26)+65) } 1..10;"]);
    if ($err) { diag $err }
    $got{$out}++;
}
ok(8 <= keys %got, "Got at least 8 different strings");
for (1..2) {
    local $ENV{PERL_RAND_SEED} = 1;
    my ($out,$err)= runperl_and_capture({}, ['-le',
            <<'EOF_TEST_CODE'
            for my $l ("A".."E") {
                my $pid= fork;
                if ($pid) {
                    push @pids, $pid;
                }
                elsif (!defined $pid) {
                    print "$l:failed fork";
                } elsif (!$pid) {
                    print "$l:", map { chr(utf8::unicode_to_native(rand(26)+65)) } 1..10;
                    exit;
                }
            }
            waitpid $_,0 for @pids;
EOF_TEST_CODE
        ]);
    is($err, "", "No exceptions forking.");
    my @parts= sort { $a cmp $b } split /\n/, $out;
    my @want= (
            "A:KNXDITWWJZ",
            "B:WDQJGTBJQS",
            "C:ZGYCCINIHE",
            "D:UGLGAEXFBP",
            "E:MQLTNZGZQB"
    );
    is("@parts","@want","Works as expected with forks.");
}

done_testing();