File: author-uniform.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 (93 lines) | stat: -rw-r--r-- 2,575 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/perl

BEGIN {
  unless ($ENV{AUTHOR_TESTING}) {
    require Test::More;
    Test::More::plan(skip_all => 'these tests are for testing by the author');
  }
}

use strict;
use warnings;
use Test::More;
use Math::Random::Secure qw(rand irand);
use Statistics::Test::RandomWalk 0.02;

# 2% variation is acceptable.
our $ACCEPTABLE = 0.02;
our $BINS = 20;
our $NUM_RUNS = 500_000;

# We want a number that's more than half of 2^32 but doesn't
# divide evenly into it.
our $LARGE_LIMIT = 3_000_893_649;

plan tests => (20 * $BINS) - (8 + 20 + 36 + 34);

sub test_uniform {
  my ($name, $limit, $rng) = @_;
  $rng ||= \&rand;

  my $num_runs = $NUM_RUNS;
  if (defined $limit and $limit > $num_runs) {

    # Uncomment this line for more extensive testing.
    #$num_runs = $limit * 2;
  }

  my $tester = Statistics::Test::RandomWalk->new();
  if ($rng == \&irand) {
    $tester->set_rescale_factor($limit || 2**32);
    $tester->set_data(sub { $rng->($limit) }, $num_runs);
  } else {
    my $divide_by;
    $divide_by = $limit - (1 / (2**32)) if $limit;
    $tester->set_data(sub { $limit ? $rng->($limit) / $divide_by : $rng->() },
      $num_runs);
  }

  my $bins = $BINS;
  if (defined $limit and $limit == 64) {
    $bins = 16;
  }
  if (defined $limit and $limit < $bins and $limit > 1) {
    $bins = $limit;
  }

  my ($quant, $got, $expected) = $tester->test($bins);

  foreach my $i (0 .. scalar(@$got) - 1) {
    cmp_ok(
      abs($got->[$i] - $expected->[$i]) / $expected->[$i],
      '<',
      $ACCEPTABLE,
      "$name: Quantile $quant->[$i] is within 2% of the expected "
        . $expected->[$i]);
  }

  if ($ENV{TEST_VERBOSE}) {
    diag $tester->data_to_report($quant, $got, $expected);
  }
}

test_uniform('rand no limit');
test_uniform('rand limit .3', .3);
test_uniform('rand limit .9', .9);
test_uniform('rand limit 2', 2);
test_uniform('rand limit 3', 3);
test_uniform('rand limit 10', 10);
test_uniform('rand limit 40', 40);
test_uniform('rand limit 64', 64);
test_uniform('rand limit 200', 200);
test_uniform('rand limit 1_000_000', 1_000_000);
test_uniform("rand limit $LARGE_LIMIT", $LARGE_LIMIT);

test_uniform('irand no limit', undef, \&irand);
test_uniform('irand limit 2', 2, \&irand);
test_uniform('irand limit 3', 3, \&irand);
test_uniform('irand limit 10', 10, \&irand);
test_uniform('irand limit 40', 40, \&irand);
test_uniform('irand limit 64', 64, \&irand);
test_uniform('irand limit 200', 200, \&irand);
test_uniform('irand limit 1_000_000', 1_000_000, \&irand);
test_uniform("irand limit $LARGE_LIMIT", $LARGE_LIMIT, \&irand);