1# -*- mode: perl; -*-
2
3use strict;
4use warnings;
5
6use Test::More tests => 3997;
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
42my $scalar_util_ok = eval { require Scalar::Util; };
43Scalar::Util -> import('refaddr') if $scalar_util_ok;
44
45diag "Skipping some tests since Scalar::Util is not installed."
46  unless $scalar_util_ok;
47
48can_ok($LIB, '_div');
49
50my @data;
51
52# Small numbers.
53
54for (my $x = 0; $x <= 24 ; ++ $x) {
55    for (my $y = 1; $y <= 24 ; ++ $y) {
56        push @data, [ $x, $y, int($x / $y), $x % $y ];
57    }
58}
59
60# Add data in data file.
61
62(my $datafile = $0) =~ s/\.t/.dat/;
63open DATAFILE, $datafile or die "$datafile: can't open file for reading: $!";
64while (<DATAFILE>) {
65    s/\s+\z//;
66    next if /^#/ || ! /\S/;
67    push @data, [ split /:/ ];
68}
69close DATAFILE or die "$datafile: can't close file after reading: $!";
70
71# List context.
72
73for (my $i = 0 ; $i <= $#data ; ++ $i) {
74    my ($in0, $in1, $out0, $out1) = @{ $data[$i] };
75
76    my ($x, $y, @got);
77
78    my $test = qq|\$x = $LIB->_new("$in0"); |
79             . qq|\$y = $LIB->_new("$in1"); |
80             . qq|\@got = $LIB->_div(\$x, \$y);|;
81
82    diag("\n$test\n\n") if $ENV{AUTHOR_DEBUGGING};
83
84    eval $test;
85    is($@, "", "'$test' gives emtpy \$\@");
86
87    subtest "_div() in list context: $test", sub {
88        plan tests => 13;
89
90        cmp_ok(scalar @got, '==', 2,
91               "'$test' gives two output args");
92
93        is(ref($got[0]), $REF,
94           "'$test' first output arg is a $REF");
95
96        is($LIB->_check($got[0]), 0,
97           "'$test' first output arg is valid");
98
99        is($LIB->_str($got[0]), $out0,
100           "'$test' first output arg has the right value");
101
102      SKIP: {
103            skip "Scalar::Util not available", 1 unless $scalar_util_ok;
104
105            isnt(refaddr($got[0]), refaddr($y),
106                 "'$test' first output arg is not the second input arg");
107        }
108
109      SKIP: {
110            skip "$LIB doesn't use real objects", 1
111              if $LIB eq 'Math::BigInt::FastCalc';
112
113            is(ref($got[1]), $REF,
114               "'$test' second output arg is a $REF");
115        }
116
117        is($LIB->_check($got[1]), 0,
118           "'$test' second output arg is valid");
119
120        is($LIB->_str($got[1]), $out1,
121           "'$test' second output arg has the right value");
122
123      SKIP: {
124            skip "Scalar::Util not available", 1 unless $scalar_util_ok;
125
126            isnt(refaddr($got[1]), refaddr($y),
127                 "'$test' second output arg is not the second input arg");
128        }
129
130        is(ref($x), $REF,
131           "'$test' first input arg is still a $REF");
132
133        ok($LIB->_str($x) eq $out0 || $LIB->_str($x) eq $in0,
134           "'$test' first input arg has the correct value");
135
136        is(ref($y), $REF,
137           "'$test' second input arg is still a $REF");
138
139        is($LIB->_str($y), $in1,
140           "'$test' second input arg is unmodified");
141    };
142}
143
144# Scalar context.
145
146for (my $i = 0 ; $i <= $#data ; ++ $i) {
147    my ($in0, $in1, $out0, $out1) = @{ $data[$i] };
148
149    my ($x, $y, $got);
150
151    my $test = qq|\$x = $LIB->_new("$in0"); |
152             . qq|\$y = $LIB->_new("$in1"); |
153             . qq|\$got = $LIB->_div(\$x, \$y);|;
154
155    diag("\n$test\n\n") if $ENV{AUTHOR_DEBUGGING};
156
157    eval $test;
158    is($@, "", "'$test' gives emtpy \$\@");
159
160    subtest "_div() in scalar context: $test", sub {
161        plan tests => 8;
162
163        is(ref($got), $REF,
164           "'$test' output arg is a $REF");
165
166        is($LIB->_check($got), 0,
167           "'$test' output is valid");
168
169        is($LIB->_str($got), $out0,
170           "'$test' output arg has the right value");
171
172      SKIP: {
173            skip "Scalar::Util not available", 1 unless $scalar_util_ok;
174
175            isnt(refaddr($got), refaddr($y),
176                 "'$test' output arg is not the second input arg");
177        }
178
179        is(ref($x), $REF,
180           "'$test' first input arg is still a $REF");
181
182        ok($LIB->_str($x) eq $out0 || $LIB->_str($x) eq $in0,
183           "'$test' first input arg has the correct value");
184
185        is(ref($y), $REF,
186           "'$test' second input arg is still a $REF");
187
188        is($LIB->_str($y), $in1,
189           "'$test' second input arg is unmodified");
190    };
191}
192