| 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 | 57 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 59 | 57 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 60 | 57 | ▶ | use PDL::Lite; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 61 | 57 | ▶ | use PDL::Types (); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||
| 63 | 57 | ▶ | use base qw( Exporter ); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 64 | 57 | our @EXPORT = qw( is_pdl ); | |||||||||||||||||||||||||||||||
| 65 | 57 | our @EXPORT_OK = qw( eq_pdl is_pdl test_pdl ); | |||||||||||||||||||||||||||||||
| 66 | 57 | our %EXPORT_TAGS = ( deep => [ qw( test_pdl ) ] ); | |||||||||||||||||||||||||||||||
| 67 | 57 | 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 | 57 | 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 | { | ||||||||||||||||||||||||||||||||
| 139 | 63 | ▶ | my $i = 0; | ||||||||||||||||||||||||||||||
|
Subroutine
import: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||
| 140 | 63 | ▶ | while( $i < @_ ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 141 | 82 | ▶ | if( $_[ $i ] =~ /^-/ ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 142 | 15 | my( $key, $val ) = splice @_, $i, 2; | |||||||||||||||||||||||||||||||
| 143 | 15 | $key =~ s/^-(.*)/$1/; | |||||||||||||||||||||||||||||||
| 144 | 15 | ▶ | PDL::barf( "invalid name $key" ) unless grep { $key eq $_ } keys %DEFAULTS; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 145 | 14 | ▶ | PDL::barf( "undefined value for $key" ) unless defined $val; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 146 | 14 | $DEFAULTS{ $key } = $val; | |||||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||||||
| 148 | 67 | else { $i++ } | |||||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||||||
| 150 | 62 | __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 | sub is_pdl { | ||||||||||||||||||||||||||||||||
| 178 | 1609 | ▶ | require Test::Builder; | ||||||||||||||||||||||||||||||
|
Subroutine
is_pdl: called CC=16 SCAR=27.8
Pod
documented
| |||||||||||||||||||||||||||||||||
| 179 | 1609 | my $tb = Test::Builder->new; | |||||||||||||||||||||||||||||||
| 180 | 1609 | ▶ | $tb->croak('error in arguments: > 3 given') if @_ > 3; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 181 | 1608 | my ( $got, $expected, $arg ) = @_; | |||||||||||||||||||||||||||||||
| 182 | $tb->croak('error in arguments: third argument is an ndarray') | ||||||||||||||||||||||||||||||||
| 183 | 1608 | ▶ | if eval { $arg->isa('PDL') }; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 184 | 1607 | my $opt = { %DEFAULTS }; | |||||||||||||||||||||||||||||||
| 185 | 1607 | my $name; | |||||||||||||||||||||||||||||||
| 186 | 1607 | ▶ | if ($arg) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 187 | 1350 | ▶ | if (ref $arg eq 'HASH') { $opt = { %$opt, %$arg } } | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 188 | 1234 | else { $name = $arg } | |||||||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||||||
| 190 | 1607 | ▶ | $name ||= $opt->{test_name} || "ndarrays are equal"; | ||||||||||||||||||||||||||||||
|
Condition80%4/5
$name ||= $opt->{"test_name"} || "ndarrays are equal"
$opt->{'test_name'} || 'ndarrays are equal'
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'}
| |||||||||||||||||||||||||||||||||
| 191 | 1607 | my ($ok, $reason, $mask) = eq_pdl($got, $expected, $opt); | |||||||||||||||||||||||||||||||
| 192 | 1607 | ▶ | return $tb->ok(1, $name) if $ok; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 193 | 15 | my $rc = $tb->ok( 0, $name ); | |||||||||||||||||||||||||||||||
| 194 | 15 | my $fmt = '%-8T %-12D (%-5S) '; | |||||||||||||||||||||||||||||||
| 195 | 15 | my @mismatch; | |||||||||||||||||||||||||||||||
| 196 | 15 | ▶ | if (defined $mask) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 197 | 7 | ▶ | my $coords = defined $mask ? $mask->not->whichND : undef; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 198 | 7 | ▶ | $coords = $coords->slice(',0:4') if defined $coords and $coords->dim(1) > 5; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $coords and $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
| |||||||||||||||||||||||||||||||||
| 199 | 7 | my $cstr = $coords->string; $cstr =~ s#\n+\z##; | |||||||||||||||||||||||||||||||
| 200 | 7 | 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 | 15 | ▶ | " got: ", eval { $got->isa('PDL') && !$got->isnull } ? $got->info( $fmt ) : '', $got, "\n", | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 209 | 15 | ▶ | " expected: ", eval { $expected->isa('PDL') && !$expected->isnull } ? $expected->info( $fmt ) : '', $expected, | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 210 | @mismatch, | ||||||||||||||||||||||||||||||||
| 211 | ); | ||||||||||||||||||||||||||||||||
| 212 | 15 | 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 | sub eq_pdl { | ||||||||||||||||||||||||||||||||
| 302 | 1861 | ▶ | my ($got, $expected, $arg) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
eq_pdl: called CC=29 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 303 | 1861 | ▶ | my $opt = { %DEFAULTS, ref $arg eq 'HASH' ? %$arg : () }; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 304 | 1861 | ▶ | PDL::barf( 'need an absolute or a relative tolerance, or both' ) unless defined $opt->{atol} || defined $opt->{rtol}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $opt->{"atol"} or 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"}
| |||||||||||||||||||||||||||||||||
| 305 | 1861 | ▶ | $opt->{atol} //= 0; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$opt->{"atol"} //= 0
MC/DC0%0/1
$opt->{"atol"} //= 0
$opt->{"atol"}
Truth table50%1/2
A
A= $opt->{"atol"}
| |||||||||||||||||||||||||||||||||
| 306 | 1861 | ▶ | $opt->{rtol} //= 0; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$opt->{"rtol"} //= 0
MC/DC0%0/1
$opt->{"rtol"} //= 0
$opt->{"rtol"}
Truth table50%1/2
A
A= $opt->{"rtol"}
| |||||||||||||||||||||||||||||||||
| 307 | 1861 | ▶ | PDL::barf('absolute tolerance cannot be negative') if $opt->{atol} < 0; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 308 | 1861 | ▶ | PDL::barf('relative tolerance cannot be negative') if $opt->{rtol} < 0; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 309 | return wantarray ? (0, 'received value is not an ndarray', undef) : 0 | ||||||||||||||||||||||||||||||||
| 310 | 1861 | ▶ | if !eval { $got->isa('PDL') }; | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 311 | return wantarray ? (0, 'expected value is not an ndarray', undef) : 0 | ||||||||||||||||||||||||||||||||
| 312 | 1851 | ▶ | if !eval { $expected->isa('PDL') }; | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 313 | return wantarray ? (0, 'types do not match (\'require_equal_types\' is true)', undef) : 0 | ||||||||||||||||||||||||||||||||
| 314 | 1846 | ▶ | if $opt->{require_equal_types} && $got->type != $expected->type; | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
$opt->{"require_equal_types"} and $got->type != $expected->type
MC/DC100%2/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
| |||||||||||||||||||||||||||||||||
| 315 | 1840 | my @got_dims = $got->dims; | |||||||||||||||||||||||||||||||
| 316 | 1840 | my @exp_dims = $expected->dims; | |||||||||||||||||||||||||||||||
| 317 | 1840 | ▶ | return wantarray ? (0, 'dimensions do not match in number', undef) : 0 | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 318 | if @got_dims != @exp_dims; | ||||||||||||||||||||||||||||||||
| 319 | 1834 | ▶ | while (@got_dims) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 320 | 2246 | ▶ | return wantarray ? (0, 'dimensions do not match in extent', undef) : 0 | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 321 | if shift(@got_dims) != shift(@exp_dims); | ||||||||||||||||||||||||||||||||
| 322 | } | ||||||||||||||||||||||||||||||||
| 323 | 1823 | ▶ | return wantarray ? (1, '', undef) : 1 | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
$got->isempty and $expected->isempty
MC/DC50%1/2
$got->isempty and $expected->isempty
$got->isempty
$expected->isempty
Truth table66%2/3
A and B
A= $got->isemptyB= $expected->isempty
| |||||||||||||||||||||||||||||||||
| 324 | if $got->isempty and $expected->isempty; | ||||||||||||||||||||||||||||||||
| 325 | # both are now non-empty | ||||||||||||||||||||||||||||||||
| 326 | 1761 | my $got_is_inplace = $got->is_inplace; | |||||||||||||||||||||||||||||||
| 327 | 1761 | my $exp_is_inplace = $expected->is_inplace; | |||||||||||||||||||||||||||||||
| 328 | 1761 | $_->set_inplace(0) for $got, $expected; | |||||||||||||||||||||||||||||||
| 329 | 1761 | my $res = PDL::Primitive::approx_artol( $got, $expected, @$opt{qw(atol rtol)} ); | |||||||||||||||||||||||||||||||
| 330 | 1761 | ▶ | $got->set_inplace(1) if $got_is_inplace; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 331 | 1761 | ▶ | $expected->set_inplace(1) if $exp_is_inplace; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 332 | 1761 | ▶ | return wantarray ? (1, '', undef) : 1 if $res->all; | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 333 | 44 | my $exp_nelem = $expected->nelem; | |||||||||||||||||||||||||||||||
| 334 | 44 | my $reason = ($exp_nelem-$res->sum)."/$exp_nelem values do not match"; | |||||||||||||||||||||||||||||||
| 335 | 44 | ▶ | return wantarray ? (0, $reason, $res) : 0; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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 | { | ||||||||||||||||||||||||||||||||
| 411 | 49 | ▶ | require Test::Deep::PDL; | ||||||||||||||||||||||||||||||
|
Subroutine
test_pdl: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 412 | 49 | my $expected = PDL::Core::pdl( @_ ); | |||||||||||||||||||||||||||||||
| 413 | 49 | 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 | 57 | for my $type ( PDL::Types::types ) { | |||||||||||||||||||||||||||||||
| 423 | my $sub = sub { | ||||||||||||||||||||||||||||||||
| 424 | 60 | ▶ | require Test::Deep::PDL; | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 425 | 60 | ▶ | my $expected = PDL::convert( | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 426 | PDL::Core::alltopdl( 'PDL', scalar(@_) > 1 ? [@_] : shift ), | ||||||||||||||||||||||||||||||||
| 427 | $type->numval | ||||||||||||||||||||||||||||||||
| 428 | ); | ||||||||||||||||||||||||||||||||
| 429 | 60 | return Test::Deep::PDL->new( $expected ); | |||||||||||||||||||||||||||||||
| 430 | 855 | }; | |||||||||||||||||||||||||||||||
| 431 | 855 | my $sub_name = 'test_' . $type->convertfunc; | |||||||||||||||||||||||||||||||
| 432 | { | ||||||||||||||||||||||||||||||||
| 433 | 57 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 434 | 855 | *$sub_name = $sub; | |||||||||||||||||||||||||||||||
| 435 | } | ||||||||||||||||||||||||||||||||
| 436 | 855 | push @EXPORT_OK, $sub_name; | |||||||||||||||||||||||||||||||
| 437 | 855 | 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 | 57 | 1; | |||||||||||||||||||||||||||||||