BSON/Double.pm

filter: Statementstmt 71.7% 56 / 78 Branchbran 46.3% 25 / 54 Conditioncond 77.7% 7 / 9 MC/DCmcdc 66.6% 4 / 6 Subroutinesub 37.1% 13 / 35 Podpod 50.0% 1 / 2 totaltotal 57.6% 106 / 184 CC 26 SCAR 16.4
CC
26
Cov
62%
TO_JSON
12.0
__ANON__
6.0
__ANON__
5.1
CRAP 5.2
« BSON/Timestamp.pm ↑ Summary BSON/Int64.pm »
1 71 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 71 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 71 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 package BSON::Double;
6 # ABSTRACT: BSON type wrapper for Double
7  
8 71 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 71 our $VERSION = 'v1.12.2';
10  
11 71 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 #pod =attr value
14 #pod
15 #pod A numeric scalar (or the special strings "Inf", "-Inf" or "NaN"). This
16 #pod will be coerced to Perl's numeric type. The default is 0.0.
17 #pod
18 #pod =cut
19  
20 71 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 71 has 'value' => (
23 is => 'ro'
24 );
25  
26 71 ▶use namespace::clean -except => 'meta';
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 71 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
29 71 nInf => unpack("d<",pack("H*","000000000000f0ff")),
30 pInf => unpack("d<",pack("H*","000000000000f07f")),
31 NaN => unpack("d<",pack("H*","000000000000f8ff")),
32 71 };
33  
34 94 ▶sub BUILD {
Subroutine BUILD: called CC=2 SCAR=6.9
Pod undocumented
35 94 my $self = shift;
36 # coerce to NV internally
37 94 ▶ $self->{value} = defined( $self->{value} ) ? $self->{value} / 1.0 : 0.0;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'value'} ? : 92 2 94
38 }
39  
40 #pod =method TO_JSON
41 #pod
42 #pod Returns a double.
43 #pod
44 #pod If the C<BSON_EXTJSON> environment variable is true and the
45 #pod C<BSON_EXTJSON_RELAXED> environment variable is false, returns a hashref
46 #pod compatible with
47 #pod MongoDB's L<extended JSON|https://github.com/mongodb/specifications/blob/master/source/extended-json.rst>
48 #pod format, which represents it as a document as follows:
49 #pod
50 #pod {"$numberDouble" : "42.0"}
51 #pod
52 #pod If C<BSON_EXTJSON> is false and the value is 'Inf', '-Inf' or 'NaN'
53 #pod (which are illegal in regular JSON), then an exception is thrown.
54 #pod
55 #pod =cut
56  
57 71 ▶my $use_win32_specials = ($^O eq 'MSWin32' && $] lt "5.022");
Condition33%1/3
$^O eq 'MSWin32' && $] lt '5.022'
!ll&&!rl&&r
7100
MC/DC0%0/2
$^O eq 'MSWin32' && $] lt '5.022'
$^O eq 'MSWin32' $] lt '5.022'
Truth table33%1/3
A && B
A= $^O eq 'MSWin32'B= $] lt '5.022'
ABresult
0X0
100
111
58  
59 71 my $win32_specials = qr/-?1.\#IN[DF]/i;
60 71 my $unix_specials = qr/-?(?:inf|nan)/i;
61 71 ▶my $illegal = $use_win32_specials ? qr/^$win32_specials/ : qr/^$unix_specials/;
Branch: 1 branch
BranchTrueFalseTotal
$use_win32_specials ? : 0 71 71
62  
63 71 ▶my $is_inf = $use_win32_specials ? qr/^1.\#INF/i : qr/^inf/i;
Branch: 1 branch
BranchTrueFalseTotal
$use_win32_specials ? : 0 71 71
64 71 ▶my $is_ninf = $use_win32_specials ? qr/^-1.\#INF/i : qr/^-inf/i;
Branch: 1 branch
BranchTrueFalseTotal
$use_win32_specials ? : 0 71 71
65 71 ▶my $is_nan = $use_win32_specials ? qr/^-?1.\#(?:IND|QNAN)/i : qr/^-?nan/i;
Branch: 1 branch
BranchTrueFalseTotal
$use_win32_specials ? : 0 71 71
66  
67 46 ▶sub TO_JSON {
Subroutine TO_JSON: called CC=12 SCAR=24.8
Pod documented
68 46 my $copy = "$_[0]->{value}"; # avoid changing value to PVNV
69  
70 46 ▶ if ($ENV{BSON_EXTJSON} && $ENV{BSON_EXTJSON_RELAXED}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"BSON_EXTJSON"} and $ENV{"BSON_EXTJSON_RELAXED"}) 16 30 46
Condition100%3/3
$ENV{"BSON_EXTJSON"} and $ENV{"BSON_EXTJSON_RELAXED"}
!ll&&!rl&&r
42616
MC/DC100%2/2
$ENV{"BSON_EXTJSON"} and $ENV{"BSON_EXTJSON_RELAXED"}
$ENV{"BSON_EXTJSON"} $ENV{"BSON_EXTJSON_RELAXED"}
Truth table100%3/3
A and B
A= $ENV{"BSON_EXTJSON"}B= $ENV{"BSON_EXTJSON_RELAXED"}
ABresult
0X0
100
111
71  
72 16 ▶ return { '$numberDouble' => 'Infinity' }
Branch: 1 branch
BranchTrueFalseTotal
if $copy =~ /$is_inf/ 2 14 16
73 if $copy =~ $is_inf;
74 14 ▶ return { '$numberDouble' => '-Infinity' }
Branch: 1 branch
BranchTrueFalseTotal
if $copy =~ /$is_ninf/ 2 12 14
75 if $copy =~ $is_ninf;
76 12 ▶ return { '$numberDouble' => 'NaN' }
Branch: 1 branch
BranchTrueFalseTotal
if $copy =~ /$is_nan/ 4 8 12
77 if $copy =~ $is_nan;
78 }
79  
80 38 ▶ if ($ENV{BSON_EXTJSON} && !$ENV{BSON_EXTJSON_RELAXED}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"BSON_EXTJSON"} and not $ENV{"BSON_EXTJSON_RELAXED"}) 26 12 38
Condition100%3/3
$ENV{"BSON_EXTJSON"} and not $ENV{"BSON_EXTJSON_RELAXED"}
!ll&&!rl&&r
4826
MC/DC100%2/2
$ENV{"BSON_EXTJSON"} and not $ENV{"BSON_EXTJSON_RELAXED"}
$ENV{"BSON_EXTJSON"} not $ENV{"BSON_EXTJSON_RELAXED"}
Truth table100%3/3
A and not(B)
A= $ENV{"BSON_EXTJSON"}B= not $ENV{"BSON_EXTJSON_RELAXED"}
ABresult
0X0
100
111
81  
82 26 ▶ return { '$numberDouble' => 'Infinity' }
Branch: 1 branch
BranchTrueFalseTotal
if $copy =~ /$is_inf/ 2 24 26
83 if $copy =~ $is_inf;
84 24 ▶ return { '$numberDouble' => '-Infinity' }
Branch: 1 branch
BranchTrueFalseTotal
if $copy =~ /$is_ninf/ 2 22 24
85 if $copy =~ $is_ninf;
86 22 ▶ return { '$numberDouble' => 'NaN' }
Branch: 1 branch
BranchTrueFalseTotal
if $copy =~ /$is_nan/ 4 18 22
87 if $copy =~ $is_nan;
88 18 my $value = $_[0]->{value}/1.0;
89 18 return { '$numberDouble' => "$value" };
90 }
91  
92 12 ▶ croak( "The value '$copy' is illegal in JSON" )
Branch: 1 branch
BranchTrueFalseTotal
if $copy =~ /$illegal/ 1 11 12
93 if $copy =~ $illegal;
94  
95 11 return $_[0]->{value}/1.0;
96 }
97  
98 71 ▶use overload (
Subroutine BEGIN: called CC=1 SCAR=4.6
99 # Unary
100 0 ▶ q{""} => sub { "$_[0]->{value}" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
101 9 ▶ q{0+} => sub { $_[0]->{value} },
Subroutine __ANON__: called CC=1 SCAR=0.0
102 0 ▶ q{~} => sub { ~( $_[0]->{value} ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
103 # Binary
104 0 ▶ ( map { $_ => eval "sub { return \$_[0]->{value} $_ \$_[1] }" } qw( + * ) ), ## no critic
Subroutine __ANON__: not called CC=1 SCAR=2.6
Subroutine __ANON__: not called CC=1 SCAR=2.6
105 (
106 map {
107 0 ▶ $_ => eval ## no critic
Subroutine __ANON__: not called CC=2 SCAR=16.3
Subroutine __ANON__: called CC=2 SCAR=16.3
Subroutine __ANON__: not called CC=2 SCAR=16.3
Subroutine __ANON__: not called CC=2 SCAR=16.3
Subroutine __ANON__: not called CC=2 SCAR=16.3
Subroutine __ANON__: not called CC=2 SCAR=16.3
Subroutine __ANON__: not called CC=2 SCAR=16.3
Subroutine __ANON__: not called CC=2 SCAR=16.3
Subroutine __ANON__: not called CC=2 SCAR=16.3
Subroutine __ANON__: not called CC=2 SCAR=16.3
Subroutine __ANON__: not called CC=2 SCAR=16.3
Subroutine __ANON__: not called CC=2 SCAR=16.3
Branch: 12 branches
BranchTrueFalseTotal
$_[2] ? : 0 0 0
$_[2] ? : 0 32 32
$_[2] ? : 0 0 0
$_[2] ? : 0 0 0
$_[2] ? : 0 0 0
$_[2] ? : 0 0 0
$_[2] ? : 0 0 0
$_[2] ? : 0 0 0
$_[2] ? : 0 0 0
$_[2] ? : 0 0 0
$_[2] ? : 0 0 0
$_[2] ? : 0 0 0
108 "sub { return \$_[2] ? \$_[1] $_ \$_[0]->{value} : \$_[0]->{value} $_ \$_[1] }"
109 } qw( - / % ** << >> x <=> cmp & | ^ )
110 ),
111 (
112 0 ▶ map { $_ => eval "sub { return $_(\$_[0]->{value}) }" } ## no critic
Subroutine __ANON__: not called CC=1 SCAR=4.9
Subroutine __ANON__: not called CC=1 SCAR=4.9
Subroutine __ANON__: not called CC=1 SCAR=4.9
Subroutine __ANON__: not called CC=1 SCAR=4.9
Subroutine __ANON__: not called CC=1 SCAR=4.9
Subroutine __ANON__: not called CC=1 SCAR=4.9
113 qw( cos sin exp log sqrt int )
114 ),
115 0 ▶ q{atan2} => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
116 0 ▶ return $_[2] ? atan2( $_[1], $_[0]->{value} ) : atan2( $_[0]->{value}, $_[1] );
Branch: 1 branch
BranchTrueFalseTotal
$_[2] ? : 0 0 0
117 },
118  
119 # Special
120 71 fallback => 1,
121 71 );
122  
123 71 1;
124  
125 =pod
126  
127 =encoding UTF-8
128  
129 =head1 NAME
130  
131 BSON::Double - BSON type wrapper for Double
132  
133 =head1 VERSION
134  
135 version v1.12.2
136  
137 =head1 SYNOPSIS
138  
139 use BSON::Types ':all';
140  
141 my $bytes = bson_double( $number );
142  
143 =head1 DESCRIPTION
144  
145 This module provides a BSON type wrapper for a numeric value that
146 would be represented in BSON as a double.
147  
148 =head1 ATTRIBUTES
149  
150 =head2 value
151  
152 A numeric scalar (or the special strings "Inf", "-Inf" or "NaN"). This
153 will be coerced to Perl's numeric type. The default is 0.0.
154  
155 =head1 METHODS
156  
157 =head2 TO_JSON
158  
159 Returns a double.
160  
161 If the C<BSON_EXTJSON> environment variable is true and the
162 C<BSON_EXTJSON_RELAXED> environment variable is false, returns a hashref
163 compatible with
164 MongoDB's L<extended JSON|https://github.com/mongodb/specifications/blob/master/source/extended-json.rst>
165 format, which represents it as a document as follows:
166  
167 {"$numberDouble" : "42.0"}
168  
169 If C<BSON_EXTJSON> is false and the value is 'Inf', '-Inf' or 'NaN'
170 (which are illegal in regular JSON), then an exception is thrown.
171  
172 =for Pod::Coverage BUILD nInf pInf NaN
173  
174 =head1 INFINITY AND NAN
175  
176 Some Perls may not support converting "Inf" or "NaN" strings to their
177 double equivalent. They are available as functions from the L<POSIX>
178 module, but as a lighter alternative to POSIX, the following functions are
179 available:
180  
181 =over 4
182  
183 =item *
184  
185 BSON::Double::pInf() – positive infinity
186  
187 =item *
188  
189 BSON::Double::nInf() – negative infinity
190  
191 =item *
192  
193 BSON::Double::NaN() – not-a-number
194  
195 =back
196  
197 =head1 OVERLOADING
198  
199 The numification operator, C<0+> is overloaded to return the C<value>,
200 the full "minimal set" of overloaded operations is provided (per L<overload>
201 documentation) and fallback overloading is enabled.
202  
203 =head1 AUTHORS
204  
205 =over 4
206  
207 =item *
208  
209 David Golden <david@mongodb.com>
210  
211 =item *
212  
213 Stefan G. <minimalist@lavabit.com>
214  
215 =back
216  
217 =head1 COPYRIGHT AND LICENSE
218  
219 This software is Copyright (c) 2020 by Stefan G. and MongoDB, Inc.
220  
221 This is free software, licensed under:
222  
223 The Apache License, Version 2.0, January 2004
224  
225 =cut
226  
227 __END__
« BSON/Timestamp.pm ↑ Summary BSON/Int64.pm »