Skip to content

Commit

Permalink
epsilon for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
LdDl committed Jun 6, 2023
1 parent 2913403 commit b53af98
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 34 deletions.
14 changes: 9 additions & 5 deletions bidirectional_ch_n_to_n_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,14 @@ func TestManyToManyShortestPath(t *testing.T) {
// t.Log("ShortestPathManyToMany returned", ans, path)
for sourceIdx := range u {
for targetIdx := range v {
// @failing
if correctPath[sourceIdx][targetIdx] != -2 && len(path[sourceIdx][targetIdx]) != correctPath[sourceIdx][targetIdx] {
t.Errorf("Num of vertices in path should be %d, but got %d", correctPath[sourceIdx][targetIdx], len(path[sourceIdx][targetIdx]))
return
}
if correctAns[sourceIdx][targetIdx] != -2 && Round(ans[sourceIdx][targetIdx], 0.00005) != Round(correctAns[sourceIdx][targetIdx], 0.00005) {
t.Errorf("Length of path should be %f, but got %f", correctAns[sourceIdx][targetIdx], ans[sourceIdx][targetIdx])
// @failing
if correctAns[sourceIdx][targetIdx] != -2 && math.Abs(ans[sourceIdx][targetIdx]-correctAns[sourceIdx][targetIdx]) > eps {
t.Errorf("Cost of path should be %f, but got %f", correctAns[sourceIdx][targetIdx], ans[sourceIdx][targetIdx])
return
}
}
Expand Down Expand Up @@ -194,9 +196,11 @@ func TestManyToManyAlternatives(t *testing.T) {
t.Errorf("Path item %d should be %d, but got %d", i, expectedPath[i], path[i])
}
}
if Round(ans[0][0], 0.00005) != Round(4.0, 0.00005) {
t.Errorf("Length of path should be 4.0, but got %f", ans)
correctCost := 4.0
// @failing
if math.Abs(ans[0][0]-correctCost) > eps {
t.Errorf("Cost of path should be %f, but got %f", correctCost, ans[0][0])
return
}

t.Log("TestManyToManyAlternatives is Ok!")
}
10 changes: 6 additions & 4 deletions bidirectional_ch_one_to_n_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ func TestOneToManyShortestPath(t *testing.T) {
}
}
for i := range ans {
if Round(ans[i], 0.00005) != Round(correctAns[i], 0.00005) {
t.Errorf("Length of path should be %f, but got %f", correctAns[i], ans[i])
if math.Abs(ans[i]-correctAns[i]) > eps {
t.Errorf("Cost of path should be %f, but got %f", correctAns[i], ans[i])
return
}
}
Expand Down Expand Up @@ -244,8 +244,10 @@ func TestOneToManyAlternatives(t *testing.T) {
t.Errorf("Path item %d should be %d, but got %d", i, expectedPath[i], path[i])
}
}
if Round(ans[0], 0.00005) != Round(4.0, 0.00005) {
t.Errorf("Length of path should be 4.0, but got %f", ans)
correctCost := 4.0
if math.Abs(ans[0]-correctCost) > eps {
t.Errorf("Cost of path should be %f, but got %f", correctCost, ans[0])
return
}

t.Log("TestOneToManyAlternatives is Ok!")
Expand Down
35 changes: 21 additions & 14 deletions bidirectional_ch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ func TestShortestPath(t *testing.T) {
t.Errorf("Num of vertices in path should be 160, but got %d", len(path))
return
}
if Round(ans, 0.00005) != Round(19135.6581215226, 0.00005) {
t.Errorf("Length of path should be 19135.6581215226, but got %f", ans)
correctCost := 19135.6581215226
if math.Abs(ans-correctCost) > eps {
t.Errorf("Cost of path should be %f, but got %f", correctCost, ans)
return
}
t.Log("TestShortestPath is Ok!")
Expand All @@ -59,8 +60,8 @@ func TestBothVanillaAndCH(t *testing.T) {
t.Errorf("Num of vertices in path should be %d, but got %d", len(pathVanilla), len(pathCH))
return
}
if Round(ansCH, 0.00005) != Round(ansVanilla, 0.00005) {
t.Errorf("Length of path should be %f, but got %f", ansVanilla, ansCH)
if math.Abs(ansCH-ansVanilla) > eps {
t.Errorf("Cost of path should be %f, but got %f", ansVanilla, ansCH)
return
}
}
Expand Down Expand Up @@ -145,8 +146,9 @@ func TestBadSpatialShortestPath(t *testing.T) {
t.Errorf("Num of vertices in path should be 5, but got %d", len(path))
return
}
if Round(ans, 0.00005) != Round(2.348720, 0.00005) {
t.Errorf("Length of path should be 2.348720, but got %f", ans)
correctCost := 2.348720
if math.Abs(ans-correctCost) > eps {
t.Errorf("Cost of path should be %f, but got %f", correctCost, ans)
return
}
t.Log("TestBadSpatialShortestPath is Ok!")
Expand Down Expand Up @@ -192,8 +194,11 @@ func TestLittleShortestPath(t *testing.T) {
if len(path) != 7 {
t.Errorf("Num of vertices in path should be 7, but got %d", len(path))
}
if Round(ans, 0.00005) != Round(20.5, 0.00005) {
t.Errorf("Length of path should be 20.0, but got %f", ans)

correctCost := 20.5
if math.Abs(ans-correctCost) > eps {
t.Errorf("Cost of path should be %f, but got %f", correctCost, ans)
return
}
t.Log("TestLittleShortestPath is Ok!")
}
Expand Down Expand Up @@ -242,10 +247,11 @@ func TestVertexAlternatives(t *testing.T) {
t.Errorf("Path item %d should be %d, but got %d", i, expectedPath[i], path[i])
}
}
if Round(ans, 0.00005) != Round(4.0, 0.00005) {
t.Errorf("Length of path should be 4.0, but got %f", ans)
correctCost := 4.0
if math.Abs(ans-correctCost) > eps {
t.Errorf("Cost of path should be %f, but got %f", correctCost, ans)
return
}

t.Log("TestVertexAlternatives is Ok!")
}

Expand Down Expand Up @@ -283,10 +289,11 @@ func TestVertexAlternativesConnected(t *testing.T) {
t.Errorf("Path item %d should be %d, but got %d", i, expectedPath[i], path[i])
}
}
if Round(ans, 0.00005) != Round(2.0, 0.00005) {
t.Errorf("Length of path should be 2.0, but got %f", ans)
correctCost := 2.0
if math.Abs(ans-correctCost) > eps {
t.Errorf("Cost of path should be %f, but got %f", correctCost, ans)
return
}

t.Log("TestVertexAlternativesConnected is Ok!")
}

Expand Down
6 changes: 4 additions & 2 deletions import_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ch

import (
"math"
"testing"
)

Expand Down Expand Up @@ -29,8 +30,9 @@ func TestImportedFileShortestPath(t *testing.T) {
t.Errorf("Num of vertices in path should be 160, but got %d", len(path))
return
}
if Round(ans, 0.00005) != Round(19135.6581215226, 0.00005) {
t.Errorf("Length of path should be 19135.6581215226, but got %f", ans)
correctCost := 19135.6581215226
if math.Abs(ans-correctCost) > eps {
t.Errorf("Cost of path should be %f, but got %f", correctCost, ans)
return
}
t.Log("TestImportedFileShortestPath is Ok!")
Expand Down
17 changes: 8 additions & 9 deletions vanilla_dijkstra_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package ch

import (
"math"
"testing"
)

const (
eps = 0.0001
)

func TestVanillaShortestPath(t *testing.T) {
g := Graph{}
err := graphFromCSV(&g, "data/pgrouting_osm.csv")
Expand All @@ -18,16 +23,10 @@ func TestVanillaShortestPath(t *testing.T) {
t.Errorf("Num of vertices in path should be 160, but got %d", len(path))
return
}
if Round(ans, 0.00005) != Round(19135.6581215226, 0.00005) {
t.Errorf("Length of path should be 19135.6581215226, but got %f", ans)
correctCost := 19135.6581215226
if math.Abs(ans-correctCost) > eps {
t.Errorf("Cost of path should be %f, but got %f", correctCost, ans)
return
}
t.Log("TestVanillaShortestPath is Ok!")
}

func Round(x, unit float64) float64 {
if x > 0 {
return float64(int64(x/unit+0.5)) * unit
}
return float64(int64(x/unit-0.5)) * unit
}

0 comments on commit b53af98

Please sign in to comment.