File Coverage

/root/.cpan/build/PDL-CCS-1.23.12-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   208598 BEGIN { $| = 1; }
5 13     13   105 use strict;
  13         25  
  13         13239  
6              
7             # isok($label,@_) -- prints helpful label
8             sub isok {
9 4994     4994   176517 my $label = shift;
10 4994 100       11853 if (@_==1) {
    50          
11 2650         6055 ok($_[0],$label);
12             } elsif (@_==2) {
13 2344         5911 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   635 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     19 if (@_==1 && ref($_[0]) && ref($_[0]) eq 'CODE') {
      33        
27 2         6 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   4 my ($label,$skip_if_true) = splice(@_,0,2);
37 1 50       4 if ($skip_if_true) {
38 0     0   0 subtest $label => sub { plan skip_all => $skip_if_true; };
  0         0  
39             } else {
40 1         4 $_[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   290030 my ($a,$b) = map {PDL->topdl($_)->setnantobad} @_[0,1];
  5008         50396  
54 2504         177886 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   872 my ($a,$b) = map {$_->setnantobad} @_[0,1];
  14         95  
59 7         14 my $eps = $_[2];
60 7 50       15 $eps = 1e-5 if (!defined($eps));
61 7         17 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   4063 my ($p1,$p2) = @_;
67 2502   33     11455 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   4987 my ($label,$ok,$got,$want) = @_;
80 2511 50       4889 $label .= "\n : got=".pdlstr($got)."\n : wanted=".pdlstr($want) if (!$ok);
81 2511         5496 return $label;
82             }
83              
84             # pdlok($label, $got, $want)
85             sub pdlok {
86 2495     2495   384798 my ($label,$got,$want) = @_;
87 2495 50       8609 $got = PDL->topdl($got) if (defined($got));
88 2495 50       22765 $want = PDL->topdl($want) if (defined($want));
89 2495   33     24142 my $ok = (defined($got) && defined($want)
90             && cmp_dims($got,$want)
91             && all(matchpdl($want,$got))
92             );
93 2495         119039 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   174 my ($label,$got,$want) = @_;
100 9 50       35 $got = PDL->topdl($got) if (defined($got));
101 9 50       98 $want = PDL->topdl($want) if (defined($want));
102 9   33     89 my $ok = (defined($got) && defined($want)
103             #&& cmp_dims($got,$want)
104             && all(matchpdl($want,$got)));
105 9         432 isok(labstr($label,$ok,$got,$want), $ok);
106             }
107              
108             # pdlapprox($label, $got, $want, $eps=1e-5)
109             sub pdlapprox {
110 7     7   31 my ($label,$got,$want,$eps) = @_;
111 7 50       21 $got = PDL->topdl($got) if (defined($got));
112 7 50       76 $want = PDL->topdl($want) if (defined($want));
113 7 50       80 $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         888 isok(labstr($label,$ok,$got,$want), $ok);
118             }
119              
120              
121             print "loaded ", __FILE__, "\n";
122              
123             1;
124