33import io .github .dfa1 .vortex .core .DType ;
44import io .github .dfa1 .vortex .core .PType ;
55import io .github .dfa1 .vortex .core .array .LongArray ;
6+ import org .junit .jupiter .api .Nested ;
67import org .junit .jupiter .api .Test ;
78
89import java .lang .foreign .Arena ;
@@ -17,6 +18,74 @@ class ExtEncodingTest {
1718 private static final ValueLayout .OfLong LE_LONG =
1819 ValueLayout .JAVA_LONG_UNALIGNED .withOrder (ByteOrder .LITTLE_ENDIAN );
1920
21+ @ Nested
22+ class Encode {
23+
24+ @ Test
25+ void accepts_extensionDtype_returnsTrue () {
26+ // Given
27+ DType extDType = new DType .Extension ("vortex.timestamp" ,
28+ new DType .Primitive (PType .I64 , false ), null , false );
29+ var sut = new ExtEncoding ();
30+
31+ // When / Then
32+ assertThat (sut .accepts (extDType )).isTrue ();
33+ }
34+
35+ @ Test
36+ void accepts_primitiveDtype_returnsFalse () {
37+ // Given
38+ var sut = new ExtEncoding ();
39+
40+ // When / Then
41+ assertThat (sut .accepts (new DType .Primitive (PType .I64 , false ))).isFalse ();
42+ }
43+
44+ @ Test
45+ void encode_extensionWrappingI64_roundTrips () {
46+ // Given
47+ long [] data = {100L , 200L , 300L , 400L };
48+ DType storageDType = new DType .Primitive (PType .I64 , false );
49+ DType extDType = new DType .Extension ("vortex.timestamp" , storageDType , null , false );
50+ var sut = new ExtEncoding ();
51+
52+ // When
53+ EncodeResult result = sut .encode (extDType , data );
54+
55+ // Then — root is ext, child is primitive
56+ assertThat (result .rootNode ().encodingId ()).isEqualTo (EncodingId .VORTEX_EXT );
57+ assertThat (result .rootNode ().children ()).hasSize (1 );
58+ assertThat (result .rootNode ().children ()[0 ].encodingId ()).isEqualTo (EncodingId .VORTEX_PRIMITIVE );
59+
60+ // Decode back
61+ EncodingRegistry registry = EncodingRegistry .empty ();
62+ registry .register (new PrimitiveEncoding ());
63+ registry .register (new ExtEncoding ());
64+ ArrayNode rootNode = encodeNodeToArrayNode (result .rootNode ());
65+ DecodeContext ctx = new DecodeContext (
66+ rootNode , extDType , data .length ,
67+ result .buffers ().toArray (MemorySegment []::new ),
68+ registry , Arena .ofAuto ());
69+ var decoded = sut .decode (ctx );
70+
71+ assertThat (decoded ).isInstanceOf (LongArray .class );
72+ for (int i = 0 ; i < data .length ; i ++) {
73+ assertThat (decoded .getLong (i )).isEqualTo (data [i ]);
74+ }
75+ }
76+
77+ private ArrayNode encodeNodeToArrayNode (EncodeNode n ) {
78+ ArrayNode [] children = new ArrayNode [n .children ().length ];
79+ for (int i = 0 ; i < children .length ; i ++) {
80+ children [i ] = encodeNodeToArrayNode (n .children ()[i ]);
81+ }
82+ return new ArrayNode (n .encodingId (), n .metadata (), children , n .bufferIndices (), null );
83+ }
84+ }
85+
86+ @ Nested
87+ class Decode {
88+
2089 @ Test
2190 void decode_extensionWrappingI64_returnsStorageArray () {
2291 // Given
@@ -56,4 +125,5 @@ void decode_extensionWrappingI64_returnsStorageArray() {
56125 assertThat (result .getLong (i )).isEqualTo (values [i ]);
57126 }
58127 }
128+ }
59129}
0 commit comments