Skip to content

Commit a17e954

Browse files
committed
Slices tests replace tapprox with is_pdl - #34
1 parent 55ced64 commit a17e954

File tree

1 file changed

+62
-108
lines changed

1 file changed

+62
-108
lines changed

Basic/t/slice.t

+62-108
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use strict;
22
use warnings;
33
use Test::More;
44
use PDL::LiteF;
5-
use PDL::Dbg;
5+
use Test::PDL;
66

77
# PDL::Core::set_debugging(1);
88

@@ -13,23 +13,14 @@ use PDL::Dbg;
1313
# kill INT,$$ if $ENV{UNDER_DEBUGGER};
1414
#}
1515

16-
sub tapprox ($$) {
17-
my $x = shift;
18-
my $y = shift;
19-
return 1 if $x->isempty and $y->isempty;
20-
my $maxdiff = abs($x-$y)->max;
21-
return $maxdiff < 0.01;
22-
}
23-
2416
my $x = (1+(xvals zeroes 4,5) + 10*(yvals zeroes 4,5));
2517

2618
is($x->at(2,2), 23, "x location (2,2) is 23");
2719

2820
my $y = $x->slice('1:3:2,2:4:2');
29-
ok(tapprox($y,pdl([[22,24],[42,44]])));
30-
21+
is_pdl $y,pdl([[22,24],[42,44]]);
3122
$y .= 0.5;
32-
ok(tapprox($y,pdl([[0.5,0.5],[0.5,0.5]])));
23+
is_pdl $y,pdl([[0.5,0.5],[0.5,0.5]]);
3324
is($x->at(1,2), 0.5);
3425
is($x->at(2,2), 23); # Check that nothing happened to other elems
3526

@@ -41,47 +32,37 @@ is("$line", '[1 1 1]', 'right value after collapsing slice (0)');
4132

4233
my $im = byte [[0,1,255],[0,0,0],[1,1,1]];
4334
(my $im1 = null) .= $im->dummy(0,3);
44-
my $im2 = $im1->clump(2)->slice(':,0:2');
45-
ok(!tapprox(ones(byte,9,3),$im2));
35+
is_pdl $im1->clump(2)->slice(':,0:2'), byte('0 0 0 1 1 1 255 255 255; 0 0 0 0 0 0 0 0 0; 1 1 1 1 1 1 1 1 1');
4636

4737
# here we encounter the problem
48-
$im2 = $im1->clump(2)->slice(':,-1:0');
49-
ok(!tapprox(ones(byte,9,3),$im2));
38+
is_pdl $im1->clump(2)->slice(':,-1:0'), byte('1 1 1 1 1 1 1 1 1; 0 0 0 0 0 0 0 0 0; 0 0 0 1 1 1 255 255 255');
5039

51-
$x = xvals( zeroes 10,10) + 0.1*yvals(zeroes 10,10);
52-
ok(tapprox($x->mslice('X',[6,7]),
53-
pdl([
54-
[0.6, 1.6, 2.6, 3.6, 4.6, 5.6, 6.6, 7.6, 8.6, 9.6],
55-
[0.7, 1.7, 2.7, 3.7, 4.7, 5.7, 6.7, 7.7, 8.7, 9.7]
56-
])));
40+
is_pdl +(xvals(10,10) + 0.1*yvals(10,10))->mslice('X',[6,7]), pdl([
41+
[0.6, 1.6, 2.6, 3.6, 4.6, 5.6, 6.6, 7.6, 8.6, 9.6],
42+
[0.7, 1.7, 2.7, 3.7, 4.7, 5.7, 6.7, 7.7, 8.7, 9.7]
43+
]);
5744

