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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
|
# Script to test the attribute index functionality of the PDL::IO::HDF5 Class
use strict;
use warnings;
use PDL;
use PDL::Char;
use PDL::IO::HDF5;
use Data::Dumper;
use Test::More;
# New File Check:
my $filename = "total.hdf5";
ok(my $hdfobj = new PDL::IO::HDF5($filename));
# It is normally a no-no to call a internal method, but we
# are just testing here:
$hdfobj->_buildAttrIndex;
my $result = recursiveDump($hdfobj->{attrIndex});
my $baseline =
q!{
/ => {
attr1 => dudeman23,
attr2 => What??,
}
/dude2 => {
attr1 => dudeman23,
attr2 => What??,
}
/mygroup => {
attr1 => dudeman23,
attr2 => What??,
}
/mygroup/subgroup => {
attr1 => dudeman23,
attr2 => What??,
}
}
!;
is($result, $baseline);
# die;
my @values = $hdfobj->allAttrValues('attr1');
$baseline =
q![
dudeman23,
dudeman23,
dudeman23,
dudeman23,
]
!;
$result = recursiveDump(\@values);
is($result, $baseline);
@values = $hdfobj->allAttrValues('attr1','attr2');
$baseline =
q![
[
dudeman23,
What??,
]
[
dudeman23,
What??,
]
[
dudeman23,
What??,
]
[
dudeman23,
What??,
]
]
!;
$result = recursiveDump(\@values);
is($result, $baseline);
my @names = $hdfobj->allAttrNames;
$baseline =
q![
attr1,
attr2,
]
!;
$result = recursiveDump(\@names);
is($result, $baseline);
# Test building the groupIndex
$hdfobj->_buildGroupIndex('attr1','attr2');
$hdfobj->_buildGroupIndex('attr2');
$hdfobj->_buildGroupIndex('attr1','attr3');
$baseline =
"{
attr1$;attr2 => {
dudeman23$;What?? => [
/,
/dude2,
/mygroup,
/mygroup/subgroup,
]
}
attr1$;attr3 => {
dudeman23$;_undef_ => [
/,
/dude2,
/mygroup,
/mygroup/subgroup,
]
}
attr2 => {
What?? => [
/,
/dude2,
/mygroup,
/mygroup/subgroup,
]
}
}
";
$result = recursiveDump($hdfobj->{groupIndex});
is($result, $baseline);
my @groups = $hdfobj->getGroupsByAttr( 'attr1' => 'dudeman23',
'attr2' => 'What??');
$baseline =
q![
/,
/dude2,
/mygroup,
/mygroup/subgroup,
]
!;
$result = recursiveDump(\@groups);
is($result, $baseline);
# clean up file
unlink $filename if( -e $filename);
done_testing;
# Dump of recursive array/hash.
# We Could use Data:Dumper for this but it doesn't
# order the keys, which causes problems
# in regression testing on different platforms
sub recursiveDump{
my ($ref, $level) = @_;
$level = 1 unless( defined($level));
my $returnString; # String to return
my $levelspace = ' '; # Space used to indent
my $indent = $levelspace x $level;
my $unindent = $levelspace x ($level-1) ;
my $displayedData = $ref;
my $arrayFlag = 0;
my @sortedKeys;
if( ref $ref eq 'ARRAY'){ # arrays are converted to hashes with indexes numbers
# as keys for display
$displayedData = {};
@$displayedData{0..$#$ref} = @$ref;
$returnString = $unindent."[\n";
$arrayFlag = 1;
@sortedKeys = (0..$#$ref);
}
else{
$returnString = $unindent."{\n";
@sortedKeys = sort keys %$displayedData;
}
my $value;
foreach my $key(@sortedKeys){
$value = $displayedData->{$key};
if( ref( $value) ){
$returnString .= $indent.$key." => " unless( $arrayFlag); # dumping hash Ref
$returnString .= recursiveDump($value,$level+1);
}
else{
$returnString .= $indent.$value.",\n" if( $arrayFlag); # dumping array Ref
$returnString .= $indent.$key.' => '.$value.",\n" unless( $arrayFlag); # dumping hash Ref
}
}
$returnString .= $unindent."}\n" unless($arrayFlag); # Dumping hash ref
$returnString .= $unindent."]\n" if($arrayFlag); # Dumping aray ref
$returnString;
}
|