From a2cd01b32bffb6fe33d34362670c8bdc4ff9974b Mon Sep 17 00:00:00 2001 From: Scott Mitchell Date: Fri, 8 May 2015 01:58:36 -0700 Subject: [PATCH] ByteString test failure bug Motivation: There is an error in the ByteString test logic which is resulting in test failures. Modifications: - Fix the loop iteration to use the loop iteration variable instead of a fixed index. Result: Tests are less buggy. --- .../java/io/netty/util/ByteStringTest.java | 23 ++++++++++++------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/common/src/test/java/io/netty/util/ByteStringTest.java b/common/src/test/java/io/netty/util/ByteStringTest.java index 1d7f786d06..5379b296ca 100644 --- a/common/src/test/java/io/netty/util/ByteStringTest.java +++ b/common/src/test/java/io/netty/util/ByteStringTest.java @@ -54,25 +54,32 @@ public class ByteStringTest { bByteString = new ByteString(b, bOffset, length, false); int i; + final int end = aOffset + length; // Find an element that can be decremented - for (i = 1; i < length; ++i) { - if (a[aOffset + 1] > Byte.MIN_VALUE) { - --a[aOffset + 1]; + for (i = aOffset + 1; i < end; ++i) { + if (a[i] > Byte.MIN_VALUE) { + --a[i]; break; } } + if (i == end) { + throw new IllegalStateException("Couldn't find an index to decrement, all random numbers Byte.MIN_VALUE"); + } lessThanAByteString = new ByteString(a, aOffset, length, true); - ++a[aOffset + i]; // Restore the a array to the original value + ++a[i]; // Restore the a array to the original value // Find an element that can be incremented - for (i = 1; i < length; ++i) { - if (a[aOffset + 1] < Byte.MAX_VALUE) { - ++a[aOffset + 1]; + for (i = aOffset + 1; i < end; ++i) { + if (a[i] < Byte.MAX_VALUE) { + ++a[i]; break; } } + if (i == end) { + throw new IllegalStateException("Couldn't find an index to increment, all random numbers Byte.MAX_VALUE"); + } greaterThanAByteString = new ByteString(a, aOffset, length, true); - --a[aOffset + i]; // Restore the a array to the original value + --a[i]; // Restore the a array to the original value } @Test