#!/usr/bin/perl -w
use strict;
use Benchmark;
my $LARGE = 30;
print '1..', $LARGE + 1, "\n";
require Sort::Merge; import Sort::Merge;
print "ok 1\n";

# Lots of randomly-generated tests.  Since the comparison function
# is a black box we might as well use something fast:
#
my $f = sub( $$ ) { $_[0] <=> $_[1] };
foreach my $i (2 .. $LARGE + 1) {
    my @ls;
    my $num_lists = int(rand($LARGE));
    while ($num_lists--) {
	my @l;
	my $len = int(rand($LARGE));
	while ($len--) {
	    push @l, int(rand($LARGE));
	}
	push @ls, [ sort $f @l ];
    }

    my @expected = sort $f (map { @$_ } @ls);
    my @got = mergesort($f, @ls);
    if (join(' ', @got) ne join(' ', @expected)) {
	print "not ok $i\n";
    }
    else {
	print "ok $i\n";
    }
}
