summaryrefslogtreecommitdiffstats
path: root/src/internal/dag/alg_test.go
blob: e5ea8b6ab669b0e85c188f01dc74048ae8bd37f0 (plain)
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
// 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"
)

func TestTranspose(t *testing.T) {
	g := mustParse(t, diamond)
	g.Transpose()
	wantEdges(t, g, "a->b a->c a->d b->d c->d")
}

func TestTopo(t *testing.T) {
	g := mustParse(t, diamond)
	got := g.Topo()
	// "d" is the root, so it's first.
	//
	// "c" and "b" could be in either order, but Topo is
	// deterministic in reverse node definition order.
	//
	// "a" is a leaf.
	wantNodes := strings.Fields("d c b a")
	if !reflect.DeepEqual(wantNodes, got) {
		t.Fatalf("want topo sort %v, got %v", wantNodes, got)
	}
}

func TestTransitiveReduction(t *testing.T) {
	t.Run("diamond", func(t *testing.T) {
		g := mustParse(t, diamond)
		g.TransitiveReduction()
		wantEdges(t, g, "b->a c->a d->b d->c")
	})
	t.Run("chain", func(t *testing.T) {
		const chain = `NONE < a < b < c < d; a, d < e;`
		g := mustParse(t, chain)
		g.TransitiveReduction()
		wantEdges(t, g, "e->d d->c c->b b->a")
	})
}