1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
|
# t/sort_tests.pl -- utility routines for Data::Sorting test scripts.
use Array::Compare;
# Inspiried by test.pl from Sort::Naturally by Sean M. Burke
sub shuffle {
my @out;
while(@_) { push @out, splice @_, rand(@_), 1 };
return @out
}
sub arrays_match {
my $array = shift;
# warn "Checking: " . join( ', ', map "'$_'", @$array ) . "\n";
CANDIDATE: foreach my $candidate (@_) {
# warn "Against: " . join( ', ', map "'$_'", @$candidate ) . "\n";
my $comp = Array::Compare->new;
return 1 if $comp->compare( $array, $candidate );
}
# warn( "Didn't match!" );
return
}
sub test_sort_cases {
my @tests = @_;
foreach my $test ( @tests ) {
my @values = @{ $test->{values} };
my @acceptable = (
$test->{okvals} ? @{ $test->{okvals} } :
$test->{okidxs} ? map({[ map $values[$_-1], @$_ ]} @{ $test->{okidxs} }) :
$test->{values}
);
# warn "Values: " . join( ', ', map "'$_'", @values ) . "\n";
# warn "Acceptable: " . join( ', ', map "'$_'", @acceptable ) . "\n";
my @params = @{ $test->{sorted} };
# warn "Sorting: " . join(', ', Data::Sorting::sort_description('text', @params) ) . "\n";
my $sort_function = Data::Sorting::sort_function( @params );
unless ( arrays_match( [ $sort_function->( @values ) ], \@values ) ) {
ok( 0, "not stable" );
next;
};
my @rc;
foreach ( 1 .. 10 ) {
my @shuffled = shuffle( @values );
# warn "Shuffled: " . join( ', ', map "'$_'", @shuffled ) . "\n";
my @sorted = $sort_function->( @shuffled );
# warn "Sorted: " . join( ', ', map "'$_'", @sorted ) . "\n";
# warn "Match: " . join( ', ', map "'$_'", \@sorted, @acceptable ) . "\n";
push @rc, arrays_match( \@sorted, @acceptable );
}
ok( ! grep { ! $_ } @rc, "not repeatable" );
}
}
1;
|