Skip to content

Commit 8ba0b86

Browse files
committed
core tests replace approx with is_pdl - #34
1 parent a6073c0 commit 8ba0b86

File tree

2 files changed

+90
-130
lines changed

2 files changed

+90
-130
lines changed

Basic/t/core.t

+61-85
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ my $b_dbl = $a_dbl->slice('5');
134134
my $c_long = $a_long->slice('4:7');
135135
my $c_dbl = $a_dbl->slice('4:7');
136136
is $b_long->sclr, 5, "sclr test of 1-elem pdl (long)";
137-
ok approx( $b_dbl->sclr, 5 ), "sclr test of 1-elem pdl (dbl)";
137+
is $b_dbl->sclr, 5, "sclr test of 1-elem pdl (dbl)";
138138
eval { $c_long->sclr };
139139
like $@, qr/multielement ndarray in 'sclr' call/, "sclr failed on multi-element ndarray (long)";
140140
eval { $c_dbl->sclr };
@@ -162,18 +162,18 @@ ok eq_array( [ $y->dims ], [3,4] ), "reshape()";
162162
my $x = ones 3,1,4;
163163
my $y = $x->reshape(-1);
164164
my $c = $x->squeeze;
165-
ok eq_array( [ $y->dims ], [3,4] ), "reshape(-1)";
166-
ok all( $y == $c ), "squeeze";
165+
is_pdl $y->shape, indx([3,4]), "reshape(-1)";
166+
is_pdl $y, $c, "squeeze";
167167
$c++; # check dataflow in reshaped PDL
168-
ok all( $y == $c ), "dataflow"; # should flow back to y
169-
ok all( $x == 2 ), "dataflow";
168+
is_pdl $y, $c, "dataflow"; # should flow back to y
169+
is_pdl $x, pdl(2)->slice('*3,*1,*4'), "dataflow";
170170
}
171171

172172
{
173173
my $d = pdl(5); # zero dim ndarray and reshape/squeeze
174-
ok $d->reshape(-1)->ndims==0, "reshape(-1) on 0-dim PDL gives 0-dim PDL";
175-
ok $d->reshape(1)->ndims==1, "reshape(1) on 0-dim PDL gives 1-dim PDL";
176-
ok $d->reshape(1)->reshape(-1)->ndims==0, "reshape(-1) on 1-dim, 1-element PDL gives 0-dim PDL";
174+
is_pdl $d->reshape(-1)->shape, empty(indx), "reshape(-1) on 0-dim PDL gives 0-dim PDL";
175+
is_pdl $d->reshape(1)->shape, indx([1]), "reshape(1) on 0-dim PDL gives 1-dim PDL";
176+
is_pdl $d->reshape(1)->reshape(-1)->shape, empty(indx), "reshape(-1) on 1-dim, 1-element PDL gives 0-dim PDL";
177177
}
178178

179179
{
@@ -260,11 +260,10 @@ my $subobj = PDL::Trivial->new(6);
260260
isa_ok $subobj, 'PDL::Trivial';
261261
isa_ok +PDL->topdl($subobj), 'PDL::Trivial';
262262
isa_ok $subobj->inplace, 'PDL::Trivial';
263-
isa_ok( PDL->topdl(1), "PDL", "topdl(1) returns an ndarray" );
264-
isa_ok( PDL->topdl([1,2,3]), "PDL", "topdl([1,2,3]) returns an ndarray" );
265-
isa_ok( PDL->topdl(1,2,3), "PDL", "topdl(1,2,3) returns an ndarray" );
266-
my $x=PDL->topdl(1,2,3);
267-
ok (($x->nelem == 3 and all($x == pdl(1,2,3))), "topdl(1,2,3) returns a 3-ndarray containing (1,2,3)");
263+
isa_ok +PDL->topdl(1), "PDL", "topdl(1) returns an ndarray";
264+
isa_ok +PDL->topdl([1,2,3]), "PDL", "topdl([1,2,3]) returns an ndarray";
265+
isa_ok +PDL->topdl(1,2,3), "PDL", "topdl(1,2,3) returns an ndarray";
266+
is_pdl +PDL->topdl(1,2,3), pdl(1,2,3), "topdl(1,2,3) returns a 3-ndarray containing (1,2,3)";
268267
eval {PDL->topdl({})};
269268
isnt $@, '', 'topdl({}) no segfault';
270269
}
@@ -331,63 +330,52 @@ is $PDL::undefval, 0, "default value of \$PDL::undefval is 0";
331330

