blib/lib/Aion/Type/DNF.pm

filter: Statementstmt 91.7% 145 / 158 Branchbran 87.7% 86 / 98 Conditioncond 80.0% 12 / 15 MC/DCmcdc 60.0% 6 / 10 Subroutinesub 100% 16 / 16 Podpod 0.0% 0 / 2 totaltotal 88.6% 265 / 299 CC 59 SCAR 21.4
CC
59
Cov
90%
_pushing
17.1
_union_enums
14.6
_ranges_bag
8.0
CRAP 8.5
« blib/lib/Aion/Meta/RequiresFeature.pm ↑ Summary blib/lib/Aion.pm »
1 package Aion::Type::DNF;
2  
3 8 ▶use common::sense;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 2692 ▶sub Any() { goto &Aion::Type::Any }
Subroutine Any: called CC=1 SCAR=0.0
Pod undocumented
6 4113 ▶sub None() { goto &Aion::Type::None }
Subroutine None: called CC=1 SCAR=0.0
Pod undocumented
7  
8 # A as B as C <=> A & B & C
9 480 ▶sub _unfolding {
Subroutine _unfolding: called CC=6 SCAR=17.9
10 480 my ($self) = @_;
11
12 480 my @u;
13 480 ▶ for(my $i=$self; $i; $i = $i->{as}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i) 1528 243 1771
14 1528 ▶ unshift(@u, $i->clone(args => [map $_->_unfolding, @{$i->{args}}])), last if $i->is_set_theoretic;
Branch: 1 branch
BranchTrueFalseTotal
if $i->is_set_theoretic 237 1291 1528
15 1291 ▶ unshift @u, $i if $i->{test} != \&Aion::Type::true;
Branch: 1 branch
BranchTrueFalseTotal
if $i->{"test"} != \&Aion::Type::true 785 506 1291
16 }
17  
18 480 ▶ @u == 0? Any:
Branch: 2 branches
BranchTrueFalseTotal
@u == 0 ? : 2 478 480
@u == 1 ? : 212 266 478
19 @u == 1? $u[0]: Aion::Types::Intersection(\@u);
20 }
21  
22 # Проталкивание исключений к термам, заодно уменьшает размерность с приведением
23 1237 ▶sub _pushing {
Subroutine _pushing: called CC=17 SCAR=28.4
24 1237 my ($self) = @_;
25
26 1237 ▶ if($self->is_exclude) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_exclude) 468 769 1237
27 468 my $inner = $self->{args}[0];
28 # ~(~A) => A
29 468 ▶ return $inner->{args}[0]->_pushing if $inner->is_exclude;
Branch: 1 branch
BranchTrueFalseTotal
if $inner->is_exclude 1 467 468
30 # ~(A | B) => ~A & ~B
31 467 ▶ return _intersection(map { (~$_)->_pushing } @{$inner->{args}}) if $inner->is_union;
Branch: 1 branch
BranchTrueFalseTotal
if $inner->is_union 31 436 467
32 # ~(A & B) => ~A | ~B
33 436 ▶ return _union(map { (~$_)->_pushing } @{$inner->{args}}) if $inner->is_intersection;
Branch: 1 branch
BranchTrueFalseTotal
if $inner->is_intersection 120 316 436
34 # Range[A, B] => Range[-Inf, Invert[A]] | Range[Invert[B], Inf]
35 316 ▶ if($inner->is_range_type) {
Branch: 1 branch
BranchTrueFalseTotal
if ($inner->is_range_type) 25 291 316
36 25 my ($min, $max) = @{$inner->{args}};
37 25 ▶ if($inner->is_range) {
Branch: 1 branch
BranchTrueFalseTotal
if ($inner->is_range) 11 14 25
38 11 ▶ return None if $min == '-Inf' && $max == 'Inf';
Branch: 1 branch
BranchTrueFalseTotal
if $min == "-Inf" and $max == "Inf" 0 11 11
Condition33%1/3
$min == "-Inf" and $max == "Inf"
!ll&&!rl&&r
1100
MC/DC0%0/2
$min == "-Inf" and $max == "Inf"
$min == "-Inf" $max == "Inf"
Truth table33%1/3
A and B
A= $min == "-Inf"B= $max == "Inf"
ABresult
0X0
100
111
39 11 ▶ return $inner->clone(args => [Aion::Type::Lim->from($max)->inc, 'Inf']) if $min == '-Inf';
Branch: 1 branch
BranchTrueFalseTotal
if $min == "-Inf" 0 11 11
40 11 ▶ return $inner->clone(args => ['-Inf', Aion::Type::Lim->from($min)->dec]) if $max == 'Inf';
Branch: 1 branch
BranchTrueFalseTotal
if $max == "Inf" 4 7 11
41 7 return $inner->clone(args => ['-Inf', Aion::Type::Lim->from($min)->dec]) | $inner->clone(args => [Aion::Type::Lim->from($max)->inc, 'Inf']);
42 }
43
44 14 ▶ return None if $min == 0 && $max == 'Inf';
Branch: 1 branch
BranchTrueFalseTotal
if $min == 0 and $max == "Inf" 1 13 14
Condition100%3/3
$min == 0 and $max == "Inf"
!ll&&!rl&&r
581
MC/DC100%2/2
$min == 0 and $max == "Inf"
$min == 0 $max == "Inf"
Truth table100%3/3
A and B
A= $min == 0B= $max == "Inf"
ABresult
0X0
100
111
45 13 ▶ return $inner->clone(args => [$max+1, 'Inf']) if $min == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $min == 0 8 5 13
46 5 ▶ return $inner->clone(args => [0, $min-1]) if $max == 'Inf';
Branch: 1 branch
BranchTrueFalseTotal
if $max == "Inf" 1 4 5
47 4 return $inner->clone(args => [0, $min-1]) | $inner->clone(args => [$max+1, 'Inf']);
48 }
49 291 return $self;
50 }
51  
52 769 ▶ return _intersection(map $_->_pushing, @{$self->{args}}) if $self->is_intersection;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_intersection 247 522 769
53 522 ▶ return _union(map $_->_pushing, @{$self->{args}}) if $self->is_union;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_union 51 471 522
54  
55 471 $self
56 }
57  
58 # Сжимает в ДНФ
59 925 ▶sub _distribute {
Subroutine _distribute: called CC=4 SCAR=13.9
60 925 my ($self) = @_;
61  
62 # (A|B) & (C|D|E) & F => (A&C&F) | (A&D&F) | (A&E&F) | (B&C&F) | (B&D&F) | (B&E&F)
63 925 ▶ if($self->is_intersection) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_intersection) 129 796 925
64 129 ▶ my @disjuncts = map { my $x = $_->_distribute; $x->is_union? [@{$x->{args}}]: [$x] } @{$self->{args}};
Branch: 1 branch
BranchTrueFalseTotal
$x->is_union ? : 126 336 462
65
66 462 ▶ my $dnf = List::Util::reduce {
Subroutine __ANON__: called CC=1 SCAR=0.0
67 462 [ map { my $p = $_; map { [@$p, $_] } @$b } @$a ]
68 129 } [[]], @disjuncts;
69
70 129 return _union(map _intersection(@$_), @$dnf);
71 }
72  
73 796 ▶ return _union(map $_->_distribute, @{$self->{args}}) if $self->is_union;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_union 126 670 796
74
75 670 $self
76 }
77  
78 # Объединение интервалов
79 51 ▶sub _union_ranges {
Subroutine _union_ranges: called CC=5 SCAR=16.3
80 51 my ($ranges) = @_;
81  
82 # Отсекаем пустые
83 51 my @ranges = grep $_->{args}[0] <= $_->{args}[1], @$ranges;
84  
85 # Сортируем в порядке возрастания нижней границы
86 51 (my $range, @ranges) = sort { $a->{args}[0] <=> $b->{args}[0] } @ranges;
87  
88 @ranges = map {
89 51 my ($min1, $max1) = @{$range->{args}};
90 22 my ($min2, $max2) = @{$_->{args}};
91 0 ▶ if($max1 > $min2) { $range = $range->clone(args => [$min1, List::Util::max($max1, $max2)]); () }
Branch: 1 branch
BranchTrueFalseTotal
if ($max1 > $min2) { } 0 22 22
92 22 else { my $arange = $range; $range = $_; $arange }
93 } @ranges;
94 51 push @ranges, $range;
95  
96 51 ▶ if(@ranges == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ranges == 1) 29 22 51
97 29 my ($min, $max) = @{$range->{args}};
98 29 ▶ return Any if $min == $range->range_lbound && $max == 'Inf';
Branch: 1 branch
BranchTrueFalseTotal
if $min == $range->range_lbound and $max == "Inf" 0 29 29
Condition66%2/3
$min == $range->range_lbound and $max == "Inf"
!ll&&!rl&&r
18110
MC/DC0%0/2
$min == $range->range_lbound and $max == "Inf"
$min == $range->range_lbound $max == "Inf"
Truth table66%2/3
A and B
A= $min == $range->range_lboundB= $max == "Inf"
ABresult
0X0
100
111
99 }
100  
101 @ranges
102 51 }
103  
104 # Обрабатывает пересечение границ однотипных диапазонов
105 1321 ▶sub _intersection_ranges($) {
Subroutine _intersection_ranges: called CC=4 SCAR=13.9
106 1321 my ($ranges) = @_;
107  
108 # Пустой диапазон - это None
109 1321 ▶ return None if 0 == grep $_->{args}[0] <= $_->{args}[1], @$ranges;
Branch: 1 branch
BranchTrueFalseTotal
if 0 == grep(($_->{"args"}[0] <= $_->{"args"}[1]), @$ranges) 0 1321 1321
110
111 # Сортируем в порядке возрастания нижней границы
112 1321 my ($range, @ranges) = sort { $a->{args}[0] <=> $b->{args}[0] } @$ranges;
113  
114 1321 for my $arange (@ranges) {
115 # Если хотя бы у одного нет пересечений – это None
116 79 my ($min1, $max1) = @{$range->{args}};
117 79 my ($min2, $max2) = @{$arange->{args}};
118 79 my $max = List::Util::min($max1, $max2);
119 79 ▶ return None if $min2 > $max;
Branch: 1 branch
BranchTrueFalseTotal
if $min2 > $max 54 25 79
120 25 $range = $range->clone(args => [$min2, $max]);
121 }
122  
123 $range
124 1267 }
125  
126 # Объединение перечислений
127 3 ▶sub _union_enums($,$) {
Subroutine _union_enums: called CC=5 SCAR=26.8
128 3 my ($enums, $exclude_enums) = @_;
129
130 3 my %enum = map {($_=>$_)} map @{$_->{args}}, @$enums;
131 3 ▶ return $enums->[0]->clone(args => [sort values %enum])->init unless @$exclude_enums;
Branch: 1 branch
BranchTrueFalseTotal
unless @$exclude_enums 3 0 3
132  
133 0 my $first_exclude_enum = shift(@$exclude_enums);
134 0 my %exclude_enum = map {($_=>$_)} @{$first_exclude_enum->{args}};
135 0 for my $exclude_enum (@$exclude_enums) {
136 0 delete @exclude_enum{grep { !($_ ~~ $exclude_enum->{args}) } keys %exclude_enum};
137 0 ▶ return Any unless keys %exclude_enum;
Branch: 1 branch
BranchTrueFalseTotal
unless keys %exclude_enum 0 0 0
138 }
139
140 0 delete @exclude_enum{keys %enum};
141  
142 0 ▶ return Any unless keys %exclude_enum;
Branch: 1 branch
BranchTrueFalseTotal
unless keys %exclude_enum 0 0 0
143  
144 0 ~$first_exclude_enum->clone(args => [sort values %exclude_enum])->init;
145 }
146  
147 # Пересечение перечислений
148 16 ▶sub _intersection_enums($,$) {
Subroutine _intersection_enums: called CC=5 SCAR=16.1
149 16 my ($enums, $exclude_enums) = @_;
150
151 16 my %exclude_enum = map {($_=>$_)} map @{$_->{args}}, @$exclude_enums;
152 16 ▶ return ~$exclude_enums->[0]->clone(args => [sort values %exclude_enum])->init unless @$enums;
Branch: 1 branch
BranchTrueFalseTotal
unless @$enums 2 14 16
153
154 14 my $first_enum = shift(@$enums);
155 14 my %enum = map {($_=>$_)} @{$first_enum->{args}};
156  
157 14 for my $enum (@$enums) {
158 3 delete @enum{grep { !($_ ~~ $enum->{args}) } keys %enum};
159 3 ▶ return None unless keys %enum;
Branch: 1 branch
BranchTrueFalseTotal
unless keys %enum 1 2 3
160 }
161  
162 13 delete @enum{keys %exclude_enum};
163  
164 13 ▶ return None unless keys %enum;
Branch: 1 branch
BranchTrueFalseTotal
unless keys %enum 8 5 13
165  
166 5 $first_enum->clone(args => [sort values %enum])->init;
167 }
168  
169 # Обрабатывает пересечение границ диапазонов
170 2752 ▶sub _ranges_bag(@) {
Subroutine _ranges_bag: called CC=8 SCAR=20.8
171 2752 my $ranges_fn = shift;
172 2752 my $enums_fn = shift;
173 2752 my %bag; my @any; my @enums; my @exclude_enums;
174 2752 for my $candidate (@_) {
175 18645 ▶ if($candidate->is_range_type) { push @{$bag{$candidate->_range_lbaund_addr}}, $candidate }
Branch: 3 branches
BranchTrueFalseTotal
if ($candidate->is_range_type) { } 1473 17172 18645
elsif ($candidate->is_enum) { } 23 17149 17172
elsif ($candidate->is_exclude and $candidate->{'args'}[0]->is_enum) { } 15 17134 17149
176 23 elsif($candidate->is_enum) { push @enums, $candidate }
177 15 ▶ elsif($candidate->is_exclude && $candidate->{args}[0]->is_enum) { push @exclude_enums, $candidate->{args}[0] }
Condition100%3/3
$candidate->is_exclude and $candidate->{"args"}[0]->is_enum
!ll&&!rl&&r
10733640115
MC/DC100%2/2
$candidate->is_exclude and $candidate->{"args"}[0]->is_enum
$candidate->is_exclude $candidate->{"args"}[0]->is_enum
Truth table100%3/3
A and B
A= $candidate->is_excludeB= $candidate->{"args"}[0]->is_enum
ABresult
0X0
100
111
178 17134 else { push @any, $candidate }
179 }
180
181 2752 ▶ return @any, @enums || @exclude_enums? $enums_fn->(\@enums, \@exclude_enums): (), map $ranges_fn->($_), values %bag;
Branch: 1 branch
BranchTrueFalseTotal
@enums || @exclude_enums ? : 19 2733 2752
Condition100%3/3
@enums or @exclude_enums
l!l&&r!l&&!r
1722733
MC/DC100%2/2
@enums or @exclude_enums
@enums @exclude_enums
Truth table100%3/3
A or B
A= @enumsB= @exclude_enums
ABresult
1X1
011
000
182 }
183  
184 # Создание пересечения с приведением
185 2326 ▶sub _intersection(@) {
Subroutine _intersection: called CC=6 SCAR=17.9
186 2326 ▶ my %x = map {($_->key => $_)} _ranges_bag \&_intersection_ranges, \&_intersection_enums, map { $_->is_intersection? @{$_->{args}}: $_ } @_;
Branch: 1 branch
BranchTrueFalseTotal
$_->is_intersection ? : 2748 6027 8775
187 # ~Any & A = ~Any
188 2326 ▶ return None if exists $x{None->key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $x{&None()->key} 63 2263 2326
189 # Any & A = A
190 2263 delete $x{Any->key};
191 # Intersection[A] = A
192 2263 ▶ return (values %x)[0] if 1 == keys %x;
Branch: 1 branch
BranchTrueFalseTotal
if 1 == keys %x 33 2230 2263
193 # Intersection[] = Any
194 2230 ▶ return Any if 0 == keys %x;
Branch: 1 branch
BranchTrueFalseTotal
if 0 == keys %x 1 2229 2230
195 # A & ~A = ~Any
196 2229 ▶ return None if List::Util::first { $_->is_exclude && exists $x{$_->{args}[0]->key} } values %x;
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 2 branches
BranchTrueFalseTotal
if &List::Util::first(sub { $_->is_exclude and exists $x{$_->{"args"}[0]->key}; } , values %x) 1213 1016 2229
$_->is_exclude and exists $x{$_->{"args"}[0]->key} 3032 4415 7447
197 1016 Aion::Types::Intersection([values %x]);
198 }
199  
200 # Создание объединения с приведением
201 426 ▶sub _union(@) {
Subroutine _union: called CC=6 SCAR=18.0
202 426 ▶ my %x = map {($_->key => $_)} _ranges_bag \&_union_ranges, \&_union_enums, map { $_->is_union? @{$_->{args}}: $_ } @_;
Branch: 1 branch
BranchTrueFalseTotal
$_->is_union ? : 78 2828 2906
203 # Any | A = Any
204 426 ▶ return Any if exists $x{Any->key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $x{&Any()->key} 0 426 426
205 # ~Any | A = A
206 426 delete $x{None->key};
207 # Union[A] = A
208 426 ▶ return (values %x)[0] if 1 == keys %x;
Branch: 1 branch
BranchTrueFalseTotal
if 1 == keys %x 58 368 426
209 # Union[] = None
210 368 ▶ return None if 0 == keys %x;
Branch: 1 branch
BranchTrueFalseTotal
if 0 == keys %x 21 347 368
211 # A | ~A = Any
212 347 ▶ return Any if List::Util::first { $_->is_exclude && exists $x{$_->{args}[0]->key} } values %x;
Subroutine __ANON__: called CC=2 SCAR=7.0
Branch: 2 branches
BranchTrueFalseTotal
if &List::Util::first(sub { $_->is_exclude and exists $x{$_->{"args"}[0]->key}; } , values %x) 0 347 347
$_->is_exclude and exists $x{$_->{"args"}[0]->key} 645 1257 1902
213 347 Aion::Types::Union([values %x]);
214 }
215  
216 8 1;
217  
218 __END__
« blib/lib/Aion/Meta/RequiresFeature.pm ↑ Summary blib/lib/Aion.pm »