| 1 | use strict; use warnings; |
| 2 | |
| 3 | use Test::More; |
| 4 | use Test::PDL; |
| 5 | |
| 6 | use PDL; |
| 7 | use PDL::IO::Matlab qw ( matlab_read matlab_write ); |
| 8 | |
| 9 | # allows any 1.{5,6}.* |
| 10 | my @got_v = PDL::IO::Matlab::get_library_version; |
| 11 | ok($got_v[0] == 1 && $got_v[1] >= 5, 'library version') or diag explain \@got_v; |
| 12 | |
| 13 | # Write one pdl |
| 14 | my $f = 'testf.mat'; |
| 15 | END { unlink $f } |
| 16 | my $mat = PDL::IO::Matlab->new($f, '>', {format => 'MAT5'}); |
| 17 | ok( $mat != 0 , 'file opened for write'); |
| 18 | $mat->write(sequence(10)); |
| 19 | $mat->close(); |
| 20 | |
| 21 | # Read the pdl |
| 22 | $mat = PDL::IO::Matlab->new($f, '<'); |
| 23 | ok($mat != 0 , 'file opened for read'); |
| 24 | ok($mat->get_version eq 'MAT5', 'file format MAT5'); |
| 25 | ok($mat->get_header, 'read header'); |
| 26 | |
| 27 | my $x = $mat->read_next; |
| 28 | $mat->close(); |
| 29 | |
| 30 | is_pdl $x,sequence(10), 'read data same as write data'; |
| 31 | |
| 32 | |
| 33 | $mat = PDL::IO::Matlab->new($f, '>', {format => 'MAT5'}); |
| 34 | |
| 35 | my @types = ( double, float, long, byte, short, ushort ); |
| 36 | map { $mat->write(sequence($_,10)) } @types; |
| 37 | |
| 38 | $mat->close; |
| 39 | |
| 40 | |
| 41 | $mat = PDL::IO::Matlab->new($f, '<'); |
| 42 | while(1) { |
| 43 | my ($err,$x) = $mat->read_next; |
| 44 | last if $err; |
| 45 | # last unless ref($x); # this works as well |
| 46 | my $type = shift @types; |
| 47 | ok($x->type == $type, "trying type $type "); |
| 48 | } |
| 49 | $mat->close; |
| 50 | |
| 51 | $mat = PDL::IO::Matlab->new($f, '<'); |
| 52 | my @pdls = $mat->read_all; |
| 53 | ok( scalar(@pdls) == 6 , 'read_all'); |
| 54 | |
| 55 | $mat->rewind; |
| 56 | @pdls = $mat->read_all; |
| 57 | ok( scalar(@pdls) == 6 , 'rewind'); |
| 58 | $mat->close; |
| 59 | |
| 60 | matlab_write('tst.mat',zeroes(10),ones(5)); |
| 61 | END { unlink 'tst.mat' } |
| 62 | ($x,my $y) = matlab_read('tst.mat'); |
| 63 | |
| 64 | is_pdl $x,zeroes(10), 'matlab_read matlab_write 1'; |
| 65 | is_pdl $y,ones(5), 'matlab_read matlab_write 2'; |
| 66 | |
| 67 | if (PDL::IO::Matlab::_have_hdf5()) { |
| 68 | matlab_write('tst.mat', 'MAT73', zeroes(10)); |
| 69 | ($x) = matlab_read('tst.mat'); |
| 70 | is_pdl $x,zeroes(10), 'matlab_read matlab_write, MAT73'; |
| 71 | |
| 72 | matlab_write('tst.mat', sequence(5)); |
| 73 | $x = matlab_read('tst.mat', {onedr => 0} ); |
| 74 | is_pdl $x->shape, pdl [5, 1], 'onedr => 0'; |
| 75 | |
| 76 | matlab_write('tst.mat', sequence(5), {onedw => 2} ); |
| 77 | $x = matlab_read('tst.mat', {onedr => 0} ); |
| 78 | is_pdl $x->shape, pdl [1, 5], 'onedr => 0 , onedw => 2'; |
| 79 | } else { |
| 80 | diag "no HDF5, skipping"; |
| 81 | } |
| 82 | |
| 83 | done_testing(); |