Skip to content

Commit 1dff102

Browse files
committed
IO::FITS tests replace approx with is_pdl - #34
1 parent c7208ab commit 1dff102

File tree

1 file changed

+15
-53
lines changed

1 file changed

+15
-53
lines changed

Basic/IO-FITS/t/fits.t

+15-53
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use File::Basename;
44
use PDL::LiteF;
55
use PDL::Core ':Internal'; # For howbig()
66
use Test::More;
7+
use Test::PDL;
78
use Test::Exception;
89
use PDL::IO::FITS;
910
require File::Spec;
@@ -21,24 +22,17 @@ my $t = long xvals(zeroes(11,20))-5;
2122
wfits($t, $file); # without a header
2223
my $t2 = rfits $file;
2324
unlike $t2->hdr->{COMMENT}, qr/HASH/, 'no "HASH" garbage in written header';
24-
2525
# note: keywords are converted to uppercase
2626
my %hdr = ('Foo'=>'foo', 'Bar'=>42, 'NUM'=>'0123',NUMSTR=>['0123']);
2727
$t->sethdr(\%hdr);
28-
2928
wfits($t, $file);
3029
$t2 = rfits $file;
31-
32-
is( sum($t->slice('0:4,:')), -sum($t2->slice('5:-1,:')),
33-
"r/wfits: slice check" );
34-
30+
is_pdl $t2, $t, 'w/rfits round-trip';
3531
my $h = $t2->gethdr;
3632
ok( $$h{FOO} eq "foo" && $$h{BAR} == 42,
3733
"header check on FOO/BAR" );
38-
3934
ok( $$h{'NUM'}+1 == 124 && $$h{'NUMSTR'} eq '0123',
4035
"header check on NUM/NUMSTR" );
41-
4236
unlink $file;
4337

