RandGen/Condition.pm

filter: Statementstmt 71.6% 81 / 113 Branchbran 42.3% 33 / 78 Conditioncond 31.4% 17 / 54 MC/DCmcdc 10.0% 3 / 30 Subroutinesub 62.5% 15 / 24 Podpod 55.5% 10 / 18 totaltotal 50.1% 159 / 317 CC 59 SCAR 32.2
CC
59
Cov
53%
quant
110.0
__ANON__
42.0
new
20.4
CRAP 25.1
« RandGen/CharClass.pm ↑ Summary RandGen/Subrule.pm »
1 # $Revision: #3 $$Date: 2005/08/31 $$Author: jd150722 $
2 ######################################################################
3 #
4 # This program is Copyright 2003-2005 by Jeff Dutton.
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of either the GNU General Public License or the
8 # Perl Artistic License.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # If you do not have a copy of the GNU General Public License write to
16 # the Free Software Foundation, Inc., 675 Mass Ave, Cambridge,
17 # MA 02139, USA.
18 ######################################################################
19  
20 package Parse::RandGen::Condition;
21  
22 4 require 5.006_001;
23 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 4 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 4 ▶use Parse::RandGen qw($Debug);
Subroutine BEGIN: called CC=1 SCAR=0.0
26 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 4 ▶use vars qw($Debug);
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 ######################################################################
30 #### Creators
31  
32 18 ▶sub new {
Subroutine new: called CC=17 SCAR=30.2
Pod documented
33 18 my $class = shift;
34 18 my $element = shift;
35 18 ▶ defined($element) or confess("%Error: Parse::RandGen::Condition::new() requires the a defined element argument as the first argument!\n");
Branch: 1 branch
BranchTrueFalseTotal
defined $element or confess("%Error: Parse::RandGen::Condition::new() requires the a defined element argument as the first argument!\n") 18 0 18
36  
37 18 my $self = {
38 _element => $element, # The element for the condition that must match
39 _min => undef, # The minimum number of times that the element must match for the condition to be true
40 _max => undef, # The maximum (inclusive) number of times that the element must match for the condition to be true
41 _greedy => undef, # By default, conditions are greedy (for pick()ing only, for parsing all conditions are greedy)
42 _production => undef, # The "parent" production that this belongs to...
43 };
44 18 ▶ my $type = ref($class)||$class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
0180
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
45 18 ▶ ($type eq "Parse::RandGen::Condition") and confess "%Error: Cannot call Parse::RandGen::Condition::new() directly! It is an abstract class!";
Branch: 1 branch
BranchTrueFalseTotal
$type eq "Parse::RandGen::Condition" and confess("%Error: Cannot call Parse::RandGen::Condition::new() directly! It is an abstract class!") 0 18 18
46 18 bless $self, $type;
47  
48 # Optional named arguments can be passed. Any unknown named arguments are turned into object data members.
49 18 my %args = (
50 # Optional
51 min => 1, # Min quantity
52 max => 1, # Max quantity
53 quant => undef, # Quantifier: [ + * ? ]
54 greedy => 1,
55 @_, # Arguments can override defaults or create new attributes in the object
56 );
57 18 ▶ if (defined($args{quant})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"quant"}) 6 12 18
58 6 my $quant = $args{quant};
59 6 ▶ ($args{min}, $args{max}) = (1, undef) if (($quant eq '+') || ($quant eq 's'));
Branch: 1 branch
BranchTrueFalseTotal
if $quant eq "+" or $quant eq "s" 3 3 6
Condition66%2/3
$quant eq "+" or $quant eq "s"
l!l&&r!l&&!r
303
MC/DC50%1/2
$quant eq "+" or $quant eq "s"
$quant eq "+" $quant eq "s"
Truth table66%2/3
A or B
A= $quant eq "+"B= $quant eq "s"
ABresult
1X1
011
000
60 6 ▶ ($args{min}, $args{max}) = (0, undef) if (($quant eq '*') || ($quant eq 's?'));
Branch: 1 branch
BranchTrueFalseTotal
if $quant eq "*" or $quant eq "s?" 0 6 6
Condition33%1/3
$quant eq "*" or $quant eq "s?"
l!l&&r!l&&!r
006
MC/DC0%0/2
$quant eq "*" or $quant eq "s?"
$quant eq "*" $quant eq "s?"
Truth table33%1/3
A or B
A= $quant eq "*"B= $quant eq "s?"
ABresult
1X1
011
000
61 6 ▶ ($args{min}, $args{max}) = (0, 1) if ($quant eq '?');
Branch: 1 branch
BranchTrueFalseTotal
if $quant eq "?" 0 6 6
62 6 ▶ if ($quant =~ m/\{(\d+)(,(\d*))?\}/) { # Support {n} , {n,} , and {n,m} formats
Branch: 1 branch
BranchTrueFalseTotal
if ($quant =~ /\{(\d+)(,(\d*))?\}/) 1 5 6
63 1 $args{min} = $1;
64 1 ▶ if (defined($2)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $2) { } 0 1 1
65 0 $args{max} = $3; # {n,} is (n,undef); {n,m} is (n,m)
66 } else {
67 1 $args{max} = $args{min}; # {n} is (n,n)
68 }
69 }
70 6 ▶ defined($args{min}) or confess("%Error: quant value of $quant is not understood!\n");
Branch: 1 branch
BranchTrueFalseTotal
defined $args{"min"} or confess("%Error: quant value of $quant is not understood!\n") 6 0 6
71 }
72 18 $self->{_min} = $args{min}; delete $args{min};
73 18 $self->{_max} = $args{max}; delete $args{max};
74 18 $self->{_greedy} = $args{greedy}; delete $args{greedy};
75 18 delete $args{quant};
76  
77 18 ▶ my ($min, $max) = ($self->min(), (defined($self->max()) ? $self->max() : "undef"));
Branch: 1 branch
BranchTrueFalseTotal
defined $self->max ? : 15 3 18
78 18 ▶ ($self->isQuantSupported() or $self->once()) or confess "%Error: new $type being created with a specified quantifier (min=$min and max=$max are not supported)!";
Branch: 1 branch
BranchTrueFalseTotal
$self->isQuantSupported or $self->once or confess("%Error: new $type being created with a specified quantifier (min=$min and max=$max are not supported)!") 18 0 18
Condition66%2/3
$self->isQuantSupported or $self->once
l!l&&r!l&&!r
6120
MC/DC0%0/2
$self->isQuantSupported or $self->once
$self->isQuantSupported $self->once
Truth table66%2/3
A or B
A= $self->isQuantSupportedB= $self->once
ABresult
1X1
011
000
79  
80 18 $self->_newDerived(\%args); # Derived class can pull out args that are custom/specific...
81  
82 # Delete named arguments, and copy any other values into the object (user-defined fields)
83 12 foreach my $userDefField (sort keys %args) {
84 0 $self->{$userDefField} = $args{$userDefField};
85 }
86  
87 12 return($self);
88 }
89  
90 ######################################################################
91 #### Methods
92  
93 #sub dump { } # Abstract Method
94  
95 1010 ▶sub dumpVal {
Subroutine dumpVal: called CC=3 SCAR=11.3
Pod undocumented
96 1010 ▶ my $self = shift or confess "%Error: Cannot call dumpVal() without a valid object!";
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or confess("%Error: Cannot call dumpVal() without a valid object!") 1010 0 1010
97 1010 my $val = shift;
98 1010 ▶ $val = "" unless defined($val);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 0 1010 1010
99 1010 my $d = Data::Dumper->new([$val])->Terse(1)->Indent(0)->Useqq(1);
100 1010 return($d->Dump());
101 }
102  
103 202 ▶sub pickRepetitions {
Subroutine pickRepetitions: called CC=13 SCAR=26.9
Pod undocumented
104 202 ▶ my $self = shift or confess "%Error: Cannot call pickRepetitions without a valid object!";
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or confess("%Error: Cannot call pickRepetitions without a valid object!") 202 0 202
105 202 my %args = @_;
106  
107 202 my ($corruptCnt, $corruptData) = (0, 0);
108 202 ▶ if (!$args{match} && !$self->zeroOrMore()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args{"match"} or $self->zeroOrMore) 50 152 202
Condition66%2/3
$args{"match"} or $self->zeroOrMore
l!l&&r!l&&!r
152050
MC/DC50%1/2
$args{"match"} or $self->zeroOrMore
$args{"match"} $self->zeroOrMore
Truth table66%2/3
A or B
A= $args{"match"}B= $self->zeroOrMore
ABresult
1X1
011
000
109 50 ▶ if (int(rand(2))) {
Branch: 1 branch
BranchTrueFalseTotal
if (int rand 2) { } 19 31 50
110 19 $corruptData = 1;
111 } else {
112 31 $corruptCnt = 1;
113 }
114 }
115  
116 202 my ($minCnt, $maxCnt);
117 202 ▶ if ($corruptCnt) {
Branch: 1 branch
BranchTrueFalseTotal
if ($corruptCnt) { } 31 171 202
118 31 ▶ if ((int(rand(2)) || !$self->max()) && $self->min()) {
Branch: 1 branch
BranchTrueFalseTotal
if (int rand 2 || !$self->max and $self->min) { } 15 16 31
Condition66%4/6
int rand 2 || !$self->max and $self->min
!ll&&!rl&&r
16015
int rand 2 or not $self->max
l!l&&r!l&&!r
15016
MC/DC33%1/3
int rand 2 || !$self->max and $self->min
int rand 2 not $self->max $self->min
Truth table40%2/5
A or not(B) and C
A= int rand 2B= not $self->maxC= $self->min
ABCresult
00X0
1X00
0100
1X11
0111
119 # Choose less than the minimum count (too few)
120 15 ($minCnt, $maxCnt) = (0, $self->min()-1);
121 } else {
122 # Choose more than the maximum count (too many)
123 16 ($minCnt, $maxCnt) = ($self->max()+1, $self->max()+4);
124 }
125 } else {
126 171 ▶ $minCnt = $self->min() || ($self->containsVals(%args) ? 1 : 0); # containsVals can only be true for SubRule
Branch: 1 branch
BranchTrueFalseTotal
$self->containsVals(%args) ? : 0 0 0
Condition33%1/3
$self->min || ($self->containsVals(%args) ? 1 : 0)
l!l&&r!l&&!r
17100
MC/DC0%0/2
$self->min || ($self->containsVals(%args) ? 1 : 0)
$self->min ($self->containsVals(%args) ? 1 : 0)
Truth table33%1/3
A || B
A= $self->minB= ($self->containsVals(%args) ? 1 : 0)
ABresult
1X1
011
000
127 171 ▶ $maxCnt = $self->max() || ($minCnt + (1<<int(rand(5))));
Condition33%1/3
$self->max || $minCnt + (1 << int(rand 5))
l!l&&r!l&&!r
17100
MC/DC0%0/2
$self->max || $minCnt + (1 << int(rand 5))
$self->max $minCnt + (1 << int(rand 5))
Truth table33%1/3
A || B
A= $self->maxB= $minCnt + (1 << int(rand 5))
ABresult
1X1
011
000
128 }
129  
130 202 my $matchCnt = $minCnt + int(rand($maxCnt-$minCnt+1));
131 202 ▶ my $badOne = $corruptData ? int(rand($matchCnt)) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$corruptData ? : 19 183 202
132  
133 202 return ( matchCnt => $matchCnt, badOne => $badOne );
134 }
135  
136 #sub pick { } # Abstract Method
137  
138 ######################################################################
139 #### Accessors
140  
141 3699 ▶sub element {
Subroutine element: called CC=2 SCAR=7.2
Pod documented
142 3699 ▶ my $self = shift or confess "%Error: Cannot call element() without a valid object!";
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or confess("%Error: Cannot call element() without a valid object!") 3699 0 3699
143 3699 return $self->{_element};
144 }
145  
146 0 ▶sub subrule { return undef; } # Default
Subroutine subrule: not called CC=1 SCAR=6.9
Pod documented
147 0 ▶sub isSubrule { return 0; } # Default
Subroutine isSubrule: not called CC=1 SCAR=6.9
Pod documented
148 0 ▶sub isTerminal { return 1; } # Default
Subroutine isTerminal: not called CC=1 SCAR=6.9
Pod documented
149 132 ▶sub isQuantSupported { return 0; } # Default (Regexp and Literal classes dont support)
Subroutine isQuantSupported: called CC=1 SCAR=0.0
Pod undocumented
150 1830 ▶sub containsVals { return 0; } # Default (only Subrule supports)
Subroutine containsVals: called CC=1 SCAR=0.0
Pod undocumented
151  
152 382 ▶sub min {
Subroutine min: called CC=2 SCAR=7.2
Pod documented
153 382 ▶ my $self = shift or confess "%Error: Cannot call min() without a valid object!";
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or confess("%Error: Cannot call min() without a valid object!") 382 0 382
154 382 return $self->{_min};
155 }
156  
157 276 ▶sub max {
Subroutine max: called CC=2 SCAR=7.2
Pod documented
158 276 ▶ my $self = shift or confess "%Error: Cannot call max() without a valid object!";
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or confess("%Error: Cannot call max() without a valid object!") 276 0 276
159 276 return $self->{_max};
160 }
161  
162 12 ▶sub once { # Returns true if the Condition must match exactly once
Subroutine once: called CC=4 SCAR=17.9
Pod undocumented
163 12 ▶ my $self = shift or confess "%Error: Cannot call once() without a valid object!";
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or confess("%Error: Cannot call once() without a valid object!") 12 0 12
164 12 ▶ return (defined($self->max()) && ($self->min() == 1) && ($self->max() == 1));
Condition33%2/6
defined $self->max && $self->min == 1 && $self->max == 1
!ll&&!rl&&r
0012
defined $self->max and $self->min == 1
!ll&&!rl&&r
0012
MC/DC0%0/3
defined $self->max && $self->min == 1 && $self->max == 1
defined $self->max $self->min == 1 $self->max == 1
Truth table25%1/4
A and B && C
A= defined $self->maxB= $self->min == 1C= $self->max == 1
ABCresult
0XX0
10X0
1100
1111
165 }
166  
167 151 ▶sub zeroOrMore { # Returns true if the Condition can match 0 or more times
Subroutine zeroOrMore: called CC=3 SCAR=13.1
Pod undocumented
168 151 ▶ my $self = shift or confess "%Error: Cannot call once() without a valid object!";
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or confess("%Error: Cannot call once() without a valid object!") 151 0 151
169 151 ▶ return (!$self->min() && !defined($self->max()));
Condition33%1/3
$self->min || defined $self->max
l!l&&r!l&&!r
15100
MC/DC0%0/2
$self->min || defined $self->max
$self->min defined $self->max
Truth table33%1/3
A || B
A= $self->minB= defined $self->max
ABresult
1X1
011
000
170 }
171  
172 0 ▶sub quant {
Subroutine quant: not called CC=10 SCAR=47.0
Pod undocumented
173 0 ▶ my $self = shift or confess "%Error: Cannot call once() without a valid object!";
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or confess("%Error: Cannot call once() without a valid object!") 0 0 0
174 0 ▶ my $ngreedy = $self->isGreedy() ? "" : "?";
Branch: 1 branch
BranchTrueFalseTotal
$self->isGreedy ? : 0 0 0
175 0 my $quant = "";
176 0 my @minmax = ($self->min(), $self->max());
177 0 ▶ my $arrayEq = sub { return (($_[0] == $_[2]) && ( (!defined($_[1]) && !defined($_[3]))
Subroutine __ANON__: not called CC=6 SCAR=37.4
Condition0%0/15
$_[0] == $_[2] && (!(defined $_[1] || defined $_[3]) || defined $_[1] && defined $_[3] && $_[1] == $_[3])
!ll&&!rl&&r
000
not defined $_[1] || defined $_[3] or defined $_[1] and defined $_[3] and $_[1] == $_[3]
l!l&&r!l&&!r
000
defined $_[1] || defined $_[3]
l!l&&r!l&&!r
000
defined $_[1] and defined $_[3] and $_[1] == $_[3]
!ll&&!rl&&r
000
defined $_[1] and defined $_[3]
!ll&&!rl&&r
000
MC/DC0%0/6
$_[0] == $_[2] && (!(defined $_[1] || defined $_[3]) || defined $_[1] && defined $_[3] && $_[1] == $_[3])
$_[0] == $_[2] defined $_[1] defined $_[3] defined $_[1] defined $_[3] $_[1] == $_[3]
Truth table0%0/10
A && not(B || C) or D and E and F
A= $_[0] == $_[2]B= defined $_[1]C= defined $_[3]D= defined $_[1]E= defined $_[3]F= $_[1] == $_[3]
ABCDEFresult
0XXXXX0
11X0XX0
11X10X0
11X1100
1010XX0
10110X0
1011100
100XXX1
11X1111
1011111
178 || ( (defined($_[1]) && defined($_[3]))
179 0 && ($_[1] == $_[3])) ) ); };
180 0 ▶ if (&$arrayEq(@minmax, 0, undef)) {
Branch: 4 branches
BranchTrueFalseTotal
if (&$arrayEq(@minmax, 0, undef)) { } 0 0 0
elsif (&$arrayEq(@minmax, 1, undef)) { } 0 0 0
elsif (&$arrayEq(@minmax, 0, 1)) { } 0 0 0
elsif (&$arrayEq(@minmax, 1, 1)) { } 0 0 0
181 0 $quant = "*" . $ngreedy;
182 } elsif (&$arrayEq(@minmax, 1, undef)) {
183 0 $quant = "+" . $ngreedy;
184 } elsif (&$arrayEq(@minmax, 0, 1)) {
185 0 $quant = "?";
186 } elsif (&$arrayEq(@minmax, 1, 1)) {
187 0 $quant = ""; # Print nothing if the quantifier is {1}
188 } else {
189 0 my $min = $self->min();
190 0 ▶ my $max = defined($self->max()) ? $self->max() : "";
Branch: 1 branch
BranchTrueFalseTotal
defined $self->max ? : 0 0 0
191 0 ▶ if ($max && ($self->min() == $self->max())) {
Branch: 1 branch
BranchTrueFalseTotal
if ($max and $self->min == $self->max) { } 0 0 0
Condition0%0/3
$max and $self->min == $self->max
!ll&&!rl&&r
000
MC/DC0%0/2
$max and $self->min == $self->max
$max $self->min == $self->max
Truth table0%0/3
A and B
A= $maxB= $self->min == $self->max
ABresult
0X0
100
111
192 0 $quant = "{$max}";
193 } else {
194 0 $quant = "{$min,$max}";
195 }
196 }
197 0 return $quant;
198 }
199  
200 0 ▶sub isGreedy { # Almost everything is greedy
Subroutine isGreedy: not called CC=2 SCAR=17.9
Pod undocumented
201 0 ▶ my $self = shift or confess "%Error: Cannot call once() without a valid object!";
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or confess("%Error: Cannot call once() without a valid object!") 0 0 0
202 0 return ($self->{_greedy});
203 }
204  
205 0 ▶sub production { # Production that this Condition belongs to
Subroutine production: not called CC=2 SCAR=17.9
Pod documented
206 0 ▶ my $self = shift or confess "%Error: Cannot call production() without a valid object!";
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or confess("%Error: Cannot call production() without a valid object!") 0 0 0
207 0 return $self->{_production};
208 }
209  
210 0 ▶sub rule {
Subroutine rule: not called CC=3 SCAR=24.8
Pod documented
211 0 ▶ my $self = shift or confess "%Error: Cannot call rule() without a valid object!";
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or confess("%Error: Cannot call rule() without a valid object!") 0 0 0
212 0 ▶ my $rule = $self->production()->rule() if defined($self->production());
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->production 0 0 0
213 0 return $rule;
214 }
215  
216 0 ▶sub grammar {
Subroutine grammar: not called CC=3 SCAR=24.8
Pod documented
217 0 ▶ my $self = shift or confess "%Error: Cannot call grammar() without a valid object!";
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or confess("%Error: Cannot call grammar() without a valid object!") 0 0 0
218 0 ▶ my $grammar = $self->rule()->grammar() if defined($self->rule());
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->rule 0 0 0
219 0 return $grammar;
220 }
221  
222 ######################################################################
223 #### Package return
224 4 1;
225 __END__
« RandGen/CharClass.pm ↑ Summary RandGen/Subrule.pm »