File Coverage

blib/lib/Math/Logic/Ternary/Calculator/Mode.pm
Criterion Covered Total %
statement 32 43 74.4
branch 2 4 50.0
condition n/a
subroutine 15 22 68.1
pod 0 14 0.0
total 49 83 59.0


line stmt bran cond sub pod time code
1             # Copyright (c) 2012-2017 Martin Becker, Blaubeuren. All rights reserved.
2             # This package is free software; you can redistribute it and/or modify it
3             # under the same terms as Perl itself.
4              
5             package Math::Logic::Ternary::Calculator::Mode;
6              
7 6     6   51660 use 5.008;
  6         20  
8 6     6   27 use strict;
  6         19  
  6         111  
9 6     6   24 use warnings;
  6         10  
  6         178  
10 6     6   26 use Carp qw(croak);
  6         11  
  6         406  
11              
12             our $VERSION = '0.004';
13              
14 6     6   38 use constant _NAME => 0;
  6         10  
  6         393  
15 6     6   28 use constant _ORDINAL => 1;
  6         12  
  6         259  
16 6     6   36 use constant _SUFFIX => 2;
  6         12  
  6         229  
17 6     6   29 use constant _LSUFFIX => 3;
  6         11  
  6         2483  
18              
19             my @modes =
20             my ($balanced, $unbalanced, $negative_base) = map { bless $_ } (
21             ['balanced', 0, '', ''],
22             ['unbalanced', 1, 'u', '_u'],
23             ['base(-3)', 2, 'v', '_v'],
24             );
25             my %from_string = (
26             b => $balanced,
27             map {($_->name => $_, $_->ordinal => $_, $_->suffix => $_)} @modes
28             );
29              
30 3     3 0 8 sub balanced { $balanced }
31 0     0 0 0 sub unbalanced { $unbalanced }
32 0     0 0 0 sub negative_base { $negative_base }
33 6     6 0 19 sub modes { @modes }
34              
35 0     0 0 0 sub from_string { $from_string{$_[1]} }
36              
37 204     204 0 449 sub name { $_[0]->[_NAME] }
38 18     18 0 37 sub ordinal { $_[0]->[_ORDINAL] }
39 202     202 0 362 sub suffix { $_[0]->[_SUFFIX] }
40 184     184 0 330 sub lsuffix { $_[0]->[_LSUFFIX] }
41              
42 0     0 0 0 sub is_equal { $_[0]->[_ORDINAL] == $_[1]->[_ORDINAL] }
43 0     0 0 0 sub is_balanced { !$_[0]->[_ORDINAL] }
44              
45 0 0   0 0 0 sub suffix_for { $_[0]->[(0 <= index $_[1], '_')? _LSUFFIX: _SUFFIX] }
46              
47             sub apply {
48 0     0 0 0 my ($this, $op) = @_;
49 0         0 return $op . $this->suffix_for($op);
50             }
51              
52             sub unapply {
53 184     184 0 291 my ($this, $op) = @_;
54 184         277 foreach my $sfx ($this->lsuffix, $this->suffix) {
55 368 100       3040 return $op if $op =~ s/$sfx\z//;
56             }
57 0           my $sfx = $this->suffix_for($op);
58 0           my $name = $this->name;
59 0           croak qq{$op: does not have suffix "$sfx" matching mode "$name"};
60             }
61              
62             1;
63             __END__