Skip to content

Commit

Permalink
IO::{STL,Dumper,FlexRaw} tests replace approx with is_pdl - #34
Browse files Browse the repository at this point in the history
  • Loading branch information
mohawk2 committed Oct 30, 2024
1 parent 1dff102 commit 0b3cd01
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 22 deletions.
31 changes: 14 additions & 17 deletions Basic/IO-FlexRaw/t/flexraw.t
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use strict;
use warnings;

use Test::More;
use Test::PDL;
use File::Temp qw(tempdir);
use File::Spec::Functions;
use PDL::IO::FlexRaw;
Expand All @@ -32,15 +33,15 @@ my $header_bis = [ { %{$header->[0]}, Dims => [2, undef] } ];
eval { readflex($name, [@$header_bis, @$header_bis]) };
like $@, qr/>1 header/, 'readflex only allows undef dim when only one hash';
my $x_bis = readflex($name, $header_bis);
ok(all(approx($x_bis,$x)), "read back with undef highest dim correct");
is_pdl $x_bis, $x, "read back with undef highest dim correct";

# **TEST 3** save a header to disk
eval { writeflexhdr($name, $header) };
ok(-f "$name.hdr", "writeflexhdr should create a header file");

# **TEST 4** read it back, and make sure it gives the same ndarray
my $y = eval { readflex($name) };
ok(all(approx($x,$y)), "A ndarray and its saved copy should be about equal");
is_pdl $x, $y, "A ndarray and its saved copy should be about equal";

# **TEST 5** save two ndarrays to disk
my ($c1, $c2) = ([0,0,0,0],[0,0,0,0]);
Expand All @@ -49,16 +50,15 @@ my $d = pdl [1,1,1];
my $cdname = $name . 'cd';
$header = eval { writeflex($cdname, $c, $d) };
ok((-f $cdname), "writeflex saves 2 pdls to a file");

# **TEST 6** save a header to disk
eval { writeflexhdr($cdname, $header) };
ok(-f "$cdname.hdr", "writeflexhdr create a header file");

# **TEST 7** read it back, and make sure it gives the same ndarray
# This is sf.net bug #3375837 "_read_flexhdr state machine fails"
my (@cd) = eval { no warnings; readflex($cdname) };
ok( (scalar(@cd)==2 and all(approx($cd[0],$c)) and all(approx($cd[1],$d)) ), 'sf.net bug 3375837');

is 0+@cd, 2, 'sf.net bug 3375837';
is_pdl $cd[0], $c, 'sf.net bug 3375837';
is_pdl $cd[1], $d, 'sf.net bug 3375837';
# Clean up for another test
unlink $cdname, $cdname . '.hdr'; # just to be absolutely sure

Expand All @@ -67,14 +67,14 @@ my $gname = $name.'g';
local $PDL::IO::FlexRaw::writeflexhdr = 1;
eval { writeflex($gname, $d, $c) }; # 2D last so can append
my @dc = eval { readflex($gname) };
ok all(approx $dc[0], $d);
ok all(approx $dc[1], $c);
is_pdl $dc[0], $d;
is_pdl $dc[1], $c;
my $e = pdl(2,2,2,2);
eval { glueflex($gname, $e) };
is $@, '', 'no error glueflex';
@dc = eval { readflex($gname) };
ok all(approx $dc[0], $d);
ok all(approx $dc[1], pdl($c1,$c2,$e));
is_pdl $dc[0], $d;
is_pdl $dc[1], pdl($c1,$c2,$e);
}

# some mapflex tests
Expand All @@ -89,7 +89,7 @@ SKIP: {
}

# **TEST 8** compare mapfraw ndarray with original ndarray
ok(all(approx($x,$c)), "An ndarray and its mapflex representation should be about equal");
is_pdl $x, $c, "An ndarray and its mapflex representation should be about equal";

# **TEST 9** modifications should be saved when $c goes out of scope
# THIS TEST FAILS.
Expand All @@ -100,7 +100,7 @@ SKIP: {
$c += 1;
undef $c;
$y = readflex($name);
ok(all(approx($x+1,$y)), "Modifications to mapfraw should be saved to disk no later than when the ndarray ceases to exist");
is_pdl $x+1, $y, "Modifications to mapfraw should be saved to disk no later than when the ndarray ceases to exist";

# We're starting a new test, so we'll remove the files we've created so far
# and clean up the memory, just to be super-safe
Expand All @@ -124,11 +124,8 @@ SKIP: {
# Load it back up and see if the values are what we expect
$y = readflex($name);
# **TEST 11**
ok(all(approx($y, PDL->pdl([[0,1,2],[0.1,1.1,2.1]]))),
"mapfraw should be able to create new ndarrays");

# **TEST 12** test the created type
ok($y->type->[0] == (&float)->[0], 'type should be of the type we specified (float)');
is_pdl $y, float([[0,1,2],[0.1,1.1,2.1]]),
"mapfraw should be able to create new ndarrays";

undef $x; undef $y; # cleanup
# test for bug mentioned in https://perlmonks.org/?node_id=387256
Expand Down
7 changes: 3 additions & 4 deletions IO/STL/t/basic.t
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use strict;
use warnings;
use Test::More;
use Test::PDL;
use PDL::LiteF;
use PDL::IO::STL;
use File::Spec::Functions;
Expand All @@ -22,8 +23,7 @@ my $cubev = pdl(
[[0,1,0], [1,1,1], [1,1,0]],
);
my ($vertices, $faceidx) = rstl(catfile qw(t cube.stl));
ok all(approx $vertices->dice_axis(1, $faceidx->flat)->splitdim(1,3), $cubev)
or diag "vertices=$vertices\nfaceidx=$faceidx";
is_pdl $vertices->dice_axis(1, $faceidx->flat)->splitdim(1,3), $cubev;

eval {wstl()};
like $@, qr/Usage:/, 'wstl error right';
Expand All @@ -32,8 +32,7 @@ like $@, qr/Usage:/, 'wstl error right';
my $fh = tempfile(CLEANUP => 1);
wstl $fh, $vertices, $faceidx;
my ($v2, $f2) = rstl($fh);
ok all(approx $v2->dice_axis(1, $f2->flat)->splitdim(1,3), $cubev)
or diag "v2=$v2\nf2=$f2";
is_pdl $v2->dice_axis(1, $f2->flat)->splitdim(1,3), $cubev;
}

done_testing;
3 changes: 2 additions & 1 deletion IO/t/dumper.t
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use strict;
use warnings;
use PDL::IO::Dumper;
use Test::More;
use Test::PDL;
use Config;
use PDL::LiteF;

Expand Down Expand Up @@ -39,7 +40,7 @@ is $@, '', 'Can eval dumped 25x25 PDL' or diag 'string: ', $s;
ok((ref $x eq 'HASH'), 'HASH structure for uuencoded 25x25 PDL restored');
isa_ok $x->{e}, 'PDL';
is $x->{e}->nelem, 625;
ok all(approx $x->{e}, xvals(25,25)), 'Verify 25x25 PDL restored data';
is_pdl $x->{e}, xvals(25,25), 'Verify 25x25 PDL restored data';

########## Check header dumping...
my $y;
Expand Down

0 comments on commit 0b3cd01

Please sign in to comment.