aboutsummaryrefslogtreecommitdiffstats
path: root/src/junit
diff options
context:
space:
mode:
authorSven Gothel <[email protected]>2014-09-29 03:57:30 +0200
committerSven Gothel <[email protected]>2014-09-29 03:57:30 +0200
commit00a9ee70054872712017b5a14b19aa92068c8420 (patch)
tree755dab002246d461f422218b75570cf7d6ef2042 /src/junit
parent92a6d2c1476fd562721f231f89afba9342ed8a20 (diff)
Bug 1080 - Refine MappedByteBuffer*Stream impl. and API [doc], adding stream to stream copy as well as direct memory mapped ByteBuffer access
Diffstat (limited to 'src/junit')
-rw-r--r--src/junit/com/jogamp/common/nio/TestByteBufferCopyStream.java177
-rw-r--r--src/junit/com/jogamp/common/nio/TestByteBufferInputStream.java22
-rw-r--r--src/junit/com/jogamp/common/nio/TestByteBufferOutputStream.java6
3 files changed, 191 insertions, 14 deletions
diff --git a/src/junit/com/jogamp/common/nio/TestByteBufferCopyStream.java b/src/junit/com/jogamp/common/nio/TestByteBufferCopyStream.java
new file mode 100644
index 0000000..3442159
--- /dev/null
+++ b/src/junit/com/jogamp/common/nio/TestByteBufferCopyStream.java
@@ -0,0 +1,177 @@
+/**
+ * Copyright 2014 JogAmp Community. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without modification, are
+ * permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice, this list of
+ * conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright notice, this list
+ * of conditions and the following disclaimer in the documentation and/or other materials
+ * provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
+ * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * The views and conclusions contained in the software and documentation are those of the
+ * authors and should not be interpreted as representing official policies, either expressed
+ * or implied, of JogAmp Community.
+ */
+package com.jogamp.common.nio;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.RandomAccessFile;
+import java.nio.channels.FileChannel;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import com.jogamp.junit.util.JunitTracer;
+
+import org.junit.FixMethodOrder;
+import org.junit.runners.MethodSorters;
+
+/**
+ * Testing {@link MappedByteBufferInputStream} and {@link MappedByteBufferOutputStream}
+ * direct stream to stream copy via mapped buffers.
+ */
+@FixMethodOrder(MethodSorters.NAME_ASCENDING)
+public class TestByteBufferCopyStream extends JunitTracer {
+
+ static void testImpl(final String srcFileName, final long size,
+ final MappedByteBufferInputStream.CacheMode srcCacheMode, final int srcSliceShift,
+ final String dstFileName,
+ final MappedByteBufferInputStream.CacheMode dstCacheMode, final int dstSliceShift ) throws IOException {
+ final Runtime runtime = Runtime.getRuntime();
+ final long[] usedMem0 = { 0 };
+ final long[] freeMem0 = { 0 };
+ final long[] usedMem1 = { 0 };
+ final long[] freeMem1 = { 0 };
+ final String prefix = "test "+String.format(TestByteBufferInputStream.PrintPrecision+" MiB", size/TestByteBufferInputStream.MIB);
+ TestByteBufferInputStream.dumpMem(prefix+" before", runtime, -1, -1, usedMem0, freeMem0 );
+
+ final File srcFile = new File(srcFileName);
+ srcFile.delete();
+ srcFile.createNewFile();
+ srcFile.deleteOnExit();
+
+ final RandomAccessFile input;
+ {
+ final RandomAccessFile _input = new RandomAccessFile(srcFile, "rw");
+ _input.setLength(size);
+ _input.close();
+ input = new RandomAccessFile(srcFile, "r");
+ }
+ final MappedByteBufferInputStream mis = new MappedByteBufferInputStream(input.getChannel(),
+ FileChannel.MapMode.READ_ONLY,
+ srcCacheMode,
+ srcSliceShift);
+ Assert.assertEquals(size, input.length());
+ Assert.assertEquals(size, mis.length());
+ Assert.assertEquals(0, mis.position());
+ Assert.assertEquals(size, mis.remaining());
+
+ final File dstFile = new File(dstFileName);
+ dstFile.delete();
+ dstFile.createNewFile();
+ dstFile.deleteOnExit();
+ final RandomAccessFile output = new RandomAccessFile(dstFile, "rw");
+ final MappedByteBufferInputStream.FileResizeOp szOp = new MappedByteBufferInputStream.FileResizeOp() {
+ @Override
+ public void setLength(final long newSize) throws IOException {
+ output.setLength(newSize);
+ }
+ };
+ final MappedByteBufferOutputStream mos = new MappedByteBufferOutputStream(output.getChannel(),
+ FileChannel.MapMode.READ_WRITE,
+ dstCacheMode,
+ srcSliceShift, szOp);
+ Assert.assertEquals(0, output.length());
+ Assert.assertEquals(0, mos.length());
+ Assert.assertEquals(0, mos.position());
+ Assert.assertEquals(0, mos.remaining());
+
+ mos.write(mis, mis.remaining());
+
+ Assert.assertEquals(size, input.length());
+ Assert.assertEquals(size, output.length());
+ Assert.assertEquals(size, mis.length());
+ Assert.assertEquals(size, mos.length());
+ Assert.assertEquals(size, mis.position());
+ Assert.assertEquals(size, mos.position());
+ Assert.assertEquals(0, mis.remaining());
+ Assert.assertEquals(0, mos.remaining());
+
+ mos.close();
+ mis.close();
+ input.close();
+ output.close();
+ srcFile.delete();
+ dstFile.delete();
+ TestByteBufferInputStream.dumpMem(prefix+" after ", runtime, usedMem0[0], freeMem0[0], usedMem1, freeMem1 );
+ System.gc();
+ try {
+ Thread.sleep(500);
+ } catch (final InterruptedException e) { }
+ TestByteBufferInputStream.dumpMem(prefix+" gc'ed ", runtime, usedMem0[0], freeMem0[0], usedMem1, freeMem1 );
+ }
+
+ @Test
+ public void test00() throws IOException {
+ final int srcSliceShift = MappedByteBufferInputStream.DEFAULT_SLICE_SHIFT;
+ final int dstSliceShift = MappedByteBufferInputStream.DEFAULT_SLICE_SHIFT;
+ final long size = 3L * ( 1L << 30 ); // 3 GiB
+ testImpl("./testIn.bin", size, MappedByteBufferInputStream.CacheMode.FLUSH_PRE_HARD, srcSliceShift,
+ "./testOut.bin", MappedByteBufferInputStream.CacheMode.FLUSH_PRE_HARD, dstSliceShift );
+ }
+
+ @Test
+ public void test01() throws IOException {
+ final int srcSliceShift = MappedByteBufferInputStream.DEFAULT_SLICE_SHIFT;
+ final int dstSliceShift = MappedByteBufferInputStream.DEFAULT_SLICE_SHIFT;
+ final long size = 3L * ( 1L << 30 ); // 3 GiB
+ testImpl("./testIn.bin", size, MappedByteBufferInputStream.CacheMode.FLUSH_PRE_SOFT, srcSliceShift,
+ "./testOut.bin", MappedByteBufferInputStream.CacheMode.FLUSH_PRE_SOFT, dstSliceShift );
+ }
+
+ @Test
+ public void test02() throws IOException {
+ final int srcSliceShift = 28; // 256M bytes per slice
+ final int dstSliceShift = 28; // 256M bytes per slice
+ final long size = 3L * ( 1L << 30 ); // 3 GiB
+ testImpl("./testIn.bin", size, MappedByteBufferInputStream.CacheMode.FLUSH_PRE_SOFT, srcSliceShift,
+ "./testOut.bin", MappedByteBufferInputStream.CacheMode.FLUSH_PRE_SOFT, dstSliceShift );
+ }
+
+ @Test
+ public void test11() throws IOException {
+ final int srcSliceShift = 28; // 256M bytes per slice
+ final int dstSliceShift = 27; // 128M bytes per slice
+ final long size = 3L * ( 1L << 30 ); // 3 GiB
+ testImpl("./testIn.bin", size, MappedByteBufferInputStream.CacheMode.FLUSH_PRE_SOFT, srcSliceShift,
+ "./testOut.bin", MappedByteBufferInputStream.CacheMode.FLUSH_PRE_SOFT, dstSliceShift );
+ }
+
+ @Test
+ public void test12() throws IOException {
+ final int srcSliceShift = 27; // 128M bytes per slice
+ final int dstSliceShift = 28; // 256M bytes per slice
+ final long size = 3L * ( 1L << 30 ); // 3 GiB
+ testImpl("./testIn.bin", size, MappedByteBufferInputStream.CacheMode.FLUSH_PRE_SOFT, srcSliceShift,
+ "./testOut.bin", MappedByteBufferInputStream.CacheMode.FLUSH_PRE_SOFT, dstSliceShift );
+ }
+
+ public static void main(final String args[]) throws IOException {
+ final String tstname = TestByteBufferCopyStream.class.getName();
+ org.junit.runner.JUnitCore.main(tstname);
+ }
+}
diff --git a/src/junit/com/jogamp/common/nio/TestByteBufferInputStream.java b/src/junit/com/jogamp/common/nio/TestByteBufferInputStream.java
index 195bef3..61961a3 100644
--- a/src/junit/com/jogamp/common/nio/TestByteBufferInputStream.java
+++ b/src/junit/com/jogamp/common/nio/TestByteBufferInputStream.java
@@ -84,15 +84,15 @@ public class TestByteBufferInputStream extends JunitTracer {
static final String fileTwoPlusGiB = "./testTwoPlusGiB.bin" ;
static final String fileOut = "./testOut.bin" ;
- static final String printPrecision = "%8.3f";
- static final double mib = 1024.0*1024.0;
+ public static final String PrintPrecision = "%8.3f";
+ public static final double MIB = 1024.0*1024.0;
@BeforeClass
public static void setup() throws IOException {
final Runtime runtime = Runtime.getRuntime();
- System.err.printf("Total Memory : "+printPrecision+" MiB%n", runtime.totalMemory() / mib);
- System.err.printf("Max Memory : "+printPrecision+" MiB%n", runtime.maxMemory() / mib);
+ System.err.printf("Total Memory : "+PrintPrecision+" MiB%n", runtime.totalMemory() / MIB);
+ System.err.printf("Max Memory : "+PrintPrecision+" MiB%n", runtime.maxMemory() / MIB);
setup(fileHalfMiB, halfMiB);
setup(fileOneMiB, oneMiB);
@@ -189,7 +189,7 @@ public class TestByteBufferInputStream extends JunitTracer {
final long[] usedMem1 = { 0 };
final long[] freeMem1 = { 0 };
- final String prefix = "test #"+iter+" "+String.format(printPrecision+" MiB", expSize/mib);
+ final String prefix = "test #"+iter+" "+String.format(PrintPrecision+" MiB", expSize/MIB);
System.err.printf("%s: mode %-5s, bufferSize %9d: BEGIN%n", prefix, srcType.toString(), bufferSize);
dumpMem(prefix+" before", runtime, -1, -1, usedMem0, freeMem0 );
@@ -323,17 +323,17 @@ public class TestByteBufferInputStream extends JunitTracer {
}
}
- static void dumpMem(final String pre,
- final Runtime runtime, final long usedMem0,
- final long freeMem0, final long[] usedMemN,
- final long[] freeMemN )
+ public static void dumpMem(final String pre,
+ final Runtime runtime, final long usedMem0,
+ final long freeMem0, final long[] usedMemN,
+ final long[] freeMemN )
{
usedMemN[0] = runtime.totalMemory() - runtime.freeMemory();
freeMemN[0] = runtime.freeMemory();
- System.err.printf("%s Used Memory : "+printPrecision, pre, usedMemN[0] / mib);
+ System.err.printf("%s Used Memory : "+PrintPrecision, pre, usedMemN[0] / MIB);
if( 0 < usedMem0 ) {
- System.err.printf(", delta "+printPrecision, (usedMemN[0]-usedMem0) / mib);
+ System.err.printf(", delta "+PrintPrecision, (usedMemN[0]-usedMem0) / MIB);
}
System.err.println(" MiB");
/**
diff --git a/src/junit/com/jogamp/common/nio/TestByteBufferOutputStream.java b/src/junit/com/jogamp/common/nio/TestByteBufferOutputStream.java
index 10d7f50..c06dbe8 100644
--- a/src/junit/com/jogamp/common/nio/TestByteBufferOutputStream.java
+++ b/src/junit/com/jogamp/common/nio/TestByteBufferOutputStream.java
@@ -47,9 +47,9 @@ import org.junit.runners.MethodSorters;
public class TestByteBufferOutputStream extends JunitTracer {
static void testImpl(final String fname,
- final byte[] payLoad, final long payLoadOffset, final long postPayLoadFiller,
- final byte[] endBytes,
- final int sliceShift)
+ final byte[] payLoad, final long payLoadOffset, final long postPayLoadFiller,
+ final byte[] endBytes,
+ final int sliceShift)
throws IOException
{
final File file = new File(fname);