Skip to content

Commit db685da

Browse files
committed
IO::*Raw tests replace tapprox with is_pdl - #34
1 parent 66b3c5a commit db685da

File tree

3 files changed

+34
-69
lines changed

3 files changed

+34
-69
lines changed

Basic/IO-FastRaw/t/fastraw.t

+9-14
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use strict;
22
use warnings;
33
use Test::More;
44
use PDL::LiteF;
5+
use Test::PDL;
56
use File::Temp qw(tempdir);
67
use File::Spec::Functions;
78
use PDL::IO::FastRaw;
@@ -13,11 +14,6 @@ my $name = catfile($tmpdir, "tmp0");
1314
my $name_hdr = "$name.hdr";
1415
my $header = catfile($tmpdir, "headerfile" . $$);
1516

16-
sub tapprox {
17-
my($x,$y) = @_;
18-
my $c = abs($x-$y);
19-
return (max($c) < 0.01);
20-
}
2117
sub startdata { pdl [2,3],[4,5],[6,7] }
2218
sub cleanfiles { unlink for grep -f, $name, $name_hdr, $header }
2319

@@ -28,7 +24,7 @@ ok((-f $name and -f ($name_hdr)), "Writing should create a file and header file"
2824

2925
# read it back, and make sure it gives the same ndarray
3026
my $y = readfraw($name);
31-
ok(tapprox($x,$y), "A ndarray and its saved copy should be about equal");
27+
is_pdl $x, $y, "A ndarray and its saved copy should be about equal";
3228

3329
# Clean things up a bit
3430
undef $x; undef $y;
@@ -40,8 +36,7 @@ writefraw($x,"$name.g");
4036
my $x1 = pdl [10,11];
4137
gluefraw($x1,"$name.g");
4238
$y = readfraw("$name.g");
43-
ok(tapprox($y, pdl([2,3],[4,5],[6,7],[10,11])), "glued data correct")
44-
or diag "got:$y";
39+
is_pdl $y, pdl([2,3],[4,5],[6,7],[10,11]), "glued data correct";
4540
unlink "$name.g", "$name.g.hdr";
4641
# Clean things up a bit
4742
undef $x; undef $y;
@@ -53,7 +48,7 @@ ok -f $header, "writefraw should create the special header file when specified";
5348

5449
# test the use of a custom header for reading
5550
$y = readfraw($name,{Header => $header});
56-
ok tapprox($x,$y), "Should be able to read given a specified header";
51+
is_pdl $x, $y, "Should be able to read given a specified header";
5752

5853
# some mapfraw tests
5954
SKIP:
@@ -68,13 +63,13 @@ SKIP:
6863
}
6964

7065
# compare mapfraw ndarray with original ndarray
71-
ok(tapprox($x,$c), "A ndarray and its mapfraw representation should be about equal");
66+
is_pdl $x, $c, "A ndarray and its mapfraw representation should be about equal";
7267

7368
# modifications should be saved when $c goes out of scope
7469
$c += 1;
7570
undef $c;
7671
$y = readfraw($name);
77-
ok(tapprox($x+1,$y), "Modifications to mapfraw should be saved to disk no later than when the ndarray ceases to exist");
72+
is_pdl $x+1,$y, "Modifications to mapfraw should be saved to disk no later than when the ndarray ceases to exist";
7873

7974
# We're starting a new test, so we'll remove the files we've created so far
8075
# and clean up the memory, just to be super-safe
@@ -91,8 +86,8 @@ SKIP:
9186
undef $x; undef $y;
9287
# Load it back up and see if the values are what we expect
9388
$y = readfraw($name);
94-
ok(tapprox($y, PDL->pdl([[0,1,2],[0.1,1.1,2.1]])),
95-
"mapfraw should be able to create new ndarrays");
89+
is_pdl $y, float([[0,1,2],[0.1,1.1,2.1]]),
90+
"mapfraw should be able to create new ndarrays";
9691

9792
# test the created type
9893
ok($y->type == float, 'type should be of the type we specified (float)');
@@ -115,7 +110,7 @@ SKIP:
115110
}
116111

117112
# test custom headers for mapfraw
118-
ok(tapprox($x,$c), "mapfraw should be able to work with a specified header");
113+
is_pdl $x, $c, "mapfraw works with a specified header";
119114
}
120115

121116
done_testing;

Basic/IO-FlexRaw/t/flexraw.t

-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ use strict;
1010
use warnings;
1111

1212
use Test::More;
13-
use PDL::LiteF;
1413
use File::Temp qw(tempdir);
1514
use File::Spec::Functions;
1615
use PDL::IO::FlexRaw;

Basic/IO-FlexRaw/t/flexraw_fortran.t

+25-54
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use PDL::LiteF;
44
use PDL::IO::FlexRaw;
55
use Config;
66
use Test::More;
7+
use Test::PDL;
78
use File::Temp qw(tempfile);
89
use File::Spec;
910
use File::Which ();
@@ -43,13 +44,6 @@ if ($ExtUtils::F77::VERSION > 1.03) {
4344
$F77flags = '';
4445
}
4546

