提交 5d5c337d authored 作者: LaughingMan's avatar LaughingMan

Add basic tests for MathUtils.nextPowerOf2

上级 48991fe5
...@@ -25,6 +25,8 @@ public class TestMathUtils extends TestBase { ...@@ -25,6 +25,8 @@ public class TestMathUtils extends TestBase {
@Override @Override
public void test() { public void test() {
testRandom(); testRandom();
testNextPowerOf2Int();
testNextPowerOf2Long();
} }
private void testRandom() { private void testRandom() {
...@@ -48,4 +50,30 @@ public class TestMathUtils extends TestBase { ...@@ -48,4 +50,30 @@ public class TestMathUtils extends TestBase {
assertTrue(data.length > 10); assertTrue(data.length > 10);
} }
private void testNextPowerOf2Int() {
// the largest power of two that fits into an integer
final int LARGEST_POW2 = 0x40000000;
int[] testValues = { 0, 1, 2, 3, 4, 12, 17, 500, 1023,
LARGEST_POW2-500, LARGEST_POW2 };
int[] resultValues = { 1, 1, 2, 4, 4, 16, 32, 512, 1024,
LARGEST_POW2, LARGEST_POW2 };
for (int i = 0; i < testValues.length; i++) {
assertEquals(resultValues[i], MathUtils.nextPowerOf2(testValues[i]));
}
}
private void testNextPowerOf2Long() {
// the largest power of two that fits into a long
final long LARGEST_POW2 = 0x4000000000000000L;
long[] testValues = { 0, 1, 2, 3, 4, 12, 17, 500, 1023,
LARGEST_POW2-500, LARGEST_POW2 };
long[] resultValues = { 1, 1, 2, 4, 4, 16, 32, 512, 1024,
LARGEST_POW2, LARGEST_POW2 };
for (int i = 0; i < testValues.length; i++) {
assertEquals(resultValues[i], MathUtils.nextPowerOf2(testValues[i]));
}
}
} }
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论