1# -*- mode: perl; -*-
2
3use strict;
4use warnings;
5
6use Test::More tests => 5469;
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, '_dec');
43
44my @data;
45
46# Small numbers.
47
48for (my $x = 1; $x <= 512 ; ++ $x) {
49    push @data, [ $x, $x - 1 ];
50}
51
52# 11 - 1, 101 - 1, 1001 - 1, 10001 - 1, ...
53
54for (my $p = 1; $p <= 50 ; ++ $p) {
55    my $x = "1" . ("0" x ($p - 1) . "1");
56    my $y = "1" . ("0" x $p);
57    push @data, [ $x, $y ];
58}
59
60# 10 - 1, 100 - 1, 1000 - 1, 10000 - 1, ...
61
62for (my $p = 1; $p <= 50 ; ++ $p) {
63    my $x = "1" . ("0" x $p);
64    my $y = ("9" x $p);
65    push @data, [ $x, $y ];
66}
67
68# 9 - 1, 99 - 1, 999 - 1, 9999 - 1, ...
69
70for (my $p = 1; $p <= 50 ; ++ $p) {
71    my $x = "9" x $p;
72    my $y = "9" x ($p - 1) . "8";
73    push @data, [ $x, $y ];
74}
75
76# Add data in data file.
77
78(my $datafile = $0) =~ s/\.t/.dat/;
79open DATAFILE, $datafile or die "$datafile: can't open file for reading: $!";
80while (<DATAFILE>) {
81    s/\s+\z//;
82    next if /^#/ || ! /\S/;
83    push @data, [ split /:/ ];
84}
85close DATAFILE or die "$datafile: can't close file after reading: $!";
86
87# List context.
88
89for (my $i = 0 ; $i <= $#data ; ++ $i) {
90    my ($in0, $out0) = @{ $data[$i] };
91
92    my ($x, @got);
93
94    my $test = qq|\$x = $LIB->_new("$in0"); |
95             . qq|\@got = $LIB->_dec(\$x);|;
96
97    diag("\n$test\n\n") if $ENV{AUTHOR_DEBUGGING};
98
99    eval $test;
100    is($@, "", "'$test' gives emtpy \$\@");
101
102    subtest "_dec() in list context: $test", sub {
103        plan tests => 6,
104
105        cmp_ok(scalar @got, "==", 1,
106               "'$test' gives one output arg");
107
108        is(ref($got[0]), $REF,
109           "'$test' output arg is a $REF");
110
111        is($LIB->_check($got[0]), 0,
112           "'$test' output is valid");
113
114        is($LIB->_str($got[0]), $out0,
115           "'$test' output arg has the right value");
116
117        is(ref($x), $REF,
118           "'$test' first input arg is still a $REF");
119
120        ok($LIB->_str($x) eq $out0 || $LIB->_str($x) eq $in0,
121           "'$test' input arg has the correct value");
122    };
123}
124
125# Scalar context.
126
127for (my $i = 0 ; $i <= $#data ; ++ $i) {
128    my ($in0, $out0) = @{ $data[$i] };
129
130    my ($x, $got);
131
132    my $test = qq|\$x = $LIB->_new("$in0"); |
133             . qq|\$got = $LIB->_dec(\$x);|;
134
135    diag("\n$test\n\n") if $ENV{AUTHOR_DEBUGGING};
136
137    eval $test;
138    is($@, "", "'$test' gives emtpy \$\@");
139
140    subtest "_dec() in scalar context: $test", sub {
141        plan tests => 5,
142
143        is(ref($got), $REF,
144           "'$test' output arg is a $REF");
145
146        is($LIB->_check($got), 0,
147           "'$test' output is valid");
148
149        is($LIB->_str($got), $out0,
150           "'$test' output arg has the right value");
151
152        is(ref($x), $REF,
153           "'$test' first input arg is still a $REF");
154
155        ok($LIB->_str($x) eq $out0 || $LIB->_str($x) eq $in0,
156           "'$test' input arg has the correct value");
157    };
158}
159