~ubuntu-branches/ubuntu/lucid/pdl/lucid

« back to all changes in this revision

Viewing changes to t/niceslice.t

  • Committer: Bazaar Package Importer
  • Author(s): Ben Gertzfield
  • Date: 2002-04-08 18:47:16 UTC
  • Revision ID: james.westby@ubuntu.com-20020408184716-0hf64dc96kin3htp
Tags: upstream-2.3.2
ImportĀ upstreamĀ versionĀ 2.3.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
use strict;
 
2
use Test;
 
3
 
 
4
use PDL::LiteF;
 
5
 
 
6
BEGIN { 
 
7
    eval 'require PDL::NiceSlice';
 
8
    unless ($@) {
 
9
        plan tests => 23;
 
10
    } else {
 
11
        plan tests => 1;
 
12
        print "ok 1 # Skipped: no sourcefilter support\n";
 
13
        exit;
 
14
    }
 
15
 
16
 
 
17
$| = 1;
 
18
sub PDL::NiceSlice::findslice;
 
19
sub translate_and_show {
 
20
  my ($txt) = @_;
 
21
  my $etxt = PDL::NiceSlice::findslice $txt;
 
22
  print "$txt -> \n\t$etxt\n";
 
23
  return $etxt;
 
24
}
 
25
 
 
26
 
 
27
ok (!$@);
 
28
 
 
29
my $a = sequence 10; # shut up -w
 
30
my $b = pdl(1);
 
31
eval translate_and_show '$b = $a((5));';
 
32
 
 
33
ok (!$@);
 
34
ok($b->at == 5);
 
35
 
 
36
eval translate_and_show '$b = $a->((5));';
 
37
ok (!$@);
 
38
ok($b->at == 5);
 
39
 
 
40
my $c = PDL->pdl(7,6);
 
41
eval translate_and_show '$b = $a(($c(1)->at(0)));';
 
42
ok (!$@);
 
43
ok($b->getndims == 0 && all $b == 6);
 
44
 
 
45
# the latest versions should do the 'at' automatically
 
46
eval translate_and_show '$b = $a(($c(1)));';
 
47
ok (!$@);
 
48
ok($b->getndims == 0 && all $b == 6);
 
49
 
 
50
eval translate_and_show '$c = $a(:);';
 
51
ok (!$@);
 
52
print $@ if $@;
 
53
ok ($c->getdim(0) == 10 && all $c == $a);
 
54
 
 
55
my $idx = pdl 1,4,5;
 
56
 
 
57
eval translate_and_show '$b = $a($idx);';
 
58
ok (!$@);
 
59
ok(all $b == $idx);
 
60
 
 
61
# use 1-el piddles as indices
 
62
my $rg = pdl(2,7,2);
 
63
my $cmp = pdl(2,4,6);
 
64
eval translate_and_show '$b = $a($rg(0):$rg(1):$rg(2));';
 
65
ok (!$@);
 
66
ok(all $b == $cmp);
 
67
 
 
68
# mix ranges and index piddles
 
69
my $twod = sequence 5,5;
 
70
$idx = pdl 2,3,0;
 
71
$cmp = $twod->slice('-1:0')->dice_axis(1,$idx);
 
72
eval translate_and_show '$b = $twod(-1:0,$idx);';
 
73
ok (!$@);
 
74
ok(all $b == $cmp);
 
75
 
 
76
# modifiers
 
77
 
 
78
$a = sequence 10;
 
79
eval translate_and_show '$b = $a($a<3;?)';
 
80
ok (!$@);
 
81
ok(all $b == pdl(0,1,2));
 
82
 
 
83
$a = sequence 3,3;
 
84
eval translate_and_show '$b = $a(0:-2;_);';
 
85
ok (!$@);
 
86
ok(all $b == sequence 8);
 
87
 
 
88
# foreach/for blocking
 
89
 
 
90
$a = '';
 
91
eval translate_and_show "foreach \n" . ' $b(1,2,3,4) {$a .= $b;}';
 
92
ok(!$@ and $a eq '1234');
 
93
 
 
94
$a = '';
 
95
eval translate_and_show 'for    $b(1,2,3,4) {$a .= $b;}';
 
96
ok(!$@ and $a eq '1234');