diff --git a/integration/service/oasis/stationgraph/edge_2_weight.go b/integration/service/oasis/stationgraph/edge_2_weight.go new file mode 100644 index 00000000000..4008ab80abd --- /dev/null +++ b/integration/service/oasis/stationgraph/edge_2_weight.go @@ -0,0 +1,21 @@ +package stationgraph + +import "github.com/Telenav/osrm-backend/integration/service/oasis/internal/common" + +type place2placeID struct { + from common.PlaceID + to common.PlaceID +} +type edgeID2EdgeData map[place2placeID]*common.Weight + +func newEdgeID2EdgeData() edgeID2EdgeData { + return make(edgeID2EdgeData, 5000000) +} + +func (edge2Weight edgeID2EdgeData) get(id place2placeID) *common.Weight { + return edge2Weight[id] +} + +func (edge2Weight edgeID2EdgeData) add(id place2placeID, weight *common.Weight) { + edge2Weight[id] = weight +} diff --git a/integration/service/oasis/stationgraph/edge_2_weight_test.go b/integration/service/oasis/stationgraph/edge_2_weight_test.go new file mode 100644 index 00000000000..c4dd9668949 --- /dev/null +++ b/integration/service/oasis/stationgraph/edge_2_weight_test.go @@ -0,0 +1,61 @@ +package stationgraph + +import ( + "reflect" + "testing" + + "github.com/Telenav/osrm-backend/integration/service/oasis/internal/common" + "github.com/Telenav/osrm-backend/integration/service/oasis/stationfinder/stationfindertype" +) + +func TestEdgeID2EdgeDataGetResultShouldEqualsToSetValue(t *testing.T) { + cases := []struct { + key place2placeID + value *common.Weight + }{ + // case 1 + { + place2placeID{ + 111, + 222, + }, + &common.Weight{ + Duration: 111.0, + Distance: 222.0, + }, + }, + // case 2 + { + place2placeID{ + stationfindertype.OrigLocationID, + stationfindertype.DestLocationID, + }, + &common.Weight{ + Duration: 333.0, + Distance: 444.0, + }, + }, + } + + edgeID2Data := newEdgeID2EdgeData() + for _, c := range cases { + edgeID2Data.add(c.key, c.value) + } + + for _, c := range cases { + actualValue := edgeID2Data.get(c.key) + if !reflect.DeepEqual(c.value, actualValue) { + t.Errorf("Expect to get value %#v for key %#v but got %#v\n", c.value, c.key, actualValue) + } + } +} + +func TestEdgeID2EdgeDataWhenGetUnsettedKeyShouldGetNil(t *testing.T) { + edgeID2Data := newEdgeID2EdgeData() + actualValue := edgeID2Data.get(place2placeID{ + stationfindertype.OrigLocationID, + stationfindertype.DestLocationID}) + if actualValue != nil { + t.Errorf("Expect to get nil for unsetted key but got %#v\n", actualValue) + } +} diff --git a/integration/service/oasis/stationgraph/node_graph.go b/integration/service/oasis/stationgraph/node_graph.go index ec37ef0113d..7db90242e70 100644 --- a/integration/service/oasis/stationgraph/node_graph.go +++ b/integration/service/oasis/stationgraph/node_graph.go @@ -10,11 +10,6 @@ import ( ) type nodeID2AdjacentNodes map[nodeID][]nodeID -type place2placeID struct { - from common.PlaceID - to common.PlaceID -} -type edgeID2EdgeData map[place2placeID]*common.Weight type nodeGraph struct { nodeContainer *nodeContainer @@ -31,7 +26,7 @@ func NewNodeGraph(strategy chargingstrategy.Strategy, query connectivitymap.Quer return &nodeGraph{ nodeContainer: newNodeContainer(), adjacentList: make(nodeID2AdjacentNodes), - edgeMetric: make(edgeID2EdgeData, 50000000), + edgeMetric: newEdgeID2EdgeData(), startNodeID: invalidNodeID, endNodeID: invalidNodeID, strategy: strategy, @@ -64,9 +59,9 @@ func (g *nodeGraph) AdjacentNodes(id nodeID) []nodeID { // Edge returns edge information between given two nodes func (g *nodeGraph) Edge(from, to nodeID) *common.Weight { - return g.edgeMetric[place2placeID{ + return g.edgeMetric.get(place2placeID{ from: g.nodeContainer.nodeID2PlaceID(from), - to: g.nodeContainer.nodeID2PlaceID(to)}] + to: g.nodeContainer.nodeID2PlaceID(to)}) } // SetStart generates start node for the nodeGraph @@ -119,9 +114,9 @@ func (g *nodeGraph) createLogicalNodes(from nodeID, toPlaceID common.PlaceID, to endNodeID := g.EndNodeID() if toPlaceID == g.PlaceID(endNodeID) { results = append(results, g.Node(endNodeID)) - g.edgeMetric[place2placeID{ + g.edgeMetric.add(place2placeID{ from: g.nodeContainer.nodeID2PlaceID(from), - to: g.nodeContainer.nodeID2PlaceID(endNodeID)}] = weight + to: g.nodeContainer.nodeID2PlaceID(endNodeID)}, weight) return results } @@ -130,9 +125,9 @@ func (g *nodeGraph) createLogicalNodes(from nodeID, toPlaceID common.PlaceID, to n := g.nodeContainer.addNode(toPlaceID, state) results = append(results, n) - g.edgeMetric[place2placeID{ + g.edgeMetric.add(place2placeID{ from: g.nodeContainer.nodeID2PlaceID(from), - to: g.nodeContainer.nodeID2PlaceID(n.id)}] = weight + to: g.nodeContainer.nodeID2PlaceID(n.id)}, weight) } return results }