|
| 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