Sim/Dispatcher.pm

filter: Statementstmt 94.9% 56 / 59 Branchbran 79.1% 19 / 24 Conditioncond 100% 8 / 8 MC/DCmcdc 100% 4 / 4 Subroutinesub 100% 11 / 11 Podpod 100% 7 / 7 totaltotal 92.9% 105 / 113 CC 16 SCAR 13.0
CC
16
Cov
91%
run
7.0
schedule
3.2
new
3.1
CRAP 3.7
↑ Summary Sim.pm »
1 package Sim::Dispatcher;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 our $VERSION = '0.03';
7  
8 3 ▶use Carp qw( carp croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 3 our $DEBUG = 0;
11  
12 3 ▶sub new {
Subroutine new: called CC=3 SCAR=11.4
Pod documented
13 3 ▶ my $self = ref $_[0] ? ref shift : shift;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 3 3
14 3 my %opts = @_;
15 3 ▶ croak "No clock given" if !$opts{clock};
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{"clock"} 0 3 3
16 bless {
17 clock => $opts{clock},
18 3 queue => [],
19 }, $self;
20 }
21  
22 84 ▶sub now {
Subroutine now: called CC=1 SCAR=0.0
Pod documented
23 84 $_[0]->{clock}->now;
24 }
25  
26 21 ▶sub schedule {
Subroutine schedule: called CC=3 SCAR=11.6
Pod documented
27 21 my $self = shift;
28 21 my %events = @_;
29 21 ▶ while (my ($time, $handle) = each %events) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %events) 29 21 50
30 29 ▶ if ($time < $self->now) {
Branch: 1 branch
BranchTrueFalseTotal
if ($time < $self->now) 0 29 29
31 0 carp "out-dated event [$time => $handle] ignored";
32 0 next;
33 }
34 29 $self->_insert_event([$time => $handle]);
35 }
36 }
37  
38 29 ▶sub _insert_event {
Subroutine _insert_event: called CC=3 SCAR=11.0
39 29 my ($self, $event) = @_;
40 29 my $queue = $self->{queue};
41 29 ▶ for (my $i = 0; $i < @$queue; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$queue) 15 22 37
42 15 ▶ if ($event->[0] < $queue->[$i]->[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($event->[0] < $queue->[$i][0]) 7 8 15
43 7 splice( @$queue, $i, 0, $event );
44 7 return;
45 }
46 }
47 22 push @$queue, $event;
48 }
49  
50 26 ▶sub fire_next ($) {
Subroutine fire_next: called CC=3 SCAR=11.2
Pod documented
51 26 my $self = shift;
52 26 my $queue = $self->{queue};
53 26 my $clock = $self->{clock};
54 26 ▶ return undef if @$queue == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @$queue == 0 0 26 26
55 26 my $event = shift @$queue;
56 26 my ($time, $handle) = @$event;
57 26 my $now = $self->now;
58 26 ▶ if ($time >= $now) {
Branch: 1 branch
BranchTrueFalseTotal
if ($time >= $now) { } 26 0 26
59 26 $clock->push_to($time);
60 26 $handle->();
61 } else {
62 0 die "Clock modified outside of the dispatcher: next event is at $time while now is $now";
63 }
64 26 return 1;
65 }
66  
67 6 ▶sub run ($@) {
Subroutine run: called CC=7 SCAR=19.5
Pod documented
68 6 my $self = shift;
69 6 my %opts = @_;
70 6 ▶ my $end_time = $self->now + $opts{duration} if defined $opts{duration};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts{"duration"} 3 3 6
71 6 ▶ my $fires = $opts{fires} || 100_000_000;
Condition100%2/2
$opts{'fires'} || 100000000
l!l
33
MC/DC100%1/1
$opts{'fires'} || 100000000
$opts{'fires'}
Truth table100%2/2
A
A= $opts{'fires'}
Aresult
11
00
72 6 my $i = 0;
73 6 while (1) {
74 #warn "run: next!";
75 32 ▶ last if ++$i > $fires;
Branch: 1 branch
BranchTrueFalseTotal
if ++$i > $fires 2 30 32
76 30 my $t = $self->time_of_next;
77 30 ▶ last if !defined $t or (defined $end_time and $t > $end_time);
Branch: 1 branch
BranchTrueFalseTotal
if not defined $t or defined $end_time and $t > $end_time 4 26 30
Condition100%6/6
not defined $t or defined $end_time and $t > $end_time
l!l&&r!l&&!r
3126
defined $end_time and $t > $end_time
!ll&&!rl&&r
15111
MC/DC100%3/3
not defined $t or defined $end_time and $t > $end_time
not defined $t defined $end_time $t > $end_time
Truth table100%4/4
not(A) or B and C
A= not defined $tB= defined $end_timeC= $t > $end_time
ABCresult
1XX1
0111
00X0
0100
78 26 $self->fire_next;
79 }
80 }
81  
82 32 ▶sub time_of_next ($) {
Subroutine time_of_next: called CC=2 SCAR=6.9
Pod documented
83 32 my $self = shift;
84 32 my $queue = $self->{queue};
85 32 ▶ return @$queue ? $queue->[0]->[0] : undef;
Branch: 1 branch
BranchTrueFalseTotal
@$queue ? : 27 5 32
86 }
87  
88 4 ▶sub reset ($) {
Subroutine reset: called CC=1 SCAR=0.0
Pod documented
89 4 my $self = shift;
90 4 $self->{queue} = [];
91 4 $self->{clock}->reset();
92 }
93  
94 3 1;
95 __END__
↑ Summary Sim.pm »