|
| 1 | +package io.github.dfa1.vortex.encoding; |
| 2 | + |
| 3 | +import dev.vortex.proto.ScalarProtos; |
| 4 | +import io.github.dfa1.vortex.core.DType; |
| 5 | +import io.github.dfa1.vortex.core.PType; |
| 6 | +import org.junit.jupiter.api.Test; |
| 7 | +import org.junit.jupiter.params.ParameterizedTest; |
| 8 | +import org.junit.jupiter.params.provider.ValueSource; |
| 9 | + |
| 10 | +import java.lang.foreign.MemorySegment; |
| 11 | +import java.lang.foreign.ValueLayout; |
| 12 | +import java.nio.ByteBuffer; |
| 13 | +import java.nio.ByteOrder; |
| 14 | + |
| 15 | +import static org.assertj.core.api.Assertions.assertThat; |
| 16 | + |
| 17 | +class FrameOfReferenceCodecTest { |
| 18 | + |
| 19 | + private static final DType I64_DTYPE = new DType.Primitive(PType.I64, false); |
| 20 | + private static final DType I32_DTYPE = new DType.Primitive(PType.I32, false); |
| 21 | + |
| 22 | + @Test |
| 23 | + void decode_i64_addsReferenceToResiduals() { |
| 24 | + // Given |
| 25 | + long reference = 1000L; |
| 26 | + long[] residuals = {0, 1, 2, 3, 4}; |
| 27 | + long[] expected = {1000, 1001, 1002, 1003, 1004}; |
| 28 | + |
| 29 | + DecodeContext ctx = buildForContext(I64_DTYPE, reference, residuals, PType.I64); |
| 30 | + FrameOfReferenceCodec sut = new FrameOfReferenceCodec(); |
| 31 | + |
| 32 | + // When |
| 33 | + Array result = sut.decode(ctx); |
| 34 | + |
| 35 | + // Then |
| 36 | + assertThat(result.length()).isEqualTo(residuals.length); |
| 37 | + var layout = ValueLayout.JAVA_LONG_UNALIGNED.withOrder(ByteOrder.LITTLE_ENDIAN); |
| 38 | + for (int i = 0; i < expected.length; i++) { |
| 39 | + assertThat(result.buffer(0).get(layout, (long) i * 8)) |
| 40 | + .as("index %d", i) |
| 41 | + .isEqualTo(expected[i]); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + void decode_i32_addsReferenceToResiduals() { |
| 47 | + // Given |
| 48 | + long reference = -100L; |
| 49 | + long[] residuals = {0, 5, 10, 15}; |
| 50 | + int[] expected = {-100, -95, -90, -85}; |
| 51 | + |
| 52 | + DecodeContext ctx = buildForContext(I32_DTYPE, reference, residuals, PType.I32); |
| 53 | + FrameOfReferenceCodec sut = new FrameOfReferenceCodec(); |
| 54 | + |
| 55 | + // When |
| 56 | + Array result = sut.decode(ctx); |
| 57 | + |
| 58 | + // Then |
| 59 | + assertThat(result.length()).isEqualTo(residuals.length); |
| 60 | + var layout = ValueLayout.JAVA_INT_UNALIGNED.withOrder(ByteOrder.LITTLE_ENDIAN); |
| 61 | + for (int i = 0; i < expected.length; i++) { |
| 62 | + assertThat(result.buffer(0).get(layout, (long) i * 4)) |
| 63 | + .as("index %d", i) |
| 64 | + .isEqualTo(expected[i]); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + void decode_zeroReference_returnsChildUnchanged() { |
| 70 | + // Given — reference == 0, should skip the add entirely |
| 71 | + long[] residuals = {7, 8, 9}; |
| 72 | + DecodeContext ctx = buildForContext(I64_DTYPE, 0L, residuals, PType.I64); |
| 73 | + FrameOfReferenceCodec sut = new FrameOfReferenceCodec(); |
| 74 | + |
| 75 | + // When |
| 76 | + Array result = sut.decode(ctx); |
| 77 | + |
| 78 | + // Then — values unchanged |
| 79 | + var layout = ValueLayout.JAVA_LONG_UNALIGNED.withOrder(ByteOrder.LITTLE_ENDIAN); |
| 80 | + for (int i = 0; i < residuals.length; i++) { |
| 81 | + assertThat(result.buffer(0).get(layout, (long) i * 8)).isEqualTo(residuals[i]); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + @ParameterizedTest |
| 86 | + @ValueSource(longs = {Long.MIN_VALUE, Long.MAX_VALUE, -1L, 1L}) |
| 87 | + void decode_wrappingAdd_i64(long reference) { |
| 88 | + // Given — wrapping arithmetic: MAX + 1 wraps to MIN |
| 89 | + long[] residuals = {1L}; |
| 90 | + DecodeContext ctx = buildForContext(I64_DTYPE, reference, residuals, PType.I64); |
| 91 | + FrameOfReferenceCodec sut = new FrameOfReferenceCodec(); |
| 92 | + |
| 93 | + // When |
| 94 | + Array result = sut.decode(ctx); |
| 95 | + |
| 96 | + // Then |
| 97 | + var layout = ValueLayout.JAVA_LONG_UNALIGNED.withOrder(ByteOrder.LITTLE_ENDIAN); |
| 98 | + long got = result.buffer(0).get(layout, 0L); |
| 99 | + assertThat(got).isEqualTo(residuals[0] + reference); |
| 100 | + } |
| 101 | + |
| 102 | + // ── Helpers ─────────────────────────────────────────────────────────────── |
| 103 | + |
| 104 | + private static DecodeContext buildForContext( |
| 105 | + DType dtype, long reference, long[] residuals, PType ptype |
| 106 | + ) { |
| 107 | + // Serialize the reference as a ScalarValue proto |
| 108 | + byte[] metaBytes = ScalarProtos.ScalarValue.newBuilder() |
| 109 | + .setInt64Value(reference) |
| 110 | + .build() |
| 111 | + .toByteArray(); |
| 112 | + |
| 113 | + // Build the child primitive buffer |
| 114 | + int elemBytes = ptype.byteSize(); |
| 115 | + byte[] childBytes = new byte[residuals.length * elemBytes]; |
| 116 | + ByteBuffer bb = ByteBuffer.wrap(childBytes).order(ByteOrder.LITTLE_ENDIAN); |
| 117 | + for (long v : residuals) { |
| 118 | + switch (ptype) { |
| 119 | + case I32, U32 -> bb.putInt((int) v); |
| 120 | + case I64, U64 -> bb.putLong(v); |
| 121 | + default -> throw new UnsupportedOperationException(ptype.name()); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + // ArrayNode for vortex.primitive child: bufferIndices=[0], no children |
| 126 | + ArrayNode childNode = new ArrayNode( |
| 127 | + "vortex.primitive", |
| 128 | + null, |
| 129 | + new ArrayNode[0], |
| 130 | + new int[]{0}, |
| 131 | + ArrayStats.empty() |
| 132 | + ); |
| 133 | + |
| 134 | + // ArrayNode for fastlanes.for: metadata=scalarBytes, child=childNode, no buffers |
| 135 | + ArrayNode forNode = new ArrayNode( |
| 136 | + "fastlanes.for", |
| 137 | + ByteBuffer.wrap(metaBytes), |
| 138 | + new ArrayNode[]{childNode}, |
| 139 | + new int[0], |
| 140 | + ArrayStats.empty() |
| 141 | + ); |
| 142 | + |
| 143 | + MemorySegment[] segments = {MemorySegment.ofArray(childBytes)}; |
| 144 | + |
| 145 | + DecoderRegistry registry = DecoderRegistry.empty(); |
| 146 | + registry.register(new FrameOfReferenceCodec()); |
| 147 | + registry.register(new PrimitiveCodec()); |
| 148 | + |
| 149 | + return new DecodeContext(forNode, dtype, residuals.length, segments, registry); |
| 150 | + } |
| 151 | +} |
0 commit comments