From Code to Community: Sponsoring The Perl and Raku Conference 2025 Learn more

class TestCase::Lib::Sort {
use Sort;
use Fn;
use Array;
use TestCase::Minimal;
static method sort_byte : int () {
# Sort array by asc order
{
my $nums = [(byte)2, 3, 1];
Sort->sort_byte($nums, 0, scalar @$nums, method : int ($a : byte, $b : byte) {
return $a <=> $b;
});
unless (Array->equals_byte($nums, [(byte)1, 2, 3])) {
return 0;
}
}
# Sort array by asc order
{
my $nums = [(byte)2, 3, 1];
Sort->sort_byte_asc($nums, 0, scalar @$nums);
unless (Array->equals_byte($nums, [(byte)1, 2, 3])) {
return 0;
}
}
# Sort array by asc order more long
{
my $nums = [(byte)5, 7, 9, 2, 4, 8, 1, 3, 6, 0];
Sort->sort_byte($nums, 0, scalar @$nums, method : int ($a : byte, $b : byte) {
return $a <=> $b;
});
unless (Array->equals_byte($nums, [(byte)0, 1, 2, 3, 4, 5, 6, 7, 8, 9])) {
return 0;
}
}
# Sort array by asc order more long with same values
{
my $nums = [(byte)5, 7, 9, 2, 5, 4, 8, 1, 3, 6, 3, 0];
Sort->sort_byte($nums, 0, scalar @$nums, method : int ($a : byte, $b : byte) {
return $a <=> $b;
});
unless (Array->equals_byte($nums, [(byte)0, 1, 2, 3, 3, 4, 5, 5, 6, 7, 8, 9])) {
return 0;
}
}
# Sort byte array by desc order
{
my $nums = [(byte)2, 3, 1];
Sort->sort_byte($nums, 0, scalar @$nums, method : int ($a : byte, $b : byte) {
return $b <=> $a;
});
unless (Array->equals_byte($nums, [(byte)3, 2, 1])) {
return 0;
}
}
{
my $nums = [(byte)2, 3, 1];
Sort->sort_byte_desc($nums, 0, scalar @$nums);
unless (Array->equals_byte($nums, [(byte)3, 2, 1])) {
return 0;
}
}
# If length is 0, There is nothing to do
{
my $nums = [(byte)2, 3, 1];
Sort->sort_byte($nums, 0, 0, method : int ($a : byte, $b : byte) {
return $a <=> $b;
});
unless (Array->equals_byte($nums, [(byte)2, 3, 1])) {
return 0;
}
}
# Sort partially
{
my $nums = [(byte)5, 2, 3, 1, -10];
Sort->sort_byte($nums, 1, 3, method : int ($a : byte, $b : byte) {
return $a <=> $b;
});
unless (Array->equals_byte($nums, [(byte)5, 1, 2, 3, -10])) {
return 0;
}
}
# Exception - Offset must be more than or equals to 0
{
my $nums = [(byte)5, 2, 3, 1, -10];
eval {
Sort->sort_byte($nums, -1, 3, method : int ($a : byte, $b : byte) {
return $a <=> $b;
});
};
unless ($@) {
return 0;
}
}
# Exception - Length must be more than or equals to 0
{
my $nums = [(byte)5, 2, 3, 1, -10];
eval {
Sort->sort_byte($nums, 1, -1, method : int ($a : byte, $b : byte) {
return $a <=> $b;
});
};
unless ($@) {
return 0;
}
}
# Exception - Offset + Length must be in the array range
{
my $nums = [(byte)5, 2, 3, 1, -10];
eval {
Sort->sort_byte($nums, 3, 3, method : int ($a : byte, $b : byte) {
return $a <=> $b;
});
};
unless ($@) {
return 0;
}
}
$@ = undef;
return 1;
}
static method sort_short : int () {
# Sort array by asc order
{
my $nums = [(short)2, 3, 1];
Sort->sort_short($nums, 0, scalar @$nums, method : int ($a : short, $b : short) {
return $a <=> $b;
});
unless (Array->equals_short($nums, [(short)1, 2, 3])) {
return 0;
}
}
{
my $nums = [(short)2, 3, 1];
Sort->sort_short_asc($nums, 0, scalar @$nums);
unless (Array->equals_short($nums, [(short)1, 2, 3])) {
return 0;
}
}
# Sort array by asc order more long
{
my $nums = [(short)5, 7, 9, 2, 4, 8, 1, 3, 6, 0];
Sort->sort_short($nums, 0, scalar @$nums, method : int ($a : short, $b : short) {
return $a <=> $b;
});
unless (Array->equals_short($nums, [(short)0, 1, 2, 3, 4, 5, 6, 7, 8, 9])) {
return 0;
}
}
# Sort array by asc order more long with same values
{
my $nums = [(short)5, 7, 9, 2, 5, 4, 8, 1, 3, 6, 3, 0];
Sort->sort_short($nums, 0, scalar @$nums, method : int ($a : short, $b : short) {
return $a <=> $b;
});
unless (Array->equals_short($nums, [(short)0, 1, 2, 3, 3, 4, 5, 5, 6, 7, 8, 9])) {
return 0;
}
}
# Sort short array by desc order
{
my $nums = [(short)2, 3, 1];
Sort->sort_short($nums, 0, scalar @$nums, method : int ($a : short, $b : short) {
return $b <=> $a;
});
unless (Array->equals_short($nums, [(short)3, 2, 1])) {
return 0;
}
}
{
my $nums = [(short)2, 3, 1];
Sort->sort_short_desc($nums, 0, scalar @$nums);
unless (Array->equals_short($nums, [(short)3, 2, 1])) {
return 0;
}
}
# If length is 0, There is nothing to do
{
my $nums = [(short)2, 3, 1];
Sort->sort_short($nums, 0, 0, method : int ($a : short, $b : short) {
return $a <=> $b;
});
unless (Array->equals_short($nums, [(short)2, 3, 1])) {
return 0;
}
}
# Sort partially
{
my $nums = [(short)5, 2, 3, 1, -10];
Sort->sort_short($nums, 1, 3, method : int ($a : short, $b : short) {
return $a <=> $b;
});
unless (Array->equals_short($nums, [(short)5, 1, 2, 3, -10])) {
return 0;
}
}
# Exception - Array must be not undef
{
my $nums = [(short)2, 3, 1];
eval {
Sort->sort_short(undef, 0, scalar @$nums, method : int ($a : short, $b : short) {
return $a <=> $b;
});
};
unless ($@) {
return 0;
}
}
# Exception - Offset must be more than or equals to 0
{
my $nums = [(short)5, 2, 3, 1, -10];
eval {
Sort->sort_short($nums, -1, 3, method : int ($a : short, $b : short) {
return $a <=> $b;
});
};
unless ($@) {
return 0;
}
}
# Exception - Length must be more than or equals to 0
{
my $nums = [(short)5, 2, 3, 1, -10];
eval {
Sort->sort_short($nums, 1, -1, method : int ($a : short, $b : short) {
return $a <=> $b;
});
};
unless ($@) {
return 0;
}
}
# Exception - Offset + Length must be in the array range
{
my $nums = [(short)5, 2, 3, 1, -10];
eval {
Sort->sort_short($nums, 3, 3, method : int ($a : short, $b : short) {
return $a <=> $b;
});
};
unless ($@) {
return 0;
}
}
$@ = undef;
return 1;
}
static method sort_int : int () {
# Sort array by asc order
{
my $nums = [(int)2, 3, 1];
Sort->sort_int($nums, 0, scalar @$nums, method : int ($a : int, $b : int) {
return $a <=> $b;
});
unless (Array->equals_int($nums, [(int)1, 2, 3])) {
return 0;
}
}
{
my $nums = [(int)2, 3, 1];
Sort->sort_int_asc($nums, 0, scalar @$nums);
unless (Array->equals_int($nums, [(int)1, 2, 3])) {
return 0;
}
}
# Sort array by asc order more long
{
my $nums = [(int)5, 7, 9, 2, 4, 8, 1, 3, 6, 0];
Sort->sort_int($nums, 0, scalar @$nums, method : int ($a : int, $b : int) {
return $a <=> $b;
});
unless (Array->equals_int($nums, [(int)0, 1, 2, 3, 4, 5, 6, 7, 8, 9])) {
return 0;
}
}
# Sort array by asc order more long with same values
{
my $nums = [(int)5, 7, 9, 2, 5, 4, 8, 1, 3, 6, 3, 0];
Sort->sort_int($nums, 0, scalar @$nums, method : int ($a : int, $b : int) {
return $a <=> $b;
});
unless (Array->equals_int($nums, [(int)0, 1, 2, 3, 3, 4, 5, 5, 6, 7, 8, 9])) {
return 0;
}
}
# Sort int array by desc order
{
my $nums = [(int)2, 3, 1];
Sort->sort_int($nums, 0, scalar @$nums, method : int ($a : int, $b : int) {
return $b <=> $a;
});
unless (Array->equals_int($nums, [(int)3, 2, 1])) {
return 0;
}
}
{
my $nums = [(int)2, 3, 1];
Sort->sort_int_desc($nums, 0, scalar @$nums);
unless (Array->equals_int($nums, [(int)3, 2, 1])) {
return 0;
}
}
# If length is 0, There is nothing to do
{
my $nums = [(int)2, 3, 1];
Sort->sort_int($nums, 0, 0, method : int ($a : int, $b : int) {
return $a <=> $b;
});
unless (Array->equals_int($nums, [(int)2, 3, 1])) {
return 0;
}
}
# Sort partially
{
my $nums = [(int)5, 2, 3, 1, -10];
Sort->sort_int($nums, 1, 3, method : int ($a : int, $b : int) {
return $a <=> $b;
});
unless (Array->equals_int($nums, [(int)5, 1, 2, 3, -10])) {
return 0;
}
}
# Exception - Array must be not undef
{
my $nums = [(int)2, 3, 1];
eval {
Sort->sort_int(undef, 0, scalar @$nums, method : int ($a : int, $b : int) {
return $a <=> $b;
});
};
unless ($@) {
return 0;
}
}
# Exception - Offset must be more than or equals to 0
{
my $nums = [(int)5, 2, 3, 1, -10];
eval {
Sort->sort_int($nums, -1, 3, method : int ($a : int, $b : int) {
return $a <=> $b;
});
};
unless ($@) {
return 0;
}
}
# Exception - Length must be more than or equals to 0
{
my $nums = [(int)5, 2, 3, 1, -10];
eval {
Sort->sort_int($nums, 1, -1, method : int ($a : int, $b : int) {
return $a <=> $b;
});
};
unless ($@) {
return 0;
}
}
# Exception - Offset + Length must be in the array range
{
my $nums = [(int)5, 2, 3, 1, -10];
eval {
Sort->sort_int($nums, 3, 3, method : int ($a : int, $b : int) {
return $a <=> $b;
});
};
unless ($@) {
return 0;
}
}
$@ = undef;
return 1;
}
static method sort_long : int () {
# Sort array by asc order
{
my $nums = [(long)2, 3, 1];
Sort->sort_long($nums, 0, scalar @$nums, method : int ($a : long, $b : long) {
return $a <=> $b;
});
unless (Array->equals_long($nums, [(long)1, 2, 3])) {
return 0;
}
}
{
my $nums = [(long)2, 3, 1];
Sort->sort_long_asc($nums, 0, scalar @$nums);
unless (Array->equals_long($nums, [(long)1, 2, 3])) {
return 0;
}
}
# Sort array by asc order more long
{
my $nums = [(long)5, 7, 9, 2, 4, 8, 1, 3, 6, 0];
Sort->sort_long($nums, 0, scalar @$nums, method : int ($a : long, $b : long) {
return $a <=> $b;
});
unless (Array->equals_long($nums, [(long)0, 1, 2, 3, 4, 5, 6, 7, 8, 9])) {
return 0;
}
}
# Sort array by asc order more long with same values
{
my $nums = [(long)5, 7, 9, 2, 5, 4, 8, 1, 3, 6, 3, 0];
Sort->sort_long($nums, 0, scalar @$nums, method : int ($a : long, $b : long) {
return $a <=> $b;
});
unless (Array->equals_long($nums, [(long)0, 1, 2, 3, 3, 4, 5, 5, 6, 7, 8, 9])) {
return 0;
}
}
# Sort long array by desc order
{
my $nums = [(long)2, 3, 1];
Sort->sort_long($nums, 0, scalar @$nums, method : int ($a : long, $b : long) {
return $b <=> $a;
});
unless (Array->equals_long($nums, [(long)3, 2, 1])) {
return 0;
}
}
{
my $nums = [(long)2, 3, 1];
Sort->sort_long_desc($nums, 0, scalar @$nums);
unless (Array->equals_long($nums, [(long)3, 2, 1])) {
return 0;
}
}
# If length is 0, There is nothing to do
{
my $nums = [(long)2, 3, 1];
Sort->sort_long($nums, 0, 0, method : int ($a : long, $b : long) {
return $a <=> $b;
});
unless (Array->equals_long($nums, [(long)2, 3, 1])) {
return 0;
}
}
# Sort partially
{
my $nums = [(long)5, 2, 3, 1, -10];
Sort->sort_long($nums, 1, 3, method : int ($a : long, $b : long) {
return $a <=> $b;
});
unless (Array->equals_long($nums, [(long)5, 1, 2, 3, -10])) {
return 0;
}
}
# Exception - Array must be not undef
{
my $nums = [(long)2, 3, 1];
eval {
Sort->sort_long(undef, 0, scalar @$nums, method : int ($a : long, $b : long) {
return $a <=> $b;
});
};
unless ($@) {
return 0;
}
}
# Exception - Offset must be more than or equals to 0
{
my $nums = [(long)5, 2, 3, 1, -10];
eval {
Sort->sort_long($nums, -1, 3, method : int ($a : long, $b : long) {
return $a <=> $b;
});
};
unless ($@) {
return 0;
}
}
# Exception - Length must be more than or equals to 0
{
my $nums = [(long)5, 2, 3, 1, -10];
eval {
Sort->sort_long($nums, 1, -1, method : int ($a : long, $b : long) {
return $a <=> $b;
});
};
unless ($@) {
return 0;
}
}
# Exception - Offset + Length must be in the array range
{
my $nums = [(long)5, 2, 3, 1, -10];
eval {
Sort->sort_long($nums, 3, 3, method : int ($a : long, $b : long) {
return $a <=> $b;
});
};
unless ($@) {
return 0;
}
}
$@ = undef;
return 1;
}
static method sort_float : int () {
# Sort array by asc order
{
my $nums = [(float)2, 3, 1];
Sort->sort_float($nums, 0, scalar @$nums, method : int ($a : float, $b : float) {
return $a <=> $b;
});
unless (Array->equals_float($nums, [(float)1, 2, 3])) {
return 0;
}
}
{
my $nums = [(float)2, 3, 1];
Sort->sort_float_asc($nums, 0, scalar @$nums);
unless (Array->equals_float($nums, [(float)1, 2, 3])) {
return 0;
}
}
# Sort array by asc order more long
{
my $nums = [(float)5, 7, 9, 2, 4, 8, 1, 3, 6, 0];
Sort->sort_float($nums, 0, scalar @$nums, method : int ($a : float, $b : float) {
return $a <=> $b;
});
unless (Array->equals_float($nums, [(float)0, 1, 2, 3, 4, 5, 6, 7, 8, 9])) {
return 0;
}
}
# Sort array by asc order more long with same values
{
my $nums = [(float)5, 7, 9, 2, 5, 4, 8, 1, 3, 6, 3, 0];
Sort->sort_float($nums, 0, scalar @$nums, method : int ($a : float, $b : float) {
return $a <=> $b;
});
unless (Array->equals_float($nums, [(float)0, 1, 2, 3, 3, 4, 5, 5, 6, 7, 8, 9])) {
return 0;
}
}
# Sort float array by desc order
{
my $nums = [(float)2, 3, 1];
Sort->sort_float($nums, 0, scalar @$nums, method : int ($a : float, $b : float) {
return $b <=> $a;
});
unless (Array->equals_float($nums, [(float)3, 2, 1])) {
return 0;
}
}
{
my $nums = [(float)2, 3, 1];
Sort->sort_float_desc($nums, 0, scalar @$nums);
unless (Array->equals_float($nums, [(float)3, 2, 1])) {
return 0;
}
}
# If length is 0, There is nothing to do
{
my $nums = [(float)2, 3, 1];
Sort->sort_float($nums, 0, 0, method : int ($a : float, $b : float) {
return $a <=> $b;
});
unless (Array->equals_float($nums, [(float)2, 3, 1])) {
return 0;
}
}
# Sort partially
{
my $nums = [(float)5, 2, 3, 1, -10];
Sort->sort_float($nums, 1, 3, method : int ($a : float, $b : float) {
return $a <=> $b;
});
unless (Array->equals_float($nums, [(float)5, 1, 2, 3, -10])) {
return 0;
}
}
# Exception - Array must be not undef
{
my $nums = [(float)2, 3, 1];
eval {
Sort->sort_float(undef, 0, scalar @$nums, method : int ($a : float, $b : float) {
return $a <=> $b;
});
};
unless ($@) {
return 0;
}
}
# Exception - Offset must be more than or equals to 0
{
my $nums = [(float)5, 2, 3, 1, -10];
eval {
Sort->sort_float($nums, -1, 3, method : int ($a : float, $b : float) {
return $a <=> $b;
});
};
unless ($@) {
return 0;
}
}
# Exception - Length must be more than or equals to 0
{
my $nums = [(float)5, 2, 3, 1, -10];
eval {
Sort->sort_float($nums, 1, -1, method : int ($a : float, $b : float) {
return $a <=> $b;
});
};
unless ($@) {
return 0;
}
}
# Exception - Offset + Length must be in the array range
{
my $nums = [(float)5, 2, 3, 1, -10];
eval {
Sort->sort_float($nums, 3, 3, method : int ($a : float, $b : float) {
return $a <=> $b;
});
};
unless ($@) {
return 0;
}
}
$@ = undef;
return 1;
}
static method sort_double : int () {
# Sort array by asc order
{
my $nums = [(double)2, 3, 1];
Sort->sort_double($nums, 0, scalar @$nums, method : int ($a : double, $b : double) {
return $a <=> $b;
});
unless (Array->equals_double($nums, [(double)1, 2, 3])) {
return 0;
}
}
# Sort array by asc order
{
my $nums = [(double)2, 3, 1];
Sort->sort_double_asc($nums, 0, scalar @$nums);
unless (Array->equals_double($nums, [(double)1, 2, 3])) {
return 0;
}
}
# Sort array by asc order more long
{
my $nums = [(double)5, 7, 9, 2, 4, 8, 1, 3, 6, 0];
Sort->sort_double($nums, 0, scalar @$nums, method : int ($a : double, $b : double) {
return $a <=> $b;
});
unless (Array->equals_double($nums, [(double)0, 1, 2, 3, 4, 5, 6, 7, 8, 9])) {
return 0;
}
}
# Sort array by asc order more long with same values
{
my $nums = [(double)5, 7, 9, 2, 5, 4, 8, 1, 3, 6, 3, 0];
Sort->sort_double($nums, 0, scalar @$nums, method : int ($a : double, $b : double) {
return $a <=> $b;
});
unless (Array->equals_double($nums, [(double)0, 1, 2, 3, 3, 4, 5, 5, 6, 7, 8, 9])) {
return 0;
}
}
# Sort double array by desc order
{
my $nums = [(double)2, 3, 1];
Sort->sort_double($nums, 0, scalar @$nums, method : int ($a : double, $b : double) {
return $b <=> $a;
});
unless (Array->equals_double($nums, [(double)3, 2, 1])) {
return 0;
}
}
{
my $nums = [(double)2, 3, 1];
Sort->sort_double_desc($nums, 0, scalar @$nums);
unless (Array->equals_double($nums, [(double)3, 2, 1])) {
return 0;
}
}
# If length is 0, There is nothing to do
{
my $nums = [(double)2, 3, 1];
Sort->sort_double($nums, 0, 0, method : int ($a : double, $b : double) {
return $a <=> $b;
});
unless (Array->equals_double($nums, [(double)2, 3, 1])) {
return 0;
}
}
# Sort partially
{
my $nums = [(double)5, 2, 3, 1, -10];
Sort->sort_double($nums, 1, 3, method : int ($a : double, $b : double) {
return $a <=> $b;
});
unless (Array->equals_double($nums, [(double)5, 1, 2, 3, -10])) {
return 0;
}
}
# Exception - Array must be not undef
{
my $nums = [(double)2, 3, 1];
eval {
Sort->sort_double(undef, 0, scalar @$nums, method : int ($a : double, $b : double) {
return $a <=> $b;
});
};
unless ($@) {
return 0;
}
}
# Exception - Offset must be more than or equals to 0
{
my $nums = [(double)5, 2, 3, 1, -10];
eval {
Sort->sort_double($nums, -1, 3, method : int ($a : double, $b : double) {
return $a <=> $b;
});
};
unless ($@) {
return 0;
}
}
# Exception - Length must be more than or equals to 0
{
my $nums = [(double)5, 2, 3, 1, -10];
eval {
Sort->sort_double($nums, 1, -1, method : int ($a : double, $b : double) {
return $a <=> $b;
});
};
unless ($@) {
return 0;
}
}
# Exception - Offset + Length must be in the array range
{
my $nums = [(double)5, 2, 3, 1, -10];
eval {
Sort->sort_double($nums, 3, 3, method : int ($a : double, $b : double) {
return $a <=> $b;
});
};
unless ($@) {
return 0;
}
}
$@ = undef;
return 1;
}
static method sort_string : int () {
# Sort array by asc order
{
my $strings = [(string)2, 3, 1];
Sort->sort_string($strings, 0, scalar @$strings, method : int ($a : string, $b : string) {
return $a cmp $b;
});
unless (Array->equals_string($strings, [(string)1, 2, 3])) {
return 0;
}
}
{
my $strings = [(string)2, 3, 1];
Sort->sort_string_asc($strings, 0, scalar @$strings);
unless (Array->equals_string($strings, [(string)1, 2, 3])) {
return 0;
}
}
# Sort array by asc order more long
{
my $strings = [(string)5, 7, 9, 2, 4, 8, 1, 3, 6, 0];
Sort->sort_string($strings, 0, scalar @$strings, method : int ($a : string, $b : string) {
return $a cmp $b;
});
unless (Array->equals_string($strings, [(string)0, 1, 2, 3, 4, 5, 6, 7, 8, 9])) {
return 0;
}
}
# Sort array by asc order more long with same values
{
my $strings = [(string)5, 7, 9, 2, 5, 4, 8, 1, 3, 6, 3, 0];
Sort->sort_string($strings, 0, scalar @$strings, method : int ($a : string, $b : string) {
return $a cmp $b;
});
unless (Array->equals_string($strings, [(string)0, 1, 2, 3, 3, 4, 5, 5, 6, 7, 8, 9])) {
return 0;
}
}
# Sort string array by desc order
{
my $strings = [(string)2, 3, 1];
Sort->sort_string($strings, 0, scalar @$strings, method : int ($a : string, $b : string) {
return $b cmp $a;
});
unless (Array->equals_string($strings, [(string)3, 2, 1])) {
return 0;
}
}
{
my $strings = [(string)2, 3, 1];
Sort->sort_string_desc($strings, 0, scalar @$strings);
unless (Array->equals_string($strings, [(string)3, 2, 1])) {
return 0;
}
}
# If length is 0, There is nothing to do
{
my $strings = [(string)2, 3, 1];
Sort->sort_string($strings, 0, 0, method : int ($a : string, $b : string) {
return $a cmp $b;
});
unless (Array->equals_string($strings, [(string)2, 3, 1])) {
return 0;
}
}
# Sort partially
{
my $strings = [(string)5, 2, 3, 1, -10];
Sort->sort_string($strings, 1, 3, method : int ($a : string, $b : string) {
return $a cmp $b;
});
unless (Array->equals_string($strings, [(string)5, 1, 2, 3, -10])) {
return 0;
}
}
# Sort length is differnt and contain empty string and undef
{
my $strings = ["11", "1", "2", undef, ""];
Sort->sort_string($strings, 0, scalar @$strings, method : int ($a : string, $b : string) {
return $a cmp $b;
});
unless (Array->equals_string($strings, [(string)undef, "", "1", "11", "2"])) {
return 0;
}
}
# Exception - Array must be not undef
{
my $strings = [(string)2, 3, 1];
eval {
Sort->sort_string(undef, 0, scalar @$strings, method : int ($a : string, $b : string) {
return $a cmp $b;
});
};
unless ($@) {
return 0;
}
}
# Exception - Offset must be more than or equals to 0
{
my $strings = [(string)5, 2, 3, 1, -10];
eval {
Sort->sort_string($strings, -1, 3, method : int ($a : string, $b : string) {
return $a cmp $b;
});
};
unless ($@) {
return 0;
}
}
# Exception - Length must be more than or equals to 0
{
my $strings = [(string)5, 2, 3, 1, -10];
eval {
Sort->sort_string($strings, 1, -1, method : int ($a : string, $b : string) {
return $a cmp $b;
});
};
unless ($@) {
return 0;
}
}
# Exception - Offset + Length must be in the array range
{
my $strings = [(string)5, 2, 3, 1, -10];
eval {
Sort->sort_string($strings, 3, 3, method : int ($a : string, $b : string) {
return $a cmp $b;
});
};
unless ($@) {
return 0;
}
}
$@ = undef;
return 1;
}
static method new_minimal : TestCase::Minimal ($x : int, $y : int) {
my $minimal = TestCase::Minimal->new;
$minimal->set_x($x);
$minimal->set_y($y);
return $minimal;
}
static method sort_object : int () {
# Sort array by asc order
{
my $objs = [&new_minimal(2, 0), &new_minimal(3, 0), &new_minimal(1, 0)];
Sort->sort_object($objs, 0, scalar @$objs, method : int ($a : object, $b : object) {
return $a->(TestCase::Minimal)->x <=> $b->(TestCase::Minimal)->x;
});
my $is_equals = Array->equals_object($objs, [&new_minimal(1, 0), &new_minimal(2, 0), &new_minimal(3, 0)], method : int ($a : object, $b : object) {
return $a->(TestCase::Minimal)->x == $b->(TestCase::Minimal)->x;
});
unless ($is_equals) {
return 0;
}
}
# Sort array by multiple conditions
{
my $objs = [&new_minimal(2, 1), &new_minimal(2, 2), &new_minimal(1, 3)];
Sort->sort_object($objs, 0, scalar @$objs, method : int ($a : object, $b : object) {
return $a->(TestCase::Minimal)->x <=> $b->(TestCase::Minimal)->x || $a->(TestCase::Minimal)->y <=> $b->(TestCase::Minimal)->y;
});
my $is_equals = Array->equals_object($objs, [&new_minimal(1, 3), &new_minimal(2, 1), &new_minimal(2, 2)], method : int ($a : object, $b : object) {
return $a->(TestCase::Minimal)->x == $b->(TestCase::Minimal)->x && ($a->(TestCase::Minimal)->y == $b->(TestCase::Minimal)->y);
});
unless ($is_equals) {
return 0;
}
}
# Sort array by desc order
{
my $objs = [&new_minimal(2, 0), &new_minimal(3, 0), &new_minimal(1, 0)];
Sort->sort_object($objs, 0, scalar @$objs, method : int ($a : object, $b : object) {
return $b->(TestCase::Minimal)->x <=> $a->(TestCase::Minimal)->x;
});
my $is_equals = Array->equals_object($objs, [&new_minimal(3, 0), &new_minimal(2, 0), &new_minimal(1, 0)], method : int ($a : object, $b : object) {
return $a->(TestCase::Minimal)->x == $b->(TestCase::Minimal)->x;
});
unless ($is_equals) {
return 0;
}
}
# If length is 0, There is nothing to do
{
my $objs = [&new_minimal(2, 0), &new_minimal(3, 0), &new_minimal(1, 0)];
Sort->sort_object($objs, 0, 0, method : int ($a : object, $b : object) {
return $a->(TestCase::Minimal)->x <=> $b->(TestCase::Minimal)->x;
});
my $is_equals = Array->equals_object($objs, [&new_minimal(2, 0), &new_minimal(3, 0), &new_minimal(1, 0)], method : int ($a : object, $b : object) {
return $a->(TestCase::Minimal)->x == $b->(TestCase::Minimal)->x;
});
unless ($is_equals) {
return 0;
}
}
# Sort partially
{
my $objs = [&new_minimal(5, 0), &new_minimal(2, 0), &new_minimal(3, 0), &new_minimal(1, 0), &new_minimal(-10, 0)];
Sort->sort_object($objs, 1, 3, method : int ($a : object, $b : object) {
return $a->(TestCase::Minimal)->x <=> $b->(TestCase::Minimal)->x;
});
my $is_equals = Array->equals_object(
$objs,
[&new_minimal(5, 0), &new_minimal(1, 0), &new_minimal(2, 0), &new_minimal(3, 0), &new_minimal(-10, 0)],
method : int ($a : object, $b : object) {
return $a->(TestCase::Minimal)->x == $b->(TestCase::Minimal)->x;
}
);
unless ($is_equals) {
return 0;
}
}
# Exception - Array must be not undef
{
my $objs = [&new_minimal(2, 0), &new_minimal(3, 0), &new_minimal(1, 0)];
eval {
Sort->sort_object(undef, 0, scalar @$objs, method : int ($a : object, $b : object) {
return $a->(TestCase::Minimal)->x <=> $b->(TestCase::Minimal)->x;
});
};
unless ($@) {
return 0;
}
}
# Exception - Offset must be more than or equals to 0
{
my $objs = [&new_minimal(2, 0), &new_minimal(3, 0), &new_minimal(1, 0)];
eval {
Sort->sort_object($objs, -1, 3, method : int ($a : object, $b : object) {
return $a->(TestCase::Minimal)->x <=> $b->(TestCase::Minimal)->x;
});
};
unless ($@) {
return 0;
}
}
# Exception - Length must be more than or equals to 0
{
my $objs = [&new_minimal(5, 0), &new_minimal(2, 0), &new_minimal(3, 0), &new_minimal(1, 0), &new_minimal(-10, 0)];
eval {
Sort->sort_object($objs, 1, -1, method : int ($a : object, $b : object) {
return $a->(TestCase::Minimal)->x <=> $b->(TestCase::Minimal)->x;
});
};
unless ($@) {
return 0;
}
}
# Exception - Offset + Length must be in the array range
{
my $objs = [&new_minimal(5, 0), &new_minimal(2, 0), &new_minimal(3, 0), &new_minimal(1, 0), &new_minimal(-10, 0)];
eval {
Sort->sort_object($objs, 3, 3, method : int ($a : object, $b : object) {
return $a->(TestCase::Minimal)->x <=> $b->(TestCase::Minimal)->x;
});
};
unless ($@) {
return 0;
}
}
$@ = undef;
return 1;
}
}