1# Tests for random.org site 2 3use strict; 4use warnings; 5 6use Test::More; 7 8# Warning signal handler 9my @WARN; 10BEGIN { 11 $SIG{__WARN__} = sub { push(@WARN, @_); }; 12} 13 14use Math::Random::MT::Auto qw(rand irand), 'random_org'; 15 16if (grep(/^Failure creating user-agent/, @WARN)) { 17 plan skip_all => 'LWP::Useragent not available'; 18} elsif (grep(/^Failure contacting/, @WARN)) { 19 plan skip_all => 'random.org not reachable'; 20} elsif ((grep(/^Failure getting data/, @WARN)) || 21 (grep(/^No seed data/, @WARN))) 22{ 23 plan skip_all => 'Seed not obtained from random.org'; 24} 25plan tests => 91; 26 27@WARN = grep(!/^Partial seed/, @WARN); 28@WARN = grep(!/only once/, @WARN); # Ingnore 'once' warnings from other modules 29ok(! @WARN, 'No warnings'); 30diag('Warnings: ' . join(' | ', @WARN)) if (@WARN); 31 32can_ok('main', qw(rand irand)); 33 34# Test rand() 35my ($rn, @rn); 36eval { $rn = rand(); }; 37ok(! $@, 'rand() died: ' . $@); 38ok(defined($rn), 'Got a random number'); 39ok(Scalar::Util::looks_like_number($rn), 'Is a number: ' . $rn); 40ok($rn >= 0.0 && $rn < 1.0, 'In range: ' . $rn); 41 42# Test several values from irand() 43for my $ii (0 .. 9) { 44 eval { $rn[$ii] = irand(); }; 45 ok(! $@, 'irand() died: ' . $@); 46 ok(defined($rn[$ii]), 'Got a random number'); 47 ok(Scalar::Util::looks_like_number($rn[$ii]), 'Is a number: ' . $rn[$ii]); 48 ok(int($rn[$ii]) == $rn[$ii], 'Integer: ' . $rn[$ii]); 49 for my $jj (0 .. $ii-1) { 50 ok($rn[$jj] != $rn[$ii], 'Randomized'); 51 } 52} 53 54exit(0); 55 56# EOF 57