<commit_before>package simulation;

import network.Link;
import network.Node;
import org.junit.Test;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static wrappers.DummyWrapper.*;
import static wrappers.PathWrapper.path;
import static wrappers.network.NetworkWrapper.anyNode;
import static wrappers.routetable.DestinationElement.destination;
import static wrappers.routetable.RouteTableWrapper.table;

public class RouteTableTest {

    @Test
    public void getRoute_OnTableWithoutOutLinks_Null() throws Exception {
        assertThat(table().getRoute(anyNode(), anyDummyLink()), is(nullValue()));
    }

    @Test
    public void setRoute_WithAttr1AndEmptyPathOnTableWithoutOutLinks_GetsNull() throws Exception {
        RouteTable routeTable = table();
        Node destination = new Node(0);
        Link outLink = dummyLink(1, 2);

        routeTable.setRoute(outLink, dummyRoute(0, 1, path()));

        assertThat(routeTable.getRoute(destination, outLink), is(nullValue()));
    }

    @Test
    public void getRoute_ForOutLink1To2OnTableWithoutKnownDestinationsAndOutLinkFrom1To2_Invalid() throws Exception {
        RouteTable routeTable = table(dummyOutLink(1, 2));
        Node destination = new Node(0);

        assertThat(routeTable.getRoute(destination, dummyLink(1, 2)), is(Route.invalidRoute(destination)));
    }

    @Test
    public void
    setRoute_FromOutLink1To2AndForDest0WithAttr1AndEmptyPath_GetsRouteWithAttr0AndEmptyPathForDest0() throws Exception {
        RouteTable routeTable = table(dummyOutLink(1, 2));
        Node destination = new Node(0);
        Link outLink = dummyLink(1, 2);

        routeTable.setRoute(outLink, dummyRoute(0, 1, path()));

        assertThat(routeTable.getRoute(destination, outLink), is(dummyRoute(0, 1, path())));
    }

    @Test
    public void
    setRoute_ForDest0WithAttr0AndEmptyPathAndAfterItChangeAttrToAttr1_GetsRouteForDest0WithAttr0AndEmptyPath()
            throws Exception {
        Node destination = new Node(0);
        Link outLink = dummyLink(1, 2);
        Route route = dummyRoute(0, 0, path());
        RouteTable routeTable = table(dummyOutLink(1, 2));

        routeTable.setRoute(outLink, route);
        route.setAttribute(dummyAttr(1));

        assertThat(routeTable.getRoute(destination, outLink), is(dummyRoute(0, 0, path())));
    }

    @Test
    public void
    setRoute_WithAttr2AndEmptyPathForOutLinkAlreadyAssignedWithRouteWithAttr1AndEmpty_GetsRouteWithAttr2AndEmptyPath()
            throws Exception {
        RouteTable routeTable = table(dummyOutLink(0, 1));
        Link outLink = dummyLink(0, 1);
        Node destination = new Node(0);
        routeTable.setRoute(outLink, dummyRoute(0, 1, path()));

        routeTable.setRoute(outLink, dummyRoute(0, 2, path()));

        assertThat(routeTable.getRoute(destination, outLink), is(dummyRoute(0, 2, path())));
    }

    @Test
    public void
    getRoute_ForDestination1OnTableWithOnlyKnownDestination0_InvalidRoute() throws Exception {
        RouteTable routeTable = table(
                                dummyOutLink(0, 1),
                destination(0), dummyRoute(0, path())
        );
        Node destination1 = new Node(1);

        assertThat(routeTable.getRoute(destination1, dummyLink(0, 1)), is(Route.invalidRoute(destination1)));
    }

    @Test
    public void
    getRoute_ForDestination0AndOutLink1To2ButTheRouteForDestination0WasOnlySetForOutLink1To3_InvalidRoute()
            throws Exception {
        RouteTable table = table(dummyOutLink(1, 2), dummyOutLink(1, 3));
        Node destination = new Node(0);
        table.setRoute(dummyLink(1, 2), dummyRoute(0, 0, path()));  // assigned attribute to other link

        assertThat(table.getRoute(destination, dummyLink(1, 3)), is(Route.invalidRoute(destination)));
    }

    @Test
    public void
    clear_EmptyTableWithoutOutLinks_TableStaysEmptyWithoutOutLinks() throws Exception {
        RouteTable routeTable = table();

        routeTable.clear();

        assertThat(routeTable, is(table()));
    }

    @Test
    public void
    clear_EmptyTableWithOutLinkBetween0And1_TableStaysEmptyWithOutLinkBetween0And1() throws Exception {
        RouteTable routeTable = table(dummyOutLink(0, 1));

        routeTable.clear();

        assertThat(routeTable, is(table(dummyOutLink(0, 1))));
    }

    @Test
    public void
    clear_TableWithOutLinkBetween0And1AndKnownDestinations0And1_EmptyTableWithOutLinkBetween0And1() throws Exception {
        RouteTable routeTable = table(
                                dummyOutLink(0, 1),
                destination(0), dummyRoute(0, path()),
                destination(1), dummyRoute(0, path())
        );

        routeTable.clear();

        assertThat(routeTable, is(table(dummyOutLink(0, 1))));
    }

