| 1 |
// Copyright 2026 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
package graph |
| 6 |
|
| 7 |
import ( |
| 8 |
"slices" |
| 9 |
"testing" |
| 10 |
) |
| 11 |
|
| 12 |
func TestTranspose(t *testing.T) { |
| 13 |
g := &stringGraph{ |
| 14 |
"a": {"b", "c"}, |
| 15 |
"b": {"c"}, |
| 16 |
"c": {"a"}, |
| 17 |
} |
| 18 |
|
| 19 |
tg := Transpose(g) |
| 20 |
|
| 21 |
if tg.NumNodes() != 3 { |
| 22 |
t.Errorf("tg.NumNodes() = %d, want 3", tg.NumNodes()) |
| 23 |
} |
| 24 |
|
| 25 |
nodes := slices.Sorted(tg.Nodes()) |
| 26 |
if want := []string{"a", "b", "c"}; !slices.Equal(nodes, want) { |
| 27 |
t.Errorf("tg.Nodes() = %v, want %v", nodes, want) |
| 28 |
} |
| 29 |
|
| 30 |
wantEdges := map[string][]string{ |
| 31 |
"a": {"c"}, |
| 32 |
"b": {"a"}, |
| 33 |
"c": {"a", "b"}, |
| 34 |
} |
| 35 |
|
| 36 |
for _, n := range []string{"a", "b", "c"} { |
| 37 |
outs := slices.Sorted(tg.Out(n)) |
| 38 |
want := wantEdges[n] |
| 39 |
if !slices.Equal(outs, want) { |
| 40 |
t.Errorf("tg.Out(%q) = %v, want %v", n, outs, want) |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
// Double transpose should return the original graph. |
| 45 |
ttg := Transpose(tg) |
| 46 |
|
| 47 |
if ttg != g { |
| 48 |
t.Errorf("Transpose(Transpose(g)) != g") |
| 49 |
} |
| 50 |
} |