More test cases
This commit is contained in:
parent
2878a47222
commit
94831112ee
@ -1055,6 +1055,80 @@ public abstract class AbstractChannelBufferTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSequentialByteBufferBackedHeapBufferTransfer1() {
|
||||
byte[] valueContent = new byte[BLOCK_SIZE * 2];
|
||||
ChannelBuffer value = wrappedBuffer(ByteBuffer.allocate(BLOCK_SIZE * 2));
|
||||
value.writerIndex(0);
|
||||
buffer.writerIndex(0);
|
||||
for (int i = 0; i < buffer.capacity() - BLOCK_SIZE + 1; i += BLOCK_SIZE) {
|
||||
random.nextBytes(valueContent);
|
||||
value.setBytes(0, valueContent);
|
||||
assertEquals(0, buffer.readerIndex());
|
||||
assertEquals(i, buffer.writerIndex());
|
||||
buffer.writeBytes(value, random.nextInt(BLOCK_SIZE), BLOCK_SIZE);
|
||||
assertEquals(0, value.readerIndex());
|
||||
assertEquals(0, value.writerIndex());
|
||||
}
|
||||
|
||||
random.setSeed(seed);
|
||||
byte[] expectedValueContent = new byte[BLOCK_SIZE * 2];
|
||||
ChannelBuffer expectedValue = wrappedBuffer(expectedValueContent);
|
||||
for (int i = 0; i < buffer.capacity() - BLOCK_SIZE + 1; i += BLOCK_SIZE) {
|
||||
random.nextBytes(expectedValueContent);
|
||||
int valueOffset = random.nextInt(BLOCK_SIZE);
|
||||
value.setBytes(0, valueContent);
|
||||
assertEquals(i, buffer.readerIndex());
|
||||
assertEquals(CAPACITY, buffer.writerIndex());
|
||||
buffer.readBytes(value, valueOffset, BLOCK_SIZE);
|
||||
for (int j = valueOffset; j < valueOffset + BLOCK_SIZE; j ++) {
|
||||
assertEquals(expectedValue.getByte(j), value.getByte(j));
|
||||
}
|
||||
assertEquals(0, value.readerIndex());
|
||||
assertEquals(0, value.writerIndex());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSequentialByteBufferBackedHeapBufferTransfer2() {
|
||||
byte[] valueContent = new byte[BLOCK_SIZE * 2];
|
||||
ChannelBuffer value = wrappedBuffer(ByteBuffer.allocate(BLOCK_SIZE * 2));
|
||||
value.writerIndex(0);
|
||||
buffer.writerIndex(0);
|
||||
for (int i = 0; i < buffer.capacity() - BLOCK_SIZE + 1; i += BLOCK_SIZE) {
|
||||
random.nextBytes(valueContent);
|
||||
value.setBytes(0, valueContent);
|
||||
assertEquals(0, buffer.readerIndex());
|
||||
assertEquals(i, buffer.writerIndex());
|
||||
int readerIndex = random.nextInt(BLOCK_SIZE);
|
||||
value.readerIndex(0);
|
||||
value.writerIndex(readerIndex + BLOCK_SIZE);
|
||||
value.readerIndex(readerIndex);
|
||||
buffer.writeBytes(value);
|
||||
assertEquals(readerIndex + BLOCK_SIZE, value.writerIndex());
|
||||
assertEquals(value.writerIndex(), value.readerIndex());
|
||||
}
|
||||
|
||||
random.setSeed(seed);
|
||||
byte[] expectedValueContent = new byte[BLOCK_SIZE * 2];
|
||||
ChannelBuffer expectedValue = wrappedBuffer(expectedValueContent);
|
||||
for (int i = 0; i < buffer.capacity() - BLOCK_SIZE + 1; i += BLOCK_SIZE) {
|
||||
random.nextBytes(expectedValueContent);
|
||||
value.setBytes(0, valueContent);
|
||||
int valueOffset = random.nextInt(BLOCK_SIZE);
|
||||
assertEquals(i, buffer.readerIndex());
|
||||
assertEquals(CAPACITY, buffer.writerIndex());
|
||||
value.readerIndex(valueOffset);
|
||||
value.writerIndex(valueOffset);
|
||||
buffer.readBytes(value, BLOCK_SIZE);
|
||||
for (int j = valueOffset; j < valueOffset + BLOCK_SIZE; j ++) {
|
||||
assertEquals(expectedValue.getByte(j), value.getByte(j));
|
||||
}
|
||||
assertEquals(valueOffset, value.readerIndex());
|
||||
assertEquals(valueOffset + BLOCK_SIZE, value.writerIndex());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSequentialByteBufferTransfer() {
|
||||
buffer.writerIndex(0);
|
||||
|
@ -0,0 +1,48 @@
|
||||
/*
|
||||
* JBoss, Home of Professional Open Source
|
||||
*
|
||||
* Copyright 2008, Red Hat Middleware LLC, and individual contributors
|
||||
* by the @author tags. See the COPYRIGHT.txt in the distribution for a
|
||||
* full listing of individual contributors.
|
||||
*
|
||||
* This is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser General Public License as
|
||||
* published by the Free Software Foundation; either version 2.1 of
|
||||
* the License, or (at your option) any later version.
|
||||
*
|
||||
* This software is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this software; if not, write to the Free
|
||||
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
|
||||
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
|
||||
*/
|
||||
package org.jboss.netty.buffer;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author The Netty Project (netty-dev@lists.jboss.org)
|
||||
* @author Trustin Lee (tlee@redhat.com)
|
||||
*
|
||||
* @version $Rev$, $Date$
|
||||
*/
|
||||
public class ByteBufferBackedHeapChannelBufferTest extends AbstractChannelBufferTest {
|
||||
|
||||
private ChannelBuffer buffer;
|
||||
|
||||
@Override
|
||||
protected ChannelBuffer newBuffer(int length) {
|
||||
buffer = new ByteBufferBackedChannelBuffer(ByteBuffer.allocate(length));
|
||||
return buffer;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ChannelBuffer[] components() {
|
||||
return new ChannelBuffer[] { buffer };
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user