summaryrefslogtreecommitdiff
path: root/cpan/Math-BigInt/t/Math/BigInt/Lib/TestUtil.pm
blob: 5ff98bf19a7c09009827e24301453c2ed4288a64 (plain)
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
# -*- mode: perl; -*-

package Math::BigInt::Lib::TestUtil;

use strict;
use warnings;

use Exporter;

our @ISA       = qw< Exporter >;
our @EXPORT_OK = qw< randstr >;

# randstr NUM, BASE
#
# Generate a string representing a NUM digit number in base BASE.

sub randstr {
    die "randstr: wrong number of input arguments\n"
      unless @_ == 2;

    my $n = shift;
    my $b = shift;

    die "randstr: first input argument must be >= 0"
      unless $n >= 0;
    die "randstr: second input argument must be in the range 2 .. 36\n"
      unless 2 <= $b && $b <= 36;

    return '' if $n == 0;

    my @dig = (0 .. 9, 'a' .. 'z');

    my $str = $dig[ 1 + int rand ($b - 1) ];
    $str .= $dig[ int rand $b ] for 2 .. $n;

    return $str;
}

1;