From aec2c8e4e0ad80d74bb3e6a537323017ade51562 Mon Sep 17 00:00:00 2001 From: feijermu Date: Mon, 2 Mar 2020 08:51:50 +0100 Subject: [PATCH] Add test cases for MathUtil. (#10071) Motivation: MathUtil needs more test cases. Modification: Add several test cases for MathUtil. Result: Improve test coverage slightly. --- .../io/netty/util/internal/MathUtilTest.java | 72 +++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 common/src/test/java/io/netty/util/internal/MathUtilTest.java diff --git a/common/src/test/java/io/netty/util/internal/MathUtilTest.java b/common/src/test/java/io/netty/util/internal/MathUtilTest.java new file mode 100644 index 0000000000..1b5764c9f5 --- /dev/null +++ b/common/src/test/java/io/netty/util/internal/MathUtilTest.java @@ -0,0 +1,72 @@ +/* + * Copyright 2020 The Netty Project + * + * The Netty Project licenses this file to you under the Apache License, + * version 2.0 (the "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ +package io.netty.util.internal; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static io.netty.util.internal.MathUtil.*; + +import org.junit.Test; + +public class MathUtilTest { + + @Test + public void testFindNextPositivePowerOfTwo() { + assertEquals(1, findNextPositivePowerOfTwo(0)); + assertEquals(1, findNextPositivePowerOfTwo(1)); + assertEquals(1024, findNextPositivePowerOfTwo(1000)); + assertEquals(1024, findNextPositivePowerOfTwo(1023)); + assertEquals(2048, findNextPositivePowerOfTwo(2048)); + assertEquals(1 << 30, findNextPositivePowerOfTwo((1 << 30) - 1)); + assertEquals(1, findNextPositivePowerOfTwo(-1)); + assertEquals(1, findNextPositivePowerOfTwo(-10000)); + } + + @Test + public void testSafeFindNextPositivePowerOfTwo() { + assertEquals(1, safeFindNextPositivePowerOfTwo(0)); + assertEquals(1, safeFindNextPositivePowerOfTwo(1)); + assertEquals(1024, safeFindNextPositivePowerOfTwo(1000)); + assertEquals(1024, safeFindNextPositivePowerOfTwo(1023)); + assertEquals(2048, safeFindNextPositivePowerOfTwo(2048)); + assertEquals(1 << 30, safeFindNextPositivePowerOfTwo((1 << 30) - 1)); + assertEquals(1, safeFindNextPositivePowerOfTwo(-1)); + assertEquals(1, safeFindNextPositivePowerOfTwo(-10000)); + assertEquals(1 << 30, safeFindNextPositivePowerOfTwo(Integer.MAX_VALUE)); + assertEquals(1 << 30, safeFindNextPositivePowerOfTwo((1 << 30) + 1)); + assertEquals(1, safeFindNextPositivePowerOfTwo(Integer.MIN_VALUE)); + assertEquals(1, safeFindNextPositivePowerOfTwo(Integer.MIN_VALUE + 1)); + } + + @Test + public void testIsOutOfBounds() { + assertFalse(isOutOfBounds(0, 0, 0)); + assertFalse(isOutOfBounds(0, 0, 1)); + assertFalse(isOutOfBounds(0, 1, 1)); + assertTrue(isOutOfBounds(1, 1, 1)); + assertTrue(isOutOfBounds(Integer.MAX_VALUE, 1, 1)); + assertTrue(isOutOfBounds(Integer.MAX_VALUE, Integer.MAX_VALUE, 1)); + assertTrue(isOutOfBounds(Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE)); + assertFalse(isOutOfBounds(0, Integer.MAX_VALUE, Integer.MAX_VALUE)); + assertFalse(isOutOfBounds(0, Integer.MAX_VALUE - 1, Integer.MAX_VALUE)); + assertTrue(isOutOfBounds(0, Integer.MAX_VALUE, Integer.MAX_VALUE - 1)); + assertFalse(isOutOfBounds(Integer.MAX_VALUE - 1, 1, Integer.MAX_VALUE)); + assertTrue(isOutOfBounds(Integer.MAX_VALUE - 1, 1, Integer.MAX_VALUE - 1)); + assertTrue(isOutOfBounds(Integer.MAX_VALUE - 1, 2, Integer.MAX_VALUE)); + assertTrue(isOutOfBounds(1, Integer.MAX_VALUE, Integer.MAX_VALUE)); + } +}