46-
sub tapprox {
47-
my ($x,$y) = @_;
48-
my $c = abs($x->flat-$y->flat);
49-
my $d = max($c);
50-
$d < 0.01;
51-
}
52-
5347
sub byte4swap {
5448
my ($file) = @_;
5549
my ($ofile) = $file.'~';
@@ -268,9 +262,9 @@ EOT
268262

269263
my @a = readflex($data);
270264
# print "@a\n";
271-
my $ok = ($a[0]->at(0) == $ndata);
265+
ok my $ok = ($a[0]->at(0) == $ndata);
272266
my $res = eval "$pdltype $exprp";
273-
ok( $ok && tapprox($res,$a[1]), "readflex $pdltype w hdr file" );
267+
is_pdl $res, $a[1], "readflex $pdltype w hdr file";
274268

275269
open(FILE,">$hdr");
276270
print FILE <<"EOT";
@@ -289,9 +283,9 @@ EOT
289283

290284
unlink $hdr;
291285

292-
$ok = ($a[0]->at(0) == $ndata);
286+
ok $ok = ($a[0]->at(0) == $ndata);
293287
$res = eval "$pdltype $exprp";
294-
ok( $ok && tapprox($res,$a[1]), "readflex $pdltype w hdr file (explicit swap)" );
288+
is_pdl $res, $a[1], "readflex $pdltype w hdr file (explicit swap)";
295289

296290
# Now try header array
297291
$ok = 1;
@@ -300,10 +294,9 @@ EOT
300294
{Type => $pdltype, NDims => 1, Dims => [ $ndata ] } ];
301295
@a = readflex($data,$header);
302296
unlink $data;
303-
$ok = ($a[0]->at(0) == $ndata);
297+
ok $ok = ($a[0]->at(0) == $ndata);
304298
$res = eval "$pdltype $exprp";
305-
ok( $ok && tapprox($res,$a[1]), "readflex $pdltype w hdr array" );
306-
# print $a[1]->getndims()," [",$a[1]->dims,"]\n";
299+
is_pdl $res, $a[1], "readflex $pdltype w hdr array";
307300

308301
} # foreach: $pdltype == 'float', 'double'
309302

@@ -350,9 +343,9 @@ EOT
350343
# print "@a\n";
351344
unlink $data, $hdr;
352345

353-
my $ok = ($a[0]->at(0) == $ndata);
346+
ok my $ok = ($a[0]->at(0) == $ndata);
354347
my $res = eval "$pdltype $exprp";
355-
ok($ok && tapprox($res,$a[1]), "f77 1D $pdltype data");
348+
is_pdl $res, $a[1], "f77 1D $pdltype data";
356349
# print $a[1]->getndims()," [",$a[1]->dims,"]\n";
357350

358351
} # foreach: $pdltype ( keys %types )
@@ -405,9 +398,9 @@ EOT
405398
# print "@a\n";
406399
unlink $data, $hdr;
407400

408-
my $ok = ($a[1]->at(0) == $ndata);
401+
ok my $ok = ($a[1]->at(0) == $ndata);
409402
my $res = eval "$pdltype $exprp";
410-
ok( $ok && tapprox($res,$a[2]), "no f77, 1D $pdltype data");
403+
is_pdl $res,$a[2], "no f77, 1D $pdltype data";
411404
# print $a[2]->getndims()," [",$a[2]->dims,"]\n";
412405
}
413406

@@ -458,9 +451,9 @@ EOT
458451
# }
459452
unlink $data, $hdr;
460453

461-
my $ok = ($a[0]->at(0) == $ndata);
454+
ok my $ok = ($a[0]->at(0) == $ndata);
462455
my $res = eval "$pdltype $expr2p";
463-
ok( $ok && tapprox($res,$a[1]), "f77 format 2D $pdltype data");
456+
is_pdl $res, $a[1], "f77 format 2D $pdltype data";
464457
# print $a[1]->getndims()," [",$a[1]->dims,"]\n";
465458
}
466459

@@ -516,12 +509,10 @@ my $l = long (10**$f);
516509
$i = short ($l);
517510
my $x = byte (32);
518511
my @req = ($x,$i,$l,$f,$d);
519-
my $ok = 1;
520512
foreach (@req) {
521513
my $h = shift @a;
522-
$ok &&= tapprox($_,$h);
514+
is_pdl $h, $_, "readflex combined types";
523515
}
524-
ok( $ok, "readflex combined types" );
525516

