blib/lib/Test/PDL.pm

filter: Statementstmt 58.9% 105 / 178 Branchbran 45.2% 67 / 148 Conditioncond 35.1% 19 / 54 MC/DCmcdc 21.8% 7 / 32 Subroutinesub 68.7% 11 / 16 Podpod 100% 6 / 6 totaltotal 49.5% 215 / 434 CC 97 SCAR 46.6
CC
97
Cov
50%
eq_pdl
171.2
eq_pdl
171.2
is_pdl
56.4
CRAP 105.7
« blib/lib/PDL/IO/FITS.pm ↑ Summary blib/lib/PDL/IO/Pic.pm »
1 package Test::PDL;
2  
3 =head1 NAME
4  
5 Test::PDL - Test Perl Data Language arrays (a.k.a. ndarrays) for equality
6  
7 =head1 SYNOPSIS
8  
9 use PDL;
10 use Test::More tests => 3;
11 use Test::PDL qw( is_pdl :deep );
12  
13 # an example of a test that succeeds
14 $got = sequence 5;
15 $expected = pdl( 0,1,2,3,4 );
16 is_pdl( $got, $expected, 'sequence() works as expected' );
17 # OUTPUT:
18 # ok 1 - sequence() works as expected
19  
20 # if a test fails, detailed diagnostics are printed; the output is
21 # similar to that of is() from L<Test::More>
22 $got = pdl( 0,-1,-2,3,4 );
23 $expected = sequence 5;
24 is_pdl( $got, $expected, 'demonstrate the output of a failing test' );
25 # OUTPUT:
26 # not ok 2 - demonstrate the output of a failing test
27 #
28 # Failed test 'demonstrate the output of a failing test'
29 # at maint/pod.t line 16.
30 # 2/5 values do not match
31 # got: Double D [5] (P ) [0 -1 -2 3 4]
32 # expected: Double D [5] (P ) [0 1 2 3 4]
33 # First <=5 values differ at:
34 # [
35 # [1]
36 # [2]
37 # ]
38 # Those 'got' values: [-1 -2]
39 # Those 'expected' values: [1 2]
40  
41 # ndarrays within other data structures can be tested with Test::Deep
42 use Test::Deep qw( cmp_deeply );
43 $got = { name => 'Histogram', data => long( 17,0,1 ) };
44 $expected = { name => 'Histogram', data => test_long( 17,0,0,1 ) };
45 cmp_deeply( $got, $expected, 'demonstrate the output of a failing deep comparison' );
46 # OUTPUT:
47 # not ok 3 - demonstrate the output of a failing deep comparison
48 #
49 # Failed test 'demonstrate the output of a failing deep comparison'
50 # at maint/pod.t line 30.
51 # Comparing $data->{"data"} as an ndarray:
52 # dimensions do not match in extent
53 # got : Long D [3] (P ) [17 0 1]
54 # expect : Long D [4] (P ) [17 0 0 1]
55  
56 =cut
57  
58 58 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
59 58 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
60 58 ▶use PDL::Lite;
Subroutine BEGIN: called CC=1 SCAR=0.0
61 58 ▶use PDL::Types ();
Subroutine BEGIN: called CC=1 SCAR=0.0
62  
63 58 ▶use base qw( Exporter );
Subroutine BEGIN: called CC=1 SCAR=0.0
64 58 our @EXPORT = qw( is_pdl );
65 58 our @EXPORT_OK = qw( eq_pdl is_pdl test_pdl );
66 58 our %EXPORT_TAGS = ( deep => [ qw( test_pdl ) ] );
67 58 our $VERSION = '0.22';
68  
69 =head1 DESCRIPTION
70  
71 With Test::PDL, you can compare two ndarrays for equality. The comparison is
72 performed as thoroughly as possible, comparing types, dimensions, bad value
73 patterns, and finally the values themselves. The exact behaviour can be
74 configured by setting certain package-wide defaults (see %DEFAULTS below), or
75 by supplying options in a function call.
76 Test::PDL is mostly useful in test scripts.
77  
78 Test::PDL is to be used with the Perl Data Language (L<PDL>).
79  
80 By default, Test::PDL exports only one function: is_pdl(). The other functions
81 are exported on demand only. The export tag C<:deep> exports test_pdl() and one
82 function for each PDL type constructor (like short(), double(), etc.), prefixed
83 with C<test_>: test_short(), test_double(), ...
84  
85 =head1 VARIABLES
86  
87 =head2 %DEFAULTS
88  
89 The default comparison criteria used by Test::PDL can be configured by setting
90 the values in the %DEFAULTS hash. This can be done directly, by addressing
91 %Test::PDL::DEFAULTS directly.
92  
93 =over 4
94  
95 =item atol
96  
97 The absolute tolerance used to compare values. Initially set to 1e-6.
98  
99 =item require_equal_types
100  
101 If true, only ndarrays with equal type can be considered equal. If false, the
102 types of the ndarrays being compared is not taken into consideration. Defaults
103 to true: types must match for the comparison to succeed. If you want to
104 write tests like
105  
106 is_pdl( $got, pdl([ 1, 3, 5, 6 ]) );
107  
108 without having to worry about the type of the ndarray being exactly I<double>
109 (which is the default type of the pdl() constructor), set I<require_equal_types> equal to
110 0.
111  
112 =item rtol
113  
114 The relative tolerance used to compare values. Initially set to 1e-6.
115  
116 =back
117  
118 =cut
119  
120 58 our %DEFAULTS = (
121 atol => 1e-6,
122 require_equal_types => 1,
123 rtol => 1e-6,
124 );
125  
126 =head1 FUNCTIONS
127  
128 =head2 import
129  
130 Custom importer that recognizes configuration defaults specified at use time, as
131 in
132  
133 use Test::PDL -require_equal_types => 0;
134  
135 =cut
136  
137 sub import
138 64 ▶{
Subroutine import: called CC=5 SCAR=21.6
139 0 ▶ my $i = 0;
Subroutine import: not called CC=5 SCAR=21.6
140 0 ▶ while( $i < @_ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($i < @_) 84 63 147
if ($i < @_) 0 0 0
141 0 ▶ if( $_[ $i ] =~ /^-/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($_[$i] =~ /^-/) { } 16 68 84
if ($_[$i] =~ /^-/) { } 0 0 0
142 0 my( $key, $val ) = splice @_, $i, 2;
143 0 $key =~ s/^-(.*)/$1/;
144 0 ▶ PDL::barf( "invalid name $key" ) unless grep { $key eq $_ } keys %DEFAULTS;
Branch: 2 branches
BranchTrueFalseTotal
unless grep {$key eq $_;} keys %Test::PDL::DEFAULTS 1 15 16
unless grep {$key eq $_;} keys %Test::PDL::DEFAULTS 0 0 0
145 0 ▶ PDL::barf( "undefined value for $key" ) unless defined $val;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $val 0 15 15
unless defined $val 0 0 0
146 0 $DEFAULTS{ $key } = $val;
147 }
148 0 else { $i++ }
149 }
150 0 __PACKAGE__->export_to_level( 1, @_ );
151 }
152  
153 =head2 is_pdl
154  
155 =for ref # PDL
156  
157 Run a test comparing an ndarray to an expected ndarray, and fail with detailed
158 diagnostics if they don't compare equal.
159  
160 =for usage # PDL
161  
162 is_pdl( $got, $expected );
163 is_pdl( $got, $expected, $test_name );
164 is_pdl( $got, $expected, { test_name => $test_name } );
165 is_pdl( $got, $expected, { atol => $absolute_tolerance, ... } );
166  
167 Yields ok if the first two arguments are ndarrays that compare equal, not ok if
168 the ndarrays are different, or if at least one is not an ndarray. Prints a
169 diagnostic when the comparison fails, with the reason and a brief printout of
170 both arguments. See the documentation of eq_pdl() for the comparison
171 criteria. $test_name is optional.
172  
173 Named after is() from L<Test::More>.
174  
175 =cut
176  
177 1612 ▶sub is_pdl {
Subroutine is_pdl: called CC=16 SCAR=40.3
Pod documented
178 0 ▶ require Test::Builder;
Subroutine is_pdl: not called CC=16 SCAR=40.3
Pod documented
179 0 my $tb = Test::Builder->new;
180 0 ▶ $tb->croak('error in arguments: > 3 given') if @_ > 3;
Branch: 2 branches
BranchTrueFalseTotal
if @_ > 3 1 1611 1612
if @_ > 3 0 0 0
181 0 my ( $got, $expected, $arg ) = @_;
182 $tb->croak('error in arguments: third argument is an ndarray')
183 0 ▶ if eval { $arg->isa('PDL') };
Branch: 2 branches
BranchTrueFalseTotal
if eval { do { $arg->isa("PDL") } } 1 1610 1611
if eval { do { $arg->isa("PDL") } } 0 0 0
184 0 my $opt = { %DEFAULTS };
185 0 my $name;
186 0 ▶ if ($arg) {
Branch: 2 branches
BranchTrueFalseTotal
if ($arg) 1350 260 1610
if ($arg) 0 0 0
187 0 ▶ if (ref $arg eq 'HASH') { $opt = { %$opt, %$arg } }
Branch: 2 branches
BranchTrueFalseTotal
if (ref $arg eq 'HASH') { } 116 1234 1350
if (ref $arg eq 'HASH') { } 0 0 0
188 0 else { $name = $arg }
189 }
190 0 ▶ $name ||= $opt->{test_name} || "ndarrays are equal";
Condition40%4/10
$name ||= $opt->{"test_name"} || "ndarrays are equal"
l!l&&r!l&&!r
12343760
$opt->{'test_name'} || 'ndarrays are equal'
l!l
87289
$name ||= $opt->{"test_name"} || "ndarrays are equal"
l!l&&r!l&&!r
000
$opt->{'test_name'} || 'ndarrays are equal'
l!l
00
MC/DC0%0/2
$name ||= $opt->{"test_name"} || "ndarrays are equal"
$name $opt->{'test_name'}
MC/DC0%0/2
$name ||= $opt->{"test_name"} || "ndarrays are equal"
$name $opt->{'test_name'}
Truth table66%2/3
A ||= B
A= $nameB= $opt->{'test_name'}
ABresult
1X1
011
000
Truth table0%0/3
A ||= B
A= $nameB= $opt->{'test_name'}
ABresult
1X1
011
000
191 0 my ($ok, $reason, $mask) = eq_pdl($got, $expected, $opt);
192 0 ▶ return $tb->ok(1, $name) if $ok;
Branch: 2 branches
BranchTrueFalseTotal
if $ok 1595 15 1610
if $ok 0 0 0
193 0 my $rc = $tb->ok( 0, $name );
194 0 my $fmt = '%-8T %-12D (%-5S) ';
195 0 my @mismatch;
196 0 ▶ if (defined $mask) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $mask) 7 8 15
if (defined $mask) 0 0 0
197 0 ▶ my $coords = defined $mask ? $mask->not->whichND : undef;
Branch: 2 branches
BranchTrueFalseTotal
defined $mask ? : 7 0 7
defined $mask ? : 0 0 0
198 0 ▶ $coords = $coords->slice(',0:4') if defined $coords and $coords->dim(1) > 5;
Branch: 2 branches
BranchTrueFalseTotal
if defined $coords and $coords->dim(1) > 5 0 7 7
if defined $coords and $coords->dim(1) > 5 0 0 0
Condition16%1/6
defined $coords and $coords->dim(1) > 5
!ll&&!rl&&r
070
defined $coords and $coords->dim(1) > 5
!ll&&!rl&&r
000
MC/DC0%0/2
defined $coords and $coords->dim(1) > 5
defined $coords $coords->dim(1) > 5
MC/DC0%0/2
defined $coords and $coords->dim(1) > 5
defined $coords $coords->dim(1) > 5
Truth table33%1/3
A and B
A= defined $coordsB= $coords->dim(1) > 5
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= defined $coordsB= $coords->dim(1) > 5
ABresult
0X0
100
111
199 0 my $cstr = $coords->string; $cstr =~ s#\n+\z##;
200 0 push @mismatch, (
201 "\nFirst <=5 values differ at: $cstr\n",
202 "Those 'got' values: ", $got->indexND($coords),
203 "\nThose 'expected' values: ", $expected->indexND($coords),
204 );
205 }
206 $tb->diag(
207 " $reason\n",
208 0 ▶ " got: ", eval { $got->isa('PDL') && !$got->isnull } ? $got->info( $fmt ) : '', $got, "\n",
Branch: 2 branches
BranchTrueFalseTotal
eval { do { $expected->isa('PDL') and not $expected->isnull } } ? : 13 2 15
eval { do { $expected->isa('PDL') and not $expected->isnull } } ? : 0 0 0
Condition50%3/6
$got->isa('PDL') && !$got->isnull
!ll&&!rl&&r
1113
$got->isa('PDL') && !$got->isnull
!ll&&!rl&&r
000
MC/DC100%2/2
$got->isa('PDL') && !$got->isnull
$got->isa('PDL') !$got->isnull
MC/DC0%0/2
$got->isa('PDL') && !$got->isnull
$got->isa('PDL') !$got->isnull
Truth table100%3/3
A && not(B)
A= $got->isa('PDL')B= !$got->isnull
ABresult
0X0
100
111
Truth table0%0/3
A && not(B)
A= $got->isa('PDL')B= !$got->isnull
ABresult
0X0
100
111
209 0 ▶ " expected: ", eval { $expected->isa('PDL') && !$expected->isnull } ? $expected->info( $fmt ) : '', $expected,
Branch: 2 branches
BranchTrueFalseTotal
eval { do { $got->isa('PDL') and not $got->isnull } } ? : 13 2 15
eval { do { $got->isa('PDL') and not $got->isnull } } ? : 0 0 0
Condition50%3/6
$expected->isa('PDL') && !$expected->isnull
!ll&&!rl&&r
1113
$expected->isa('PDL') && !$expected->isnull
!ll&&!rl&&r
000
MC/DC100%2/2
$expected->isa('PDL') && !$expected->isnull
$expected->isa('PDL') !$expected->isnull
MC/DC0%0/2
$expected->isa('PDL') && !$expected->isnull
$expected->isa('PDL') !$expected->isnull
Truth table100%3/3
A && not(B)
A= $expected->isa('PDL')B= !$expected->isnull
ABresult
0X0
100
111
Truth table0%0/3
A && not(B)
A= $expected->isa('PDL')B= !$expected->isnull
ABresult
0X0
100
111
210 @mismatch,
211 );
212 0 return $rc;
213 }
214  
215 =head2 eq_pdl
216  
217 =for ref # PDL
218  
219 Return true if two ndarrays compare equal, false otherwise. In list context,
220 additionally returns a diagnostic string.
221  
222 =for usage # PDL
223  
224 my $equal = eq_pdl( $got, $expected );
225 my $equal = eq_pdl( $got, $expected, { atol => $absolute_tolerance, ... } );
226 my( $equal, $diag ) = eq_pdl( $got, $expected );
227 my( $equal, $diag ) = eq_pdl( $got, $expected, { atol => $absolute_tolerance, ... } );
228  
229 eq_pdl() contains just the comparison part of is_pdl(), without the
230 infrastructure required to write tests with L<Test::More>. It could be used as
231 part of a larger test in which the equality of two ndarrays must be verified. By
232 itself, eq_pdl() does not generate any output, so it should be safe to use
233 outside test suites.
234  
235 In list context, eq_pdl() returns a list with three elements, the first one being
236 a boolean whether the ndarrays compared equal, the second being a diagnostic
237 string explaining why the comparison failed (or the empty string, if it didn't
238 fail). The third is either the mask of not-equal if the values didn't
239 match, or C<undef>.
240 This is useful in combination with L<Test::Deep>, but might also be
241 useful on its own.
242  
243 eq_pd() does not need L<Test::Builder>, so you can use it as part of something
244 else, without side effects (like generating output).
245  
246 The criteria for equality are the following:
247  
248 =over 4
249  
250 =item *
251  
252 Both arguments must be ndarrays for the comparison to succeed. Currently, there
253 is no implicit conversion from scalar to ndarray.
254  
255 =item *
256  
257 The type of both ndarrays must be equal if (and only if) I<require_equal_types> is true.
258  
259 =item *
260  
261 The number of dimensions must be equal. That is, a two-dimensional ndarray only
262 compares equal with another two-dimensional ndarray.
263  
264 =item *
265  
266 The extent of the dimensions are compared one by one and must match. That is, a
267 ndarray with dimensions (5,4) cannot compare equal with an ndarray of dimensions
268 (5,3). Note that degenerate dimensions are not treated specially, and thus a
269 ndarray with dimensions (5,4,1) is considered different from an ndarray with
270 dimensions (5,4).
271  
272 =item *
273  
274 For ndarrays that conform in type and shape, the bad value pattern is examined.
275 If the two ndarrays have bad values in different positions, the ndarrays are
276 considered different. Note that two ndarrays may compare equal even though their
277 bad flag is different, if there are no bad values.
278  
279 =item *
280  
281 And last but not least, the values themselves are examined one by one.
282 As of 0.21, both integer and floating-point types are compared approximately.
283 The approximate comparison is
284 implemented using a combination of relative and absolute tolerances, which can
285 be set by supplying an argument to C<use Test::PDL>, or by supplying an
286 optional hash to this function. By default, the absolute and relative
287 tolerances are both equal to 1e-6. The user can specify a pure relative
288 tolerance by specifying C<atol =E<gt> 0>, and a pure absolute tolerance by
289 specifying C<rtol =E<gt> 0>. If both tolerances are specified, values compare
290 equal if I<either> their difference is lower than or equal to the absolute
291 tolerance I<or> their relative difference (with respect to the expected
292 value) is lower than or equal to the relative tolerance. For expected
293 values equal to zero, relative differences (with respect to the expected
294 value) make no sense, and the use of combined absolute and relative
295 tolerances is recommended.
296  
297 =back
298  
299 =cut
300  
301 1864 ▶sub eq_pdl {
Subroutine eq_pdl: called CC=29 SCAR=51.4
Pod documented
302 0 ▶ my ($got, $expected, $arg) = @_;
Subroutine eq_pdl: not called CC=29 SCAR=51.4
Pod documented
303 0 ▶ my $opt = { %DEFAULTS, ref $arg eq 'HASH' ? %$arg : () };
Branch: 2 branches
BranchTrueFalseTotal
ref $arg eq 'HASH' ? : 1680 184 1864
ref $arg eq 'HASH' ? : 0 0 0
304 0 ▶ PDL::barf( 'need an absolute or a relative tolerance, or both' ) unless defined $opt->{atol} || defined $opt->{rtol};
Branch: 2 branches
BranchTrueFalseTotal
unless defined $opt->{"atol"} or defined $opt->{"rtol"} 0 1864 1864
unless defined $opt->{"atol"} or defined $opt->{"rtol"} 0 0 0
Condition16%1/6
defined $opt->{"atol"} or defined $opt->{"rtol"}
l!l&&r!l&&!r
186400
defined $opt->{"atol"} or defined $opt->{"rtol"}
l!l&&r!l&&!r
000
MC/DC0%0/2
defined $opt->{"atol"} or defined $opt->{"rtol"}
defined $opt->{"atol"} defined $opt->{"rtol"}
MC/DC0%0/2
defined $opt->{"atol"} or defined $opt->{"rtol"}
defined $opt->{"atol"} defined $opt->{"rtol"}
Truth table33%1/3
A or B
A= defined $opt->{"atol"}B= defined $opt->{"rtol"}
ABresult
1X1
011
000
Truth table0%0/3
A or B
A= defined $opt->{"atol"}B= defined $opt->{"rtol"}
ABresult
1X1
011
000
305 0 ▶ $opt->{atol} //= 0;
Condition25%1/4
$opt->{"atol"} //= 0
l!l
18640
$opt->{"atol"} //= 0
l!l
00
MC/DC0%0/1
$opt->{"atol"} //= 0
$opt->{"atol"}
MC/DC0%0/1
$opt->{"atol"} //= 0
$opt->{"atol"}
Truth table50%1/2
A
A= $opt->{"atol"}
Aresult
11
00
Truth table0%0/2
A
A= $opt->{"atol"}
Aresult
11
00
306 0 ▶ $opt->{rtol} //= 0;
Condition25%1/4
$opt->{"rtol"} //= 0
l!l
18640
$opt->{"rtol"} //= 0
l!l
00
MC/DC0%0/1
$opt->{"rtol"} //= 0
$opt->{"rtol"}
MC/DC0%0/1
$opt->{"rtol"} //= 0
$opt->{"rtol"}
Truth table50%1/2
A
A= $opt->{"rtol"}
Aresult
11
00
Truth table0%0/2
A
A= $opt->{"rtol"}
Aresult
11
00
307 0 ▶ PDL::barf('absolute tolerance cannot be negative') if $opt->{atol} < 0;
Branch: 2 branches
BranchTrueFalseTotal
if $opt->{"atol"} < 0 0 1864 1864
if $opt->{"atol"} < 0 0 0 0
308 0 ▶ PDL::barf('relative tolerance cannot be negative') if $opt->{rtol} < 0;
Branch: 2 branches
BranchTrueFalseTotal
if $opt->{"rtol"} < 0 0 1864 1864
if $opt->{"rtol"} < 0 0 0 0
309 return wantarray ? (0, 'received value is not an ndarray', undef) : 0
310 0 ▶ if !eval { $got->isa('PDL') };
Branch: 4 branches
BranchTrueFalseTotal
unless eval { do { $got->isa("PDL") } } 10 1854 1864
wantarray ? : 6 4 10
unless eval { do { $got->isa("PDL") } } 0 0 0
wantarray ? : 0 0 0
311 return wantarray ? (0, 'expected value is not an ndarray', undef) : 0
312 0 ▶ if !eval { $expected->isa('PDL') };
Branch: 4 branches
BranchTrueFalseTotal
unless eval { do { $expected->isa("PDL") } } 5 1849 1854
wantarray ? : 3 2 5
unless eval { do { $expected->isa("PDL") } } 0 0 0
wantarray ? : 0 0 0
313 return wantarray ? (0, 'types do not match (\'require_equal_types\' is true)', undef) : 0
314 0 ▶ if $opt->{require_equal_types} && $got->type != $expected->type;
Branch: 4 branches
BranchTrueFalseTotal
if $opt->{"require_equal_types"} and $got->type != $expected->type 6 1843 1849
wantarray ? : 4 2 6
if $opt->{"require_equal_types"} and $got->type != $expected->type 0 0 0
wantarray ? : 0 0 0
Condition50%3/6
$opt->{"require_equal_types"} and $got->type != $expected->type
!ll&&!rl&&r
5317906
$opt->{"require_equal_types"} and $got->type != $expected->type
!ll&&!rl&&r
000
MC/DC100%2/2
$opt->{"require_equal_types"} and $got->type != $expected->type
$opt->{"require_equal_types"} $got->type != $expected->type
MC/DC0%0/2
$opt->{"require_equal_types"} and $got->type != $expected->type
$opt->{"require_equal_types"} $got->type != $expected->type
Truth table100%3/3
A and B
A= $opt->{"require_equal_types"}B= $got->type != $expected->type
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= $opt->{"require_equal_types"}B= $got->type != $expected->type
ABresult
0X0
100
111
315 0 my @got_dims = $got->dims;
316 0 my @exp_dims = $expected->dims;
317 0 ▶ return wantarray ? (0, 'dimensions do not match in number', undef) : 0
Branch: 4 branches
BranchTrueFalseTotal
if @got_dims != @exp_dims 6 1837 1843
wantarray ? : 4 2 6
if @got_dims != @exp_dims 0 0 0
wantarray ? : 0 0 0
318 if @got_dims != @exp_dims;
319 0 ▶ while (@got_dims) {
Branch: 2 branches
BranchTrueFalseTotal
if (@got_dims) 2253 1826 4079
if (@got_dims) 0 0 0
320 0 ▶ return wantarray ? (0, 'dimensions do not match in extent', undef) : 0
Branch: 4 branches
BranchTrueFalseTotal
if shift @got_dims != shift @exp_dims 11 2242 2253
wantarray ? : 7 4 11
if shift @got_dims != shift @exp_dims 0 0 0
wantarray ? : 0 0 0
321 if shift(@got_dims) != shift(@exp_dims);
322 }
323 0 ▶ return wantarray ? (1, '', undef) : 1
Branch: 4 branches
BranchTrueFalseTotal
if $got->isempty and $expected->isempty 62 1764 1826
wantarray ? : 60 2 62
if $got->isempty and $expected->isempty 0 0 0
wantarray ? : 0 0 0
Condition33%2/6
$got->isempty and $expected->isempty
!ll&&!rl&&r
1764062
$got->isempty and $expected->isempty
!ll&&!rl&&r
000
MC/DC50%1/2
$got->isempty and $expected->isempty
$got->isempty $expected->isempty
MC/DC0%0/2
$got->isempty and $expected->isempty
$got->isempty $expected->isempty
Truth table66%2/3
A and B
A= $got->isemptyB= $expected->isempty
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= $got->isemptyB= $expected->isempty
ABresult
0X0
100
111
324 if $got->isempty and $expected->isempty;
325 # both are now non-empty
326 0 my $got_is_inplace = $got->is_inplace;
327 0 my $exp_is_inplace = $expected->is_inplace;
328 0 $_->set_inplace(0) for $got, $expected;
329 0 my $res = PDL::Primitive::approx_artol( $got, $expected, @$opt{qw(atol rtol)} );
330 0 ▶ $got->set_inplace(1) if $got_is_inplace;
Branch: 2 branches
BranchTrueFalseTotal
if $got_is_inplace 3 1761 1764
if $got_is_inplace 0 0 0
331 0 ▶ $expected->set_inplace(1) if $exp_is_inplace;
Branch: 2 branches
BranchTrueFalseTotal
if $exp_is_inplace 0 1764 1764
if $exp_is_inplace 0 0 0
332 0 ▶ return wantarray ? (1, '', undef) : 1 if $res->all;
Branch: 4 branches
BranchTrueFalseTotal
if $res->all 1720 44 1764
wantarray ? : 1696 24 1720
if $res->all 0 0 0
wantarray ? : 0 0 0
333 0 my $exp_nelem = $expected->nelem;
334 0 my $reason = ($exp_nelem-$res->sum)."/$exp_nelem values do not match";
335 0 ▶ return wantarray ? (0, $reason, $res) : 0;
Branch: 2 branches
BranchTrueFalseTotal
wantarray ? : 26 18 44
wantarray ? : 0 0 0
336 }
337  
338 =head2 test_pdl
339  
340 =for ref # PDL
341  
342 Special comparison to be used in conjunction with L<Test::Deep> to test ndarrays
343 inside data structures.
344  
345 =for usage # PDL
346  
347 my $expected = { ..., some_field => test_pdl( 1,2,-7 ), ... };
348 my $expected = [ ..., test_short( 1,2,-7 ), ... ];
349  
350 Suppose you want to compare data structures that happen to contain ndarrays. You
351 use is_deeply() (from L<Test::More>) or cmp_deeply() (from L<Test::Deep>) to
352 compare the structures element by element. Unfortunately, you cannot just write
353  
354 my $got = my_sub( ... );
355 my $expected = {
356 ...,
357 some_field => pdl( ... ),
358 ...
359 };
360 is_deeply $got, $expected;
361  
362 Neither does cmp_deeply() work in the same situation. is_deeply() tries to
363 compare the ndarrays using the (overloaded) C<==> comparison operator, which
364 doesn't work. It simply dies with an error message saying that multidimensional
365 ndarrays cannot be compared, whereas cmp_deeply() performs only a shallow
366 comparison of the references.
367  
368 What you need is a special comparison, which is provided by this function, to
369 be used with cmp_deeply(). You need to rewrite $expected as follows
370  
371 my $expected = {
372 ...,
373 some_field => test_pdl( ... ),
374 ...
375 };
376 cmp_deeply $got, $expected;
377  
378 Note that you need to write test_pdl() instead of pdl(). You could achieve the
379 same thing with
380  
381 my $expected = {
382 ...,
383 some_field => code( sub { eq_pdl( shift, pdl( ... ) ) } ),
384 ...
385 };
386  
387 but the diagnostics provided by test_pdl() are better, and it's easier to use.
388 test_pdl() accepts the same arguments as the PDL constructor pdl() does. If you
389 need to compare an ndarray with a type different from the default type, use one
390 of the provided test_byte(), test_short(), test_long(), etc.:
391  
392 my $expected = { data => test_short( -4,-9,13 ) };
393  
394 If you need to manipulate the expected value, you should keep in mind that the
395 return value of test_pdl() and the like are not ndarrays. Therefore, in-place
396 modification of the expected value won't work:
397  
398 my $expected = { data => test_short( -99,-9,13 )->inplace->setvaltobad( -99 ) }; # won't work!
399  
400 You should rather do
401  
402 my $expected = { data => test_pdl( short(-99,-9,13)->inplace->setvaltobad(-99) ) };
403  
404 test_pdl() will correctly set the type of the expected value to I<short> in the
405 above example.
406  
407 =cut
408  
409 sub test_pdl
410 49 ▶{
Subroutine test_pdl: called CC=1 SCAR=1.2
Pod documented
411 0 ▶ require Test::Deep::PDL;
Subroutine test_pdl: not called CC=1 SCAR=1.2
Pod documented
412 0 my $expected = PDL::Core::pdl( @_ );
413 0 return Test::Deep::PDL->new( $expected );
414 }
415  
416 =for Pod::Coverage test_anyval test_byte test_short test_ushort test_long
417 test_indx test_longlong test_float test_double test_cfloat test_cdouble
418 test_cldouble test_ldouble test_sbyte test_ulong test_ulonglong
419  
420 =cut
421  
422 58 for my $type ( PDL::Types::types ) {
423 60 ▶ my $sub = sub {
Subroutine __ANON__: called CC=2 SCAR=9.2
424 0 ▶ require Test::Deep::PDL;
Subroutine __ANON__: not called CC=2 SCAR=9.2
425 0 ▶ my $expected = PDL::convert(
Branch: 2 branches
BranchTrueFalseTotal
scalar @_ > 1 ? : 45 15 60
scalar @_ > 1 ? : 0 0 0
426 PDL::Core::alltopdl( 'PDL', scalar(@_) > 1 ? [@_] : shift ),
427 $type->numval
428 );
429 0 return Test::Deep::PDL->new( $expected );
430 870 };
431 870 my $sub_name = 'test_' . $type->convertfunc;
432 {
433 58 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
434 870 *$sub_name = $sub;
435 }
436 870 push @EXPORT_OK, $sub_name;
437 870 push @{ $EXPORT_TAGS{deep} }, $sub_name;
438 }
439  
440 =head1 BUGS
441  
442 None reported so far.
443  
444 =head1 SEE ALSO
445  
446 L<PDL>, L<Test::More>, L<Test::Deep>, L<Test::PDL::Deep>
447  
448 =head1 ACKNOWLEDGMENTS
449  
450 Thanks to PDL Porters Joel Berger, Chris Marshall, and David Mertens for
451 feedback and improvements.
452  
453 Thanks to Ed J, Zakariyya Mughal, and Diab Jerius for feedback, improvements,
454 maintenance of the code, and encouragement!
455  
456 =cut
457  
458 58 1;
« blib/lib/PDL/IO/FITS.pm ↑ Summary blib/lib/PDL/IO/Pic.pm »