|
1 | 1 | package io.github.dfa1.vortex.writer.encode; |
2 | 2 |
|
| 3 | +import io.github.dfa1.vortex.core.DType; |
| 4 | +import io.github.dfa1.vortex.core.PType; |
3 | 5 | import io.github.dfa1.vortex.reader.array.Array; |
4 | 6 | import io.github.dfa1.vortex.encoding.DTypes; |
5 | 7 | import io.github.dfa1.vortex.reader.decode.DecodeContext; |
|
8 | 10 | import io.github.dfa1.vortex.reader.ReadRegistry; |
9 | 11 | import io.github.dfa1.vortex.reader.decode.TestRegistry; |
10 | 12 | import io.github.dfa1.vortex.proto.DeltaMetadata; |
| 13 | +import io.github.dfa1.vortex.proto.ScalarValue; |
11 | 14 | import io.github.dfa1.vortex.reader.decode.DeltaEncodingDecoder; |
12 | 15 | import io.github.dfa1.vortex.reader.decode.PrimitiveEncodingDecoder; |
13 | 16 | import org.junit.jupiter.api.Test; |
14 | 17 | import org.junit.jupiter.params.ParameterizedTest; |
15 | 18 | import org.junit.jupiter.params.provider.Arguments; |
16 | 19 | import org.junit.jupiter.params.provider.MethodSource; |
| 20 | +import org.junit.jupiter.params.provider.ValueSource; |
17 | 21 |
|
18 | 22 | import java.lang.foreign.Arena; |
19 | 23 | import java.lang.foreign.MemorySegment; |
| 24 | +import java.lang.foreign.ValueLayout; |
| 25 | +import java.util.ArrayList; |
| 26 | +import java.util.List; |
| 27 | +import java.util.Random; |
20 | 28 | import java.util.stream.Stream; |
21 | 29 |
|
22 | 30 | import static org.assertj.core.api.Assertions.assertThat; |
@@ -120,4 +128,142 @@ void encode_i64_metadata_deltasLen_isNonZero() throws Exception { |
120 | 128 | // Then |
121 | 129 | assertThat(meta.deltas_len()).isGreaterThan(0); |
122 | 130 | } |
| 131 | + |
| 132 | + // Property test: seeded-random arrays across every accepted integer ptype and a range of sizes. |
| 133 | + // The hand-picked cases above all stay under one FastLanes chunk (1024); the 1024/1025/3000 sizes |
| 134 | + // here exercise the multi-chunk loop, the cross-chunk transpose, and the offset-slice tail — the |
| 135 | + // bulk of the encode/decode logic that small arrays never reach. |
| 136 | + @ParameterizedTest(name = "{0}") |
| 137 | + @MethodSource("randomIntegerArrays") |
| 138 | + void encodeDecode_randomAcrossPtypesAndSizes_isLossless(String name, DType dtype, Object data, int n) { |
| 139 | + // Given |
| 140 | + EncodeResult encoded = ENCODER.encode(dtype, data, EncodeTestHelper.testCtx()); |
| 141 | + DecodeContext ctx = DecodeTestHelper.toDecodeContext(encoded, n, dtype, REGISTRY); |
| 142 | + |
| 143 | + // When |
| 144 | + Array result = DECODER.decode(ctx); |
| 145 | + |
| 146 | + // Then — round-trip reproduces every element's raw bytes exactly |
| 147 | + assertThat(result.length()).isEqualTo(n); |
| 148 | + MemorySegment seg = result.materialize(Arena.ofAuto()); |
| 149 | + PType ptype = ((DType.Primitive) dtype).ptype(); |
| 150 | + for (int i = 0; i < n; i++) { |
| 151 | + long off = (long) i * ptype.byteSize(); |
| 152 | + switch (ptype) { |
| 153 | + case I8, U8 -> assertThat(seg.get(ValueLayout.JAVA_BYTE, off)).as("idx %d", i).isEqualTo(((byte[]) data)[i]); |
| 154 | + case I16, U16 -> assertThat(seg.get(PTypeIO.LE_SHORT, off)).as("idx %d", i).isEqualTo(((short[]) data)[i]); |
| 155 | + case I32, U32 -> assertThat(seg.get(PTypeIO.LE_INT, off)).as("idx %d", i).isEqualTo(((int[]) data)[i]); |
| 156 | + case I64, U64 -> assertThat(seg.get(PTypeIO.LE_LONG, off)).as("idx %d", i).isEqualTo(((long[]) data)[i]); |
| 157 | + default -> throw new AssertionError(ptype); |
| 158 | + } |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + @ParameterizedTest |
| 163 | + @ValueSource(strings = {"I8", "I16", "I32", "I64", "U8", "U16", "U32", "U64"}) |
| 164 | + void accepts_everyIntegerPtype_isTrue(String ptype) { |
| 165 | + // Given / When / Then |
| 166 | + assertThat(ENCODER.accepts(new DType.Primitive(PType.valueOf(ptype), false))).isTrue(); |
| 167 | + assertThat(DECODER.accepts(new DType.Primitive(PType.valueOf(ptype), false))).isTrue(); |
| 168 | + } |
| 169 | + |
| 170 | + @Test |
| 171 | + void accepts_nonIntegerOrNonPrimitive_isFalse() { |
| 172 | + // Given / When / Then — floats and non-primitive dtypes are rejected by both sides |
| 173 | + assertThat(ENCODER.accepts(DTypes.F64)).isFalse(); |
| 174 | + assertThat(ENCODER.accepts(DTypes.UTF8)).isFalse(); |
| 175 | + assertThat(DECODER.accepts(DTypes.F32)).isFalse(); |
| 176 | + assertThat(DECODER.accepts(DTypes.BOOL)).isFalse(); |
| 177 | + } |
| 178 | + |
| 179 | + @Test |
| 180 | + void encode_signedI64_statsCarryMinAndMax() throws Exception { |
| 181 | + // Given — unordered; min/max are interior so a broken scan (negated compare) picks a wrong value |
| 182 | + long[] data = {30L, -10L, 50L, 20L, 40L}; |
| 183 | + |
| 184 | + // When |
| 185 | + EncodeResult result = ENCODER.encode(DTypes.I64, data, EncodeTestHelper.testCtx()); |
| 186 | + |
| 187 | + // Then — signed stats use the int64 scalar field, min/max by signed ordering |
| 188 | + assertThat(result.hasStats()).isTrue(); |
| 189 | + assertThat(scalar(result.statsMin()).int64_value()).isEqualTo(-10L); |
| 190 | + assertThat(scalar(result.statsMax()).int64_value()).isEqualTo(50L); |
| 191 | + } |
| 192 | + |
| 193 | + @Test |
| 194 | + void encode_unsignedU64_statsUseUnsignedOrderingAndField() throws Exception { |
| 195 | + // Given — -1L is the max value under unsigned ordering but the min under signed ordering, so this |
| 196 | + // pins both the unsigned compare (lines 57/60) and the unsigned stats field (isUnsigned/statsBytes) |
| 197 | + long[] data = {1L, -1L, 5L}; |
| 198 | + |
| 199 | + // When |
| 200 | + EncodeResult result = ENCODER.encode(DTypes.U64, data, EncodeTestHelper.testCtx()); |
| 201 | + |
| 202 | + // Then |
| 203 | + assertThat(scalar(result.statsMin()).uint64_value()).isEqualTo(1L); |
| 204 | + assertThat(scalar(result.statsMax()).uint64_value()).isEqualTo(-1L); |
| 205 | + } |
| 206 | + |
| 207 | + @Test |
| 208 | + void encode_empty_hasNoStats() { |
| 209 | + // Given / When — the n>0 guard must suppress stats for an empty array |
| 210 | + EncodeResult result = ENCODER.encode(DTypes.I64, new long[0], EncodeTestHelper.testCtx()); |
| 211 | + |
| 212 | + // Then |
| 213 | + assertThat(result.statsMin()).isNull(); |
| 214 | + assertThat(result.statsMax()).isNull(); |
| 215 | + assertThat(result.hasStats()).isFalse(); |
| 216 | + } |
| 217 | + |
| 218 | + private static ScalarValue scalar(byte[] bytes) throws java.io.IOException { |
| 219 | + MemorySegment seg = MemorySegment.ofArray(bytes); |
| 220 | + return ScalarValue.decode(seg, 0, seg.byteSize()); |
| 221 | + } |
| 222 | + |
| 223 | + private static Stream<Arguments> randomIntegerArrays() { |
| 224 | + Random rng = new Random(0xD317A1L); |
| 225 | + // 0 → empty path; 1/5 → sub-chunk; 1024 → exactly one chunk; 1025/3000 → multi-chunk + tail slice. |
| 226 | + int[] sizes = {0, 1, 5, 1024, 1025, 3000}; |
| 227 | + DType[] dtypes = {DTypes.I8, DTypes.I16, DTypes.I32, DTypes.I64, DTypes.U8, DTypes.U16, DTypes.U32, DTypes.U64}; |
| 228 | + List<Arguments> out = new ArrayList<>(); |
| 229 | + for (DType dtype : dtypes) { |
| 230 | + PType ptype = ((DType.Primitive) dtype).ptype(); |
| 231 | + for (int n : sizes) { |
| 232 | + out.add(Arguments.of(ptype + "/" + n, dtype, randomArray(ptype, n, rng), n)); |
| 233 | + } |
| 234 | + } |
| 235 | + return out.stream(); |
| 236 | + } |
| 237 | + |
| 238 | + private static Object randomArray(PType ptype, int n, Random rng) { |
| 239 | + return switch (ptype) { |
| 240 | + case I8, U8 -> { |
| 241 | + byte[] a = new byte[n]; |
| 242 | + rng.nextBytes(a); |
| 243 | + yield a; |
| 244 | + } |
| 245 | + case I16, U16 -> { |
| 246 | + short[] a = new short[n]; |
| 247 | + for (int i = 0; i < n; i++) { |
| 248 | + a[i] = (short) rng.nextInt(); |
| 249 | + } |
| 250 | + yield a; |
| 251 | + } |
| 252 | + case I32, U32 -> { |
| 253 | + int[] a = new int[n]; |
| 254 | + for (int i = 0; i < n; i++) { |
| 255 | + a[i] = rng.nextInt(); |
| 256 | + } |
| 257 | + yield a; |
| 258 | + } |
| 259 | + case I64, U64 -> { |
| 260 | + long[] a = new long[n]; |
| 261 | + for (int i = 0; i < n; i++) { |
| 262 | + a[i] = rng.nextLong(); |
| 263 | + } |
| 264 | + yield a; |
| 265 | + } |
| 266 | + default -> throw new AssertionError(ptype); |
| 267 | + }; |
| 268 | + } |
123 | 269 | } |
0 commit comments