<commit_before>package org.opentripplanner.analyst.scenario;

import junit.framework.TestCase;
import org.junit.Test;
import org.onebusaway.gtfs.model.Agency;
import org.onebusaway.gtfs.model.AgencyAndId;
import org.onebusaway.gtfs.model.Route;
import org.onebusaway.gtfs.model.Stop;
import org.onebusaway.gtfs.model.StopTime;
import org.onebusaway.gtfs.model.Trip;
import org.opentripplanner.model.StopPattern;
import org.opentripplanner.routing.edgetype.TripPattern;
import org.opentripplanner.routing.trippattern.Deduplicator;
import org.opentripplanner.routing.trippattern.FrequencyEntry;
import org.opentripplanner.routing.trippattern.TripTimes;

import java.util.Arrays;
import java.util.List;

/**
 * Tests for various timetable filters
 */
public class TimetableFilterTest extends TestCase {
    private TripPattern pattern;
    private Trip trip;
    private TripTimes times;
    private FrequencyEntry frequencyEntry;
    private Stop[] stops;
    private Agency agency;
    private Route route;


    /** test that routes that should be removed are */
    @Test
    public void testRouteRemoval () {
        RemoveTrips rt = new RemoveTrips();
        rt.agencyId = agency.getId();
        rt.routeId = Arrays.asList(route.getId().getId());

        assertNull(rt.apply(trip, pattern, times));
        assertNull(rt.apply(trip, pattern, frequencyEntry));
    }

    /** test that routes that should not be removed are not */
    @Test
    public void testRoutePreservation () {
        RemoveTrips rt = new RemoveTrips();
        rt.agencyId = agency.getId();
        rt.routeId = Arrays.asList("SOMETHING ELSE");

        assertEquals(times, rt.apply(trip, pattern, times));
        assertEquals(frequencyEntry, rt.apply(trip, pattern, frequencyEntry));
    }

    /** Test that trips that should be removed are */
    @Test
    public void testTripRemoval () {
        RemoveTrips rt = new RemoveTrips();
        rt.agencyId = agency.getId();
        rt.tripId = Arrays.asList(trip.getId().getId());

        assertNull(rt.apply(trip, pattern, times));
        assertNull(rt.apply(trip, pattern, frequencyEntry));
    }

    /** test that trips that should not be removed are not */
    @Test
    public void testTripPreservation () {
        RemoveTrips rt = new RemoveTrips();
        rt.agencyId = agency.getId();
        rt.tripId = Arrays.asList("SOMETHING ELSE");

        assertEquals(times, rt.apply(trip, pattern, times));
        assertEquals(frequencyEntry, rt.apply(trip, pattern, frequencyEntry));
    }

    /** test a highly specific match */
    @Test
    public void testSpecificRemoval () {
        // route ID and trip ID are supposed to combined with a logical and, ensure that they are.
        RemoveTrips rt = new RemoveTrips();
        rt.agencyId = agency.getId();
        rt.routeId = Arrays.asList(route.getId().getId());
        rt.tripId = Arrays.asList(trip.getId().getId());

        assertNull(rt.apply(trip, pattern, times));
        assertNull(rt.apply(trip, pattern, frequencyEntry));

        rt.routeId = Arrays.asList("SOMETHING ELSE");

        assertEquals(times, rt.apply(trip, pattern, times));
        assertEquals(frequencyEntry, rt.apply(trip, pattern, frequencyEntry));

        rt.tripId = Arrays.asList("SOMETHING ELSE");

        assertEquals(times, rt.apply(trip, pattern, times));
        assertEquals(frequencyEntry, rt.apply(trip, pattern, frequencyEntry));

        rt.routeId = Arrays.asList(route.getId().getId());

        assertEquals(times, rt.apply(trip, pattern, times));
        assertEquals(frequencyEntry, rt.apply(trip, pattern, frequencyEntry));
    }

