[#5773] AbstractByteBuf.forEachByteDesc(ByteProcessor) starts from wrong index

Motivation:

We introduced a regression in 1abdbe6f67 which let the iteration start from the wrong index.

Modifications:

Fix start index and add tests.

Result:

Fix regression.
This commit is contained in:
Norman Maurer 2016-08-31 07:15:10 +02:00
parent 6fd8bb8c63
commit 463b5cf21b
4 changed files with 71 additions and 1 deletions

View File

@ -1274,7 +1274,7 @@ public abstract class AbstractByteBuf extends ByteBuf {
public int forEachByteDesc(ByteProcessor processor) { public int forEachByteDesc(ByteProcessor processor) {
ensureAccessible(); ensureAccessible();
try { try {
return forEachByteDesc0(writerIndex, readerIndex, processor); return forEachByteDesc0(writerIndex - 1, readerIndex, processor);
} catch (Exception e) { } catch (Exception e) {
PlatformDependent.throwException(e); PlatformDependent.throwException(e);
return -1; return -1;

View File

@ -3203,6 +3203,52 @@ public abstract class AbstractByteBufTest {
assertEquals(0, buffer2.refCnt()); assertEquals(0, buffer2.refCnt());
} }
@Test
public void testForEachByteDesc2() {
byte[] expected = {1, 2, 3, 4};
ByteBuf buf = newBuffer(expected.length);
try {
buf.writeBytes(expected);
final byte[] bytes = new byte[expected.length];
int i = buf.forEachByteDesc(new ByteProcessor() {
private int index = bytes.length - 1;
@Override
public boolean process(byte value) throws Exception {
bytes[index--] = value;
return true;
}
});
assertEquals(-1, i);
assertArrayEquals(expected, bytes);
} finally {
buf.release();
}
}
@Test
public void testForEachByte2() {
byte[] expected = {1, 2, 3, 4};
ByteBuf buf = newBuffer(expected.length);
try {
buf.writeBytes(expected);
final byte[] bytes = new byte[expected.length];
int i = buf.forEachByte(new ByteProcessor() {
private int index;
@Override
public boolean process(byte value) throws Exception {
bytes[index++] = value;
return true;
}
});
assertEquals(-1, i);
assertArrayEquals(expected, bytes);
} finally {
buf.release();
}
}
private static void assertTrueAndRelease(ByteBuf buf, boolean actual) { private static void assertTrueAndRelease(ByteBuf buf, boolean actual) {
try { try {
assertTrue(actual); assertTrue(actual);

View File

@ -126,6 +126,18 @@ public class SlicedByteBufTest extends AbstractByteBufTest {
// ignore for SlicedByteBuf // ignore for SlicedByteBuf
} }
@Test
@Override
public void testForEachByteDesc2() {
// Ignore for SlicedByteBuf
}
@Test
@Override
public void testForEachByte2() {
// Ignore for SlicedByteBuf
}
@Test(expected = UnsupportedOperationException.class) @Test(expected = UnsupportedOperationException.class)
@Override @Override
public void testDuplicateCapacityChange() { public void testDuplicateCapacityChange() {

View File

@ -123,4 +123,16 @@ public class WrappedUnpooledUnsafeByteBufTest extends BigEndianUnsafeDirectByteB
public void testLittleEndianWithExpand() { public void testLittleEndianWithExpand() {
super.testLittleEndianWithExpand(); super.testLittleEndianWithExpand();
} }
@Test
@Override
public void testForEachByteDesc2() {
// Ignore
}
@Test
@Override
public void testForEachByte2() {
// Ignore
}
} }