BSON/Int64.pm

filter: Statementstmt 64.5% 51 / 79 Branchbran 26.0% 12 / 46 Conditioncond 83.3% 10 / 12 MC/DCmcdc 75.0% 6 / 8 Subroutinesub 42.1% 16 / 38 Podpod 100% 1 / 1 totaltotal 52.1% 96 / 184 CC 28 SCAR 15.9
CC
28
Cov
53%
__ANON__
20.0
BEGIN
6.6
__ANON__
6.0
CRAP 4.9
« BSON/Double.pm ↑ Summary BSON/Int32.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::Int64;
6 # ABSTRACT: BSON type wrapper for Int64
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 71 ▶use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 71 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 #pod =attr value
16 #pod
17 #pod A numeric scalar. It will be coerced to an integer. The default is 0.
18 #pod
19 #pod =cut
20  
21 71 has 'value' => (
22 is => 'ro'
23 );
24  
25 71 ▶use if !$Config{use64bitint}, "Math::BigInt";
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 71 ▶use namespace::clean -except => 'meta';
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 # With long doubles or a 32-bit integer perl, we're able to directly check
30 # if a value exceeds the maximum bounds of an int64_t. On a 64-bit Perl
31 # with only regular doubles, the loss of precision for doubles makes an
32 # exact check against the negative boundary impossible from pure-Perl.
33 # (The positive boundary isn't an issue because Perl will upgrade
34 # internally to uint64_t to do the comparision). Fortunately, we can take
35 # advantage of a quirk in pack(), where a float that is in the ambiguous
36 # negative zone or that is too negative to fit will get packed like the
37 # smallest negative int64_t.
38  
39 71 ▶BEGIN {
Subroutine BEGIN: called CC=5 SCAR=18.9
40 71 ▶ my $max_int64 = $Config{use64bitint} ? 9223372036854775807 : Math::BigInt->new("9223372036854775807");
Branch: 1 branch
BranchTrueFalseTotal
$BSON::Int64::Config{'use64bitint'} ? : 71 0 71
41 71 ▶ my $min_int64 = $Config{use64bitint} ? -9223372036854775808 : Math::BigInt->new("-9223372036854775808");
Branch: 1 branch
BranchTrueFalseTotal
$BSON::Int64::Config{'use64bitint'} ? : 71 0 71
42 71 ▶ if ( $Config{nvsize} == 16 || ! $Config{use64bitint} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($BSON::Int64::Config{'nvsize'} == 16 or not $BSON::Int64::Config{'use64bitint'}) { } 0 71 71
Condition33%1/3
$BSON::Int64::Config{"nvsize"} == 16 or not $BSON::Int64::Config{"use64bitint"}
l!l&&r!l&&!r
0071
MC/DC0%0/2
$BSON::Int64::Config{"nvsize"} == 16 or not $BSON::Int64::Config{"use64bitint"}
$BSON::Int64::Config{"nvsize"} == 16 not $BSON::Int64::Config{"use64bitint"}
Truth table33%1/3
A or not(B)
A= $BSON::Int64::Config{"nvsize"} == 16B= not $BSON::Int64::Config{"use64bitint"}
ABresult
1X1
011
000
43 0 ▶ *BUILD = sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
44 0 my $self = shift;
45  
46 0 ▶ my $value = defined $self->{value} ? int($self->{value}) : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'value'} ? : 0 0 0
47  
48 0 ▶ if ( $value > $max_int64 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($value > $max_int64) { } 0 0 0
elsif ($value < $min_int64) { } 0 0 0
49 0 $value = $max_int64;
50 }
51 elsif ( $value < $min_int64 ) {
52 0 $value = $min_int64;
53 }
54  
55 0 return $self->{value} = $value;
56 }
57 0 }
58 else {
59 71 my $packed_min_int64 = pack("q<", $min_int64);
60 67 ▶ *BUILD = sub {
Subroutine __ANON__: called CC=6 SCAR=17.9
61 67 my $self = shift;
62  
63 67 ▶ my $value = defined $self->{value} ? int($self->{value}) : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'value'} ? : 65 2 67
64  
65 67 ▶ if ( $value >= 0 && $value > $max_int64 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($value >= 0 and $value > $max_int64) { } 4 63 67
elsif ($value < 0 and pack('q<', $value) eq $packed_min_int64) { } 9 98 107
Condition100%3/3
$value >= 0 and $value > $max_int64
!ll&&!rl&&r
19444
MC/DC100%2/2
$value >= 0 and $value > $max_int64
$value >= 0 $value > $max_int64
Truth table100%3/3
A and B
A= $value >= 0B= $value > $max_int64
ABresult
0X0
100
111
66 4 $value = $max_int64;
67 }
68 ▶ elsif ( $value < 0 && pack("q<", $value) eq $packed_min_int64 ) {
Condition100%3/3
$value < 0 and pack("q<", $value) eq $packed_min_int64
!ll&&!rl&&r
44109
MC/DC100%2/2
$value < 0 and pack("q<", $value) eq $packed_min_int64
$value < 0 pack("q<", $value) eq $packed_min_int64
Truth table100%3/3
A and B
A= $value < 0B= pack("q<", $value) eq $packed_min_int64
ABresult
0X0
100
111
69 9 $value = $min_int64;
70 }
71  
72 67 return $self->{value} = $value;
73 }
74 71 }
75 }
76  
77 #pod =method TO_JSON
78 #pod
79 #pod On a 64-bit perl, returns the value as an integer. On a 32-bit Perl, it
80 #pod will be returned as a Math::BigInt object, which will
81 #pod fail to serialize unless a C<TO_JSON> method is defined
82 #pod for that or in package C<universal>.
83 #pod
84 #pod If the C<BSON_EXTJSON> environment variable is true and the
85 #pod C<BSON_EXTJSON_RELAXED> environment variable is false, returns a hashref
86 #pod compatible with
87 #pod MongoDB's L<extended JSON|https://github.com/mongodb/specifications/blob/master/source/extended-json.rst>
88 #pod format, which represents it as a document as follows:
89 #pod
90 #pod {"$numberLong" : "223372036854775807"}
91 #pod
92 #pod =cut
93  
94 21 ▶sub TO_JSON {
Subroutine TO_JSON: called CC=3 SCAR=11.0
Pod documented
95 21 ▶ return int($_[0]->{value}) if ! $ENV{BSON_EXTJSON} || $ENV{BSON_EXTJSON_RELAXED};
Branch: 1 branch
BranchTrueFalseTotal
if not $ENV{"BSON_EXTJSON"} or $ENV{"BSON_EXTJSON_RELAXED"} 7 14 21
Condition100%3/3
not $ENV{"BSON_EXTJSON"} or $ENV{"BSON_EXTJSON_RELAXED"}
l!l&&r!l&&!r
2514
MC/DC100%2/2
not $ENV{"BSON_EXTJSON"} or $ENV{"BSON_EXTJSON_RELAXED"}
not $ENV{"BSON_EXTJSON"} $ENV{"BSON_EXTJSON_RELAXED"}
Truth table100%3/3
not(A) or B
A= not $ENV{"BSON_EXTJSON"}B= $ENV{"BSON_EXTJSON_RELAXED"}
ABresult
1X1
011
000
96 14 return { '$numberLong' => "$_[0]->{value}" };
97 }
98  
99 71 ▶use overload (
Subroutine BEGIN: called CC=1 SCAR=4.4
100 # Unary
101 0 ▶ q{""} => sub { "$_[0]->{value}" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
102 30 ▶ q{0+} => sub { $_[0]->{value} },
Subroutine __ANON__: called CC=1 SCAR=0.0
103 0 ▶ q{~} => sub { ~( $_[0]->{value} ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
104 # Binary
105 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
106 (
107 map {
108 0 ▶ $_ => eval ## no critic
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__: 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 0 0
$_[2] ? : 0 0 0
$_[2] ? : 0 0 0
$_[2] ? : 0 0 0
$_[2] ? : 0 0 0
$_[2] ? : 0 0 0
$_[2] ? : 0 32 32
$_[2] ? : 0 0 0
$_[2] ? : 0 0 0
$_[2] ? : 0 0 0
$_[2] ? : 0 0 0
109 "sub { return \$_[2] ? \$_[1] $_ \$_[0]->{value} : \$_[0]->{value} $_ \$_[1] }"
110 } qw( - / % ** << >> x <=> cmp & | ^ )
111 ),
112 (
113 0 ▶ map { $_ => eval "sub { return $_(\$_[0]->{value}) }" } ## no critic
Subroutine __ANON__: not called CC=1 SCAR=3.1
Subroutine __ANON__: not called CC=1 SCAR=3.1
Subroutine __ANON__: not called CC=1 SCAR=3.1
Subroutine __ANON__: not called CC=1 SCAR=3.1
Subroutine __ANON__: not called CC=1 SCAR=3.1
Subroutine __ANON__: called CC=1 SCAR=3.1
114 qw( cos sin exp log sqrt int )
115 ),
116 0 ▶ q{atan2} => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
117 0 ▶ return $_[2] ? atan2( $_[1], $_[0]->{value} ) : atan2( $_[0]->{value}, $_[1] );
Branch: 1 branch
BranchTrueFalseTotal
$_[2] ? : 0 0 0
118 },
119  
120 # Special
121 71 fallback => 1,
122 71 );
123  
124 71 1;
125  
126 =pod
127  
128 =encoding UTF-8
129  
130 =head1 NAME
131  
132 BSON::Int64 - BSON type wrapper for Int64
133  
134 =head1 VERSION
135  
136 version v1.12.2
137  
138 =head1 SYNOPSIS
139  
140 use BSON::Types ':all';
141  
142 bson_int64( $number );
143  
144 =head1 DESCRIPTION
145  
146 This module provides a BSON type wrapper for a numeric value that
147 would be represented in BSON as a 64-bit integer.
148  
149 If the value won't fit in a 64-bit integer, an error will be thrown.
150  
151 On a Perl without 64-bit integer support, the value must be a
152 L<Math::BigInt> object.
153  
154 =head1 ATTRIBUTES
155  
156 =head2 value
157  
158 A numeric scalar. It will be coerced to an integer. The default is 0.
159  
160 =head1 METHODS
161  
162 =head2 TO_JSON
163  
164 On a 64-bit perl, returns the value as an integer. On a 32-bit Perl, it
165 will be returned as a Math::BigInt object, which will
166 fail to serialize unless a C<TO_JSON> method is defined
167 for that or in package C<universal>.
168  
169 If the C<BSON_EXTJSON> environment variable is true and the
170 C<BSON_EXTJSON_RELAXED> environment variable is false, returns a hashref
171 compatible with
172 MongoDB's L<extended JSON|https://github.com/mongodb/specifications/blob/master/source/extended-json.rst>
173 format, which represents it as a document as follows:
174  
175 {"$numberLong" : "223372036854775807"}
176  
177 =for Pod::Coverage BUILD
178  
179 =head1 OVERLOADING
180  
181 The numification operator, C<0+> is overloaded to return the C<value>,
182 the full "minimal set" of overloaded operations is provided (per L<overload>
183 documentation) and fallback overloading is enabled.
184  
185 =head1 AUTHORS
186  
187 =over 4
188  
189 =item *
190  
191 David Golden <david@mongodb.com>
192  
193 =item *
194  
195 Stefan G. <minimalist@lavabit.com>
196  
197 =back
198  
199 =head1 COPYRIGHT AND LICENSE
200  
201 This software is Copyright (c) 2020 by Stefan G. and MongoDB, Inc.
202  
203 This is free software, licensed under:
204  
205 The Apache License, Version 2.0, January 2004
206  
207 =cut
208  
209 __END__
« BSON/Double.pm ↑ Summary BSON/Int32.pm »