File Coverage

blib/lib/REFECO/Blockchain/Contract/Solidity/ABI/Type/Int.pm
Criterion Covered Total %
statement 43 43 100.0
branch 12 12 100.0
condition 10 12 83.3
subroutine 11 11 100.0
pod 0 3 0.0
total 76 81 93.8


line stmt bran cond sub pod time code
1             package REFECO::Blockchain::Contract::Solidity::ABI::Type::Int;
2              
3 5     5   2728 use v5.26;
  5         19  
4 5     5   25 use strict;
  5         14  
  5         105  
5 5     5   25 use warnings;
  5         10  
  5         149  
6 5     5   28 no indirect;
  5         12  
  5         24  
7              
8 5     5   239 use Carp;
  5         12  
  5         353  
9 5     5   6791 use Math::BigInt;
  5         181772  
  5         28  
10 5     5   134817 use parent qw(REFECO::Blockchain::Contract::Solidity::ABI::Type);
  5         29  
  5         43  
11              
12 5     5   426 use constant DEFAULT_INT_SIZE => 256;
  5         14  
  5         3066  
13              
14             sub encode {
15 87     87 0 150 my $self = shift;
16 87 100       195 return $self->encoded if $self->encoded;
17              
18 39         126 my $bdata = Math::BigInt->new($self->data);
19              
20 39 100       3411 croak "Invalid numeric data @{[$self->data]}" if $bdata->is_nan;
  1         12  
21              
22 38 100 66     299 croak "Invalid data length, signature: @{[$self->fixed_length]}, data length: @{[$bdata->length]}"
  1         4  
  1         3  
23             if $self->fixed_length && $bdata->length > $self->fixed_length;
24              
25 37 100 66     116 croak "Invalid negative numeric data @{[$self->data]}"
  2         6  
26             if $bdata->is_neg && $self->signature =~ /^uint|bool/;
27              
28 35 100 100     294 croak "Invalid bool data it must be 1 or 0 but given @{[$self->data]}"
  1   100     3  
29             if !$bdata->is_zero && !$bdata->is_one && $self->signature =~ /^bool/;
30              
31 34         351 $self->push_static($self->pad_left($bdata->to_hex));
32              
33 34         105 return $self->encoded;
34             }
35              
36             sub decode {
37 14     14 0 27 my $self = shift;
38 14         37 return Math::BigInt->from_hex($self->data->[0]);
39             }
40              
41             sub fixed_length {
42 77     77 0 721 my $self = shift;
43 77 100       194 if ($self->signature =~ /[a-z](\d+)/) {
44 59         310 return $1;
45             }
46 18         69 return DEFAULT_INT_SIZE;
47             }
48              
49             1;
50