Skip to content

Commit 821b2ac

Browse files
committed
test-data: Sort shard folders and chunks by the IDs embedded in their name
1 parent 742e79a commit 821b2ac

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

test-data/src/main/java/dev/freya02/discord/zstd/TestChunks.java

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,32 @@
1212
import java.nio.file.Files;
1313
import java.nio.file.Path;
1414
import java.security.AlgorithmParameters;
15-
import java.util.ArrayList;
16-
import java.util.Base64;
17-
import java.util.Collections;
18-
import java.util.List;
15+
import java.util.*;
16+
import java.util.regex.Matcher;
1917
import java.util.regex.Pattern;
2018
import java.util.stream.Stream;
2119

2220
@NullMarked
2321
public class TestChunks {
2422

25-
public static final Pattern shardFolderPattern = Pattern.compile("shard-\\d+");
26-
public static final Pattern decompressedChunkPattern = Pattern.compile("chunk-\\d+\\.bin");
23+
public static final Pattern shardFolderPattern = Pattern.compile("shard-(\\d+)");
24+
public static final Pattern decompressedChunkPattern = Pattern.compile("chunk-(\\d+)\\.bin");
25+
26+
private static final Comparator<Path> shardFolderComparator = Comparator.comparingInt(path -> {
27+
Matcher matcher = shardFolderPattern.matcher(path.getFileName().toString());
28+
if (!matcher.matches()) {
29+
throw new IllegalArgumentException("Invalid shard folder path: " + path);
30+
}
31+
return Integer.parseInt(matcher.group(1));
32+
});
33+
34+
private static final Comparator<Path> decompressedChunkComparator = Comparator.comparingInt(path -> {
35+
Matcher matcher = decompressedChunkPattern.matcher(path.getFileName().toString());
36+
if (!matcher.matches()) {
37+
throw new IllegalArgumentException("Invalid decompressed chunk path: " + path);
38+
}
39+
return Integer.parseInt(matcher.group(1));
40+
});
2741

2842
public static List<List<Chunk>> get() {
2943
final EncryptedTestData encryptedTestData = getEncryptedTestData();
@@ -71,10 +85,10 @@ private static EncryptedTestData getEncryptedTestData() {
7185

7286
List<List<Chunk>> shards = new ArrayList<>();
7387
try (Stream<Path> shardDirStream = Files.walk(chunksDirectory, 1)) {
74-
for (Path shardDir : shardDirStream.filter(TestChunks::isShardFolder).sorted().toList()) {
88+
for (Path shardDir : shardDirStream.filter(TestChunks::isShardFolder).sorted(shardFolderComparator).toList()) {
7589
List<Chunk> chunks = new ArrayList<>();
7690
try (Stream<Path> chunkStream = Files.walk(shardDir, 1)) {
77-
for (Path path : chunkStream.filter(TestChunks::isDecompressedChunk).sorted().toList()) {
91+
for (Path path : chunkStream.filter(TestChunks::isDecompressedChunk).sorted(decompressedChunkComparator).toList()) {
7892
byte[] decompressed = Files.readAllBytes(path);
7993
byte[] zlibCompressed = Files.readAllBytes(path.resolveSibling(path.getFileName().toString() + ".zlib"));
8094
byte[] zstdCompressed = Files.readAllBytes(path.resolveSibling(path.getFileName().toString() + ".zstd"));

0 commit comments

Comments
 (0)