Debian Codegraph @

path_test.go

raw symbols SCIP
1 package graph
2
3 import (
4 "math/rand"
5 "testing"
6 )
7
8 func TestShortestPath(t *testing.T) {
9 g := New(6)
10 g.AddCost(0, 1, 1)
11 g.AddCost(0, 2, 1)
12 g.AddCost(0, 3, 3)
13 g.AddCost(1, 3, 0)
14 g.AddCost(2, 3, 1)
15 g.AddCost(2, 5, 8)
16 g.AddCost(3, 5, 7)
17 g.AddCost(1, 5, -1)
18 parent, dist := ShortestPaths(g, 0)
19 expParent := []int{-1, 0, 0, 1, -1, 3}
20 expDist := []int64{0, 1, 1, 1, -1, 8}
21 if mess, diff := diff(parent, expParent); diff {
22 t.Errorf("ShortestPaths->parent %s", mess)
23 }
24 if mess, diff := diff(dist, expDist); diff {
25 t.Errorf("ShortestPaths->dist %s", mess)
26 }
27
28 path, d := ShortestPath(g, 0, 5)
29 if mess, diff := diff(path, []int{0, 1, 3, 5}); diff {
30 t.Errorf("ShortestPath->path %s", mess)
31 }
32 if mess, diff := diff(d, int64(8)); diff {
33 t.Errorf("ShortestPath->dist %s", mess)
34 }
35
36 path, d = ShortestPath(g, 0, 0)
37 if mess, diff := diff(path, []int{0}); diff {
38 t.Errorf("ShortestPath->path %s", mess)
39 }
40 if mess, diff := diff(d, int64(0)); diff {
41 t.Errorf("ShortestPath->dist %s", mess)
42 }
43
44 path, d = ShortestPath(g, 0, 4)
45 if mess, diff := diff(path, []int{}); diff {
46 t.Errorf("ShortestPath->path %s", mess)
47 }
48 if mess, diff := diff(d, int64(-1)); diff {
49 t.Errorf("ShortestPath->dist %s", mess)
50 }
51 }
52
53 func BenchmarkShortestPaths(b *testing.B) {
54 n := 1000
55 b.StopTimer()
56 g := New(n)
57 for i := 0; i < n; i++ {
58 g.Add(0, rand.Intn(n))
59 g.Add(rand.Intn(n), rand.Intn(n))
60 }
61 b.StartTimer()
62 for i := 0; i < b.N; i++ {
63 _, _ = ShortestPaths(g, 0)
64 }
65 }