1# -*- mode: perl; -*-
2
3use strict;
4use warnings;
5
6use Test::More tests => 14523;
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, '_modinv');
49
50my @data;
51
52# Add data in data file.
53
54(my $datafile = $0) =~ s/\.t/.dat/;
55open DATAFILE, $datafile or die "$datafile: can't open file for reading: $!";
56while (<DATAFILE>) {
57    s/\s+\z//;
58    next if /^#/ || ! /\S/;
59    push @data, [ split /:/ ];
60}
61close DATAFILE or die "$datafile: can't close file after reading: $!";
62
63# List context.
64
65for (my $i = 0 ; $i <= $#data ; ++ $i) {
66    my ($in0, $in1, $out0, $out1, $out2, $out3) = @{ $data[$i] };
67
68    my ($x, $m, @got);
69
70    my $test = qq|\$x = $LIB->_new("$in0"); |
71             . qq|\$m = $LIB->_new("$in1"); |
72             . qq|\@got = $LIB->_modinv(\$x, \$m);|;
73
74    diag("\n$test\n\n") if $ENV{AUTHOR_DEBUGGING};
75
76    eval $test;
77    is($@, "", "'$test' gives emtpy \$\@");
78
79    subtest "_modinv() in list context: $test", sub {
80        if ($out0 eq "undef") {
81
82            plan tests => 1;
83
84            cmp_ok(scalar @got, "==", 0,
85                   "'$test' gives two output args");
86
87        } else {
88
89            plan tests => 7;
90
91            cmp_ok(scalar @got, "==", 2,
92                   "'$test' gives two output args");
93
94            is(ref($got[0]), $REF,
95               "'$test' first output arg is a $REF");
96
97            is($LIB->_check($got[0]), 0,
98               "'$test' first output arg is valid");
99
100          SKIP: {
101                skip "Scalar::Util not available", 1 unless $scalar_util_ok;
102
103                isnt(refaddr($got[0]), refaddr($m),
104                     "'$test' first output arg is not the second input arg");
105            }
106
107            is(ref($got[1]), "",
108               "'$test' second output arg is a scalar");
109
110            like($got[1], qr/^[+-]\z/,
111               "'$test' second output arg is valid");
112
113            my $got  = $got[1] . $LIB->_str($got[0]);   # prepend sign to value
114            my $alt0 = $out1 . $out0;
115            my $alt1 = $out3 . $out2;
116
117            ok($got eq $alt0 || $got eq $alt1,
118               "'$test' output args have the right value");
119        }
120    };
121}
122