5845
my $lut = pdl [[1,0],[0,1]];
5946
$im = pdl indx, [1];
6047
my $in = $lut->transpose->index($im->dummy(0));
61-
is("$in", "\n[\n [0 1]\n]\n");
48+
is_pdl $in, pdl([[0,1]]);
6249
$in .= pdl 1;
63-
is("$in", "\n[\n [1 1]\n]\n");
64-
my $expected = pdl([[1,0],[1,1]]);
65-
ok(tapprox($lut, $expected)) or diag "lut=$lut exp=$expected";
50+
is_pdl $in, pdl([[1,1]]);
51+
is_pdl $lut, pdl([[1,0],[1,1]]);
6652

6753
# Test of dice and dice_axis
6854
$x = sequence(10,4);
6955
is($x->dice([1,2],[0,3])->sum, 66, "dice");
7056
is($x->dice([0,1],'X')->sum, 124, "dice 'X'");
7157

72-
my $got;
7358
# Test of dice clump compatibility
7459
my $xxx = PDL->new([[[0,0]],[[1,1]],[[2,2]]]);
75-
is_deeply($xxx->where($xxx == 0)->unpdl,[0,0],"dice clump base zero");
60+
is_pdl $xxx->where($xxx == 0), pdl([0,0]), "dice clump base zero";
7661
my $dice = $xxx->dice("X","X",[1,0]);
77-
is_deeply($got=$dice->clump(-1)->unpdl,[1,1,0,0],"dice clump correct") or diag "got=", explain $got;
78-
is_deeply($dice->where($dice == 0)->unpdl,[0,0],"dice clump where zero");
62+
is_pdl $dice->clump(-1), pdl([1,1,0,0]), "dice clump correct";
63+
is_pdl $dice->where($dice == 0), pdl([0,0]), "dice clump where zero";
7964

80-
$x = sequence(5,3,2);
81-
my @newDimOrder = (2,1,0);
82-
$y = $x->reorder(@newDimOrder);
83-
$got = [$y->dims];
84-
is_deeply($got, [2,3,5], "Test of reorder") or diag explain $got;
65+
is_pdl zeroes(5,3,2)->reorder(2,1,0)->shape, indx([2,3,5]), "reorder";
8566

8667
$x = zeroes(3,4);
8768
$y = $x->dummy(-1,2);
@@ -93,17 +74,17 @@ for my $in (
9374
$x->cat(map $x->rotate($_), 1..4)
9475
) {
9576
rle($in,my $y=null,my $z=null);
96-
ok(tapprox(rld($y,$z), $in),"rle with null input");
77+
is_pdl rld($y,$z), $in,"rle with null input";
9778
($y,$z) = rle($in);
98-
ok(tapprox(rld($y,$z), $in),"rle with return vals");
79+
is_pdl rld($y,$z), $in,"rle with return vals";
9980
}
10081

10182
$y = $x->mslice(0.5);
102-
ok(tapprox($y, 1), "mslice 1");
83+
is_pdl $y, pdl([1]), "mslice 1";
10384
$y = mslice($x, 0.5);
104-
ok(tapprox($y, 1), "func mslice 1");
85+
is_pdl $y, pdl([1]), "func mslice 1";
10586
$y = $x->mslice([0.5,2.11]);
106-
is("$y", "[1 1 1]", "mslice 2");
87+
is_pdl $y, pdl("[1 1 1]"), "mslice 2";
10788

10889
$x = zeroes(3,3);
10990
$y = $x->splitdim(3,3);
@@ -173,7 +154,7 @@ for (
173154
is_deeply([$y->dims], ref($exp) eq 'ARRAY' ? $exp : [$exp->dims], "$label dims right") or diag explain [$y->dims];
174155
next if ref($exp) eq 'ARRAY';
175156
is $y->nelem, $exp->nelem, "$label works right";
176-
ok tapprox($y, $exp), "$label works right";
157+
is_pdl $y, $exp, "$label works right";
177158
}
178159

179160
my $d = eval { $x2->slice("0:1,2:3,0")->xchg(0,2)->make_physical };
@@ -192,14 +173,14 @@ my $source = 10*xvals(10,10) + yvals(10,10);
192173
my $index = pdl([[2,3],[4,5]],[[6,7],[8,9]]);
193174
eval { $x = $source->indexND( $index ) };
194175
is $@, '';
195-
ok(tapprox($x, pdl([23,45],[67,89])));
176+
is_pdl $x, pdl([23,45],[67,89]);
196177

