From b53af9810b4051afae6913449f5c11dddc125cd5 Mon Sep 17 00:00:00 2001 From: Dimitrii Date: Tue, 6 Jun 2023 14:41:35 +0300 Subject: [PATCH] epsilon for tests --- bidirectional_ch_n_to_n_test.go | 14 ++++++++----- bidirectional_ch_one_to_n_test.go | 10 +++++---- bidirectional_ch_test.go | 35 ++++++++++++++++++------------- import_test.go | 6 ++++-- vanilla_dijkstra_test.go | 17 +++++++-------- 5 files changed, 48 insertions(+), 34 deletions(-) diff --git a/bidirectional_ch_n_to_n_test.go b/bidirectional_ch_n_to_n_test.go index b05f115..aa9061b 100644 --- a/bidirectional_ch_n_to_n_test.go +++ b/bidirectional_ch_n_to_n_test.go @@ -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 } } @@ -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!") } diff --git a/bidirectional_ch_one_to_n_test.go b/bidirectional_ch_one_to_n_test.go index 039abac..d3ff1ed 100644 --- a/bidirectional_ch_one_to_n_test.go +++ b/bidirectional_ch_one_to_n_test.go @@ -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 } } @@ -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!") diff --git a/bidirectional_ch_test.go b/bidirectional_ch_test.go index a415d7c..be7e9b6 100644 --- a/bidirectional_ch_test.go +++ b/bidirectional_ch_test.go @@ -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!") @@ -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 } } @@ -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!") @@ -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!") } @@ -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!") } @@ -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!") } diff --git a/import_test.go b/import_test.go index 9208d5e..42305fe 100644 --- a/import_test.go +++ b/import_test.go @@ -1,6 +1,7 @@ package ch import ( + "math" "testing" ) @@ -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!") diff --git a/vanilla_dijkstra_test.go b/vanilla_dijkstra_test.go index 671abcb..b29989b 100644 --- a/vanilla_dijkstra_test.go +++ b/vanilla_dijkstra_test.go @@ -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") @@ -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 -}