📄 byteaccesstest.java
字号:
{ // Remove nothing. resetOperations(); ByteArray removed = cba.removeTo(0); assertEquals(0, removed.first()); assertEquals(0, removed.last()); assertEquals(0, cba.first()); assertEquals(100, cba.last()); removed.free(); assertOperationCountEquals(0); } { // Remove entire component. resetOperations(); ByteArray removed = cba.removeTo(100); assertEquals(0, removed.first()); assertEquals(100, removed.last()); assertEquals(100, cba.first()); assertEquals(100, cba.last()); removed.free(); assertOperationCountEquals(1); } { // Remove nothing. resetOperations(); ByteArray removed = cba.removeTo(100); assertEquals(0, removed.first()); assertEquals(0, removed.last()); assertEquals(100, cba.first()); assertEquals(100, cba.last()); removed.free(); assertOperationCountEquals(0); } cba.addLast(getByteArrayFactory().create(100)); { // Remove nothing. resetOperations(); ByteArray removed = cba.removeTo(100); assertEquals(0, removed.first()); assertEquals(0, removed.last()); assertEquals(100, cba.first()); assertEquals(200, cba.last()); removed.free(); assertOperationCountEquals(0); } { // Remove half a component. resetOperations(); ByteArray removed = cba.removeTo(150); assertEquals(0, removed.first()); assertEquals(50, removed.last()); assertEquals(150, cba.first()); assertEquals(200, cba.last()); removed.free(); assertOperationCountEquals(0); // Doesn't free until component finished. } { // Remove nothing. resetOperations(); ByteArray removed = cba.removeTo(150); assertEquals(0, removed.first()); assertEquals(0, removed.last()); assertEquals(150, cba.first()); assertEquals(200, cba.last()); removed.free(); assertOperationCountEquals(0); } { // Remove other half. resetOperations(); ByteArray removed = cba.removeTo(200); assertEquals(0, removed.first()); assertEquals(50, removed.last()); assertEquals(200, cba.first()); assertEquals(200, cba.last()); removed.free(); assertOperationCountEquals(1); // Frees ByteArray behind both buffers. } } public void testCompositeByteArraySlicing() { CompositeByteArray cba = new CompositeByteArray(); cba.addLast(getByteArrayFactory().create(10)); cba.addLast(getByteArrayFactory().create(10)); cba.addLast(getByteArrayFactory().create(10)); testByteArraySlicing(cba, 0, 30); testByteArraySlicing(cba, 5, 10); testByteArraySlicing(cba, 10, 20); testByteArraySlicing(cba, 1, 28); testByteArraySlicing(cba, 19, 2); } public void testBufferByteArraySlicing() { ByteArray bba = getByteArrayFactory().create(30); testByteArraySlicing(bba, 0, 30); testByteArraySlicing(bba, 5, 10); testByteArraySlicing(bba, 10, 20); testByteArraySlicing(bba, 1, 28); testByteArraySlicing(bba, 19, 2); } private void testByteArraySlicing(ByteArray ba, int start, int length) { ByteArray slice = ba.slice(start, length); for (int i = 0; i < length; i++) { byte b1 = (byte) (i % 67); byte b2 = (byte) (i % 36); int sourceIndex = i + start; int sliceIndex = i; ba.put(sourceIndex, b1); assertEquals(b1, ba.get(sourceIndex)); assertEquals(b1, slice.get(sliceIndex)); slice.put(sliceIndex, b2); assertEquals(b2, ba.get(sourceIndex)); assertEquals(b2, slice.get(sliceIndex)); } } private ChunkedExpander getExpander(final int chunkSize) { return new ChunkedExpander(getByteArrayFactory(), chunkSize) { @Override public void expand(CompositeByteArray cba, int minSize) { addOperation("ChunkedExpander(" + chunkSize + ").expand(" + cba + "," + minSize + ")"); super.expand(cba, minSize); } }; } private Flusher getFlusher() { return new CompositeByteArrayRelativeWriter.Flusher() { public void flush(ByteArray ba) { addOperation("Flusher().flush(" + ba + ")"); ba.free(); } }; } private SimpleByteArrayFactory getByteArrayFactory() { return new SimpleByteArrayFactory() { @Override public ByteArray create(final int size) { if (size < 0) { throw new IllegalArgumentException( "Buffer size must not be negative:" + size); } IoBuffer bb = IoBuffer.allocate(size); ByteArray ba = new BufferByteArray(bb) { @Override public void free() { addOperation(this + ".free()"); // Nothing to do. } }; addOperation("SimpleByteArrayFactory().create(" + size + ") = " + ba); return ba; } }; } private void testRelativeReaderAndWriter(int length, IoRelativeReader reader, IoRelativeWriter writer) { for (int i = 0; i < length; i++) { byte b = (byte) (i % 67); writer.put(b); assertEquals(b, reader.get()); } } private void testAbsoluteReaderAndWriter(int start, int length, IoAbsoluteReader reader, IoAbsoluteWriter writer) { for (int i = start; i < length; i++) { byte b = (byte) (i % 67); writer.put(i, b); assertEquals(b, reader.get(i)); } } public void testByteArrayPrimitiveAccess() { ByteArray bbaBig = getByteArrayFactory().create(1000); bbaBig.order(ByteOrder.BIG_ENDIAN); testPrimitiveAccess(bbaBig.cursor(), bbaBig.cursor()); ByteArray bbaLittle = getByteArrayFactory().create(1000); bbaLittle.order(ByteOrder.LITTLE_ENDIAN); testPrimitiveAccess(bbaLittle.cursor(), bbaLittle.cursor()); } public void testByteArrayBufferAccess() { ByteArray ba = getByteArrayFactory().create(1); ba.put(0, (byte) 99); IoBuffer bb = IoBuffer.allocate(2); bb.clear(); Cursor cursor = ba.cursor(); assertEquals(0, cursor.getIndex()); assertEquals(1, cursor.getRemaining()); assertEquals(0, bb.position()); assertEquals(2, bb.remaining()); cursor.get(bb); assertEquals(1, cursor.getIndex()); assertEquals(0, cursor.getRemaining()); assertEquals(1, bb.position()); assertEquals(1, bb.remaining()); } public void testCompositeByteArrayPrimitiveAccess() { CompositeByteArray cbaBig = new CompositeByteArray(); cbaBig.order(ByteOrder.BIG_ENDIAN); for (int i = 0; i < 1000; i++) { ByteArray component = getByteArrayFactory().create(1); component.order(ByteOrder.BIG_ENDIAN); cbaBig.addLast(component); } testPrimitiveAccess(cbaBig.cursor(), cbaBig.cursor()); CompositeByteArray cbaLittle = new CompositeByteArray(); cbaLittle.order(ByteOrder.LITTLE_ENDIAN); for (int i = 0; i < 1000; i++) { ByteArray component = getByteArrayFactory().create(1); component.order(ByteOrder.LITTLE_ENDIAN); cbaLittle.addLast(component); } testPrimitiveAccess(cbaLittle.cursor(), cbaLittle.cursor()); } public void testCompositeByteArrayWrapperPrimitiveAccess() { CompositeByteArray cbaBig = new CompositeByteArray(); cbaBig.order(ByteOrder.BIG_ENDIAN); for (int i = 0; i < 1000; i++) { ByteArray component = getByteArrayFactory().create(1); component.order(ByteOrder.BIG_ENDIAN); cbaBig.addLast(component); } testPrimitiveAccess(new CompositeByteArrayRelativeWriter(cbaBig, getExpander(10), getFlusher(), false), new CompositeByteArrayRelativeReader(cbaBig, true)); CompositeByteArray cbaLittle = new CompositeByteArray(); cbaLittle.order(ByteOrder.LITTLE_ENDIAN); for (int i = 0; i < 1000; i++) { ByteArray component = getByteArrayFactory().create(1); component.order(ByteOrder.LITTLE_ENDIAN); cbaLittle.addLast(component); } testPrimitiveAccess(new CompositeByteArrayRelativeWriter(cbaLittle, getExpander(10), getFlusher(), false), new CompositeByteArrayRelativeReader(cbaLittle, true)); } private void testPrimitiveAccess(IoRelativeWriter write, IoRelativeReader read) { byte b = (byte) 0x12; write.put(b); assertEquals(b, read.get()); short s = (short) 0x12; write.putShort(s); assertEquals(s, read.getShort()); int i = 0x12345678; write.putInt(i); assertEquals(i, read.getInt()); long l = 0x1234567890123456L; write.putLong(l); assertEquals(l, read.getLong()); float f = Float.intBitsToFloat(i); write.putFloat(f); assertEquals(f, read.getFloat()); double d = Double.longBitsToDouble(l); write.putDouble(d); assertEquals(d, read.getDouble()); char c = (char) 0x1234; write.putChar(c); assertEquals(c, read.getChar()); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -