Skip to content

Commit b05ee34

Browse files
dfa1claude
andcommitted
test: hoist shared helpers into ZstdTestSupport
- merge RandomArrays (bytes/levels generators) into ZstdTestSupport - centralize sample(int), randomBytes(seed,n); drop per-class copies - replace manual arena.allocate()+copy with segmentOf() Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
1 parent 592928e commit b05ee34

9 files changed

Lines changed: 96 additions & 128 deletions

File tree

zstd/src/test/java/io/github/dfa1/zstd/RandomArrays.java

Lines changed: 0 additions & 56 deletions
This file was deleted.

zstd/src/test/java/io/github/dfa1/zstd/RefPrefixTest.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
package io.github.dfa1.zstd;
22

33
import static io.github.dfa1.zstd.ZstdTestSupport.bytesOf;
4+
import static io.github.dfa1.zstd.ZstdTestSupport.randomBytes;
45
import static io.github.dfa1.zstd.ZstdTestSupport.segmentOf;
56
import static org.assertj.core.api.Assertions.assertThat;
67
import static org.assertj.core.api.Assertions.assertThatThrownBy;
78

89
import java.lang.foreign.Arena;
910
import java.lang.foreign.MemorySegment;
1011
import java.util.Arrays;
11-
import java.util.Random;
1212
import org.assertj.core.api.ThrowableAssert.ThrowingCallable;
1313
import org.junit.jupiter.api.Test;
1414

@@ -260,9 +260,4 @@ void loadDictionaryFromSegmentReturnsTheSameContextForChaining() {
260260
}
261261
}
262262

263-
private static byte[] randomBytes(long seed, int n) {
264-
byte[] b = new byte[n];
265-
new Random(seed).nextBytes(b);
266-
return b;
267-
}
268263
}

zstd/src/test/java/io/github/dfa1/zstd/ZstdDictionaryTest.java

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@
1010

1111
import java.lang.foreign.Arena;
1212
import java.lang.foreign.MemorySegment;
13-
import java.lang.foreign.ValueLayout;
1413
import java.nio.charset.StandardCharsets;
1514
import java.util.ArrayList;
1615
import java.util.List;
1716

17+
import static io.github.dfa1.zstd.ZstdTestSupport.sample;
18+
import static io.github.dfa1.zstd.ZstdTestSupport.segmentOf;
1819
import static org.assertj.core.api.Assertions.assertThat;
1920
import static org.assertj.core.api.Assertions.assertThatThrownBy;
2021

