コード例 #1
0
  @Test
  public void shouldReturnBestPath() {
    List<JajascriptAction.Flight> flights =
        Lists.newArrayList(
            new Flight("A", 0, 5, 10),
            new Flight("B", 6, 4, 21),
            new Flight("C", 8, 1, 10),
            new Flight("D", 9, 2, 10),
            new Flight("E", 12, 1, 10),
            new Flight("F", 4, 9, 40));
    Path bestPath = new JajascriptAction(flights).findBestPath();
    assertThat(bestPath.getGain()).isEqualTo(41);
    assertThat(bestPath.getPath()).onProperty("name").containsExactly("A", "B", "E");

    flights =
        Lists.newArrayList(
            new Flight("A", 0, 5, 10),
            new Flight("B", 5, 5, 10),
            new Flight("C", 5, 5, 30),
            new Flight("D", 5, 5, 20),
            new Flight("E", 10, 3, 10),
            new Flight("F", 13, 9, 40));
    bestPath = new JajascriptAction(flights).findBestPath();
    assertThat(bestPath.getGain()).isEqualTo(90);
    assertThat(bestPath.getPath()).onProperty("name").containsExactly("A", "C", "E", "F");
  }
コード例 #2
0
  @Test
  public void shouldCorrectPath() {
    List<JajascriptAction.Flight> flights = Lists.newArrayList();
    Map<String, Flight> flightsByName = Maps.newHashMap();
    Random random = new Random();
    for (int i = 0; i < 10000; i++) {
      int start = random.nextInt(23);
      int duration = 1 + random.nextInt(24 - start);
      Flight f = new Flight(String.valueOf(i + 1), start, duration, random.nextInt(21) + 1);
      flights.add(f);
      flightsByName.put(f.getName(), f);
    }

    Path bestPath = new JajascriptAction(flights).findBestPath();

    int gain = bestPath.getPath().get(0).getPrice();
    for (int i = 1; i < bestPath.getPath().size(); i++) {
      Flight f = bestPath.getPath().get(i);
      Flight before = bestPath.getPath().get(i - 1);
      assertThat(before.getStartTime() + before.getDuration() <= f.getStartTime());
      gain += f.getPrice();
    }
    assertThat(bestPath.getGain() == gain);
  }