Add primitive library snapshot codecs
This commit is contained in:
@@ -96,4 +96,69 @@ object LibrarySnapshotCodec {
|
||||
else -> throw IOException("Invalid nullable string marker: $marker")
|
||||
}
|
||||
}
|
||||
|
||||
fun writeInt(output: OutputStream, value: Int) {
|
||||
val dataOutput = DataOutputStream(output)
|
||||
dataOutput.writeInt(value)
|
||||
dataOutput.flush()
|
||||
}
|
||||
|
||||
fun readInt(input: InputStream) = DataInputStream(input).readInt()
|
||||
|
||||
fun writeLong(output: OutputStream, value: Long) {
|
||||
val dataOutput = DataOutputStream(output)
|
||||
dataOutput.writeLong(value)
|
||||
dataOutput.flush()
|
||||
}
|
||||
|
||||
fun readLong(input: InputStream) = DataInputStream(input).readLong()
|
||||
|
||||
fun writeFloat(output: OutputStream, value: Float) {
|
||||
val dataOutput = DataOutputStream(output)
|
||||
dataOutput.writeFloat(value)
|
||||
dataOutput.flush()
|
||||
}
|
||||
|
||||
fun readFloat(input: InputStream) = DataInputStream(input).readFloat()
|
||||
|
||||
fun writeBoolean(output: OutputStream, value: Boolean) {
|
||||
val dataOutput = DataOutputStream(output)
|
||||
dataOutput.writeBoolean(value)
|
||||
dataOutput.flush()
|
||||
}
|
||||
|
||||
fun readBoolean(input: InputStream) = DataInputStream(input).readBoolean()
|
||||
|
||||
fun writeNullableFloat(output: OutputStream, value: Float?) {
|
||||
val dataOutput = DataOutputStream(output)
|
||||
dataOutput.writeByte(if (value == null) 0 else 1)
|
||||
if (value != null) {
|
||||
writeFloat(dataOutput, value)
|
||||
}
|
||||
dataOutput.flush()
|
||||
}
|
||||
|
||||
fun readNullableFloat(input: InputStream): Float? {
|
||||
val marker = DataInputStream(input).readUnsignedByte()
|
||||
return when (marker) {
|
||||
0 -> null
|
||||
1 -> readFloat(input)
|
||||
else -> throw IOException("Invalid nullable float marker: $marker")
|
||||
}
|
||||
}
|
||||
|
||||
fun writeCount(output: OutputStream, value: Int, maximum: Int) {
|
||||
require(maximum >= 0) { "Snapshot collection maximum must be non-negative" }
|
||||
require(value >= 0 && value <= maximum) { "Snapshot collection count is out of bounds" }
|
||||
writeInt(output, value)
|
||||
}
|
||||
|
||||
fun readCount(input: InputStream, maximum: Int): Int {
|
||||
require(maximum >= 0) { "Snapshot collection maximum must be non-negative" }
|
||||
val value = readInt(input)
|
||||
if (value < 0 || value > maximum) {
|
||||
throw IOException("Invalid snapshot collection count: $value")
|
||||
}
|
||||
return value
|
||||
}
|
||||
}
|
||||
|
||||
@@ -146,4 +146,107 @@ class LibrarySnapshotCodecTest {
|
||||
LibrarySnapshotCodec.readNullableString(ByteArrayInputStream(output.toByteArray()))
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun integer_roundTrip_preservesBoundaries() {
|
||||
val output = ByteArrayOutputStream()
|
||||
LibrarySnapshotCodec.writeInt(output, Int.MIN_VALUE)
|
||||
LibrarySnapshotCodec.writeInt(output, Int.MAX_VALUE)
|
||||
LibrarySnapshotCodec.writeLong(output, Long.MIN_VALUE)
|
||||
LibrarySnapshotCodec.writeLong(output, Long.MAX_VALUE)
|
||||
val input = ByteArrayInputStream(output.toByteArray())
|
||||
|
||||
assertEquals(Int.MIN_VALUE, LibrarySnapshotCodec.readInt(input))
|
||||
assertEquals(Int.MAX_VALUE, LibrarySnapshotCodec.readInt(input))
|
||||
assertEquals(Long.MIN_VALUE, LibrarySnapshotCodec.readLong(input))
|
||||
assertEquals(Long.MAX_VALUE, LibrarySnapshotCodec.readLong(input))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun float_roundTrip_preservesValues() {
|
||||
val values =
|
||||
listOf(
|
||||
0.0f,
|
||||
-0.0f,
|
||||
-123.5f,
|
||||
Float.MIN_VALUE,
|
||||
Float.MAX_VALUE,
|
||||
Float.NEGATIVE_INFINITY,
|
||||
Float.POSITIVE_INFINITY,
|
||||
Float.NaN,
|
||||
)
|
||||
val output = ByteArrayOutputStream()
|
||||
values.forEach { LibrarySnapshotCodec.writeFloat(output, it) }
|
||||
val input = ByteArrayInputStream(output.toByteArray())
|
||||
|
||||
val decoded = values.map { LibrarySnapshotCodec.readFloat(input) }
|
||||
|
||||
assertEquals(values, decoded)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun boolean_roundTrip_preservesFalseAndTrue() {
|
||||
val output = ByteArrayOutputStream()
|
||||
LibrarySnapshotCodec.writeBoolean(output, false)
|
||||
LibrarySnapshotCodec.writeBoolean(output, true)
|
||||
val input = ByteArrayInputStream(output.toByteArray())
|
||||
|
||||
assertEquals(false, LibrarySnapshotCodec.readBoolean(input))
|
||||
assertEquals(true, LibrarySnapshotCodec.readBoolean(input))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun nullableFloat_roundTrip_distinguishesNullAndZero() {
|
||||
val values = listOf<Float?>(null, 0.0f, -6.5f)
|
||||
val output = ByteArrayOutputStream()
|
||||
values.forEach { LibrarySnapshotCodec.writeNullableFloat(output, it) }
|
||||
val input = ByteArrayInputStream(output.toByteArray())
|
||||
|
||||
val decoded = values.map { LibrarySnapshotCodec.readNullableFloat(input) }
|
||||
|
||||
assertEquals(values, decoded)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun primitives_truncatedInput_throwEOFException() {
|
||||
assertThrows(EOFException::class.java) {
|
||||
LibrarySnapshotCodec.readInt(ByteArrayInputStream(ByteArray(3)))
|
||||
}
|
||||
assertThrows(EOFException::class.java) {
|
||||
LibrarySnapshotCodec.readLong(ByteArrayInputStream(ByteArray(7)))
|
||||
}
|
||||
assertThrows(EOFException::class.java) {
|
||||
LibrarySnapshotCodec.readFloat(ByteArrayInputStream(ByteArray(3)))
|
||||
}
|
||||
assertThrows(EOFException::class.java) {
|
||||
LibrarySnapshotCodec.readBoolean(ByteArrayInputStream(ByteArray(0)))
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun collectionCount_codec_enforcesCallerMaximum() {
|
||||
val output = ByteArrayOutputStream()
|
||||
LibrarySnapshotCodec.writeCount(output, 3, maximum = 5)
|
||||
assertEquals(
|
||||
3,
|
||||
LibrarySnapshotCodec.readCount(ByteArrayInputStream(output.toByteArray()), maximum = 5),
|
||||
)
|
||||
|
||||
assertThrows(IllegalArgumentException::class.java) {
|
||||
LibrarySnapshotCodec.writeCount(ByteArrayOutputStream(), -1, maximum = 5)
|
||||
}
|
||||
assertThrows(IllegalArgumentException::class.java) {
|
||||
LibrarySnapshotCodec.writeCount(ByteArrayOutputStream(), 6, maximum = 5)
|
||||
}
|
||||
for (count in listOf(-1, 6)) {
|
||||
val invalid = ByteArrayOutputStream()
|
||||
DataOutputStream(invalid).writeInt(count)
|
||||
assertThrows(IOException::class.java) {
|
||||
LibrarySnapshotCodec.readCount(
|
||||
ByteArrayInputStream(invalid.toByteArray()),
|
||||
maximum = 5,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user