332331
{
333332
my $x = [ [ 2, undef ], [3, 4 ] ];
334-
my $y = pdl( $x );
335-
my $c = pdl( [ 2, 0, 3, 4 ] )->reshape(2,2);
336-
ok all( $y == $c ), "undef converted to 0 (dbl)";
337-
ok eq_array( $x, [[2,undef],[3,4]] ), "pdl() has not changed input array";
338-
$y = pdl( long, $x );
339-
$c = pdl( long, [ 2, 0, 3, 4 ] )->reshape(2,2);
340-
ok all( $y == $c ), "undef converted to 0 (long)";
341-
}
342-
343-
{
344-
local($PDL::undefval) = -999;
345-
my $x = [ [ 2, undef ], [3, 4 ] ];
346-
my $y = pdl( $x );
347-
my $c = pdl( [ 2, -999, 3, 4 ] )->reshape(2,2);
348-
ok all( $y == $c ), "undef converted to -999 (dbl)";
349-
$y = pdl( long, $x );
350-
$c = pdl( long, [ 2, -999, 3, 4 ] )->reshape(2,2);
351-
ok all( $y == $c ), "undef converted to -999 (long)";
333+
my $y = pdl($x);
334+
my $c = pdl([[2, 0],[3, 4]]);
335+
is_pdl $y, $c, "undef converted to 0 (dbl)";
336+
is_deeply $x, [[2,undef],[3,4]], "pdl() has not changed input array";
337+
is_pdl long($x), long($c), "undef converted to 0 (long)";
338+
}
339+
340+
{
341+
local($PDL::undefval) = -999;
342+
my $x = [ [ 2, undef ], [3, 4 ] ];
343+
my $y = pdl($x);
344+
my $c = pdl('2 -999; 3 4');
345+
is_pdl $y, $c, "undef converted to -999 (dbl)";
346+
is_pdl long($x), long($c), "undef converted to -999 (long)";
352347
};
353348

354349
{
355350
# Funky constructor cases
356351
# pdl of a pdl
357-
my $x = pdl(pdl(5));
358-
ok all( $x== pdl(5)), "pdl() can piddlify an ndarray";
359-
$x = pdl(null);
360-
ok $x->isnull, 'pdl(null) gives null' or diag "x(", $x->info, ")";
352+
is_pdl pdl(pdl(5)), pdl(5), "pdl() can piddlify an ndarray";
353+
is_pdl pdl(null), null, 'pdl(null) gives null';
361354

362-
$x = pdl(null, null);
363-
is_deeply [$x->dims], [0,2], 'pdl(null, null) gives empty' or diag "x(", $x->info, ")";
364-
ok !$x->isnull, 'pdl(null, null) gives non-null' or diag "x(", $x->info, ")";
355+
is_pdl pdl(null, null), zeroes(0,2), 'pdl(null, null) gives empty';
365356

366357
# pdl of mixed-dim pdls: pad within a dimension
367-
$x = pdl( zeroes(5), ones(3) );
368-
ok all($x == pdl([0,0,0,0,0],[1,1,1,0,0])),"Piddlifying two ndarrays concatenates them and pads to length" or diag("x=$x\n");
358+
is_pdl pdl( zeroes(5), ones(3) ), pdl([0,0,0,0,0],[1,1,1,0,0]),"Piddlifying two ndarrays concatenates them and pads to length";
369359

370360
# pdl of mixed-dim pdls: pad a whole dimension
371-
$x = pdl( [[9,9],[8,8]], xvals(3)+1 );
372-
ok all($x == pdl([[[9,9],[8,8],[0,0]] , [[1,0],[2,0],[3,0]] ])),"can concatenate mixed-dim ndarrays" or diag("x=$x\n");
361+
is_pdl pdl( [[9,9],[8,8]], xvals(3)+1 ), pdl([[[9,9],[8,8],[0,0]] , [[1,0],[2,0],[3,0]] ]),"can concatenate mixed-dim ndarrays";
373362

374363
# pdl of mixed-dim pdls: a hairier case
375-
my $c = pdl [1], pdl[2,3,4], pdl[5];
376-
ok all($c == pdl([[[1,0,0],[0,0,0]],[[2,3,4],[5,0,0]]])),"Can concatenate mixed-dim ndarrays: hairy case" or diag("c=$c\n");
364+
is_pdl pdl([1], pdl[2,3,4], pdl[5]), pdl([[[1,0,0],[0,0,0]],[[2,3,4],[5,0,0]]]),"Can concatenate mixed-dim ndarrays: hairy case";
377365
}
378366