197178
# Broadcast indexND operation
198179
$source = 100*xvals(10,10,2)+10*yvals(10,10,2)+zvals(10,10,2);
199180
$index = pdl([[2,3],[4,5]],[[6,7],[8,9]]);
200181
eval { $x = $source->indexND($index) };
201182
is $@, '';
202-
ok(tapprox($x, pdl([[230,450],[670,890]],[[231,451],[671,891]])));
183+
is_pdl $x, pdl([[230,450],[670,890]],[[231,451],[671,891]]);
203184

204185
# Tests of range operator
205186
$source = 10*xvals(10,10) + yvals(10,10);
@@ -216,7 +197,7 @@ for (
216197
[$source, [$index,3,["e","p"]], [2,2,3,3], pdl([[89,99,99],[80,90,90],[81,91,91]]), "extension+periodic list syntax size 3", sub {shift->slice("(1),(1)")}],
217198
[$dex, [$mt], [0], pdl([]), "scalar Empty[0] indices"],
218199
[$dex, [zeroes(1,0)], [0], pdl([]), "Empty[1,0] indices"],
219-
[$mt, [$dex,undef,'e'], [], pdl(0), "empty source"],
200+
[$mt, [$dex,undef,'e'], [], indx(0), "empty source"],
220201
[$mt, [$mt], [0], pdl([]), "empty source and index"],
221202
[pdl(5,5,5,5), [$mt], [0], pdl([]), "non-empty source, empty index", sub {$_[0] .= 2}],
222203
) {
@@ -231,11 +212,11 @@ for (
231212
is $@, '', "$label works 2";
232213
$y = $exp_mod->($y) if $exp_mod;
233214
is $y->nelem, $exp->nelem, "$label nelem right";
234-
ok tapprox($y, $exp), "$label right data" or diag "got=$y\nexp=$exp";
235-
ok tapprox($src, $src_copy), "$label source not mutated";
215+
is_pdl $y, $exp, "$label right data";
216+
is_pdl $src, $src_copy, "$label source not mutated";
236217
next if !$mutate;
237218
$mutate->($y);
238-
ok tapprox($src, $mutate_exp), "$label src right data after mutation" or diag "got=$src";
219+
is_pdl $src, pdl($mutate_exp), "$label src right data after mutation";
239220
}
240221

241222
# range on higher-dimensional
@@ -352,26 +333,23 @@ $root .= 3;
352333
vafftest(\%addr2label, $all, [[0,1,0,0],[1,1,0,0],[1,1,0,0]], "root assigned to");
353334
$clumped2->make_physvaffine;
354335
vafftest(\%addr2label, $all, [[0,1,0,0],[0,1,0,0],[0,1,0,0]], "clumped2 physvaff 2");
355-
is "@{$clumped2->unpdl}", "3 3 3 3 3 3 3 3";
336+
is_pdl $clumped2, pdl("3 3 3 3 3 3 3 3");
356337

357338
# Make sure that vaffining is properly working:
358339
my $y = xvals(5,6,2) + 0.1 * yvals(5,6,2) + 0.01 * zvals(5,6,2);
359340
my $c = $y->copy->slice("2:3");
360-
ok tapprox $c, $c->copy;
341+
is_pdl $c, $c->copy;
361342
for ([0,1], [1,0], [1,1]) {
362343
my ($mv, $mult) = @$_;
363344
my $x_orig = pdl [1..4];
364345
my $x_mv = $mv ? $x_orig->mv(-1,0) : $x_orig;
365346
my $x_slice = $x_mv->slice("0:2");
366347
$x_slice->make_physvaffine;
367348
$x_slice *= 100 if $mult;
368-
my $y = PDL::_clump_int($x_slice,-1);
369-
$y->make_physvaffine;
370-
my $got = [$x_slice->firstvals_nophys];
371-
my $exp = [map $_*($mult ? 100 : 1), 1..3];
372-
is_deeply $got, $exp, "mv=$mv mult=$mult firstvals_nophys" or diag explain $got;
373-
$got = $y->unpdl;
374-
is_deeply $got, $exp, "mv=$mv mult=$mult clump" or diag explain $got;
349+
my $y = PDL::_clump_int($x_slice,-1)->make_physvaffine;
350+
my $exp = pdl(map $_*($mult ? 100 : 1), 1..3);
351+
is_pdl pdl($x_slice->firstvals_nophys), $exp, "mv=$mv mult=$mult firstvals_nophys";
352+
is_pdl $y, $exp, "mv=$mv mult=$mult clump";
375353
}
376354
# test the bug alluded to in the comments in pdl_changed (pdlapi.c)
377355
# used to segfault
@@ -382,12 +360,9 @@ my $sl2 = $xx->slice('(1)');
382360
my $sl22 = $sl2->slice('');
383361
my $roots = pdl '[1 -2396-2796i -778800+5024412i 2652376792-1643494392i -684394069604-217389559200i]'; # gives 4 roots of 599+699i
384362
PDL::polyroots($roots->re, $roots->im, $sl11, $sl22);
385-
my $got;
386-
ok all(approx $got=$xx->slice('(0)'), 599), "col=0"
387-
or diag "roots=$roots\n",
388-
"roots:", PDL::Core::pdump($roots),
389-
"got=$got\n", "return=", PDL::polyroots($roots->re, $roots->im);
390-
ok all(approx $got=$xx->slice('(1)'), 699), "col=1" or diag "got=$got";
363+
is_pdl $xx->slice('(0)'), pdl(599)->dummy(0,4), "col=0"
364+
or diag "roots=$roots\n", "roots:", PDL::Core::pdump($roots);
365+
is_pdl $xx->slice('(1)'), pdl(699)->dummy(0,4), "col=1";
391366

392367
eval {(my $y = zeroes(3,6)) += sequence(6,6)->mv(1,0)->slice("1:-1:2")};
393368
is $@, '', 'can += an mv->slice';
@@ -400,12 +375,9 @@ for ([0,0], [0,1], [1,0], [1,1]) {
400375
my $clump = $orig->clump(1,2);
401376
$clump->make_physvaffine if $phys_clump;
402377
($mutate_orig ? $orig : $clump) .= 3;
403-
my $got = $orig->unpdl;
404-
is_deeply $got, [[[(3)x3],[(3)x3]]], "phys_clump=$phys_clump mutate_orig=$mutate_orig orig" or diag explain $got;
405-
$got = $clump->unpdl;
406-
is_deeply $got, [[(3)x3],[(3)x3]], "phys_clump=$phys_clump mutate_orig=$mutate_orig clump" or diag explain $got;
407-
$got = $clump->uniqvec->unpdl;
408-
is_deeply $got, [[(3)x3]], "phys_clump=$phys_clump mutate_orig=$mutate_orig uniqvec" or diag explain $got;
378+
is_pdl $orig, pdl([[[(3)x3],[(3)x3]]]), "phys_clump=$phys_clump mutate_orig=$mutate_orig orig";
379+
is_pdl $clump, pdl([[(3)x3],[(3)x3]]), "phys_clump=$phys_clump mutate_orig=$mutate_orig clump";
380+
is_pdl $clump->uniqvec, pdl([[(3)x3]]), "phys_clump=$phys_clump mutate_orig=$mutate_orig uniqvec";
409381
}
410382

411383
my $pa = zeroes(7, 7); $pa->set(3, 4, 1);
@@ -425,88 +397,70 @@ is $@, '', 'no error assigning $x->index(..) to $x';
425397
## rlevec(), rldvec(): 2d ONLY
426398
my $p = pdl([[1,2],[1,2],[1,2],[3,4],[3,4],[5,6]]);
427399
my ($pf,$pv) = rlevec($p);
428-
my $pf_expect = pdl(long,[3,2,1,0,0,0]);
429-
my $pv_expect = pdl([[1,2],[3,4],[5,6],[0,0],[0,0],[0,0]]);
430-
ok all(approx($pf, $pf_expect)), "rlevec():counts";
431-
ok all(approx($pv, $pv_expect)), "rlevec():elts";
400+
is_pdl $pf, my $pf_expect = indx([3,2,1,0,0,0]), "rlevec():counts";
401+
is_pdl $pv, my $pv_expect = pdl([[1,2],[3,4],[5,6],[0,0],[0,0],[0,0]]), "rlevec():elts";
432402

433403
my $pd = rldvec($pf,$pv);
434-
ok all(approx($pd, $p)), "rldvec()";
435-
436-
my $pk = enumvec($p);
437-
ok all(approx($pk, pdl(long,[0,1,2,0,1,0]))), "enumvec()";
404+
is_pdl $pd, $p, "rldvec()";
405+
is_pdl enumvec($p), indx([0,1,2,0,1,0]), "enumvec()";
406+
is_pdl enumvecg($p), indx([0,0,0,1,1,2]), "enumvecg()";
438407

439-
$pk = enumvecg($p);
440-
ok all(approx($pk, pdl(long,[0,0,0,1,1,2]))), "enumvecg()";
441-
442-
## 6..7: test rleND(): 2d
443408
($pf,$pv) = rleND($p);
444-
ok all(approx($pf, $pf_expect)), "rleND():2d:counts";
445-
ok all(approx($pv, $pv_expect)), "rleND():2d:elts";
409+
is_pdl $pf, $pf_expect, "rleND():2d:counts";
410+
is_pdl $pv, $pv_expect, "rleND():2d:elts";
446411

447-
## 8..8: test rldND(): 2d
448412
$pd = rldND($pf,$pv);
449-
ok all(approx($pd, $p)), "rldND():2d";
413+
is_pdl $pd, $p, "rldND():2d";
450414

451415
## rleND, rldND: Nd
452416
my $pnd1 = (1 *(sequence(long, 2,3 )+1))->slice(",,*3");
453417
my $pnd2 = (10 *(sequence(long, 2,3 )+1))->slice(",,*2");
454418
my $pnd3 = (100*(sequence(long, 2,3,2)+1));
455419
my $p_nd = $pnd1->mv(-1,0)->append($pnd2->mv(-1,0))->append($pnd3->mv(-1,0))->mv(0,-1);
456420

457-
my $pf_expect_nd = pdl(long,[3,2,1,1,0,0,0]);
421+
my $pf_expect_nd = indx([3,2,1,1,0,0,0]);
458422
my $pv_expect_nd = zeroes($p_nd->type, $p_nd->dims);
459423
(my $tmp=$pv_expect_nd->slice(",,0:3")) .= $p_nd->dice_axis(-1,[0,3,5,6]);
460424

461425
## 9..10: test rleND(): Nd
462426
my ($pf_nd,$pv_nd) = rleND($p_nd);
463-
ok all(approx($pf_nd, $pf_expect_nd)), "rleND():Nd:counts";
464-
ok all(approx($pv_nd, $pv_expect_nd)), "rleND():Nd:elts";
427+
is_pdl $pf_nd, $pf_expect_nd, "rleND():Nd:counts";
428+
is_pdl $pv_nd, $pv_expect_nd, "rleND():Nd:elts";
465429

466430
## 11..11: test rldND(): Nd
467431
my $pd_nd = rldND($pf_nd,$pv_nd);
468-
ok all(approx($pd_nd, $p_nd)), "rldND():Nd";
432+
is_pdl $pd_nd, $p_nd, "rldND():Nd";
469433

470434
## 12..12: test enumvec(): nd
471435
my $v_nd = $p_nd->clump(2);
472436
my $k_nd = $v_nd->enumvec();
473-
ok all(approx($k_nd, pdl(long,[0,1,2,0,1,0,0]))), "enumvec():Nd";
437+
is_pdl $k_nd, indx([0,1,2,0,1,0,0]), "enumvec():Nd";
474438

475439
# from PDL::CCS tests revealing enumvec bug
476440
my $col = pdl("[5 5 4 4 4 3 3 3 3 2 2 2 1 1 0]")->transpose;
477-
$got = $col->enumvec;
478-
ok all(approx($got, pdl('[0 1 0 1 2 0 1 2 3 0 1 2 0 1 0]'))), 'enumvec'
479-
or diag "got=$got";
441+
is_pdl $col->enumvec, indx('[0 1 0 1 2 0 1 2 3 0 1 2 0 1 0]'), 'enumvec';
480442
$col = pdl("[0 0 1 1 2 2 2 3 3 3 3 4 4 4 5 5]")->transpose;
481-
$got = $col->enumvec;
482-
ok all(approx($got, pdl('[0 1 0 1 0 1 2 0 1 2 3 0 1 2 0 1]'))), 'enumvec 2'
483-
or diag "got=$got";
443+
is_pdl $col->enumvec, indx('[0 1 0 1 0 1 2 0 1 2 3 0 1 2 0 1]'), 'enumvec 2';
484444
$col = pdl("[0 0 1 1 2 2 2 3 3 3 3 4 4 4 5 5 6]")->transpose;
485-
$got = $col->enumvec;
486-
ok all(approx($got, pdl('[0 1 0 1 0 1 2 0 1 2 3 0 1 2 0 1 0]'))), 'enumvec 3'
487-
or diag "got=$got";
445+
is_pdl $col->enumvec, indx('[0 1 0 1 0 1 2 0 1 2 3 0 1 2 0 1 0]'), 'enumvec 3';
488446

489447
## 13..17: test rldseq(), rleseq()
490-
my $lens = pdl(long,[qw(3 0 1 4 2)]);
448+
my $lens = indx([qw(3 0 1 4 2)]);
491449
my $offs = (($lens->xvals+1)*100)->short;
492450
my $seqs = zeroes(short, 0);
493451
$seqs = $seqs->append(sequence(short,$_)) foreach ($lens->list);
494452
$seqs += $lens->rld($offs);
495-
496-
my $seqs_got = $lens->rldseq($offs);
497-
is $seqs_got->type, $seqs->type, "rldseq():type";
498-
ok all(approx($seqs_got, $seqs)), "rldseq():data";
499-
453+
is_pdl $lens->rldseq($offs), $seqs, "rldseq():data";
500454
my ($len_got,$off_got) = $seqs->rleseq();
501455
is $off_got->type, $seqs->type, "rleseq():type";
502-
ok all(approx($len_got->where($len_got), $lens->where($lens))), "rleseq():lens";
503-
ok all(approx($off_got->where($len_got), $offs->where($lens))), "rleseq():offs";
456+
is_pdl $len_got->where($len_got), $lens->where($lens), "rleseq():lens";
457+
is_pdl $off_got->where($len_got), $offs->where($lens), "rleseq():offs";
504458

505459
eval {meshgrid(sequence(2,2))};
506460
like $@, qr/1-dimensional/, 'meshgrid rejects >1-D';
507461
my @vecs = (xvals(3), xvals(4)+5, xvals(2)+10);
508462
my @mesh_got = meshgrid(@vecs);
509-
is_deeply [$_->dims], [3,4,2] for @mesh_got;
510-
ok all($mesh_got[$_]->mv($_,0)->slice(',(0),(0)')==$vecs[$_]), "meshgrid $_" for 0..$#vecs;
463+
is_pdl $_->shape, indx([3,4,2]) for @mesh_got;
464+
is_pdl $mesh_got[$_]->mv($_,0)->slice(',(0),(0)'), $vecs[$_], "meshgrid $_" for 0..$#vecs;
511465

512466
done_testing;

0 commit comments

Comments
 (0)