提交 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 ...@@ -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>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>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>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> </li></ul>
<h2>Version 1.3.170 (2012-11-30)</h2> <h2>Version 1.3.170 (2012-11-30)</h2>
......
...@@ -469,6 +469,9 @@ public class Utils { ...@@ -469,6 +469,9 @@ public class Utils {
if (low > end || high < start || (low > start && high < end)) { if (low > end || high < start || (low > start && high < end)) {
return; return;
} }
if (low == high) {
return;
}
int i = low, j = high; int i = low, j = high;
// use a random pivot to protect against // use a random pivot to protect against
// the worst case order // the worst case order
......
...@@ -16,6 +16,7 @@ import java.util.Date; ...@@ -16,6 +16,7 @@ import java.util.Date;
import java.util.Random; import java.util.Random;
import org.h2.test.TestBase; import org.h2.test.TestBase;
import org.h2.util.Utils; import org.h2.util.Utils;
import org.junit.Test;
/** /**
* Tests reflection utilities. * Tests reflection utilities.
...@@ -36,13 +37,15 @@ public class TestUtils extends TestBase { ...@@ -36,13 +37,15 @@ public class TestUtils extends TestBase {
TestBase.createCaller().init().test(); TestBase.createCaller().init().test();
} }
@Test
public void test() throws Exception { public void test() throws Exception {
testSortTopN();
testSortTopNRandom();
testWriteReadLong(); testWriteReadLong();
testGetNonPrimitiveClass(); testGetNonPrimitiveClass();
testGetNonPrimitiveClass(); testGetNonPrimitiveClass();
testGetNonPrimitiveClass(); testGetNonPrimitiveClass();
testReflectionUtils(); testReflectionUtils();
testSortTopN();
} }
private void testWriteReadLong() { private void testWriteReadLong() {
...@@ -63,6 +66,25 @@ public class TestUtils extends TestBase { ...@@ -63,6 +66,25 @@ public class TestUtils extends TestBase {
} }
private void testSortTopN() { 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(); Random rnd = new Random();
Comparator<Integer> comp = new Comparator<Integer>() { Comparator<Integer> comp = new Comparator<Integer>() {
@Override @Override
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论