    @Test
    public void removeOutLink_From0To1OnEmptyTable_EmptyTable() throws Exception {
        RouteTable routeTable = table();

        routeTable.removeOutLink(dummyLink(0, 1));

        assertThat(routeTable, is(table()));
    }

    @Test
    public void removeOutLink_From0To1OnTableWithOutLink2To3_TableWithOutLink2To3() throws Exception {
        RouteTable routeTable = table(dummyOutLink(2, 3));

        routeTable.removeOutLink(dummyLink(0, 1));

        assertThat(routeTable, is(table(dummyOutLink(2, 3))));
    }

    @Test
    public void removeOutLink_From0To1OnTableWithOutLink0To1_EmptyTable() throws Exception {
        RouteTable routeTable = table(dummyOutLink(0, 1));

        routeTable.removeOutLink(dummyLink(0, 1));

        assertThat(routeTable, is(table()));
    }

    @Test
    public void removeOutLink_From0To1OnTableWithOutLink0To1AndOutLink0To2_TableWithOutLink0To2() throws Exception {
        RouteTable routeTable = table(dummyOutLink(0, 1), dummyOutLink(0, 2));

        routeTable.removeOutLink(dummyLink(0, 1));

        assertThat(routeTable, is(table(dummyOutLink(0, 2))));
    }

    @Test
    public void
    removeOutLink_From0To1OnTableWithOneKnownDestination_RoutesAreKeptForOtherOutLink() throws Exception {
        RouteTable routeTable = table(
                                dummyOutLink(0, 1),     dummyOutLink(0, 2),
                destination(0), dummyRoute(0, path()),  dummyRoute(0, path())
        );

        routeTable.removeOutLink(dummyLink(0, 2));

        assertThat(routeTable, is(table(
                                dummyOutLink(0, 1),
                destination(0), dummyRoute(0, path())
        )));
    }
}<commit_msg>added extra test for the removeOutLink() method<commit_after>package simulation;

import network.Link;
import network.Node;
import org.junit.Test;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static wrappers.DummyWrapper.*;
import static wrappers.PathWrapper.path;
import static wrappers.network.NetworkWrapper.anyNode;
import static wrappers.routetable.DestinationElement.destination;
import static wrappers.routetable.RouteTableWrapper.table;

public class RouteTableTest {

    @Test
    public void getRoute_OnTableWithoutOutLinks_Null() throws Exception {
        assertThat(table().getRoute(anyNode(), anyDummyLink()), is(nullValue()));
    }

    @Test
    public void setRoute_WithAttr1AndEmptyPathOnTableWithoutOutLinks_GetsNull() throws Exception {
        RouteTable routeTable = table();
        Node destination = new Node(0);
        Link outLink = dummyLink(1, 2);

        routeTable.setRoute(outLink, dummyRoute(0, 1, path()));

        assertThat(routeTable.getRoute(destination, outLink), is(nullValue()));
    }

    @Test
    public void getRoute_ForOutLink1To2OnTableWithoutKnownDestinationsAndOutLinkFrom1To2_Invalid() throws Exception {
        RouteTable routeTable = table(dummyOutLink(1, 2));
        Node destination = new Node(0);

        assertThat(routeTable.getRoute(destination, dummyLink(1, 2)), is(Route.invalidRoute(destination)));
    }

    @Test
    public void
    setRoute_FromOutLink1To2AndForDest0WithAttr1AndEmptyPath_GetsRouteWithAttr0AndEmptyPathForDest0() throws Exception {
        RouteTable routeTable = table(dummyOutLink(1, 2));
        Node destination = new Node(0);
        Link outLink = dummyLink(1, 2);

        routeTable.setRoute(outLink, dummyRoute(0, 1, path()));

        assertThat(routeTable.getRoute(destination, outLink), is(dummyRoute(0, 1, path())));
    }

    @Test
    public void
    setRoute_ForDest0WithAttr0AndEmptyPathAndAfterItChangeAttrToAttr1_GetsRouteForDest0WithAttr0AndEmptyPath()
            throws Exception {
        Node destination = new Node(0);
        Link outLink = dummyLink(1, 2);
        Route route = dummyRoute(0, 0, path());
        RouteTable routeTable = table(dummyOutLink(1, 2));

        routeTable.setRoute(outLink, route);
        route.setAttribute(dummyAttr(1));

        assertThat(routeTable.getRoute(destination, outLink), is(dummyRoute(0, 0, path())));
    }

    @Test
    public void
    setRoute_WithAttr2AndEmptyPathForOutLinkAlreadyAssignedWithRouteWithAttr1AndEmpty_GetsRouteWithAttr2AndEmptyPath()
            throws Exception {
        RouteTable routeTable = table(dummyOutLink(0, 1));
        Link outLink = dummyLink(0, 1);
        Node destination = new Node(0);
        routeTable.setRoute(outLink, dummyRoute(0, 1, path()));

        routeTable.setRoute(outLink, dummyRoute(0, 2, path()));

        assertThat(routeTable.getRoute(destination, outLink), is(dummyRoute(0, 2, path())));
    }

