package TestCase::Lib::SPVM::LongList {
  use SPVM::LongList;
  use SPVM::Util (equals_larray);
  use SPVM::Util (INT8_MIN, INT8_MAX, INT16_MIN, INT16_MAX, INT32_MIN, INT32_MAX, INT64_MIN, INT64_MAX, FLT_MIN, FLT_MAX, DBL_MIN, DBL_MAX);

  sub new : int () {
    {
      my $list = SPVM::LongList->new([(long)3, 5, INT64_MAX()]);
      
      unless ($list->get(0) == 3) {
        return 0;
      }
      
      unless ($list->get(1) == 5) {
        return 0;
      }

      unless ($list->get(2) == INT64_MAX()) {
        return 0;
      }
      
      my $length = $list->length;
      unless ($length == 3) {
        return 0;
      }
    }
    
    return 1;
  }

  sub new_capacity : int () {
    {
      my $list = SPVM::LongList->new_capacity(32);
      
      unless ($list->length == 0) {
        return 0;
      }
      
      unless ($list->capacity == 32) {
        return 0;
      }
    }
    
    return 1;
  }

  sub new_len : int () {
    {
      my $list = SPVM::LongList->new_len(32);
      
      unless ($list->length == 32) {
        return 0;
      }
      
      unless ($list->capacity == 32) {
        return 0;
      }
    }
    
    return 1;
  }

  sub insert : int () {
    # Insert to first
    {
      my $list = SPVM::LongList->new([(long)1, 2, 3]);
      $list->insert(0 => 4);
      unless (equals_larray($list->to_array, [(long)4, 1, 2, 3])) {
        return 0;
      }
    }

    # Insert
    {
      my $list = SPVM::LongList->new([(long)1, 2, 3]);
      $list->insert(2 => 4);
      unless (equals_larray($list->to_array, [(long)1, 2, 4, 3])) {
        return 0;
      }
    }

    # Insert to last
    {
      my $list = SPVM::LongList->new([(long)1, 2, 3]);
      $list->insert(3 => 4);
      unless (equals_larray($list->to_array, [(long)1, 2, 3, 4])) {
        return 0;
      }
    }

    # Extend
    {
      my $list = SPVM::LongList->new([(long)1, 2, 3]);
      $list->insert(0 => 4);
    }
    
    # Exception - insert to -1
    eval {
      my $list = SPVM::LongList->new([(long)1, 2, 3]);
      $list->insert(-1 => 2);
    };
    unless ($@) {
      return 0;
    }
    $@ = undef;

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

  sub remove : int () {
    # Remove
    {
      my $list = SPVM::LongList->new([(long)1, 2, 3, 4]);
      my $value = $list->remove(1);
      unless (equals_larray($list->to_array, [(long)1, 3, 4])) {
        return 0;
      }
      unless ($value == 2) {
        return 0;
      }
    }

    # Remove last
    {
      my $list = SPVM::LongList->new([(long)1, 2, 3]);
      $list->remove(2);
      unless (equals_larray($list->to_array, [(long)1, 2])) {
        return 0;
      }
    }

    # Remove first
    {
      my $list = SPVM::LongList->new([(long)1, 2, 3]);
      $list->remove(0);
      unless (equals_larray($list->to_array, [(long)2, 3])) {
        return 0;
      }
    }

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

    # Exception - remove to length
    eval {
      my $list = SPVM::LongList->new([(long)1, 2, 3]);
      $list->remove(3);
    };
    unless ($@) {
      return 0;
    }
    $@ = undef;
    
    return 1;
  }


  sub to_array : int () {
    {
      my $list = SPVM::LongList->new([(long)3, 5, INT64_MAX()]);
      
      my $array = $list->to_array;
      
      unless ($array isa long[]) {
        return 0;
      }
      
      unless ($array->[0] == 3) {
        return 0;
      }

      unless ($array->[1] == 5) {
        return 0;
      }

      unless ($array->[2] == INT64_MAX()) {
        return 0;
      }
      
      my $length = @$array;
      unless ($length == 3) {
        return 0;
      }
    }
    
    return 1;
  }

  sub get : int () {
    {
      my $list = SPVM::LongList->new([(long)3, 5, INT64_MAX()]);
      
      unless ($list->get(0) == 3) {
        return 0;
      }

      unless ($list->get(0) isa long) {
        return 0;
      }

      unless ($list->get(1) == 5) {
        return 0;
      }

      unless ($list->get(2) == INT64_MAX()) {
        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;
  }

  sub set : int () {
    {
      my $list = SPVM::LongList->new([(long)0, 0, 0]);
      
      $list->set(0 => 3);
      $list->set(1 => 5);
      $list->set(2 => INT64_MAX());
      
      unless ($list->get(0) == 3) {
        return 0;
      }

      unless ($list->get(1) == 5) {
        return 0;
      }

      unless ($list->get(2) == INT64_MAX()) {
        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;
  }

  sub push : int () {
    # push
    {
      my $list = SPVM::LongList->new_len(0);
      
      $list->push(3);
      $list->push(5);
      $list->push(INT64_MAX());
      
      my $length = $list->length;
      unless ($length == 3) {
        return 0;
      }
      
      unless ($list->get(0) == 3 && $list->get(1) == 5 && $list->get(2) == INT64_MAX()) {
        return 0;
      }
    }
    
    # internal _extend tests
    {
      my $list = SPVM::LongList->new_len(0);
      
      for (my $i = 0; $i < 100; $i++) {
        $list->push(1);
      }
      
      my $length = $list->length;
      unless ($length == 100) {
        return 0;
      }
      
      for (my $i = 0; $i < 100; $i++) {
        unless ($list->get($i) == 1) {
          return 0;
        }
      }
    }
    
    
    return 1;
  }

  sub pop : int () {
    # pop
    {
      my $list = SPVM::LongList->new_len(0);
      
      $list->push(3);
      $list->push(5);
      $list->push(INT64_MAX());
      
      my $pop0 = $list->pop;
      unless ($pop0 == INT64_MAX()) {
        return 0;
      }
      unless ($pop0 isa long) {
        return 0;
      }

      unless ($list->pop == 5) {
        return 0;
      }

      unless ($list->pop == 3) {
        return 0;
      }
      
      my $length = $list->length;
      unless ($length == 0) {
        return 0;
      }
      
      eval {
        $list->pop;
      };
      unless ($@) {
        return 0;
      }
      $@ = undef;
    }
    
    return 1;
  }

  sub unshift : int () {
    # unshift
    {
      my $list = SPVM::LongList->new_len(0);
      
      $list->unshift(3);
      $list->unshift(5);
      $list->unshift(INT64_MAX());
      
      my $length = $list->length;
      unless ($length == 3) {
        return 0;
      }
      
      unless ($list->get(0) == INT64_MAX() && $list->get(1) == 5 && $list->get(2) == 3) {
        return 0;
      }
    }
    
    # internal _extend tests
    {
      my $list = SPVM::LongList->new_len(0);
      
      for (my $i = 0; $i < 100; $i++) {
        $list->unshift(1);
      }
      
      my $length = $list->length;
      unless ($length == 100) {
        return 0;
      }
      
      for (my $i = 0; $i < 100; $i++) {
        unless ($list->get($i) == 1) {
          return 0;
        }
      }
    }
    
    return 1;
  }

  sub shift : int () {
    # shift
    {
      my $list = SPVM::LongList->new_len(0);
      
      $list->unshift(3);
      $list->unshift(5);
      $list->unshift(INT64_MAX());
      
      unless ($list->shift == INT64_MAX()) {
        return 0;
      }

      unless ($list->shift == 5) {
        return 0;
      }

      unless ($list->shift == 3) {
        return 0;
      }
      
      my $length = $list->length;
      unless ($length == 0) {
        return 0;
      }
      
      eval {
        $list->shift;
      };
      unless ($@) {
        return 0;
      }
      $@ = undef;
    }
    
    return 1;
  }

  sub length : int () {
    # length
    {
      my $list = SPVM::LongList->new_len(0);
      
      $list->unshift(3);
      $list->unshift(5);
      $list->unshift(INT64_MAX());
      
      my $length = $list->length;
      unless ($length == 3) {
        return 0;
      }
    }
    
    return 1;
  }

}
