Skip to content

Commit

Permalink
feat: implment node container
Browse files Browse the repository at this point in the history
issue: #289
  • Loading branch information
CodeBear801 committed Apr 20, 2020
1 parent ac135a3 commit d4d712a
Show file tree
Hide file tree
Showing 2 changed files with 364 additions and 0 deletions.
76 changes: 76 additions & 0 deletions integration/service/oasis/stationgraph/node_container.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package stationgraph

import "github.com/Telenav/osrm-backend/integration/service/oasis/chargingstrategy"

type logicNodeIdentifier2NodePtr map[logicNodeIdentifier]*node
type nodeID2NodePtr map[nodeID]*node
type nodeID2StationID map[nodeID]string

type nodeContainer struct {
logicNode2NodePtr logicNodeIdentifier2NodePtr
id2NodePtr nodeID2NodePtr
id2StationID nodeID2StationID
counter int
}

func newNodeContainer() *nodeContainer {
return &nodeContainer{
logicNode2NodePtr: make(logicNodeIdentifier2NodePtr),
id2NodePtr: make(nodeID2NodePtr),
id2StationID: make(nodeID2StationID),
counter: 0,
}
}

func (nc *nodeContainer) addNode(stationID string, targetState chargingstrategy.State, location locationInfo) *node {
key := logicNodeIdentifier{stationID, targetState}

if n, ok := nc.logicNode2NodePtr[key]; ok {
return n
} else {
n = &node{
id: (nodeID(nc.counter)),
chargeInfo: chargeInfo{
arrivalEnergy: 0.0,
chargeTime: 0.0,
targetState: targetState,
},
locationInfo: location,
}
nc.logicNode2NodePtr[key] = n
nc.id2NodePtr[n.id] = n
nc.id2StationID[n.id] = stationID
nc.counter++

return n
}
}

func (nc *nodeContainer) getNode(id nodeID) *node {
if n, ok := nc.id2NodePtr[id]; ok {
return n
} else {
return nil
}

}

func (nc *nodeContainer) isNodeVisited(id nodeID) bool {
_, ok := nc.id2NodePtr[id]
return ok
}

func (nc *nodeContainer) stationID(id nodeID) string {
if stationID, ok := nc.id2StationID[id]; ok {
return stationID
} else {
return invalidStationID
}
}

const invalidStationID = "InvalidStationID"

type logicNodeIdentifier struct {
stationID string
targetState chargingstrategy.State
}
288 changes: 288 additions & 0 deletions integration/service/oasis/stationgraph/node_container_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,288 @@
package stationgraph

import (
"math/rand"
"reflect"
"testing"
"time"

"github.com/Telenav/osrm-backend/integration/service/oasis/chargingstrategy"
)

