| 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 |
"maps" |
| 9 |
"testing" |
| 10 |
) |
| 11 |
|
| 12 |
func TestReachable(t *testing.T) { |
| 13 |
g := stringGraph{ |
| 14 |
"A": {"B", "C"}, |
| 15 |
"B": {"D"}, |
| 16 |
"C": {"D"}, |
| 17 |
"D": {}, |
| 18 |
"E": {"F"}, |
| 19 |
"F": {}, |
| 20 |
// Cycle |
| 21 |
"c1": {"c2"}, |
| 22 |
"c2": {"c3"}, |
| 23 |
"c3": {"c2", "c4"}, |
| 24 |
"c4": {}, |
| 25 |
// Self-cycle |
| 26 |
"sc1": {"sc1", "sc2"}, |
| 27 |
"sc2": {}, |
| 28 |
} |
| 29 |
|
| 30 |
got := Reachable(g, "A") |
| 31 |
want := map[string]bool{"A": true, "B": true, "C": true, "D": true} |
| 32 |
if !maps.Equal(got, want) { |
| 33 |
t.Errorf("Reachable(A) = %v, want %v", got, want) |
| 34 |
} |
| 35 |
|
| 36 |
got = Reachable(g, "E") |
| 37 |
want = map[string]bool{"E": true, "F": true} |
| 38 |
if !maps.Equal(got, want) { |
| 39 |
t.Errorf("Reachable(E) = %v, want %v", got, want) |
| 40 |
} |
| 41 |
|
| 42 |
got = Reachable(g, "c1") |
| 43 |
want = map[string]bool{"c1": true, "c2": true, "c3": true, "c4": true} |
| 44 |
if !maps.Equal(got, want) { |
| 45 |
t.Errorf("Reachable(c1) = %v, want %v", got, want) |
| 46 |
} |
| 47 |
|
| 48 |
got = Reachable(g, "sc1") |
| 49 |
want = map[string]bool{"sc1": true, "sc2": true} |
| 50 |
if !maps.Equal(got, want) { |
| 51 |
t.Errorf("Reachable(sc1) = %v, want %v", got, want) |
| 52 |
} |
| 53 |
} |