<commit_before>package de.unitrier.st.soposthistory.tests;

import com.google.common.collect.Lists;
import de.unitrier.st.soposthistory.blocks.CodeBlockVersion;
import de.unitrier.st.soposthistory.blocks.PostBlockVersion;
import de.unitrier.st.soposthistory.blocks.TextBlockVersion;
import de.unitrier.st.soposthistory.gt.*;
import de.unitrier.st.soposthistory.version.PostVersion;
import de.unitrier.st.soposthistory.version.PostVersionList;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;

import static org.hamcrest.Matchers.is;
import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.assertEquals;

class BlockLifeSpanAndGroundTruthTest {
    private static Path pathToPostIdList = Paths.get("testdata/postIds.csv");
    private static Path pathToPostHistory = Paths.get("testdata");
    private static Path pathToGroundTruth = Paths.get("testdata", "gt");

    @Test
    void testReadFromDirectory() {
        List<PostGroundTruth> postGroundTruthList = PostGroundTruth.readFromDirectory(pathToGroundTruth);
        try {
            assertEquals(Files.list(pathToGroundTruth).filter(
                    file -> PostGroundTruth.fileNamePattern.matcher(file.toFile().getName()).matches())
                            .count(),
                    postGroundTruthList.size());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    void testPostBlockLifeSpanVersionsEqual(){
        // compare two PostBlockLifeSpanVersions
        PostBlockLifeSpanVersion original = new PostBlockLifeSpanVersion(4711, 42, 1, 0, 0, 0, "");
        PostBlockLifeSpanVersion differentPostId = new PostBlockLifeSpanVersion(4712, 42, 1, 0, 0, 0, "");
        PostBlockLifeSpanVersion differentPostHistoryId = new PostBlockLifeSpanVersion(4711, 43, 1, 0, 0, 0, "");
        PostBlockLifeSpanVersion differentPostBlockTypeId = new PostBlockLifeSpanVersion(4711, 42, 2, 0, 0, 0, "");
        PostBlockLifeSpanVersion differentLocalId = new PostBlockLifeSpanVersion(4711, 42, 1, 1, 0, 0, "");
        PostBlockLifeSpanVersion differentPredId = new PostBlockLifeSpanVersion(4711, 42, 1, 0, 1, 0,"");
        PostBlockLifeSpanVersion differentSuccId = new PostBlockLifeSpanVersion(4711, 42, 1, 0, 0, 1, "");

        assertTrue(original.equals(original));
        assertFalse(original.equals(differentPostId));
        assertFalse(original.equals(differentPostHistoryId));
        assertFalse(original.equals(differentPostBlockTypeId));
        assertFalse(original.equals(differentLocalId));
        assertTrue(original.equals(differentPredId));
        assertTrue(original.equals(differentSuccId));
    }

    @Test
    void testPostBlockLifeSpanExtraction() {
        int postId = 22037280;
        PostVersionList a_22037280 = PostVersionList.readFromCSV(pathToPostHistory, postId, 2);
        PostGroundTruth a_22037280_gt = PostGroundTruth.readFromCSV(pathToGroundTruth, postId);

        List<PostBlockLifeSpan> lifeSpans = a_22037280.getPostBlockLifeSpans();
        List<PostBlockLifeSpan> lifeSpansGT = a_22037280_gt.getPostBlockLifeSpans();
        assertEquals(lifeSpans.size(), lifeSpansGT.size());
        assertEquals(5, lifeSpans.size());
        for (int i=0; i<lifeSpans.size(); i++) {
            assertTrue(lifeSpans.get(i).equals(lifeSpansGT.get(i)));
        }
    }

    @Test
    void testPostBlockLifeSpanExtractionFilter() {
        int postId = 22037280;
        PostVersionList a_22037280 = PostVersionList.readFromCSV(pathToPostHistory, postId, 2);
        PostGroundTruth a_22037280_gt = PostGroundTruth.readFromCSV(pathToGroundTruth, postId);

        // text
        List<PostBlockLifeSpan> textBlockLifeSpans = a_22037280.getPostBlockLifeSpans(
                TextBlockVersion.getPostBlockTypeIdFilter()
        );
        List<PostBlockLifeSpan> textLifeSpansGT = a_22037280_gt.getPostBlockLifeSpans(
                TextBlockVersion.getPostBlockTypeIdFilter()
        );
        assertEquals(textBlockLifeSpans.size(), textLifeSpansGT.size());
        assertEquals(3, textBlockLifeSpans.size());
        for (int i=0; i<textBlockLifeSpans.size(); i++) {
            assertTrue(textBlockLifeSpans.get(i).equals(textLifeSpansGT.get(i)));
        }

        // code
        List<PostBlockLifeSpan> codeBlockLifeSpans = a_22037280.getPostBlockLifeSpans(
                CodeBlockVersion.getPostBlockTypeIdFilter()
        );
        List<PostBlockLifeSpan> codeLifeSpansGT = a_22037280_gt.getPostBlockLifeSpans(
                CodeBlockVersion.getPostBlockTypeIdFilter()
        );
        assertEquals(codeBlockLifeSpans.size(), codeLifeSpansGT.size());
        assertEquals(2, codeBlockLifeSpans.size());
        for (int i=0; i<codeBlockLifeSpans.size(); i++) {
            assertTrue(codeBlockLifeSpans.get(i).equals(codeLifeSpansGT.get(i)));
        }
    }

    @Test
    void testPostBlockConnectionExtraction() {
        int postId = 22037280;
        PostVersionList a_22037280 = PostVersionList.readFromCSV(pathToPostHistory, postId, 2);
        PostGroundTruth a_22037280_gt = PostGroundTruth.readFromCSV(pathToGroundTruth, postId);

        List<PostBlockLifeSpan> lifeSpans = a_22037280.getPostBlockLifeSpans();
        List<PostBlockLifeSpan> lifeSpansGT = a_22037280_gt.getPostBlockLifeSpans();

        Set<PostBlockConnection> connections = PostBlockLifeSpan.toPostBlockConnections(lifeSpans);
        Set<PostBlockConnection> connectionsGT = PostBlockLifeSpan.toPostBlockConnections(lifeSpansGT);

        assertEquals(connections.size(), connectionsGT.size());
        assertTrue(PostBlockConnection.equals(connections, connectionsGT));

        assertTrue(PostBlockConnection.equals(
                PostBlockConnection.intersection(connections, connectionsGT),
                connections)
        );

        assertEquals(0, PostBlockConnection.difference(connections, connectionsGT).size());
    }

    @Test
    void testPostBlockPossibleConnectionsComparison() {
        int postId = 22037280;
        PostVersionList a_22037280 = PostVersionList.readFromCSV(pathToPostHistory, postId, 2);
        PostGroundTruth a_22037280_gt = PostGroundTruth.readFromCSV(pathToGroundTruth, postId);

        assertEquals(78, a_22037280.getPossibleConnections());
        assertEquals(a_22037280.getPossibleConnections(), a_22037280_gt.getPossibleConnections());
    }

    @Test
    void testGetConnections(){<commit_msg>Add test case for equality of PostBlockConnections<commit_after>package de.unitrier.st.soposthistory.tests;

import com.google.common.collect.Sets;
import de.unitrier.st.soposthistory.blocks.CodeBlockVersion;
import de.unitrier.st.soposthistory.blocks.PostBlockVersion;
import de.unitrier.st.soposthistory.blocks.TextBlockVersion;
import de.unitrier.st.soposthistory.gt.*;
import de.unitrier.st.soposthistory.version.PostVersion;
import de.unitrier.st.soposthistory.version.PostVersionList;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import static org.hamcrest.Matchers.is;
import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.assertEquals;

class BlockLifeSpanAndGroundTruthTest {
    private static Path pathToPostIdList = Paths.get("testdata/postIds.csv");
    private static Path pathToPostHistory = Paths.get("testdata");
    private static Path pathToGroundTruth = Paths.get("testdata", "gt");

    @Test
    void testReadFromDirectory() {
        List<PostGroundTruth> postGroundTruthList = PostGroundTruth.readFromDirectory(pathToGroundTruth);
        try {
            assertEquals(Files.list(pathToGroundTruth).filter(
                    file -> PostGroundTruth.fileNamePattern.matcher(file.toFile().getName()).matches())
                            .count(),
                    postGroundTruthList.size());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    void testPostBlockLifeSpanVersionsEqual(){
        // compare two PostBlockLifeSpanVersions
        PostBlockLifeSpanVersion original = new PostBlockLifeSpanVersion(4711, 42, 1, 0, 0, 0, "");
        PostBlockLifeSpanVersion differentPostId = new PostBlockLifeSpanVersion(4712, 42, 1, 0, 0, 0, "");
        PostBlockLifeSpanVersion differentPostHistoryId = new PostBlockLifeSpanVersion(4711, 43, 1, 0, 0, 0, "");
        PostBlockLifeSpanVersion differentPostBlockTypeId = new PostBlockLifeSpanVersion(4711, 42, 2, 0, 0, 0, "");
        PostBlockLifeSpanVersion differentLocalId = new PostBlockLifeSpanVersion(4711, 42, 1, 1, 0, 0, "");
        PostBlockLifeSpanVersion differentPredId = new PostBlockLifeSpanVersion(4711, 42, 1, 0, 1, 0,"");
        PostBlockLifeSpanVersion differentSuccId = new PostBlockLifeSpanVersion(4711, 42, 1, 0, 0, 1, "");

        assertTrue(original.equals(original));
        assertFalse(original.equals(differentPostId));
        assertFalse(original.equals(differentPostHistoryId));
        assertFalse(original.equals(differentPostBlockTypeId));
        assertFalse(original.equals(differentLocalId));
        assertTrue(original.equals(differentPredId));
        assertTrue(original.equals(differentSuccId));
    }

    @Test
    void testPostBlockLifeSpanExtraction() {
        int postId = 22037280;
        PostVersionList a_22037280 = PostVersionList.readFromCSV(pathToPostHistory, postId, 2);
        PostGroundTruth a_22037280_gt = PostGroundTruth.readFromCSV(pathToGroundTruth, postId);

        List<PostBlockLifeSpan> lifeSpans = a_22037280.getPostBlockLifeSpans();
        List<PostBlockLifeSpan> lifeSpansGT = a_22037280_gt.getPostBlockLifeSpans();
        assertEquals(lifeSpans.size(), lifeSpansGT.size());
        assertEquals(5, lifeSpans.size());
        for (int i=0; i<lifeSpans.size(); i++) {
            assertTrue(lifeSpans.get(i).equals(lifeSpansGT.get(i)));
        }
    }

    @Test
    void testPostBlockLifeSpanExtractionFilter() {
        int postId = 22037280;
        PostVersionList a_22037280 = PostVersionList.readFromCSV(pathToPostHistory, postId, 2);
        PostGroundTruth a_22037280_gt = PostGroundTruth.readFromCSV(pathToGroundTruth, postId);

        // text
        List<PostBlockLifeSpan> textBlockLifeSpans = a_22037280.getPostBlockLifeSpans(
                TextBlockVersion.getPostBlockTypeIdFilter()
        );
        List<PostBlockLifeSpan> textLifeSpansGT = a_22037280_gt.getPostBlockLifeSpans(
                TextBlockVersion.getPostBlockTypeIdFilter()
        );
        assertEquals(textBlockLifeSpans.size(), textLifeSpansGT.size());
        assertEquals(3, textBlockLifeSpans.size());
        for (int i=0; i<textBlockLifeSpans.size(); i++) {
            assertTrue(textBlockLifeSpans.get(i).equals(textLifeSpansGT.get(i)));
        }

        // code
        List<PostBlockLifeSpan> codeBlockLifeSpans = a_22037280.getPostBlockLifeSpans(
                CodeBlockVersion.getPostBlockTypeIdFilter()
        );
        List<PostBlockLifeSpan> codeLifeSpansGT = a_22037280_gt.getPostBlockLifeSpans(
                CodeBlockVersion.getPostBlockTypeIdFilter()
        );
        assertEquals(codeBlockLifeSpans.size(), codeLifeSpansGT.size());
        assertEquals(2, codeBlockLifeSpans.size());
        for (int i=0; i<codeBlockLifeSpans.size(); i++) {
            assertTrue(codeBlockLifeSpans.get(i).equals(codeLifeSpansGT.get(i)));
        }
    }

    @Test
    void testPostBlockConnectionExtraction() {
        int postId = 22037280;
        PostVersionList a_22037280 = PostVersionList.readFromCSV(pathToPostHistory, postId, 2);
        PostGroundTruth a_22037280_gt = PostGroundTruth.readFromCSV(pathToGroundTruth, postId);

        List<PostBlockLifeSpan> lifeSpans = a_22037280.getPostBlockLifeSpans();
        List<PostBlockLifeSpan> lifeSpansGT = a_22037280_gt.getPostBlockLifeSpans();

        Set<PostBlockConnection> connections = PostBlockLifeSpan.toPostBlockConnections(lifeSpans);
        Set<PostBlockConnection> connectionsGT = PostBlockLifeSpan.toPostBlockConnections(lifeSpansGT);

        assertEquals(connections.size(), connectionsGT.size());
        assertTrue(PostBlockConnection.equals(connections, connectionsGT));

        assertTrue(PostBlockConnection.equals(
                PostBlockConnection.intersection(connections, connectionsGT),
                connections)
        );

        assertEquals(0, PostBlockConnection.difference(connections, connectionsGT).size());
    }

    @Test
    void testPostBlockPossibleConnectionsComparison() {
        int postId = 22037280;
        PostVersionList a_22037280 = PostVersionList.readFromCSV(pathToPostHistory, postId, 2);
        PostGroundTruth a_22037280_gt = PostGroundTruth.readFromCSV(pathToGroundTruth, postId);

        assertEquals(78, a_22037280.getPossibleConnections());
        assertEquals(a_22037280.getPossibleConnections(), a_22037280_gt.getPossibleConnections());
    }

    @Test
    void testPostBlockConnectionEquals() {
        PostBlockLifeSpanVersion v1_1 = new PostBlockLifeSpanVersion(1, 1, 1, 1);
        PostBlockLifeSpanVersion v1_2 = new PostBlockLifeSpanVersion(1, 1, 1, 1);
        PostBlockLifeSpanVersion v2 = new PostBlockLifeSpanVersion(1, 2, 1, 1);
        PostBlockLifeSpanVersion v3 = new PostBlockLifeSpanVersion(1, 3, 1, 1);
        PostBlockLifeSpanVersion v4 = new PostBlockLifeSpanVersion(1, 4, 1, 1);

        // test equality of PostBlockLifeSpanVersions
        assertEquals(v1_1.getPostId(), v1_2.getPostId());
        assertEquals(v1_1.getPostHistoryId(), v1_2.getPostHistoryId());
        assertEquals(v1_1.getPostBlockTypeId(), v1_2.getPostBlockTypeId());
        assertEquals(v1_1.getLocalId(), v1_2.getLocalId());

        // test equality of PostBlockConnections
        PostBlockConnection connection1 = new PostBlockConnection(v1_1, v2);
        PostBlockConnection connection2 = new PostBlockConnection(v1_2, v2);
        assertTrue(connection1.equals(connection2));

        // test equaliy of a set of PostBlockConnections
        PostBlockConnection connection3 = new PostBlockConnection(v1_2, v2);
        PostBlockConnection connection4 = new PostBlockConnection(v3, v4);
        assertTrue(PostBlockConnection.equals(
                Sets.newHashSet(connection1, connection2, connection3, connection4),
                Sets.newHashSet(connection1, connection2, connection3, connection4))
        );
    }

    @Test
    void testGetConnections(){
{'fraction_non_alphanumeric': 0.09870804566272473, 'fraction_numerical': 0.041151138150249504, 'mean_word_length': 3.7392290249433104, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 27, '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': '3635882', 'n_tokens_mistral': 4759, 'n_tokens_neox': 4321, 'n_words': 682}