4438
SKIP: {
@@ -53,28 +47,6 @@ SKIP: {
5347
# instead they write out a file, read it back in, and
5448
# compare to the data used to create the file.
5549
# So it is more of a "self consistent" test.
56-
#
57-
sub compare_ndarrays ($$$) {
58-
my $orig = shift;
59-
my $new = shift;
60-
my $label = shift;
61-
62-
TODO: {
63-
local $TODO = "Need to fix alias between PDL_IND and PDL_L or PDL_LL";
64-
65-
is( $new->type->symbol, $orig->type->symbol, "$label has the correct type" );
66-
}
67-
is( $new->nelem, $orig->nelem, " and the right number of elements" );
68-
is( $new->ndims, $orig->ndims, " and the right number of dimensions" );
69-
70-
my $flag;
71-
if ( $orig->type() < float() ) {
72-
$flag = all( $new == $orig );
73-
} else {
74-
$flag = all( approx( $orig, $new ) );
75-
}
76-
ok( $flag, " and all the values agree" );
77-
}
7850

7951
unless($PDL::Astro_FITS_Header) {
8052
# Astro::FITS::Header is not present, ignore table tests
@@ -99,8 +71,8 @@ unless($PDL::Astro_FITS_Header) {
9971
is( $$table2{hdr}{TTYPE2}, "COLB", "column #2 is COLB" ); #11
10072
is( $$table2{hdr}{TFORM2}, "1D", " stored as 1D" ); #12
10173

102-
compare_ndarrays $x, $$table2{COLA}, "COLA"; #13-16
103-
compare_ndarrays $y, $$table2{COLB}, "COLB"; #17-20
74+
is_pdl $x, $$table2{COLA}, "COLA"; #13-16
75+
is_pdl $y, $$table2{COLB}, "COLB"; #17-20
10476

10577
$table = { BAR => $x, FOO => $y,
10678
hdr => { TTYPE1 => 'FOO', TTYPE2 => 'BAR' } };
@@ -116,8 +88,8 @@ unless($PDL::Astro_FITS_Header) {
11688
is( $$table2{hdr}{TTYPE2}, "BAR", "column #2 is BAR" ); #24
11789
is( $$table2{hdr}{TFORM2}, "1J", " stored as 1J" ); #25
11890

119-
compare_ndarrays $x, $$table2{BAR}, "BAR"; #26-29
120-
compare_ndarrays $y, $$table2{FOO}, "FOO"; #30-33
91+
is_pdl $x, $$table2{BAR}, "BAR"; #26-29
92+
is_pdl $y, $$table2{FOO}, "FOO"; #30-33
12193

12294
# try out more "exotic" data types
12395

@@ -139,12 +111,8 @@ unless($PDL::Astro_FITS_Header) {
139111
ok( defined $table2 && ref($table2) eq "HASH" && $$table2{tbl} eq "binary",
140112
"Read in the third binary table" ); #34
141113
my @elem = sort keys %$table2;
142-
##my @expected = sort( qw( ACOL BCOL CCOL DCOL ECOL FCOL hdr tbl ) );
143-
##is ( $#elem+1, 8, "hash contains 8 elements" );
144114
my @expected = sort( qw( ACOL BCOL CCOL DCOL ECOL hdr tbl ) );
145-
is ( $#elem+1, 7, "hash contains 7 elements" ); #35
146-
ok( eq_array( \@elem, \@expected ), "hash contains expected
147-
keys" ); #36
115+
is_deeply \@elem, \@expected, "hash contains expected keys";
148116

149117
# convert the string array so that each element has the same length
150118
# (and calculate the maximum length to use in the check below)
@@ -173,13 +141,12 @@ unless($PDL::Astro_FITS_Header) {
173141
is( $$table2{hdr}{"TFORM$i"}, $$colinfo[1], " and is stored as $$colinfo[1]" ); #38,44,50,56,59
174142
my $col = $$table2{$$colinfo[0]};
175143
if ( UNIVERSAL::isa($col,"PDL") ) {
176-
compare_ndarrays $col, $$colinfo[2], $$colinfo[0]; #39-42,45-48,51-54,60-63
144+
is_pdl $col, $$colinfo[2], $$colinfo[0]; #39-42,45-48,51-54,60-63
177145
} else {
178146
# Need to somehow handle the arrays since the data read in from the
179147
# file all have 15-character length strings (or whatever the length is)
180148
#
181-
ok( eq_array($col, $$colinfo[2]),
182-
" $$colinfo[0] values agree (as an array reference)" );#57
149+
is_deeply $col, $$colinfo[2], "$$colinfo[0] values agree (as an array reference)";
183150
}
184151
$i++;
185152
}
@@ -321,39 +288,34 @@ if(-w dirname($tildefile)) {
321288
{
322289
(undef, my $fname) = File::Temp::tempfile( 'delmeXXXXX', SUFFIX => '.fits', %tmp_opts );
323290
my $x = sequence(10)->setbadat(0);
324-
#diag "Writing to fits: $x type = (", $x->get_datatype, ")\n";
325291
$x->wfits($fname);
326292
my $y = rfits($fname);
327-
#diag "Read from fits: $y type = (", $y->get_datatype, ")\n";
328-
ok( $y->slice('0:0')->isbad, "rfits/wfits propagated bad flag" );
329-
ok( sum(abs($x-$y)) < 1.0e-5, " and values" );
293+
is_pdl $y, $x, "wfits/rfits propagated bad flag and values";
330294
# now force to integer
331295
$x->wfits($fname,16);
332296
$y = rfits($fname);
333-
my $got = $y->slice('0:0');
334-
ok( $got->isbad, "wfits coerced bad flag with integer datatype" ) or diag "got: $got (from $y)";
335-
ok( sum(abs(convert($x,short)-$y)) < 1.0e-5, " and the values" );
297+
is_pdl $y, $x->short, "integer wfits/rfits propagated bad flag and values";
336298
}
337299

338300
{
339301
my $m51 = rfits('t/m51.fits.fz');
340-
is_deeply [$m51->dims], [384,384], 'right dims from compressed FITS file';
302+
is_pdl $m51->shape, indx([384,384]), 'right dims from compressed FITS file';
341303
(undef, my $fname) = File::Temp::tempfile( 'delmeXXXXX', SUFFIX => '.fits', %tmp_opts );
342304
if ($PDL::Astro_FITS_Header) {
343305
my $m51_tbl = rfits('t/m51.fits.fz',{expand=>0});
344306
wfits($m51_tbl, $fname);
345307
my $m51_2 = rfits($fname);
346-
ok all(approx $m51, $m51_2), 'read back written-out bintable FITS file' or diag "got:", $m51_2->info;
308+
is_pdl $m51_2, $m51, 'read back written-out bintable FITS file';
347309
$m51->wfits($fname, {compress=>1});
348310
$m51_2 = rfits($fname);
349-
ok all(approx $m51, $m51_2), 'read back written-out compressed FITS file' or diag "got:", $m51_2->info;
311+
is_pdl $m51_2, $m51, 'read back written-out compressed FITS file';
350312
$m51_2->hdrcpy(1);
351313
$m51_2 = $m51_2->dummy(2,3)->sever;
352314
$m51_2->hdr->{NAXIS} = 3;
353315
$m51_2->hdr->{NAXIS3} = 3;
354316
$m51_2->wfits($fname, {compress=>1});
355317
my $m51_3 = rfits($fname);
356-
ok all(approx $m51_3, $m51_2), 'read back written-out compressed RGB FITS file' or diag "got:", $m51_3->info;
318+
is_pdl $m51_3, $m51_2, 'read back written-out compressed RGB FITS file';
357319
}
358320
}
359321

0 commit comments

Comments
 (0)