提交 b7d25de6 authored 作者: Thomas Mueller's avatar Thomas Mueller

Issue 439: Utils.sortTopN does not handle single-element arrays.

上级 ecaf3d67
......@@ -66,6 +66,7 @@ Change Log
</li><li>MySQL compatibility: support for ALTER TABLE tableName MODIFY [COLUMN] columnName columnDef. Patch from Ville Koskela.
</li><li>Issue 404: SHOW COLUMNS FROM tableName does not work with ALLOW_LITERALS=NUMBERS.
</li><li>Throw an explicit error to make it clear we don't support the TRIGGER combination of SELECT and FOR EACH ROW.
</li><li>Issue 439: Utils.sortTopN does not handle single-element arrays.
</li></ul>
<h2>Version 1.3.170 (2012-11-30)</h2>
......
......@@ -469,6 +469,9 @@ public class Utils {
if (low > end || high < start || (low > start && high < end)) {
return;
}
if (low == high) {
return;
}
int i = low, j = high;
// use a random pivot to protect against
// the worst case order
......
......@@ -16,6 +16,7 @@ import java.util.Date;
import java.util.Random;
import org.h2.test.TestBase;
import org.h2.util.Utils;
import org.junit.Test;
/**
* Tests reflection utilities.
......@@ -36,13 +37,15 @@ public class TestUtils extends TestBase {
TestBase.createCaller().init().test();
}
@Test
public void test() throws Exception {
testSortTopN();
testSortTopNRandom();
testWriteReadLong();
testGetNonPrimitiveClass();
testGetNonPrimitiveClass();
testGetNonPrimitiveClass();
testReflectionUtils();
testSortTopN();
}
private void testWriteReadLong() {
......@@ -61,8 +64,27 @@ public class TestUtils extends TestBase {
assertEquals(x, y);
}
}
private void testSortTopN() {
Comparator<Integer> comp = new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return o1.compareTo(o2);
}
};
Integer[] arr = new Integer[] {};
Utils.sortTopN(arr, 0, 5, comp);
arr = new Integer[] { 1 };
Utils.sortTopN(arr, 0, 5, comp);
arr = new Integer[] { 3, 5, 1, 4, 2 };
Utils.sortTopN(arr, 0, 2, comp);
assertEquals(arr[0].intValue(), 1);
assertEquals(arr[1].intValue(), 2);
}
private void testSortTopNRandom() {
Random rnd = new Random();
Comparator<Integer> comp = new Comparator<Integer>() {
@Override
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论