File Coverage

blib/lib/AI/Pathfinding/SMAstar/PairObj.pm
Criterion Covered Total %
statement 18 42 42.8
branch 4 18 22.2
condition 1 3 33.3
subroutine 5 9 55.5
pod 0 8 0.0
total 28 80 35.0


line stmt bran cond sub pod time code
1             package AI::Pathfinding::SMAstar::PairObj;
2 1     1   5 use strict;
  1         2  
  1         565  
3              
4             ##################################################
5             # PairObj constructor
6             ##################################################
7             sub new {
8 25     25 0 31 my $invocant = shift;
9 25   33     80 my $class = ref($invocant) || $invocant;
10 25         78 my $self = {
11             _key => undef,
12             _value => undef,
13             @_, # Override previous attributes
14             };
15 25         87 return bless $self, $class;
16             }
17              
18             ##############################################
19             # accessors
20             ##############################################
21             sub value {
22 0     0 0 0 my $self = shift;
23 0 0       0 if (@_) { $self->{_value} = shift }
  0         0  
24 0         0 return $self->{_value};
25             }
26              
27             sub val {
28 0     0 0 0 my $self = shift;
29 0 0       0 if (@_) { $self->{_value} = shift }
  0         0  
30 0         0 return $self->{_value};
31             }
32              
33             sub key {
34 26     26 0 251 my $self = shift;
35 26 50       54 if (@_) { $self->{_key} = shift }
  0         0  
36 26         55 return $self->{_key};
37             }
38              
39              
40              
41              
42             # compare_vals
43             #
44             # usage: $pair_obj->compare($other_pair_obj)
45             #
46             # Accepts another PairObj object as an argument.
47             # Returns 1 if greater than argument, 0 if equal, and -1 if
48             # less than argument
49             sub compare_vals{
50 0     0 0 0 my ($self,$arg_obj) = @_;
51            
52 0         0 my $arg_value = $arg_obj->{_value};
53 0         0 my $value = $self->{_value};
54            
55 0 0       0 if($arg_value gt $value){
    0          
56 0         0 return -1;
57             }
58             elsif($arg_value eq $value){
59 0         0 return 0;
60             }
61 0         0 return 1;
62             }
63              
64              
65             # compare_keys
66             #
67             # usage: $pair_obj->compare($other_pair_obj)
68             #
69             # Accepts another PairObj object as an argument.
70             # Returns 1 if greater than argument, 0 if equal, and -1 if
71             # less than argument
72             sub compare_keys{
73 0     0 0 0 my ($self,$arg_obj) = @_;
74            
75 0         0 my $arg_key = $arg_obj->{_key};
76 0         0 my $key = $self->{_key};
77            
78 0 0       0 if($arg_key gt $key){
    0          
79 0         0 return -1;
80             }
81             elsif($arg_key eq $key){
82 0         0 return 0;
83             }
84 0         0 return 1;
85             }
86              
87              
88             sub compare_keys_numeric{
89 10     10 0 33 my ($self,$arg_obj) = @_;
90            
91 10         16 my $arg_key = $arg_obj->{_key};
92 10         9 my $key = $self->{_key};
93            
94 10 100       27 if($arg_key > $key){
    50          
95 1         2 return -1;
96             }
97             elsif($self->fp_equal($arg_key, $key, 10)){
98 9         22 return 0;
99             }
100 0         0 return 1;
101             }
102              
103              
104              
105              
106             sub fp_equal {
107 9     9 0 14 my ($self, $A, $B, $dp) = @_;
108              
109 9         63 return sprintf("%.${dp}g", $A) eq sprintf("%.${dp}g", $B);
110             }
111              
112              
113              
114              
115             1; # so the require or use succeeds
116