@@ -356,8 +357,8 @@ void roundTripsViaSegmentBuiltCDictAndDDict() {
356357
try (Arena arena = Arena.ofConfined();
357358
ZstdCompressCtx cctx = new ZstdCompressCtx();
358359
ZstdDecompressCtx dctx = new ZstdDecompressCtx();
359-
ZstdCompressDict cdict = new ZstdCompressDict(nativeDict(arena, raw), 19);
360-
ZstdDecompressDict ddict = new ZstdDecompressDict(nativeDict(arena, raw))) {
360+
ZstdCompressDict cdict = new ZstdCompressDict(segmentOf(arena, raw), 19);
361+
ZstdDecompressDict ddict = new ZstdDecompressDict(segmentOf(arena, raw))) {
361362

362363
// When round-tripped through the segment-built dictionaries
363364
byte[] frame = cctx.compress(sample, cdict);
@@ -377,8 +378,8 @@ void segmentBuiltDictionariesReportSameIdAsHeap() {
377378

378379
// Then they carry the same id as the heap-built dictionary
379380
try (Arena arena = Arena.ofConfined();
380-
ZstdCompressDict cdict = new ZstdCompressDict(nativeDict(arena, raw));
381-
ZstdDecompressDict ddict = new ZstdDecompressDict(nativeDict(arena, raw))) {
381+
ZstdCompressDict cdict = new ZstdCompressDict(segmentOf(arena, raw));
382+
ZstdDecompressDict ddict = new ZstdDecompressDict(segmentOf(arena, raw))) {
382383
assertThat(cdict.id()).isEqualTo(sut.id());
383384
assertThat(ddict.id()).isEqualTo(sut.id());
384385
}
@@ -394,7 +395,7 @@ void interoperatesWithHeapBuiltDictionaries() {
394395
try (Arena arena = Arena.ofConfined();
395396
ZstdCompressCtx cctx = new ZstdCompressCtx();
396397
ZstdDecompressCtx dctx = new ZstdDecompressCtx();
397-
ZstdCompressDict cdict = new ZstdCompressDict(nativeDict(arena, raw));
398+
ZstdCompressDict cdict = new ZstdCompressDict(segmentOf(arena, raw));
398399
ZstdDecompressDict ddict = new ZstdDecompressDict(sut)) {
399400
byte[] frame = cctx.compress(sample, cdict);
400401

@@ -534,9 +535,9 @@ void loadsDictionaryFromNativeSegmentWithoutHeapCopy() {
534535
try (Arena arena = Arena.ofConfined();
535536
ZstdCompressCtx cctx = new ZstdCompressCtx();
536537
ZstdDecompressCtx dctx = new ZstdDecompressCtx()) {
537-
cctx.loadDictionary(nativeDict(arena, raw));
538+
cctx.loadDictionary(segmentOf(arena, raw));
538539
byte[] frame = cctx.compress(sample);
539-
dctx.loadDictionary(nativeDict(arena, raw));
540+
dctx.loadDictionary(segmentOf(arena, raw));
540541
restored = dctx.decompress(frame, sample.length);
541542
}
542543

@@ -586,7 +587,7 @@ void loadAndRefReturnTheSameCompressContext() {
586587

587588
// Then every sticky-dictionary call returns the same context, for chaining
588589
assertThat(cctx.loadDictionary(sut)).isSameAs(cctx);
589-
assertThat(cctx.loadDictionary(nativeDict(arena, sut.toByteArray()))).isSameAs(cctx);
590+
assertThat(cctx.loadDictionary(segmentOf(arena, sut.toByteArray()))).isSameAs(cctx);
590591
assertThat(cctx.loadDictionary((ZstdDictionary) null)).isSameAs(cctx);
591592
assertThat(cctx.refDictionary(cdict)).isSameAs(cctx);
592593
assertThat(cctx.refDictionary(null)).isSameAs(cctx);
@@ -602,25 +603,12 @@ void loadAndRefReturnTheSameDecompressContext() {
602603

603604
// Then every sticky-dictionary call returns the same context, for chaining
604605
assertThat(dctx.loadDictionary(sut)).isSameAs(dctx);
605-
assertThat(dctx.loadDictionary(nativeDict(arena, sut.toByteArray()))).isSameAs(dctx);
606+
assertThat(dctx.loadDictionary(segmentOf(arena, sut.toByteArray()))).isSameAs(dctx);
606607
assertThat(dctx.loadDictionary((ZstdDictionary) null)).isSameAs(dctx);
607608
assertThat(dctx.refDictionary(ddict)).isSameAs(dctx);
608609
assertThat(dctx.refDictionary(null)).isSameAs(dctx);
609610
}
610611
}
611612
}
612613

613-
private static byte[] sample(int i) {
614-
return ("{\"id\":" + i
615-
+ ",\"user\":\"user_" + (i % 50)
616-
+ "\",\"active\":" + (i % 2 == 0)
617-
+ ",\"score\":" + (i * 7 % 1000)
618-
+ ",\"tag\":\"event\"}").getBytes(StandardCharsets.UTF_8);
619-
}
620-
621-
private static MemorySegment nativeDict(Arena arena, byte[] raw) {
622-
MemorySegment seg = arena.allocate(raw.length);
623-
MemorySegment.copy(raw, 0, seg, ValueLayout.JAVA_BYTE, 0, raw.length);
624-
return seg;
625-
}
626614
}

zstd/src/test/java/io/github/dfa1/zstd/ZstdFrameTest.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ void isNoneForDictionarylessFrame() {
382382
@Test
383383
void matchesTheDictionaryUsed() {
384384
// Given a frame compressed with a dictionary
385-
ZstdDictionary dict = trainDict();
385+
ZstdDictionary dict = trainDictionary(3000);
386386
byte[] frame;
387387
try (ZstdCompressCtx ctx = new ZstdCompressCtx()) {
388388
frame = ctx.compress(PAYLOAD, dict);
@@ -395,7 +395,7 @@ void matchesTheDictionaryUsed() {
395395
@Test
396396
void matchesThroughTheSegmentOverload() {
397397
// Given a dictionary frame in a native segment
398-
ZstdDictionary dict = trainDict();
398+
ZstdDictionary dict = trainDictionary(3000);
399399
byte[] frame;
400400
try (ZstdCompressCtx ctx = new ZstdCompressCtx()) {
401401
frame = ctx.compress(PAYLOAD, dict);
@@ -408,9 +408,6 @@ void matchesThroughTheSegmentOverload() {
408408
}
409409
}
410410

411-
private ZstdDictionary trainDict() {
412-
return trainDictionary(3000);
413-
}
414411
}
415412

416413
@Nested

zstd/src/test/java/io/github/dfa1/zstd/ZstdMemoryTest.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ void decompressContextHasSize() {
5757

5858
@Test
5959
void digestedDictionariesHaveSize() {
60-
ZstdDictionary dict = trainDict();
60+
ZstdDictionary dict = trainDictionary(2000);
6161
try (ZstdCompressDict cdict = new ZstdCompressDict(dict);
6262
ZstdDecompressDict ddict = new ZstdDecompressDict(dict)) {
6363
assertThat(cdict.sizeOf()).isPositive();
@@ -74,8 +74,5 @@ void streamsReportContextSize() {
7474
}
7575
}
7676

77-
private ZstdDictionary trainDict() {
78-
return trainDictionary(2000);
79-
}
8077
}
8178
}

zstd/src/test/java/io/github/dfa1/zstd/ZstdSegmentStreamTest.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
import java.lang.foreign.Arena;
88
import java.lang.foreign.MemorySegment;
99
import java.nio.charset.StandardCharsets;
10-
import java.util.Random;
1110

1211
import static io.github.dfa1.zstd.ZstdTestSupport.bytesOf;
12+
import static io.github.dfa1.zstd.ZstdTestSupport.randomBytes;
1313
import static io.github.dfa1.zstd.ZstdTestSupport.segmentOf;
1414
import static io.github.dfa1.zstd.ZstdTestSupport.trainDictionary;
1515
import static org.assertj.core.api.Assertions.assertThat;
@@ -53,8 +53,7 @@ class Chunked {
5353
@Test
5454
void roundTripsThroughTinyBuffers() {
5555
// Given a payload far larger than the streaming buffers
56-
byte[] original = new byte[2 * 1024 * 1024];
57-
new Random(11).nextBytes(original);
56+
byte[] original = randomBytes(11, 2 * 1024 * 1024);
5857

5958
byte[] frame = drive(original, 8 * 1024, true);
6059
byte[] restored = drive(frame, 8 * 1024, false);
@@ -120,7 +119,7 @@ class WithDictionary {
120119

121120
@Test
122121
void roundTripsAgainstDictionary() {
123-
ZstdDictionary dict = trainDict();
122+
ZstdDictionary dict = trainDictionary(3000);
124123
byte[] sample = "{\"id\":1,\"user\":\"u\",\"event\":\"x\"}".getBytes(StandardCharsets.UTF_8);
125124

126125
try (Arena arena = Arena.ofConfined();
@@ -137,8 +136,5 @@ void roundTripsAgainstDictionary() {
137136
}
138137
}
139138

140-
private ZstdDictionary trainDict() {
141-
return trainDictionary(3000);
142-
}
143139
}
144140
}

zstd/src/test/java/io/github/dfa1/zstd/ZstdStreamTest.java

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@
1414
import java.lang.foreign.MemorySegment;
1515
import java.nio.ByteBuffer;
1616
import java.nio.charset.StandardCharsets;
17-
import java.util.Random;
1817

18+
import static io.github.dfa1.zstd.ZstdTestSupport.randomBytes;
19+
import static io.github.dfa1.zstd.ZstdTestSupport.sample;
1920
import static io.github.dfa1.zstd.ZstdTestSupport.trainDictionary;
2021
import static org.assertj.core.api.Assertions.assertThat;
2122
import static org.assertj.core.api.Assertions.assertThatThrownBy;
@@ -29,7 +30,7 @@ class RoundTrip {
2930
@ValueSource(ints = {0, 1, 100, 64 * 1024, 5 * 1024 * 1024})
3031
void preservesPayloadsAcrossBufferBoundaries(int size) throws IOException {
3132
// Given a payload, some far larger than the stream buffers
32-
byte[] original = randomBytes(size);
33+
byte[] original = randomBytes(7, size);
3334

3435
// When streamed through compress then decompress
3536
byte[] restored = streamDecompress(streamCompress(original, 3));
@@ -94,7 +95,7 @@ class Dictionary {
9495
@Test
9596
void roundTripsWithDictionary() throws IOException {
9697
// Given a dictionary and a small sample
97-
ZstdDictionary dict = trainDict();
98+
ZstdDictionary dict = trainDictionary(3000);
9899
byte[] sample = sample(42);
99100

100101
// When streamed through compress and decompress with the same dictionary
@@ -113,7 +114,7 @@ void roundTripsWithDictionary() throws IOException {
113114

114115
@Test
115116
void dictionaryShrinksStreamedRecord() throws IOException {
116-
ZstdDictionary dict = trainDict();
117+
ZstdDictionary dict = trainDictionary(3000);
117118
byte[] sample = sample(123);
118119

119120
ByteArrayOutputStream withDict = new ByteArrayOutputStream();
@@ -128,7 +129,7 @@ void dictionaryShrinksStreamedRecord() throws IOException {
128129
@Test
129130
void streamWithDictionaryDecodesWithOneShot() throws IOException {
130131
// Given a dictionary frame produced by the streaming compressor
131-
ZstdDictionary dict = trainDict();
132+
ZstdDictionary dict = trainDictionary(3000);
132133
byte[] sample = sample(7);
133134
ByteArrayOutputStream sink = new ByteArrayOutputStream();
134135
try (ZstdOutputStream zout = new ZstdOutputStream(sink, dict)) {
@@ -141,14 +142,6 @@ void streamWithDictionaryDecodesWithOneShot() throws IOException {
141142
}
142143
}
143144

144-
private ZstdDictionary trainDict() {
145-
return trainDictionary(3000);
146-
}
147-
148-
private byte[] sample(int i) {
149-
return ("{\"id\":" + i + ",\"user\":\"user_" + (i % 40) + "\",\"event\":\"click\"}")
150-
.getBytes(StandardCharsets.UTF_8);
151-
}
152145
}
153146

154147
@Nested
@@ -159,7 +152,7 @@ class Truncation {
159152
void throwsWhenFinalFrameIsCutShort(int bytesDropped) throws IOException {
160153
// Given a valid frame with its tail bytes lopped off (random data so the
161154
// frame stays large enough to drop bytes from)
162-
byte[] original = randomBytes(50_000);
155+
byte[] original = randomBytes(7, 50_000);
163156
byte[] frame = streamCompress(original, 6);
164157
byte[] cut = java.util.Arrays.copyOf(frame, frame.length - bytesDropped);
165158

@@ -415,7 +408,7 @@ void firstSingleByteIsCorrectEvenWhenInputDribblesInOneByteAtATime() throws IOEx
415408
void readsCorrectlyWhenInputArrivesOneByteAtATime(int size) throws IOException {
416409
// Given a frame fed through a source that yields a single byte per read,
417410
// forcing the decoder across many refill/no-progress iterations
418-
byte[] original = randomBytes(size);
411+
byte[] original = randomBytes(7, size);
419412
byte[] frame = streamCompress(original, 3);
420413

421414
// When drained
@@ -504,10 +497,4 @@ private static byte[] streamDecompress(byte[] frame) throws IOException {
504497
return zin.readAllBytes();
505498
}
506499
}
507-
508-
private static byte[] randomBytes(int size) {
509-
byte[] b = new byte[size];
510-
new Random(7).nextBytes(b);
511-
return b;
512-
}
513500
}

zstd/src/test/java/io/github/dfa1/zstd/ZstdTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class ZstdTest {
2020
class RoundTrip {
2121

2222
@ParameterizedTest
23-
@MethodSource("io.github.dfa1.zstd.RandomArrays#bytes")
23+
@MethodSource("io.github.dfa1.zstd.ZstdTestSupport#bytes")
2424
void preservesArbitraryBytes(byte[] original) {
2525
// Given a payload and its compressed frame
2626
byte[] frame = Zstd.compress(original);
@@ -61,7 +61,7 @@ void shrinksCompressibleInput() {
6161
class Levels {
6262

6363
@ParameterizedTest
64-
@MethodSource("io.github.dfa1.zstd.RandomArrays#levels")
64+
@MethodSource("io.github.dfa1.zstd.ZstdTestSupport#levels")
6565
void roundTripAtEveryLevel(int level) {
6666
// Given a payload compressed at the given level
6767
byte[] original = "payload-data-".repeat(500).getBytes(StandardCharsets.UTF_8);

0 commit comments

Comments
 (0)