blib/lib/Term/Fabulous/Chart/Scale/Log.pm

filter: Statementstmt 100% 116 / 116 Branchbran 92.1% 35 / 38 Conditioncond 75.5% 34 / 45 MC/DCmcdc 56.0% 14 / 25 Subroutinesub 100% 20 / 20 Podpod 80.0% 4 / 5 totaltotal 89.5% 223 / 249 CC 37 SCAR 30.5
CC
37
Cov
93%
fit
34.8
position
3.0
_label
3.0
CRAP 21.1
« blib/lib/Term/Fabulous/Widget/Table/Style.pm ↑ Summary blib/lib/Term/Fabulous/Render/Attr.pm »
1 package Term::Fabulous::Chart::Scale::Log;
2  
3 1 ▶use v5.32;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use feature 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶no warnings 'experimental::signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 our $VERSION = '0.05';
9  
10 1 ▶use Object::Pad 0.825;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 ▶use Term::Fabulous::Chart::Scale;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 class Term::Fabulous::Chart::Scale::Log :isa(Term::Fabulous::Chart::Scale) {
15 1 ▶ use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶ use List::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶ use POSIX qw(ceil floor);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶ use Term::Fabulous::Chart::Format qw(number_formatter format_value);
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 1 ▶ use constant EPSILON => 1e-9;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 1 ▶ field $base :param :reader;
Subroutine base: called CC=1 SCAR=0.0
Pod undocumented
23  
24 1 ▶ method kind () { return 'log' }
Subroutine kind: called CC=1 SCAR=0.0
Pod documented
25  
26 86 ▶ method _log ($value) {
Subroutine _log: called CC=1 SCAR=0.0
27 86 return log($value) / log($base);
28 1 }
29  
30 32 ▶ method position ($value) {
Subroutine position: called CC=3 SCAR=11.0
Pod documented
31 32 ▶ return undef unless defined $value && $value > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value and $value > 0 4 28 32
Condition100%3/3
defined $value and $value > 0
!ll&&!rl&&r
1328
MC/DC100%2/2
defined $value and $value > 0
defined $value $value > 0
Truth table100%3/3
A and B
A= defined $valueB= $value > 0
ABresult
0X0
100
111
32 28 my ( $low, $high ) = map { $self->_log($_) } $self->min, $self->max;
33 28 return ( $self->_log($value) - $low ) / ( $high - $low );
34 1 }
35  
36 1 ▶ method value_at ($position) {
Subroutine value_at: called CC=1 SCAR=0.0
Pod documented
37 1 my ( $low, $high ) = map { $self->_log($_) } $self->min, $self->max;
38 1 return $base**( $low + $position * ( $high - $low ) );
39 1 }
40  
41 13 ▶ method fit :common (%options) {
Subroutine fit: called CC=33 SCAR=35.5
Pod documented
42 13 ▶ my $cells = $options{cells} // croak "$class: fit needs cells";
Condition50%1/2
$options{"cells"} // croak("${class}: fit needs cells")
l!l
130
MC/DC0%0/1
$options{"cells"} // croak("${class}: fit needs cells")
$options{"cells"}
Truth table50%1/2
A
A= $options{"cells"}
Aresult
11
00
43 13 ▶ croak "$class: cells must be a positive integer, got $cells" unless $cells =~ /\A[1-9][0-9]*\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $cells =~ /\A[1-9][0-9]*\z/u 0 13 13
44 13 ▶ my $base = $options{base} // 10;
Condition100%2/2
$options{"base"} // 10
l!l
211
MC/DC100%1/1
$options{"base"} // 10
$options{"base"}
Truth table100%2/2
A
A= $options{"base"}
Aresult
11
00
45 13 ▶ croak "$class: base must be a number greater than 1, got $base" unless $base > 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $base > 1 1 12 13
46 12 my ( $fixed_low, $fixed_high ) = @options{qw(min max)};
47 12 foreach my $end ( [ min => $fixed_low ], [ max => $fixed_high ] ) {
48 23 ▶ croak "$class: $end->[0] of a logarithmic axis must be greater than 0, got $end->[1]" if defined $end->[1] && $end->[1] <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if defined $end->[1] and $end->[1] <= 0 1 22 23
Condition100%3/3
defined $end->[1] and $end->[1] <= 0
!ll&&!rl&&r
1751
MC/DC100%2/2
defined $end->[1] and $end->[1] <= 0
defined $end->[1] $end->[1] <= 0
Truth table100%3/3
A and B
A= defined $end->[1]B= $end->[1] <= 0
ABresult
0X0
100
111
49 }
50 11 ▶ croak "$class: min ($fixed_low) must be less than max ($fixed_high)" if defined $fixed_low && defined $fixed_high && $fixed_low >= $fixed_high;
Branch: 1 branch
BranchTrueFalseTotal
if defined $fixed_low and defined $fixed_high and $fixed_low >= $fixed_high 1 10 11
Condition83%5/6
defined $fixed_low and defined $fixed_high and $fixed_low >= $fixed_high
!ll&&!rl&&r
1001
defined $fixed_low and defined $fixed_high
!ll&&!rl&&r
821
MC/DC66%2/3
defined $fixed_low and defined $fixed_high and $fixed_low >= $fixed_high
defined $fixed_low defined $fixed_high $fixed_low >= $fixed_high
Truth table75%3/4
A and B and C
A= defined $fixed_lowB= defined $fixed_highC= $fixed_low >= $fixed_high
ABCresult
0XX0
10X0
1100
1111
51 10 ▶ my $vertical = ( $options{orientation} // 'vertical' ) eq 'vertical';
Condition100%2/2
$options{"orientation"} // "vertical"
l!l
19
MC/DC100%1/1
$options{"orientation"} // "vertical"
$options{"orientation"}
Truth table100%2/2
A
A= $options{"orientation"}
Aresult
11
00
52 10 ▶ my $logarithm = sub ($value) { log($value) / log($base) };
Subroutine __ANON__: called CC=1 SCAR=0.0
53  
54 10 ▶ my ( $low, $high ) = $options{extent} ? $options{extent}->@* : ( 1, $base );
Branch: 1 branch
BranchTrueFalseTotal
$options{'extent'} ? : 10 0 10
55 10 ▶ $low = $fixed_low if defined $fixed_low;
Branch: 1 branch
BranchTrueFalseTotal
if defined $fixed_low 2 8 10
56 10 ▶ $high = $fixed_high if defined $fixed_high;
Branch: 1 branch
BranchTrueFalseTotal
if defined $fixed_high 1 9 10
57 10 ▶ ( $low, $high ) = defined $fixed_high && !defined $fixed_low ? ( $high / $base, $high ) : ( $low, $low * $base ) if $high <= $low; # a fixed end stays
Branch: 2 branches
BranchTrueFalseTotal
if $high <= $low 3 7 10
defined $fixed_high && !defined($fixed_low) ? : 1 2 3
Condition66%2/3
defined $fixed_high and not defined $fixed_low
!ll&&!rl&&r
201
MC/DC50%1/2
defined $fixed_high and not defined $fixed_low
defined $fixed_high not defined $fixed_low
Truth table66%2/3
A and not(B)
A= defined $fixed_highB= not defined $fixed_low
ABresult
0X0
100
111
58  
59 10 ▶ my $first = defined $fixed_low ? $logarithm->($low) : floor( $logarithm->($low) + EPSILON );
Branch: 1 branch
BranchTrueFalseTotal
defined $fixed_low ? : 2 8 10
60 10 ▶ my $last = defined $fixed_high ? $logarithm->($high) : ceil( $logarithm->($high) - EPSILON );
Branch: 1 branch
BranchTrueFalseTotal
defined $fixed_high ? : 1 9 10
61 10 ▶ $last = $first + 1 if $last <= $first;
Branch: 1 branch
BranchTrueFalseTotal
if $last <= $first 0 10 10
62 10 my ( $domain_low, $domain_high ) = ( $base**$first, $base**$last );
63  
64 10 ▶ my @powers = grep { $_ >= $first - EPSILON && $_ <= $last + EPSILON } ceil( $first - EPSILON ) .. floor( $last + EPSILON );
Condition33%1/3
$_ >= $first - 1e-09 && $_ <= $last + 1e-09
!ll&&!rl&&r
0040
MC/DC0%0/2
$_ >= $first - 1e-09 && $_ <= $last + 1e-09
$_ >= $first - 1e-09 $_ <= $last + 1e-09
Truth table33%1/3
A && B
A= $_ >= $first - 1e-09B= $_ <= $last + 1e-09
ABresult
0X0
100
111
65 10 my $decades = @powers - 1;
66 10 ▶ my $spans_domain = @powers && abs( $powers[0] - $first ) < EPSILON && abs( $powers[-1] - $last ) < EPSILON && $decades > 0;
Condition77%7/9
@powers && abs $powers[0] - $first < 1e-09 && abs $powers[-1] - $last < 1e-09 && $decades > 0
!ll&&!rl&&r
307
@powers and abs $powers[0] - $first < "1e-09" and abs $powers[-1] - $last < "1e-09"
!ll&&!rl&&r
217
@powers and abs $powers[0] - $first < "1e-09"
!ll&&!rl&&r
028
MC/DC50%2/4
@powers && abs $powers[0] - $first < 1e-09 && abs $powers[-1] - $last < 1e-09 && $decades > 0
@powers abs $powers[0] - $first < "1e-09" abs $powers[-1] - $last < "1e-09" $decades > 0
Truth table60%3/5
A and B and C && D
A= @powersB= abs $powers[0] - $first < "1e-09"C= abs $powers[-1] - $last < "1e-09"D= $decades > 0
ABCDresult
0XXX0
10XX0
110X0
11100
11111
67 10 ▶ my $per_decade = $spans_domain ? floor( ( $cells - 1 ) / $decades ) : 0;
Branch: 1 branch
BranchTrueFalseTotal
$spans_domain ? : 7 3 10
68 10 ▶ my $used = $spans_domain && $per_decade >= 1 ? $decades * $per_decade + 1 : $cells;
Branch: 1 branch
BranchTrueFalseTotal
$spans_domain && $per_decade >= 1 ? : 6 4 10
Condition100%3/3
$spans_domain and $per_decade >= 1
!ll&&!rl&&r
316
MC/DC100%2/2
$spans_domain and $per_decade >= 1
$spans_domain $per_decade >= 1
Truth table100%3/3
A and B
A= $spans_domainB= $per_decade >= 1
ABresult
0X0
100
111
69  
70 # Label every power when there is room, else every second or third.
71 10 my $format = $options{format};
72 10 ▶ my $measure = $options{measure} // sub ($label) { length $label };
Subroutine __ANON__: called CC=1 SCAR=0.6
Condition33%1/3
$options{"measure"} // sub ($label) { length $label; }
l!l&&r!l&&!r
0100
MC/DC0%0/2
$options{"measure"} // sub ($label) { length $label; }
$options{"measure"} sub ($label) { length $label; }
Truth table33%1/3
A // B
A= $options{"measure"}B= sub ($label) { length $label; }
ABresult
1X1
011
000
73 10 my @values = map { $base**$_ } @powers;
74 10 my @labels = map { _label( $format, $_ ) } @values;
75 10 ▶ my $room = $vertical ? 2 : 2 + List::Util::max( 1, map { $measure->($_) } @labels );
Branch: 1 branch
BranchTrueFalseTotal
$vertical ? : 9 1 10
76 10 my $stride = 1;
77 10 ▶ $stride++ while @powers > 1 && ( $cells - 1 ) * $stride / ( $last - $first ) < $room && $stride < @powers;
Branch: 1 branch
BranchTrueFalseTotal
@powers > 1 and ($cells - 1) * $stride / ($last - $first) < $room and $stride < @powers and do { ++$stride } 4 10 14
Condition66%4/6
@powers > 1 and ($cells - 1) * $stride / ($last - $first) < $room and $stride < @powers
!ll&&!rl&&r
1004
@powers > 1 and ($cells - 1) * $stride / ($last - $first) < $room
!ll&&!rl&&r
0104
MC/DC33%1/3
@powers > 1 and ($cells - 1) * $stride / ($last - $first) < $room and $stride < @powers
@powers > 1 ($cells - 1) * $stride / ($last - $first) < $room $stride < @powers
Truth table50%2/4
A and B and C
A= @powers > 1B= ($cells - 1) * $stride / ($last - $first) < $roomC= $stride < @powers
ABCresult
0XX0
10X0
1100
1111
78 10 my @ticks = map { { value => $values[$_], label => $labels[$_] } } grep { ( $powers[$_] - $powers[0] ) % $stride == 0 } 0 .. $#powers;
79  
80 10 return $class->new( min => $domain_low, max => $domain_high, cells => $cells, used => $used, base => $base, ticks => \@ticks );
81 1 }
82  
83 # Powers from 1 up are written with SI prefixes (1k, 10k), smaller
84 # ones as decimals (0.01).
85 40 ▶ sub _label ( $format, $value ) {
Subroutine _label: called CC=3 SCAR=11.0
86 40 ▶ return number_formatter( $format, $value, $value )->($value) if defined $format;
Branch: 1 branch
BranchTrueFalseTotal
if defined $format 4 36 40
87 36 ▶ return format_value( $value, 'si' ) if $value >= 1;
Branch: 1 branch
BranchTrueFalseTotal
if $value >= 1 33 3 36
88 3 my $text = sprintf '%.10f', $value;
89 3 $text =~ s/0+\z//;
90 3 return $text;
91 }
92 }
93  
94 1 1;
95  
96 __END__
« blib/lib/Term/Fabulous/Widget/Table/Style.pm ↑ Summary blib/lib/Term/Fabulous/Render/Attr.pm »