379367
# same thing, with undefval set differently
380368
{
381369
local($PDL::undefval) = 99;
382370
my $c = pdl undef;
383-
ok all($c == pdl(99)), "explicit, undefval of 99 works" or diag("c=$c\n");
371+
is_pdl $c, pdl(99), "explicit, undefval of 99 works";
384372
$c = pdl [1], pdl[2,3,4], pdl[5];
385-
ok all($c == pdl([[[1,99,99],[99,99,99]],[[2,3,4],[5,99,99]]])), "implicit, undefval works for padding" or diag("c=$c\n");
373+
is_pdl $c, pdl([[[1,99,99],[99,99,99]],[[2,3,4],[5,99,99]]]), "implicit, undefval works for padding";
386374
$PDL::undefval = undef;
387375
$c = pdl undef;
388-
ok all($c == pdl(0)), "explicit, undefval of undef falls back to 0" or diag("c=$c\n");
376+
is_pdl $c, pdl(0), "explicit, undefval of undef falls back to 0";
389377
$c = pdl [1], [2,3,4];
390-
ok all($c == pdl([1,0,0],[2,3,4])), "implicit, undefval of undef falls back to 0" or diag("c=$c\n");
378+
is_pdl $c, pdl([1,0,0],[2,3,4]), "implicit, undefval of undef falls back to 0";
391379
$PDL::undefval = inf;
392380
$c = pdl undef;
393381
ok all($c == inf), "explicit, undefval of PDL scalar works" or diag("c=$c\n");
@@ -400,22 +388,17 @@ ok all($c == pdl([[[1,0,0],[0,0,0]],[[2,3,4],[5,0,0]]])),"Can concatenate mixed-
400388
my $x = eval {zeroes(2,0,1);};
401389
is($@, '', "zeroes accepts empty PDL specification");
402390

403-
my $y = eval { pdl($x,sequence(2,0,1)); };
404-
is $@, '';
405-
ok all(pdl($y->dims) == pdl(2,0,1,2)), "concatenating two empties gives an empty";
391+
my $y = pdl($x,sequence(2,0,1));
392+
is_pdl $y->shape, indx(2,0,1,2), "concatenating two empties gives an empty";
406393

407-
eval { $y = pdl($x,sequence(2,1,1)); };
408-
is $@, '';
409-
ok all(pdl($y->dims) == pdl(2,1,1,2)), "concatenating an empty and a nonempty treats the empty as a filler";
394+
$y = pdl($x,sequence(2,1,1));
395+
is_pdl $y->shape, indx(2,1,1,2), "concatenating an empty and a nonempty treats the empty as a filler";
410396

411-
eval { $y = pdl($x,5) };
412-
is $@, '';
413-
ok all(pdl($y->dims)==pdl(2,1,1,2)), "concatenating an empty and a scalar on the right works";
397+
$y = pdl($x,5);
398+
is_pdl $y->shape, indx(2,1,1,2), "concatenating an empty and a scalar on the right works";
414399

415-
eval { $y = pdl(5,$x) };
416-
is $@, '';
417-
ok all(pdl($y->dims)==pdl(2,1,1,2)), "concatenating an empty and a scalar on the left works";
418-
ok( all($y==pdl([[[5,0]]],[[[0,0]]])), "concatenating an empty and a scalar on the left gives the right answer");
400+
$y = pdl(5,$x);
401+
is_pdl $y, pdl([[[5,0]]],[[[0,0]]]), "concatenating an empty and a scalar on the left gives the right answer";
419402
}
420403

421404
# cat problems
@@ -445,12 +428,7 @@ like($@, qr/\(argument 1\)/,
445428
'cat properly identifies the first actual ndarray in combined screw-ups');
446429
}
447430

448-
{
449-
my $x = eval {cat(pdl(1),pdl(2,3));};
450-
is($@, '', 'cat(pdl(1),pdl(2,3)) succeeds');
451-
is_deeply [$x->dims], [2,2], 'weird cat case has the right shape';
452-
ok( all( $x == pdl([1,1],[2,3]) ), "cat does the right thing with catting a 0-pdl and 2-pdl together");
453-
}
431+
is_pdl cat(pdl(1),pdl(2,3)), pdl([1,1],[2,3]), "cat does the right thing with catting a 0-pdl and 2-pdl together";
454432

