| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||
| 58 | 814 | my ($method, $char); | |||||||||||||||||||||||||||||||||||||||||
| 59 | 814 | ▶ | while (!$args{match} && $keepTrying-- && ($val eq $self->element())) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
not $args{"match"} and $keepTrying-- and $val eq $self->element
not $args{"match"} and $keepTrying--
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 83 | 810 | ▶ | if ($args{match}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||||||||||