    @Test
    public void
    getRoute_ForDestination1OnTableWithOnlyKnownDestination0_InvalidRoute() throws Exception {
        RouteTable routeTable = table(
                                dummyOutLink(0, 1),
                destination(0), dummyRoute(0, path())
        );
        Node destination1 = new Node(1);

        assertThat(routeTable.getRoute(destination1, dummyLink(0, 1)), is(Route.invalidRoute(destination1)));
    }

    @Test
    public void
    getRoute_ForDestination0AndOutLink1To2ButTheRouteForDestination0WasOnlySetForOutLink1To3_InvalidRoute()
            throws Exception {
        RouteTable table = table(dummyOutLink(1, 2), dummyOutLink(1, 3));
        Node destination = new Node(0);
        table.setRoute(dummyLink(1, 2), dummyRoute(0, 0, path()));  // assigned attribute to other link

        assertThat(table.getRoute(destination, dummyLink(1, 3)), is(Route.invalidRoute(destination)));
    }

    @Test
    public void
    clear_EmptyTableWithoutOutLinks_TableStaysEmptyWithoutOutLinks() throws Exception {
        RouteTable routeTable = table();

        routeTable.clear();

        assertThat(routeTable, is(table()));
    }

    @Test
    public void
    clear_EmptyTableWithOutLinkBetween0And1_TableStaysEmptyWithOutLinkBetween0And1() throws Exception {
        RouteTable routeTable = table(dummyOutLink(0, 1));

        routeTable.clear();

        assertThat(routeTable, is(table(dummyOutLink(0, 1))));
    }

    @Test
    public void
    clear_TableWithOutLinkBetween0And1AndKnownDestinations0And1_EmptyTableWithOutLinkBetween0And1() throws Exception {
        RouteTable routeTable = table(
                                dummyOutLink(0, 1),
                destination(0), dummyRoute(0, path()),
                destination(1), dummyRoute(0, path())
        );

        routeTable.clear();

        assertThat(routeTable, is(table(dummyOutLink(0, 1))));
    }

    @Test
    public void removeOutLink_From0To1OnEmptyTable_EmptyTable() throws Exception {
        RouteTable routeTable = table();

        routeTable.removeOutLink(dummyLink(0, 1));

        assertThat(routeTable, is(table()));
    }

    @Test
    public void removeOutLink_From0To1OnTableWithOutLink2To3_TableWithOutLink2To3() throws Exception {
        RouteTable routeTable = table(dummyOutLink(2, 3));

        routeTable.removeOutLink(dummyLink(0, 1));

        assertThat(routeTable, is(table(dummyOutLink(2, 3))));
    }

    @Test
    public void removeOutLink_From0To1OnTableWithOutLink0To1_EmptyTable() throws Exception {
        RouteTable routeTable = table(dummyOutLink(0, 1));

        routeTable.removeOutLink(dummyLink(0, 1));

        assertThat(routeTable, is(table()));
    }

    @Test
    public void removeOutLink_From0To1OnTableWithOutLink0To1AndOutLink0To2_TableWithOutLink0To2() throws Exception {
        RouteTable routeTable = table(dummyOutLink(0, 1), dummyOutLink(0, 2));

        routeTable.removeOutLink(dummyLink(0, 1));

        assertThat(routeTable, is(table(dummyOutLink(0, 2))));
    }

    @Test
    public void
    removeOutLink_From0To1OnTableWithOneKnownDestination_RoutesAreKeptForOtherOutLink() throws Exception {
        RouteTable routeTable = table(
                                dummyOutLink(0, 1),     dummyOutLink(0, 2),
                destination(0), dummyRoute(0, path()),  dummyRoute(0, path())
        );

        routeTable.removeOutLink(dummyLink(0, 2));

        assertThat(routeTable, is(table(
                                dummyOutLink(0, 1),
                destination(0), dummyRoute(0, path())
        )));
    }

    @Test
    public void
    removeOutLink_From0To1WithAssignedRouteForDestination0_GetRouteForOutLink0To1ReturnsNull() throws Exception {
        RouteTable routeTable = table(
                                dummyOutLink(0, 1),     dummyOutLink(0, 2),
                destination(0), dummyRoute(0, path()),  dummyRoute(0, path())
        );
        Link removedOutLink = dummyLink(0, 1);
        Node destination = new Node(0);

        routeTable.removeOutLink(removedOutLink);

        assertThat(routeTable.getRoute(destination, removedOutLink), is(nullValue()));
    }

}
{'fraction_non_alphanumeric': 0.10015944339759385, 'fraction_numerical': 0.02500362371358168, 'mean_word_length': 3.361251580278129, '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': '4656557', 'n_tokens_mistral': 4091, 'n_tokens_neox': 3916, 'n_words': 756}