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
|
// Copyright 2022 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package dag
import (
"reflect"
"strings"
"testing"
)
const diamond = `
NONE < a < b, c < d;
`
func mustParse(t *testing.T, dag string) *Graph {
t.Helper()
g, err := Parse(dag)
if err != nil {
t.Fatal(err)
}
return g
}
func wantEdges(t *testing.T, g *Graph, edges string) {
t.Helper()
wantEdges := strings.Fields(edges)
wantEdgeMap := make(map[string]bool)
for _, e := range wantEdges {
wantEdgeMap[e] = true
}
for _, n1 := range g.Nodes {
for _, n2 := range g.Nodes {
got := g.HasEdge(n1, n2)
want := wantEdgeMap[n1+"->"+n2]
if got && want {
t.Logf("%s->%s", n1, n2)
} else if got && !want {
t.Errorf("%s->%s present but not expected", n1, n2)
} else if want && !got {
t.Errorf("%s->%s missing but expected", n1, n2)
}
}
}
}
func TestParse(t *testing.T) {
// Basic smoke test for graph parsing.
g := mustParse(t, diamond)
wantNodes := strings.Fields("a b c d")
if !reflect.DeepEqual(wantNodes, g.Nodes) {
t.Fatalf("want nodes %v, got %v", wantNodes, g.Nodes)
}
// Parse returns the transitive closure, so it adds d->a.
wantEdges(t, g, "b->a c->a d->a d->b d->c")
}
|