Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: A proper name for is_sorted function that can check more than just sorting order?

by 1nickt (Canon)
on Dec 25, 2017 at 12:54 UTC ( [id://1206165]=note: print w/replies, xml ) Need Help??


in reply to A proper name for is_sorted function that can check more than just sorting order?

Hi Dallaylaen,

Well, it may be because I am grumpy as always at this time of year, but to me it seems that you are trying to make things too shiny.

I prefer to use Lego bricks rather than modelling clay. As soon as you get your testing function just right, Murphy's Law says you will encounter data that doesn't fit. I would test your condition like this:

use strict; use warnings; use Test::More; my @data = ( { id => 1, start => 2, end => 3 }, { id => 2, start => 3, end => 4 }, { id => 3, start => 4, end => 5 }, # fail { id => 42, start => 6, end => 7 }, { id => 666, start => 7, end => 8 }, { id => 999, start => 8, end => 9 }, ); for ( 0 .. $#data - 1 ) { is( $data[ $_ + 1 ]->{'start'}, $data[ $_ ]->{'end'}, "$data[ $_ ] +->{'id'} sequence" ); } done_testing; __END__
Output:
1206160.pl .. 1/? # Failed test '3 sequence' # at 1206160.pl line 18. # got: '6' # expected: '5' # Looks like you failed 1 test of 5. 1206160.pl .. Dubious, test returned 1 (wstat 256, 0x100) Failed 1/5 subtests Test Summary Report ------------------- 1206160.pl (Wstat: 256 Tests: 5 Failed: 1) Failed test: 3 Non-zero exit status: 1 Files=1, Tests=5, 0 wallclock secs ( 0.03 usr 0.00 sys + 0.04 cusr + 0.01 csys = 0.08 CPU) Result: FAIL

Or maybe like this .... Edit: as ikegami revealed the below is buggy. (emits a warning if number of list elements is not even):

se strict; use warnings; use Test::More; use List::Util 'pairfirst'; my @data = ( { id => 1, start => 2, end => 3 }, { id => 2, start => 3, end => 4 }, { id => 3, start => 4, end => 5 }, # fail { id => 42, start => 6, end => 7 }, { id => 666, start => 7, end => 8 }, { id => 999, start => 8, end => 9 }, ); my @failed = pairfirst { $a->{'end'} ne $b->{'start'} } @data; is( @failed, undef, 'sequencing' ) or diag sprintf 'ID %s and ID %s are not sequential', map { $_->{'id +'} } @failed; done_testing; __END__
Output:
prove 1206160.pl 1206160.pl .. 1/? # Failed test 'sequencing' # at 1206160.pl line 16. # got: '2' # expected: undef # ID 3 and ID 42 are not sequential # Looks like you failed 1 test of 1. 1206160.pl .. Dubious, test returned 1 (wstat 256, 0x100) Failed 1/1 subtests Test Summary Report ------------------- 1206160.pl (Wstat: 256 Tests: 1 Failed: 1) Failed test: 1 Non-zero exit status: 1 Files=1, Tests=1, 0 wallclock secs ( 0.02 usr 0.00 sys + 0.06 cusr + 0.00 csys = 0.08 CPU) Result: FAIL

Hope this helps, and Happy Merry!


The way forward always starts with a minimal test.

Replies are listed 'Best First'.
Re^2: A proper name for is_sorted function that can check more than just sorting order?
by ikegami (Patriarch) on Dec 25, 2017 at 20:50 UTC

    Your second solution is buggy. It fails to find a non-matching record for the following:

    my @data = ( { id => 1, start => 2, end => 3 }, { id => 2, start => 3, end => 5 }, { id => 3, start => 4, end => 5 }, { id => 5, start => 5, end => 6 }, );

      Withdrawn, thanks.

      The way forward always starts with a minimal test.
Re^2: A proper name for is_sorted function that can check more than just sorting order?
by Dallaylaen (Chaplain) on Dec 28, 2017 at 10:58 UTC

    Good point. For complex checks (anything but simple ordering) division into smaller conditions is almost always better.

    This makes me think of another function that (1) packs all smaller tests into a subtest (so that it's "array is such-and-such" and not "ok 1, ok 2, ... ok 100500") and (2) provides access to adjacent array elements via $a and $b.

    Although possibly it should be just handed off to List::Util::foreach_adjacent (lame name off the top of my head) or smth.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1206165]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2024-04-24 20:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found