11package io .github .dfa1 .zstd ;
22
3- import static java .lang .foreign .ValueLayout .JAVA_BYTE ;
3+ import static io .github .dfa1 .zstd .ZstdTestSupport .bytesOf ;
4+ import static io .github .dfa1 .zstd .ZstdTestSupport .segmentOf ;
45import static org .assertj .core .api .Assertions .assertThat ;
56import static org .assertj .core .api .Assertions .assertThatThrownBy ;
67
@@ -21,8 +22,8 @@ void roundTripsWithMatchingPrefix() {
2122 try (Arena arena = Arena .ofConfined ();
2223 ZstdCompressCtx cctx = new ZstdCompressCtx ();
2324 ZstdDecompressCtx dctx = new ZstdDecompressCtx ()) {
24- MemorySegment prefix = copy (arena , prefixBytes );
25- MemorySegment src = copy (arena , dataBytes );
25+ MemorySegment prefix = segmentOf (arena , prefixBytes );
26+ MemorySegment src = segmentOf (arena , dataBytes );
2627 MemorySegment frame = arena .allocate (Zstd .compressBound (dataBytes .length ));
2728 MemorySegment out = arena .allocate (dataBytes .length );
2829
@@ -34,7 +35,7 @@ void roundTripsWithMatchingPrefix() {
3435
3536 // Then
3637 assertThat (m ).isEqualTo (dataBytes .length );
37- assertThat (bytes (out , (int ) m )).isEqualTo (dataBytes );
38+ assertThat (bytesOf (out , (int ) m )).isEqualTo (dataBytes );
3839 }
3940 }
4041
@@ -47,8 +48,8 @@ void prefixIsAppliedAndRequiredToDecode() {
4748 // for random content.)
4849 byte [] random = randomBytes (0xBEEF , 16384 );
4950 try (Arena arena = Arena .ofConfined ()) {
50- MemorySegment prefix = copy (arena , random );
51- MemorySegment src = copy (arena , random );
51+ MemorySegment prefix = segmentOf (arena , random );
52+ MemorySegment src = segmentOf (arena , random );
5253
5354 long baseline ;
5455 try (ZstdCompressCtx cctx = new ZstdCompressCtx ().level (19 )) {
@@ -58,7 +59,7 @@ void prefixIsAppliedAndRequiredToDecode() {
5859 try (ZstdCompressCtx cctx = new ZstdCompressCtx ().level (19 )) {
5960 cctx .refPrefix (prefix );
6061 MemorySegment f = cctx .compress (arena , src );
61- frame = bytes (f , (int ) f .byteSize ());
62+ frame = bytesOf (f , (int ) f .byteSize ());
6263 }
6364
6465 // Then — the prefix slashes the frame (16 KiB random → tens of bytes)
@@ -68,16 +69,16 @@ void prefixIsAppliedAndRequiredToDecode() {
6869 try (ZstdDecompressCtx dctx = new ZstdDecompressCtx ()) {
6970 MemorySegment out = arena .allocate (random .length );
7071 dctx .refPrefix (prefix );
71- long m = dctx .decompress (out , copy (arena , frame ));
72- assertThat (bytes (out , (int ) m )).isEqualTo (random );
72+ long m = dctx .decompress (out , segmentOf (arena , frame ));
73+ assertThat (bytesOf (out , (int ) m )).isEqualTo (random );
7374 }
7475
7576 // But — it cannot be recovered without the prefix
7677 boolean reproduced ;
7778 try (ZstdDecompressCtx dctx = new ZstdDecompressCtx ()) {
7879 MemorySegment out = arena .allocate (random .length );
79- long m = dctx .decompress (out , copy (arena , frame ));
80- reproduced = Arrays .equals (bytes (out , (int ) m ), random );
80+ long m = dctx .decompress (out , segmentOf (arena , frame ));
81+ reproduced = Arrays .equals (bytesOf (out , (int ) m ), random );
8182 } catch (ZstdException e ) {
8283 reproduced = false ;
8384 }
@@ -91,8 +92,8 @@ void clearingPrefixWithNullCompressesPlainly() {
9192 byte [] dataBytes = "the quick brown fox" .getBytes ();
9293 try (Arena arena = Arena .ofConfined ();
9394 ZstdCompressCtx cctx = new ZstdCompressCtx ()) {
94- MemorySegment prefix = copy (arena , "a prior version of the text" .getBytes ());
95- MemorySegment src = copy (arena , dataBytes );
95+ MemorySegment prefix = segmentOf (arena , "a prior version of the text" .getBytes ());
96+ MemorySegment src = segmentOf (arena , dataBytes );
9697 MemorySegment frame = arena .allocate (Zstd .compressBound (dataBytes .length ));
9798
9899 // When — set then clear the prefix before compressing
@@ -105,7 +106,7 @@ void clearingPrefixWithNullCompressesPlainly() {
105106 try (ZstdDecompressCtx dctx = new ZstdDecompressCtx ()) {
106107 MemorySegment out = arena .allocate (dataBytes .length );
107108 long m = dctx .decompress (out , frame .asSlice (0 , n ));
108- restored = bytes (out , (int ) m );
109+ restored = bytesOf (out , (int ) m );
109110 }
110111 assertThat (restored ).isEqualTo (dataBytes );
111112 }
@@ -118,8 +119,8 @@ void prefixIsSingleUseAndDoesNotStickAcrossFrames() {
118119 byte [] random = randomBytes (0xCAFE , 16384 );
119120 try (Arena arena = Arena .ofConfined ();
120121 ZstdCompressCtx cctx = new ZstdCompressCtx ().level (19 )) {
121- MemorySegment prefix = copy (arena , random );
122- MemorySegment src = copy (arena , random );
122+ MemorySegment prefix = segmentOf (arena , random );
123+ MemorySegment src = segmentOf (arena , random );
123124 MemorySegment first = arena .allocate (Zstd .compressBound (random .length ));
124125 MemorySegment second = arena .allocate (Zstd .compressBound (random .length ));
125126
@@ -137,7 +138,7 @@ void prefixIsSingleUseAndDoesNotStickAcrossFrames() {
137138 try (ZstdDecompressCtx dctx = new ZstdDecompressCtx ()) {
138139 MemorySegment out = arena .allocate (random .length );
139140 long m = dctx .decompress (out , second .asSlice (0 , n2 ));
140- restored = bytes (out , (int ) m );
141+ restored = bytesOf (out , (int ) m );
141142 }
142143 assertThat (restored ).isEqualTo (random );
143144 }
@@ -221,7 +222,7 @@ void refPrefixReturnsTheSameContextForChaining() {
221222 try (Arena arena = Arena .ofConfined ();
222223 ZstdCompressCtx cctx = new ZstdCompressCtx ();
223224 ZstdDecompressCtx dctx = new ZstdDecompressCtx ()) {
224- MemorySegment prefix = copy (arena , "a prior version" .getBytes ());
225+ MemorySegment prefix = segmentOf (arena , "a prior version" .getBytes ());
225226
226227 // When setting and then clearing a prefix on both contexts
227228 ZstdCompressCtx cSet = cctx .refPrefix (prefix );
@@ -243,7 +244,7 @@ void loadDictionaryFromSegmentReturnsTheSameContextForChaining() {
243244 try (Arena arena = Arena .ofConfined ();
244245 ZstdCompressCtx cctx = new ZstdCompressCtx ();
245246 ZstdDecompressCtx dctx = new ZstdDecompressCtx ()) {
246- MemorySegment dict = copy (arena , "dictionary sample payload " .repeat (64 ).getBytes ());
247+ MemorySegment dict = segmentOf (arena , "dictionary sample payload " .repeat (64 ).getBytes ());
247248
248249 // When loading and then clearing a segment dictionary on both contexts
249250 ZstdCompressCtx cSet = cctx .loadDictionary (dict );
@@ -259,18 +260,6 @@ void loadDictionaryFromSegmentReturnsTheSameContextForChaining() {
259260 }
260261 }
261262
262- private static MemorySegment copy (Arena arena , byte [] src ) {
263- MemorySegment seg = arena .allocate (src .length );
264- MemorySegment .copy (src , 0 , seg , JAVA_BYTE , 0 , src .length );
265- return seg ;
266- }
267-
268- private static byte [] bytes (MemorySegment seg , int len ) {
269- byte [] out = new byte [len ];
270- MemorySegment .copy (seg , JAVA_BYTE , 0 , out , 0 , len );
271- return out ;
272- }
273-
274263 private static byte [] randomBytes (long seed , int n ) {
275264 byte [] b = new byte [n ];
276265 new Random (seed ).nextBytes (b );
0 commit comments