summaryrefslogtreecommitdiff
path: root/cpan/Math-BigInt/t/from_base_num-mbi.t
blob: b407787274050ce399268b3d455fc38312bc4cf0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!perl

use strict;
use warnings;

use Test::More tests => 365;

my $class;

BEGIN { $class = 'Math::BigInt'; }
BEGIN { use_ok($class); }

# For simplicity, we use the same data in the test programs for to_base_num() and
# from_base_num().

my @data =
  (
   [ 0, 2, [ 0 ] ],
   [ 1, 2, [ 1 ] ],
   [ 2, 2, [ 1, 0 ] ],
   [ 3, 2, [ 1, 1, ] ],
   [ 4, 2, [ 1, 0, 0 ] ],

   [ 0, 10, [ 0 ] ],
   [ 1, 10, [ 1 ] ],
   [ 12, 10, [ 1, 2 ] ],
   [ 123, 10, [ 1, 2, 3 ] ],
   [ 1230, 10, [ 1, 2, 3, 0 ] ],

   [ "123456789", 100, [ 1, 23, 45, 67, 89 ] ],

   [ "1234567890" x 3,
     "987654321",
     [ "128", "142745769", "763888804", "574845669" ]],

   [ "1234567890" x 5,
     "987654321" x 3,
     [ "12499999874843750102814", "447551941015330718793208596" ]],
  );

for (my $i = 0 ; $i <= $#data ; ++ $i) {
    my @in = ($data[$i][2], $data[$i][1]);
    my $out = $data[$i][0];

    # As class method.

    {
        for my $base_as_scalar (1, 0) {
            for my $elements_as_scalar (1, 0) {

                my $x;
                my $test = "\$x = $class -> from_base_num([";
                if ($elements_as_scalar) {
                    $test .= join ", ", map qq|"$_"|, @{ $in[0] };
                } else {
                    $test .= join ", ", map qq|$class -> new("$_")|, @{ $in[0] };
                }
                $test .= "], ";
                if ($base_as_scalar) {
                    $test .= qq|"$in[1]"|;
                } else {
                    $test .= qq|$class -> new("$in[1]")|;
                }
                $test .= ")";

                eval $test;
                die "\nThe following test died when eval()'ed. This",
                  "indicates a broken test\n\n    $test\n\nThe error",
                  " message was\n\n    $@\n" if $@;

                subtest $test, sub {
                    plan tests => 2,

                    is(ref($x), $class, "output arg is a $class");
                    is($x, $out, 'output arg has the right value');
                };
            }
        }
    }

    # As instance method.

    {
        for my $base_as_scalar (1, 0) {
            for my $elements_as_scalar (1, 0) {
                for my $str ("-1", "0", "1", "-inf", "+inf", "NaN") {

                    my $x;
                    my $test = qq|\$x = $class -> new("$str");|;
                    $test .= " \$x -> from_base_num([";
                    if ($elements_as_scalar) {
                        $test .= join ", ", map qq|"$_"|, @{ $in[0] };
                    } else {
                        $test .= join ", ", map qq|$class -> new("$_")|, @{ $in[0] };
                    }
                    $test .= "], ";
                    if ($base_as_scalar) {
                        $test .= qq|"$in[1]"|;
                    } else {
                        $test .= qq|$class -> new("$in[1]")|;
                    }
                    $test .= ")";

                    eval $test;
                    die "\nThe following test died when eval()'ed. This",
                      "indicates a broken test\n\n    $test\n\nThe error",
                      " message was\n\n    $@\n" if $@;

                    subtest $test, sub {
                        plan tests => 2,

                        is(ref($x), $class, "output arg is a $class");
                        is($x, $out, 'output arg has the right value');
                    };
                }
            }
        }
    }
}