Debian Codegraph @

go/callgraph/vta/internal/trie/bits_test.go

raw symbols SCIP
1 // Copyright 2021 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 //go:build go1.13
6
7 package triescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta/internal/trie`/
8
9 import (
10 "math/randscip-go gomod github.com/golang/go/src go1.25.0 `math/rand`/"
11 "testingscip-go gomod github.com/golang/go/src go1.25.0 testing/"
12 )
13
14 func TestMaskscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta/internal/trie`/TestMask().(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#) {
15 for _, c := range []struct {
16 p prefixscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta/internal/trie`/prefix#
17 b bitposscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta/internal/trie`/bitpos#
18 want prefixscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta/internal/trie`/prefix#
19 }{
20 {
21 p: 0b00001000,
22 b: 0b00000100,
23 want: 0b00001011,
24 }, {
25 p: 0b01011011,
26 b: 0b00000000,
27 want: ^prefixscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta/internal/trie`/prefix#(0),
28 }, {
29 p: 0b01011011,
30 b: 0b00000001,
31 want: 0b01011010,
32 }, {
33 p: 0b01011011,
34 b: 0b00000010,
35 want: 0b01011001,
36 }, {
37 p: 0b01011011,
38 b: 0b00000100,
39 want: 0b01011011,
40 }, {
41 p: 0b01011011,
42 b: 0b00001000,
43 want: 0b01010111,
44 }, {
45 p: 0b01011011,
46 b: 0b00010000,
47 want: 0b01001111,
48 }, {
49 p: 0b01011011,
50 b: 0b00100000,
51 want: 0b01011111,
52 }, {
53 p: 0b01011011,
54 b: 0b01000000,
55 want: 0b00111111,
56 }, {
57 p: 0b01011011,
58 b: 0b01000000,
59 want: 0b00111111,
60 }, {
61 p: 0b01011011,
62 b: 0b10000000,
63 want: 0b01111111,
64 },
65 } {
66 if got := maskscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta/internal/trie`/mask().(c.p, c.b); got != c.want {
67 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("mask(%#b,%#b) got %#b. want %#b", c.p, c.b, got, c.want)
68 }
69 }
70 }
71
72 func TestMaskImpotentscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta/internal/trie`/TestMaskImpotent().(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#) {
73 // test mask(mask(p, b), b) == mask(p,b)
74 for _, p := range []prefixscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta/internal/trie`/prefix#{
75 0b0, 0b1, 0b100, ^prefixscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta/internal/trie`/prefix#(0b0), ^prefixscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta/internal/trie`/prefix#(0b10),
76 } {
77 for _, b := range []bitposscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta/internal/trie`/bitpos#{
78 0, 0b1, 1 << 2, 1 << 63,
79 } {
80 once := maskscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta/internal/trie`/mask().(p, b)
81 twice := maskscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta/internal/trie`/mask().(once, b)
82 if once != twice {
83 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("mask(mask(%#b,%#b), %#b) != mask(%#b,%#b) got %#b. want %#b",
84 p, b, b, p, b, twice, once)
85 }
86 }
87 }
88 }
89
90 func TestMatchPrefixscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta/internal/trie`/TestMatchPrefix().(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 for _, c := range []struct {
92 k prefixscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta/internal/trie`/prefix#
93 p prefixscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta/internal/trie`/prefix#
94 b bitposscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta/internal/trie`/bitpos#
95 }{
96 {
97 k: 0b1000,
98 p: 0b1011,
99 b: 0b0100,
100 }, {
101 k: 0b1001,
102 p: 0b1011,
103 b: 0b0100,
104 }, {
105 k: 0b1010,
106 p: 0b1011,
107 b: 0b0100,
108 }, {
109 k: 0b1011,
110 p: 0b1011,
111 b: 0b0100,
112 }, {
113 k: 0b1100,
114 p: 0b1011,
115 b: 0b0100,
116 }, {
117 k: 0b1101,
118 p: 0b1011,
119 b: 0b0100,
120 }, {
121 k: 0b1110,
122 p: 0b1011,
123 b: 0b0100,
124 }, {
125 k: 0b1111,
126 p: 0b1011,
127 b: 0b0100,
128 },
129 } {
130 if !matchPrefixscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta/internal/trie`/matchPrefix().(c.k, c.p, c.b) {
131 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("matchPrefix(%#b, %#b,%#b) should be true", c.k, c.p, c.b)
132 }
133 }
134 }
135
136 func TestNotMatchPrefixscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta/internal/trie`/TestNotMatchPrefix().(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#) {
137 for _, c := range []struct {
138 k prefixscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta/internal/trie`/prefix#
139 p prefixscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta/internal/trie`/prefix#
140 b bitposscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta/internal/trie`/bitpos#
141 }{
142 {
143 k: 0b0000,
144 p: 0b1011,
145 b: 0b0100,
146 }, {
147 k: 0b0010,
148 p: 0b1011,
149 b: 0b0100,
150 },
151 } {
152 if matchPrefixscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta/internal/trie`/matchPrefix().(c.k, c.p, c.b) {
153 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("matchPrefix(%#b, %#b,%#b) should be false", c.k, c.p, c.b)
154 }
155 }
156 }
157
158 func TestBranchingBitscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta/internal/trie`/TestBranchingBit().(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#) {
159 for _, c := range []struct {
160 x prefixscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta/internal/trie`/prefix#
161 y prefixscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta/internal/trie`/prefix#
162 want bitposscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta/internal/trie`/bitpos#
163 }{
164 {
165 x: 0b0000,
166 y: 0b1011,
167 want: 0b1000,
168 }, {
169 x: 0b1010,
170 y: 0b1011,
171 want: 0b0001,
172 }, {
173 x: 0b1011,
174 y: 0b1111,
175 want: 0b0100,
176 }, {
177 x: 0b1011,
178 y: 0b1001,
179 want: 0b0010,
180 },
181 } {
182 if got := branchingBitscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta/internal/trie`/branchingBit().(c.x, c.y); got != c.want {
183 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("branchingBit(%#b, %#b,) is not expected value. got %#b want %#b",
184 c.x, c.y, got, c.want)
185 }
186 }
187 }
188
189 func TestZeroBitscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta/internal/trie`/TestZeroBit().(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#) {
190 for _, c := range []struct {
191 k prefixscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta/internal/trie`/prefix#
192 b bitposscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta/internal/trie`/bitpos#
193 }{
194 {
195 k: 0b1000,
196 b: 0b0100,
197 }, {
198 k: 0b1001,
199 b: 0b0100,
200 }, {
201 k: 0b1010,
202 b: 0b0100,
203 },
204 } {
205 if !zeroBitscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta/internal/trie`/zeroBit().(c.k, c.b) {
206 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("zeroBit(%#b, %#b) should be true", c.k, c.b)
207 }
208 }
209 }
210 func TestZeroBitFailsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta/internal/trie`/TestZeroBitFails().(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#) {
211 for _, c := range []struct {
212 k prefixscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta/internal/trie`/prefix#
213 b bitposscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta/internal/trie`/bitpos#
214 }{
215 {
216 k: 0b1000,
217 b: 0b1000,
218 }, {
219 k: 0b1001,
220 b: 0b0001,
221 }, {
222 k: 0b1010,
223 b: 0b0010,
224 }, {
225 k: 0b1011,
226 b: 0b0001,
227 },
228 } {
229 if zeroBitscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta/internal/trie`/zeroBit().(c.k, c.b) {
230 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("zeroBit(%#b, %#b) should be false", c.k, c.b)
231 }
232 }
233 }
234
235 func TestOrdscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta/internal/trie`/TestOrd().(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#) {
236 a := bitposscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta/internal/trie`/bitpos#(0b0010)
237 b := bitposscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta/internal/trie`/bitpos#(0b1000)
238 if ordscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta/internal/trie`/ord().(a, b) {
239 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("ord(%#b, %#b) should be false", a, b)
240 }
241 if !ordscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta/internal/trie`/ord().(b, a) {
242 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("ord(%#b, %#b) should be true", b, a)
243 }
244 if ordscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta/internal/trie`/ord().(a, a) {
245 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("ord(%#b, %#b) should be false", a, a)
246 }
247 if !ordscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta/internal/trie`/ord().(a, 0) {
248 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("ord(%#b, %#b) should be true", a, 0)
249 }
250 }
251
252 func TestPrefixesOverlapLemmascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta/internal/trie`/TestPrefixesOverlapLemma().(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#) {
253 // test
254 // mask(p, fbb) == mask(q, fbb)
255 // iff
256 // m > n && matchPrefix(q, p, m) or (note: big endian encoding)
257 // m < n && matchPrefix(p, q, n) or (note: big endian encoding)
258 // m ==n && p == q
259
260 // Case 1: mask(p, fbb) == mask(q, fbb) => m > n && matchPrefix(q, p, m)
261 m, n := bitposscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta/internal/trie`/bitpos#(1<<2), bitposscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta/internal/trie`/bitpos#(1<<1)
262 p, q := maskscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta/internal/trie`/mask().(0b100, m), maskscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta/internal/trie`/mask().(0b010, n)
263 if !(prefixesOverlapscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta/internal/trie`/prefixesOverlap().(p, m, q, n) && m > n && matchPrefixscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta/internal/trie`/matchPrefix().(q, p, m)) {
264 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("prefixesOverlap(%#b, %#b, %#b, %#b) lemma does not hold",
265 p, m, q, n)
266 }
267 // Case 2: mask(p, fbb) == mask(q, fbb) => m < n && matchPrefix(p, q, n)
268 m, n = bitposscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta/internal/trie`/bitpos#(1<<2), bitposscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta/internal/trie`/bitpos#(1<<3)
269 p, q = maskscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta/internal/trie`/mask().(0b100, m), maskscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta/internal/trie`/mask().(0b1000, n)
270 if !(prefixesOverlapscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta/internal/trie`/prefixesOverlap().(p, m, q, n) && m < n && matchPrefixscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta/internal/trie`/matchPrefix().(p, q, n)) {
271 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("prefixesOverlap(%#b, %#b, %#b, %#b) lemma does not hold",
272 p, m, q, n)
273 }
274 // Case 3: mask(p, fbb) == mask(q, fbb) => m < n && matchPrefix(p, q, n)
275 m, n = bitposscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta/internal/trie`/bitpos#(1<<2), bitposscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta/internal/trie`/bitpos#(1<<2)
276 p, q = maskscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta/internal/trie`/mask().(0b100, m), maskscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta/internal/trie`/mask().(0b001, n)
277 if !(prefixesOverlapscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta/internal/trie`/prefixesOverlap().(p, m, q, n) && m == n && p == q) {
278 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("prefixesOverlap(%#b, %#b, %#b, %#b) lemma does not hold",
279 p, m, q, n)
280 }
281 // Case 4: mask(p, fbb) != mask(q, fbb)
282 m, n = bitposscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta/internal/trie`/bitpos#(1<<1), bitposscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta/internal/trie`/bitpos#(1<<1)
283 p, q = maskscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta/internal/trie`/mask().(0b100, m), maskscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta/internal/trie`/mask().(0b001, n)
284 if prefixesOverlapscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta/internal/trie`/prefixesOverlap().(p, m, q, n) ||
285 (m > n && matchPrefixscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta/internal/trie`/matchPrefix().(q, p, m)) ||
286 (m < n && matchPrefixscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta/internal/trie`/matchPrefix().(p, q, n)) ||
287 (m == n && p == q) {
288 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("prefixesOverlap(%#b, %#b, %#b, %#b) lemma does not hold",
289 p, m, q, n)
290 }
291
292 // Do a few more random cases
293 r := 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().(123))
294 N := 2000
295 for i := 0; i < N; i++ {
296 m := bitposscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta/internal/trie`/bitpos#(1 << (r.Uint64scip-go gomod github.com/golang/go/src go1.25.0 `math/rand`/Rand#Uint64().() % (64 + 1)))
297 n := bitposscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta/internal/trie`/bitpos#(1 << (r.Uint64scip-go gomod github.com/golang/go/src go1.25.0 `math/rand`/Rand#Uint64().() % (64 + 1)))
298
299 p := maskscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta/internal/trie`/mask().(prefixscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta/internal/trie`/prefix#(r.Uint64scip-go gomod github.com/golang/go/src go1.25.0 `math/rand`/Rand#Uint64().()), m)
300 q := maskscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta/internal/trie`/mask().(prefixscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta/internal/trie`/prefix#(r.Uint64scip-go gomod github.com/golang/go/src go1.25.0 `math/rand`/Rand#Uint64().()), n)
301
302 lhs := prefixesOverlapscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta/internal/trie`/prefixesOverlap().(p, m, q, n)
303 rhs := (m > n && matchPrefixscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta/internal/trie`/matchPrefix().(q, p, m)) ||
304 (m < n && matchPrefixscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/callgraph/vta/internal/trie`/matchPrefix().(p, q, n)) ||
305 (m == n && p == q)
306
307 if lhs != rhs {
308 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("prefixesOverlap(%#b, %#b, %#b, %#b) != <lemma> got %v. want %v",
309 p, m, q, n, lhs, rhs)
310 }
311 }
312 }