Navigation Menu

Skip to content

Commit

Permalink
test cleanup: imagend.t
Browse files Browse the repository at this point in the history
  • Loading branch information
zmughal committed Mar 3, 2015
1 parent 7614054 commit 16da656
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 75 deletions.
1 change: 0 additions & 1 deletion TODO
Expand Up @@ -45,7 +45,6 @@ support for 2-D graphics (via PLplot) and 3-D graphics


Refactor to use Test::More
* imagend.t
* imagergb.t
* interp.t
* interp_slatec.t
Expand Down
162 changes: 88 additions & 74 deletions t/imagend.t
@@ -1,84 +1,98 @@
use t::lib::TestHelper; # TODO migrate
use PDL;
use PDL::ImageND;
use PDL::NiceSlice;

kill INT,$$ if $ENV{UNDER_DEBUGGER}; # Useful for debugging.
use Test::More tests => 7;
use strict;
use warnings;

print "1..7\n";
kill 'INT',$$ if $ENV{UNDER_DEBUGGER}; # Useful for debugging.

# Right answer

my $ans = pdl(
[ 3, 9, 15, 21, 27, 33, 39, 45, 51, 27],
[ 3, 9, 15, 21, 27, 33, 39, 45, 51, 27],
[ 3, 9, 15, 21, 27, 33, 39, 45, 51, 27]
);

my $a = xvals zeroes 10,3;

my $b = pdl [1,2],[2,1];

my $c=convolve ($a, $b);

num_ok( 1, int(at(sum($c-$ans)))==0 );


$a = zeroes(6,6);

my $ta;

($ta = $a(4,:)) .= 1;
($ta = $a(5,:)) .= 1;
($ta = $a(1,2)) .= 1;
($ta = $a(0,4)) .= 1;
($ta = $a(2,0)) .= 1;
my $eps = 1e-15;

$b = pdl( [-1,0],[0,1] );


my $ans_e = pdl(
[ 0, 0, 1, -1, 0, 0],
[-1, 0, 0, -1, 0, 0],
[ 0, 1, 0, -1, 0, 0],
[ 0, 0, 0, -1, 0, 0],
[ 1, 0, 0, -1, 0, 0],
[ 0, 0, 0, -1, 0, 0]
);
$c = convolveND($a,$b,{m=>'d',b=>'e'});
num_ok( 2, all( abs($c - $ans_e) < 1e-15 ) );

$c = convolveND($a,$b,{m=>'f',b=>'e'});
num_ok( 3, all( abs($c - $ans_e) < 1e-15 ) );

$ans_p = pdl(
[ 0, 0, 1, -1, 0, 1],
[-1, 0, 0, -1, 0, 1],
[ 0, 1, 0, -1, 0, 1],
[ 0, 0, 0, -1, 0, 0],
[ 1, 0, 0, -1, 0, 1],
[ 0, -1, 0, -1, 0, 1]
);
$c = convolveND($a,$b,{m=>'d',b=>'p'});
num_ok( 4, all( abs($c - $ans_p) < 1e-15 ) );

$c = convolveND($a,$b,{m=>'f',b=>'p'});
num_ok( 5, all( abs($c - $ans_p) < 1e-15 ) );
# Right answer


$ans_t = pdl(
[ 0, 0, 1, -1, 0, 1],
[-1, 0, 0, -1, 0, 1],
[ 0, 1, 0, -1, 0, 1],
[ 0, 0, 0, -1, 0, 1],
[ 1, 0, 0, -1, 0, 1],
[ 0, 0, 0, 0, 1, 1]
{
my $ans = pdl(
[ 3, 9, 15, 21, 27, 33, 39, 45, 51, 27],
[ 3, 9, 15, 21, 27, 33, 39, 45, 51, 27],
[ 3, 9, 15, 21, 27, 33, 39, 45, 51, 27]
);
$c = convolveND($a,$b,{m=>'d',b=>'t'});
num_ok( 6, all( abs($c - $ans_t) < 1e-15 ) );

$c = convolveND($a,$b,{m=>'f',b=>'t'});
num_ok( 7, all( abs($c - $ans_t) < 1e-15 ) );



my $pa = xvals zeroes 10,3;
my $pb = pdl [1,2],[2,1];
my $pc = convolve ($pa, $pb);
ok(all PDL::approx( $pc, $ans, $eps ) );
}


my $pa = zeroes(6,6);
my $pb = pdl( [-1,0],[0,1] );

{
# XXX Dead code
my $ta;

($ta = $pa(4,:)) .= 1;
($ta = $pa(5,:)) .= 1;
($ta = $pa(1,2)) .= 1;
($ta = $pa(0,4)) .= 1;
($ta = $pa(2,0)) .= 1;
}


{
my $pc;
my $ans_e = pdl(
[ 0, 0, 1, -1, 0, 0],
[-1, 0, 0, -1, 0, 0],
[ 0, 1, 0, -1, 0, 0],
[ 0, 0, 0, -1, 0, 0],
[ 1, 0, 0, -1, 0, 0],
[ 0, 0, 0, -1, 0, 0]
);
$pc = convolveND($pa,$pb,{m=>'d',b=>'e'});
ok( all PDL::approx($pc,$ans_e, $eps) );

$pc = convolveND($pa,$pb,{m=>'f',b=>'e'});
ok( all PDL::approx($pc,$ans_e, $eps) );
}

{
my $pc;
my $ans_p = pdl(
[ 0, 0, 1, -1, 0, 1],
[-1, 0, 0, -1, 0, 1],
[ 0, 1, 0, -1, 0, 1],
[ 0, 0, 0, -1, 0, 0],
[ 1, 0, 0, -1, 0, 1],
[ 0, -1, 0, -1, 0, 1]
);

$pc = convolveND($pa,$pb,{m=>'d',b=>'p'});
ok( all( PDL::approx($pc, $ans_p, $eps) ) );

$pc = convolveND($pa,$pb,{m=>'f',b=>'p'});
ok(all PDL::approx($pc, $ans_p, $eps) );
}


{
my $pc;
my $ans_t = pdl(
[ 0, 0, 1, -1, 0, 1],
[-1, 0, 0, -1, 0, 1],
[ 0, 1, 0, -1, 0, 1],
[ 0, 0, 0, -1, 0, 1],
[ 1, 0, 0, -1, 0, 1],
[ 0, 0, 0, 0, 1, 1]
);
$pc = convolveND($pa,$pb,{m=>'d',b=>'t'});
ok(all PDL::approx($pc,$ans_t, $eps) );

$pc = convolveND($pa,$pb,{m=>'f',b=>'t'});
ok( all( PDL::approx($pc, $ans_t, $eps) ) );
}


done_testing;

0 comments on commit 16da656

Please sign in to comment.