# Test routine for PDL::IO::Misc module use strict; use PDL::LiteF; use PDL::IO::Misc; use PDL::Core ':Internal'; # For howbig() use PDL::Config; kill 'INT',$$ if $ENV{UNDER_DEBUGGER}; # Useful for debugging. use Test; BEGIN { plan tests => 16; } sub tapprox { my($a,$b) = @_; my $c = abs($a-$b); my $d = max($c); $d < 0.0001; } require File::Spec; my $fs = 'File::Spec'; sub cdir { return $fs->catdir(@_)} sub cfile { return $fs->catfile(@_)} my $tempd = $PDL::Config{TEMPDIR} or die "TEMPDIR not found in %PDL::Config"; my $file = cfile $tempd, "iotest$$"; ############# Test rcols with filename and pattern ############# open(OUT, ">$file") || die "Can not open $file for writing\n"; print OUT <getdim(0)==5), 1, "rcols with filename" ); ($a,$b) = rcols $file, "/FOO/",0,1; $a = long($a); $b=long($b); ok( (sum($a)==6 && max($b)==33 && $b->getdim(0)==2), 1, "rcols with filename + pattern" ); ############### Test rgrep with FILEHANDLE ##################### open(OUT, ">$file") || die "Can not open $file for writing\n"; print OUT <getdim(0)==5), 1, "rgrep" ); ########### Explicit test of byte swapping ################# $a = short(3); $b = long(3); # $c=long([3,3]); bswap2($a); bswap4($b); ok(sum($a)==768 && sum($b)==50331648,1,"bswap2"); ############# Test rasc ############# open(OUT, ">$file") || die "Can not open $file for writing\n"; print OUT <null; $a->rasc($file,20); ok( abs($a->sum - 5.13147) < .01, 1, "rasc on null piddle" ); $b = zeroes(float,20,2); $b->rasc($file); ok( abs($b->sum - 5.13147) < .01, 1, "rasc on existing piddle" ); eval '$b->rasc("file_that_does_not_exist")'; ok( $@, qr/Can't open/, "rasc on non-existant file" ); unlink $file; # clean up ####################################################### # Tests of rcols() options # EXCLUDE/INCLUDE/LINES/DEFTYPE/TYPES open(OUT, ">$file") || die "Can not open $file for writing\n"; print OUT <nelem==4 && sum($a)==6 && sum($b)==20, 1, "rcols: default" ); ($a,$b) = rcols $file,0,1, { INCLUDE => '/^-/' }; ok( $a->nelem==1 && $a->at(0)==-5 && $b->at(0)==6, 1, "rcols: include pattern" ); ($a,$b) = rcols $file,0,1, { LINES => '-2:0' }; ok( $a->nelem==3 && tapprox($a,pdl(-5,3,1)) && tapprox($b,pdl(6,4,2)), 1, "rcols: lines option" ); use PDL::Types; ($a,$b) = rcols $file, { DEFTYPE => long }; ok( $a->nelem==4 && $a->get_datatype==$PDL_L && $b->get_datatype==$PDL_L, 1, "rcols: deftype option" ); ($a,$b) = rcols $file, { TYPES => [ ushort ] }; ok( $a->nelem==4 && $a->get_datatype==$PDL_US && $b->get_datatype==$PDL_D, 1, "rcols: types option" ); ok( UNIVERSAL::isa($PDL::IO::Misc::deftype,"PDL::Type"), 1, "PDL::IO::Misc::deftype is a PDL::Type object" ); ok( $PDL::IO::Misc::deftype->[0], double->[0], "PDL::IO::Misc::deftype check" ); $PDL::IO::Misc::deftype = short; ($a,$b) = rcols $file; ok( $a->get_datatype, short->[0], "rcols: can read in as 'short'" ); unlink $file; eval { wcols $a, $b }; ok(!$@,1, "wcols" ); 1;