File: range.t

package info (click to toggle)
libmath-random-secure-perl 0.080001-1.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 156 kB
  • sloc: perl: 368; makefile: 2
file content (44 lines) | stat: -rw-r--r-- 1,243 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/perl
use strict;
use warnings;
use Test::More tests => 16;
use Math::Random::Secure qw(rand irand);
use Data::Dumper;
use List::MoreUtils qw(all);

our $HOW_MANY = 10_000;

sub check_range {
  my ($numbers, $type, $limit) = @_;
  $limit ||= 10;
  my $all_less = all { $_ < $limit } @$numbers;
  ok($all_less, "all $type less than $limit")
    or diag Dumper($numbers);
  my $all_more = all { $_ >= 0 } @$numbers;
  ok($all_more, "all $type greater or equal to 0")
    or diag Dumper($numbers);
}

my @numbers = map { rand(10) } (1..$HOW_MANY);
check_range(\@numbers, 'floats');

my @ints = map { irand(10) } (1..$HOW_MANY);
check_range(\@ints, 'integers');

my @made_ints = map { int(rand(10)) } (1..$HOW_MANY);
check_range(\@made_ints, 'made integers');

my @zero_to_one = map { rand() } (1..$HOW_MANY);
check_range(\@zero_to_one, 'zero to one', 1);

my @all_ints = map { irand() } (1..$HOW_MANY);
check_range(\@all_ints, 'full range integers', 2**32);

my @floats_zero = map { rand(0) } (1..$HOW_MANY);
check_range(\@floats_zero, 'zero floats', 1);

my @ints_zero = map { irand(0) } (1..$HOW_MANY);
check_range(\@ints_zero, 'zero ints', 1);

my @ints_one = map { irand(1) } (1..$HOW_MANY);
check_range(\@ints_one, 'one ints', 1);