1# -*- mode: perl; -*-
2
3use strict;
4use warnings;
5
6use Test::More tests => 593;
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# Read the reference type(s) the library uses.
27
28our $REF = $config->{_}->{ref};
29
30die "No reference type defined in file '$config_file'"
31  unless defined $REF;
32die "Invalid reference type '$REF' in file '$config_file'"
33  unless $REF =~ /^[A-Za-z]\w*(::\w+)*\z/;
34
35# Load the library.
36
37eval "require $LIB";
38die $@ if $@;
39
40###############################################################################
41
42can_ok($LIB, '_digitsum');
43
44use lib 't';
45use Math::BigInt::Lib::TestUtil qw< randstr >;
46
47# Generate test data.
48
49my @data;
50
51for (my $x = 0 ; $x <= 100 ; ++ $x) {
52    my $str = sprintf "%u", $x;
53    my @digits = $] >= 5.008 ? unpack "(a)*", $str
54                             : split //, $str;
55    my $sum = 0;
56    $sum += $_ for @digits;
57    push @data, [ $str, $sum ];
58}
59
60for (my $n = 4 ; $n <= 50 ; ++ $n) {
61    my $str = randstr($n, 10);
62    my @digits = $] >= 5.008 ? unpack "(a)*", $str
63                             : split //, $str;
64    my $sum = 0;
65    $sum += $_ for @digits;
66    push @data, [ $str, $sum ];
67}
68
69# List context.
70
71for (my $i = 0 ; $i <= $#data ; ++ $i) {
72    my ($in0, $out0) = @{ $data[$i] };
73
74    my ($x, @got);
75
76    my $test = qq|\$x = $LIB->_new("$in0"); |
77             . qq|\@got = $LIB->_digitsum(\$x);|;
78
79    diag("\n$test\n\n") if $ENV{AUTHOR_DEBUGGING};
80
81    eval $test;
82    is($@, "", "'$test' gives emtpy \$\@");
83
84    subtest "_digitsum() in list context: $test", sub {
85        plan tests => 3,
86
87        cmp_ok(scalar @got, "==", 1,
88               "'$test' gives one output arg");
89
90        is(ref($got[0]), $REF,
91           "'$test' output arg is a $REF");
92
93        is($LIB->_str($got[0]), $out0,
94           "'$test' output arg has the right value");
95    };
96}
97
98# Scalar context.
99
100for (my $i = 0 ; $i <= $#data ; ++ $i) {
101    my ($in0, $out0) = @{ $data[$i] };
102
103    my ($x, $got);
104
105    my $test = qq|\$x = $LIB->_new("$in0"); |
106             . qq|\$got = $LIB->_digitsum(\$x);|;
107
108    diag("\n$test\n\n") if $ENV{AUTHOR_DEBUGGING};
109
110    eval $test;
111    is($@, "", "'$test' gives emtpy \$\@");
112
113    subtest "_digitsum() in scalar context: $test", sub {
114        plan tests => 2,
115
116        is(ref($got), $REF,
117           "'$test' output arg is a $REF");
118
119        is($LIB->_str($got), $out0,
120           "'$test' output arg has the right value");
121    };
122}
123