class TestCase::List {
  
  has values : object[];
  has length : int;
  has capacity : int;
  
  static method test_list : int () {
    my $list = TestCase::List->new(5);
    $list->add(SPVM::Int->new(1));
    $list->add(SPVM::Int->new(2));
    
    my $onum1 = (SPVM::Int)$list->get(1);
    my $objects = new object[] {SPVM::Int->new(4), SPVM::Int->new(5)};
    $list->add_all($objects);
    
    my $onum2 = (SPVM::Int)$list->get(2);

    my $onums = $list->method_list(2, 3);
    my $onum3 = (SPVM::Int)$onums->[1];
    
    my $ok = 0;
    if ($onum1->value == 2) {
      if ($onum2->value == 4) {
        if ($onum3->value == 5) {
          $ok = 1;
        }
      }
    }
    
    return $ok;
  }
  
  static method new : TestCase::List ($capacity : int) {
    my $self = new TestCase::List;
    
    $self->{capacity} = $capacity;
    
    $self->{values} = new object[$capacity];
    
    return $self;
  }
  
  method add : void ($object : object) {
    my $length = $self->{length};
    my $capacity = $self->{capacity};
    
    if ($length >= $capacity) {
      my $new_capacity = $capacity * 2;
      $self->{values} = new object[$new_capacity];
      $self->{capacity} = $new_capacity;
    }
    
    $self->{values}[$length] = $object;
    
    $self->{length} = $self->{length} + 1;
  }
  
  method add_all : void ($objects : object[]) {
    for (my $i = 0; $i < @$objects; $i++) {
      if ($objects->[$i]) {
        $self->add($objects->[$i]);
      }
      else {
        die "Error";
      }
    }
  }
  
  method get : object ($index : int) {
    return $self->{values}[$index];
  }
  
  method method_list : object[] ($from : int, $to : int) {
    my $length = $to - $from + 1;
    
    my $new_values = new object[$length];
    for (my $i = $from; $i <= $to; $i++) {
      $new_values->[$i - $from] = $self->{values}[$i];
    }
    
    return $new_values;
  }
}