52 lines
1.6 KiB
Java
52 lines
1.6 KiB
Java
package org.warp.jcwdb;
|
|
|
|
import org.junit.Test;
|
|
|
|
import java.io.IOException;
|
|
import java.nio.ByteBuffer;
|
|
import java.nio.channels.SeekableByteChannel;
|
|
import java.nio.file.Files;
|
|
import java.nio.file.Path;
|
|
import java.nio.file.StandardOpenOption;
|
|
|
|
import static org.junit.Assert.assertEquals;
|
|
import static org.junit.Assert.assertTrue;
|
|
|
|
public class FileAllocatorTest {
|
|
|
|
@Test
|
|
public void shouldAllocateAtZero() throws IOException {
|
|
Path tempFile = Files.createTempFile("", "");
|
|
SeekableByteChannel byteCh = Files.newByteChannel(tempFile);
|
|
FileAllocator allocator = new FileAllocator(byteCh);
|
|
long offset1 = allocator.allocate(512);
|
|
assertEquals(0, offset1);
|
|
}
|
|
|
|
@Test
|
|
public void shouldAllocateAt512() throws IOException {
|
|
Path tempFile = Files.createTempFile("", "");
|
|
SeekableByteChannel byteCh = Files.newByteChannel(tempFile, StandardOpenOption.WRITE);
|
|
byteCh.write(ByteBuffer.wrap(new byte[512]));
|
|
FileAllocator allocator = new FileAllocator(byteCh);
|
|
long offset1 = allocator.allocate(512);
|
|
assertEquals(512, offset1);
|
|
}
|
|
|
|
@Test
|
|
public void shouldAllocateUnusedSpace() throws IOException {
|
|
Path tempFile = Files.createTempFile("", "");
|
|
SeekableByteChannel byteCh = Files.newByteChannel(tempFile, StandardOpenOption.WRITE);
|
|
FileAllocator allocator = new FileAllocator(byteCh);
|
|
long offset1 = allocator.allocate(512);
|
|
allocator.markFree(offset1, 512);
|
|
long offset2 = allocator.allocate(128);
|
|
long offset3 = allocator.allocate(512-128);
|
|
long offset4 = allocator.allocate(128);
|
|
assertEquals(0, offset2);
|
|
assertEquals(128, offset3);
|
|
assertEquals(512, offset4);
|
|
}
|
|
|
|
}
|