Skip to content

Commit

Permalink
IO::{Misc,Pnm} tests replace tapprox with is_pdl - #34
Browse files Browse the repository at this point in the history
  • Loading branch information
mohawk2 committed Oct 29, 2024
1 parent db685da commit 4db9639
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 39 deletions.
40 changes: 16 additions & 24 deletions Basic/IO-Misc/t/misc.t
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,7 @@ use PDL::LiteF;
use PDL::IO::Misc;
use File::Temp qw( tempfile tempdir );
use Test::More;

sub tapprox {
my($x,$y) = @_;
my $c = abs($x-$y);
my $d = max($c);
$d < 0.0001;
}
use Test::PDL;

my $tempd = tempdir( CLEANUP => 1 ) or die "Couldn't get tempdir\n";
my ($fileh,$file) = tempfile( DIR => $tempd );
Expand Down Expand Up @@ -154,38 +148,36 @@ EOD
close($fileh);

($x,$y) = rcols $file,0,1;
is( $x->nelem==4 && sum($x)==6 && sum($y)==20, 1,
"rcols: default" );
is_pdl $x, pdl(1,3,-5,7), "rcols: default";
is_pdl $y, pdl(2,4,6,8), "rcols: default";

($x,$y) = rcols \*DATA,0,1;
is( $x->nelem==4 && sum($x)==6 && sum($y)==20, 1,
"rcols: pipe" );
is_pdl $x, pdl(1,3,-5,7), "rcols: fh";
is_pdl $y, pdl(2,4,6,8), "rcols: fh";

($x,$y) = rcols $file,0,1, { INCLUDE => '/^-/' };
is( $x->nelem==1 && $x->at(0)==-5 && $y->at(0)==6, 1,
"rcols: include pattern" );
is_pdl $x, pdl([-5]), "rcols: include pattern";
is_pdl $y, pdl([6]), "rcols: include pattern";

($x,$y) = rcols $file,0,1, { LINES => '-2:0' };
is( $x->nelem==3 && tapprox($x,pdl(-5,3,1)) && tapprox($y,pdl(6,4,2)), 1,
"rcols: lines option" );
is_pdl $x, pdl(-5,3,1), "rcols: lines option";
is_pdl $y, pdl(6,4,2), "rcols: lines option";

use PDL::Types;
($x,$y) = rcols $file, { DEFTYPE => long };
is( $x->nelem==4 && $x->get_datatype==$PDL_L && $y->get_datatype==$PDL_L, 1,
"rcols: deftype option" );
is_pdl $x, long(1,3,-5,7), "rcols: deftype option";
is_pdl $y, long(2,4,6,8), "rcols: deftype option";

($x,$y) = rcols $file, { TYPES => [ ushort ] };
is( $x->nelem==4 && $x->get_datatype==$PDL_US && $y->get_datatype==$PDL_D, 1,
"rcols: types option" );
is_pdl $x, ushort(1,3,-5,7), "rcols: types option";

This comment has been minimized.

Copy link
@mohawk2

mohawk2 Nov 3, 2024

Author Member

Unexpectedly, this has caused #502 due to the convert operation turning -5 into 0.

is_pdl $y, double(2,4,6,8), "rcols: types option";

is( UNIVERSAL::isa($PDL::IO::Misc::deftype,"PDL::Type"), 1,
"PDL::IO::Misc::deftype is a PDL::Type object" );
is( $PDL::IO::Misc::deftype->[0], double->[0],
"PDL::IO::Misc::deftype check" );
isa_ok $PDL::IO::Misc::deftype, "PDL::Type", "PDL::IO::Misc::deftype";
is $PDL::IO::Misc::deftype, 'double', "PDL::IO::Misc::deftype check";

$PDL::IO::Misc::deftype = short;
($x,$y) = rcols $file;
is( $x->get_datatype, short->[0], "rcols: can read in as 'short'" );
is( $x->get_datatype, short->enum, "rcols: can read in as 'short'" );

unlink $file || warn "Could not unlink $file: $!";

Expand Down
16 changes: 4 additions & 12 deletions Basic/IO-Pnm/t/pnm.t
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,7 @@ use PDL::Dbg;
use File::Temp qw(tempdir);
use File::Spec;
use Test::More;

# we need tests with index shuffling once vaffines are fixed

sub tapprox {
my($pa,$pb,$mdiff) = @_;
all approx($pa, $pb,$mdiff || 0.01);
}
use Test::PDL;

my $tmpdir = tempdir( CLEANUP => 1 );
sub rpnm_unlink {
Expand Down Expand Up @@ -63,18 +57,16 @@ for my $raw (0,1) {
foreach my $form (@formats) {
my $in = rpnm_unlink($im2, $form->[1], 'PGM', $raw);
my $comp = ($form->[3] ? $im2->dummy(0,3) : $im2);
ok(tapprox($in,$comp)) or diag "got=$in\nexpected=$comp";
is_pdl $in,$comp;
$comp = $form->[3] ? ($im3->dummy(0,3)>0)*255 : ($im3 > 0);
$comp = $comp->ushort*65535 if $form->[0] eq 'SGI'; # yet another format quirk
$in = rpnm_unlink($im3, $form->[1], 'PBM', $raw);
ok(tapprox($in,$comp)) or diag "got=$in\nexpected=$comp";
is_pdl $in,$comp;
next if $form->[0] eq 'GIF';
$in = rpnm_unlink($im1, $form->[1], 'PGM', $raw);
my $scale = $form->[3] ? $im1->dummy(0,3) : $im1;
$comp = $scale / $form->[2];
ok(tapprox($in,$comp,$form->[4]), $form->[0])
or diag "got=$in\nexpected=$comp", $in->info;
note $in->px if $PDL::debug and $form->[0] ne 'TIFF';
is_pdl $in, $comp, {atol=>$form->[4], test_name=>$form->[0]};
}
}

Expand Down
1 change: 0 additions & 1 deletion Basic/Lib-ImageRGB/t/picnorgb.t
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use File::Spec;
use strict;
use warnings;

# we need tests with index shuffling once vaffines are fixed
use Test::More;

sub tapprox {
Expand Down
2 changes: 0 additions & 2 deletions Basic/Lib-ImageRGB/t/picrgb.t
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ use PDL::Dbg;
use File::Temp qw(tempdir);
use File::Spec;

# we need tests with index shuffling once vaffines are fixed

sub tapprox {
my($pa,$pb,$mdiff) = @_;
all approx($pa, $pb,$mdiff || 0.01);
Expand Down

0 comments on commit 4db9639

Please sign in to comment.