class TestCase::Module::StringList {
use StringList;
use Fn;
use Array;
# Fields
static method fields : int () {
my $list = StringList->new([(string)1, 2, 3]);
# length
my $length = $list->length;
unless ($length == 3) {
return 0;
}
# capacity
unless ($list->capacity == 4) {
return 0;
}
return 1;
}
# Class methods
static method new : int () {
# new with array
{
my $list = StringList->new([(string)"abc", "def", "ABC"]);
unless ($list->get(0) eq "abc") {
return 0;
}
unless ($list->get(1) eq "def") {
return 0;
}
unless ($list->get(2) eq "ABC") {
return 0;
}
my $length = $list->length;
unless ($length == 3) {
return 0;
}
}
# new with undef
{
my $list = StringList->new((string[])undef);
unless ($list->length == 0) {
return 0;
}
unless ($list->capacity == 4) {
return 0;
}
}
{
my $list = StringList->new((string[])undef, 5);
unless ($list->capacity == 5) {
return 0;
}
}
# new without arugments
{
my $list = StringList->new;
unless ($list->length == 0) {
return 0;
}
}
return 1;
}
static method new_len : int () {
{
my $list = StringList->new_len(32);
unless ($list->length == 32) {
return 0;
}
unless ($list->capacity == 32) {
return 0;
}
}
{
my $list = StringList->new_len(0);
unless ($list->length == 0) {
return 0;
}
unless ($list->capacity == 4) {
return 0;
}
}
{
eval { StringList->new_len(-1); };
unless (Fn->contains($@, "The length \$length must be greater than or equal to 0")) {
return 0;
}
}
return 1;
}
# Instance methods
static method get : int () {
{
my $list = StringList->new([(string)"abc", "def", "ABC"]);
unless ($list->get(0) eq "abc") {
return 0;
}
unless ($list->get(0) isa string) {
return 0;
}
unless ($list->get(1) eq "def") {
return 0;
}
unless ($list->get(2) eq "ABC") {
return 0;
}
my $length = $list->length;
unless ($length == 3) {
return 0;
}
eval { $list->get(-1); };
unless (Fn->contains($@, "The index \$index must be greater than or equal to 0")) {
return 0;
}
eval { $list->get(3); };
unless (Fn->contains($@, "The index \$index must be less than the length of \$list")) {
return 0;
}
$@ = undef;
}
return 1;
}
static method insert : int () {
# Insert to first
{
my $list = StringList->new([(string)"1", "2", "3"]);
$list->insert(0 => "4");
unless (Array->equals_string($list->to_array, [(string)"4", "1", "2", "3"])) {
return 0;
}
}
# Insert
{
my $list = StringList->new([(string)"1", "2", "3"]);
$list->insert(2 => "4");
unless (Array->equals_string($list->to_array, [(string)"1", "2", "4", "3"])) {
return 0;
}
}
# Insert to last
{
my $list = StringList->new([(string)"1", "2", "3"]);
$list->insert(3 => "4");
unless (Array->equals_string($list->to_array, [(string)"1", "2", "3", "4"])) {
return 0;
}
}
# Extend
{
my $list = StringList->new([(string)"1", "2", "3"]);
$list->insert(0 => "4");
}
# Exception - insert to -1
{
my $list = StringList->new([(string)1, 2, 3]);
eval { $list->insert(-1 => 2); };
unless (Fn->contains($@, "The index \$index must be greater than or equal to 0")) {
return 0;
}
}
# Exception - insert to length + 1
{
my $list = StringList->new([(string)1, 2, 3]);
eval { $list->insert(4 => 2); };
unless (Fn->contains($@, "The index \$index must be less than or equal to the length of \$list")) {
return 0;
}
}
$@ = undef;
return 1;
}
static method pop : int () {
# pop
{
my $list = StringList->new_len(0);
$list->push("abc");
$list->push("def");
$list->push("ABC");
my $pop0 = $list->pop;
unless ($pop0 eq "ABC") {
return 0;
}
unless ($pop0 isa string) {
return 0;
}
unless ($list->pop eq "def") {
return 0;
}
unless ($list->pop eq "abc") {
return 0;
}
my $length = $list->length;
unless ($length == 0) {
return 0;
}
{
my $list = StringList->new_len(0);
eval { $list->pop; };
unless (Fn->contains($@, "The length of the list \$list must be greater than 0")) {
return 0;
}
}
$@ = undef;
}
return 1;
}
static method push : int () {
# push
{
my $list = StringList->new_len(0);
$list->push("abc");
$list->push("def");
$list->push("ABC");
my $length = $list->length;
unless ($length == 3) {
return 0;
}
unless ($list->get(0) eq "abc" && $list->get(1) eq "def" && $list->get(2) eq "ABC") {
return 0;
}
}
# internal _extend tests
{
my $list = StringList->new_len(0);
for (my $i = 0; $i < 100; $i++) {
$list->push("p");
}
my $length = $list->length;
unless ($length == 100) {
return 0;
}
for (my $i = 0; $i < 100; $i++) {
unless ($list->get($i) eq "p") {
return 0;
}
}
}
return 1;
}
static method remove : int () {
# Remove
{
my $list = StringList->new([(string)"1", "2", "3", "4"]);
my $value = $list->remove(1);
unless (Array->equals_string($list->to_array, [(string)"1", "3", "4"])) {
return 0;
}
unless ($value eq "2") {
return 0;
}
}
# Remove last
{
my $list = StringList->new([(string)"1", "2", "3"]);
$list->remove(2);
unless (Array->equals_string($list->to_array, [(string)"1", "2"])) {
return 0;
}
}
# Remove first
{
my $list = StringList->new([(string)"1", "2", "3"]);
$list->remove(0);
unless (Array->equals_string($list->to_array, [(string)"2", "3"])) {
return 0;
}
}
# Exception - remove to -1
{
my $list = StringList->new([(string)1, 2, 3]);
eval { $list->remove(-1); };
unless (Fn->contains($@, "The index \$index must be greater than or equal to 0")) {
return 0;
}
}
$@ = undef;
# Exception - remove to length
{
my $list = StringList->new([(string)1, 2, 3]);
eval { $list->remove(3); };
unless (Fn->contains($@, "The index \$index must be less than the length of \$list")) {
return 0;
}
}
$@ = undef;
return 1;
}
static method replace : int () {
{
my $list = StringList->new([(string)1, 2, 3, 4]);
$list->replace(1, 2, undef);
unless (Array->equals_string($list->to_array, [(string)1, 4])) {
return 0;
}
}
{
my $list = StringList->new([(string)1, 2, 3, 4]);
$list->replace(1, 2, [(string)20, 30]);
unless (Array->equals_string($list->to_array, [(string)1, 20, 30, 4])) {
return 0;
}
}
{
my $list = StringList->new([(string)1, 2, 3, 4]);
$list->replace(0, 2, [(string)10, 20]);
unless (Array->equals_string($list->to_array, [(string)10, 20, 3, 4])) {
return 0;
}
}
{
my $list = StringList->new([(string)1, 2, 3, 4]);
$list->replace(2, 2, [(string)30, 40]);
unless (Array->equals_string($list->to_array, [(string)1, 2, 30, 40])) {
return 0;
}
}
{
my $list = StringList->new([(string)1, 2, 3, 4]);
$list->replace(4, 0, [(string)50, 60]);
unless (Array->equals_string($list->to_array, [(string)1, 2, 3, 4, 50, 60])) {
return 0;
}
}
{
my $list = StringList->new([(string)1, 2, 3, 4]);
eval { $list->replace(-1, 3, [(string)30, 40]); };
unless (Fn->contains($@, "The offset \$offset must be greater than or equal to 0")) {
return 0;
}
}
{
my $list = StringList->new([(string)1, 2, 3, 4]);
eval { $list->replace(2, 3, [(string)30, 40, 50]); };
unless (Fn->contains($@, "The offset \$offset + the removal length \$remove_length must be less than or equal to the length of this list")) {
return 0;
}
}
{
my $list = StringList->new([(string)1]);
$list->replace(1, 0, [(string)2, 3]);
unless (Array->equals_string($list->to_array, [(string)1, 2, 3])) {
return 0;
}
}
{
my $list = StringList->new([(string)1]);
eval { $list->replace(2, 0, [(string)1, 2]); };
unless (Fn->contains($@, "The offset \$offset + the removal length \$remove_length must be less than or equal to the length of this list")) {
return 0;
}
}
{
my $list = StringList->new([(string)1]);
eval { $list->replace(1, 2, [(string)1, 2]); };
unless (Fn->contains($@, "The offset \$offset + the removal length \$remove_length must be less than or equal to the length of this list")) {
return 0;
}
}
{
my $list = StringList->new([(string)1]);
eval { $list->replace(2, 2, [(string)1, 2]); };
unless (Fn->contains($@, "The offset \$offset + the removal length \$remove_length must be less than or equal to the length of this list")) {
return 0;
}
}
$@ = undef;
return 1;
}
static method splice : int () {
{
my $list = StringList->new([(string)1, 2, 3, 4]);
my $removed = $list->splice(1, 2, [(string)20, 30]);
unless (Array->equals_string($list->to_array, [(string)1, 20, 30, 4])) {
return 0;
}
unless (Array->equals_string($removed, [(string)2, 3])) {
return 0;
}
}
return 1;
}
static method reserve : int () {
{
my $list = StringList->new([(string)1]);
$list->reserve(4);
unless ($list->capacity == 4) {
return 0;
}
$list->reserve(0);
unless ($list->capacity == 4) {
return 0;
}
}
{
my $list = StringList->new([(string)1, 2, 3]);
my $array = $list->to_array;
$list->reserve(5);
unless ($list->capacity == 5) {
return 0;
}
unless (Array->equals_string_address($list->to_array, $array)) {
return 0;
}
}
return 1;
}
static method resize : int () {
my $a = (string)1;
my $b = (string)2;
my $c = (string)3;
# 3 to 3
{
my $list = StringList->new(new string[0]);
$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 = StringList->new(new string[0]);
$list->push($a);
$list->push($b);
$list->push($c);
$list->resize(0);
unless ($list->length == 0) {
return 0;
}
}
# 3 to 4
{
my $list = StringList->new(new string[0]);
$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 = StringList->new(new string[0]);
$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 and 2 to 3 again
{
my $list = StringList->new([(string)1, 2, 3]);
$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 greater than or equal to 0
{
my $list = StringList->new(new string[0]);
$list->push($a);
$list->push($b);
$list->push($c);
eval { $list->resize(-1); };
unless (Fn->contains($@, "The new length \$new_length must be greater than or equal to 0")) {
return 0;
}
}
$@ = undef;
return 1;
}
static method set : int () {
{
my $list = StringList->new([(string)0, 0, 0]);
$list->set(0 => "abc");
$list->set(1 => "def");
$list->set(2 => "ABC");
unless ($list->get(0) eq "abc") {
return 0;
}
unless ($list->get(1) eq "def") {
return 0;
}
unless ($list->get(2) eq "ABC") {
return 0;
}
my $length = $list->length;
unless ($length == 3) {
return 0;
}
eval {
$list->get(-1);
};
unless ($@) {
return 0;
}
eval {
$list->get(3);
};
unless ($@) {
return 0;
}
eval { $list->get(-1); };
unless (Fn->contains($@,"The index \$index must be greater than or equal to 0")) {
return 0;
}
eval { $list->get(3); };
unless (Fn->contains($@, "The index \$index must be less than the length of \$list")) {
return 0;
}
$@ = undef;
}
return 1;
}
static method shift : int () {
# shift
{
my $list = StringList->new_len(0);
$list->unshift("abc");
$list->unshift("def");
$list->unshift("ABC");
unless ($list->shift eq "ABC") {
return 0;
}
unless ($list->shift eq "def") {
return 0;
}
unless ($list->shift eq "abc") {
return 0;
}
my $length = $list->length;
unless ($length == 0) {
return 0;
}
eval { $list->shift; };
unless (Fn->contains($@, "The length of the list \$list must be greater than 0")) {
return 0;
}
$@ = undef;
}
return 1;
}
static method to_array : int () {
{
my $list = StringList->new([(string)"abc", "def", "ABC"]);
my $array = $list->to_array;
unless ($array isa string[]) {
return 0;
}
unless ($array->[0] eq "abc") {
return 0;
}
unless ($array->[1] eq "def") {
return 0;
}
unless ($array->[2] eq "ABC") {
return 0;
}
my $length = @$array;
unless ($length == 3) {
return 0;
}
}
return 1;
}
static method unshift : int () {
# unshift
{
my $list = StringList->new_len(0);
$list->unshift("abc");
$list->unshift("def");
$list->unshift("ABC");
my $length = $list->length;
unless ($length == 3) {
return 0;
}
unless ($list->get(0) eq "ABC") {
return 0;
}
unless ($list->get(1) eq "def") {
return 0;
}
unless ($list->get(2) eq "abc") {
return 0;
}
}
# internal _extend tests
{
my $list = StringList->new_len(0);
for (my $i = 0; $i < 100; $i++) {
$list->unshift("p");
}
my $length = $list->length;
unless ($length == 100) {
return 0;
}
for (my $i = 0; $i < 100; $i++) {
unless ($list->get($i) eq "p") {
return 0;
}
}
}
return 1;
}
static method clone : int () {
{
my $array = [(string)"1", "2"];
my $capacity = 3;
my $list = StringList->new($array, $capacity);
my $clone = $list->clone;
unless ($clone is_type StringList) {
return 0;
}
unless ($clone->length == $list->length) {
return 0;
}
unless ($clone->capacity == $list->capacity) {
return 0;
}
if ($clone == $list) {
return 0;
}
unless (Array->equals($array, $clone->to_array)) {
return 0;
}
}
return 1;
}
static method push_array : int () {
{
my $list = StringList->new_len(0);
$list->push_array([(string)3, 5, 7]);
my $length = $list->length;
unless ($length == 3) {
return 0;
}
unless ($list->get(0) eq "3" && $list->get(1) eq "5" && $list->get(2) eq "7") {
return 0;
}
}
return 1;
}
static method unshift_array : int () {
{
my $list = StringList->new_len(0);
$list->unshift_array([(string)3, 5, 7]);
my $length = $list->length;
unless ($length == 3) {
return 0;
}
unless ($list->get(0) eq "3" && $list->get(1) eq "5" && $list->get(2) eq "7") {
return 0;
}
}
return 1;
}
}