-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShow_test.go
More file actions
92 lines (79 loc) · 2.25 KB
/
Copy pathShow_test.go
File metadata and controls
92 lines (79 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// SPDX-License-Identifier: MIT
// Copyright (c) 2026 MaIII Themd
package dijkstra
import (
"bytes"
"strings"
"testing"
)
func TestShowDefault(t *testing.T) {
var g StGraph
g.VertexAdd("A", 0, 0, 0)
g.VertexAdd("B", 1, 0, 0)
g.VertexAddEdge("A", "B", 2.5, true) // one-way
var buf bytes.Buffer
g.Show(&buf)
out := buf.String()
if !strings.Contains(out, "Vertex A") || !strings.Contains(out, "Vertex B") {
t.Errorf("Show missing vertices:\n%s", out)
}
if !strings.Contains(out, "(B, 2.50") {
t.Errorf("Show missing edge weight:\n%s", out)
}
if !strings.Contains(out, "one-way") {
t.Errorf("Show should mark one-way edges:\n%s", out)
}
}
func TestShowFuncCustom(t *testing.T) {
var g StGraph
g.VertexAdd("A", 0, 0, 0)
g.VertexAdd("B", 1, 0, 0)
var buf bytes.Buffer
g.ShowFunc(&buf, func(v StVertex) string { return v.Name + "\n" })
if got, want := buf.String(), "A\nB\n"; got != want {
t.Errorf("ShowFunc custom = %q, want %q", got, want)
}
}
func TestShowFuncNilUsesDefault(t *testing.T) {
var g StGraph
g.VertexAdd("A", 0, 0, 0)
var a, b bytes.Buffer
g.Show(&a)
g.ShowFunc(&b, nil)
if a.String() != b.String() {
t.Errorf("ShowFunc(nil) should equal Show:\n%q\n%q", a.String(), b.String())
}
}
func TestPathString(t *testing.T) {
var g StGraph
g.VertexAdd("A", 0, 0, 0)
g.VertexAdd("B", 1, 0, 0)
g.VertexAdd("C", 2, 0, 0)
for _, e := range [][3]interface{}{{"A", "B", 1.0}, {"B", "C", 2.0}} {
g.VertexAddEdge(e[0].(string), e[1].(string), e[2].(float64), false)
g.VertexAddEdge(e[1].(string), e[0].(string), e[2].(float64), false)
}
ok, path := g.DijkstraSearch("A", "C")
if !ok {
t.Fatal("expected a path")
}
if got, want := PathString(path), "A -> B -> C (cost 3)"; got != want {
t.Errorf("PathString = %q, want %q", got, want)
}
if got := PathString(nil); got != "(no path)" {
t.Errorf("PathString(nil) = %q, want %q", got, "(no path)")
}
}
func TestShowDijkstra(t *testing.T) {
var g StGraph
g.VertexAdd("A", 0, 0, 0)
g.VertexAdd("B", 1, 0, 0)
g.VertexAddEdge("A", "B", 1, false)
g.VertexAddEdge("B", "A", 1, false)
g.DijkstraSearch("A", "B")
var buf bytes.Buffer
g.ShowDijkstra(&buf)
if !strings.Contains(buf.String(), "parent A") {
t.Errorf("ShowDijkstra should report B's parent as A:\n%s", buf.String())
}
}