blib/lib/Logic/Relational/Domain.pm

filter: Statementstmt 90.0% 45 / 50 Branchbran 67.8% 19 / 28 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 90.9% 10 / 11 Podpod 0.0% 0 / 8 totaltotal 75.4% 77 / 102 CC 19 SCAR 17.9
CC
19
Cov
81%
new
10.8
intersect
6.0
as_string
6.0
CRAP 6.0
« lib/Logic/Relational/ArrayRest.pm ↑ Summary lib/Logic/Relational/State.pm »
1 package Logic::Relational::Domain;
2  
3 30 ▶use v5.38;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 30 ▶use experimental 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 30 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 =head1 NAME
8  
9 Logic::Relational::Domain - Represents a finite domain of allowed integer values.
10  
11 =cut
12  
13 101 ▶sub new ( $class, $args ) {
Subroutine new: called CC=10 SCAR=23.8
Pod undocumented
14 101 my %values;
15 101 ▶ if ( exists $args->{min} && exists $args->{max} ) {
Branch: 3 branches
BranchTrueFalseTotal
if (exists $args->{'min'} and exists $args->{'max'}) { } 22 79 101
elsif (exists $args->{'values'}) { } 2 77 79
elsif (ref $args eq 'HASH') { } 77 0 77
Condition66%2/3
exists $args->{"min"} and exists $args->{"max"}
!ll&&!rl&&r
79022
MC/DC50%1/2
exists $args->{"min"} and exists $args->{"max"}
exists $args->{"min"} exists $args->{"max"}
Truth table66%2/3
A and B
A= exists $args->{"min"}B= exists $args->{"max"}
ABresult
0X0
100
111
16 22 my $min = $args->{min};
17 22 my $max = $args->{max};
18 22 ▶ croak "Invalid range: min $min > max $max" if $min > $max;
Branch: 1 branch
BranchTrueFalseTotal
if $min > $max 0 22 22
19 22 for my $i ( $min .. $max ) {
20 86 $values{$i} = 1;
21 }
22 }
23 elsif ( exists $args->{values} ) {
24 2 my $vals = $args->{values};
25 2 ▶ croak "values must be an array reference" unless ref($vals) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $vals eq "ARRAY" 0 2 2
26 2 for my $v (@$vals) {
27 7 ▶ croak "Domain values must be integers: $v" unless $v =~ /^-?\d+$/x;
Branch: 1 branch
BranchTrueFalseTotal
unless $v =~ /^-?\d+$/ux 0 7 7
28 7 $values{$v} = 1;
29 }
30 }
31 elsif ( ref($args) eq 'HASH' ) {
32 77 %values = %$args;
33 }
34 else {
35 0 croak "Invalid arguments to Domain->new";
36 }
37  
38 101 return bless { values => \%values }, $class;
39 }
40  
41 187 ▶sub values ($self) {
Subroutine values: called CC=1 SCAR=0.0
Pod undocumented
42 187 my @sorted = sort { $a <=> $b } keys %{ $self->{values} };
43 187 return @sorted;
44 }
45  
46 352 ▶sub size ($self) {
Subroutine size: called CC=1 SCAR=0.0
Pod undocumented
47 352 return scalar keys %{ $self->{values} };
48 }
49  
50 121 ▶sub contains ( $self, $val ) {
Subroutine contains: called CC=2 SCAR=6.9
Pod undocumented
51 121 ▶ return exists $self->{values}{$val} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'values'}{$val} ? : 120 1 121
52 }
53  
54 132 ▶sub is_bound ($self) {
Subroutine is_bound: called CC=2 SCAR=6.9
Pod undocumented
55 132 ▶ return $self->size == 1 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->size == 1 ? : 66 66 132
56 }
57  
58 33 ▶sub bound_value ($self) {
Subroutine bound_value: called CC=2 SCAR=7.0
Pod undocumented
59 33 ▶ return unless $self->is_bound;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_bound 0 33 33
60 33 my ($val) = keys %{ $self->{values} };
61 33 return $val;
62 }
63  
64 3 ▶sub intersect ( $self, $other ) {
Subroutine intersect: called CC=6 SCAR=18.0
Pod undocumented
65 3 my %new_values;
66 3 ▶ if ( ref($other) eq 'Logic::Relational::Domain' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $other eq 'Logic::Relational::Domain') { } 2 1 3
67 2 for my $v ( keys %{ $self->{values} } ) {
68 8 ▶ if ( exists $other->{values}{$v} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $other->{"values"}{$v}) 4 4 8
69 4 $new_values{$v} = 1;
70 }
71 }
72 }
73 else {
74 1 ▶ if ( exists $self->{values}{$other} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"values"}{$other}) 1 0 1
75 1 $new_values{$other} = 1;
76 }
77 }
78  
79 3 ▶ return unless keys %new_values;
Branch: 1 branch
BranchTrueFalseTotal
unless keys %new_values 0 3 3
80 3 return ref($self)->new( \%new_values );
81 }
82  
83 0 ▶sub as_string ($self) {
Subroutine as_string: not called CC=2 SCAR=17.9
Pod undocumented
84 0 my @vals = $self->values;
85 0 ▶ if ( @vals > 5 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@vals > 5) 0 0 0
86 return
87 0 "domain("
88 . $vals[0] . ".."
89 . $vals[-1]
90 . ", size="
91 . $self->size . ")";
92 }
93 0 return "domain(" . join( ",", @vals ) . ")";
94 }
95  
96 30 1;
« lib/Logic/Relational/ArrayRest.pm ↑ Summary lib/Logic/Relational/State.pm »