1# -*- mode: perl; -*-
2
3use strict;
4use warnings;
5
6use Test::More tests => 4393;
7
8###############################################################################
9# Read and load configuration file and backend library.
10
11use Config::Tiny ();
12
13my $config_file = 'xt/author/lib.ini';
14my $config = Config::Tiny -> read('xt/author/lib.ini')
15  or die Config::Tiny -> errstr();
16
17# Read the library to test.
18
19our $LIB = $config->{_}->{lib};
20
21die "No library defined in file '$config_file'"
22  unless defined $LIB;
23die "Invalid library name '$LIB' in file '$config_file'"
24  unless $LIB =~ /^[A-Za-z]\w*(::\w+)*\z/;
25
26# Load the library.
27
28eval "require $LIB";
29die $@ if $@;
30
31###############################################################################
32
33can_ok($LIB, '_alen');
34
35use lib 't';
36use Math::BigInt::Lib::TestUtil qw< randstr >;
37
38# Generate test data.
39
40my @data;
41
42# Small integers.
43
44for my $x (0 .. 99) {
45    push @data, [ $x ];
46}
47
48# Random large integers.
49
50for (3 .. 1000) {
51    my $nx = 2 + int rand 35;           # number of digits in $x
52    my $x  = randstr($nx, 10);          # generate $a
53    push @data, [ $x ];
54}
55
56# List context.
57
58for (my $i = 0 ; $i <= $#data ; ++ $i) {
59    my ($in0) = @{ $data[$i] };
60
61    my ($x, @got);
62
63    my $test = qq|\$x = $LIB->_new("$in0"); |
64             . qq|\@got = $LIB->_alen(\$x);|;
65
66    diag("\n$test\n\n") if $ENV{AUTHOR_DEBUGGING};
67
68    eval $test;
69    is($@, "", "'$test' gives emtpy \$\@");
70
71    subtest "_alen() in list context: $test", sub {
72        plan tests => 5,
73
74        cmp_ok(scalar @got, '==', 1,
75               "'$test' gives one output arg");
76
77        is(ref($got[0]), "",
78           "'$test' output arg is a Perl scalar");
79
80        isnt($got[0], undef,
81             "'$test' output arg is defined");
82
83        like($got[0], qr/^[+-]?(\d+(\.\d*)?|\.\d+)([Ee][+-]?\d+)?\z/,
84             "'$test' output arg looks like a number");
85
86        is($got[0], int($got[0]),
87           "'$test' output arg is an integer");
88    };
89}
90
91# Scalar context.
92
93for (my $i = 0 ; $i <= $#data ; ++ $i) {
94    my ($in0) = @{ $data[$i] };
95
96    my ($x, $got);
97
98    my $test = qq|\$x = $LIB->_new("$in0"); |
99             . qq|\$got = $LIB->_alen(\$x);|;
100
101    diag("\n$test\n\n") if $ENV{AUTHOR_DEBUGGING};
102
103    eval $test;
104    is($@, "", "'$test' gives emtpy \$\@");
105
106    subtest "_alen() in scalar context: $test", sub {
107        plan tests => 4,
108
109        is(ref($got), "",
110           "'$test' output arg is a Perl scalar");
111
112        isnt($got, undef,
113             "'$test' output arg is defined");
114
115        like($got, qr/^[+-]?(\d+(\.\d*)?|\.\d+)([Ee][+-]?\d+)?\z/,
116             "'$test' output arg looks like a number");
117
118        is($got, int($got),
119           "'$test' output arg is an integer");
120    };
121}
122