<commit_before>package eu.dirk.haase.io.storage.record.header;

import eu.dirk.haase.io.storage.channel.SeekableInMemoryByteChannel;
import org.junit.After;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;

import java.io.File;
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.assertj.core.api.Assertions.assertThat;

/**
 * Created by dhaa on 14.07.17.
 */
@RunWith(BlockJUnit4ClassRunner.class)
public class RecordHeaderTest {

    private SeekableByteChannel channel1;
    private SeekableByteChannel channel2;
    private File file;

    @After
    public void tearDown() throws IOException {
        if (file != null) {
            file.delete();
        }
        if (channel1 != null) {
            channel1.close();
        }
        if (channel2 != null) {
            channel2.close();
        }
        file = null;
        channel1 = null;
        channel2 = null;
    }


    @Test
    public void testRecordHeader_FirstHeader_Values() {
        // ============
        // Given
        RecordHeader firstHeader = new RecordHeader();
        // ============
        // When
        // ============
        // Then
        assertThat(firstHeader.getStartPointer()).isEqualTo(73L);
        assertThat(firstHeader.getStartDataPointer()).isEqualTo(109L);
        assertThat(firstHeader.getEndPointer()).isEqualTo(109L);
        assertThat(firstHeader.getLength()).isEqualTo(36);
        assertThat(firstHeader.getRecordDataCapacity()).isEqualTo(0);
        assertThat(firstHeader.getRecordDataLength()).isEqualTo(0);
        assertThat(firstHeader.getRecordIndex()).isEqualTo(0);
    }


    @Test
    public void testRecordHeader_FirstHeader_Values_With_Data() {
        // ============
        // Given
        RecordHeader firstHeader = new RecordHeader();
        // ============
        // When
        firstHeader.setRecordDataLength(123);
        firstHeader.setRecordDataCapacity(200);
        // ============
        // Then
        assertThat(firstHeader.getStartPointer()).isEqualTo(73L);
        assertThat(firstHeader.getStartDataPointer()).isEqualTo(109L);
        assertThat(firstHeader.getEndPointer()).isEqualTo(109L);
        assertThat(firstHeader.getLength()).isEqualTo(36);
        assertThat(firstHeader.getRecordDataCapacity()).isEqualTo(200);
        assertThat(firstHeader.getRecordDataLength()).isEqualTo(123);
        assertThat(firstHeader.getRecordIndex()).isEqualTo(0);
    }


    @Test
    public void testAdvanceRecordHeader() {
        // ============
        // Given
        RecordHeader firstHeader = new RecordHeader();
        firstHeader.setRecordDataLength(123);
        firstHeader.setRecordDataCapacity(200);
        // ============
        // When
        RecordHeader nextHeader = firstHeader.nextHeader();
        // ============
        // Then
        assertThat(nextHeader.getStartPointer()).isEqualTo(109L);
        assertThat(nextHeader.getStartDataPointer()).isEqualTo(145L);
        assertThat(nextHeader.getEndPointer()).isEqualTo(145L);
        assertThat(nextHeader.getLength()).isEqualTo(36);
        assertThat(nextHeader.getRecordDataCapacity()).isEqualTo(0);
        assertThat(nextHeader.getRecordDataLength()).isEqualTo(0);
        assertThat(nextHeader.getRecordIndex()).isEqualTo(1);
    }

