From 81a913ced197c2a1c3219ab602c49ded9ec195e7 Mon Sep 17 00:00:00 2001 From: Scott Mitchell Date: Fri, 9 Oct 2015 10:39:39 -0700 Subject: [PATCH] ByteBufUtilTest bug Motivation: The logic in ByteBufUtilTest.ByteBufUtilTest is wrong. It is attempting to ensure at least 1 byte is different in the ranges that will be subsequently compared, but does so before the copy operation. Modifications: - Move the code which ensures there is a difference to after the copy - Simplify the logic which ensures there is a difference Result: Unit test now operates as designed. --- .../java/io/netty/buffer/ByteBufUtilTest.java | 23 ++++++++----------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/buffer/src/test/java/io/netty/buffer/ByteBufUtilTest.java b/buffer/src/test/java/io/netty/buffer/ByteBufUtilTest.java index 9cc68e22d4..19aa053642 100644 --- a/buffer/src/test/java/io/netty/buffer/ByteBufUtilTest.java +++ b/buffer/src/test/java/io/netty/buffer/ByteBufUtilTest.java @@ -42,6 +42,10 @@ public class ByteBufUtilTest { assertTrue(ByteBufUtil.equals(Unpooled.wrappedBuffer(b1), iB1, Unpooled.wrappedBuffer(b2), iB2, length)); } + private static int random(Random r, int min, int max) { + return r.nextInt((max - min) + 1) + min; + } + @Test public void notEqualsBufferSubsections() { byte[] b1 = new byte[50]; @@ -52,20 +56,11 @@ public class ByteBufUtilTest { final int iB1 = b1.length / 2; final int iB2 = iB1 + b1.length; final int length = b1.length - iB1; - // Even though we choose random values...make sure they are not equal. - final int copyLength = length - 1; - final int end = iB1 + copyLength; - boolean foundDiff = false; - for (int i = iB1, j = iB2; i < end; ++i, ++j) { - if (b1[i] != b2[j]) { - foundDiff = true; - break; - } - } - if (!foundDiff) { - ++b1[iB1]; - } - System.arraycopy(b1, iB1, b2, iB2, copyLength); + System.arraycopy(b1, iB1, b2, iB2, length); + // Randomly pick an index in the range that will be compared and make the value at that index differ between + // the 2 arrays. + int diffIndex = random(rand, iB1, iB1 + length - 1); + ++b1[diffIndex]; assertFalse(ByteBufUtil.equals(Unpooled.wrappedBuffer(b1), iB1, Unpooled.wrappedBuffer(b2), iB2, length)); }