blib/lib/Language/Farnsworth/Units.pm

filter: Statementstmt 98.7% 77 / 78 Branchbran 81.2% 13 / 16 Conditioncond 75.0% 9 / 12 MC/DCmcdc 25.0% 2 / 8 Subroutinesub 100% 17 / 17 Podpod 0.0% 0 / 9 totaltotal 84.2% 118 / 140 CC 15 SCAR 11.3
CC
15
Cov
93%
getprefix
6.1
hasprefix
5.0
addunit
3.1
CRAP 3.1
« blib/lib/Language/Farnsworth/Error.pm ↑ Summary blib/lib/Language/Farnsworth/Functions/StdMath.pm »
1 package Language::Farnsworth::Units;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 4 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Language::Farnsworth::Value::Pari;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use Math::Pari;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use Language::Farnsworth::Output;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use Language::Farnsworth::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 4 our $lock = 0;
13  
14 sub new
15 4 ▶{
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
16 #i should make a constructor that copies, but that'll come later
17 4 my $self = {units=>{1=>new Language::Farnsworth::Value::Pari(1)}, dimens=>{}}; #hack to make things work right
18 4 bless $self;
19 }
20  
21 sub addunit
22 8716 ▶{
Subroutine addunit: called CC=3 SCAR=11.2
Pod undocumented
23 8716 my $self = shift;
24 8716 my $name = shift;
25 8716 my $value = shift;
26  
27 8716 ▶ error("won't redefine existing units") if (exists($self->{units}{$name}) && $lock);
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"units"}{$name} and $Language::Farnsworth::Units::lock 0 8716 8716
Condition66%2/3
exists $self->{"units"}{$name} and $Language::Farnsworth::Units::lock
!ll&&!rl&&r
85401760
MC/DC0%0/2
exists $self->{"units"}{$name} and $Language::Farnsworth::Units::lock
exists $self->{"units"}{$name} $Language::Farnsworth::Units::lock
Truth table66%2/3
A and B
A= exists $self->{"units"}{$name}B= $Language::Farnsworth::Units::lock
ABresult
0X0
100
111
28 8716 $self->{units}{$name} = $value;
29 8716 $self->{units}{$name."s"} = $value; #HACK! #causes issues with ms not meaning milliseconds, need to change lookup code!
30 }
31  
32 sub getunit
33 11534 ▶{
Subroutine getunit: called CC=3 SCAR=11.0
Pod undocumented
34 11534 my $self = shift;
35 11534 my $name = shift;
36  
37 11534 my $return;
38  
39 11534 ▶ if ($self->_isunit($name))
Branch: 2 branches
BranchTrueFalseTotal
if ($self->_isunit($name)) { } 10162 1372 11534
elsif ($self->hasprefix($name)) { } 1372 0 1372
40 {
41 10162 $return = $self->{units}{$name};
42 }
43 elsif ($self->hasprefix($name))
44 {
45 1372 my ($preval, undef, $realname) = $self->getprefix($name);
46 # print "GETTING PREFIXES: $name :: $preval :: $realname ::".Dumper($preval, $realname) if (($name eq "mg") || ($name eq "l") || $name eq "milli");
47  
48 1372 $return = $preval * $self->{units}{$realname};
49 }
50  
51 # print "GETTING UNIT: $name : $return : ".Dumper($return) if (($name eq "mg") || ($name eq "l") || $name eq "milli");
52 11534 return $return;
53 }
54  
55 sub hasprefix
56 12906 ▶{
Subroutine hasprefix: called CC=5 SCAR=16.1
Pod undocumented
57 12906 my $self = shift;
58 12906 my $name = shift;
59  
60 #sort them by length, solves issues with longer ones not being found first
61 12906 my @keys = keys %{$self->{prefix}};
62 12906 for my $pre (sort {length($b) <=> length($a)} @keys)
63 {
64 717726 ▶ if ($name =~ /^\Q$pre\E(.*)$/)
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /^\Q$pre\E(.*)$/) 7871 709855 717726
65 {
66 7871 ▶ return 1 if ($self->_isunit($1) || !length($1));
Branch: 1 branch
BranchTrueFalseTotal
if $self->_isunit($1) or not length $1 4233 3638 7871
Condition100%3/3
$self->_isunit($1) or not length $1
l!l&&r!l&&!r
34168173638
MC/DC100%2/2
$self->_isunit($1) or not length $1
$self->_isunit($1) not length $1
Truth table100%3/3
A or not(B)
A= $self->_isunit($1)B= not length $1
ABresult
1X1
011
000
67 }
68 }
69 8673 return 0; #no prefix!
70 }
71  
72 sub getprefix
73 1372 ▶{
Subroutine getprefix: called CC=6 SCAR=18.2
Pod undocumented
74 1372 my $self = shift;
75 1372 my $name = shift;
76  
77 #sort them by length, solves issues with longer ones not being found first
78 1372 for my $pre (sort {length($b) <=> length($a)} keys %{$self->{prefix}})
79 {
80 #print "CHECKING PREFIX: $pre\n" if ($name eq "mg");
81 60259 ▶ if ($name =~ /^\Q$pre\E(.*)$/)
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /^\Q$pre\E(.*)$/) 1372 58887 60259
82 {
83 1372 my $u = $1;
84 #print "FOUND: $name == $pre * $u\n";
85 #print Dumper($self->{prefix}{$pre}) if ($name eq "mg");
86 1372 ▶ $u = 1 unless length($1); #to make certain things work right
Branch: 1 branch
BranchTrueFalseTotal
unless length $1 152 1220 1372
87 1372 ▶ return ($self->{prefix}{$pre},$pre,$u) if ($self->_isunit($1) || !length($1));
Branch: 1 branch
BranchTrueFalseTotal
if $self->_isunit($1) or not length $1 1372 0 1372
Condition66%2/3
$self->_isunit($1) or not length $1
l!l&&r!l&&!r
12201520
MC/DC0%0/2
$self->_isunit($1) or not length $1
$self->_isunit($1) not length $1
Truth table66%2/3
A or not(B)
A= $self->_isunit($1)B= not length $1
ABresult
1X1
011
000
88 }
89 }
90 0 return undef; #to cause errors when not there
91 }
92  
93 sub isunit
94 11534 ▶{
Subroutine isunit: called CC=2 SCAR=7.0
Pod undocumented
95 11534 my $self = shift;
96 11534 my $name = shift;
97  
98 11534 ▶ return $self->hasprefix($name) || $self->_isunit($name);
Condition66%2/3
$self->hasprefix($name) || $self->_isunit($name)
l!l&&r!l&&!r
286186730
MC/DC0%0/2
$self->hasprefix($name) || $self->_isunit($name)
$self->hasprefix($name) $self->_isunit($name)
Truth table66%2/3
A || B
A= $self->hasprefix($name)B= $self->_isunit($name)
ABresult
1X1
011
000
99 }
100  
101 sub _isunit
102 29450 ▶{
Subroutine _isunit: called CC=1 SCAR=0.0
103 29450 my $self = shift;
104 29450 my $name = shift;
105 29450 return exists($self->{units}{$name});
106 }
107  
108 sub adddimen
109 36 ▶{
Subroutine adddimen: called CC=1 SCAR=0.0
Pod undocumented
110 36 my $self = shift;
111 36 my $name = shift;
112 36 my $default = shift; #primitive unit for the dimension, all other units are defined against this
113 36 my $val = new Language::Farnsworth::Value::Pari(1, {$name => 1}); #i think this is right
114 36 Language::Farnsworth::Output::addcombo($name,$val);
115 36 $self->{dimens}{$name} = $default;
116 36 $self->addunit($default, $val);
117 }
118  
119 #is this useful? yes, need it for display
120 sub getdimen
121 15 ▶{
Subroutine getdimen: called CC=1 SCAR=0.0
Pod undocumented
122 15 my $self = shift;
123 15 my $name = shift;
124  
125 15 return $self->{dimens}{$name};
126 }
127  
128 sub setprefix
129 340 ▶{
Subroutine setprefix: called CC=1 SCAR=0.0
Pod undocumented
130 340 my $self = shift;
131 340 my $name = shift;
132 340 my $value = shift;
133  
134 #print "SETTING PREFIX: $name : $value\n" if ($name eq "m");
135 340 $self->{prefix}{$name} = $value;
136 }
137  
138 4 1;
« blib/lib/Language/Farnsworth/Error.pm ↑ Summary blib/lib/Language/Farnsworth/Functions/StdMath.pm »