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
|
|
|
|
|
|
|
BEGIN { $| = 1; } |
5
|
|
|
|
|
|
|
use strict; |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
# isok($label,@_) -- prints helpful label |
8
|
|
|
|
|
|
|
sub isok { |
9
|
|
|
|
|
|
|
my $label = shift; |
10
|
|
|
|
|
|
|
if (@_==1) { |
11
|
|
|
|
|
|
|
ok($_[0],$label); |
12
|
|
|
|
|
|
|
} elsif (@_==2) { |
13
|
|
|
|
|
|
|
is($_[0],$_[1], $label); |
14
|
|
|
|
|
|
|
} else { |
15
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
my ($label,$skip_if_true) = splice(@_,0,2); |
23
|
|
|
|
|
|
|
if ($skip_if_true) { |
24
|
|
|
|
|
|
|
subtest $label => sub { plan skip_all => $skip_if_true; }; |
25
|
|
|
|
|
|
|
} else { |
26
|
|
|
|
|
|
|
if (@_==1 && ref($_[0]) && ref($_[0]) eq 'CODE') { |
27
|
|
|
|
|
|
|
isok($label, $_[0]->()); |
28
|
|
|
|
|
|
|
} else { |
29
|
|
|
|
|
|
|
isok($label,@_); |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
} |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
# skipordo($label,$skip_if_true,sub { ok ... },@args_for_sub) |
35
|
|
|
|
|
|
|
sub skipordo { |
36
|
|
|
|
|
|
|
my ($label,$skip_if_true) = splice(@_,0,2); |
37
|
|
|
|
|
|
|
if ($skip_if_true) { |
38
|
|
|
|
|
|
|
subtest $label => sub { plan skip_all => $skip_if_true; }; |
39
|
|
|
|
|
|
|
} else { |
40
|
|
|
|
|
|
|
$_[0]->(@_[1..$#_]); |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
# ulistok($label,\@got,\@expect) |
45
|
|
|
|
|
|
|
# --> ok() for unsorted lists |
46
|
|
|
|
|
|
|
sub ulistok { |
47
|
|
|
|
|
|
|
my ($label,$l1,$l2) = @_; |
48
|
|
|
|
|
|
|
is_deeply([sort @$l1],[sort @$l2],$label); |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
# matchpdl($a,$b) : returns pdl identity check, including BAD |
52
|
|
|
|
|
|
|
sub matchpdl { |
53
|
|
|
|
|
|
|
my ($a,$b) = map {PDL->topdl($_)->setnantobad} @_[0,1]; |
54
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
my ($a,$b) = map {$_->setnantobad} @_[0,1]; |
59
|
|
|
|
|
|
|
my $eps = $_[2]; |
60
|
|
|
|
|
|
|
$eps = 1e-5 if (!defined($eps)); |
61
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
my ($p1,$p2) = @_; |
67
|
|
|
|
|
|
|
return $p1->ndims==$p2->ndims && all(pdl(PDL::long(),[$p1->dims])==pdl(PDL::long(),[$p2->dims])); |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
sub pdlstr { |
71
|
|
|
|
|
|
|
my $a = shift; |
72
|
|
|
|
|
|
|
return '(undef)' if (!defined($a)); |
73
|
|
|
|
|
|
|
my $typ = UNIVERSAL::can($a,'type') ? $a->type : 'NOTYPE'; |
74
|
|
|
|
|
|
|
my $str = "($typ) $a"; |
75
|
|
|
|
|
|
|
#$str =~ s/\n/ /g; |
76
|
|
|
|
|
|
|
return $str; |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
sub labstr { |
79
|
|
|
|
|
|
|
my ($label,$ok,$got,$want) = @_; |
80
|
|
|
|
|
|
|
$label .= "\n : got=".pdlstr($got)."\n : wanted=".pdlstr($want) if (!$ok); |
81
|
|
|
|
|
|
|
return $label; |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
# pdlok($label, $got, $want) |
85
|
|
|
|
|
|
|
sub pdlok { |
86
|
|
|
|
|
|
|
my ($label,$got,$want) = @_; |
87
|
|
|
|
|
|
|
$got = PDL->topdl($got) if (defined($got)); |
88
|
|
|
|
|
|
|
$want = PDL->topdl($want) if (defined($want)); |
89
|
|
|
|
|
|
|
my $ok = (defined($got) && defined($want) |
90
|
|
|
|
|
|
|
&& cmp_dims($got,$want) |
91
|
|
|
|
|
|
|
&& all(matchpdl($want,$got)) |
92
|
|
|
|
|
|
|
); |
93
|
|
|
|
|
|
|
isok(labstr($label,$ok,$got,$want), $ok); |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
# pdlok_nodims($label, $got, $want) |
97
|
|
|
|
|
|
|
# + ignores dimensions |
98
|
|
|
|
|
|
|
sub pdlok_nodims { |
99
|
|
|
|
|
|
|
my ($label,$got,$want) = @_; |
100
|
|
|
|
|
|
|
$got = PDL->topdl($got) if (defined($got)); |
101
|
|
|
|
|
|
|
$want = PDL->topdl($want) if (defined($want)); |
102
|
|
|
|
|
|
|
my $ok = (defined($got) && defined($want) |
103
|
|
|
|
|
|
|
#&& cmp_dims($got,$want) |
104
|
|
|
|
|
|
|
&& all(matchpdl($want,$got))); |
105
|
|
|
|
|
|
|
isok(labstr($label,$ok,$got,$want), $ok); |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
# pdlapprox($label, $got, $want, $eps=1e-5) |
109
|
|
|
|
|
|
|
sub pdlapprox { |
110
|
|
|
|
|
|
|
my ($label,$got,$want,$eps) = @_; |
111
|
|
|
|
|
|
|
$got = PDL->topdl($got) if (defined($got)); |
112
|
|
|
|
|
|
|
$want = PDL->topdl($want) if (defined($want)); |
113
|
|
|
|
|
|
|
$eps = 1e-5 if (!defined($eps)); |
114
|
|
|
|
|
|
|
my $ok = (defined($got) && defined($want) |
115
|
|
|
|
|
|
|
&& cmp_dims($got,$want) |
116
|
|
|
|
|
|
|
&& all(matchpdla($want,$got,$eps))); |
117
|
|
|
|
|
|
|
isok(labstr($label,$ok,$got,$want), $ok); |
118
|
|
|
|
|
|
|
} |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
print "loaded ", __FILE__, "\n"; |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
1; |
124
|
|
|
|
|
|
|
|