package TestCase::ArrayBigSize {
  use TestCase::Minimal;
  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);
  
  our $BIG_INDEX : int;
  INIT {
    $BIG_INDEX = 200_000_000;
  }
  
  sub array_max_index_byte : int () {
    my $nums = new byte[INT32_MAX()];
    
    $nums->[0] = 1;
    $nums->[INT32_MAX() - 1] = 2;
    
    unless ($nums->[0] == 1) {
      return 0;
    }
    
    unless ($nums->[INT32_MAX() - 1] == 2) {
      return 0;
    }
    
    eval {
      $nums->[INT32_MAX()];
    };
    unless ($@) {
      return 0;
    }
    
    # Additional use memory
    my $nums1 = new byte[INT32_MAX()];
    my $nums2 = new byte[INT32_MAX()];
    my $nums3 = new byte[INT32_MAX()];
    
    return 1;
  }

  sub array_big_index_byte : int () {
    my $index = $BIG_INDEX;
    my $nums = new byte[$index];
    
    $nums->[0] = 1;
    $nums->[$index - 1] = 2;
    
    unless ($nums->[0] == 1) {
      return 0;
    }
    
    unless ($nums->[$index - 1] == 2) {
      return 0;
    }
    
    eval {
      $nums->[$index];
    };
    unless ($@) {
      return 0;
    }
    
    return 1;
  }

  sub array_big_index_short : int () {
    my $index = $BIG_INDEX;
    my $nums = new short[$index];
    
    $nums->[0] = 1;
    $nums->[$index - 1] = 2;
    
    unless ($nums->[0] == 1) {
      return 0;
    }
    
    unless ($nums->[$index - 1] == 2) {
      return 0;
    }
    
    eval {
      $nums->[$index];
    };
    unless ($@) {
      return 0;
    }
    
    return 1;
  }

  sub array_big_index_int : int () {
    my $index = $BIG_INDEX;
    my $nums = new int[$index];
    
    $nums->[0] = 1;
    $nums->[$index - 1] = 2;
    
    unless ($nums->[0] == 1) {
      return 0;
    }
    
    unless ($nums->[$index - 1] == 2) {
      return 0;
    }
    
    eval {
      $nums->[$index];
    };
    unless ($@) {
      return 0;
    }
    
    return 1;
  }

  sub array_big_index_long : int () {
    my $index = $BIG_INDEX;
    my $nums = new long[$index];
    
    $nums->[0] = 1;
    $nums->[$index - 1] = 2;
    
    unless ($nums->[0] == 1) {
      return 0;
    }
    
    unless ($nums->[$index - 1] == 2) {
      return 0;
    }
    
    eval {
      $nums->[$index];
    };
    unless ($@) {
      return 0;
    }
    
    return 1;
  }

  sub array_big_index_float : int () {
    my $index = $BIG_INDEX;
    my $nums = new float[$index];
    
    $nums->[0] = 1;
    $nums->[$index - 1] = 2;
    
    unless ($nums->[0] == 1) {
      return 0;
    }
    
    unless ($nums->[$index - 1] == 2) {
      return 0;
    }
    
    eval {
      $nums->[$index];
    };
    unless ($@) {
      return 0;
    }
    
    return 1;
  }

  sub array_big_index_double : int () {
    my $index = $BIG_INDEX;
    my $nums = new double[$index];
    
    $nums->[0] = 1;
    $nums->[$index - 1] = 2;
    
    unless ($nums->[0] == 1) {
      return 0;
    }
    
    unless ($nums->[$index - 1] == 2) {
      return 0;
    }
    
    eval {
      $nums->[$index];
    };
    unless ($@) {
      return 0;
    }
    
    return 1;
  }

}
