class TestCase::Lib::List {
use Int;
use List;
use Fn;
use EqualityChecker::SameObject;
static method new : int () {
# new with undef
{
my $list = List->new(undef);
unless ($list->length == 0) {
return 0;
}
$list->push(Int->new(1));
unless ((int)$list->get(0) == 1) {
return 0;
}
}
return 1;
}
static method insert : int () {
# Insert to first
{
my $x1 = "1";
my $x2 = "2";
my $x3 = "3";
my $x4 = "4";
my $list = List->new([(object)$x1, $x2, $x3]);
$list->insert(0 => $x4);
unless (Fn->equals_array_object($list->to_array, [$x4, $x1, $x2, $x3], EqualityChecker::SameObject->new)) {
return 0;
}
}
# Insert
{
my $x1 = "1";
my $x2 = "2";
my $x3 = "3";
my $x4 = "4";
my $list = List->new([(object)$x1, $x2, $x3]);
$list->insert(2 => $x4);
unless (Fn->equals_array_object($list->to_array, [$x1, $x2, $x4, $x3], EqualityChecker::SameObject->new)) {
return 0;
}
}
# Insert to last
{
my $x1 = "1";
my $x2 = "2";
my $x3 = "3";
my $x4 = "4";
my $list = List->new([(object)$x1, $x2, $x3]);
$list->insert(3 => $x4);
unless (Fn->equals_array_object($list->to_array, [$x1, $x2, $x3, $x4], EqualityChecker::SameObject->new)) {
return 0;
}
}
# Extend
{
my $x1 = "1";
my $x2 = "2";
my $x3 = "3";
my $x4 = "4";
my $list = List->new([(object)$x1, $x2, $x3]);
$list->insert(0 => $x4);
}
# Exception - insert to -1
eval {
my $list = List->new([(object)"1", "2", "3"]);
$list->insert(-1 => "2");
};
unless ($@) {
return 0;
}
$@ = undef;
# Exception - insert to length + 1
eval {
my $list = List->new([(object)"1", "2", "3"]);
$list->insert(4 => "2");
};
unless ($@) {
return 0;
}
$@ = undef;
return 1;
}
static method remove : int () {
# Remove
{
my $x1 = "1";
my $x2 = "2";
my $x3 = "3";
my $x4 = "4";
my $list = List->new([(object)$x1, $x2, $x3, $x4]);
my $value = $list->remove(1);
unless (Fn->equals_array_object($list->to_array, [$x1, $x3, $x4], EqualityChecker::SameObject->new)) {
return 0;
}
unless ($value == $x2) {
return 0;
}
}
# Remove last
{
my $x1 = "1";
my $x2 = "2";
my $x3 = "3";
my $x4 = "4";
my $list = List->new([(object)$x1, $x2, $x3]);
$list->remove(2);
unless (Fn->equals_array_object($list->to_array, [$x1, $x2], EqualityChecker::SameObject->new)) {
return 0;
}
}
# Remove first
{
my $x1 = "1";
my $x2 = "2";
my $x3 = "3";
my $x4 = "4";
my $list = List->new([(object)$x1, $x2, $x3]);
$list->remove(0);
unless (Fn->equals_array_object($list->to_array, [$x2, $x3], EqualityChecker::SameObject->new)) {
return 0;
}
}
# Exception - remove to -1
eval {
my $list = List->new([(object)"1", "2", "3"]);
$list->remove(-1);
};
unless ($@) {
return 0;
}
$@ = undef;
# Exception - remove to length
eval {
my $list = List->new([(object)"1", "2", "3"]);
$list->remove(3);
};
unless ($@) {
return 0;
}
$@ = undef;
return 1;
}
static method equals_list_deeply : int ($got : List, $expected : List) {
my $all_match = 1;
for (my $i = 0; $i < $expected->length; $i++) {
unless ($got->get($i) == $expected->get($i)) {
$all_match = 0;
}
}
if ($all_match) {
return 1;
}
else {
return 0;
}
}
static method equals_array : int ($got : object[], $expected : object[]) {
if (@$got != @$expected) {
warn("Array size mismatch. got: " . @$got . ", expected: " . @$expected);
return 0;
}
for (my $i = 0; $i < @$expected; $i++) {
if ($got->[$i] != $expected->[$i]) {
warn("Array content mismatch. methodscript " . $i);
return 0;
}
}
return 1;
}
static method length : int () {
if (List->new_len([], 0)->length != 0) {
return 0;
}
if (List->new([Int->new(1)])->length != 1) {
return 0;
}
return 1;
}
static method push : int () {
my $list = List->new_len(new Int[0], 0);
my $v1 = Int->new(1);
$list->push($v1);
unless (TestCase::Lib::List->equals_list_deeply($list, List->new([$v1]))) {
return 0;
}
my $v2 = Int->new(2);
$list->push($v2);
unless (TestCase::Lib::List->equals_list_deeply($list, List->new([$v1, $v2]))) {
return 0;
}
my $v3 = Int->new(3);
$list->push($v3);
unless (TestCase::Lib::List->equals_list_deeply($list, List->new([$v1, $v2, $v3]))) {
return 0;
}
# no reallocation
my $v4 = Int->new(3);
$list->push($v4);
unless (TestCase::Lib::List->equals_list_deeply($list, List->new([$v1, $v2, $v3, $v4]))) {
return 0;
}
return 1;
}
static method pop : int () {
my $list = List->new([Int->new(1), undef, Int->new(2)]);
unless (((Int)$list->pop)->value == 2) {
return 0;
}
unless ($list->pop == undef) {
return 0;
}
unless (((Int)$list->pop)->value == 1) {
return 0;
}
unless (TestCase::Lib::List->equals_list_deeply($list, List->new_len([], 0))) {
return 0;
}
eval {
$list->pop;
};
unless ($@) {
return 0;
}
$@ = undef;
return 1;
}
static method unshift : int () {
my $list = List->new_len(new Int[0], 1);
my $v1 = Int->new(1);
$list->unshift($v1);
my $v2 = Int->new(2);
$list->unshift($v2);
my $v3 = Int->new(3);
$list->unshift($v3);
my $v4 = Int->new(4);
$list->unshift($v4);
my $v5 = Int->new(5);
$list->unshift($v5);
unless (TestCase::Lib::List->equals_list_deeply($list, List->new([$v5, $v4, $v3, $v2, $v1]))) {
return 0;
}
return 1;
}
static method shift : int () {
my $list = List->new([Int->new(1), undef, Int->new(2)]);
unless (((Int)$list->shift)->value == 1) {
return 0;
}
unless ($list->shift == undef) {
return 0;
}
unless (((Int)$list->shift)->value == 2) {
return 0;
}
eval {
$list->shift;
};
unless ($@) {
return 0;
}
$@ = undef;
unless (TestCase::Lib::List->equals_list_deeply($list, List->new(new object[0]))) {
return 0;
}
return 1;
}
static method offset_by_alternate_push_and_shift : int () {
my $list = List->new_len(new Int[0], 4);
for (my $i = 0; $i < 16; $i++) {
$list->push(1);
$list->shift;
}
return 1;
}
static method offset_by_alternate_unshift_and_pop : int () {
my $list = List->new_len(new Int[0], 4);
for (my $i = 0; $i < 16; $i++) {
$list->unshift(1);
$list->pop;
}
return 1;
}
static method set : int () {
my $list = List->new([Int->new(1), undef]);
my $v1 = Int->new(2);
$list->set(0, undef);
$list->set(1, $v1);
my $expected = new Int[2];
$expected->[1] = $v1;
unless (TestCase::Lib::List->equals_list_deeply($list, List->new($expected))) {
return 0;
}
eval { $list->set(0, Long->new(1)); };
unless ($@) {
return 0;
}
$@ = undef;
return 1;
}
static method set_array : int () {
# Set array
{
my $list = List->new(["1", "2", "3"]);
$list->set_array(["3", "4", "5"]);
unless (Fn->equals_array_string((string[])$list->to_array, ["3", "4", "5"])) {
return 0;
}
}
# Exception - Array must be defined
{
my $list = List->new(["1", "2", "3"]);
eval { $list->set_array(undef); };
unless ($@) {
return 0;
}
}
# Exception - The length of argument array must be same as the length of current list array
{
my $list = List->new(["1", "2", "3"]);
eval { $list->set_array(["1", "2", "3", "4"]); };
unless ($@) {
return 0;
}
}
$@ = undef;
return 1;
}
static method get : int () {
my $list = List->new([Int->new(1), undef]);
unless (((Int)$list->get(0))->value == 1) {
return 0;
}
unless ($list->get(1) == undef) {
return 0;
}
return 1;
}
static method to_array : int () {
{
my $list = List->new([(object)"abc", 1, 3.14]);
my $objects = $list->to_array;
unless ($objects isa object[]) {
return 0;
}
unless ((string)($objects->[0]) eq "abc" &&
((Int)$objects->[1])->value == 1 &&
((Double)$objects->[2])->value == 3.14) {
return 0;
}
}
{
my $list = List->new([Int->new(1), Int->new(2)]);
my $objects = (Int[])$list->to_array;
unless ($objects isa Int[]) {
return 0;
}
unless ($objects->[0]->value == 1 && $objects->[1]->value == 2) {
return 0;
}
eval { (Long[])$list->to_array; };
unless ($@) {
return 0;
}
}
$@ = undef;
return 1;
}
static method resize : int () {
my $a = "a";
my $b = "b";
my $c = "c";
# 3 to 3
{
my $list = List->new([]);
$list->push($a);
$list->push($b);
$list->push($c);
$list->resize(3);
unless ($list->length == 3) {
return 0;
}
unless ($list->get(0) == $a && $list->get(1) == $b && $list->get(2) == $c) {
return 0;
}
}
# 3 to 0
{
my $list = List->new([]);
$list->push($a);
$list->push($b);
$list->push($c);
$list->resize(0);
unless ($list->length == 0) {
return 0;
}
}
# 3 to 4
{
my $list = List->new([]);
$list->push($a);
$list->push($b);
$list->push($c);
$list->resize(4);
unless ($list->length == 4) {
return 0;
}
unless ($list->get(3) == undef && $list->get(0) == $a && $list->get(1) == $b && $list->get(2) == $c) {
return 0;
}
}
# 3 to 32(over capacity)
{
my $list = List->new([]);
$list->push($a);
$list->push($b);
$list->push($c);
$list->resize(32);
unless ($list->length == 32) {
return 0;
}
unless ($list->get(3) == undef && $list->get(31) == undef) {
return 0;
}
}
# 3 to 2, 2 to 3 again
{
my $list = List->new([]);
$list->push($a);
$list->push($b);
$list->push($c);
$list->resize(2);
unless ($list->length == 2) {
return 0;
}
$list->resize(3);
unless ($list->length == 3) {
return 0;
}
unless ($list->get(2) == undef) {
return 0;
}
}
# Exception - New length must be more than or equals to 0
{
my $list = List->new([]);
$list->push($a);
$list->push($b);
$list->push($c);
eval { $list->resize(-1); };
unless ($@) {
return 0;
}
}
$@ = undef;
return 1;
}
}