1# -*- mode: perl; -*-
2
3use strict;
4use warnings;
5
6use Test::More tests => 105;
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, '_from_base_num');
43
44# For simplicity, we use the same data in the test programs for _to_base_num() and
45# _from_base_num().
46
47my @data =
48  (
49   [ 0, 2, [ 0 ] ],
50   [ 1, 2, [ 1 ] ],
51   [ 2, 2, [ 1, 0 ] ],
52   [ 3, 2, [ 1, 1, ] ],
53   [ 4, 2, [ 1, 0, 0 ] ],
54
55   [ 0, 10, [ 0 ] ],
56   [ 1, 10, [ 1 ] ],
57   [ 12, 10, [ 1, 2 ] ],
58   [ 123, 10, [ 1, 2, 3 ] ],
59   [ 1230, 10, [ 1, 2, 3, 0 ] ],
60
61   [ "123456789", 100, [ 1, 23, 45, 67, 89 ] ],
62
63   [ "1234567890" x 3,
64     "987654321",
65     [ "128", "142745769", "763888804", "574845669" ]],
66
67   [ "1234567890" x 5,
68     "987654321" x 3,
69     [ "12499999874843750102814", "447551941015330718793208596" ]],
70  );
71
72# List context.
73
74for (my $i = 0 ; $i <= $#data ; ++ $i) {
75    my @in   = ($data[$i][2], $data[$i][1]);
76    my $out0 = $data[$i][0];
77
78    my ($x, @got);
79
80    # We test with the base given as a scalar and as a reference.
81
82    for my $base_as_scalar (1, 0) {
83        for my $elements_as_scalar (1, 0) {
84
85            my $test = "\@got = $LIB->_from_base_num([";
86            if ($elements_as_scalar) {
87                $test .= join ", ", map qq|"$_"|, @{ $in[0] };
88            } else {
89                $test .= join ", ", map qq|$LIB->_new("$_")|, @{ $in[0] };
90            }
91            $test .= "], ";
92            if ($base_as_scalar) {
93                $test .= qq|"$in[1]"|;
94            } else {
95                $test .= qq|$LIB->_new("$in[1]")|;
96            }
97            $test .= ")";
98
99            diag("\n$test\n\n") if $ENV{AUTHOR_DEBUGGING};
100
101            eval $test;
102            is($@, "", "'$test' gives emtpy \$\@");
103
104            subtest "_from_base_num() in list context: $test", sub {
105                plan tests => 4,
106
107                cmp_ok(scalar @got, '==', 1,
108                       "'$test' gives one output arg");
109
110                is(ref($got[0]), $REF,
111                   "'$test' output arg is a $REF");
112
113                is($LIB->_check($got[0]), 0,
114                   "'$test' output is valid");
115
116                is($LIB->_str($got[0]), $out0,
117                   "'$test' output arg has the right value");
118            };
119        }
120    }
121}
122