package TestCase::Interface {
  use TestCase::Minimal;
  use TestCase::Interface::Interface;
  use TestCase::Interface::ImplementInterface1;
  use TestCase::Interface::ImplementInterface2;
  use SPVM::Comparator;

  sub sort_obj : void ($values : oarray, $comparator : SPVM::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 comparator : int () {
      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;
      
      sort_obj($minimals, $comparator);
      
      if ($minimals->[0]{x} == 1 && $minimals->[1]{x} == 2 && $minimals->[2]{x} == 3) {
        return 1;
      }
      
      return 0;
  }

  sub basic : int () {
    my $object : object = TestCase::Minimal->new;
    my $implement_interface1 : TestCase::Interface::Interface = TestCase::Interface::ImplementInterface1->new;
    my $implement_interface2 : TestCase::Interface::Interface = TestCase::Interface::ImplementInterface2->new;
    
    my $num1 = $implement_interface1->foo(0, 0);
    my $num2 = $implement_interface2->foo(0, 0);

    my $minimal = (TestCase::Minimal)$object;
    $minimal->{x} = 4;
    
    if ($num1 == 1) {
      if ($num2 == 2) {
        if ($minimal->{x} == 4) {
          return 1;
        }
      }
    }
  }
}
