-
Notifications
You must be signed in to change notification settings - Fork 140
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Max Lepikhin <[email protected]>
- Loading branch information
1 parent
25df76e
commit 78a7c88
Showing
1 changed file
with
62 additions
and
0 deletions.
There are no files selected for viewing
62 changes: 62 additions & 0 deletions
62
ml-algorithms/src/test/java/org/opensearch/ml/engine/utils/FileUtilsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.ml.engine.utils; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
import java.io.File; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Random; | ||
|
||
import org.junit.After; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.rules.TemporaryFolder; | ||
|
||
public class FileUtilsTest { | ||
private final TemporaryFolder tempDir = new TemporaryFolder(); | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
tempDir.create(); | ||
} | ||
|
||
@After | ||
public void tearUp() { | ||
tempDir.delete(); | ||
} | ||
|
||
@Test | ||
public void testSplitFileIntoChunks() throws Exception { | ||
// Write file. | ||
Random random = new Random(); | ||
File file = tempDir.newFile("large_file"); | ||
byte[] data = new byte[1017]; | ||
random.nextBytes(data); | ||
Files.write(file.toPath(), data); | ||
|
||
// Split file into chunks. | ||
int chunkSize = 325; | ||
List<String> chunkPaths = FileUtils | ||
.splitFileIntoChunks(file, tempDir.newFolder().toPath(), chunkSize); | ||
|
||
// Verify. | ||
int currentPosition = 0; | ||
for (String chunkPath : chunkPaths) { | ||
byte[] chunk = Files.readAllBytes(Path.of(chunkPath)); | ||
assertTrue("Chunk size", currentPosition + chunk.length <= data.length); | ||
Assert.assertArrayEquals(Arrays | ||
.copyOfRange(data, currentPosition, currentPosition + chunk.length), chunk); | ||
currentPosition += chunk.length; | ||
} | ||
assertEquals(currentPosition, data.length); | ||
} | ||
} |