Skip to content

Commit a1d76d0

Browse files
committed
routing: rework java test
* split tests * add few tests to improve coverage
1 parent 5a746e1 commit a1d76d0

File tree

5 files changed

+241
-137
lines changed

5 files changed

+241
-137
lines changed
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
// Copyright 2010-2025 Google LLC
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
package com.google.ortools.routing;
15+
16+
import static org.junit.jupiter.api.Assertions.assertEquals;
17+
import static org.junit.jupiter.api.Assertions.assertFalse;
18+
import static org.junit.jupiter.api.Assertions.assertNotNull;
19+
import static org.junit.jupiter.api.Assertions.assertTrue;
20+
21+
import com.google.ortools.Loader;
22+
import java.util.stream.IntStream;
23+
import org.junit.jupiter.api.BeforeEach;
24+
import org.junit.jupiter.api.Test;
25+
26+
public final class RoutingDimensionTest {
27+
@BeforeEach
28+
public void setUp() {
29+
Loader.loadNativeLibraries();
30+
}
31+
32+
@Test
33+
public void testBoundCost_ctor() {
34+
// Create Routing Index Manager
35+
BoundCost boundCost = new BoundCost();
36+
assertNotNull(boundCost);
37+
assertEquals(0, boundCost.getBound());
38+
assertEquals(0, boundCost.getCost());
39+
40+
boundCost = new BoundCost(/* bound= */ 97, /* cost= */ 101);
41+
assertNotNull(boundCost);
42+
assertEquals(97, boundCost.getBound());
43+
assertEquals(101, boundCost.getCost());
44+
}
45+
46+
@Test
47+
public void testRoutingDimension_ctor() {
48+
final IndexManager manager = new IndexManager(31, 7, 3);
49+
assertNotNull(manager);
50+
final Model model = new Model(manager);
51+
assertNotNull(model);
52+
final int transitIndex = model.registerTransitCallback((long fromIndex, long toIndex) -> {
53+
final int fromNode = manager.indexToNode(fromIndex);
54+
final int toNode = manager.indexToNode(toIndex);
55+
return (long) Math.abs(toNode - fromNode);
56+
});
57+
assertTrue(model.addDimension(transitIndex, 100, 100, true, "Dimension"));
58+
final Dimension dimension = model.getMutableDimension("Dimension");
59+
assertNotNull(dimension);
60+
}
61+
62+
@Test
63+
public void testRoutingDimension_softSpanUpperBound() {
64+
final IndexManager manager = new IndexManager(31, 7, 3);
65+
assertNotNull(manager);
66+
final Model model = new Model(manager);
67+
assertNotNull(model);
68+
final int transitIndex = model.registerTransitCallback((long fromIndex, long toIndex) -> {
69+
final int fromNode = manager.indexToNode(fromIndex);
70+
final int toNode = manager.indexToNode(toIndex);
71+
return (long) Math.abs(toNode - fromNode);
72+
});
73+
assertTrue(model.addDimension(transitIndex, 100, 100, true, "Dimension"));
74+
final Dimension dimension = model.getMutableDimension("Dimension");
75+
76+
final BoundCost boundCost = new BoundCost(/* bound= */ 97, /* cost= */ 101);
77+
assertNotNull(boundCost);
78+
assertFalse(dimension.hasSoftSpanUpperBounds());
79+
for (int v : IntStream.range(0, manager.getNumberOfVehicles()).toArray()) {
80+
dimension.setSoftSpanUpperBoundForVehicle(boundCost, v);
81+
final BoundCost bc = dimension.getSoftSpanUpperBoundForVehicle(v);
82+
assertNotNull(bc);
83+
assertEquals(97, bc.getBound());
84+
assertEquals(101, bc.getCost());
85+
}
86+
assertTrue(dimension.hasSoftSpanUpperBounds());
87+
}
88+
89+
@Test
90+
public void testRoutingDimension_quadraticCostSoftSpanUpperBound() {
91+
final IndexManager manager = new IndexManager(31, 7, 3);
92+
assertNotNull(manager);
93+
final Model model = new Model(manager);
94+
assertNotNull(model);
95+
final int transitIndex = model.registerTransitCallback((long fromIndex, long toIndex) -> {
96+
final int fromNode = manager.indexToNode(fromIndex);
97+
final int toNode = manager.indexToNode(toIndex);
98+
return (long) Math.abs(toNode - fromNode);
99+
});
100+
assertTrue(model.addDimension(transitIndex, 100, 100, true, "Dimension"));
101+
final Dimension dimension = model.getMutableDimension("Dimension");
102+
103+
final BoundCost boundCost = new BoundCost(/* bound= */ 97, /* cost= */ 101);
104+
assertNotNull(boundCost);
105+
assertFalse(dimension.hasQuadraticCostSoftSpanUpperBounds());
106+
for (int v : IntStream.range(0, manager.getNumberOfVehicles()).toArray()) {
107+
dimension.setQuadraticCostSoftSpanUpperBoundForVehicle(boundCost, v);
108+
final BoundCost bc = dimension.getQuadraticCostSoftSpanUpperBoundForVehicle(v);
109+
assertNotNull(bc);
110+
assertEquals(97, bc.getBound());
111+
assertEquals(101, bc.getCost());
112+
}
113+
assertTrue(dimension.hasQuadraticCostSoftSpanUpperBounds());
114+
}
115+
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
// Copyright 2010-2025 Google LLC
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
package com.google.ortools.routing;
15+
16+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
17+
import static org.junit.jupiter.api.Assertions.assertEquals;
18+
import static org.junit.jupiter.api.Assertions.assertNotNull;
19+
20+
import com.google.ortools.Loader;
21+
import org.junit.jupiter.api.BeforeEach;
22+
import org.junit.jupiter.api.Test;
23+
24+
public final class RoutingIndexManagerTest {
25+
@BeforeEach
26+
public void setUp() {
27+
Loader.loadNativeLibraries();
28+
}
29+
30+
@Test
31+
public void testSingleDepot() {
32+
final int numNodes = 10;
33+
final int numVehicles = 5;
34+
final int depotIndex = 1;
35+
final IndexManager manager = new IndexManager(numNodes, numVehicles, depotIndex);
36+
assertNotNull(manager);
37+
assertEquals(numNodes, manager.getNumberOfNodes());
38+
assertEquals(numVehicles, manager.getNumberOfVehicles());
39+
assertEquals(numNodes + 2 * numVehicles - 1, manager.getNumberOfIndices());
40+
assertEquals(1, manager.getNumberOfUniqueDepots());
41+
42+
final long[] expectedStarts = {1, 10, 11, 12, 13};
43+
final long[] expectedEnds = {14, 15, 16, 17, 18};
44+
for (int i = 0; i < manager.getNumberOfVehicles(); ++i) {
45+
assertEquals(expectedStarts[i], manager.getStartIndex(i));
46+
assertEquals(expectedEnds[i], manager.getEndIndex(i));
47+
}
48+
49+
for (int i = 0; i < manager.getNumberOfIndices(); ++i) {
50+
if (i >= numNodes) { // start or end nodes
51+
assertEquals(depotIndex, manager.indexToNode(i));
52+
} else {
53+
assertEquals(i, manager.indexToNode(i));
54+
}
55+
}
56+
57+
final long[] inputIndices = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18};
58+
final int[] expectedNodes = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 1, 1, 1, 1, 1, 1, 1, 1};
59+
assertArrayEquals(expectedNodes, manager.indicesToNodes(inputIndices));
60+
61+
for (int i = 0; i < manager.getNumberOfNodes(); ++i) {
62+
assertEquals(i, manager.nodeToIndex(i));
63+
}
64+
65+
final int[] inputNodes = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
66+
final long[] expectedIndicesFromNodes = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
67+
assertArrayEquals(expectedIndicesFromNodes, manager.nodesToIndices(inputNodes));
68+
}
69+
70+
@Test
71+
public void testMultiDepot() {
72+
final int numNodes = 10;
73+
final int numVehicles = 5;
74+
final int[] starts = {0, 3, 9, 2, 2};
75+
final int[] ends = {0, 9, 3, 2, 1};
76+
final IndexManager manager = new IndexManager(numNodes, numVehicles, starts, ends);
77+
assertNotNull(manager);
78+
assertEquals(numNodes, manager.getNumberOfNodes());
79+
assertEquals(numVehicles, manager.getNumberOfVehicles());
80+
assertEquals(numNodes + 2 * numVehicles - 5, manager.getNumberOfIndices());
81+
assertEquals(5, manager.getNumberOfUniqueDepots());
82+
83+
final long[] expectedStarts = {0, 2, 8, 1, 9};
84+
final long[] expectedEnds = {10, 11, 12, 13, 14};
85+
for (int i = 0; i < manager.getNumberOfVehicles(); ++i) {
86+
assertEquals(expectedStarts[i], manager.getStartIndex(i));
87+
assertEquals(expectedEnds[i], manager.getEndIndex(i));
88+
}
89+
90+
final int[] expectedNodes = {0, 2, 3, 4, 5, 6, 7, 8, 9, 2, 0, 9, 3, 2, 1};
91+
for (int i = 0; i < manager.getNumberOfIndices(); ++i) {
92+
assertEquals(expectedNodes[i], manager.indexToNode(i));
93+
}
94+
95+
final long[] inputIndices = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14};
96+
assertArrayEquals(expectedNodes, manager.indicesToNodes(inputIndices));
97+
98+
final long unassigned = IndexManager.getKUnassigned();
99+
final long[] expectedIndices = {0, unassigned, 1, 2, 3, 4, 5, 6, 7, 8};
100+
for (int i = 0; i < manager.getNumberOfNodes(); ++i) {
101+
assertEquals(expectedIndices[i], manager.nodeToIndex(i));
102+
}
103+
104+
final int[] inputNodes = {0, 2, 3, 4, 5, 6, 7, 8, 9};
105+
final long[] expectedIndicesFromNodes = {0, 1, 2, 3, 4, 5, 6, 7, 8};
106+
assertArrayEquals(expectedIndicesFromNodes, manager.nodesToIndices(inputNodes));
107+
}
108+
}

0 commit comments

Comments
 (0)