Search in sources :

Example 1 with Random

use of com.cinchapi.concourse.util.Random in project concourse by cinchapi.

the class ByteSinkTest method testFileChannelSinkWithBufferAccuracy.

@Test
public void testFileChannelSinkWithBufferAccuracy() throws IOException {
    java.util.Random random = new java.util.Random();
    ByteBuffer expected = ByteBuffer.allocate(random.nextInt(64000000) + 1000000);
    ByteSink a = ByteSink.to(expected);
    FileChannel channel = FileSystem.getFileChannel(Paths.get(TestData.getTemporaryTestFile()));
    ByteSink b = ByteSink.to(channel, 512);
    while (expected.hasRemaining()) {
        int seed = Math.abs(random.nextInt());
        try {
            if (seed % 3 == 0) {
                String src = Random.getString();
                a.putUtf8(src);
                b.putUtf8(src);
            } else if (seed % 5 == 0) {
                long src = random.nextLong();
                a.putLong(src);
                b.putLong(src);
            } else if (seed % 4 == 0) {
                int src = random.nextInt();
                a.putInt(src);
                b.putInt(src);
            } else if (seed % 2 == 0) {
                double src = random.nextDouble();
                a.putDouble(src);
                b.putDouble(src);
            } else {
                byte[] src = new byte[1024];
                Arrays.fill(src, (byte) 17);
                a.put(src);
                b.put(src);
            }
        } catch (BufferOverflowException e) {
            break;
        }
    }
    a.flush();
    b.flush();
    expected.flip();
    ByteBuffer actual = ByteBuffer.allocate((int) channel.position());
    channel.position(0);
    channel.read(actual);
    actual.flip();
    Assert.assertEquals(expected.remaining(), actual.remaining());
    while (expected.hasRemaining()) {
        Assert.assertEquals(expected.get(), actual.get());
    }
}
Also used : Random(com.cinchapi.concourse.util.Random) FileChannel(java.nio.channels.FileChannel) BufferOverflowException(java.nio.BufferOverflowException) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Aggregations

Random (com.cinchapi.concourse.util.Random)1 BufferOverflowException (java.nio.BufferOverflowException)1 ByteBuffer (java.nio.ByteBuffer)1 FileChannel (java.nio.channels.FileChannel)1 Test (org.junit.Test)1