| 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_test |
| 6 | |
| 7 | import ( |
| 8 | "iter" |
| 9 | "math/rand" |
| 10 | "slices" |
| 11 | "testing" |
| 12 | "time" |
| 13 | |
| 14 | "golang.org/x/tools/internal/flow" |
| 15 | ) |
| 16 | |
| 17 | type simpleGraph struct { |
| 18 | numNodes int |
| 19 | edges [][]int |
| 20 | } |
| 21 | |
| 22 | func (g *simpleGraph) NumNodes() int { |
| 23 | return g.numNodes |
| 24 | } |
| 25 | |
| 26 | func (g *simpleGraph) Nodes() iter.Seq[int] { |
| 27 | return func(yield func(int) bool) { |
| 28 | for i := range g.numNodes { |
| 29 | if !yield(i) { |
| 30 | break |
| 31 | } |
| 32 | } |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | func (g *simpleGraph) Out(nid int) iter.Seq[int] { |
| 37 | return slices.Values(g.edges[nid]) |
| 38 | } |
| 39 | |
| 40 | func checkAnalysis[Fact comparable](t *testing.T, analysis *flow.Analysis[Fact, int], wantIns map[int]Fact, wantEdges map[[2]int]Fact) { |
| 41 | t.Helper() |
| 42 | for nid, want := range wantIns { |
| 43 | if got := analysis.In(nid); got != want { |
| 44 | t.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.Edge(from, to); got != want { |
| 51 | t.Errorf("Edge(%d, %d): got %v, want %v", from, to, got, want) |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | func TestBasic(t *testing.T) { |
| 57 | // Define a trivial graph of 4 nodes in sequence: 0 -> 1 -> 2 -> 3 |
| 58 | g := &simpleGraph{ |
| 59 | numNodes: 4, |
| 60 | 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 nodeSet) nodeSet { |
| 70 | return fact | (1 << uint(from)) |
| 71 | } |
| 72 | |
| 73 | analysis := flow.Forward[nodeSetUnion](g, nil, transfer) |
| 74 | |
| 75 | checkAnalysis(t, analysis, |
| 76 | map[int]nodeSet{ |
| 77 | 0: 0, |
| 78 | 1: set(0), |
| 79 | 2: set(0, 1), |
| 80 | 3: set(0, 1, 2), |
| 81 | }, |
| 82 | map[[2]int]nodeSet{ |
| 83 | {0, 1}: set(0), |
| 84 | {1, 2}: set(0, 1), |
| 85 | {2, 3}: set(0, 1, 2), |
| 86 | }, |
| 87 | ) |
| 88 | } |
| 89 | |
| 90 | func TestDiamond(t *testing.T) { |
| 91 | // Diamond graph: |
| 92 | // 0 |
| 93 | // / \ |
| 94 | // 1 2 |
| 95 | // \ / |
| 96 | // 3 |
| 97 | g := &simpleGraph{ |
| 98 | numNodes: 4, |
| 99 | edges: [][]int{ |
| 100 | 0: {1, 2}, |
| 101 | 1: {3}, |
| 102 | 2: {3}, |
| 103 | 3: {}, |
| 104 | }, |
| 105 | } |
| 106 | |
| 107 | transfer := func(from, to int, fact nodeSet) nodeSet { |
| 108 | return fact | (1 << uint(from)) |
| 109 | } |
| 110 | |
| 111 | analysis := flow.Forward[nodeSetUnion](g, nil, transfer) |
| 112 | |
| 113 | checkAnalysis(t, analysis, |
| 114 | map[int]nodeSet{ |
| 115 | 0: 0, |
| 116 | 1: set(0), // Edge (0, 1) carries NodeID 0 |
| 117 | 2: set(0), // Edge (0, 2) carries NodeID 0 |
| 118 | 3: set(0, 1, 2), |
| 119 | }, |
| 120 | map[[2]int]nodeSet{ |
| 121 | {0, 1}: set(0), |
| 122 | {0, 2}: set(0), |
| 123 | {1, 3}: set(0, 1), |
| 124 | {2, 3}: set(0, 2), |
| 125 | }, |
| 126 | ) |
| 127 | } |
| 128 | |
| 129 | func TestCycle(t *testing.T) { |
| 130 | // Graph with a cycle: |
| 131 | // 0 -> 1 -> 2 |
| 132 | // ^ | |
| 133 | // +----+ |
| 134 | g := &simpleGraph{ |
| 135 | numNodes: 3, |
| 136 | edges: [][]int{ |
| 137 | 0: {1}, |
| 138 | 1: {2}, |
| 139 | 2: {1}, |
| 140 | }, |
| 141 | } |
| 142 | |
| 143 | transfer := func(from, to int, fact nodeSet) nodeSet { |
| 144 | return fact | (1 << uint(from)) |
| 145 | } |
| 146 | |
| 147 | analysis := flow.Forward[nodeSetUnion](g, nil, transfer) |
| 148 | |
| 149 | checkAnalysis(t, analysis, |
| 150 | map[int]nodeSet{ |
| 151 | 0: 0, |
| 152 | 1: set(0, 1, 2), |
| 153 | 2: set(0, 1, 2), |
| 154 | }, |
| 155 | map[[2]int]nodeSet{ |
| 156 | {0, 1}: set(0), |
| 157 | {1, 2}: set(0, 1, 2), |
| 158 | {2, 1}: set(0, 1, 2), |
| 159 | }, |
| 160 | ) |
| 161 | } |
| 162 | |
| 163 | func TestFlowOrder(t *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 := rand.New(rand.NewSource(42)).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 := &simpleGraph{ |
| 216 | numNodes: N, |
| 217 | 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 nodeSet) 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 := time.Now() |
| 233 | _ = flow.Forward[nodeSetUnion](g, nil, transfer) |
| 234 | durRPO := time.Since(start) |
| 235 | t.Logf("RPO time: %v", durRPO) |
| 236 | |
| 237 | // RPO should result in optimal evaluation, visiting each edge exactly once. |
| 238 | if transfers != nEdges { |
| 239 | t.Errorf("got %d transfer calls, expected %d for optimal evaluation strategy", transfers, nEdges) |
| 240 | } |
| 241 | } |