Skip to content

Commit

Permalink
feat: implement unit test for station graph.
Browse files Browse the repository at this point in the history
issue: #282
  • Loading branch information
CodeBear801 committed Apr 23, 2020
1 parent c9b6996 commit e1b26fb
Show file tree
Hide file tree
Showing 7 changed files with 269 additions and 795 deletions.
2 changes: 1 addition & 1 deletion integration/service/oasis/stationgraph/dijkstra.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func dijkstra(g Graph, start, end nodeID) []nodeID {
node := g.Node(currID)
for _, targetID := range g.AdjacentNodes(currID) {
if g.Edge(currID, targetID) == nil {
glog.Errorf("No connectivity between %#v and %#v which is unexpected, check your logic.\n", currID, targetID)
glog.Errorf("No connectivity between %+v and %+v which is unexpected, check your logic.\n", currID, targetID)
}

len := g.Edge(currID, targetID).distance
Expand Down
98 changes: 6 additions & 92 deletions integration/service/oasis/stationgraph/graph_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ func NewMockGraph1() Graph {
{
0,
chargeInfo{
arrivalEnergy: 999,
targetState: chargingstrategy.State{
Energy: 999,
},
Expand All @@ -28,7 +27,6 @@ func NewMockGraph1() Graph {
{
1,
chargeInfo{
arrivalEnergy: 999,
targetState: chargingstrategy.State{
Energy: 999,
},
Expand All @@ -41,7 +39,6 @@ func NewMockGraph1() Graph {
{
2,
chargeInfo{
arrivalEnergy: 999,
targetState: chargingstrategy.State{
Energy: 999,
},
Expand All @@ -54,7 +51,6 @@ func NewMockGraph1() Graph {
{
3,
chargeInfo{
arrivalEnergy: 999,
targetState: chargingstrategy.State{
Energy: 999,
},
Expand All @@ -67,7 +63,6 @@ func NewMockGraph1() Graph {
{
4,
chargeInfo{
arrivalEnergy: 999,
targetState: chargingstrategy.State{
Energy: 999,
},
Expand All @@ -89,76 +84,22 @@ func NewMockGraph1() Graph {
// node_0 -> node_1, duration = 30, distance = 30
// node_0 -> node_2, duration = 20, distance = 20
0: {
{
edgeID{
0,
1,
},
&edge{
30,
30,
},
},
{
edgeID{
0,
2,
},
&edge{
20,
20,
},
},
{edgeID{0, 1}, &edge{30, 30}},
{edgeID{0, 2}, &edge{20, 20}},
},
// node_1 -> node_3, duration = 10, distance = 10
1: {
{
edgeID{
1,
3,
},
&edge{
10,
10,
},
},
{edgeID{1, 3}, &edge{10, 10}},
},
// node_2 -> node_4, duration = 50, distance = 50
// node_2 -> node_3, duration = 50, distance = 50
2: {
{
edgeID{
2,
4,
},
&edge{
50,
50,
},
},
{
edgeID{
2,
3,
},
&edge{
50,
50,
},
},
{edgeID{2, 4}, &edge{50, 50}},
{edgeID{2, 3}, &edge{50, 50}},
},
// node_3 -> node_4, duration = 10, distance = 10
3: {
{
edgeID{
3,
4,
},
&edge{
10,
10,
},
},
{edgeID{3, 4}, &edge{10, 10}},
},
},
chargingstrategy.NewNullChargeStrategy(),
Expand Down Expand Up @@ -187,7 +128,6 @@ func NewMockGraph2() Graph {
{
0,
chargeInfo{
arrivalEnergy: 999,
targetState: chargingstrategy.State{
Energy: 999,
},
Expand All @@ -200,7 +140,6 @@ func NewMockGraph2() Graph {
{
1,
chargeInfo{
arrivalEnergy: 999,
targetState: chargingstrategy.State{
Energy: 999,
},
Expand All @@ -213,7 +152,6 @@ func NewMockGraph2() Graph {
{
2,
chargeInfo{
arrivalEnergy: 999,
targetState: chargingstrategy.State{
Energy: 999,
},
Expand All @@ -226,7 +164,6 @@ func NewMockGraph2() Graph {
{
3,
chargeInfo{
arrivalEnergy: 999,
targetState: chargingstrategy.State{
Energy: 999,
},
Expand All @@ -239,7 +176,6 @@ func NewMockGraph2() Graph {
{
4,
chargeInfo{
arrivalEnergy: 999,
targetState: chargingstrategy.State{
Energy: 999,
},
Expand All @@ -252,7 +188,6 @@ func NewMockGraph2() Graph {
{
5,
chargeInfo{
arrivalEnergy: 999,
targetState: chargingstrategy.State{
Energy: 999,
},
Expand All @@ -265,7 +200,6 @@ func NewMockGraph2() Graph {
{
6,
chargeInfo{
arrivalEnergy: 999,
targetState: chargingstrategy.State{
Energy: 999,
},
Expand All @@ -278,7 +212,6 @@ func NewMockGraph2() Graph {
{
7,
chargeInfo{
arrivalEnergy: 999,
targetState: chargingstrategy.State{
Energy: 999,
},
Expand All @@ -291,7 +224,6 @@ func NewMockGraph2() Graph {
{
8,
chargeInfo{
arrivalEnergy: 999,
targetState: chargingstrategy.State{
Energy: 999,
},
Expand Down Expand Up @@ -522,7 +454,6 @@ func NewMockGraph3() Graph {
{
0,
chargeInfo{
arrivalEnergy: 999,
targetState: chargingstrategy.State{
Energy: 999,
},
Expand All @@ -535,7 +466,6 @@ func NewMockGraph3() Graph {
{
1,
chargeInfo{
arrivalEnergy: 999,
targetState: chargingstrategy.State{
Energy: 999,
},
Expand All @@ -548,7 +478,6 @@ func NewMockGraph3() Graph {
{
2,
chargeInfo{
arrivalEnergy: 999,
targetState: chargingstrategy.State{
Energy: 999,
},
Expand All @@ -561,7 +490,6 @@ func NewMockGraph3() Graph {
{
3,
chargeInfo{
arrivalEnergy: 999,
targetState: chargingstrategy.State{
Energy: 999,
},
Expand All @@ -574,7 +502,6 @@ func NewMockGraph3() Graph {
{
4,
chargeInfo{
arrivalEnergy: 999,
targetState: chargingstrategy.State{
Energy: 999,
},
Expand All @@ -587,7 +514,6 @@ func NewMockGraph3() Graph {
{
5,
chargeInfo{
arrivalEnergy: 999,
targetState: chargingstrategy.State{
Energy: 999,
},
Expand All @@ -600,7 +526,6 @@ func NewMockGraph3() Graph {
{
6,
chargeInfo{
arrivalEnergy: 999,
targetState: chargingstrategy.State{
Energy: 999,
},
Expand All @@ -613,7 +538,6 @@ func NewMockGraph3() Graph {
{
7,
chargeInfo{
arrivalEnergy: 999,
targetState: chargingstrategy.State{
Energy: 999,
},
Expand All @@ -626,7 +550,6 @@ func NewMockGraph3() Graph {
{
8,
chargeInfo{
arrivalEnergy: 999,
targetState: chargingstrategy.State{
Energy: 999,
},
Expand Down Expand Up @@ -858,7 +781,6 @@ func NewMockGraph4() Graph {
{
0,
chargeInfo{
arrivalEnergy: 16,
targetState: chargingstrategy.State{
Energy: 16,
},
Expand All @@ -871,7 +793,6 @@ func NewMockGraph4() Graph {
{
1,
chargeInfo{
arrivalEnergy: 0,
targetState: chargingstrategy.State{
Energy: 16,
},
Expand All @@ -884,7 +805,6 @@ func NewMockGraph4() Graph {
{
2,
chargeInfo{
arrivalEnergy: 0,
targetState: chargingstrategy.State{
Energy: 16,
},
Expand All @@ -897,7 +817,6 @@ func NewMockGraph4() Graph {
{
3,
chargeInfo{
arrivalEnergy: 0,
targetState: chargingstrategy.State{
Energy: 16,
},
Expand All @@ -910,7 +829,6 @@ func NewMockGraph4() Graph {
{
4,
chargeInfo{
arrivalEnergy: 0,
targetState: chargingstrategy.State{
Energy: 16,
},
Expand All @@ -923,7 +841,6 @@ func NewMockGraph4() Graph {
{
5,
chargeInfo{
arrivalEnergy: 0,
targetState: chargingstrategy.State{
Energy: 16,
},
Expand All @@ -936,7 +853,6 @@ func NewMockGraph4() Graph {
{
6,
chargeInfo{
arrivalEnergy: 0,
targetState: chargingstrategy.State{
Energy: 16,
},
Expand All @@ -949,7 +865,6 @@ func NewMockGraph4() Graph {
{
7,
chargeInfo{
arrivalEnergy: 0,
targetState: chargingstrategy.State{
Energy: 16,
},
Expand All @@ -962,7 +877,6 @@ func NewMockGraph4() Graph {
{
8,
chargeInfo{
arrivalEnergy: 0,
targetState: chargingstrategy.State{
Energy: 0,
},
Expand Down
9 changes: 7 additions & 2 deletions integration/service/oasis/stationgraph/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,13 @@ func (n *node) updateChargingTime(chargingTime float64) {
}

func (n *node) updateArrivalEnergy(prev *node, distance float64) {
n.arrivalEnergy = prev.targetState.Energy - distance
if n.arrivalEnergy < 0 {
n.arrivalEnergy = calculateArrivalEnergy(prev, distance)
}

func calculateArrivalEnergy(prev *node, distance float64) float64 {
energy := prev.targetState.Energy - distance
if energy < 0 {
glog.Fatalf("Before updateNode should check reachableByDistance() prev.arrivalEnergy=%#v distance=%#v", prev.arrivalEnergy, distance)
}
return energy
}
Loading

0 comments on commit e1b26fb

Please sign in to comment.