File Coverage

inc/Test/Deep/Cache/Simple.pm
Criterion Covered Total %
statement 34 41 82.9
branch 2 4 50.0
condition 1 3 33.3
subroutine 9 10 90.0
pod 0 5 0.0
total 46 63 73.0


line stmt bran cond sub pod time code
1 2     2   8 #line 1
  2         4  
  2         57  
2 2     2   9 use strict;
  2         2  
  2         60  
3             use warnings;
4              
5 2     2   9 package Test::Deep::Cache::Simple;
  2         3  
  2         82  
6             use Carp qw( confess );
7 2     2   10  
  2         3  
  2         335  
8             use Scalar::Util qw( refaddr );
9              
10             BEGIN
11 2 50   2   10 {
12             if (grep /^weaken$/, @Scalar::Util::EXPORT_FAIL)
13             {
14             # we're running on a version of perl that has no weak refs, so we
15 0         0 # just install a no-op sub for weaken instead of importing it
  0         0  
16             *weaken = sub {};
17             }
18             else
19 2         946 {
20             Scalar::Util->import('weaken');
21             }
22             }
23              
24             sub new
25 24     24 0 33 {
26             my $pkg = shift;
27 24         62  
28             my $self = bless {}, $pkg;
29 24         97  
30             return $self;
31             }
32              
33             sub add
34 60     60 0 68 {
35             my $self = shift;
36 60         77  
37             my ($d1, $d2) = @_;
38 60         78 {
  60         191  
39             local $SIG{__DIE__};
40              
41             # cannot weaken read only refs, no harm if we can't as they never
42 60         69 # disappear
  60         119  
43 60         59 eval{weaken($d1)};
  60         192  
44             eval{weaken($d2)};
45             }
46 60         156  
47             $self->{fn_get_key(@_)} = [$d1, $d2];
48             }
49              
50             sub cmp
51 120     120 0 129 {
52             my $self = shift;
53 120         195  
54 120         209 my $key = fn_get_key(@_);
55             my $pair = $self->{$key};
56              
57 120 50 33     330 # are both weakened refs still valid, if not delete this entry
58             if (ref($pair->[0]) and ref($pair->[1]))
59 0         0 {
60             return 1;
61             }
62             else
63 120         143 {
64 120         484 delete $self->{$key};
65             return 0;
66             }
67             }
68              
69             sub absorb
70 0     0 0 0 {
71             my $self = shift;
72 0         0  
73             my $other = shift;
74 0         0  
  0         0  
75             @{$self}{keys %$other} = values %$other;
76             }
77              
78             sub fn_get_key
79 180     180 0 262 {
  360         2132  
80             return join(",", sort (map {refaddr($_)} @_));
81             }
82             1;