Debian Codegraph @

internal/graph/order_test.go

raw symbols SCIP
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 graphscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/
6
7 import (
8 "reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/"
9 "testingscip-go gomod github.com/golang/go/src go1.25.0 testing/"
10 )
11
12 func TestPostorderscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/TestPostorder().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) {
13 tests := []struct {
14 name string
15 numNodes int
16 adj map[int][]int
17 want []int
18 }{
19 {
20 name: "Simple DAG",
21 numNodes: 3,
22 adj: map[int][]int{
23 0: {1, 2},
24 1: {2},
25 },
26 want: []int{2, 1, 0},
27 },
28 {
29 name: "Stability Check (Post-order)",
30 numNodes: 4,
31 adj: map[int][]int{
32 0: {2},
33 1: {2},
34 2: {3},
35 },
36 // DFS Walk:
37 // 1. Start at 0 -> visit 2 -> visit 3. Post-order: [3, 2, 0]
38 // 2. Start at 1 -> visit 2 (visited). Post-order: [3, 2, 0, 1]
39 want: []int{3, 2, 0, 1},
40 },
41 {
42 name: "Disconnected Components",
43 numNodes: 4,
44 adj: map[int][]int{
45 0: {1},
46 2: {3},
47 },
48 // DFS Walk:
49 // 1. Start at 0 -> visit 1. Post: [1, 0]
50 // 2. Start at 2 -> visit 3. Post: [1, 0, 3, 2]
51 want: []int{1, 0, 3, 2},
52 },
53 {
54 name: "Simple Cycle Resolution",
55 numNodes: 3,
56 adj: map[int][]int{
57 0: {1},
58 1: {2},
59 2: {0},
60 },
61 // Start 0 -> 1 -> 2 -> 0 (on stack, skip).
62 // Post: [2, 1, 0].
63 want: []int{2, 1, 0},
64 },
65 {
66 name: "Empty Graph",
67 numNodes: 0,
68 adj: map[int][]int{},
69 want: nil,
70 },
71 {
72 name: "Single Node",
73 numNodes: 1,
74 adj: map[int][]int{},
75 want: []int{0},
76 },
77 }
78
79 for _, tt := range tests {
80 t.Runscip-go gomod github.com/golang/go/src go1.25.0 testing/T#Run().(tt.name, func(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) {
81 g := newTestGraphscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/newTestGraph().(tt.numNodes, tt.adj)
82 got := Postorderscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/Postorder().(g)
83 if !reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.DeepEqualscip-go gomod github.com/golang/go/src go1.25.0 reflect/DeepEqual().(got, tt.want) {
84 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("got %v, want %v", got, tt.want)
85 }
86 })
87 }
88 }
89
90 func TestPostorder_LargeChainscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/TestPostorder_LargeChain().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) {
91 // Create a large enough graph that super-linear behavior would become a problem.
92 n := 10000
93 adj := make(map[int][]int)
94 var want []int
95 for i := range n {
96 if i < n-1 {
97 adj[int(i)] = []int{i + 1}
98 }
99 // Postorder of 0->1->2... is [n-1, n-2, ..., 0]
100 // Because we visit children fully before adding parent.
101 }
102 // Construct expected want:
103 for i := range n {
104 want = append(want, n-1-i)
105 }
106
107 g := newTestGraphscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/newTestGraph().(n, adj)
108 got := Postorderscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/Postorder().(g)
109 if !reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.DeepEqualscip-go gomod github.com/golang/go/src go1.25.0 reflect/DeepEqual().(got, want) {
110 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Large chain failed: got %d elements, first is %v", len(got), got[0])
111 }
112 }
113
114 func TestReversePostorderscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/TestReversePostorder().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) {
115 g := newTestGraphscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/newTestGraph().(3, map[int][]int{
116 0: {1, 2},
117 1: {2},
118 })
119 // Postorder: [2, 1, 0]
120 // ReversePostorder: [0, 1, 2]
121 want := []int{0, 1, 2}
122 got := ReversePostorderscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/ReversePostorder().(g)
123 if !reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.DeepEqualscip-go gomod github.com/golang/go/src go1.25.0 reflect/DeepEqual().(got, want) {
124 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("ReversePostorder: got %v, want %v", got, want)
125 }
126 }