Fixed random fit test of ImmutableByteSequence
Change-Id: I22c0279f06dfb0101ee0c871b6c0c5443d3bd4a9
(cherry picked from commit d047bd2a99d3bbe64f1505cdb07a1c3a52d018a7)
diff --git a/utils/misc/src/test/java/org/onlab/util/ImmutableByteSequenceTest.java b/utils/misc/src/test/java/org/onlab/util/ImmutableByteSequenceTest.java
index b297f6e..e15065e 100644
--- a/utils/misc/src/test/java/org/onlab/util/ImmutableByteSequenceTest.java
+++ b/utils/misc/src/test/java/org/onlab/util/ImmutableByteSequenceTest.java
@@ -26,12 +26,15 @@
import java.nio.ByteOrder;
import java.util.Random;
+import static java.lang.Integer.max;
import static java.lang.String.format;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
public class ImmutableByteSequenceTest {
+ public static final int MIN_RAND_FIT_VALUE = 0xf;
+ public static final int MAX_RAND_FIT_VALUE = 0x7fffffff;
@Rule
public ExpectedException thrown = ExpectedException.none();
@@ -238,10 +241,14 @@
// Test fit against the computed MSB index.
Random random = new Random();
for (int i = 0; i < 1000; i++) {
- ImmutableByteSequence bytes = ImmutableByteSequence.copyFrom((long) Math.abs(random.nextInt()));
+ int randValue = random.nextInt((MAX_RAND_FIT_VALUE - MIN_RAND_FIT_VALUE) + 1) + MIN_RAND_FIT_VALUE;
+ ImmutableByteSequence bytes = ImmutableByteSequence.copyFrom((long) randValue);
int msbIndex = bytes.msbIndex();
- checkIllegalFit(bytes, msbIndex - random.nextInt(16));
- checkLegalFit(bytes, msbIndex + 2 + random.nextInt(16));
+ // Truncate.
+ checkIllegalFit(bytes, max(msbIndex - random.nextInt(16), 1));
+ // Expand.
+ checkLegalFit(bytes, msbIndex + 2 + random.nextInt(128));
+ // Fit to same bit-width of original value.
checkLegalFit(bytes, msbIndex + 1);
}
}