455433
{
456434
my $lo=sequence(long,5)+32766;
@@ -460,13 +438,13 @@ my $by=sequence(byte,5)+253;
460438
my @list = ($lo,$so,$fl,$by);
461439
my $c2 = cat(@list);
462440
is($c2->type,'float','concatenating different datatypes returns the highest type');
463-
ok(all($_==shift @list),"cat/dog symmetry for values") for $c2->dog;
441+
is_pdl $_, shift @list, {require_equal_types=>0, test_name=>"cat/dog symmetry for values"} for $c2->dog;
464442
my ($dogcopy) = $c2->dog({Break=>1});
465443
$dogcopy++;
466-
ok all($dogcopy != $c2->slice(':,(0)')), 'Break means copy'; # not lo as cat no flow
444+
is_pdl $dogcopy, $c2->slice(':,(0)')+1, 'Break means copy'; # not lo as cat no flow
467445
my ($dogslice) = $c2->dog;
468446
$dogslice++;
469-
ok all($dogslice == $c2->slice(':,(0)')), 'no Break means dataflow' or diag "got=$dogslice\nexpected=$lo";
447+
is_pdl $dogslice, $c2->slice(':,(0)'), 'no Break means dataflow';
470448
eval {pdl([3])->dog(5)};
471449
like $@, qr/Usage/, "error if excess args";
472450
for ([[], qr/at least/], [[5]], [[4,5]]) {
@@ -489,36 +467,36 @@ my $y = $x->copy;
489467
ok $x->is_inplace,"original item true inplace flag after copy";
490468
ok !$y->is_inplace,"copy has false inplace flag";
491469
$y++;
492-
ok all($y!=sequence(byte,5)),"copy returns severed copy of the original thing if inplace is set";
470+
is_pdl $y, sequence(byte,5)+1,"copy returns severed copy of the original thing if inplace is set";
493471
ok $x->is_inplace,"original item still true inplace flag";
494472
ok !$y->is_inplace,"copy still false inplace flag";
495-
ok all($x==sequence(byte,5)),"copy really is severed";
473+
is_pdl $x, sequence(byte,5),"copy really is severed";
496474
}
497475

498476
{
499477
# new_or_inplace
500478
my $x = sequence(byte,5);
501479
my $y = $x->new_or_inplace;
502-
ok( all($y==$x) && ($y->get_datatype == $x->get_datatype), "new_or_inplace with no pref returns something like the orig.");
480+
is_pdl $y, $x, "new_or_inplace with no pref returns something like the orig.";
503481
$y++;
504-
ok(all($y!=$x),"new_or_inplace with no inplace flag returns something disconnected from the orig.");
482+
is_pdl $y, $x+1, "new_or_inplace with no inplace flag returns something disconnected from the orig.";
505483

506484
$y = $x->new_or_inplace("float,long");
507-
ok($y->type eq 'float',"new_or_inplace returns the first type in case of no match");
485+
is $y->type, 'float',"new_or_inplace returns first type in case of no match";
508486

509487
$y = $x->inplace->new_or_inplace;
510488
$y++;
511-
ok(all($y==$x),"new_or_inplace returns the original thing if inplace is set");
512-
ok(!($y->is_inplace),"new_or_inplace clears the inplace flag");
489+
is_pdl $y, $x, "new_or_inplace returns the original thing if inplace is set";
490+
ok !$y->is_inplace,"new_or_inplace clears the inplace flag";
513491
}
514492

515493
{
516494
# check reshape and dims. While we're at it, check null & empty creation too.
517495
my $empty = empty();
518496
is $empty->type->enum, 0, 'empty() gives lowest-numbered type';
519497
is empty(float)->type, 'float', 'empty(float) works';
520-
ok($empty->nelem==0,"you can make an empty PDL with zeroes(0)");
521-
ok("$empty" =~ m/Empty/, "an empty PDL prints 'Empty'");
498+
is $empty->nelem, 0, "you can make an empty PDL with zeroes(0)";
499+
like "$empty", qr/Empty/, "an empty PDL prints 'Empty'";
522500
}
523501

524502
{
@@ -545,11 +523,10 @@ like $@, qr/null/, 'null->long gives right error';
545523
}
546524

547525
{
548-
my $x = short pdl(3,4,5,6);
526+
my $x = short(3,4,5,6);
549527
eval { $x->reshape(2,2);};
550528
is($@, '', "reshape succeeded in the normal case");
551-
ok( ( $x->ndims==2 and $x->dim(0)==2 and $x->dim(1)==2 ), "reshape did the right thing");
552-
ok(all($x == short pdl([[3,4],[5,6]])), "reshape moved the elements to the right place");
529+
is_pdl $x, short([[3,4],[5,6]]), "reshape moved the elements to the right place";
553530
my $y = $x->slice(":,:");
554531
eval { $y->reshape(4); };
555532
unlike $@, qr/Can't/, "reshape doesn't fail on a PDL with a parent";
@@ -584,9 +561,8 @@ SKIP: {
584561
my $neg = -684394069604;
585562
my $straight_pdl = pdl($neg);
586563
my $multed = pdl(1) * $neg;
587-
ok $straight_pdl == $multed, 'upgrade of large negative SV to ndarray'
588-
or diag "straight=$straight_pdl mult=$multed\n",
589-
"straight:", $straight_pdl->info, " mult:", $multed->info;
564+
is $straight_pdl, $multed, 'upgrade of large negative SV to ndarray'
565+
or diag "straight:", $straight_pdl->info, " mult:", $multed->info;
590566
}
591567
{
592568
my $fromuv_r = pdl('10223372036854775507');

0 commit comments

Comments
 (0)