Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vsearch: fix segv if vsearch $x arg is empty; add support for bad vaues in $vals argument #454

Merged
merged 1 commit into from Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
47 changes: 35 additions & 12 deletions Basic/Primitive/primitive.pd
Expand Up @@ -2913,17 +2913,39 @@ for my $func ( [


my $code = undent q[
PDL_Indx n1 = $SIZE(n)-1;
PDL_Indx low = %LOW%;
PDL_Indx high = %HIGH%;
PDL_Indx mid;
if ( $SIZE(n) == 0 ){
broadcastloop %{
loop(n) %{
$SETBAD(idx());
%}
%}
}
else {
broadcastloop %{

$GENERIC() value = $vals();
loop(n) %{

/* determine sort order of data */
int up = %UP%;
%CODE%
----
if ( $ISGOOD(vals()) ) {
PDL_Indx n1 = $SIZE(n)-1;
PDL_Indx low = %LOW%;
PDL_Indx high = %HIGH%;
PDL_Indx mid;

$GENERIC() value = $vals();

/* determine sort order of data */
int up = %UP%;
%CODE%
}

else {
$SETBAD(idx());
}
%}

%}
}
----
];

my $doc = undent q[
Expand All @@ -2936,7 +2958,8 @@ for my $func ( [
$idx = %FUNC%($vals, $x);

C<$x> must be sorted, but may be in decreasing or increasing
order.
order. if C<$x> is empty, then all values in C<$idx> will be
set to the bad value.

%PRE%
%BODY%
Expand All @@ -2952,8 +2975,8 @@ for my $func ( [

pp_def(
$func,
HandleBad => 0,
BadDoc => 'needs major (?) work to handles bad values',
HandleBad => 1,
BadDoc => 'bad values in vals() result in bad values in idx()',
Pars => 'vals(); x(n); indx [o]idx()',
GenericTypes => $F, # too restrictive ?
Code => $code,
Expand Down
27 changes: 27 additions & 0 deletions t/primitive/vsearch.t
Expand Up @@ -3,6 +3,7 @@
use strict;
use warnings;
use Test2::V0 '!float';
use Test2::Util;

use PDL::LiteF;
use Test::Lib;
Expand Down Expand Up @@ -375,6 +376,14 @@ for my $mode ( sort keys %search ) {
$so->{equal} ),
'equal elements';

my $badmask = $so->{x}->random < 0.25;
my $badx = $so->{x}->setbadif( $badmask );
my $bad_eq = $so->{equal}->setbadif( $badmask );

ok tapprox( vsearch( $badx, $so->{x}, { mode => $mode } ),
$bad_eq ),
'equal elements w/ bad vals';

ok tapprox(
vsearch( $so->{x} - 5, $so->{x}, { mode => $mode } ),
$so->{nequal_m} ),
Expand Down Expand Up @@ -446,4 +455,22 @@ for my $mode ( sort keys %search ) {
ok tapprox( $indx0, $indx1 ), 'explicit ndarray == implicit ndarray';
}

subtest regressions => sub {

subtest '$xs->is_empty' => sub {

skip 'check for regression requires fork' unless Test2::Util::CAN_FORK;
require Test2::AsyncSubtest;

my $ast = Test2::AsyncSubtest->new( name => 'vsearch' );
$ast->run_fork(
sub {
ok lives { pdl( [0] )->vsearch_bin_inclusive( pdl( [] ) ) }
}
);
$ast->finish;

};

};
done_testing;