1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
|
use PDL;
use PDL::IO::HDF5;
use PDL::IO::HDF5::Group;
use PDL::IO::HDF5::Dataset;
# Script to test the group/dataset object separately.
# i.e. not the way they would normally be used as described
# in the PDL::IO::HDF5 synopsis
use Test::More tests => 17;
# New File Check:
my $filename = "newFile.hdf5";
# get rid of filename if it already exists
unlink $filename if( -e $filename);
my $hdfobj;
ok($hdfobj = new PDL::IO::HDF5($filename));
my $group = new PDL::IO::HDF5::Group( name => '/dude', parent => $hdfobj,
fileObj => $hdfobj);
# Set attribute for group
ok($group->attrSet( 'attr1' => 'dudeman', 'attr2' => 'What??'));
# Try Setting attr for an existing attr
ok($group->attrSet( 'attr1' => 'dudeman23'));
# Add a attribute and then delete it
ok( $group->attrSet( 'dummyAttr' => 'dummyman',
'dummyAttr2' => 'dummyman'));
ok( $group->attrDel( 'dummyAttr', 'dummyAttr2' ));
# Get list of attributes
my @attrs = $group->attrs;
ok( join(",",sort @attrs) eq 'attr1,attr2' );
# Get a list of attribute values
my @attrValues = $group->attrGet(sort @attrs);
ok( join(",",@attrValues) eq 'dudeman23,What??' );
# print "Attr Values = '".join("', '",@attrValues)."'\n";
# Get a list of datasets (should be none)
my @datasets = $group->datasets;
ok( scalar(@datasets) == 0 );
# Create another group
my $group2 = new PDL::IO::HDF5::Group( 'name'=> '/dude2', parent => $hdfobj,
fileObj => $hdfobj);
# open the root group
my $rootGroup = new PDL::IO::HDF5::Group( 'name'=> '/', parent => $hdfobj,
fileObj => $hdfobj);
# Get a list of groups
my @groups = $rootGroup->groups;
# print "Root group has these groups '".join(",",sort @groups)."'\n";
ok( join(",",sort @groups) eq 'dude,dude2' );
# Get a list of groups in group2 (should be none)
@groups = $group2->groups;
ok( scalar(@groups) == 0 );
# Create a dataset in the root group
my $dataset = new PDL::IO::HDF5::Dataset( 'name'=> 'data1', parent => $rootGroup,
fileObj => $hdfobj);
my $pdl = sequence(5,4);
ok( $dataset->set($pdl, unlimited => 1) );
# print "pdl written = \n".$pdl."\n";
my $pdl2 = $dataset->get;
# print "pdl read = \n".$pdl2."\n";
ok( (($pdl - $pdl2)->sum) < .001 );
# Set attribute for dataset
ok( $dataset->attrSet( 'attr1' => 'dataset dudeman', 'attr2' => 'Huh What??'));
# Try Setting attr for an existing attr
ok($dataset->attrSet( 'attr1' => 'dataset dudeman23'));
# Add a attribute and then delete it
ok( $dataset->attrSet( 'dummyAttr' => 'dummyman',
'dummyAttr2' => 'dummyman'));
ok( $dataset->attrDel( 'dummyAttr', 'dummyAttr2' ));
# Get list of attributes
@attrs = $dataset->attrs;
ok( join(",",sort @attrs) eq 'attr1,attr2' );
# clean up file
unlink $filename if( -e $filename);
#print "completed\n";
|