File Coverage

/root/.cpan/build/PDL-CCS-1.23.13-0/t/common.plt
Criterion Covered Total %
statement 44 57 77.1
branch 15 32 46.8
condition 6 18 33.3
subroutine 12 16 75.0
pod n/a
total 77 123 62.6


line stmt bran cond sub pod time code
1             # -*- Mode: CPerl -*-
2             # File: t/common.plt
3             # Description: re-usable test subs; requires Test::More
4 13     13   214942 BEGIN { $| = 1; }
5 13     13   121 use strict;
  13         22  
  13         14496  
6              
7             # isok($label,@_) -- prints helpful label
8             sub isok {
9 4994     4994   213720 my $label = shift;
10 4994 100       13496 if (@_==1) {
    50          
11 2650         7753 ok($_[0],$label);
12             } elsif (@_==2) {
13 2344         7378 is($_[0],$_[1], $label);
14             } else {
15 0         0 die("isok(): expected 1 or 2 non-label arguments, but got ", scalar(@_));
16             }
17             }
18              
19             # skipok($label,$skip_if_true,@_) -- prints helpful label
20             # skipok($label,$skip_if_true,\&CODE) -- prints helpful label
21             sub skipok {
22 2     2   647 my ($label,$skip_if_true) = splice(@_,0,2);
23 2 50       7 if ($skip_if_true) {
24 0     0   0 subtest $label => sub { plan skip_all => $skip_if_true; };
  0         0  
25             } else {
26 2 50 33     18 if (@_==1 && ref($_[0]) && ref($_[0]) eq 'CODE') {
      33        
27 2         5 isok($label, $_[0]->());
28             } else {
29 0         0 isok($label,@_);
30             }
31             }
32             }
33              
34             # skipordo($label,$skip_if_true,sub { ok ... },@args_for_sub)
35             sub skipordo {
36 1     1   5 my ($label,$skip_if_true) = splice(@_,0,2);
37 1 50       3 if ($skip_if_true) {
38 0     0   0 subtest $label => sub { plan skip_all => $skip_if_true; };
  0         0  
39             } else {
40 1         5 $_[0]->(@_[1..$#_]);
41             }
42             }
43              
44             # ulistok($label,\@got,\@expect)
45             # --> ok() for unsorted lists
46             sub ulistok {
47 0     0   0 my ($label,$l1,$l2) = @_;
48 0         0 is_deeply([sort @$l1],[sort @$l2],$label);
49             }
50              
51             # matchpdl($a,$b) : returns pdl identity check, including BAD
52             sub matchpdl {
53 2504     2504   354185 my ($a,$b) = map {PDL->topdl($_)->setnantobad} @_[0,1];
  5008         61031  
54 2504         215216 return ($a==$b)->setbadtoval(0) | ($a->isbad & $b->isbad) | ($a->isfinite->not & $b->isfinite->not);
55             }
56             # matchpdl($a,$b,$eps) : returns pdl approximation check, including BAD
57             sub matchpdla {
58 7     7   931 my ($a,$b) = map {$_->setnantobad} @_[0,1];
  14         109  
59 7         15 my $eps = $_[2];
60 7 50       17 $eps = 1e-5 if (!defined($eps));
61 7         23 return $a->approx($b,$eps)->setbadtoval(0) | ($a->isbad & $b->isbad) | ($a->isfinite->not & $b->isfinite->not);
62             }
63              
64             # cmp_dims($got_pdl,$expect_pdl)
65             sub cmp_dims {
66 2502     2502   4861 my ($p1,$p2) = @_;
67 2502   33     14349 return $p1->ndims==$p2->ndims && all(pdl(PDL::long(),[$p1->dims])==pdl(PDL::long(),[$p2->dims]));
68             }
69              
70             sub pdlstr {
71 0     0   0 my $a = shift;
72 0 0       0 return '(undef)' if (!defined($a));
73 0 0       0 my $typ = UNIVERSAL::can($a,'type') ? $a->type : 'NOTYPE';
74 0         0 my $str = "($typ) $a";
75             #$str =~ s/\n/ /g;
76 0         0 return $str;
77             }
78             sub labstr {
79 2511     2511   6095 my ($label,$ok,$got,$want) = @_;
80 2511 50       6003 $label .= "\n : got=".pdlstr($got)."\n : wanted=".pdlstr($want) if (!$ok);
81 2511         6023 return $label;
82             }
83              
84             # pdlok($label, $got, $want)
85             sub pdlok {
86 2495     2495   446198 my ($label,$got,$want) = @_;
87 2495 50       10163 $got = PDL->topdl($got) if (defined($got));
88 2495 50       29923 $want = PDL->topdl($want) if (defined($want));
89 2495   33     29432 my $ok = (defined($got) && defined($want)
90             && cmp_dims($got,$want)
91             && all(matchpdl($want,$got))
92             );
93 2495         142271 isok(labstr($label,$ok,$got,$want), $ok);
94             }
95              
96             # pdlok_nodims($label, $got, $want)
97             # + ignores dimensions
98             sub pdlok_nodims {
99 9     9   208 my ($label,$got,$want) = @_;
100 9 50       40 $got = PDL->topdl($got) if (defined($got));
101 9 50       116 $want = PDL->topdl($want) if (defined($want));
102 9   33     103 my $ok = (defined($got) && defined($want)
103             #&& cmp_dims($got,$want)
104             && all(matchpdl($want,$got)));
105 9         521 isok(labstr($label,$ok,$got,$want), $ok);
106             }
107              
108             # pdlapprox($label, $got, $want, $eps=1e-5)
109             sub pdlapprox {
110 7     7   37 my ($label,$got,$want,$eps) = @_;
111 7 50       29 $got = PDL->topdl($got) if (defined($got));
112 7 50       87 $want = PDL->topdl($want) if (defined($want));
113 7 50       64 $eps = 1e-5 if (!defined($eps));
114 7   33     33 my $ok = (defined($got) && defined($want)
115             && cmp_dims($got,$want)
116             && all(matchpdla($want,$got,$eps)));
117 7         1238 isok(labstr($label,$ok,$got,$want), $ok);
118             }
119              
120              
121             print "loaded ", __FILE__, "\n";
122              
123             1;
124