Mango/BSON/Number.pm

filter: Statementstmt 92.3% 36 / 39 Branchbran 91.6% 11 / 12 Conditioncond 84.6% 22 / 26 MC/DCmcdc 23.0% 3 / 13 Subroutinesub 83.3% 10 / 12 Podpod 100% 5 / 5 totaltotal 81.3% 87 / 107 CC 16 SCAR 15.5
CC
16
Cov
90%
new
7.1
isa_number
7.0
guess_type
5.0
CRAP 4.7
« Mango/Protocol.pm ↑ Summary Mango/Auth/SCRAM.pm »
1 package Mango::BSON::Number;
2 11 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 0 ▶use overload bool => sub { !!shift->value }, '""' => sub { shift->to_string },
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine __ANON__: not called CC=1 SCAR=1.2
Subroutine __ANON__: called CC=1 SCAR=1.2
4 11 fallback => 1;
5  
6 11 ▶use B;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 11 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 # 32bit integer range
10 11 ▶use constant { INT32_MIN => -(1 << 31) + 1, INT32_MAX => (1 << 31) - 1 };
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 11 has [qw(value type)];
13  
14 17 ▶sub new {
Subroutine new: called CC=6 SCAR=19.6
Pod documented
15 17 my ($class, $value, $type) = @_;
16  
17 17 ▶ $value //= 0;
Condition50%1/2
$value //= 0
l!l
170
MC/DC0%0/1
$value //= 0
$value
Truth table50%1/2
A
A= $value
Aresult
11
00
18 17 ▶ $type //= Mango::BSON::DOUBLE();
Condition33%1/3
$type //= &Mango::BSON::DOUBLE()
l!l&&r!l&&!r
1700
MC/DC0%0/2
$type //= &Mango::BSON::DOUBLE()
$type &Mango::BSON::DOUBLE()
Truth table33%1/3
A //= B
A= $typeB= &Mango::BSON::DOUBLE()
ABresult
1X1
011
000
19  
20 17 ▶ if ($type ne Mango::BSON::DOUBLE() &&
Branch: 1 branch
BranchTrueFalseTotal
if ($type ne &Mango::BSON::DOUBLE() and $type ne &Mango::BSON::INT32() and $type ne &Mango::BSON::INT64()) 0 17 17
Condition83%5/6
$type ne &Mango::BSON::DOUBLE() and $type ne &Mango::BSON::INT32() and $type ne &Mango::BSON::INT64()
!ll&&!rl&&r
1070
$type ne &Mango::BSON::DOUBLE() and $type ne &Mango::BSON::INT32()
!ll&&!rl&&r
377
MC/DC0%0/3
$type ne &Mango::BSON::DOUBLE() and $type ne &Mango::BSON::INT32() and $type ne &Mango::BSON::INT64()
$type ne &Mango::BSON::DOUBLE() $type ne &Mango::BSON::INT32() $type ne &Mango::BSON::INT64()
Truth table75%3/4
A and B and C
A= $type ne &Mango::BSON::DOUBLE()B= $type ne &Mango::BSON::INT32()C= $type ne &Mango::BSON::INT64()
ABCresult
0XX0
10X0
1100
1111
21 $type ne Mango::BSON::INT32() &&
22 $type ne Mango::BSON::INT64())
23 {
24 0 croak "Invalid numerical type: '$type'";
25 }
26  
27 17 return $class->SUPER::new(value => $value, type => $type);
28 }
29  
30 0 ▶sub TO_JSON { 0 + shift->value }
Subroutine TO_JSON: not called CC=1 SCAR=6.9
Pod documented
31  
32 34 ▶sub to_string { '' . shift->value }
Subroutine to_string: called CC=1 SCAR=0.0
Pod documented
33  
34 47 ▶sub isa_number {
Subroutine isa_number: called CC=7 SCAR=19.5
Pod documented
35 47 my $value = shift;
36  
37 47 my $flags = B::svref_2object(\$value)->FLAGS;
38  
39 47 ▶ if ($flags & (B::SVp_IOK | B::SVp_NOK)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flags & 12288) 26 21 47
40 26 ▶ if ( ( 0 + $value eq $value && $value * 0 == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (0 + $value eq $value and $value * 0 == 0 or "NaN" eq $value or "Inf" eq $value or "-Inf" eq $value) 23 3 26
Condition100%12/12
0 + $value eq $value and $value * 0 == 0 or "NaN" eq $value or "Inf" eq $value or "-Inf" eq $value
l!l&&r!l&&!r
2213
0 + $value eq $value and $value * 0 == 0 or "NaN" eq $value or "Inf" eq $value
l!l&&r!l&&!r
2024
0 + $value eq $value and $value * 0 == 0 or "NaN" eq $value
l!l&&r!l&&!r
1916
0 + $value eq $value and $value * 0 == 0
!ll&&!rl&&r
3419
MC/DC20%1/5
0 + $value eq $value and $value * 0 == 0 or "NaN" eq $value or "Inf" eq $value or "-Inf" eq $value
0 + $value eq $value $value * 0 == 0 "NaN" eq $value "Inf" eq $value "-Inf" eq $value
Truth table55%5/9
A and B or C or D or E
A= 0 + $value eq $valueB= $value * 0 == 0C= "NaN" eq $valueD= "Inf" eq $valueE= "-Inf" eq $value
ABCDEresult
11XXX1
0X1XX1
101XX1
0X01X1
1001X1
0X0011
100011
0X0000
100000
41 || ( 0 + 'nan' eq $value )
42 || ( 0 + '+inf' eq $value )
43 || ( 0 + '-inf' eq $value ) )
44 {
45 23 return $flags;
46 }
47 }
48  
49 24 return undef;
50 }
51  
52 47 ▶sub guess_type {
Subroutine guess_type: called CC=5 SCAR=16.1
Pod documented
53 47 my $value = shift;
54  
55 47 ▶ if (my $flags = isa_number($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $flags = isa_number($value)) 23 24 47
56 # Double
57 23 ▶ return Mango::BSON::DOUBLE() if $flags & B::SVp_NOK;
Branch: 1 branch
BranchTrueFalseTotal
if $flags & 8192 8 15 23
58  
59 # Int32
60 15 ▶ return Mango::BSON::INT32() if $value <= INT32_MAX && $value >= INT32_MIN;
Branch: 1 branch
BranchTrueFalseTotal
if $value <= 2147483647 and $value >= '-2147483647' 13 2 15
Condition100%3/3
$value <= 2147483647 and $value >= '-2147483647'
!ll&&!rl&&r
1113
MC/DC100%2/2
$value <= 2147483647 and $value >= '-2147483647'
$value <= 2147483647 $value >= '-2147483647'
Truth table100%3/3
A and B
A= $value <= 2147483647B= $value >= '-2147483647'
ABresult
0X0
100
111
61  
62 # Int64
63 2 return Mango::BSON::INT64();
64 }
65  
66 24 return undef;
67 }
68  
69 11 1;
70  
71 =encoding utf8
72  
73 =head1 NAME
74  
75 Mango::BSON::Number - Numerical types
76  
77 =head1 SYNOPSIS
78  
79 use Mango::BSON;
80 use Mango::BSON::Number;
81  
82 my $number = Mango::BSON::Number->new(666, Mango::BSON::INT64);
83 say $number;
84  
85 =head1 DESCRIPTION
86  
87 L<Mango::BSON::Number> is a container for numerical values with a strict
88 type.
89  
90 =head1 METHODS
91  
92 L<Mango::BSON::Number> inherits all methods from L<Mojo::Base> and implements
93 the following new ones.
94  
95 =head2 new
96  
97 my $number = Mango::BSON::Number->new(3.14, Mango::BSON::DOUBLE);
98  
99 Construct a new L<Mango::BSON::Number> object. Croak if the value is
100 incompatible with the given type. The 3 supported types are C<DOUBLE>,
101 C<INT32> and C<INT64>.
102  
103 =head2 TO_JSON
104  
105 my $num = $obj->TO_JSON;
106  
107 Return the numerical value.
108  
109 =head2 to_string
110  
111 my $str = $num->to_string;
112  
113 Return the value as a string.
114  
115 =head2 isa_number
116  
117 my $flags = Mango::BSON::Number::isa_number(25);
118  
119 Determine if the given variable is a number by looking at the internal
120 flags of the perl scalar object.
121  
122 Return C<undef> if the value is not a number, or a non-null value otherwise.
123 This value contains flags which can be used for finer analysis of the scalar.
124  
125 =head2 guess_type
126  
127 my $mongo_type = Mango::BSON::Number::guess_type(25);
128  
129 Chose which BSON type to use to encode the given numeric value. Possible
130 types are: C<Mango::BSON::DOUBLE>, C<Mango::BSON::INT32> or
131 C<Mango::BSON::INT64>.
132  
133 Return C<undef> if the given value is not a number.
134  
135 =head1 OPERATORS
136  
137 L<Mango::BSON::Time> overloads the following operators.
138  
139 =head2 bool
140  
141 my $bool = !!$num;
142  
143 =head2 stringify
144  
145 my $str = "$num";
146  
147 Alias for L</to_string>.
148  
149 =head1 SEE ALSO
150  
151 L<Mango>, L<Mojolicious::Guides>, L<http://mojolicio.us>.
152  
153 =cut
« Mango/Protocol.pm ↑ Summary Mango/Auth/SCRAM.pm »