Skip to content

Commit

Permalink
Also check for overflow
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardehrenfried committed Nov 20, 2023
1 parent bf73b15 commit 2a6bc4d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
public class Transfer {

/**
* Since transfers costs are not computed through a full A* with pruning they can incur an
* Since transfer costs are not computed through a full A* with pruning they can incur an
* absurdly high cost that overflows the integer cost inside RAPTOR
* (https://github.com/opentripplanner/OpenTripPlanner/issues/5509).
* <p>
Expand All @@ -31,8 +31,8 @@ public class Transfer {
* <p>
* The unit is in RAPTOR cost, so it's centiseconds.
*
* @see RaptorCostConverter
* @see EdgeTraverser
* @see RaptorCostConverter
*/
private static final int MAX_TRANSFER_RAPTOR_COST = Integer.MAX_VALUE / 30;

Expand Down Expand Up @@ -80,7 +80,8 @@ public List<Edge> getEdges() {
}

public Optional<RaptorTransfer> asRaptorTransfer(StreetSearchRequest request) {
return toRaptor(request).filter(s -> s.generalizedCost() < MAX_TRANSFER_RAPTOR_COST);
return toRaptor(request)
.filter(s -> s.generalizedCost() < MAX_TRANSFER_RAPTOR_COST && s.generalizedCost() >= 0);
}

private Optional<RaptorTransfer> toRaptor(StreetSearchRequest request) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,18 @@ void allowLowCost() {
@Nested
class WithoutEdges {

@Test
void overflow() {
var veryLongTransfer = new Transfer(0, Integer.MAX_VALUE);
assertTrue(veryLongTransfer.asRaptorTransfer(StreetSearchRequest.of().build()).isEmpty());
}

@Test
void negativeCost() {
var veryLongTransfer = new Transfer(0, -5);
assertTrue(veryLongTransfer.asRaptorTransfer(StreetSearchRequest.of().build()).isEmpty());
}

@Test
void limitMaxCost() {
var veryLongTransfer = new Transfer(0, 800_000);
Expand Down

0 comments on commit 2a6bc4d

Please sign in to comment.