blib/lib/Langertha/Holmgang/Sim/Denken.pm

filter: Statementstmt 92.8% 429 / 462 Branchbran 77.8% 246 / 316 Conditioncond 60.5% 83 / 137 MC/DCmcdc 38.5% 32 / 83 Subroutinesub 98.1% 52 / 53 Podpod 100% 6 / 6 totaltotal 80.2% 848 / 1057 CC 230 SCAR 27.9
CC
230
Cov
83%
_cd_number
105.6
_pl_parse
33.4
__ANON__
20.0
CRAP 16.3
« blib/lib/Langertha/Holmgang/Runner.pm ↑ Summary blib/lib/Langertha/Holmgang/CLI.pm »
1 package Langertha::Holmgang::Sim::Denken;
2 # ABSTRACT: The answer world denken — reasoning tasks, the answer handed in by tool, one checker per task like score_answer of Reasoning Gym
3  
4 39 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 39 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 39 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 39 ▶use B ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 39 ▶use JSON::MaybeXS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 39 ▶use Math::BigInt ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 39 ▶use Math::BigRat ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 39 with 'Langertha::Holmgang::Role::AnswerWorld';
13  
14 39 our $VERSION = '0.001';
15  
16  
17 39 our %TOOLS = ( antwort_abgeben => ['antwort'] );
18  
19 39 my %CHECK = (
20 zebra_puzzles => \&_zebra,
21 knights_knaves => \&_knights,
22 propositional_logic => \&_proplogic,
23 syllogism => \&_exact,
24 course_schedule => \&_exact,
25 maze => \&_exact,
26 self_reference => \&_exact,
27 shortest_path => \&_shortest_path,
28 graph_color => \&_graph_color,
29 calendar_arithmetic => \&_calendar,
30 time_intervals => \&_time_intervals,
31 cryptarithm => \&_cryptarithm,
32 mini_sudoku => \&_mini_sudoku,
33 family_relationships => \&_family,
34 countdown => \&_countdown,
35 jugs => \&_jugs,
36 );
37  
38 # Pflichtfelder von pruefung je Aufgabe
39 39 my %NEED = (
40 propositional_logic => [qw( premissen variablen )],
41 shortest_path => [qw( antwort matrix )],
42 graph_color => [qw( knoten kanten farben )],
43 calendar_arithmetic => [qw( antwort form )],
44 time_intervals => [qw( antwort form )],
45 cryptarithm => [qw( woerter ergebnis )],
46 countdown => [qw( zahlen ziel )],
47 jugs => [qw( kruege ziel )],
48 );
49  
50 #### Plugin contract
51  
52 78 ▶sub device_type { 'denken' }
Subroutine device_type: called CC=1 SCAR=0.0
Pod documented
53  
54  
55 2 ▶sub tools { return \%TOOLS }
Subroutine tools: called CC=1 SCAR=0.0
Pod documented
56  
57  
58 4 ▶sub prepare {
Subroutine prepare: called CC=7 SCAR=19.5
Pod documented
59 4 my ( $self, $dev, $ctx ) = @_;
60 4 ▶ my $art = $dev->{art} // '';
Condition50%1/2
$$dev{"art"} // ""
l!l
40
MC/DC0%0/1
$$dev{"art"} // ""
$$dev{"art"}
Truth table50%1/2
A
A= $$dev{"art"}
Aresult
11
00
61 4 ▶ die "denken: unbekannte Aufgabe '$art' (" . join( ', ', arten() ) . ")\n" unless $CHECK{$art};
Branch: 1 branch
BranchTrueFalseTotal
unless $CHECK{$art} 1 3 4
62 3 my $p = $dev->{pruefung};
63 3 ▶ die "denken: pruefung fehlt (Hash)\n" unless ref $p eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $p eq "HASH" 1 2 3
64 2 ▶ for my $k ( @{ $NEED{$art} || ['antwort'] } ) {
Condition100%2/2
$NEED{$art} || ['antwort']
l!l
11
MC/DC100%1/1
$NEED{$art} || ['antwort']
$NEED{$art}
Truth table100%2/2
A
A= $NEED{$art}
Aresult
11
00
65 3 ▶ die "denken: pruefung.$k fehlt ($art)\n" unless defined $p->{$k};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $$p{$k} 1 2 3
66 }
67 1 return;
68 }
69  
70  
71 5 ▶sub tool {
Subroutine tool: called CC=9 SCAR=22.1
Pod documented
72 5 my ( $self, $dev, $name, $args, $ctx ) = @_;
73 5 ▶ return ( "Fehler: unbekanntes Werkzeug '" . ( $name // '' ) . "'", 0 ) unless ( $name // '' ) eq 'antwort_abgeben';
Branch: 1 branch
BranchTrueFalseTotal
unless ($name // "") eq "antwort_abgeben" 1 4 5
Condition50%2/4
$name // ""
l!l
50
$name // ""
l!l
10
MC/DC0%0/1
$name // ""
$name
MC/DC0%0/1
$name // ""
$name
Truth table50%1/2
A
A= $name
Aresult
11
00
Truth table50%1/2
A
A= $name
Aresult
11
00
74 4 ▶ my $ans = ( $args || {} )->{antwort};
Condition50%1/2
$args || {}
l!l
40
MC/DC0%0/1
$args || {}
$args
Truth table50%1/2
A
A= $args
Aresult
11
00
75 4 ▶ return ( 'Fehler: antwort fehlt (Text).', 0 ) unless defined $ans && !ref $ans;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ans and not ref $ans 2 2 4
Condition100%3/3
defined $ans and not ref $ans
!ll&&!rl&&r
112
MC/DC100%2/2
defined $ans and not ref $ans
defined $ans not ref $ans
Truth table100%3/3
A and not(B)
A= defined $ansB= not ref $ans
ABresult
0X0
100
111
76 2 my ( $ok, $why ) = pruefe( $dev->{art}, $dev->{pruefung}, "$ans" );
77 2 $dev->{antwort} = "$ans";
78 2 ▶ $dev->{abgaben} = ( $dev->{abgaben} // 0 ) + 1;
Condition100%2/2
$$dev{"abgaben"} // 0
l!l
11
MC/DC100%1/1
$$dev{"abgaben"} // 0
$$dev{"abgaben"}
Truth table100%2/2
A
A= $$dev{"abgaben"}
Aresult
11
00
79 2 ▶ $dev->{ergebnis} = $ok ? 'richtig' : 'falsch';
Branch: 1 branch
BranchTrueFalseTotal
$ok ? : 1 1 2
80 2 $dev->{befund} = $why;
81 2 return ( 'Antwort gespeichert.', 1 );
82 }
83  
84  
85 #### Pruefer (reine Funktionen)
86  
87 2 ▶sub arten { return sort keys %CHECK }
Subroutine arten: called CC=1 SCAR=0.0
Pod documented
88  
89  
90 105 ▶sub pruefe {
Subroutine pruefe: called CC=10 SCAR=24.2
Pod documented
91 105 my ( $art, $p, $answer ) = @_;
92 105 ▶ my $code = $CHECK{ $art // '' } or return ( 0, 'unbekannte Aufgabe' );
Branch: 1 branch
BranchTrueFalseTotal
my $code = $CHECK{$art // ""} or return 0, "unbekannte Aufgabe" 104 1 105
Condition50%1/2
$art // ""
l!l
1050
MC/DC0%0/1
$art // ""
$art
Truth table50%1/2
A
A= $art
Aresult
11
00
93 104 ▶ return ( 0, 'keine Antwort' ) unless defined $answer && !ref $answer;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $answer and not ref $answer 1 103 104
Condition66%2/3
defined $answer and not ref $answer
!ll&&!rl&&r
10103
MC/DC50%1/2
defined $answer and not ref $answer
defined $answer not ref $answer
Truth table66%2/3
A and not(B)
A= defined $answerB= not ref $answer
ABresult
0X0
100
111
94 103 ▶ my @r = eval { $code->( $p || {}, $answer ) };
Condition50%1/2
$p || {}
l!l
1030
MC/DC0%0/1
$p || {}
$p
Truth table50%1/2
A
A= $p
Aresult
11
00
95 103 ▶ return ( 0, 'nicht auswertbar: ' . ( "$@" =~ s/\s+\z//r ) ) unless @r;
Branch: 1 branch
BranchTrueFalseTotal
unless @r 2 101 103
96 101 ▶ return ( $r[0] ? 1 : 0, $r[1] // ( $r[0] ? 'richtig' : 'falsch' ) );
Branch: 2 branches
BranchTrueFalseTotal
$r[0] ? : 41 60 101
$r[0] ? : 0 0 0
Condition50%1/2
$r[1] // ($r[0] ? "richtig" : "falsch")
l!l
1010
MC/DC0%0/1
$r[1] // ($r[0] ? "richtig" : "falsch")
$r[1]
Truth table50%1/2
A
A= $r[1]
Aresult
11
00
97 }
98  
99  
100 # --- Hilfen wie Python --------------------------------------------------------
101  
102 273 ▶sub _strip { my ($s) = @_; $s =~ s/\A\s+|\s+\z//g; return $s }
Subroutine _strip: called CC=1 SCAR=0.0
103  
104 # int(text) wie Python: Leerraum aussen, Vorzeichen, Ziffern mit einzelnen _
105 91 ▶sub _pyint {
Subroutine _pyint: called CC=5 SCAR=16.8
106 91 my ($s) = @_;
107 91 ▶ return undef unless defined $s;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s 0 91 91
108 91 $s = _strip($s);
109 91 ▶ return undef unless $s =~ /\A([+-]?)(\d+(?:_\d+)*)\z/a;
Branch: 1 branch
BranchTrueFalseTotal
unless $s =~ /\A([+-]?)(\d+(?:_\d+)*)\z/a 2 89 91
110 89 my ( $sign, $d ) = ( $1, $2 =~ tr/_//dr );
111 89 $d =~ s/\A0+(?=\d)//;
112 89 ▶ return $sign eq '-' && $d ne '0' ? "-$d" : $d;
Branch: 1 branch
BranchTrueFalseTotal
$sign eq '-' && $d ne '0' ? : 0 89 89
Condition33%1/3
$sign eq "-" and $d ne 0
!ll&&!rl&&r
8900
MC/DC0%0/2
$sign eq "-" and $d ne 0
$sign eq "-" $d ne 0
Truth table33%1/3
A and B
A= $sign eq "-"B= $d ne 0
ABresult
0X0
100
111
113 }
114  
115 60 ▶sub _fail { return ( 0, $_[0] ) }
Subroutine _fail: called CC=1 SCAR=0.0
116  
117 # --- Prüfer -----------------------------------------------------------------
118  
119 9 ▶sub _exact {
Subroutine _exact: called CC=2 SCAR=6.9
120 9 my ( $p, $ans ) = @_;
121 9 ▶ return $ans eq "$p->{antwort}" ? ( 1, 'gleich' ) : _fail('nicht gleich');
Branch: 1 branch
BranchTrueFalseTotal
$ans eq "$$p{'antwort'}" ? : 4 5 9
122 }
123  
124 5 ▶sub _zebra {
Subroutine _zebra: called CC=2 SCAR=6.9
125 5 my ( $p, $ans ) = @_;
126 5 ▶ my $norm = sub { lc( $_[0] =~ s/\n//gr ) };
Subroutine __ANON__: called CC=1 SCAR=0.0
127 5 ▶ return $norm->($ans) eq $norm->( $p->{antwort} ) ? ( 1, 'gleich' ) : _fail('nicht gleich');
Branch: 1 branch
BranchTrueFalseTotal
&$norm($ans) eq &$norm($$p{'antwort'}) ? : 3 2 5
128 }
129  
130 2 ▶sub _family {
Subroutine _family: called CC=2 SCAR=6.9
131 2 my ( $p, $ans ) = @_;
132 2 ▶ return lc( _strip($ans) ) eq lc( _strip( $p->{antwort} ) ) ? ( 1, 'gleich' ) : _fail('nicht gleich');
Branch: 1 branch
BranchTrueFalseTotal
lc _strip($ans) eq lc _strip($$p{'antwort'}) ? : 1 1 2
133 }
134  
135 39 my %ROLES = map { $_ => 1 }
136 qw( knight sinner fool pioneer laggard hero sage villain egoist angel altruist saint devil knave );
137  
138 8 ▶sub _kk_pairs {
Subroutine _kk_pairs: called CC=7 SCAR=19.5
139 8 my ($s) = @_;
140 8 $s = _strip( lc $s );
141 8 $s =~ tr/.,)(/ /;
142 8 $s =~ s/ and / /g;
143 8 my ( %set, $name );
144 8 for my $w ( split ' ', $s ) {
145 60 ▶ next if $w eq 'is' || $w eq 'a' || $w eq 'an';
Branch: 1 branch
BranchTrueFalseTotal
if $w eq "is" or $w eq "a" or $w eq "an" 30 30 60
Condition83%5/6
$w eq "is" or $w eq "a" or $w eq "an"
l!l&&r!l&&!r
30030
$w eq "is" or $w eq "a"
l!l&&r!l&&!r
151530
MC/DC66%2/3
$w eq "is" or $w eq "a" or $w eq "an"
$w eq "is" $w eq "a" $w eq "an"
Truth table75%3/4
A or B or C
A= $w eq "is"B= $w eq "a"C= $w eq "an"
ABCresult
1XX1
01X1
0011
0000
146 30 ▶ if ( $ROLES{$w} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ROLES{$w}) { } 15 15 30
147 15 ▶ $set{"$name\0$w"} = 1 if defined $name;
Branch: 1 branch
BranchTrueFalseTotal
if defined $name 15 0 15
148 15 undef $name;
149 }
150 15 else { $name = $w }
151 }
152 8 return join "\n", sort keys %set;
153 }
154  
155 5 ▶sub _knights {
Subroutine _knights: called CC=3 SCAR=11.0
156 5 my ( $p, $ans ) = @_;
157 5 ▶ return _fail('leer') unless length $ans;
Branch: 1 branch
BranchTrueFalseTotal
unless length $ans 1 4 5
158 4 ▶ return _kk_pairs($ans) eq _kk_pairs( $p->{antwort} ) ? ( 1, 'Zuordnung gleich' ) : _fail('Zuordnung anders');
Branch: 1 branch
BranchTrueFalseTotal
_kk_pairs($ans) eq _kk_pairs($$p{'antwort'}) ? : 2 2 4
159 }
160  
161 # Aussagenlogik: Parser wie parse_expr in reasoning_gym/logic/propositional_logic.py
162 39 my @PL_LEVELS = ( '↔', '→', '∨', '∧' );
163  
164 35 ▶sub _pl_parse {
Subroutine _pl_parse: called CC=20 SCAR=35.1
165 35 my ( $e, $depth ) = @_;
166 35 ▶ die "zu tief verschachtelt\n" if ( $depth // 0 ) > 400;
Branch: 1 branch
BranchTrueFalseTotal
if ($depth // 0) > 400 0 35 35
Condition100%2/2
$depth // 0
l!l
2015
MC/DC100%1/1
$depth // 0
$depth
Truth table100%2/2
A
A= $depth
Aresult
11
00
167 35 ▶ $depth = ( $depth // 0 ) + 1;
Condition100%2/2
$depth // 0
l!l
2015
MC/DC100%1/1
$depth // 0
$depth
Truth table100%2/2
A
A= $depth
Aresult
11
00
168 35 $e = _strip($e);
169 35 ▶ die "leerer Ausdruck\n" unless length $e;
Branch: 1 branch
BranchTrueFalseTotal
unless length $e 0 35 35
170 35 ▶ if ( substr( $e, 0, 1 ) eq '(' && substr( $e, -1 ) eq ')' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($e, 0, 1) eq "(" and substr($e, -1) eq ")") 2 33 35
Condition66%2/3
substr($e, 0, 1) eq "(" and substr($e, -1) eq ")"
!ll&&!rl&&r
3302
MC/DC50%1/2
substr($e, 0, 1) eq "(" and substr($e, -1) eq ")"
substr($e, 0, 1) eq "(" substr($e, -1) eq ")"
Truth table66%2/3
A and B
A= substr($e, 0, 1) eq "("B= substr($e, -1) eq ")"
ABresult
0X0
100
111
171 2 my ( $level, $valid ) = ( 0, 1 );
172 2 for my $ch ( split //, substr( $e, 1, length($e) - 2 ) ) {
173 0 ▶ if ( $ch eq '(' ) { $level++ }
Branch: 2 branches
BranchTrueFalseTotal
if ($ch eq '(') { } 0 11 11
elsif ($ch eq ')') { } 0 11 11
174 0 ▶ elsif ( $ch eq ')' ) { $level--; if ( $level < 0 ) { $valid = 0; last } }
Branch: 1 branch
BranchTrueFalseTotal
if ($level < 0) 0 0 0
175 }
176 2 ▶ return _pl_parse( substr( $e, 1, length($e) - 2 ), $depth ) if $level == 0 && $valid;
Branch: 1 branch
BranchTrueFalseTotal
if $level == 0 and $valid 2 0 2
Condition33%1/3
$level == 0 and $valid
!ll&&!rl&&r
002
MC/DC0%0/2
$level == 0 and $valid
$level == 0 $valid
Truth table33%1/3
A and B
A= $level == 0B= $valid
ABresult
0X0
100
111
177 }
178 33 for my $op (@PL_LEVELS) {
179 121 my $level = 0;
180 121 ▶ for ( my $i = length($e) - 1; $i >= 0; $i-- ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= 0) 200 113 313
181 200 my $ch = substr( $e, $i, 1 );
182 0 ▶ if ( $ch eq ')' ) { $level++ }
Branch: 3 branches
BranchTrueFalseTotal
if ($ch eq ')') { } 0 200 200
elsif ($ch eq '(') { } 0 200 200
elsif ($level == 0 and $ch eq $op) { } 8 192 200
183 0 elsif ( $ch eq '(' ) { $level-- }
184 ▶ elsif ( $level == 0 && $ch eq $op ) {
Condition66%2/3
$level == 0 and $ch eq $op
!ll&&!rl&&r
01928
MC/DC50%1/2
$level == 0 and $ch eq $op
$level == 0 $ch eq $op
Truth table66%2/3
A and B
A= $level == 0B= $ch eq $op
ABresult
0X0
100
111
185 8 return [ $op, _pl_parse( substr( $e, 0, $i ), $depth ), _pl_parse( substr( $e, $i + 1 ), $depth ) ];
186 }
187 }
188 }
189 25 ▶ return [ '¬', _pl_parse( substr( $e, 1 ), $depth ) ] if substr( $e, 0, 1 ) eq '¬';
Branch: 1 branch
BranchTrueFalseTotal
if substr($e, 0, 1) eq "\x{ac}" 2 23 25
190 23 return [ var => $e ];
191 }
192  
193 43 ▶sub _pl_vars {
Subroutine _pl_vars: called CC=3 SCAR=11.0
194 43 my ( $x, $acc ) = @_;
195 43 ▶ if ( $x->[0] eq 'var' ) { $acc->{ $x->[1] } = 1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($$x[0] eq 'var') { } 29 14 43
196 14 else { _pl_vars( $_, $acc ) for @{$x}[ 1 .. $#$x ] }
197 43 return $acc;
198 }
199  
200 125 ▶sub _pl_eval {
Subroutine _pl_eval: called CC=11 SCAR=24.9
201 125 my ( $x, $v ) = @_;
202 125 my $op = $x->[0];
203 125 ▶ return $v->{ $x->[1] } if $op eq 'var';
Branch: 1 branch
BranchTrueFalseTotal
if $op eq "var" 88 37 125
204 37 ▶ return !_pl_eval( $x->[1], $v ) if $op eq '¬';
Branch: 1 branch
BranchTrueFalseTotal
if $op eq "\x{ac}" 4 33 37
205 33 my ( $l, $r ) = ( _pl_eval( $x->[1], $v ), _pl_eval( $x->[2], $v ) );
206 33 ▶ return $op eq '∧' ? $l && $r : $op eq '∨' ? $l || $r : $op eq '→' ? !$l || $r : ( $l ? 1 : 0 ) == ( $r ? 1 : 0 );
Branch: 5 branches
BranchTrueFalseTotal
$op eq "\x{2227}" ? : 10 23 33
$op eq "\x{2228}" ? : 3 20 23
$op eq "\x{2192}" ? : 20 0 20
$l ? : 0 0 0
$r ? : 0 0 0
Condition88%8/9
$l and $r
!ll&&!rl&&r
424
$l or $r
l!l&&r!l&&!r
210
not $l or $r
l!l&&r!l&&!r
1055
MC/DC100%2/2
$l and $r
$l $r
MC/DC0%0/2
$l or $r
$l $r
MC/DC100%2/2
not $l or $r
not $l $r
Truth table100%3/3
A and B
A= $lB= $r
ABresult
0X0
100
111
Truth table66%2/3
A or B
A= $lB= $r
ABresult
1X1
011
000
Truth table100%3/3
not(A) or B
A= not $lB= $r
ABresult
1X1
011
000
207 }
208  
209 8 ▶sub _pl_assignments {
Subroutine _pl_assignments: called CC=2 SCAR=7.0
210 8 my (@vars) = sort @_;
211 8 ▶ die "zu viele Atome\n" if @vars > 16;
Branch: 1 branch
BranchTrueFalseTotal
if @vars > 16 0 8 8
212 8 return map { my $i = $_; +{ map { ( $vars[$_] => ( $i >> $_ ) & 1 ) } 0 .. $#vars } } 0 .. 2**@vars - 1;
213 }
214  
215 6 ▶sub _proplogic {
Subroutine _proplogic: called CC=9 SCAR=22.0
216 6 my ( $p, $ans ) = @_;
217 6 my %valid = map { $_ => 1 } @{ $p->{variablen} };
218 6 my ($bad) = grep { !$valid{$_} } $ans =~ /([A-Z])/g;
219 6 ▶ return _fail("Atom $bad kommt in der Aufgabe nicht vor") if defined $bad;
Branch: 1 branch
BranchTrueFalseTotal
if defined $bad 1 5 6
220 5 my @prem = map { _pl_parse($_) } @{ $p->{premissen} };
221 5 ▶ my $c = eval { _pl_parse($ans) } or return _fail( 'nicht lesbar: ' . ( "$@" =~ s/\s+\z//r ) );
Branch: 1 branch
BranchTrueFalseTotal
my $c = eval { do { _pl_parse($ans) } } or return _fail('nicht lesbar: ' . "$@" =~ s/\s+\z//r) 5 0 5
222 5 my %all;
223 5 _pl_vars( $_, \%all ) for @prem, $c;
224 5 for my $v ( _pl_assignments( keys %all ) ) {
225 20 ▶ return _fail('folgt nicht aus den Prämissen') if !( grep { !_pl_eval( $_, $v ) } @prem ) && !_pl_eval( $c, $v );
Branch: 1 branch
BranchTrueFalseTotal
unless grep {not _pl_eval($_, $v);} @prem or _pl_eval($c, $v) 1 19 20
Condition100%3/3
grep {not _pl_eval($_, $v);} @prem or _pl_eval($c, $v)
l!l&&r!l&&!r
1541
MC/DC100%2/2
grep {not _pl_eval($_, $v);} @prem or _pl_eval($c, $v)
grep {not _pl_eval($_, $v);} @prem _pl_eval($c, $v)
Truth table100%3/3
A or B
A= grep {not _pl_eval($_, $v);} @premB= _pl_eval($c, $v)
ABresult
1X1
011
000
226 }
227 4 ▶ return _fail('trivial (einzelnes Atom)') if $c->[0] eq 'var';
Branch: 1 branch
BranchTrueFalseTotal
if $$c[0] eq "var" 1 3 4
228 3 my $own = _pl_vars( $c, {} );
229 3 ▶ return _fail('trivial (Tautologie)') unless grep { !_pl_eval( $c, $_ ) } _pl_assignments( keys %$own );
Branch: 1 branch
BranchTrueFalseTotal
unless grep {not _pl_eval($c, $_);} _pl_assignments(keys %$own) 1 2 3
230 2 return ( 1, 'folgt und ist nicht trivial' );
231 }
232  
233 39 my %DIR = ( right => [ 0, 1 ], down => [ 1, 0 ], left => [ 0, -1 ], up => [ -1, 0 ] );
234  
235 9 ▶sub _shortest_path {
Subroutine _shortest_path: called CC=19 SCAR=29.7
236 9 my ( $p, $ans ) = @_;
237 9 ▶ return _fail('leer') unless length $ans;
Branch: 1 branch
BranchTrueFalseTotal
unless length $ans 1 8 9
238 8 my $oracle = _strip( $p->{antwort} );
239 8 ▶ return ( 1, 'gleich' ) if _strip($ans) eq $oracle;
Branch: 1 branch
BranchTrueFalseTotal
if _strip($ans) eq $oracle 2 6 8
240 6 my @path = split ' ', $ans;
241 6 my @want = split ' ', $oracle;
242 6 my $m = $p->{matrix};
243 6 my ( $rows, $cols ) = ( scalar @$m, scalar @{ $m->[0] } );
244 6 my ( @start, @goal );
245 6 for my $r ( 0 .. $rows - 1 ) {
246 18 for my $c ( 0 .. $cols - 1 ) {
247 54 ▶ @start = ( $r, $c ) if !@start && $m->[$r][$c] eq '*';
Branch: 1 branch
BranchTrueFalseTotal
if not @start and $$m[$r][$c] eq "*" 6 48 54
Condition66%2/3
not @start and $$m[$r][$c] eq "*"
!ll&&!rl&&r
4806
MC/DC50%1/2
not @start and $$m[$r][$c] eq "*"
not @start $$m[$r][$c] eq "*"
Truth table66%2/3
not(A) and B
A= not @startB= $$m[$r][$c] eq "*"
ABresult
0X0
100
111
248 54 ▶ @goal = ( $r, $c ) if !@goal && $m->[$r][$c] eq '#';
Branch: 1 branch
BranchTrueFalseTotal
if not @goal and $$m[$r][$c] eq "#" 6 48 54
Condition66%2/3
not @goal and $$m[$r][$c] eq "#"
!ll&&!rl&&r
0486
MC/DC50%1/2
not @goal and $$m[$r][$c] eq "#"
not @goal $$m[$r][$c] eq "#"
Truth table66%2/3
not(A) and B
A= not @goalB= $$m[$r][$c] eq "#"
ABresult
0X0
100
111
249 }
250 }
251 6 my ( $r, $c ) = @start;
252 6 for my $d (@path) {
253 20 ▶ my $step = $DIR{$d} or return _fail("keine Richtung: $d");
Branch: 1 branch
BranchTrueFalseTotal
my $step = $DIR{$d} or return _fail("keine Richtung: $d") 19 1 20
254 19 ( $r, $c ) = ( $r + $step->[0], $c + $step->[1] );
255 19 ▶ return _fail('verlässt das Gitter') unless $r >= 0 && $r < $rows && $c >= 0 && $c < $cols;
Branch: 1 branch
BranchTrueFalseTotal
unless $r >= 0 and $r < $rows and $c >= 0 and $c < $cols 1 18 19
Condition66%6/9
$r >= 0 and $r < $rows and $c >= 0 and $c < $cols
!ll&&!rl&&r
1018
$r >= 0 and $r < $rows and $c >= 0
!ll&&!rl&&r
1018
$r >= 0 and $r < $rows
!ll&&!rl&&r
1018
MC/DC25%1/4
$r >= 0 and $r < $rows and $c >= 0 and $c < $cols
$r >= 0 $r < $rows $c >= 0 $c < $cols
Truth table40%2/5
A and B and C and D
A= $r >= 0B= $r < $rowsC= $c >= 0D= $c < $cols
ABCDresult
0XXX0
10XX0
110X0
11100
11111
256 18 ▶ return _fail('läuft durch X') if $m->[$r][$c] eq 'X';
Branch: 1 branch
BranchTrueFalseTotal
if $$m[$r][$c] eq "X" 1 17 18
257 }
258 3 ▶ return _fail('endet nicht am Ziel') unless $r == $goal[0] && $c == $goal[1];
Branch: 1 branch
BranchTrueFalseTotal
unless $r == $goal[0] and $c == $goal[1] 1 2 3
Condition66%2/3
$r == $goal[0] and $c == $goal[1]
!ll&&!rl&&r
102
MC/DC50%1/2
$r == $goal[0] and $c == $goal[1]
$r == $goal[0] $c == $goal[1]
Truth table66%2/3
A and B
A= $r == $goal[0]B= $c == $goal[1]
ABresult
0X0
100
111
259 2 ▶ return @path == @want ? ( 1, 'gültiger kürzester Weg' ) : _fail( 'gültig, aber ' . scalar(@path) . ' statt ' . scalar(@want) . ' Schritte' );
Branch: 1 branch
BranchTrueFalseTotal
@path == @want ? : 1 1 2
260 }
261  
262 39 my $JSON = JSON::MaybeXS->new( utf8 => 0, allow_nonref => 1 );
263  
264 # Zahl aus JSON (keine Zeichenkette); Wahrheitswerte wie in Python 1/0
265 11 ▶sub _json_num {
Subroutine _json_num: called CC=9 SCAR=27.2
266 11 my ($v) = @_;
267 11 ▶ return undef unless defined $v;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 0 11 11
268 11 ▶ if ( ref $v ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $v) 0 11 11
269 0 ▶ return JSON::MaybeXS::is_bool($v) ? ( $v ? 1 : 0 ) : undef;
Branch: 2 branches
BranchTrueFalseTotal
JSON::MaybeXS::is_bool($v) ? : 0 0 0
$v ? : 0 0 0
270 }
271 11 my $f = B::svref_2object( \$v )->FLAGS;
272 11 ▶ return undef if $f & B::SVf_POK && !( $f & ( B::SVf_IOK | B::SVf_NOK ) );
Branch: 1 branch
BranchTrueFalseTotal
if $f & 1024 and not $f & 768 1 10 11
Condition66%2/3
$f & 1024 and not $f & 768
!ll&&!rl&&r
1001
MC/DC50%1/2
$f & 1024 and not $f & 768
$f & 1024 not $f & 768
Truth table66%2/3
A and not(B)
A= $f & 1024B= not $f & 768
ABresult
0X0
100
111
273 10 ▶ return undef if $f & B::SVf_POK && $v !~ /\A-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $f & 1024 and not $v =~ /\A-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?\z/ 0 10 10
Condition33%1/3
$f & 1024 and not $v =~ /\A-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?\z/
!ll&&!rl&&r
1000
MC/DC0%0/2
$f & 1024 and not $v =~ /\A-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?\z/
$f & 1024 not $v =~ /\A-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?\z/
Truth table33%1/3
A and not(B)
A= $f & 1024B= not $v =~ /\A-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?\z/
ABresult
0X0
100
111
274 10 return 0 + $v;
275 }
276  
277 8 ▶sub _graph_color {
Subroutine _graph_color: called CC=11 SCAR=25.6
278 8 my ( $p, $ans ) = @_;
279 8 my $col = eval { $JSON->decode($ans) };
280 8 ▶ return _fail('kein JSON') if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 1 7 8
281 7 my $get;
282 7 ▶ if ( ref $col eq 'HASH' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $col eq 'HASH') { } 6 1 7
elsif (ref $col eq 'ARRAY') { } 0 1 1
283 6 ▶ $get = sub { exists $col->{"$_[0]"} ? ( 1, $col->{"$_[0]"} ) : (0) };
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
exists $$col{"$_[0]"} ? : 17 1 18
284 }
285 elsif ( ref $col eq 'ARRAY' ) {
286 # Python: `knoten in liste` vergleicht Werte, dann liste[knoten]
287 0 ▶ $get = sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
288 0 my $v = shift;
289 0 ▶ return (0) unless grep { defined && $_ == $v } map { _json_num($_) } @$col;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {defined $_ and $_ == $v;} map({_json_num($_);} @$col) 0 0 0
Condition0%0/3
defined $_ && $_ == $v
!ll&&!rl&&r
000
MC/DC0%0/2
defined $_ && $_ == $v
defined $_ $_ == $v
Truth table0%0/3
A && B
A= defined $_B= $_ == $v
ABresult
0X0
100
111
290 0 ▶ die "Index $v ausserhalb der Liste\n" if $v > $#$col;
Branch: 1 branch
BranchTrueFalseTotal
if $v > $col->$#* 0 0 0
291 0 return ( 1, $col->[$v] );
292 0 };
293 }
294 1 else { return _fail('kein JSON-Objekt') }
295 6 my %allowed = map { $_ => 1 } @{ $p->{farben} };
296 6 my %c;
297 6 for my $v ( @{ $p->{knoten} } ) {
298 18 my ( $has, $val ) = $get->($v);
299 18 ▶ return _fail("Knoten $v ohne Farbe") unless $has;
Branch: 1 branch
BranchTrueFalseTotal
unless $has 1 17 18
300 17 $c{$v} = $val;
301 }
302 5 for my $v ( @{ $p->{knoten} } ) {
303 11 my $n = _json_num( $c{$v} );
304 11 ▶ return _fail("Knoten $v mit unerlaubter Farbe") unless defined $n && grep { $_ == $n } keys %allowed;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $n and grep {$_ == $n;} keys %allowed 2 9 11
Condition100%3/3
defined $n and grep {$_ == $n;} keys %allowed
!ll&&!rl&&r
119
MC/DC100%2/2
defined $n and grep {$_ == $n;} keys %allowed
defined $n grep {$_ == $n;} keys %allowed
Truth table100%3/3
A and B
A= defined $nB= grep {$_ == $n;} keys %allowed
ABresult
0X0
100
111
305 9 $c{$v} = $n;
306 }
307 3 for my $e ( @{ $p->{kanten} } ) {
308 5 my ( $u, $v ) = @$e;
309 5 ▶ return _fail("Kante $u-$v gleich gefärbt") if $c{$u} == $c{$v};
Branch: 1 branch
BranchTrueFalseTotal
if $c{$u} == $c{$v} 1 4 5
310 }
311 2 return ( 1, 'gültige Färbung' );
312 }
313  
314 39 my %WEEKDAY_TASK = map { $_ => 1 } qw( weekday_offset weekday_of_date weekday_of_date_from_first_date );
315 39 my %NUMBER_TASK = map { $_ => 1 } qw( count_business_days count_days recurring_event_day );
316  
317 10 ▶sub _calendar {
Subroutine _calendar: called CC=10 SCAR=23.1
318 10 my ( $p, $ans ) = @_;
319 10 ▶ return _fail('leer') unless length $ans;
Branch: 1 branch
BranchTrueFalseTotal
unless length $ans 0 10 10
320 10 my ( $form, $o ) = ( $p->{form}, "$p->{antwort}" );
321 10 ▶ if ( $WEEKDAY_TASK{$form} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($WEEKDAY_TASK{$form}) 2 8 10
322 2 ▶ return _strip($ans) eq $o ? ( 1, 'gleich' ) : _fail('anderer Wochentag oder Form');
Branch: 1 branch
BranchTrueFalseTotal
_strip($ans) eq $o ? : 1 1 2
323 }
324 8 ▶ if ( $NUMBER_TASK{$form} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($NUMBER_TASK{$form}) 5 3 8
325 5 my ( $x, $y ) = ( _pyint($ans), _pyint($o) );
326 5 ▶ return _fail('keine ganze Zahl') unless defined $x && defined $y;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $x and defined $y 1 4 5
Condition66%2/3
defined $x and defined $y
!ll&&!rl&&r
104
MC/DC50%1/2
defined $x and defined $y
defined $x defined $y
Truth table66%2/3
A and B
A= defined $xB= defined $y
ABresult
0X0
100
111
327 4 ▶ return $x eq $y ? ( 1, 'gleiche Zahl' ) : _fail('andere Zahl');
Branch: 1 branch
BranchTrueFalseTotal
$x eq $y ? : 3 1 4
328 }
329 3 ▶ if ( $form eq 'is_leap_year' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($form eq "is_leap_year") 2 1 3
330 2 ▶ return lc( _strip($ans) ) eq lc($o) ? ( 1, 'gleich' ) : _fail('nicht gleich');
Branch: 1 branch
BranchTrueFalseTotal
lc _strip($ans) eq lc $o ? : 1 1 2
331 }
332 1 return _fail("unbekannte Form $form");
333 }
334  
335 49 ▶sub _need_int { my $v = _pyint( $_[0] ); die "keine ganze Zahl\n" unless defined $v; return 0 + $v }
Subroutine _need_int: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 1 48 49
336  
337 11 ▶sub _parse_time {
Subroutine _parse_time: called CC=5 SCAR=16.1
338 11 my ($t) = @_;
339 11 my @parts = split /:/, _strip($t), -1;
340 11 ▶ die "Zeit ohne Minuten\n" unless @parts >= 2;
Branch: 1 branch
BranchTrueFalseTotal
unless @parts >= 2 1 10 11
341 10 my $s = _need_int( $parts[0] ) * 3600 + _need_int( $parts[1] ) * 60;
342 10 ▶ if ( @parts > 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@parts > 2) 5 5 10
343 5 ▶ if ( index( $parts[2], '.' ) >= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($parts[2], '.') != -1) { } 4 1 5
344 4 my @q = split /\./, $parts[2], -1;
345 4 ▶ die "Sekunden mit mehr als einem Punkt\n" unless @q == 2;
Branch: 1 branch
BranchTrueFalseTotal
unless @q == 2 0 4 4
346 4 $s += _need_int( $q[0] ) + _need_int( $q[1] ) / 1000;
347 }
348 1 else { $s += _need_int( $parts[2] ) }
349 }
350 10 return $s;
351 }
352  
353 5 ▶sub _parse_datetime {
Subroutine _parse_datetime: called CC=4 SCAR=14.0
354 5 my ($t) = @_;
355 5 my $days = _need_int( ( split /\Q days,\E/, $t, -1 )[0] );
356 4 my @c = split /,/, $t, -1;
357 4 ▶ die "ohne Komma\n" unless @c > 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @c > 1 0 4 4
358 4 my @parts = split /:/, _strip( $c[1] ), -1;
359 4 ▶ die "Zeit ohne Minuten\n" unless @parts >= 2;
Branch: 1 branch
BranchTrueFalseTotal
unless @parts >= 2 0 4 4
360 4 my $s = _need_int( $parts[0] ) * 3600 + _need_int( $parts[1] ) * 60;
361 4 ▶ $s += _need_int( $parts[2] ) if @parts > 2;
Branch: 1 branch
BranchTrueFalseTotal
if @parts > 2 3 1 4
362 4 return $days * 86400 + $s;
363 }
364  
365 11 ▶sub _time_intervals {
Subroutine _time_intervals: called CC=4 SCAR=13.9
366 11 my ( $p, $ans ) = @_;
367 11 my ( $form, $o ) = ( $p->{form}, "$p->{antwort}" );
368 11 my ( $x, $y );
369 11 ▶ if ( $form eq 'date' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($form eq 'date') { } 2 9 11
elsif ($form =~ /\Atime/) { } 6 3 9
370 2 ▶ my $first = sub { my @w = split ' ', _strip( $_[0] ); die "leer\n" unless @w; _need_int( $w[0] ) };
Subroutine __ANON__: called CC=2 SCAR=7.0
Branch: 1 branch
BranchTrueFalseTotal
unless @w 0 4 4
371 2 ( $x, $y ) = ( $first->($ans), $first->($o) );
372 }
373 6 elsif ( $form =~ /\Atime/ ) { ( $x, $y ) = ( _parse_time($ans), _parse_time($o) ) }
374 3 else { ( $x, $y ) = ( _parse_datetime($ans), _parse_datetime($o) ) }
375 9 ▶ return $x == $y ? ( 1, 'gleiche Dauer' ) : _fail( 'andere Dauer (' . ( $x - $y ) . ')' );
Branch: 1 branch
BranchTrueFalseTotal
$x == $y ? : 6 3 9
376 }
377  
378 9 ▶sub _cryptarithm {
Subroutine _cryptarithm: called CC=19 SCAR=29.6
379 9 my ( $p, $ans ) = @_;
380 9 my %map;
381 9 for my $pair ( split /,/, $ans, -1 ) {
382 33 my @lr = split /=/, _strip($pair), -1;
383 33 ▶ @lr = ('') unless @lr;
Branch: 1 branch
BranchTrueFalseTotal
unless @lr 0 33 33
384 33 ▶ return _fail("nicht lesbar: '$pair'") unless @lr == 2;
Branch: 1 branch
BranchTrueFalseTotal
unless @lr == 2 1 32 33
385 32 my $d = _pyint( $lr[1] );
386 32 ▶ return _fail("keine Zahl: '$lr[1]'") unless defined $d;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $d 0 32 32
387 32 $map{ _strip( $lr[0] ) } = 0 + $d;
388 }
389 8 my @words = @{ $p->{woerter} };
390 8 my $res = $p->{ergebnis};
391 8 my %need = map { $_ => 1 } map { split // } @words, $res;
392 8 ▶ for ( sort keys %need ) { return _fail("Buchstabe $_ fehlt") unless exists $map{$_} }
Branch: 1 branch
BranchTrueFalseTotal
unless exists $map{$_} 1 31 32
393 7 ▶ for ( sort keys %map ) { return _fail("Buchstabe $_ zu viel") unless $need{$_} }
Branch: 1 branch
BranchTrueFalseTotal
unless $need{$_} 1 28 29
394 6 ▶ for ( sort keys %map ) { return _fail("Ziffer für $_ ungültig") unless $map{$_} >= 0 && $map{$_} <= 9 }
Branch: 1 branch
BranchTrueFalseTotal
unless $map{$_} >= 0 and $map{$_} <= 9 0 24 24
Condition33%1/3
$map{$_} >= 0 and $map{$_} <= 9
!ll&&!rl&&r
0024
MC/DC0%0/2
$map{$_} >= 0 and $map{$_} <= 9
$map{$_} >= 0 $map{$_} <= 9
Truth table33%1/3
A and B
A= $map{$_} >= 0B= $map{$_} <= 9
ABresult
0X0
100
111
395 6 my %seen;
396 6 ▶ return _fail('Ziffer doppelt') if grep { $seen{$_}++ } values %map;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$seen{$_}++;} values %map 1 5 6
397 5 ▶ unless ( $p->{fuehrende_null} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($$p{"fuehrende_null"}) 4 1 5
398 4 for my $w ( @words, $res ) {
399 10 ▶ return _fail( 'führende Null bei ' . substr( $w, 0, 1 ) ) if length $w && $map{ substr( $w, 0, 1 ) } == 0;
Branch: 1 branch
BranchTrueFalseTotal
if length $w and $map{substr($w, 0, 1)} == 0 1 9 10
Condition66%2/3
length $w and $map{substr($w, 0, 1)} == 0
!ll&&!rl&&r
091
MC/DC50%1/2
length $w and $map{substr($w, 0, 1)} == 0
length $w $map{substr($w, 0, 1)} == 0
Truth table66%2/3
A and B
A= length $wB= $map{substr($w, 0, 1)} == 0
ABresult
0X0
100
111
400 }
401 }
402 4 ▶ my $num = sub { Math::BigInt->new( join '', map { $map{$_} } split //, $_[0] ) };
Subroutine __ANON__: called CC=1 SCAR=0.0
403 4 my $sum = Math::BigInt->new(0);
404 4 $sum += $num->($_) for @words;
405 4 ▶ return $sum == $num->($res) ? ( 1, 'Gleichung stimmt' ) : _fail('Summe stimmt nicht');
Branch: 1 branch
BranchTrueFalseTotal
$sum == &$num($res) ? : 3 1 4
406 }
407  
408 5 ▶sub _mini_sudoku {
Subroutine _mini_sudoku: called CC=4 SCAR=13.9
409 5 my ( $p, $ans ) = @_;
410 5 ▶ return _fail('leer') unless length $ans;
Branch: 1 branch
BranchTrueFalseTotal
unless length $ans 1 4 5
411 4 my $o = "$p->{antwort}";
412 4 ▶ my $norm = sub { join "\n", map { s/\s+\z//r } split /\n/, $_[0], -1 };
Subroutine __ANON__: called CC=1 SCAR=0.0
413 4 ▶ return _fail('anderes Gitter oder andere Form') unless $norm->($ans) eq $norm->($o);
Branch: 1 branch
BranchTrueFalseTotal
unless &$norm($ans) eq &$norm($o) 1 3 4
414 3 ▶ return _fail('länger als die Lösung') if length $ans > length $o;
Branch: 1 branch
BranchTrueFalseTotal
if length $ans > length $o 1 2 3
415 2 return ( 1, 'gleich' );
416 }
417  
418 8 ▶sub _jugs {
Subroutine _jugs: called CC=19 SCAR=29.8
419 8 my ( $p, $ans ) = @_;
420 8 my $moves = eval { $JSON->decode($ans) };
421 8 ▶ return _fail('kein JSON') if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 1 7 8
422 7 ▶ return _fail('keine Liste von Zügen') unless ref $moves eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $moves eq "ARRAY" 1 6 7
423 6 my @cap = @{ $p->{kruege} };
424 6 my %idx = map { ( chr( 65 + $_ ) => $_ ) } 0 .. $#cap;
425 6 my @s = (0) x @cap;
426 6 for my $m (@$moves) {
427 18 ▶ return _fail('Zug ist kein Text') if !defined $m || ref $m;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $m or ref $m 0 18 18
Condition33%1/3
not defined $m or ref $m
l!l&&r!l&&!r
0018
MC/DC0%0/2
not defined $m or ref $m
not defined $m ref $m
Truth table33%1/3
not(A) or B
A= not defined $mB= ref $m
ABresult
1X1
011
000
428 18 my @t = split ' ', $m;
429 18 ▶ return _fail('leerer Zug') unless @t;
Branch: 1 branch
BranchTrueFalseTotal
unless @t 0 18 18
430 18 ▶ if ( $t[0] eq 'fill' || $t[0] eq 'empty' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($t[0] eq 'fill' or $t[0] eq 'empty') { } 9 9 18
elsif ($t[0] eq 'pour') { } 8 1 9
Condition100%3/3
$t[0] eq "fill" or $t[0] eq "empty"
l!l&&r!l&&!r
729
MC/DC100%2/2
$t[0] eq "fill" or $t[0] eq "empty"
$t[0] eq "fill" $t[0] eq "empty"
Truth table100%3/3
A or B
A= $t[0] eq "fill"B= $t[0] eq "empty"
ABresult
1X1
011
000
431 9 ▶ return _fail("unbekannter Krug in '$m'") unless defined $t[1] && exists $idx{ $t[1] };
Branch: 1 branch
BranchTrueFalseTotal
unless defined $t[1] and exists $idx{$t[1]} 1 8 9
Condition66%2/3
defined $t[1] and exists $idx{$t[1]}
!ll&&!rl&&r
018
MC/DC50%1/2
defined $t[1] and exists $idx{$t[1]}
defined $t[1] exists $idx{$t[1]}
Truth table66%2/3
A and B
A= defined $t[1]B= exists $idx{$t[1]}
ABresult
0X0
100
111
432 8 ▶ $s[ $idx{ $t[1] } ] = $t[0] eq 'fill' ? $cap[ $idx{ $t[1] } ] : 0;
Branch: 1 branch
BranchTrueFalseTotal
$t[0] eq 'fill' ? : 6 2 8
433 }
434 elsif ( $t[0] eq 'pour' ) {
435 8 ▶ return _fail("unvollständig: '$m'") unless defined $t[1];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $t[1] 0 8 8
436 8 my @xy = split /->/, $t[1], -1;
437 8 ▶ return _fail("Form X->Y erwartet: '$m'") unless @xy == 2 && exists $idx{ $xy[0] } && exists $idx{ $xy[1] };
Branch: 1 branch
BranchTrueFalseTotal
unless @xy == 2 and exists $idx{$xy[0]} and exists $idx{$xy[1]} 1 7 8
Condition66%4/6
@xy == 2 and exists $idx{$xy[0]} and exists $idx{$xy[1]}
!ll&&!rl&&r
107
@xy == 2 and exists $idx{$xy[0]}
!ll&&!rl&&r
107
MC/DC33%1/3
@xy == 2 and exists $idx{$xy[0]} and exists $idx{$xy[1]}
@xy == 2 exists $idx{$xy[0]} exists $idx{$xy[1]}
Truth table50%2/4
A and B and C
A= @xy == 2B= exists $idx{$xy[0]}C= exists $idx{$xy[1]}
ABCresult
0XX0
10X0
1100
1111
438 7 my ( $i, $j ) = @idx{@xy};
439 7 ▶ my $amt = $s[$i] < $cap[$j] - $s[$j] ? $s[$i] : $cap[$j] - $s[$j];
Branch: 1 branch
BranchTrueFalseTotal
$s[$i] < $cap[$j] - $s[$j] ? : 4 3 7
440 7 $s[$i] -= $amt;
441 7 $s[$j] += $amt;
442 }
443 1 else { return _fail("unbekannter Zug '$m'") }
444 }
445 3 ▶ return ( grep { $_ == $p->{ziel} } @s ) ? ( 1, "Ziel erreicht (@s)" ) : _fail("Ziel nicht erreicht (@s)");
Branch: 1 branch
BranchTrueFalseTotal
grep({$_ == $$p{'ziel'};} @s) ? : 2 1 3
446 }
447  
448 # --- Countdown: arithmetischer Ausdruck wie Python (Präzedenz, // % **, n!) ---
449  
450 15 ▶sub _cd_tokens {
Subroutine _cd_tokens: called CC=9 SCAR=22.0
451 15 my ($s) = @_;
452 15 my ( @t, $depth );
453 15 $depth = 0;
454 15 pos($s) = 0;
455 15 ▶ while ( pos($s) < length $s ) {
Branch: 1 branch
BranchTrueFalseTotal
if (pos $s < length $s) 93 13 106
456 93 ▶ if ( $s =~ /\G([ \t\f]+|\n)/gc ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s =~ /\G([ \t\f]+|\n)/cg) 5 88 93
457 5 ▶ die "Zeilenumbruch ausserhalb von Klammern\n" if $1 eq "\n" && !$depth;
Branch: 1 branch
BranchTrueFalseTotal
if $1 eq "\n" and not $depth 0 5 5
Condition33%1/3
$1 eq "\n" and not $depth
!ll&&!rl&&r
500
MC/DC0%0/2
$1 eq "\n" and not $depth
$1 eq "\n" not $depth
Truth table33%1/3
A and not(B)
A= $1 eq "\n"B= not $depth
ABresult
0X0
100
111
458 5 next;
459 }
460 88 ▶ if ( $s =~ /\G(\d[\d_]*(?:\.[\d_]*)?(?:[eE][+-]?\d[\d_]*)?|\.\d[\d_]*(?:[eE][+-]?\d[\d_]*)?)/agc ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s =~ /\G(\d[\d_]*(?:\.[\d_]*)?(?:[eE][+-]?\d[\d_]*)?|\.\d[\d_]*(?:[eE][+-]?\d[\d_]*)?)/acg) 41 47 88
461 41 push @t, [ num => $1 ];
462 41 next;
463 }
464 47 ▶ if ( $s =~ m{\G(\*\*|//|[-+*/%()!])}gc ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s =~ m[\G(\*\*|//|[-+*/%()!])]cg) 45 2 47
465 45 ▶ $depth++ if $1 eq '(';
Branch: 1 branch
BranchTrueFalseTotal
if $1 eq "(" 9 36 45
466 45 ▶ $depth-- if $1 eq ')';
Branch: 1 branch
BranchTrueFalseTotal
if $1 eq ")" 8 37 45
467 45 push @t, [ op => $1 ];
468 45 next;
469 }
470 2 die "Zeichen nicht erlaubt\n";
471 }
472 13 return \@t;
473 }
474  
475 37 ▶sub _cd_number {
Subroutine _cd_number: called CC=13 SCAR=46.6
476 37 my ($lit) = @_;
477 37 my $d = qr/\d(?:_?\d)*/a;
478 37 ▶ if ( $lit =~ /\A$d\z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lit =~ /\A$d\z/) 37 0 37
479 37 ▶ die "führende Null\n" if $lit =~ /\A0/ && $lit =~ /[1-9]/;
Branch: 1 branch
BranchTrueFalseTotal
if $lit =~ /\A0/ and $lit =~ /[1-9]/ 0 37 37
Condition33%1/3
$lit =~ /\A0/ and $lit =~ /[1-9]/
!ll&&!rl&&r
3700
MC/DC0%0/2
$lit =~ /\A0/ and $lit =~ /[1-9]/
$lit =~ /\A0/ $lit =~ /[1-9]/
Truth table33%1/3
A and B
A= $lit =~ /\A0/B= $lit =~ /[1-9]/
ABresult
0X0
100
111
480 37 return Math::BigRat->new( $lit =~ tr/_//dr );
481 }
482 0 ▶ die "Zahl nicht lesbar\n" unless $lit =~ /\A(?:$d)?(?:\.(?:$d)?)?(?:[eE][+-]?$d)?\z/ && $lit =~ /\d/;
Branch: 1 branch
BranchTrueFalseTotal
unless $lit =~ /\A(?:$d)?(?:\.(?:$d)?)?(?:[eE][+-]?$d)?\z/ and $lit =~ /\d/ 0 0 0
Condition0%0/3
$lit =~ /\A(?:$d)?(?:\.(?:$d)?)?(?:[eE][+-]?$d)?\z/ and $lit =~ /\d/
!ll&&!rl&&r
000
MC/DC0%0/2
$lit =~ /\A(?:$d)?(?:\.(?:$d)?)?(?:[eE][+-]?$d)?\z/ and $lit =~ /\d/
$lit =~ /\A(?:$d)?(?:\.(?:$d)?)?(?:[eE][+-]?$d)?\z/ $lit =~ /\d/
Truth table0%0/3
A and B
A= $lit =~ /\A(?:$d)?(?:\.(?:$d)?)?(?:[eE][+-]?$d)?\z/B= $lit =~ /\d/
ABresult
0X0
100
111
483 0 ( my $x = $lit ) =~ tr/_//d;
484 0 my ( $mant, $exp ) = split /[eE]/, $x;
485 0 ▶ $mant = "0$mant" if $mant =~ /\A\./;
Branch: 1 branch
BranchTrueFalseTotal
if $mant =~ /\A\./ 0 0 0
486 0 ▶ $mant .= '0' if $mant =~ /\.\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $mant =~ /\.\z/ 0 0 0
487 0 my ( $i, $f ) = split /\./, $mant;
488 0 ▶ $f //= '';
Condition0%0/2
$f //= ""
l!l
00
MC/DC0%0/1
$f //= ""
$f
Truth table0%0/2
A
A= $f
Aresult
11
00
489 0 my $r = Math::BigRat->new( ( $i . $f ) =~ s/\A0+(?=\d)//r ) / Math::BigRat->new( '1' . ( '0' x length $f ) );
490 0 ▶ $exp //= 0;
Condition0%0/2
$exp //= 0
l!l
00
MC/DC0%0/1
$exp //= 0
$exp
Truth table0%0/2
A
A= $exp
Aresult
11
00
491 0 ▶ die "Exponent zu gross\n" if abs($exp) > 400;
Branch: 1 branch
BranchTrueFalseTotal
if abs $exp > 400 0 0 0
492 0 ▶ $r *= Math::BigRat->new(10)**$exp if $exp > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $exp > 0 0 0 0
493 0 ▶ $r /= Math::BigRat->new(10)**( -$exp ) if $exp < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $exp < 0 0 0 0
494 0 return $r;
495 }
496  
497 13 ▶sub _cd_parse {
Subroutine _cd_parse: called CC=2 SCAR=7.1
498 13 my ($t) = @_;
499 13 my $i = 0;
500 13 ▶ my $peek = sub { $i < @$t && $t->[$i][0] eq 'op' ? $t->[$i][1] : '' };
Subroutine __ANON__: called CC=3 SCAR=11.0
Branch: 1 branch
BranchTrueFalseTotal
$i < @$t && $$t[$i][0] eq 'op' ? : 124 81 205
Condition100%3/3
$i < @$t and $$t[$i][0] eq "op"
!ll&&!rl&&r
4437124
MC/DC100%2/2
$i < @$t and $$t[$i][0] eq "op"
$i < @$t $$t[$i][0] eq "op"
Truth table100%3/3
A and B
A= $i < @$tB= $$t[$i][0] eq "op"
ABresult
0X0
100
111
501 13 my ( $expr, $term, $factor, $power, $atom );
502 21 ▶ $expr = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
503 21 my $v = $term->();
504 18 ▶ while ( ( my $op = $peek->() ) =~ /\A[-+]\z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ((my $op = &$peek()) =~ /\A[-+]\z/) 13 18 31
505 13 $i++;
506 13 my $r = $term->();
507 13 ▶ $v = $op eq '+' ? $v + $r : $v - $r;
Branch: 1 branch
BranchTrueFalseTotal
$op eq '+' ? : 12 1 13
508 }
509 18 return $v;
510 13 };
511 34 ▶ $term = sub {
Subroutine __ANON__: called CC=6 SCAR=18.0
512 34 my $v = $factor->();
513 32 ▶ while ( ( my $op = $peek->() ) =~ m{\A(?:\*|/|//|%)\z} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ((my $op = &$peek()) =~ m[\A(?:\*|/|//|%)\z]) 9 31 40
514 9 $i++;
515 9 my $r = $factor->();
516 9 ▶ if ( $op eq '*' ) { $v = $v * $r; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($op eq "*") 6 3 9
517 3 ▶ die "Division durch null\n" if $r->is_zero;
Branch: 1 branch
BranchTrueFalseTotal
if $r->is_zero 1 2 3
518 2 ▶ if ( $op eq '/' ) { $v = $v / $r }
Branch: 2 branches
BranchTrueFalseTotal
if ($op eq '/') { } 1 1 2
elsif ($op eq '//') { } 1 0 1
519 1 elsif ( $op eq '//' ) { $v = ( $v / $r )->bfloor }
520 0 else { $v = $v - $r * ( $v / $r )->bfloor }
521 }
522 31 return $v;
523 13 };
524 45 ▶ $factor = sub {
Subroutine __ANON__: called CC=4 SCAR=20.4
525 45 my $op = $peek->();
526 0 ▶ if ( $op eq '-' || $op eq '+' ) { $i++; my $v = $factor->(); return $op eq '-' ? -$v : $v }
Branch: 2 branches
BranchTrueFalseTotal
if ($op eq "-" or $op eq "+") 0 45 45
$op eq '-' ? : 0 0 0
Condition33%1/3
$op eq "-" or $op eq "+"
l!l&&r!l&&!r
0045
MC/DC0%0/2
$op eq "-" or $op eq "+"
$op eq "-" $op eq "+"
Truth table33%1/3
A or B
A= $op eq "-"B= $op eq "+"
ABresult
1X1
011
000
527 45 return $power->();
528 13 };
529 45 ▶ $power = sub {
Subroutine __ANON__: called CC=7 SCAR=20.2
530 45 my $v = $atom->();
531 44 ▶ if ( $peek->() eq '**' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&$peek() eq "**") 2 42 44
532 2 $i++;
533 2 my $e = $factor->();
534 2 ▶ die "Exponent nicht ganzzahlig\n" unless $e->is_int;
Branch: 1 branch
BranchTrueFalseTotal
unless $e->is_int 0 2 2
535 2 ▶ die "Exponent zu gross\n" if abs($e) > 64;
Branch: 1 branch
BranchTrueFalseTotal
if abs $e > 64 1 1 2
536 1 ▶ die "0 hoch negativ\n" if $v->is_zero && $e < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $v->is_zero and $e < 0 0 1 1
Condition33%1/3
$v->is_zero and $e < 0
!ll&&!rl&&r
100
MC/DC0%0/2
$v->is_zero and $e < 0
$v->is_zero $e < 0
Truth table33%1/3
A and B
A= $v->is_zeroB= $e < 0
ABresult
0X0
100
111
537 1 ▶ $v = $e < 0 ? Math::BigRat->new(1) / ( $v->copy**( -$e ) ) : $v->copy**$e;
Branch: 1 branch
BranchTrueFalseTotal
$e < 0 ? : 0 1 1
538 }
539 43 return $v;
540 13 };
541 45 ▶ $atom = sub {
Subroutine __ANON__: called CC=9 SCAR=22.5
542 45 ▶ die "Ausdruck endet zu früh\n" unless $i < @$t;
Branch: 1 branch
BranchTrueFalseTotal
unless $i < @$t 0 45 45
543 45 my $tok = $t->[ $i++ ];
544 45 my $v;
545 45 ▶ if ( $tok->[0] eq 'num' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$tok[0] eq "num") 37 8 45
546 37 $v = _cd_number( $tok->[1] );
547 37 ▶ if ( $peek->() eq '!' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&$peek() eq "!") 1 36 37
548 1 $i++;
549 1 ▶ die "Fakultät nur von ganzen Zahlen 0..20\n" unless $tok->[1] =~ /\A\d+\z/a && $v <= 20;
Branch: 1 branch
BranchTrueFalseTotal
unless $$tok[1] =~ /\A\d+\z/a and $v <= 20 0 1 1
Condition33%1/3
$$tok[1] =~ /\A\d+\z/a and $v <= 20
!ll&&!rl&&r
001
MC/DC0%0/2
$$tok[1] =~ /\A\d+\z/a and $v <= 20
$$tok[1] =~ /\A\d+\z/a $v <= 20
Truth table33%1/3
A and B
A= $$tok[1] =~ /\A\d+\z/aB= $v <= 20
ABresult
0X0
100
111
550 1 my $f = Math::BigRat->new(1);
551 1 $f *= $_ for 2 .. $v->numify;
552 1 $v = $f;
553 }
554 37 return $v;
555 }
556 8 ▶ if ( $tok->[1] eq '(' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$tok[1] eq "(") 8 0 8
557 8 $v = $expr->();
558 8 ▶ die "Klammer nicht geschlossen\n" unless $peek->() eq ')';
Branch: 1 branch
BranchTrueFalseTotal
unless &$peek() eq ")" 1 7 8
559 7 $i++;
560 7 return $v;
561 }
562 0 die "unerwartet: $tok->[1]\n";
563 13 };
564 13 my $v = $expr->();
565 10 ▶ die "unerwartet am Ende\n" if $i < @$t;
Branch: 1 branch
BranchTrueFalseTotal
if $i < @$t 0 10 10
566 10 return $v;
567 }
568  
569 16 ▶sub _countdown {
Subroutine _countdown: called CC=5 SCAR=16.1
570 16 my ( $p, $ans ) = @_;
571 16 ▶ return _fail('leer') unless length _strip($ans);
Branch: 1 branch
BranchTrueFalseTotal
unless length _strip($ans) 1 15 16
572 15 my $v = eval { _cd_parse( _cd_tokens( _strip($ans) ) ) };
573 15 ▶ return _fail( 'nicht auswertbar: ' . ( "$@" =~ s/\s+\z//r ) ) unless defined $v;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 5 10 15
574 10 my @used = sort { $a <=> $b } map { 0 + $_ } $ans =~ /\b(\d+)\b/ag;
575 10 my @want = sort { $a <=> $b } @{ $p->{zahlen} };
576 10 ▶ return _fail('nicht genau die gegebenen Zahlen') unless "@used" eq "@want";
Branch: 1 branch
BranchTrueFalseTotal
unless join($", @used) eq join($", @want) 2 8 10
577 8 my $t = Math::BigRat->new( $p->{ziel} );
578 8 my $tol = ( Math::BigRat->new(1) + Math::BigRat->new(10) * abs($t) ) / Math::BigRat->new(1_000_000);
579 8 ▶ return abs( $v - $t ) <= $tol ? ( 1, 'ergibt das Ziel' ) : _fail( 'ergibt ' . $v->numify );
Branch: 1 branch
BranchTrueFalseTotal
abs $v - $t <= $tol ? : 6 2 8
580 }
581  
582 39 __PACKAGE__->meta->make_immutable;
583  
584 __END__
« blib/lib/Langertha/Holmgang/Runner.pm ↑ Summary blib/lib/Langertha/Holmgang/CLI.pm »