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
|
use PDL::LiteF;
########### Test of method over-riding in subclassed objects ###########
### Global Variable used to tell if method over-riding worked ###
$main::OVERRIDEWORKED = 0;
## First define a PDL-derived object:
package PDL::Derived;
@PDL::Derived::ISA = qw/PDL/;
sub new {
my $class = shift;
my $data = $_[0];
my $self;
if(ref($data) eq 'PDL' ){ # if $data is an object (a pdl)
$self = $class->initialize;
$self->{PDL} = $data;
}
else{ # if $data not an object call inherited constructor
$self = $class->SUPER::new($data);
}
return $self;
}
####### Initialize function. This over-ridden function is called by the PDL constructors
sub initialize {
my $class = shift;
my $self = {
PDL => PDL->null, # used to store PDL object
someThingElse => 42,
};
$class = (ref $class ? ref $class : $class );
bless $self, $class;
}
###### Derived Object Needs to supply its own copy #####
sub copy {
my $self = shift;
# setup the object
my $new = $self->initialize;
# copy the PDL
$new->{PDL} = $self->{PDL}->SUPER::copy;
# copy the other stuff:
$new->{someThingElse} = $self->{someThingElse};
return $new;
}
### Check of over-riding sumover
### This sumover should be called from PDL->sum.
### If the result is different from the normal sumover by $self->{SomethingElse} (42) then
### we will know that it has been called.
sub sumover{
my $self = shift;
my ($arg) = @_;
if( ! defined $arg){ # no-argument form of calling
$arg = $self->SUPER::sumover;
return $self->{someThingElse} + $arg;
}
else{ # one-argument form of calling
$self->SUPER::sumover($arg);
$arg += $self->{someThingElse};
}
}
#### test of overriding minmaximum. Calls inherited minmaximum and
#### Sets the Global variable main::OVERRIDEWORKED if called ####
sub minmaximum{
my $self = shift;
my ($arg) = @_;
$main::OVERRIDEWORKED = 1; # set the global variable so we know over-ride worked.
# print "In over-ridden minmaximum\n";
$self->SUPER::minmaximum(@_);
}
#### test of overriding inner. Calls inherited inner and
#### Sets the Global variable main::OVERRIDEWORKED if called ####
sub inner{
my $self = shift;
my ($arg) = @_;
$main::OVERRIDEWORKED = 1; # set the global variable so we know over-ride worked.
# print "In over-ridden inner\n";
$self->SUPER::inner(@_);
}
#### test of overriding which. Calls inherited which and
#### Sets the Global variable main::OVERRIDEWORKED if called ####
sub which{
my $self = shift;
my ($arg) = @_;
$main::OVERRIDEWORKED++; # set the global variable so we know over-ride worked.
# print "In over-ridden which\n";
$self->SUPER::which(@_);
}
#### test of overriding one2nd. Calls inherited one2nd and
#### increments the Global variable main::OVERRIDEWORKED if called ####
sub one2nd{
my $self = shift;
my ($arg) = @_;
$main::OVERRIDEWORKED++; # set the global variable so we know over-ride worked.
# print "In over-ridden one2nd\n";
$self->SUPER::one2nd(@_);
}
#######################################################
package main;
###### Testing Begins #########
print "1..7\n";
my $testNo = 1;
$im = new PDL::Derived [
[ 1, 2, 3, 3 , 5],
[ 2, 3, 4, 5, 6],
[13, 13, 13, 13, 13],
[ 1, 3, 1, 3, 1],
[10, 10, 2, 2, 2,]
];
# Check for PDL::sumover being called by sum
ok($testNo++, $im->sum == 176 ); # result will be = 134 if derived sumover not called, 176 if it is called.
### Test over-ride of minmaximum:
$main::OVERRIDEWORKED = 0;
my @minMax = $im->minmax;
ok($testNo++, $main::OVERRIDEWORKED == 1 );
### Test over-ride of inner:
## Update to use inner, not matrix mult - CED 8-May-2010
$main::OVERRIDEWORKED = 0;
my $matMultRes = $im->inner($im);
ok($testNo++, $main::OVERRIDEWORKED == 1 );
### Test over-ride of which, one2nd
$main::OVERRIDEWORKED = 0;
# which ND test
my $a= PDL::Derived->sequence(10,10,3,4);
my ($x, $y, $z, $w)=whichND($a == 203);
ok($testNo++, $main::OVERRIDEWORKED == 2 );
# Check to see if the clip functions return a derived object:
ok( $testNo++, ref( $im->clip(5,7) ) eq "PDL::Derived");
ok( $testNo++, ref( $im->hclip(5) ) eq "PDL::Derived");
ok( $testNo++, ref( $im->lclip(5) ) eq "PDL::Derived");
sub ok {
my $no = shift ;
my $result = shift ;
print "not " unless $result ;
print "ok $no\n" ;
}
|