Lite/t/bignums.t

filter: Statementstmt 94.5% 52 / 55 Branchbran 75.0% 9 / 12 Conditioncond 40.0% 2 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 100% 10 / 10 Podpod 100% 1 / 1 totaltotal 86.0% 74 / 86 CC 7 SCAR 11.3
CC
7
Cov
88%
new
6.9
_new
2.0
_str
2.0
CRAP 3.1
« Lite/t/v4-num.t ↑ Summary Lite/t/bigint.t »
1  
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use Test::More;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶use NetAddr::IP::Lite;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 #use NetAddr::IP::Util qw(
7 # bcd2bin
8 # ipv6_n2x
9 # bin2bcd
10 #);
11 #use Data::Dumper;
12  
13 1 my @num = qw(
14 2001:468:D01:3C:0:0:80DF:3C1B/128 42540577535367674011024906890208295963
15 73.150.6.197/32 1234568901
16 128.0.0.0/32 2147483648
17 0:0:0:0:0:1:0:0/128 4294967296
18 0:0:0:0:0:2:0:0/128 8589934592
19 0:0:0:0:0:2:540B:E400/128 10000000000
20 0:0:0:0:0:4:0:0/128 17179869184
21 0:0:0:0:0:8:0:0/128 34359738368
22 0:0:0:0:0:10:0:0/128 68719476736
23 0:0:0:0:0:20:0:0/128 137438953472
24 0:0:0:0:0:40:0:0/128 274877906944
25 0:0:0:0:0:80:0:0/128 549755813888
26 0:0:0:0:0:100:0:0/128 1099511627776
27 0:0:0:0:0:200:0:0/128 2199023255552
28 0:0:0:0:0:400:0:0/128 4398046511104
29 0:0:0:0:0:800:0:0/128 8796093022208
30 0:0:0:0:0:1000:0:0/128 17592186044416
31 0:0:0:0:0:2000:0:0/128 35184372088832
32 0:0:0:0:0:4000:0:0/128 70368744177664
33 0:0:0:0:0:8000:0:0/128 140737488355328
34 0:0:0:0:8000:0:0:0/128 9223372036854775808
35 0:0:0:8000:0:0:0:0/128 604462909807314587353088
36 0:0:8000:0:0:0:0:0/128 39614081257132168796771975168
37 0:8000:0:0:0:0:0:0/128 2596148429267413814265248164610048
38 8000:0:0:0:0:0:0:0/128 170141183460469231731687303715884105728
39 255.255.255.255/32 4294967295
40 1.2.3.4/32 16909060
41 10.253.230.9/32 184411657
42 );
43  
44 1 plan tests => scalar @num;
45  
46 #diag ("\ntesting SCALARS\n\n");
47  
48 1 ▶for(my $i = 0;$i <= $#num;$i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#num) 28 1 29
49 28 my $n = $num[$i +1];
50 28 my $ip = new NetAddr::IP::Lite($n);
51 28 ok($ip eq $num[$i],"$n\t=> got: $ip\texp: ". $num[$i]);
52 }
53  
54 #diag ("\ntesting Math::BigInt's\n\n");
55  
56 1 ▶for(my $i = 0;$i <= $#num;$i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#num) 28 1 29
57 28 my $n = new Math::BigInt($num[$i +1]);
58 28 my $ip = new NetAddr::IP::Lite($num[$i +1]);
59 28 ok($ip eq $num[$i],"$n\t=> got: $ip\texp: ". $num[$i]);
60 }
61  
62  
63  
64  
65 # simulate the use of Math::BigInt
66  
67 package Math::BigInt;
68 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
69  
70 use overload
71 1 ▶'""' => sub { $_[0]->_str(); };
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
72  
73 sub BASE_LEN () { 7 };
74  
75 28 ▶sub _str { # adapted from Math::BigInt::Calc::_str
Subroutine _str: called CC=2 SCAR=6.9
76 # (ref to BINT) return num_str
77 # Convert number from internal base 100000 format to string format.
78 # internal format is always normalized (no leading zeros, "-0" => "+0")
79 28 my $ar = $_[0]->{value};
80  
81 28 my $l = scalar @$ar; # number of parts
82 28 my $ret = "";
83 # handle first one different to strip leading zeros from it (there are no
84 # leading zero parts in internal representation)
85 28 $l --; $ret .= int($ar->[$l]); $l--;
86 # Interestingly, the pre-padd method uses more time
87 # the old grep variant takes longer (14 vs. 10 sec)
88 28 my $z = '0' x (BASE_LEN -1);
89 28 ▶ while ($l >= 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($l >= 0) 46 28 74
90 {
91 46 $ret .= substr($z.$ar->[$l],- BASE_LEN); # fastest way I could think of
92 46 $l--;
93 }
94 28 $ret;
95 }
96  
97 28 ▶sub new { # adapted from Math::BigInt::new
Subroutine new: called CC=5 SCAR=19.3
Pod documented
98 28 my ($class,$wanted) = @_;
99 28 my $self = bless {}, $class;
100  
101 28 ▶ die "oops, not a good Math::BigInt number"
Branch: 1 branch
BranchTrueFalseTotal
unless not ref $wanted and $wanted =~ /^([+-]?)[1-9][0-9]*\z/ 0 28 28
Condition33%1/3
not ref $wanted and $wanted =~ /^([+-]?)[1-9][0-9]*\z/
!ll&&!rl&&r
0028
MC/DC0%0/2
not ref $wanted and $wanted =~ /^([+-]?)[1-9][0-9]*\z/
not ref $wanted $wanted =~ /^([+-]?)[1-9][0-9]*\z/
Truth table33%1/3
not(A) and B
A= not ref $wantedB= $wanted =~ /^([+-]?)[1-9][0-9]*\z/
ABresult
0X0
100
111
102 unless ((!ref $wanted) && ($wanted =~ /^([+-]?)[1-9][0-9]*\z/));
103 28 ▶ $self->{sign} = $1 || '+';
Condition50%1/2
$1 || '+'
l!l
028
MC/DC0%0/1
$1 || '+'
$1
Truth table50%1/2
A
A= $1
Aresult
11
00
104  
105 28 ▶ if ($wanted =~ /^[+-]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wanted =~ /^[+-]/) { } 0 28 28
106 # remove sign without touching wanted to make it work with constants
107 0 my $t = $wanted; $t =~ s/^[+-]//;
108 0 $self->{value} = _new($t);
109 }
110 else {
111 28 $self->{value} = _new($wanted);
112 }
113 28 return $self;
114 }
115  
116 28 ▶sub _new { # adapted from Math::BigInt::Calc::_new
Subroutine _new: called CC=2 SCAR=7.0
117 28 my $wanted = $_[0];
118 # (ref to string) return ref to num_array
119 # Convert a number from string format (without sign) to internal base
120 # 1ex format. Assumes normalized value as input.
121 28 my $il = length($wanted)-1;
122  
123 # < BASE_LEN due len-1 above
124 28 ▶ return [ int($wanted) ] if $il < BASE_LEN; # shortcut for short numbers
Branch: 1 branch
BranchTrueFalseTotal
if $il < 7 0 28 28
125  
126 28 my $base_len = BASE_LEN;
127 # this leaves '00000' instead of int 0 and will be corrected after any op
128 28 [ reverse(unpack("a" . ($il % BASE_LEN +1)
129 . ("a$base_len" x ($il / BASE_LEN)), $wanted)) ];
130 }
« Lite/t/v4-num.t ↑ Summary Lite/t/bigint.t »