| 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 flow_testscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/flow_test`/ |
| 6 | |
| 7 | import ( |
| 8 | "iterscip-go gomod github.com/golang/go/src go1.25.0 iter/" |
| 9 | "math/randscip-go gomod github.com/golang/go/src go1.25.0 `math/rand`/" |
| 10 | "slicesscip-go gomod github.com/golang/go/src go1.25.0 slices/" |
| 11 | "testingscip-go gomod github.com/golang/go/src go1.25.0 testing/" |
| 12 | "timescip-go gomod github.com/golang/go/src go1.25.0 time/" |
| 13 | |
| 14 | "golang.org/x/tools/internal/flowscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/flow`/" |
| 15 | ) |
| 16 | |
| 17 | type simpleGraphscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/flow_test`/simpleGraph# struct { |
| 18 | numNodesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/flow_test`/simpleGraph#numNodes. int |
| 19 | edgesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/flow_test`/simpleGraph#edges. [][]int |
| 20 | } |
| 21 | |
| 22 | func (g *simpleGraphscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/flow_test`/simpleGraph#) NumNodesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/flow_test`/simpleGraph#NumNodes().() int { |
| 23 | return g.numNodesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/flow_test`/simpleGraph#numNodes. |
| 24 | } |
| 25 | |
| 26 | func (g *simpleGraphscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/flow_test`/simpleGraph#) Nodesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/flow_test`/simpleGraph#Nodes().() iterscip-go gomod github.com/golang/go/src go1.25.0 iter/.Seqscip-go gomod github.com/golang/go/src go1.25.0 iter/Seq#[int] { |
| 27 | return func(yield func(int) bool) { |
| 28 | for i := range g.numNodesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/flow_test`/simpleGraph#numNodes. { |
| 29 | if !yield(i) { |
| 30 | break |
| 31 | } |
| 32 | } |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | func (g *simpleGraphscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/flow_test`/simpleGraph#) Outscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/flow_test`/simpleGraph#Out().(nid int) iterscip-go gomod github.com/golang/go/src go1.25.0 iter/.Seqscip-go gomod github.com/golang/go/src go1.25.0 iter/Seq#[int] { |
| 37 | return slicesscip-go gomod github.com/golang/go/src go1.25.0 slices/.Valuesscip-go gomod github.com/golang/go/src go1.25.0 slices/Values().(g.edgesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/flow_test`/simpleGraph#edges.[nid]) |
| 38 | } |
| 39 | |
| 40 | func checkAnalysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/flow_test`/checkAnalysis().[Fact comparable](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#, analysis *flowscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/flow`/.Analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/flow`/Analysis#[Fact, int], wantIns map[int]Fact, wantEdges map[[2]int]Fact) { |
| 41 | t.Helperscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Helper().() |
| 42 | for nid, want := range wantIns { |
| 43 | if got := analysis.Inscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/flow`/Analysis#In().(nid); got != want { |
| 44 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("In(%d): got %v, want %v", nid, got, want) |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | for edge, want := range wantEdges { |
| 49 | from, to := edge[0], edge[1] |
| 50 | if got := analysis.Edgescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/flow`/Analysis#Edge().(from, to); got != want { |
| 51 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Edge(%d, %d): got %v, want %v", from, to, got, want) |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | func TestBasicscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/flow_test`/TestBasic().(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#) { |
| 57 | // Define a trivial graph of 4 nodes in sequence: 0 -> 1 -> 2 -> 3 |
| 58 | g := &simpleGraphscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/flow_test`/simpleGraph#{ |
| 59 | numNodesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/flow_test`/simpleGraph#numNodes.: 4, |
| 60 | edgesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/flow_test`/simpleGraph#edges.: [][]int{ |
| 61 | 0: {1}, |
| 62 | 1: {2}, |
| 63 | 2: {3}, |
| 64 | 3: {}, |
| 65 | }, |
| 66 | } |
| 67 | |
| 68 | // Transfer function: OR source node's ID into the bitset. |
| 69 | transfer := func(from, to int, fact nodeSetscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/flow_test`/nodeSet#) nodeSetscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/flow_test`/nodeSet# { |
| 70 | return fact | (1 << uint(from)) |
| 71 | } |
| 72 | |
| 73 | analysis := flowscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/flow`/.Forwardscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/flow`/Forward().[nodeSetUnionscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/flow_test`/nodeSetUnion#](g, nil, transfer) |
| 74 | |
| 75 | checkAnalysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/flow_test`/checkAnalysis().(t, analysis, |
| 76 | map[int]nodeSetscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/flow_test`/nodeSet#{ |
| 77 | 0: 0, |
| 78 | 1: setscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/flow_test`/set().(0), |
| 79 | 2: setscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/flow_test`/set().(0, 1), |
| 80 | 3: setscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/flow_test`/set().(0, 1, 2), |
| 81 | }, |
| 82 | map[[2]int]nodeSetscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/flow_test`/nodeSet#{ |
| 83 | {0, 1}: setscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/flow_test`/set().(0), |
| 84 | {1, 2}: setscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/flow_test`/set().(0, 1), |
| 85 | {2, 3}: setscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/flow_test`/set().(0, 1, 2), |
| 86 | }, |
| 87 | ) |
| 88 | } |
| 89 | |
| 90 | func TestDiamondscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/flow_test`/TestDiamond().(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 | // Diamond graph: |
| 92 | // 0 |
| 93 | // / \ |
| 94 | // 1 2 |
| 95 | // \ / |
| 96 | // 3 |
| 97 | g := &simpleGraphscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/flow_test`/simpleGraph#{ |
| 98 | numNodesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/flow_test`/simpleGraph#numNodes.: 4, |
| 99 | edgesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/flow_test`/simpleGraph#edges.: [][]int{ |
| 100 | 0: {1, 2}, |
| 101 | 1: {3}, |
| 102 | 2: {3}, |
| 103 | 3: {}, |
| 104 | }, |
| 105 | } |
| 106 | |
| 107 | transfer := func(from, to int, fact nodeSetscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/flow_test`/nodeSet#) nodeSetscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/flow_test`/nodeSet# { |
| 108 | return fact | (1 << uint(from)) |
| 109 | } |
| 110 | |
| 111 | analysis := flowscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/flow`/.Forwardscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/flow`/Forward().[nodeSetUnionscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/flow_test`/nodeSetUnion#](g, nil, transfer) |
| 112 | |
| 113 | checkAnalysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/flow_test`/checkAnalysis().(t, analysis, |
| 114 | map[int]nodeSetscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/flow_test`/nodeSet#{ |
| 115 | 0: 0, |
| 116 | 1: setscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/flow_test`/set().(0), // Edge (0, 1) carries NodeID 0 |
| 117 | 2: setscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/flow_test`/set().(0), // Edge (0, 2) carries NodeID 0 |
| 118 | 3: setscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/flow_test`/set().(0, 1, 2), |
| 119 | }, |
| 120 | map[[2]int]nodeSetscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/flow_test`/nodeSet#{ |
| 121 | {0, 1}: setscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/flow_test`/set().(0), |
| 122 | {0, 2}: setscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/flow_test`/set().(0), |
| 123 | {1, 3}: setscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/flow_test`/set().(0, 1), |
| 124 | {2, 3}: setscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/flow_test`/set().(0, 2), |
| 125 | }, |
| 126 | ) |
| 127 | } |
| 128 | |
| 129 | func TestCyclescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/flow_test`/TestCycle().(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#) { |
| 130 | // Graph with a cycle: |
| 131 | // 0 -> 1 -> 2 |
| 132 | // ^ | |
| 133 | // +----+ |
| 134 | g := &simpleGraphscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/flow_test`/simpleGraph#{ |
| 135 | numNodesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/flow_test`/simpleGraph#numNodes.: 3, |
| 136 | edgesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/flow_test`/simpleGraph#edges.: [][]int{ |
| 137 | 0: {1}, |
| 138 | 1: {2}, |
| 139 | 2: {1}, |
| 140 | }, |
| 141 | } |
| 142 | |
| 143 | transfer := func(from, to int, fact nodeSetscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/flow_test`/nodeSet#) nodeSetscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/flow_test`/nodeSet# { |
| 144 | return fact | (1 << uint(from)) |
| 145 | } |
| 146 | |
| 147 | analysis := flowscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/flow`/.Forwardscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/flow`/Forward().[nodeSetUnionscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/flow_test`/nodeSetUnion#](g, nil, transfer) |
| 148 | |
| 149 | checkAnalysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/flow_test`/checkAnalysis().(t, analysis, |
| 150 | map[int]nodeSetscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/flow_test`/nodeSet#{ |
| 151 | 0: 0, |
| 152 | 1: setscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/flow_test`/set().(0, 1, 2), |
| 153 | 2: setscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/flow_test`/set().(0, 1, 2), |
| 154 | }, |
| 155 | map[[2]int]nodeSetscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/flow_test`/nodeSet#{ |
| 156 | {0, 1}: setscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/flow_test`/set().(0), |
| 157 | {1, 2}: setscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/flow_test`/set().(0, 1, 2), |
| 158 | {2, 1}: setscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/flow_test`/set().(0, 1, 2), |
| 159 | }, |
| 160 | ) |
| 161 | } |
| 162 | |
| 163 | func TestFlowOrderscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/flow_test`/TestFlowOrder().(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#) { |
| 164 | // This test constructs a "Spine with Bottleneck" graph in which a naive |
| 165 | // visit order would result in quadratic time. |
| 166 | // |
| 167 | // Structure: |
| 168 | // - Spine: Chain 0 -> 1 -> ... -> T-1. |
| 169 | // - Bottleneck: All spine nodes (0..T-1) also point to T. |
| 170 | // - Tail: T begins a chain T -> T+1 -> ... -> N-1. |
| 171 | // |
| 172 | // However, we randomly permute the node IDs to prevent a "lucky" order |
| 173 | // derived from the node IDs. |
| 174 | // |
| 175 | // With a naive order, updates to T from the spine would arrive one by one |
| 176 | // (or in small batches), triggering re-evaluation of the entire tail |
| 177 | // multiple times, and O(T*N) transfer calls. |
| 178 | // |
| 179 | // With RPO, T is evaluated once after all spine nodes are processed. |
| 180 | |
| 181 | T := 64 // Max ID allowed by nodeSet |
| 182 | N := 2 * T |
| 183 | |
| 184 | // Permute IDs to prevent "lucky" ordering. |
| 185 | perm := randscip-go gomod github.com/golang/go/src go1.25.0 `math/rand`/.Newscip-go gomod github.com/golang/go/src go1.25.0 `math/rand`/New().(randscip-go gomod github.com/golang/go/src go1.25.0 `math/rand`/.NewSourcescip-go gomod github.com/golang/go/src go1.25.0 `math/rand`/NewSource().(42)).Permscip-go gomod github.com/golang/go/src go1.25.0 `math/rand`/Rand#Perm().(N) |
| 186 | invPerm := make([]int, N) |
| 187 | for i, p := range perm { |
| 188 | invPerm[p] = i |
| 189 | } |
| 190 | // node maps a logical node ID to a permuted NodeID. |
| 191 | node := func(logical int) int { |
| 192 | return int(perm[logical]) |
| 193 | } |
| 194 | |
| 195 | // Build the edges. |
| 196 | edges := make([][]int, N) |
| 197 | nEdges := 0 |
| 198 | addEdge := func(u, v int) { |
| 199 | uID := node(u) |
| 200 | edges[uID] = append(edges[uID], node(v)) |
| 201 | nEdges++ |
| 202 | } |
| 203 | // Spine: 0 -> 1 -> ... -> T-1 |
| 204 | for i := range T { |
| 205 | if i+1 != T { |
| 206 | addEdge(i, i+1) |
| 207 | } |
| 208 | addEdge(i, T) |
| 209 | } |
| 210 | // Tail: T -> T+1 -> ... -> N-1 |
| 211 | for i := T; i < N-2; i++ { |
| 212 | addEdge(i, i+1) |
| 213 | } |
| 214 | |
| 215 | g := &simpleGraphscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/flow_test`/simpleGraph#{ |
| 216 | numNodesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/flow_test`/simpleGraph#numNodes.: N, |
| 217 | edgesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/flow_test`/simpleGraph#edges.: edges, |
| 218 | } |
| 219 | |
| 220 | transfers := 0 |
| 221 | // Transfer: if pred is on the spine (< T), set its bit. |
| 222 | transfer := func(from, to int, in nodeSetscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/flow_test`/nodeSet#) nodeSetscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/flow_test`/nodeSet# { |
| 223 | transfers++ |
| 224 | logicalPred := invPerm[from] |
| 225 | if logicalPred < T { |
| 226 | return in | (1 << uint64(logicalPred)) |
| 227 | } |
| 228 | return in |
| 229 | } |
| 230 | |
| 231 | // Measure flow.Forward (RPO) |
| 232 | start := timescip-go gomod github.com/golang/go/src go1.25.0 time/.Nowscip-go gomod github.com/golang/go/src go1.25.0 time/Now().() |
| 233 | _ = flowscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/flow`/.Forwardscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/flow`/Forward().[nodeSetUnionscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/flow_test`/nodeSetUnion#](g, nil, transfer) |
| 234 | durRPO := timescip-go gomod github.com/golang/go/src go1.25.0 time/.Sincescip-go gomod github.com/golang/go/src go1.25.0 time/Since().(start) |
| 235 | t.Logfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Logf().("RPO time: %v", durRPO) |
| 236 | |
| 237 | // RPO should result in optimal evaluation, visiting each edge exactly once. |
| 238 | if transfers != nEdges { |
| 239 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("got %d transfer calls, expected %d for optimal evaluation strategy", transfers, nEdges) |
| 240 | } |
| 241 | } |