github.com/LdDl/ch@v1.7.8/isochrones_test.go (about) 1 package ch 2 3 import ( 4 "testing" 5 ) 6 7 func TestIsochrones(t *testing.T) { 8 correctIsochrones := map[int64]float64{ 9 5: 0.0, 10 3: 1.0, 11 4: 1.0, 12 6: 1.0, 13 7: 3.0, 14 1: 3.0, 15 8: 4.0, // <---- Because of breadth-first search 16 9: 2.0, 17 } 18 graph := Graph{} 19 20 vertices := []V{ 21 {from: 5, to: 3, weight: 1.0}, 22 {from: 5, to: 4, weight: 1.0}, 23 {from: 5, to: 6, weight: 1.0}, 24 {from: 5, to: 7, weight: 2.0}, 25 {from: 3, to: 7, weight: 2.0}, 26 {from: 6, to: 9, weight: 1.0}, 27 {from: 7, to: 8, weight: 4.0}, 28 {from: 7, to: 3, weight: 2.0}, 29 {from: 9, to: 8, weight: 2.0}, 30 {from: 8, to: 10, weight: 3.0}, 31 {from: 3, to: 1, weight: 2.0}, 32 {from: 1, to: 2, weight: 3.0}, 33 {from: 4, to: 11, weight: 7.0}, 34 {from: 11, to: 2, weight: 2.0}, 35 {from: 2, to: 11, weight: 2.0}, 36 } 37 38 for i := range vertices { 39 err := graph.CreateVertex(vertices[i].from) 40 if err != nil { 41 t.Error(err) 42 return 43 } 44 err = graph.CreateVertex(vertices[i].to) 45 if err != nil { 46 t.Error(err) 47 return 48 } 49 err = graph.AddEdge(vertices[i].from, vertices[i].to, vertices[i].weight) 50 if err != nil { 51 t.Error(err) 52 return 53 } 54 } 55 56 graph.PrepareContractionHierarchies() // This is excess in current example, but just for proof that contraction map isn't used. 57 58 sourceVertex := int64(5) 59 maxCost := 5.0 60 isochrones, err := graph.Isochrones(sourceVertex, maxCost) 61 if err != nil { 62 t.Error(err) 63 return 64 } 65 66 if len(isochrones) != len(correctIsochrones) { 67 t.Errorf("Number of isochrones should be %d, but got %d", len(correctIsochrones), len(isochrones)) 68 return 69 } 70 71 for k, val := range isochrones { 72 correctValue, ok := correctIsochrones[k] 73 if !ok { 74 t.Errorf("Isochrones should contain vertex %d, but it does not", k) 75 return 76 } 77 if val != correctValue { 78 t.Errorf("Travel cost to vertex %d should be %f, but got %f", k, correctValue, val) 79 return 80 } 81 } 82 }