    @Test
    public void testReadWrite_ByteBuffer() {
        // ============
        // Given
        RecordHeader givenHeader = new RecordHeader();
        ByteBuffer buffer = ByteBuffer.allocate(givenHeader.getLength());
        int dataBlockCapacity = 12;
        int occupiedBytes = 45;
        int startDataPointer = 80;
        int startPointer = 78;
        givenHeader.setRecordDataCapacity(dataBlockCapacity);
        givenHeader.setRecordDataLength(occupiedBytes);
        givenHeader.setStartDataPointer(startDataPointer);<commit_msg>intermediate commit<commit_after>package eu.dirk.haase.io.storage.record.header;

import eu.dirk.haase.io.storage.channel.SeekableInMemoryByteChannel;
import org.junit.After;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;

import java.io.File;
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.assertj.core.api.Assertions.assertThat;

/**
 * Created by dhaa on 14.07.17.
 */
@RunWith(BlockJUnit4ClassRunner.class)
public class RecordHeaderTest {

    private SeekableByteChannel channel1;
    private SeekableByteChannel channel2;
    private File file;

    @After
    public void tearDown() throws IOException {
        if (file != null) {
            file.delete();
        }
        if (channel1 != null) {
            channel1.close();
        }
        if (channel2 != null) {
            channel2.close();
        }
        file = null;
        channel1 = null;
        channel2 = null;
    }


    @Test
    public void testRecordHeader_FirstHeader_Values() {
        // ============
        // Given
        RecordHeader firstHeader = new RecordHeader();
        // ============
        // When
        // ============
        // Then
        assertThat(firstHeader.getStartPointer()).isEqualTo(73L);
        assertThat(firstHeader.getStartDataPointer()).isEqualTo(145L);
        assertThat(firstHeader.getEndPointer()).isEqualTo(145L);
        assertThat(firstHeader.getLength()).isEqualTo(72);
        assertThat(firstHeader.getRecordDataCapacity()).isEqualTo(0);
        assertThat(firstHeader.getRecordDataLength()).isEqualTo(0);
        assertThat(firstHeader.getRecordIndex()).isEqualTo(0);
    }


    @Test
    public void testRecordHeader_FirstHeader_Values_With_Data() {
        // ============
        // Given
        RecordHeader firstHeader = new RecordHeader();
        // ============
        // When
        firstHeader.setRecordDataLength(123);
        firstHeader.setRecordDataCapacity(200);
        // ============
        // Then
        assertThat(firstHeader.getStartPointer()).isEqualTo(73L);
        assertThat(firstHeader.getStartDataPointer()).isEqualTo(145L);
        assertThat(firstHeader.getEndPointer()).isEqualTo(145L);
        assertThat(firstHeader.getLength()).isEqualTo(72);
        assertThat(firstHeader.getRecordDataCapacity()).isEqualTo(200);
        assertThat(firstHeader.getRecordDataLength()).isEqualTo(123);
        assertThat(firstHeader.getRecordIndex()).isEqualTo(0);
    }


    @Test
    public void testAdvanceRecordHeader() {
        // ============
        // Given
        RecordHeader firstHeader = new RecordHeader();
        firstHeader.setRecordDataLength(123);
        firstHeader.setRecordDataCapacity(200);
        // ============
        // When
        RecordHeader nextHeader = firstHeader.nextHeader();
        // ============
        // Then
        assertThat(nextHeader.getStartPointer()).isEqualTo(145L);
        assertThat(nextHeader.getStartDataPointer()).isEqualTo(217L);
        assertThat(nextHeader.getEndPointer()).isEqualTo(217L);
        assertThat(nextHeader.getLength()).isEqualTo(72);
        assertThat(nextHeader.getRecordDataCapacity()).isEqualTo(0);
        assertThat(nextHeader.getRecordDataLength()).isEqualTo(0);
        assertThat(nextHeader.getRecordIndex()).isEqualTo(1);
    }

    @Test
    public void testReadWrite_ByteBuffer() {
        // ============
        // Given
        RecordHeader givenHeader = new RecordHeader();
        ByteBuffer buffer = ByteBuffer.allocate(givenHeader.getLength());
        int dataBlockCapacity = 12;
        int occupiedBytes = 45;
        int startDataPointer = 80;
        int startPointer = 78;
        givenHeader.setRecordDataCapacity(dataBlockCapacity);
        givenHeader.setRecordDataLength(occupiedBytes);
        givenHeader.setStartDataPointer(startDataPointer);
{'fraction_non_alphanumeric': 0.1369489846458643, 'fraction_numerical': 0.01981178801386825, 'mean_word_length': 2.94, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2406927', 'n_tokens_mistral': 2263, 'n_tokens_neox': 2216, 'n_words': 318}