526517
SKIP: {
527518
my $compress = File::Which::which('compress') ? 'compress' : 'gzip'; # some linuxes don't have compress
@@ -532,20 +523,19 @@ SKIP: {
532523
}
533524

534525
# Try compressed data
535-
$ok = 1;
536526
0 == system "$compress -c $data > ${data}.Z" or diag "system $compress -c $data >${data}.Z failed: $?";
537527
unlink( $data );
538528
@a = readflex($data);
539-
$ok &&= $#a==6;
529+
ok $#a==6;
540530
@a = readflex("${data}.Z");
541-
$ok &&= $#a==6;
531+
ok $#a==6;
542532
my $NULL = File::Spec->devnull();
543533
0 == system "gunzip -q ${data}.Z >$NULL 2>&1" or diag "system gunzip -q ${data}.Z failed: $?";
544534
0 == system "gzip -q $data >$NULL 2>&1" or diag "system gzip -q $data failed: $?";
545535
@a = readflex($data);
546-
$ok &&= $#a==6;
536+
ok $#a==6;
547537
@a = readflex("${data}.gz");
548-
$ok &&= $#a==6;
538+
ok $#a==6;
549539
shift @a;
550540
unlink "${data}.gz", $hdr;
551541
$d = double pdl (4*atan2(1,1));
@@ -556,29 +546,22 @@ SKIP: {
556546
@req = ($x,$i,$l,$f,$d);
557547
foreach (@req) {
558548
my $h = shift @a;
559-
$ok &&= tapprox($_,$h);
549+
is_pdl $h, $_, "readflex compressed data";
560550
}
561-
ok( $ok, "readflex compressed data" );
562551
}
563552

564553
# Try writing data
565554
my $flexhdr = writeflex($data,@req);
566555
writeflexhdr($data,$flexhdr) unless $PDL::IO::FlexRaw::writeflexhdr;
567556
@a = readflex($data);
568557
unlink $hdr;
569-
$ok = 1;
570558
foreach (@req) {
571-
# print "$_ vs ",@a[0],"\n";
572-
$ok &&= tapprox($_,shift @a);
559+
is_pdl shift @a, $_, "writeflex combined data types, hdr file";
573560
}
574-
ok( $ok, "writeflex combined data types, hdr file" );
575561
@a = readflex($data, $flexhdr);
576-
$ok = 1;
577562
foreach (@req) {
578-
# print "$_ vs ",@a[0],"\n";
579-
$ok &&= tapprox($_,shift @a);
563+
is_pdl shift @a, $_, "writeflex combined data types, readflex hdr array";
580564
}
581-
ok( $ok, "writeflex combined data types, readflex hdr array" );
582565
unlink $data;
583566

584567
$#a = -1;
@@ -593,12 +576,9 @@ $flexhdr = [ {Type => 'byte', NDims => 1, Dims => 10},
593576
{Type => 'double', NDims => 1, Dims => 10} ];
594577
@a = readflex($data, $flexhdr);
595578
unlink $data;
596-
$ok = 1;
597579
foreach (@req) {
598-
# print "$_ vs ",@a[0],"\n";
599-
$ok &&= tapprox($_,slice(shift @a,"(0)"));
580+
is_pdl slice(shift @a,"(0)"), $_, "writeflex combined types[10], readflex explicit hdr array";
600581
}
601-
ok( $ok, "writeflex combined types[10], readflex explicit hdr array");
602582

603583
# Writing multidimensional data
604584
map {$_ = $_->dummy(0,10)} @req;
@@ -607,12 +587,9 @@ writeflexhdr($data,$flexhdr) unless $PDL::IO::FlexRaw::writeflexhdr;
607587
@a = readflex($data);
608588
unlink $data;
609589
unlink $hdr;
610-
$ok = 1;
611590
foreach (@req) {
612-
# print "$_ vs ",@a[0],"\n";
613-
$ok &&= tapprox($_,shift @a);
591+
is_pdl shift @a, $_, "multidimensional data";
614592
}
615-
ok( $ok, "multidimensional data" );
616593

617594
# Use readflex with an open file handle
618595
@req = (byte(1..3),
@@ -623,26 +600,20 @@ $flexhdr = writeflex($data, @req);
623600

624601
open(IN, $data);
625602
@a = readflex(\*IN, $flexhdr);
626-
$ok = 1;
627603
foreach (@req) {
628-
# print "$_ vs ",@a[0],"\n";
629-
$ok &&= tapprox($_,shift @a);
604+
is_pdl shift @a, $_, "readflex with file handle";
630605
}
631606
close(IN);
632607
unlink $data;
633-
ok( $ok, "readflex with file handle" );
634608

635609
# use writeflex with an open file handle
636610
open(OUT, ">$data");
637611
$flexhdr = writeflex(\*OUT, @req);
638612
close(OUT);
639613
@a = readflex($data, $flexhdr);
640-
$ok = 1;
641614
foreach (@req) {
642-
# print "$_ vs ",@a[0],"\n";
643-
$ok &&= tapprox($_,shift @a);
615+
is_pdl shift @a, $_, "writeflex with file handle";
644616
}
645617
unlink $data;
646-
ok( $ok, "writeflex with file handle" );
647618

648619
done_testing;

0 commit comments

Comments
 (0)