package TestCase {
  use TestCase::EnumA;
  use TestCase::EnumB;
  use TestCase::EnumC;
  use TestCase::EnumD;
  use TestCase::Simple;
  use TestCase::Minimal;
  use TestCase::Destructor;
  use TestCase::Interface1;
  use TestCase::Interface2;
  use TestCase::ImplementInterface1;
  use TestCase::ImplementInterface2;
  use TestCase::Comparator;
  use TestCase::Pointer;
  use TestCase::ComplexCalc;
  use SPVM::Complex_2d;
  use SPVM::Comparator;

  use TestCase::Point_3b;
  use TestCase::Point_3s;
  use TestCase::Point_3i;
  use TestCase::Point_3l;
  use TestCase::Point_3f;
  use TestCase::Point_3d;
  use TestCase::XGetter;

=pod

ABCDE

=cut ppppp

=head1 NAME

SPVM::Test - SPVM test module

=cut

  
  our $PACKAGE_VAR_INT : public int;
  our $PACKAGE_VAR_TEST_CASE : public TestCase;
  
  our $PACKAGE_VAR_RW : public rw int;
  
  our $BEGIN_INIT : int;
  
  BEGIN {
    $BEGIN_INIT = 7;
  }
  
  has next_test : public TestCase;
  has minimal : public TestCase::Minimal;
  has x_byte : public byte;
  has x_short : public short;
  has x_int : public int;
  has x_long : public long;
  has x_float : public float;
  has x_double : public double;
  has private_field : public int;
  
  has x_rw : public rw int;
  
  sub BYTE_MAX : byte () { return (byte)127; }
  sub BYTE_MIN : byte () { return (byte)-128; }
  sub SHORT_MAX : short () { return (short)32767; }
  sub SHORT_MIN : short () { return (short)-32768; }
  sub INT_MAX : int () { return 2147483647; }
  sub INT_MIN : int () { return -2147483648; }
  sub LONG_MAX : long () { return 9223372036854775807L; }
  sub LONG_MIN : long () { return -9223372036854775808L; }
  sub FLOAT_PRECICE : float () { return 32767f; }
  sub DOUBLE_PRECICE : double () { return 2147483647.0; }

  sub main : int ($args : string[]) {
    
    {
      my $x_getter : TestCase::XGetter;
      if (require TestCase::Minimal) {
        my $minimal = TestCase::Minimal->new;
        $minimal->set_x(78);
        $x_getter = $minimal;
      }
      
      my $minimal_def = TestCase::Minimal->new;
      $minimal_def->set_x(99);
      unless ($x_getter) {
        $x_getter = $minimal_def;
      }
      
      print($x_getter->x . "\n");
    }
    
    if (require PPP) {
      PPP->x(1);
    }
    
    {
      print($BEGIN_INIT . "\n");
    }
    {
      TestCase->SET_PACKAGE_VAR_RW(6);
      
      my $package_var_rw = TestCase->PACKAGE_VAR_RW;
      print("$package_var_rw\n");
    }
    
    
    
    
    
    
    
    
    

    {
      my $test_case = new TestCase;
      $test_case->set_x_rw(5);
      my $test_case_x_rw = $test_case->x_rw;
      print("$test_case_x_rw\n");
    }
    
    {
      my $string_length = length "abc";
      print("$string_length\n");
    }

    {
      vaarg(10, 20, 30);
    }
    
    {
      my $var1 = 1;
      my $var2 = 2;
      
      my $string = "a $var1 ppp $var2 qqq";
    }
    
    {
      my $capture1 = 7;
      my $capture2 = 10;
      my $anon_sub = [$capture1 : int, $capture2 : int] sub : int ($self : self, $x1 : object, $x2 : object) {
        print($capture1 . "\n");
        print($capture2 . "\n");

        $self->{capture1} = 5;
        
        print($capture1 . "\n");
        
        return -1;
      };
      
      $anon_sub->(undef, undef);
    }

    {
      my $anon_sub = sub : int ($self : self, $x1 : object, $x2 : object) {
        return -1;
      };
      
      my $ret = $anon_sub->(undef, undef);
      
      print($ret . "\n");
      
      my $comparator : SPVM::Comparator = $anon_sub;
      
      my $ret2 = $comparator->compare(undef, undef);
      
      print($ret . "\n");
    }
    
    {
      my $i : int;
      
      my $p = $i++;
      
      warn($p);
      warn($i);
    }

    {
      my $values_point_byte = new TestCase::Point_3b[1];
      my $values_point_short = new TestCase::Point_3s[1];
      my $values_point_int = new TestCase::Point_3i[1];
      my $values_point_long = new TestCase::Point_3l[1];
      my $values_point_float = new TestCase::Point_3f[1];
      my $values_point_double = new TestCase::Point_3d[1];
      
      $values_point_byte->[0]{x} = (byte)INT8_MIN();
      $values_point_short->[0]{x} = (short)INT16_MIN();
      $values_point_int->[0]{x} = INT32_MIN();
      $values_point_long->[0]{x} = INT64_MIN();
      $values_point_float->[0]{x} = FLT_MIN();
      $values_point_double->[0]{x} = DBL_MIN();

      my $value_byte2 = ++$values_point_byte->[0]{x};
      my $value_short2 = ++$values_point_short->[0]{x};
      my $value_int2 = ++$values_point_int->[0]{x};
      my $value_long2 = ++$values_point_long->[0]{x};
      my $value_float2 = ++$values_point_float->[0]{x};
      my $value_double2 = ++$values_point_double->[0]{x};
    }

    # Increment long
    {
      my $nums = new int[2];
      
      ++$nums->[0];
      
      warn($nums->[0]);
    }

    {
      my $nums = new int[2];
    
      my $var2 = $nums->[0] = $nums->[1] = 1;
    }
    {
      my $nums = new int[1];
      my $var2 = $nums->[0] = 5;
      
      warn($var2);
    }

    {
      my $v1 : int;
      my $v2 : int;
      my $v3 : int;
      
      $v3 = $v2 = $v1 = 543;
      warn($v2);
      warn($v3);
    }

    {
      my $nums = new int[1];
      
      my $var1 = 5;
      my $var2 = $nums->[0] = $var1;
      
      warn($var2);
    }
    {
      my $objects1 = new object[1];
      
      $objects1->[0] = $objects1;
      
      weaken $objects1->[0];
      
    }
    {
      my $num : string = 1;
    }
    
    print($args->[0] . "\n");
    print($args->[1] . "\n");
    print($args->[2] . "\n");
    
    {
      my $point1 : TestCase::Point_3d;
      
      $point1->{x} = DBL_MIN();
      $point1->{y} = 0.5;
      $point1->{z} = 0.125;
      
      my $point1_ref = \$point1;
      
      my $point2 = $$point1_ref;
      
      if ($point2->{x} == DBL_MIN() && $point2->{y} == 0.5 && $point2->{z} == 0.125) {
        1;
      }
    }

    {
      my $z : SPVM::Complex_2d;
      
      $z->{re} = 0.25;
      $z->{im} = 0.5;
      
      my $z_ref = \$z;
      
      my $z_re = $z_ref->{re};
      my $z_im = $z_ref->{im};
      
      print($z_re . " " . $z_im . "\n");
      
      my $out : SPVM::Complex_2d;
      
      ref_sum_complex($z, $z, \$out);
      
      print($out->{re} . " " . $out->{im} . "\n");
    }
    
    {
      my $z1 : SPVM::Complex_2d;
      $z1->{re} = 0.25;
      $z1->{im} = 0.5;
      
      my $z1_ref = \$z1;
      
      my $z2 = $$z1_ref;
    }
    
    {
      my $out : double;
      ref_sum(0.25, 0.5, \$out);
      
      print($out . "\n");
    }
    {
      my $num = 4;
      
      my $num_ref = \$num;
      
      my $num2 = $$num_ref;
      
      $$num_ref = 5;
      
      print($num2 . "\n");
      print($num . "\n");
    }

    {
      my $anon_sub = sub : long ($self : self, $x1 : long, $x2 : long) {
        return $x1 * $x2;
      };
      
      my $ret = $anon_sub->(4L, 5L);
      print($ret . "\n");
    }

    {
      my $array = [1, 2,    5, 7,    9, 3,    4, 10];
      print($array->[7] . "\n");
    }
    {
      my $strings = ["abc", "def", "hij"];
      print($strings->[2] . "\n");
      
    }
    {
      my $nums1 = new SPVM::Complex_2d[10];
      $nums1->[9]{re} = 5;
      $nums1->[9]{im} = 9;
      
      my $nums1_9_re = $nums1->[9]{re};
      my $nums1_9_im = $nums1->[9]{im};
      
      print($nums1->[9]{re} . "\n");
      print($nums1_9_im . "\n");
      
      my $nums2 = $nums1->[9];
      print($nums1->[9]{re} . "\n");
      print($nums1->[9]{im} . "\n");
      
      $nums1->[5] = $nums2;
      print($nums1->[5]{re} . "\n");
      print($nums1->[5]{im} . "\n");
    }
    
    {
      my $z1 : SPVM::Complex_2d;
      $z1->{re} = 0.5;
      $z1->{im} = 0.25;
      
      my $z2 : SPVM::Complex_2d;
      $z2->{re} = 1.0;
      $z2->{im} = 0.5;
      
      my $z3 = TestCase::ComplexCalc->add($z1, $z2);
      
      my $z3_re = $z3->{re};
      my $z3_im = $z3->{im};

      print($z3_re . "\n");
      print($z3_im . "\n");
    }

    {
      my $z : SPVM::Complex_2d;
      $z->{re} = 0.5;
      $z->{im} = 0.25;
      
      my $re = $z->{re};
      my $im = $z->{im};
      
      print($re . "\n");
      print($im . "\n");
      
      my $z2 = $z;
      
      my $z2_re = $z2->{re};
      my $z2_im = $z2->{im};

      print($z2_re . "\n");
      print($z2_im . "\n");
    }
    
    {
      my $total = sum0(sum0(1, 2), sum0(3, 4));
    }
    
    # Multi array init
    {
      my $nums1 = [
        [1, 2, 3],
        [5, 6, 7]
      ];
    }

    {
      my $nums1 = [1, 2, 3];
      my $nums2 = [1.0, 2, 3];
      
      print($nums2->[2] . "\n");
    }
    
    # Pre increment and assign
    {
      my $num1 = 0;
      my $num2 = ++$num1;
      print($num2 . "\n");
    }

    # Pre decrement and assign
    {
      my $num1 = 0;
      my $num2 = --$num1;
      print($num2 . "\n");
    }

    # Post increment and assign
    {
      my $num1 = 0;
      my $num2 = $num1++;
      print($num2 . "\n");
    }

    # Post decrement and assign
    {
      my $num1 = 0;
      my $num2 = $num1--;
      print($num2 . "\n");
    }
    
    # UNDEF
    {
      $PACKAGE_VAR_TEST_CASE = undef;
      $@ = undef;
      {
        my $test_case = new TestCase;
        $test_case->{minimal} = undef;
      }
      {
        my $nums = new TestCase[3];
        $nums->[0] = undef;
      }
      {
        my $minimal = TestCase::Minimal->new;
        $minimal = undef;
      }
      {
        receive_undef(undef);
      }
    }
    
    # Concat
    {
      my $str1 = "a";
      my $str2 = "b";
      $str2 .= $str1;
    }
    
    # Create object in loop
    for (my $i = 0; $i < 3; $i++) {
      my $minimal = TestCase::Minimal->new;
    }

    # Assign same variable
    {
      my $var1 = TestCase::Minimal->new;
      $var1 = $var1;
    }
    
    print(2.23e-9 . "\n");
    
    # sort
    {
      my $comparator  = sub : int ($self : self, $object1 : object, $object2 : object) {
        my $minimal1 = (TestCase::Minimal)$object1;
        my $minimal2 = (TestCase::Minimal)$object2;
        
        my $x1 = $minimal1->{x};
        my $x2 = $minimal2->{x};
        
        if ($x1 > $x2) {
          return 1;
        }
        elsif ($x1 < $x2) {
          return -1;
        }
        else {
          return 0;
        }
      };
      
      my $minimals = new TestCase::Minimal[3];
      $minimals->[0] = TestCase::Minimal->new;
      $minimals->[0]{x} = 3;
      $minimals->[1] = TestCase::Minimal->new;
      $minimals->[1]{x} = 1;
      $minimals->[2] = TestCase::Minimal->new;
      $minimals->[2]{x} = 2;
      
      my $objects = new object[scalar @$minimals];
      for (my $i = 0; $i < @$objects; $i++) { $objects->[$i] = $minimals->[$i]; }
      sort_obj($objects, $comparator);
      for (my $i = 0; $i < @$objects; $i++) { $minimals->[$i] = (TestCase::Minimal)$objects->[$i]; }
      
      print($minimals->[0]{x} . " " . $minimals->[1]{x} . " " . $minimals->[2]{x} . "\n");
    }
    if (!(1 == 2)) {
      
    }
    
    # isa
    {
      my $minimal = TestCase::Minimal->new;
      if ($minimal isa TestCase::Minimal) {
        print("isa OK\n");
      }
      if ($minimal isa TestCase::Simple) {
        print("isa NOT OK\n");
      }
    }
    
    # String comparison operator
    {
      my $string1 = "abc";
      my $string2 = "abc";
      
      if ($string1 eq $string2) {
        print("String is same\n");
      }
    }
    {
      my $string1 = "abc";
      my $string2 = "ab";
      
      if ($string1 ne $string2) {
        print("String is different\n");
      }
    }
    
    my $byte_string = (string)(byte)23;
    my $short_string = (string)(short)23;
    my $int_string = (string)23;
    my $long_string = (string)23L;
    my $float_string = (string)0.25f;
    my $double_string = (string)0.25;
    
    print($byte_string . "\n");
    print($short_string . "\n");
    print($int_string . "\n");
    print($long_string . "\n");
    print($float_string . "\n");
    print($double_string . "\n");
    
    {
      my $object : object = TestCase::Minimal->new;
      my $implement_interface1 : TestCase::Interface1 = TestCase::ImplementInterface1->new;
      my $implement_interface2 : TestCase::Interface1 = TestCase::ImplementInterface2->new;
      
      print($implement_interface1->foo(0, 0) . "\n");
      print($implement_interface2->foo(0, 0) . "\n");
      
      my $minimal = (TestCase::Minimal)$object;
      
      $minimal->{x} = 4;
    }
    
    my $num : byte = 127;
    
    eval {
      exception_croak_return_object();
    };

=pod
    {
      my $NUM = 0;
    }
=cut
    
    {
      my $num = sum0(1, 2);
      print($num . "\n");
    }
    
    {
      $TestCase::PACKAGE_VAR_TEST_CASE = undef;
    }

    {
      my $num = 0xFFFFFFF0;
      my $num2 = ~$num;
    }
    {
      my $values = new int[] {1, 2, 3};
      
      print($values->[0] . "\n");
    }

    {
      my $values : int[];
      $values = new int[] {1, 2, 3};
    }

    {
      my $var = $PACKAGE_VAR_INT;
      
      $PACKAGE_VAR_INT = 1;
      
      if ($PACKAGE_VAR_INT == 0) {
        $PACKAGE_VAR_INT = 1;
        if ($PACKAGE_VAR_INT == 1) {
          return 1;
        }
      }
      my $str = "" . $PACKAGE_VAR_INT;
    }
    
    {
      my $var = $TestCase::PACKAGE_VAR_INT;
      
      $TestCase::PACKAGE_VAR_INT = 1;
      
      if ($TestCase::PACKAGE_VAR_INT == 0) {
        $TestCase::PACKAGE_VAR_INT = 1;
        if ($TestCase::PACKAGE_VAR_INT == 1) {
          return 1;
        }
      }
      my $str = "" . $TestCase::PACKAGE_VAR_INT;
    }
    
    
=pod
    {
      my $simple1 = new TestCase::Simple;
      $simple1->{private_field};
    }
=cut

    {
      my $test_case = new TestCase;
      $test_case->{private_field};
    }
    
    warn("warn");
    warn(1);
    warn(1L);
    warn(0.5f);
    warn(0.5);

    warn("warn");
    warn(1);
    warn(1L);
    warn(0.5f);
    warn(0.5);
    
    # .
    {
      my $foo = "abc" . "def";
    }
    
    {
      my $num = 1;
      
      $num += 4;
      
      print($num . "\n");
    }
    
    {
      TestCase->error_eval_call();
    }
    
    {
      # error_call_stack();
    }
    
    {
      my $ret = error_eval();
      
      print($ret . "\n");
    }
    
    error_eval();
    
    my $dest = TestCase::Destructor->new();
    
    # enum
    {
      TestCase->BYTE_MAX();
      TestCase->BYTE_MIN();
      TestCase->SHORT_MAX();
      TestCase->SHORT_MIN();
      TestCase->INT_MAX();
      TestCase->INT_MIN();
      TestCase->LONG_MAX();
      TestCase->LONG_MIN();
    }
    # Field set and get
    {
      my $test = new TestCase;
      
      $test->{x_byte} = TestCase->BYTE_MAX();
      $test->{x_short} = TestCase->SHORT_MAX();
      $test->{x_int} = TestCase->INT_MAX();
      $test->{x_long} = TestCase->LONG_MAX();
      $test->{x_float} = TestCase->FLOAT_PRECICE();
      $test->{x_double} = TestCase->DOUBLE_PRECICE();
    }
    
    # Weaken
    {
      my $num = TestCase->weaken_reference_count1_object();
    }
    
    # Weaken
    {
      my $test = new TestCase;
      
      $test->{next_test} = $test;
      
      weaken $test->{next_test};
    }
    
    TestCase->call_void();
    
    print("a" . "\n");
    
    # Default return value object
    if (TestCase->default_return_value_object_sub() == undef) {
      if (TestCase->default_return_value_object_sub_empty() == undef) {
        1;
      }
    }

    # Get object from freelist
    {
       TestCase->check_freelist();
       
       my $result = new int[65];
       $result->[64] = 0;
    }
    
    my $ppp = new int[] {
      1,
      2,
      3
    };
    
    # Logical or
    {
      if (1 || 0) {
        print("Logical or" . "\n");
      }
    }

    # Logical and
    {
      if (1 && 1) {
        print("Logical and" . "\n");
      }
    }
    
    # Constant e
    {
      my $num = 0.25E+3;
      print($num . "\n");
    }

    # Constant e
    {
      my $num = 0.25E-3f;
      print($num . "\n");
    }
    # Convert to string to byte[]
    {
      my $string = "abc";
      # my $byte = $string->[0];
    }
    
    # MIN long constant;
    {
      my $num = -9223372036854775808L;
      print($num . "\n");
    }
    {
      my $var = 3;
      while (my $var = 964) {
        print($var . "\n");
        last;
      }
    }
    
    {
      my $error = "First";
      $@ = "Error";
      if (my $error = $@) {
        print($error . "\n");
      }
    }
    
    # Check for third part bug
    {
      for (my $i = 0; $i < 260; $i++) {
      }
    }
    
    # Exception variable
    {
      $@ = "Exception";
      print($@ . "\n");
    }
    
    # $array->[0]{x}
    {
      my $objs = new TestCase::Minimal[3];
      $objs->[0] = TestCase::Minimal->new();
      $objs->[0]{x} = 111;
      print($objs->[0]{x} . "\n");
    }
    
    # $obj->{x}[0]
    {
      my $obj = TestCase::Simple->new();
      $obj->{values1} = new int[5];
      $obj->{values1}[0] = 221;
      print($obj->{values1}[0] . "\n");
    }
    
    # Call new subroutine without variable
    {
      TestCase::Minimal->new();
      sum0(1, 3);
    }
    
    # Create temporary varialbe
    {
      TestCase::Minimal->new();
      TestCase::Minimal->new();
      TestCase::Minimal->new()->{x};
    }
    
    my $obj1 = TestCase::Minimal->new();
    
    # last
    {
      while (1) {
        my $obj1 = TestCase::Minimal->new();
        my $obj2 = TestCase::Minimal->new();
        {
          my $obj3 = TestCase::Minimal->new();
          last;
          next;
        }
      }
    }
    # String escape character
    {
      my $string = "abc\"\'\\\n\t\b\r\fdef";
     
      print($string . "\n");
    }
    
    # Escape sequence
    {
      my $ch1 = '\"';
      my $ch2 = '\'';
      my $ch3 = '\\';
      my $ch4 = '\n';
      my $ch5 = '\t';
      my $ch6 = '\b';
      my $ch7 = '\r';
      my $ch8 = '\f';
    }
    
    # char null string
    {
      my $ch : byte = (byte)0x04;
    }
    
    # char
    {
      my $ch : byte = 'a';
    }
    
    # Malloc mutil array
    {
      my $nums : int[][] = new int[][3];
      $nums->[0] = new int[1];
      $nums->[0][0] = 178;
      print($nums->[0][0] . "\n");
    }
    
    # Declare mutil array
    {
      my $nums : int[][];
    }
    
    # Core functions
    {
      print((byte)1 . "\n");
      print((short)1 . "\n");
      print(1 . "\n");
      print(1L . "\n");
      print(1f . "\n");
      print(1.0 . "\n");
      print("end\n");
    }

    {
      my $nums = new int[258];
      my $len = @$nums;
      for (my $i = 0; $i < $len; $i++) {
        $nums->[$i] = $i;
      }

      for (my $i = 0; $i < @$nums; $i++) {
        $nums->[$i] = $i;
      }
    }
    
    # length with {} Too many elements over 4G
    {
      my $nums = new int[150000000];
      my $len = @{$nums};
      $nums->[149999999] = 764;
      print($nums->[149999999] . "\n");
    }
    
    {
      my $nums = new int[2000000];
      my $len = scalar @$nums;
      my $i = 0;
      for ($i = 0; $i < $len; $i = $i + 1) {
        $nums->[$i] = $i;
      }
      print($i . "\n");
      print($nums->[$i - 1] . "\n");
    }

    {
      my $nums = new int[2000000];
      my $len = scalar @{$nums};
      my $i = 0;
      for ($i = 0; $i < $len; $i = $i + 1) {
        $nums->[$i] = $i;
      }
      print($i . "\n");
      print($nums->[$i - 1] . "\n");
    }
    
    # object to get long field is undef
    #{
    #  my $obj : TestCase::Minimal;
    #  $obj{x} = 1L;
    #}

    # object to get long field is undef
    #{
    #  my $obj : TestCase::Minimal;
    #  $obj{x};
    #}

    # Index is out of range
    #{
    #  my $nums = new int[3];
    #  $nums->[3] = 1;
    #}
    
    # Index is out of range
    #{
    #  my $nums = new int[3];
    #  $nums->[-1] = 3;
    #}

    # Array is undef
    #{
    #  my $nums : int[];
    #  $nums->[0] = 1;
    #}

    # Index is out of range
    #{
    #  my $nums = new int[3];
    #  $nums->[3];
    #}
    
    # Index is out of range
    #{
    #  my $nums = new int[3];
    #  $nums->[-1];
    #}
    
    # Array is undef
    #{
    #  my $nums : int[];
    #  $nums->[0];
    #}
    
    {
      TestCase::Minimal->new();
    }
    
    {
      my $obj1 = TestCase::Minimal->new();
      
      my $obj2 : TestCase::Minimal;
      
      my $obj3 : TestCase::Minimal = undef;
    }
    
    # Increment byte
    {
      my $num = (byte)1;
      $num++;
      print($num . "\n");
    }
    
    # Increment short 
    {
      my $num = (short)1;
      $num++;
      print($num . "\n");
    }
    
    # increment int
    {
      my $var = 4;
      $var++;
      $var--;
      --$var;
      ++$var;
    }
    
    # Increment long
    {
      my $var = (long)4;
      $var++;
      $var--;
      --$var;
      ++$var;
    }
    
    eval {
      sum0(1, 2);
      
      3;
      
      sum0(3, 4);
      
      croak "Catch error";
      
      1;
    };
    
    {
      sum0(5, 6);
      2;
      print("Error" . "\n");
    }
    
    {
      my $string = "ace";
     
      print($string . "\n");
    }

    {
      my $string = "ace";
     
      print($string . "\n");
    }
    
    {
      my $num = (byte)3;
      print($num . "\n");
      
      my $num2 = (long)1 + (long)$num;
      
      print($num2 . "\n");
    }
    
    # get and set field
    {
      my $m = TestCase::Minimal->new();
      
      $m->{x} = 99;
      $m->{y} = 100;
      
      print($m->{x} . "\n");
      print($m->{y} . "\n");
    }

    # Free when assignment
    {
      my $m = TestCase::Minimal->new();
      $m = TestCase::Minimal->new();
    }

    # left is object, right is undef
    {
      my $obj : TestCase::Minimal = undef;
    }
    
    if (1) {
      2;
      if (3) {
        4;
      }
      elsif (8) {
        9;
      }
      else {
        5;
      }
    }
    else {
      6;
    }
    7;
    
    print(sum0(1, 1) . "\n");
    print(sum2(1, 2) . "\n");

    # Constant float
    print(0.3f . "\n");
    print(1f . "\n");
    print(2f . "\n");
    print(1.2f . "\n");
    
    # Constant double
    print(0d . "\n");
    print(1d . "\n");
    print(1.2 . "\n");

    # Constant int
    print(-2147483648 . "\n");
    print(-32769 . "\n");
    print(-32768 . "\n");
    print(-129 . "\n");
    print(-128 . "\n");
    print(-2 . "\n");
    print(-1 . "\n");
    print(0 . "\n");
    print(1 . "\n");
    print(2 . "\n");
    print(3 . "\n");
    print(4 . "\n");
    print(5 . "\n");
    print(6 . "\n");
    print(127 . "\n");
    print(128 . "\n");
    print(255 . "\n");
    print(256 . "\n");
    print(32767 . "\n");
    print(32768 . "\n");
    print(65535 . "\n");
    print(65536 . "\n");
    print(2147483647 . "\n");
    
    # Constant long
    print(-1L . "\n");
    print(0L . "\n");
    print(1L . "\n");
    print(2L . "\n");
    print(9223372036854775807L . "\n");
    print(-9223372036854775807L . "\n");
    print(-2147483648L . "\n");
    print(-32769L . "\n");
    print(-32768L . "\n");
    print(-129L . "\n");
    print(-128L . "\n");
    print(-2L . "\n");
    print(-1L . "\n");
    print(0L . "\n");
    print(1L . "\n");
    print(2L . "\n");
    print(3L . "\n");
    print(4L . "\n");
    print(5L . "\n");
    print(6L . "\n");
    print(127L . "\n");
    print(128L . "\n");
    print(255L . "\n");
    print(256L . "\n");
    print(32767L . "\n");
    print(32768L . "\n");
    print(65535L . "\n");
    print(65536L . "\n");
    print(2147483647L . "\n");
    print(0xFFL . "\n");
    
    "abc";
    
    # Table switch int
    {
      my $num = 3;
      
      switch($num) {
        case TestCase::EnumD->THREE():
          print(1 . "\n");
          last;
        case TestCase::EnumD->FORE():
          print(2 . "\n");
          last;
        case 5:
          print(3 . "\n");
          last;
        default:
          print(5 . "\n");
      }
    }

    # Lookup switch int
    {
      my $num = 3;
      switch ($num) {
        case 1:
          print(1 . "\n");
          last;
        case 3:
          print(2 . "\n");
          last;
        case 10000:
          print(2 . "\n");
          last;
        default:
          print(5 . "\n");
      }
    }
    
    # {
    #   my $num = 5;
    #   switch($num) {
    #     default:
    #       print(5 . "\n");
    #   }
    # }
    
    # my $num;
    # my $num1 = undef;
    
    if (1) {
      3;
      if (2) {
        4;
      }
      5;
    }
    6;

    my $simple3 : TestCase::Simple = TestCase::Simple->new();
    
    print($simple3->{x} . "\n");
    $simple3->{x};
    
    $simple3->{y} = 2;
    $simple3->{x};
    $simple3->{y};

    my $simple2 : TestCase::Simple = TestCase::Simple->new();
    
    if (1) { }
    
    if (1 == 1) {
    
    }

    if (1 != 1) {
    
    }

    if (1 <= 1) {
    
    }

    if (1 < 1) {
    
    }

    if (1 >= 1) {
    
    }

    if (1 > 1) {
    
    }

    if (!1) { }
    
    if (1L) { }
    if (1.5) { }
    if ($simple2) { }
    
    if (undef) {
    
    }
    
    if ($simple2 == undef) {
      
    }

    if (undef == $simple2) {
      
    }
    
    if (undef == undef) {
    
    }
    if (undef != undef) {
    
    }
    
    if (5L || 6L) {
    
    }

    if (5L && 6L) {
    
    }
    if (!1L) {
    
    }
    
    if (1L > 2L) {
      3L;
      4L;
    };
    5L;

    if (1.2 > 2.0) {};
    if (1.2 >= 2.0) {};
    if (1.2 < 2.0) {};
    if (1.2 <= 2.0) {};

    if (1.2 == 1.0) { }
    if (1.2 != 2.0) { };

    if (1 > 2) {};
    if (1 >= 2) {};
    if (1 < 2) {};
    if (1 <= 2) {};

    if (1 == 1) { }
    if (1 != 2) { };

    {
      my $nums = new int[3];
      $nums->[0] = 13;
      $nums->[1] = 14;
      print($nums->[0] . "\n");
      print($nums->[1] . "\n");
    }
    
    {
      my $nums : long[] = new long[3];
      $nums->[0] = 11L;
      $nums->[1] = 12L;
      print($nums->[0] . "\n");
      print($nums->[1] . "\n");
      print(@$nums . "\n");
      my $nums_length : int = @$nums;
    }
    
    my $simple : TestCase::Simple = TestCase::Simple->new();
    
    
    my $v1 : int;
    my $v2 : int;
    my $v3 : int;
    
    $v3 = $v2 = $v1 = 543;
    print($v3 . "\n");
    
    100;
    1000;
    1 << 2;
    1 >> 2;
    1 >>> 2;
    
    TestCase::EnumA->ONE();
    TestCase::EnumA->TWO();
    
    TestCase::EnumA->ONE();
    TestCase::EnumA->ONE();
    
    # Basic operation byte
    {
    
    }
    
    # Basic operation short
    {
    
    }
    
    # Basic operation int
    {
      1 ^ 4;
      1 & 2;
      1 | 2;
      -3 + +2;
      3 - (1 + 2);
      5 + 19;
      1 + 2;
      1 - 2;
      1 * 2;
      1 / 3;
      4 % 6;
    }
    
    # Basic operation long
    {
      1L ^ 4L;
      1L & 2L;
      1L | 2L;
      -3L + +2L;
      3L - (1L + 2L);
      5L + 19L;
      1L + 2L;
      1L - 2L;
      1L * 2L;
      1L / 3L;
      4L % 6L;
    }
    
    1.2 / 3.0;
    1.2f / 3.0f;
    1.2 * 4.0;
    1.2f * 4.0f;
    1.2 + 3.0;
    1.2f + 3.0f;
    1.2 - 3.0;
    1.2f - 3.0f;
    
    # Compare long
    {
      if (1L > 2L) {};
      if (1L >= 2L) {};
      if (1L < 2L) {};
      if (1L <= 2L) {};
      if (1L == 1L) { }
      if (1L != 2L) { };
    }
    
    my $bar : double = (double)1;
    undef;
    
    sum0(1, 2);
    sum0(1, 2);
    
    test1();
    
    while (1) {
      1;
      last;
    }
    
    # for (my $i : int = 0; $i < 5; $i = $i + 1) {
    #   1;
    #   last;
    #   next;
    # }

    {
      my $num0 = (byte)0;
      my $num1 = (byte)1;
      my $num2 = (byte)2;
      my $num3 = (byte)((int)$num0 + (int)$num1 + (int)$num2);
      print($num3 . "\n");
    }

    {
      my $num0 = (short)0;
      my $num1 = (short)1;
      my $num2 = (short)2;
      my $num3 = (short)((int)$num0 + (int)$num1 + (int)$num2);
      print($num3 . "\n");
    }

    # my $error : String = "Error";
    # croak $error;
    
    return 0;
  }

  # Call void function
  sub call_void_sub : void ($nums : int[]) {
    $nums->[0] = 5;
  }
  sub call_void : int () {
    my $nums = new int[] {1};
    
    call_void_sub($nums);
    
    if ($nums->[0] == 5) {
      return 1;
    }
    return 0;
  }
  
  sub test1 : int () {
    my $num0 = 1;
    my $num1 = 2;
    my $num2 = 3;
    my $num3 = 4;
    my $num4 = 5;
    
    return 0;
  }
  
  sub sum4 : long ($num1 : long, $num2 : long) {
    return $num1 + $num2;
  }
  
  sub sum3 : int ($simple : TestCase::Simple, $foo : long, $bar : float) {
    
    if (3) {
    
    }
    
    if (3) {
      1;
    }
    elsif (4) {
      4;
    }
    else {
      
    }

    if (3) {
      1;
    }
    elsif (4) {
      4;
    }
    elsif (5) {
    
    }
    else {
      
    }
    
    if (1) {
      
    }
    else {
      
    }
    
    array_new();
    
    return 2;
  }
  
  sub sum1 : long ($num1 : long, $num2 : long) {
    return $num1 + $num2;
  }
  sub sum0 : int ($num1 : int, $num2 : int) {
    return $num1 + $num2;
  }
  
  sub sum2 : int ($num1 : int, $num2 : int) {
    # croak "Error";
    
    my $num3 = sum0($num1, $num2);
    
    return $num3 + 3;
  }

  sub increfcount : int ($test : TestCase::Minimal, $num : int) {
    
    my $aaa = TestCase::Minimal->new();
  }

  sub decinctest : int ($num1 : TestCase::Minimal, $num2 : int, $num3 : TestCase::Minimal) {
    {
      my $num4 = TestCase::Minimal->new();
      my $num5 = TestCase::Minimal->new();
    }
    return 2;
  }

  sub return_object : TestCase::Minimal () {
    my $obj0 = TestCase::Minimal->new();
    {
      my $obj1 = TestCase::Minimal->new();
      
      my $obj2 : TestCase::Minimal;
      
      my $obj3 : TestCase::Minimal = undef;

      return $obj2;
    }
  }

  sub eval_block : void () {
    my $obj0 = TestCase::Minimal->new();
    
    eval {
      my $obj1 = TestCase::Minimal->new();
      
      my $obj2 : TestCase::Minimal;
      
      my $obj3 : TestCase::Minimal = undef;
      
      my $obj_error1 = "Error1";
      
      croak $obj_error1;
    };
    
    {
      my $obj4 = TestCase::Minimal->new();
      
      my $obj5 : TestCase::Minimal;
      
      my $obj6 : TestCase::Minimal = undef;
      
      my $obj_error2 = "Error2";
      
      croak $obj_error2;
    }
  }
  sub array_new : int () {
    
    my $nums = new int[3];
    
    return @$nums;
  }
  sub check_freelist : int[] () {
    my $result = new int[63];
    my $true_result = new int[1];
    return $true_result;
  }
  sub default_return_value_object_sub : TestCase () {
    1;
  }
  sub default_return_value_object_sub_empty : TestCase () {
    
  }
  sub weaken_reference_count1_object : int (){
    my $minimal = TestCase::Minimal->new();
    my $test = new TestCase;
    
    $test->{minimal} = $minimal;
    $minimal = undef;
    weaken $test->{minimal};
    
    if ($test->{minimal} == undef) {
      return 1;
    }
    
    return 0;
  }
  
  sub error : int () {
    croak "ERROR";
  }
  
  sub error_call_stack : int () {
    error();
  }

  sub error_eval : int () {
    eval {
      croak "ERROR";
    };
    
    if ($@) {
      return 1;
    }
    else {
      return 0;
    }
  }

  sub error_eval_call : int () {
    eval {
      error();
    };
    
    if ($@) {
      return 1;
    }
    else {
      return 0;
    }
  }
  sub exception_croak_return_object : TestCase::Minimal () {
    croak "Error";
  }
  sub new : TestCase () {
    return new TestCase;
  }

  sub call_sub_args_convertion_stab1 : double ($var6 : double) {
    return ($var6);
  }
  
  sub call_sub_args_convertion : int () {
    my $return_value1 = call_sub_args_convertion_stab1(16);
    return 0;
  }

  sub sort_obj : void ($values : object[], $comparator : TestCase::Comparator) {

    my $change_cnt = @$values - 1;
    while( $change_cnt > 0){
      for (my $i = 0; $i < $change_cnt; $i++) {
        my $ret = $comparator->compare($values->[$i], $values->[$i + 1]);
        
        if ($comparator->compare($values->[$i], $values->[$i + 1]) == 1) {
          my $tmp_value = $values->[$i];
          $values->[$i] = $values->[$i + 1];
          $values->[$i + 1] = $tmp_value;
        }
      }
      $change_cnt--;
    }
  }

  sub receive_undef : void ($test_case : TestCase) {
    
  }
  sub ref_sum : double ($x_in1 : double, $x_in2 : double, $x_out : double&) {
    $$x_out = $x_in1 + $x_in2;
  }
  
  sub ref_sum_complex : double ($x_in1 : SPVM::Complex_2d, $x_in2 : SPVM::Complex_2d, $x_out : SPVM::Complex_2d&) {
    $x_out->{re} = $x_in1->{re} + $x_in2->{re};
    $x_out->{im} = $x_in1->{im} + $x_in2->{im};
  }

  sub vaarg : void ($flag : int, $nums : int[]...) {
    print($nums->[0] . "\n");
    print($nums->[1] . "\n");
  }
}

=head1 NAME

SPVM::Test - SPVM test module

