package TestCase::Lib::SPVM::Sort {
  use TestCase::Minimal;
  use SPVM::Sort(sortb, sorts, sorti, sortl, sortf, sortd, sorto, sortstr);
  use SPVM::Util (
    equals_oarray,
    equals_barray,
    equals_sarray,
    equals_iarray,
    equals_larray,
    equals_farray,
    equals_darray,
    equals_strarray,
    INT8_MIN, INT8_MAX, INT16_MIN, INT16_MAX, INT32_MIN, INT32_MAX, INT64_MIN, INT64_MAX, FLT_MIN, FLT_MAX, DBL_MIN, DBL_MAX,
  );
  use SPVM::EqualityChecker::SameObject;
  
  sub test_sortb : int () {
    {
      my $nums = [(byte)5, -7, 9, INT8_MIN(), 127, 15, 19];
      sortb($nums);
      unless (equals_barray($nums, [(byte)INT8_MIN(), -7, 5, 9, 15, 19, 127])) {
        return 0;
      }
    }

    return 1;
  }
  sub test_sorts : int () {
    {
      my $nums = [(short)5, -7, 9, INT16_MIN(), 127, 15, 19];
      sorts($nums);
      unless (equals_sarray($nums, [(short)INT16_MIN(), -7, 5, 9, 15, 19, 127])) {
        return 0;
      }
    }

    return 1;
  }
  sub test_sorti : int () {
    {
      my $nums = [(int)5, -7, 9, INT32_MIN(), 127, 15, 19];
      sorti($nums);
      unless (equals_iarray($nums, [(int)INT32_MIN(), -7, 5, 9, 15, 19, 127])) {
        return 0;
      }
    }
    return 1;
  }
  sub test_sortl : int () {
    {
      my $nums = [(long)5, -7, 9, INT64_MIN(), 127, 15, 19];
      sortl($nums);
      unless (equals_larray($nums, [(long)INT64_MIN(), -7, 5, 9, 15, 19, 127])) {
        return 0;
      }
    }
    
    return 1;
  }
  sub test_sortf : int () {
    {
      my $nums = [(float)5, -7, 9, FLT_MIN(), 127, 15, 19];
      sortf($nums);
      unless (equals_farray($nums, [(float)-7, FLT_MIN(), 5, 9, 15, 19, 127])) {
        return 0;
      }
    }
    
    return 1;
  }
  sub test_sortd : int () {
    {
      my $nums = [(double)5, -7, 9, DBL_MIN(), 127, 15, 19];
      sortd($nums);
      unless (equals_darray($nums, [(double)-7, DBL_MIN(), 5, 9, 15, 19, 127])) {
        return 0;
      }
    }
    return 1;
  }

  sub test_sorto : int () {
    {
      my $m10 = TestCase::Minimal->newp(10, 419);
      my $m11 = TestCase::Minimal->newp(11, 366);
      my $m12 = TestCase::Minimal->newp(12, 402);
      my $m13 = TestCase::Minimal->newp(13, 453);
      my $m14 = TestCase::Minimal->newp(14, 419);
      my $m15 = TestCase::Minimal->newp(15, 402);
      
      my $ms = [$m10, $m11, $m12, $m13, $m14, $m15];
      sorto($ms, sub : int ($self : self, $x1 : object, $x2 : object) {
        my $m1 = (TestCase::Minimal)$x1;
        my $m2 = (TestCase::Minimal)$x2;
        if ($m1->y > $m2->y) {
          return 1;
        }
        elsif ($m1->y < $m2->y) {
          return -1;
        }
        else {
          return 0;
        }
      });
      unless (equals_oarray($ms, [$m11, $m12, $m15, $m10, $m14, $m13], SPVM::EqualityChecker::SameObject->new)) {
        return 0;
      }
    }
    return 1;
  }

  sub test_sortstr : int () {
    my $strs = ["aba", "ab", "abc", "a"];
    my $expected = ["a", "ab", "aba", "abc"];
    sortstr($strs);
    unless (equals_strarray($strs, $expected)) {
      return 0;
    }
    return 1;
  }
}