func TestAddAndGetFunctionsForNodeContainer(t *testing.T) {
input := []struct {
stationID string
chargeState chargingstrategy.State
location locationInfo
}{
{
"station1",
chargingstrategy.State{
Energy: 10.0,
},
locationInfo{
lat: 1.1,
lon: 1.1,
},
},
{
"station1",
chargingstrategy.State{
Energy: 20.0,
},
locationInfo{
lat: 1.1,
lon: 1.1,
},
},
{
"station1",
chargingstrategy.State{
Energy: 30.0,
},
locationInfo{
lat: 1.1,
lon: 1.1,
},
},
{
"station2",
chargingstrategy.State{
Energy: 10.0,
},
locationInfo{
lat: 2.2,
lon: 2.2,
},
},
{
"station2",
chargingstrategy.State{
Energy: 20.0,
},
locationInfo{
lat: 2.2,
lon: 2.2,
},
},
{
"station3",
chargingstrategy.State{
Energy: 15.0,
},
locationInfo{
lat: 3.3,
lon: 3.3,
},
},
}

expect := []struct {
n *node
stationID string
}{
{
&node{
0,
chargeInfo{
targetState: chargingstrategy.State{
Energy: 10.0,
},
},
locationInfo{
lat: 1.1,
lon: 1.1,
},
},
"station1",
},
{
&node{
1,
chargeInfo{
targetState: chargingstrategy.State{
Energy: 20.0,
},
},
locationInfo{
lat: 1.1,
lon: 1.1,
},
},
"station1",
},
{
&node{
2,
chargeInfo{
targetState: chargingstrategy.State{
Energy: 30.0,
},
},
locationInfo{
lat: 1.1,
lon: 1.1,
},
},
"station1",
},
{
&node{
3,
chargeInfo{
targetState: chargingstrategy.State{
Energy: 10.0,
},
},
locationInfo{
lat: 2.2,
lon: 2.2,
},
},
"station2",
},
{
&node{
4,
chargeInfo{
targetState: chargingstrategy.State{
Energy: 20.0,
},
},
locationInfo{
lat: 2.2,
lon: 2.2,
},
},
"station2",
},
{
&node{
5,
chargeInfo{
targetState: chargingstrategy.State{
Energy: 15.0,
},
},
locationInfo{
lat: 3.3,
lon: 3.3,
},
},
"station3",
},
}

if len(input) != len(expect) {
t.Errorf("Incorrect test case, array of input and array of expect should 1-to-1 match.\n")
}

nc := newNodeContainer()

for i := 0; i < len(input); i++ {
tmpNode := nc.addNode(input[i].stationID, input[i].chargeState, input[i].location)

if !reflect.DeepEqual(tmpNode, expect[i].n) {
t.Errorf("Calling nodeContainer's addNode() generate incorrect result, expect %#v but got %#v.\n", expect[i].n, tmpNode)
}
}

for i := 0; i < len(input); i++ {
isVisited := nc.isNodeVisited((nodeID)(i))
if !isVisited {
t.Errorf("Calling nodeContainer's isNodeVisited() generate incorrect result, expect node %v is visited but returns no.\n", i)
}

tmpNode := nc.getNode((nodeID)(i))
if !reflect.DeepEqual(tmpNode, expect[i].n) {
t.Errorf("Calling nodeContainer's getNode() generate incorrect result, expect %#v but got %#v.\n", expect[i].n, tmpNode)
}

tmpStationID := nc.stationID((nodeID)(i))
if !reflect.DeepEqual(tmpStationID, expect[i].stationID) {
t.Errorf("Calling nodeContainer's stationID() generate incorrect result, expect %#v but got %#v.\n", expect[i].stationID, tmpStationID)
}
}

rand.Seed(time.Now().UnixNano())
for i := 0; i < 10; i++ {
randomGen := func(min, max int) int {
return rand.Intn(max-min) + min
}
unVisitedNodeID := randomGen(7, 100)
isVisited := nc.isNodeVisited((nodeID)(unVisitedNodeID))
if isVisited {
t.Errorf("Calling nodeContainer's isNodeVisited() generate incorrect result, expect node %v is unvisited but returns yes.\n", unVisitedNodeID)
}
}

}

func TestAddDuplicateNodeForNodeContainer(t *testing.T) {
input := []struct {
stationID string
chargeState chargingstrategy.State
location locationInfo
}{
{
"station1",
chargingstrategy.State{
Energy: 10.0,
},
locationInfo{
lat: 1.1,
lon: 1.1,
},
},
{
"station1",
chargingstrategy.State{
Energy: 10.0,
},
locationInfo{
lat: 1.1,
lon: 1.1,
},
},
{
"station1",
chargingstrategy.State{
Energy: 10.0,
},
locationInfo{
lat: 1.1,
lon: 1.1,
},
},
}

expect := struct {
n *node
stationID string
}{
&node{
0,
chargeInfo{
targetState: chargingstrategy.State{
Energy: 10.0,
},
},
locationInfo{
lat: 1.1,
lon: 1.1,
},
},
"station1",
}

nc := newNodeContainer()

for i := 0; i < len(input); i++ {
tmpNode := nc.addNode(input[i].stationID, input[i].chargeState, input[i].location)

if !reflect.DeepEqual(tmpNode, expect.n) {
t.Errorf("Calling nodeContainer's addNode() generate incorrect result, expect %#v but got %#v.\n", expect.n, tmpNode)
}
}

}

0 comments on commit d4d712a

Please sign in to comment.