use of org.apache.commons.lang3.RandomStringUtils in project pravega by pravega.
the class RevisionDataOutputStreamTests method testWritePrimitives.
/**
* Tests all the primitive data types that had to be implemented because of {@link DataOutput} which are not otherwise
* tested by other means.
*/
@Test
public void testWritePrimitives() throws IOException {
// We write to 2 streams in parallel. We verify our RevisionDataOutputStream vs Java's DataOutputStream - if the
// outputs match, then a DataInputStream (and implicitly RevisionDataInputStream) should be able to read them back.
@Cleanup val expectedStream = new ByteArrayOutputStream();
@Cleanup val expectedDataStream = new DataOutputStream(expectedStream);
@Cleanup val actualStream = new ByteArrayOutputStream();
@Cleanup val actualDataStream = RevisionDataOutputStream.wrap(actualStream);
// Generate a random set of strings using all available characters.
val strings = IntStream.range(0, 20).mapToObj(RandomStringUtils::random).collect(Collectors.toList());
// .. and another set using ASCII alphanumeric characters.
IntStream.range(1, 20).mapToObj(i -> RandomStringUtils.random(i, true, true)).forEach(strings::add);
// Calculate the total length expected.
int stringLengths = strings.stream().mapToInt(s -> s.length() + 2 * s.length() + actualDataStream.getUTFLength(s)).sum();
actualDataStream.length(1 + 1 + 1 + Short.BYTES + Short.BYTES + Integer.BYTES + Long.BYTES + Float.BYTES + Double.BYTES + stringLengths);
// Write some data.
WritePrimitive.apply(true, actualDataStream::writeBoolean, expectedDataStream::writeBoolean);
WritePrimitive.apply(false, actualDataStream::writeBoolean, expectedDataStream::writeBoolean);
WritePrimitive.apply(1, actualDataStream::writeByte, expectedDataStream::writeByte);
WritePrimitive.apply(Short.MAX_VALUE - 2, actualDataStream::writeShort, expectedDataStream::writeShort);
WritePrimitive.apply((int) 'a', actualDataStream::writeChar, expectedDataStream::writeChar);
WritePrimitive.apply(Integer.MAX_VALUE - 4, actualDataStream::writeInt, expectedDataStream::writeInt);
WritePrimitive.apply(Long.MAX_VALUE - 7, actualDataStream::writeLong, expectedDataStream::writeLong);
WritePrimitive.apply(4.0f, actualDataStream::writeFloat, expectedDataStream::writeFloat);
WritePrimitive.apply(8.0d, actualDataStream::writeDouble, expectedDataStream::writeDouble);
for (val s : strings) {
WritePrimitive.apply(s, actualDataStream::writeBytes, expectedDataStream::writeBytes);
WritePrimitive.apply(s, actualDataStream::writeChars, expectedDataStream::writeChars);
WritePrimitive.apply(s, actualDataStream::writeUTF, expectedDataStream::writeUTF);
}
actualDataStream.flush();
expectedDataStream.flush();
val expected = expectedStream.toByteArray();
val actual = actualStream.toByteArray();
Assert.assertEquals(expected.length, actualDataStream.getSize());
Assert.assertEquals(expected.length + Integer.BYTES, actual.length);
AssertExtensions.assertArrayEquals("", expected, 0, actual, Integer.BYTES, expected.length);
}
Aggregations