Fix Int52, add tests
This commit is contained in:
parent
2dfd2cc86e
commit
b19b322cfd
23
pom.xml
23
pom.xml
@ -52,6 +52,17 @@
|
||||
<artifactId>maven-plugin-plugin</artifactId>
|
||||
<version>3.6.0</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>3.0.0-M5</version>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-engine</artifactId>
|
||||
<version>5.7.0</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
@ -130,5 +141,17 @@
|
||||
<artifactId>log4j-slf4j-impl</artifactId>
|
||||
<version>2.12.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-api</artifactId>
|
||||
<version>5.7.0</version>
|
||||
<scope>test</scope>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>org.hamcrest</groupId>
|
||||
<artifactId>hamcrest-core</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
@ -12,22 +12,42 @@ public class Int52Serializer implements DataSerializer<Int52> {
|
||||
|
||||
@Override
|
||||
public void serialize(DataOutput dataOutput, @NotNull Int52 data) throws IOException {
|
||||
dataOutput.write(toByteArray(data.getValue()));
|
||||
serializeValue(dataOutput, data);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Int52 deserialize(DataInput dataInput) throws IOException {
|
||||
byte[] bytes = new byte[7];
|
||||
dataInput.readFully(bytes);
|
||||
return Int52.fromLong(fromByteArray(bytes));
|
||||
return deserializeValue(dataInput);
|
||||
}
|
||||
|
||||
public static void serializeValue(DataOutput dataOutput, @NotNull Int52 data) throws IOException {
|
||||
long value = data.getValue();
|
||||
|
||||
for(int i = 0; i < 7; i++) {
|
||||
dataOutput.writeByte(((int)((value >> (6 - i) * 8) & (i == 0 ? 0b00001111L : 255L))));
|
||||
}
|
||||
}
|
||||
|
||||
public static Int52 deserializeValue(DataInput dataInput) throws IOException {
|
||||
long value = 0;
|
||||
|
||||
return Int52.fromLong(
|
||||
((long) dataInput.readUnsignedByte() & 0b00001111) << 48
|
||||
| ((long) dataInput.readUnsignedByte()) << 40
|
||||
| ((long) dataInput.readUnsignedByte() << 32)
|
||||
| ((long) dataInput.readUnsignedByte() << 24)
|
||||
| ((long) dataInput.readUnsignedByte()) << 16
|
||||
| ((long) dataInput.readUnsignedByte()) << 8
|
||||
| ((long) dataInput.readUnsignedByte())
|
||||
);
|
||||
}
|
||||
|
||||
public static byte[] toByteArray(long value) {
|
||||
byte[] result = new byte[7];
|
||||
|
||||
for(int i = 6; i >= 0; --i) {
|
||||
result[i] = (byte)((int)(value & (i == 6 ? 0b00001111L : 255L)));
|
||||
result[i] = (byte)((int)(value & (i == 0 ? 0b00001111L : 255L)));
|
||||
value >>= 8;
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,69 @@
|
||||
package it.cavallium.data.generator.nativedata;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.DataInputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class TestInt52Serializer {
|
||||
|
||||
@Test
|
||||
public void testInt52Serialization() throws IOException {
|
||||
for (int i = 0; i <= 300; i++) {
|
||||
testInt52Serialization(i);
|
||||
}
|
||||
testInt52Serialization(0xF_FF_FF_FF_FF_FF_FFL);
|
||||
}
|
||||
|
||||
public void testInt52Serialization(long n) throws IOException {
|
||||
var serializer = new Int52Serializer();
|
||||
byte[] out;
|
||||
try (var baos = new ByteArrayOutputStream()) {
|
||||
try (var dos = new DataOutputStream(baos)) {
|
||||
serializer.serialize(dos, Int52.fromLong(n));
|
||||
}
|
||||
out = baos.toByteArray();
|
||||
}
|
||||
|
||||
var bais = new ByteArrayInputStream(out);
|
||||
var dis = new DataInputStream(bais);
|
||||
Assertions.assertEquals(n, serializer.deserialize(dis).longValue(), "Deserialized number differ");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInt52OptionalSerialization() throws IOException {
|
||||
testInt52OptionalSerialization(null);
|
||||
for (long i = 0; i <= 300; i++) {
|
||||
testInt52OptionalSerialization(i);
|
||||
}
|
||||
testInt52OptionalSerialization(0xF_FF_FF_FF_FF_FF_FFL);
|
||||
}
|
||||
|
||||
public void testInt52OptionalSerialization(@Nullable Long n) throws IOException {
|
||||
var serializer = new NullableInt52Serializer();
|
||||
byte[] out;
|
||||
try (var baos = new ByteArrayOutputStream()) {
|
||||
try (var dos = new DataOutputStream(baos)) {
|
||||
if (n == null) {
|
||||
serializer.serialize(dos, NullableInt52.empty());
|
||||
} else {
|
||||
serializer.serialize(dos, NullableInt52.of(Int52.fromLong(n)));
|
||||
}
|
||||
}
|
||||
out = baos.toByteArray();
|
||||
}
|
||||
|
||||
var bais = new ByteArrayInputStream(out);
|
||||
var dis = new DataInputStream(bais);
|
||||
if (n == null) {
|
||||
Assertions.assertNull(serializer.deserialize(dis).getNullable(), "Deserialized number is not empty");
|
||||
} else {
|
||||
Assertions.assertEquals(n, serializer.deserialize(dis).get().longValue(), "Deserialized number differ");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user