1# -*- mode: perl; -*-
2
3use strict;
4use warnings;
5
6use Test::More tests => 1597;
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, '_copy');
49
50use lib 't';
51use Math::BigInt::Lib::TestUtil qw< randstr >;
52
53# Generate test data.
54
55my @data;
56
57for (my $x = 0 ; $x <= 100 ; ++ $x) {
58    push @data, [ $x ];
59}
60
61for (my $n = 3 ; $n <= 300 ; ++ $n) {
62    my $x = randstr($n, 10);            # random $n-digit integer
63    push @data, [ $x ];
64}
65
66# List context.
67
68for (my $i = 0 ; $i <= $#data ; ++ $i) {
69    my ($in0) = @{ $data[$i] };
70    my $out0 = $in0;
71
72    my ($x, @got);
73
74    my $test = qq|\$x = $LIB->_new("$in0"); |
75             . qq|\@got = $LIB->_copy(\$x);|;
76
77    diag("\n$test\n\n") if $ENV{AUTHOR_DEBUGGING};
78
79    eval $test;
80    is($@, "", "'$test' gives emtpy \$\@");
81
82    subtest "_copy() in list context: $test", sub {
83        plan tests => 5;
84
85        cmp_ok(scalar @got, "==", 1,
86               "'$test' gives one output arg");
87
88        is(ref($got[0]), $REF,
89           "'$test' output arg is a $REF");
90
91        is($LIB->_check($got[0]), 0,
92           "'$test' output is valid");
93
94        is($LIB->_str($got[0]), $out0,
95           "'$test' output arg has the right value");
96
97      SKIP: {
98            skip "Scalar::Util not available", 1 unless $scalar_util_ok;
99
100            isnt(refaddr($got[0]), refaddr($x),
101                 "'$test' output arg is not the input arg");
102        }
103    };
104}
105
106# Scalar context.
107
108for (my $i = 0 ; $i <= $#data ; ++ $i) {
109    my ($in0) = @{ $data[$i] };
110    my $out0 = $in0;
111
112    my ($x, $got);
113
114    my $test = qq|\$x = $LIB->_new("$in0"); |
115             . qq|\$got = $LIB->_copy(\$x);|;
116
117    diag("\n$test\n\n") if $ENV{AUTHOR_DEBUGGING};
118
119    eval $test;
120    is($@, "", "'$test' gives emtpy \$\@");
121
122    subtest "_copy() in scalar context: $test", sub {
123        plan tests => 4;
124
125        is(ref($got), $REF,
126           "'$test' output arg is a $REF");
127
128        is($LIB->_check($got), 0,
129           "'$test' output is valid");
130
131        is($LIB->_str($got), $out0,
132           "'$test' output arg has the right value");
133
134      SKIP: {
135            skip "Scalar::Util not available", 1 unless $scalar_util_ok;
136
137            isnt(refaddr($got), refaddr($x),
138                 "'$test' output arg is not the input arg");
139        }
140    };
141}
142