    @Override
    protected void setUp () {
        agency = new Agency();
        agency.setId("AGENCY");
        route = new Route();
        route.setShortName("T");
        route.setLongName("TEST");
        route.setAgency(agency);
        route.setId(new AgencyAndId(agency.getId(), "TEST"));

        trip = new Trip();
        trip.setRoute(route);
        trip.setId(new AgencyAndId(agency.getId(), "TRIP"));

        stops = new Stop[4];

        for (int i = 0; i < stops.length; i++) {
            Stop s = new Stop();
            s.setLat(-122.123);
            s.setLon(37.363 + i * 0.001);
            s.setId(new AgencyAndId(agency.getId(), "" + i));
            stops[i] = s;<commit_msg>add test for dwell time modification.<commit_after>package org.opentripplanner.analyst.scenario;

import junit.framework.TestCase;
import org.junit.Test;
import org.onebusaway.gtfs.model.Agency;
import org.onebusaway.gtfs.model.AgencyAndId;
import org.onebusaway.gtfs.model.Route;
import org.onebusaway.gtfs.model.Stop;
import org.onebusaway.gtfs.model.StopTime;
import org.onebusaway.gtfs.model.Trip;
import org.opentripplanner.model.StopPattern;
import org.opentripplanner.routing.edgetype.TripPattern;
import org.opentripplanner.routing.trippattern.Deduplicator;
import org.opentripplanner.routing.trippattern.FrequencyEntry;
import org.opentripplanner.routing.trippattern.TripTimes;

import java.util.Arrays;
import java.util.List;

/**
 * Tests for various timetable filters
 */
public class TimetableFilterTest extends TestCase {
    private TripPattern pattern;
    private Trip trip;
    private TripTimes times;
    private FrequencyEntry frequencyEntry;
    private Stop[] stops;
    private Agency agency;
    private Route route;

    /* ROUTE REMOVAL (also matching) */

    /** test that routes that should be removed are */
    @Test
    public void testRouteRemoval () {
        RemoveTrips rt = new RemoveTrips();
        rt.agencyId = agency.getId();
        rt.routeId = Arrays.asList(route.getId().getId());

        assertNull(rt.apply(trip, pattern, times));
        assertNull(rt.apply(trip, pattern, frequencyEntry));
    }

    /** test that routes that should not be removed are not */
    @Test
    public void testRoutePreservation () {
        RemoveTrips rt = new RemoveTrips();
        rt.agencyId = agency.getId();
        rt.routeId = Arrays.asList("SOMETHING ELSE");

        assertEquals(times, rt.apply(trip, pattern, times));
        assertEquals(frequencyEntry, rt.apply(trip, pattern, frequencyEntry));
    }

    /** Test that trips that should be removed are */
    @Test
    public void testTripRemoval () {
        RemoveTrips rt = new RemoveTrips();
        rt.agencyId = agency.getId();
        rt.tripId = Arrays.asList(trip.getId().getId());

        assertNull(rt.apply(trip, pattern, times));
        assertNull(rt.apply(trip, pattern, frequencyEntry));
    }

    /** test that trips that should not be removed are not */
    @Test
    public void testTripPreservation () {
        RemoveTrips rt = new RemoveTrips();
        rt.agencyId = agency.getId();
        rt.tripId = Arrays.asList("SOMETHING ELSE");

        assertEquals(times, rt.apply(trip, pattern, times));
        assertEquals(frequencyEntry, rt.apply(trip, pattern, frequencyEntry));
    }

    /** test a highly specific match */
    @Test
    public void testSpecificRemoval () {
        // route ID and trip ID are supposed to combined with a logical and, ensure that they are.
        RemoveTrips rt = new RemoveTrips();
        rt.agencyId = agency.getId();
        rt.routeId = Arrays.asList(route.getId().getId());
        rt.tripId = Arrays.asList(trip.getId().getId());

        assertNull(rt.apply(trip, pattern, times));
        assertNull(rt.apply(trip, pattern, frequencyEntry));

        rt.routeId = Arrays.asList("SOMETHING ELSE");

        assertEquals(times, rt.apply(trip, pattern, times));
        assertEquals(frequencyEntry, rt.apply(trip, pattern, frequencyEntry));

        rt.tripId = Arrays.asList("SOMETHING ELSE");

        assertEquals(times, rt.apply(trip, pattern, times));
        assertEquals(frequencyEntry, rt.apply(trip, pattern, frequencyEntry));

        rt.routeId = Arrays.asList(route.getId().getId());

        assertEquals(times, rt.apply(trip, pattern, times));
        assertEquals(frequencyEntry, rt.apply(trip, pattern, frequencyEntry));
    }

    /* DWELL TIMES */
    @Test
    public void testDwellTimes () {
        AdjustDwellTime adt = new AdjustDwellTime();
        adt.routeId = Arrays.asList(route.getId().getId());
        adt.agencyId = agency.getId();
        adt.stopId = Arrays.asList(stops[0].getId().getId(), stops[2].getId().getId());
        adt.dwellTime = 60;

        TripTimes tt2 = adt.apply(trip, pattern, times);
        assertNotNull(tt2);
        assertEquals(60, tt2.getDepartureTime(0) - tt2.getArrivalTime(0));
        assertEquals(30, tt2.getDepartureTime(1) - tt2.getArrivalTime(1));
        assertEquals(60, tt2.getDepartureTime(2) - tt2.getArrivalTime(2));
        assertEquals(30, tt2.getDepartureTime(3) - tt2.getArrivalTime(3));

        // make sure we didn't accidentally modify the orignal times
        assertEquals(30, times.getDepartureTime(2) - times.getArrivalTime(2));

        FrequencyEntry fe2 = adt.apply(trip, pattern, frequencyEntry);
        assertNotNull(fe2);

        tt2 = fe2.tripTimes;

        assertEquals(60, tt2.getDepartureTime(0) - tt2.getArrivalTime(0));
        assertEquals(30, tt2.getDepartureTime(1) - tt2.getArrivalTime(1));
        assertEquals(60, tt2.getDepartureTime(2) - tt2.getArrivalTime(2));
        assertEquals(30, tt2.getDepartureTime(3) - tt2.getArrivalTime(3));

        // make sure we didn't accidentally modify the original times
        assertEquals(30, frequencyEntry.tripTimes.getDepartureTime(2) - frequencyEntry.tripTimes.getArrivalTime(2));
    }


    @Override
    protected void setUp () {
        agency = new Agency();
        agency.setId("AGENCY");
        route = new Route();
        route.setShortName("T");
        route.setLongName("TEST");
        route.setAgency(agency);
        route.setId(new AgencyAndId(agency.getId(), "TEST"));

        trip = new Trip();
        trip.setRoute(route);
        trip.setId(new AgencyAndId(agency.getId(), "TRIP"));

        stops = new Stop[4];

        for (int i = 0; i < stops.length; i++) {
            Stop s = new Stop();
            s.setLat(-122.123);
            s.setLon(37.363 + i * 0.001);
            s.setId(new AgencyAndId(agency.getId(), "" + i));
            stops[i] = s;
{'fraction_non_alphanumeric': 0.12517099863201095, 'fraction_numerical': 0.009771350400625366, 'mean_word_length': 3.0058708414872797, 'pattern_counts': {'":': 0, '<': 5, '<?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': '5046392', 'n_tokens_mistral': 3219, 'n_tokens_neox': 3074, 'n_words': 685}