class TestCase::Lib::StringList {
  use StringList;
  use Fn;

  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(undef);
      
      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;
      }
    }
    
    return 1;
  }

  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 ($@) {
        return 0;
      }
      
      eval {
        $list->get(3);
      };
      unless ($@) {
        return 0;
      }
      
      $@ = undef;
    }
    
    return 1;
  }

  static method length : int () {
    # length
    {
      my $list = StringList->new_len(0);
      
      $list->unshift("abc");
      $list->unshift("def");
      $list->unshift("ABC");
      
      my $length = $list->length;
      unless ($length == 3) {
        return 0;
      }
    }
    
    return 1;
  }

  static method insert : int () {
    # Insert to first
    {
      my $list = StringList->new([(string)"1", "2", "3"]);
      $list->insert(0 => "4");
      unless (Fn->equals_array_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 (Fn->equals_array_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 (Fn->equals_array_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
    eval {
      my $list = StringList->new([(string)"1", "2", "3"]);
      $list->insert(-1 => "2");
    };
    unless ($@) {
      return 0;
    }
    $@ = undef;

    # Exception - insert to length + 1
    eval {
      my $list = StringList->new([(string)"1", "2", "3"]);
      $list->insert(4 => "2");
    };
    unless ($@) {
      return 0;
    }
    $@ = undef;
    
    return 1;
  }

  static method remove : int () {
    # Remove
    {
      my $list = StringList->new([(string)"1", "2", "3", "4"]);
      my $value = $list->remove(1);
      unless (Fn->equals_array_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 (Fn->equals_array_string($list->to_array, [(string)"1", "2"])) {
        return 0;
      }
    }

    # Remove first
    {
      my $list = StringList->new([(string)"1", "2", "3"]);
      $list->remove(0);
      unless (Fn->equals_array_string($list->to_array, [(string)"2", "3"])) {
        return 0;
      }
    }

    # Exception - remove to -1
    eval {
      my $list = StringList->new([(string)"1", "2", "3"]);
      $list->remove(-1);
    };
    unless ($@) {
      return 0;
    }
    $@ = undef;

    # Exception - remove to length
    eval {
      my $list = StringList->new([(string)"1", "2", "3"]);
      $list->remove(3);
    };
    unless ($@) {
      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 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;
      }
      
      $@ = undef;
    }
    
    return 1;
  }

  static method set_array : int () {
    # Set array
    {
      my $list = StringList->new([(string)"1", "2", "3"]);
      $list->set_array([(string)3, 4, 5]);
      unless (Fn->equals_array_string($list->to_array, [(string)3, 4, 5])) {
        return 0;
      }
    }
    
    # Exception - Array must be defined
    {
      my $list = StringList->new([(string)"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 = StringList->new([(string)"1", "2", "3"]);
      eval { $list->set_array([(string)1, 2, 3, 4]); };
      unless ($@) {
        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 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;
      }
      
      eval {
        $list->pop;
      };
      unless ($@) {
        return 0;
      }
      $@ = undef;
    }
    
    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 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 more than or equals to 0
    {
      my $list = StringList->new(new string[0]);
      $list->push($a);
      $list->push($b);
      $list->push($c);
      eval { $list->resize(-1); };
      unless ($@) {
        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 ($@) {
        return 0;
      }
      $@ = undef;
    }
    
    return 1;
  }
}