diff --git a/core/src/test/java/io/parsingdata/metal/data/ByteStreamSourceTest.java b/core/src/test/java/io/parsingdata/metal/data/ByteStreamSourceTest.java index 3c408ee1..ac9a53c8 100644 --- a/core/src/test/java/io/parsingdata/metal/data/ByteStreamSourceTest.java +++ b/core/src/test/java/io/parsingdata/metal/data/ByteStreamSourceTest.java @@ -18,14 +18,20 @@ import static java.math.BigInteger.TEN; import static java.math.BigInteger.ZERO; +import static java.math.BigInteger.valueOf; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertThrows; import java.io.IOException; import java.io.UncheckedIOException; import java.math.BigInteger; +import java.util.Random; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.Timeout; + +import io.parsingdata.metal.util.InMemoryByteStream; public class ByteStreamSourceTest { @@ -39,4 +45,27 @@ public void brokenByteStream() { assertThrows(UncheckedIOException.class, () -> Slice.createFromSource(DUMMY_BYTE_STREAM_SOURCE, ZERO, TEN).get().getData()); } + @Test + @Timeout(value=1) + public void byteStreamSourceRead() { + // Create a large array with random data + final int arraySize = 5_120_000; + final byte[] bytes = new byte[arraySize]; + new Random().nextBytes(bytes); + + // Create a value that has a ConcatenatedValueSource as source. + final ByteStreamSource source = new ByteStreamSource(new InMemoryByteStream(bytes)); + + // Read from the source in small parts. + final int readSize = 512; + final byte[] valueBytes = new byte[arraySize]; + for (int part = 0; part < arraySize / readSize; part++) { + final byte[] data = source.getData(valueOf(readSize * part), valueOf(readSize)); + System.arraycopy(data, 0, valueBytes, readSize * part, data.length); + } + + // Make sure we read the data correctly. + assertArrayEquals(bytes, valueBytes); + } + } diff --git a/core/src/test/java/io/parsingdata/metal/data/ConcatenatedValueSourceTest.java b/core/src/test/java/io/parsingdata/metal/data/ConcatenatedValueSourceTest.java index e351525d..176fcc23 100644 --- a/core/src/test/java/io/parsingdata/metal/data/ConcatenatedValueSourceTest.java +++ b/core/src/test/java/io/parsingdata/metal/data/ConcatenatedValueSourceTest.java @@ -107,7 +107,7 @@ public void concatenatedValueSourceRead() { new Random().nextBytes(bytes); // Split the data in separate CoreValues. - int parts = 4; + final int parts = 4; ImmutableList values = new ImmutableList<>(); for (int part = 0; part < parts; part++) { values = values.addHead(new CoreValue(Slice.createFromBytes(Arrays.copyOfRange(bytes, (arraySize / parts) * part, (arraySize / parts) * (part + 1))), Encoding.DEFAULT_ENCODING)); @@ -119,13 +119,10 @@ public void concatenatedValueSourceRead() { // Read from the source in small parts. final int readSize = 512; final byte[] bytesRead = new byte[arraySize]; - final long start = System.currentTimeMillis(); for (int part = 0; part < arraySize / readSize; part++) { final byte[] data = source.getData(valueOf(readSize * part), valueOf(readSize)); System.arraycopy(data, 0, bytesRead, readSize * part, data.length); } - final long end = System.currentTimeMillis(); - System.out.printf("Source read: %ss%n", (end - start) / 1000.0); // Make sure we read the data correctly. assertArrayEquals(bytes, bytesRead); diff --git a/core/src/test/java/io/parsingdata/metal/data/ConstantSourceTest.java b/core/src/test/java/io/parsingdata/metal/data/ConstantSourceTest.java new file mode 100644 index 00000000..2c22d6a9 --- /dev/null +++ b/core/src/test/java/io/parsingdata/metal/data/ConstantSourceTest.java @@ -0,0 +1,54 @@ +/* + * Copyright 2013-2023 Netherlands Forensic Institute + * Copyright 2021-2023 Infix Technologies B.V. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.parsingdata.metal.data; + +import static java.math.BigInteger.valueOf; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import java.util.Random; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.Timeout; + +class ConstantSourceTest { + + @Test + @Timeout(value=1) + public void constantSourceRead() { + // Create a large array with random data + final int arraySize = 5_120_000; + final byte[] bytes = new byte[arraySize]; + new Random().nextBytes(bytes); + + // Create a value that has a ConcatenatedValueSource as source. + final ConstantSource source = new ConstantSource(bytes); + + // Read from the source in small parts. + final int readSize = 512; + final byte[] valueBytes = new byte[arraySize]; + for (int part = 0; part < arraySize / readSize; part++) { + final byte[] data = source.getData(valueOf(readSize * part), valueOf(readSize)); + System.arraycopy(data, 0, valueBytes, readSize * part, data.length); + } + + // Make sure we read the data correctly. + assertArrayEquals(bytes, valueBytes); + } + +} \ No newline at end of file diff --git a/core/src/test/java/io/parsingdata/metal/data/DataExpressionSourceTest.java b/core/src/test/java/io/parsingdata/metal/data/DataExpressionSourceTest.java index 4a7298d6..ff269fe7 100644 --- a/core/src/test/java/io/parsingdata/metal/data/DataExpressionSourceTest.java +++ b/core/src/test/java/io/parsingdata/metal/data/DataExpressionSourceTest.java @@ -17,7 +17,10 @@ package io.parsingdata.metal.data; import static java.math.BigInteger.ZERO; +import static java.math.BigInteger.valueOf; +import static java.nio.charset.StandardCharsets.UTF_8; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -28,6 +31,7 @@ import static io.parsingdata.metal.Shorthand.ref; import static io.parsingdata.metal.Shorthand.seq; import static io.parsingdata.metal.Shorthand.tie; +import static io.parsingdata.metal.data.Slice.createFromSource; import static io.parsingdata.metal.data.selection.ByName.getValue; import static io.parsingdata.metal.expression.value.BytesTest.EMPTY_PARSE_STATE; import static io.parsingdata.metal.util.EncodingFactory.enc; @@ -35,12 +39,20 @@ import static io.parsingdata.metal.util.ParseStateFactory.stream; import java.math.BigInteger; +import java.nio.charset.StandardCharsets; +import java.util.Arrays; import java.util.Optional; +import java.util.Random; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.Timeout; +import io.parsingdata.metal.encoding.Encoding; +import io.parsingdata.metal.expression.value.CoreValue; +import io.parsingdata.metal.expression.value.Value; import io.parsingdata.metal.token.Token; +import io.parsingdata.metal.util.ParseStateFactory; public class DataExpressionSourceTest { @@ -79,4 +91,31 @@ public void notAValue() { assertEquals("ValueExpression dataExpression yields NOT_A_VALUE at index 0.", e.getMessage()); } + @Test + @Timeout(value=1) + public void dataExpressionSourceRead() { + // Create a large array with random data + final int arraySize = 5_120_000; + final byte[] bytes = new byte[arraySize]; + new Random().nextBytes(bytes); + + // Split the data in separate CoreValues. + final ParseValue parseValue = new ParseValue("test", def("def", bytes.length), Slice.createFromBytes(bytes), Encoding.DEFAULT_ENCODING); + final ParseState parseState = stream("some parse state", UTF_8).add(parseValue); + + // Create a DataExpressionSource to read from. + final DataExpressionSource source = new DataExpressionSource(ref("test"), 0, parseState, Encoding.DEFAULT_ENCODING); + + // Read from the source in small parts. + final int readSize = 512; + final byte[] valueBytes = new byte[arraySize]; + for (int part = 0; part < arraySize / readSize; part++) { + final byte[] data = source.getData(valueOf(readSize * part), valueOf(readSize)); + System.arraycopy(data, 0, valueBytes, readSize * part, data.length); + } + + // Make sure we read the data correctly. + assertArrayEquals(bytes, valueBytes); + } + }