Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into #400_immutable_link…
Browse files Browse the repository at this point in the history
…edlist
  • Loading branch information
mvanaken committed Feb 7, 2024
2 parents 4629640 + 18421b5 commit 36769b6
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Value> 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));
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -28,19 +31,28 @@
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;
import static io.parsingdata.metal.util.EnvironmentFactory.env;
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 {

Expand Down Expand Up @@ -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);
}

}

0 comments on commit 36769b6

Please sign in to comment.