RandGen/Literal.pm

filter: Statementstmt 93.8% 46 / 49 Branchbran 75.0% 18 / 24 Conditioncond 83.3% 5 / 6 MC/DCmcdc 66.6% 2 / 3 Subroutinesub 87.5% 7 / 8 Podpod 33.3% 1 / 3 totaltotal 84.9% 79 / 93 CC 15 SCAR 20.1
CC
15
Cov
87%
pick
12.1
_newDerived
3.1
dump
2.1
CRAP 7.5
« RandGen/Rule.pm ↑ Summary RandGen.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::Literal;
21  
22 4 require 5.006_001;
23 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 4 ▶use Parse::RandGen qw($Debug);
Subroutine BEGIN: called CC=1 SCAR=0.0
25 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 4 ▶use vars qw(@ISA $ValidLiteralRE $Debug);
Subroutine BEGIN: called CC=1 SCAR=0.0
27 4 @ISA = ('Parse::RandGen::Condition');
28  
29 # This regular expression defines whether a given regexp condition is valid (i.e. can be understood by the Condition module)
30 # To be valid, the regexp must
31 4 $ValidLiteralRE = qr /
32 ([\'\"]) # Match either a single- or double-quote delimiter
33 (?: [^\\\1] | \\. )+ # In the middle of the regexp, match either <1> a character that is not a backslash or the delimiter or <2> a backslash followed by any character
34 \1 # Match the original delimiter that was found
35 /x;
36  
37 7 ▶sub _newDerived {
Subroutine _newDerived: called CC=3 SCAR=11.4
38 7 ▶ my $self = shift or confess("%Error: Cannot call without a valid object!");
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or confess("%Error: Cannot call without a valid object!") 7 0 7
39 7 my $type = ref($self);
40 7 my $elemRef = ref($self->element());
41 7 ▶ (!$elemRef) or confess("%Error: $type has an element is a reference (ref=\"$elemRef\") instead of a literal scalar!");
Branch: 1 branch
BranchTrueFalseTotal
not $elemRef or confess(qq[%Error: $type has an element is a reference (ref="$elemRef") instead of a literal scalar!]) 7 0 7
42 }
43  
44 400 ▶sub dump {
Subroutine dump: called CC=2 SCAR=7.2
Pod undocumented
45 400 ▶ my $self = shift or confess("%Error: Cannot call without a valid object!");
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or confess("%Error: Cannot call without a valid object!") 400 0 400
46 400 return ("'" . $self->element() . "'");
47 }
48  
49 814 ▶sub pick {
Subroutine pick: called CC=12 SCAR=24.9
Pod documented
50 814 ▶ my $self = shift or confess("%Error: Cannot call without a valid object!");
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or confess("%Error: Cannot call without a valid object!") 814 0 814
51 814 my %args = ( match=>1, # Default is to pick matching data
52 @_ );
53 814 my $val = $self->element(); # Reset to element before each attempt
54 814 my $keepTrying = 10;
55 814 my $length = length($self->element());
56 814 ▶ confess "Literal length is 0! This should never be!\n" unless ($length);
Branch: 1 branch
BranchTrueFalseTotal
unless $length 0 814 814
57  
58 814 my ($method, $char);
59 814 ▶ while (!$args{match} && $keepTrying-- && ($val eq $self->element())) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $args{"match"} and $keepTrying-- and $val eq $self->element) 70 814 884
Condition83%5/6
not $args{"match"} and $keepTrying-- and $val eq $self->element
!ll&&!rl&&r
7447070
not $args{"match"} and $keepTrying--
!ll&&!rl&&r
7440140
MC/DC66%2/3
not $args{"match"} and $keepTrying-- and $val eq $self->element
not $args{"match"} $keepTrying-- $val eq $self->element
Truth table75%3/4
not(A) and B and C
A= not $args{"match"}B= $keepTrying--C= $val eq $self->element
ABCresult
0XX0
10X0
1100
1111
60 70 $val = $self->element(); # Reset to element before each corruption attempt
61 70 $method = int(rand(4)); # Method of corruption
62 70 $char = int(rand($length)); # Which character
63  
64 70 ▶ if ($method == 0) {
Branch: 3 branches
BranchTrueFalseTotal
if ($method == 0) { } 21 49 70
elsif ($method == 1) { } 18 31 49
elsif ($method == 2) { } 15 16 31
65 # Try changing the case of first character
66 21 substr($val, $char, 1) = lc(substr($val, $char, 1));
67 21 ▶ substr($val, $char, 1) = uc(substr($val, $char, 1)) unless ($val ne $self->element());
Branch: 1 branch
BranchTrueFalseTotal
unless $val ne $self->element 21 0 21
68 } elsif ($method == 1) {
69 # Randomly change the value of one of the characters
70 18 substr($val, $char, 1) = chr( (ord(substr($val, $char, 1)) + int(rand(256))) % 256 );
71 } elsif ($method == 2) {
72 # Insert a random character into the literal
73 15 $char = int(rand($length+1)); # Where to insert character
74 15 substr($val, $char, 0) = int(rand(256)) # Insert random character
75 } else {
76 # Remove a character
77 16 substr($val, $char, 1) = '';
78 }
79 }
80  
81 814 my $elem = $self->element();
82 814 ▶ if ($Debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Parse::RandGen::Literal::Debug) 810 4 814
83 810 ▶ if ($args{match}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'match'}) { } 741 69 810
84 741 print ("Parse::RandGen::Literal($elem)::pick(match=>$args{match}) with value of ", $self->dumpVal($val), "\n");
85 } else {
86 69 print ("Parse::RandGen::Literal($elem)::pick(match=>$args{match}, method=>$method, char=>$char) with value of ", $self->dumpVal($val), "\n");
87 }
88 }
89 814 return ($val);
90 }
91  
92 0 ▶sub stripLiteral {
Subroutine stripLiteral: not called CC=1 SCAR=6.9
Pod undocumented
93 0 my $lit = shift;
94 0 $lit =~ s/([\'\"])(.+)\1/$2/;
95 0 return ($lit);
96 }
97  
98 ######################################################################
99 #### Package return
100 4 1;
101 __END__
« RandGen/Rule.pm ↑ Summary RandGen.pm »