Debian Codegraph @

internal/refactor/inline/falcon_test.go

raw symbols SCIP
1 // Copyright 2023 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 inline_test
6
7 import "testing"
8
9 // Testcases mostly come in pairs, of a success and a failure
10 // to substitute based on specific constant argument values.
11
12 func TestFalconStringIndex(t *testing.T) {
13 runTests(t, []testcase{
14 {
15 "Non-negative string index.",
16 `func f(i int) byte { return s[i] }; var s string`,
17 `func _() { f(0) }`,
18 `func _() { _ = s[0] }`,
19 },
20 {
21 "Negative string index.",
22 `func f(i int) byte { return s[i] }; var s string`,
23 `func _() { f(-1) }`,
24 `func _() {
25 var i int = -1
26 _ = s[i]
27 }`,
28 },
29 {
30 "String index in range.",
31 `func f(s string, i int) byte { return s[i] }`,
32 `func _() { f("-", 0) }`,
33 `func _() { _ = "-"[0] }`,
34 },
35 {
36 "String index out of range.",
37 `func f(s string, i int) byte { return s[i] }`,
38 `func _() { f("-", 1) }`,
39 `func _() {
40 var (
41 s string = "-"
42 i int = 1
43 )
44 _ = s[i]
45 }`,
46 },
47 {
48 "Remove known prefix (OK)",
49 `func f(s, prefix string) string { return s[:len(prefix)] }`,
50 `func _() { f("", "") }`,
51 `func _() { _ = ""[:len("")] }`,
52 },
53 {
54 "Remove not-a-prefix (out of range)",
55 `func f(s, prefix string) string { return s[:len(prefix)] }`,
56 `func _() { f("", "pre") }`,
57 `func _() {
58 var s, prefix string = "", "pre"
59 _ = s[:len(prefix)]
60 }`,
61 },
62 })
63 }
64
65 func TestFalconSliceIndices(t *testing.T) {
66 runTests(t, []testcase{
67 {
68 "Monotonic (0<=i<=j) slice indices (len unknown).",
69 `func f(i, j int) []int { return s[i:j] }; var s []int`,
70 `func _() { f(0, 1) }`,
71 `func _() { _ = s[0:1] }`,
72 },
73 {
74 "Non-monotonic slice indices (len unknown).",
75 `func f(i, j int) []int { return s[i:j] }; var s []int`,
76 `func _() { f(1, 0) }`,
77 `func _() {
78 var i, j int = 1, 0
79 _ = s[i:j]
80 }`,
81 },
82 {
83 "Negative slice index.",
84 `func f(i, j int) []int { return s[i:j] }; var s []int`,
85 `func _() { f(-1, 1) }`,
86 `func _() {
87 var i, j int = -1, 1
88 _ = s[i:j]
89 }`,
90 },
91 })
92 }
93
94 func TestFalconMapKeys(t *testing.T) {
95 runTests(t, []testcase{
96 {
97 "Unique map keys (int)",
98 `func f(x int) { _ = map[int]bool{1: true, x: true} }`,
99 `func _() { f(2) }`,
100 `func _() { _ = map[int]bool{1: true, 2: true} }`,
101 },
102 {
103 "Duplicate map keys (int)",
104 `func f(x int) { _ = map[int]bool{1: true, x: true} }`,
105 `func _() { f(1) }`,
106 `func _() {
107 var x int = 1
108 _ = map[int]bool{1: true, x: true}
109 }`,
110 },
111 {
112 "Generic map keys",
113 // We have to use a named map type here, because the substituter tries to
114 // wrap the type expr in parens, which are not allowed around the type in
115 // composite literal expressions.
116 `type Map map[int]bool; func f[M Map](x int) { _ = M{1: true, x: true} }`,
117 `func _() { f[Map](1) }`,
118 `func _() {
119 var x int = 1
120 _ = Map{1: true, x: true}
121 }`,
122 },
123 {
124 "Array keys", // not a map; shouldn't crash (golang/go#74393)
125 `func f(x int) { _ = [2]int{1: 0} }`,
126 `func _() { f(0) }`,
127 `func _() { _ = [2]int{1: 0} }`,
128 },
129 {
130 "Slice keys", // not a map; shouldn't crash (golang/go#74393)
131 `func f(x int) { _ = []int{1: 0} }`,
132 `func _() { f(0) }`,
133 `func _() { _ = []int{1: 0} }`,
134 },
135 {
136 "Unique map keys (varied built-in types)",
137 `func f(x int16) { _ = map[any]bool{1: true, x: true} }`,
138 `func _() { f(2) }`,
139 `func _() { _ = map[any]bool{1: true, int16(2): true} }`,
140 },
141 {
142 "Duplicate map keys (varied built-in types)",
143 `func f(x int16) { _ = map[any]bool{1: true, x: true} }`,
144 `func _() { f(1) }`,
145 `func _() { _ = map[any]bool{1: true, int16(1): true} }`,
146 },
147 {
148 "Unique map keys (varied user-defined types)",
149 `func f(x myint) { _ = map[any]bool{1: true, x: true} }; type myint int`,
150 `func _() { f(2) }`,
151 `func _() { _ = map[any]bool{1: true, myint(2): true} }`,
152 },
153 {
154 "Duplicate map keys (varied user-defined types)",
155 `func f(x myint, y myint2) { _ = map[any]bool{x: true, y: true} }; type (myint int; myint2 int)`,
156 `func _() { f(1, 1) }`,
157 `func _() {
158 var (
159 x myint = 1
160 y myint2 = 1
161 )
162 _ = map[any]bool{x: true, y: true}
163 }`,
164 },
165 {
166 "Duplicate map keys (user-defined alias to built-in)",
167 `func f(x myint, y int) { _ = map[any]bool{x: true, y: true} }; type myint = int`,
168 `func _() { f(1, 1) }`,
169 `func _() {
170 var (
171 x myint = 1
172 y int = 1
173 )
174 _ = map[any]bool{x: true, y: true}
175 }`,
176 },
177 })
178 }
179
180 func TestFalconSwitchCases(t *testing.T) {
181 runTests(t, []testcase{
182 {
183 "Unique switch cases (int).",
184 `func f(x int) { switch 0 { case x: case 1: } }`,
185 `func _() { f(2) }`,
186 `func _() {
187 switch 0 {
188 case 2:
189 case 1:
190 }
191 }`,
192 },
193 {
194 "Duplicate switch cases (int).",
195 `func f(x int) { switch 0 { case x: case 1: } }`,
196 `func _() { f(1) }`,
197 `func _() {
198 var x int = 1
199 switch 0 {
200 case x:
201 case 1:
202 }
203 }`,
204 },
205 {
206 "Unique switch cases (varied built-in types).",
207 `func f(x int) { switch any(nil) { case x: case int16(1): } }`,
208 `func _() { f(2) }`,
209 `func _() {
210 switch any(nil) {
211 case 2:
212 case int16(1):
213 }
214 }`,
215 },
216 {
217 "Duplicate switch cases (varied built-in types).",
218 `func f(x int) { switch any(nil) { case x: case int16(1): } }`,
219 `func _() { f(1) }`,
220 `func _() {
221 switch any(nil) {
222 case 1:
223 case int16(1):
224 }
225 }`,
226 },
227 })
228 }
229
230 func TestFalconDivision(t *testing.T) {
231 runTests(t, []testcase{
232 {
233 "Division by two.",
234 `func f(x, y int) int { return x / y }`,
235 `func _() { f(1, 2) }`,
236 `func _() { _ = 1 / 2 }`,
237 },
238 {
239 "Division by zero.",
240 `func f(x, y int) int { return x / y }`,
241 `func _() { f(1, 0) }`,
242 `func _() {
243 var x, y int = 1, 0
244 _ = x / y
245 }`,
246 },
247 {
248 "Division by two (statement).",
249 `func f(x, y int) { x /= y }`,
250 `func _() { f(1, 2) }`,
251 `func _() {
252 var x int = 1
253 x /= 2
254 }`,
255 },
256 {
257 "Division by zero (statement).",
258 `func f(x, y int) { x /= y }`,
259 `func _() { f(1, 0) }`,
260 `func _() {
261 var x, y int = 1, 0
262 x /= y
263 }`,
264 },
265 {
266 "Division of minint by two (ok).",
267 `func f(x, y int32) { _ = x / y }`,
268 `func _() { f(-0x80000000, 2) }`,
269 `func _() { _ = int32(-0x80000000) / int32(2) }`,
270 },
271 {
272 "Division of minint by -1 (overflow).",
273 `func f(x, y int32) { _ = x / y }`,
274 `func _() { f(-0x80000000, -1) }`,
275 `func _() {
276 var x, y int32 = -0x80000000, -1
277 _ = x / y
278 }`,
279 },
280 })
281 }
282
283 func TestFalconMinusMinInt(t *testing.T) {
284 runTests(t, []testcase{
285 {
286 "Negation of maxint.",
287 `func f(x int32) int32 { return -x }`,
288 `func _() { f(0x7fffffff) }`,
289 `func _() { _ = -int32(0x7fffffff) }`,
290 },
291 {
292 "Negation of minint.",
293 `func f(x int32) int32 { return -x }`,
294 `func _() { f(-0x80000000) }`,
295 `func _() {
296 var x int32 = -0x80000000
297 _ = -x
298 }`,
299 },
300 })
301 }
302
303 func TestFalconArithmeticOverflow(t *testing.T) {
304 runTests(t, []testcase{
305 {
306 "Addition without overflow.",
307 `func f(x, y int32) int32 { return x + y }`,
308 `func _() { f(100, 200) }`,
309 `func _() { _ = int32(100) + int32(200) }`,
310 },
311 {
312 "Addition with overflow.",
313 `func f(x, y int32) int32 { return x + y }`,
314 `func _() { f(1<<30, 1<<30) }`,
315 `func _() {
316 var x, y int32 = 1 << 30, 1 << 30
317 _ = x + y
318 }`,
319 },
320 {
321 "Conversion in range.",
322 `func f(x int) int8 { return int8(x) }`,
323 `func _() { f(123) }`,
324 `func _() { _ = int8(123) }`,
325 },
326 {
327 "Conversion out of range.",
328 `func f(x int) int8 { return int8(x) }`,
329 `func _() { f(456) }`,
330 `func _() {
331 var x int = 456
332 _ = int8(x)
333 }`,
334 },
335 })
336 }
337
338 func TestFalconComplex(t *testing.T) {
339 runTests(t, []testcase{
340 {
341 "Complex arithmetic (good).",
342 `func f(re, im float64, z complex128) byte { return "x"[int(real(complex(re, im)*complex(re, -im)-z))] }`,
343 `func _() { f(1, 2, 5+0i) }`,
344 // The float64 conversions are excessively conservative here
345 // but in general may affect the type of complex produced.
346 `func _() { _ = "x"[int(real(complex(float64(1), float64(2))*complex(float64(1), -2)-(5+0i)))] }`,
347 },
348 {
349 "Complex arithmetic (bad).",
350 `func f(re, im float64, z complex128) byte { return "x"[int(real(complex(re, im)*complex(re, -im)-z))] }`,
351 `func _() { f(1, 3, 5+0i) }`,
352 `func _() {
353 var (
354 re, im float64 = 1, 3
355 z complex128 = 5 + 0i
356 )
357 _ = "x"[int(real(complex(re, im)*complex(re, -im)-z))]
358 }`,
359 },
360 })
361 }
362 func TestFalconMisc(t *testing.T) {
363 runTests(t, []testcase{
364 {
365 "Compound constant expression (good).",
366 `func f(x, y string, i, j int) byte { return x[i*len(y)+j] }`,
367 `func _() { f("abc", "xy", 2, -3) }`,
368 `func _() { _ = "abc"[2*len("xy")+-3] }`,
369 },
370 {
371 "Compound constant expression (index out of range).",
372 `func f(x, y string, i, j int) byte { return x[i*len(y)+j] }`,
373 `func _() { f("abc", "xy", 4, -3) }`,
374 `func _() {
375 var (
376 x, y string = "abc", "xy"
377 i, j int = 4, -3
378 )
379 _ = x[i*len(y)+j]
380 }`,
381 },
382 {
383 "Constraints within nested functions (good).",
384 `func f(x int) { _ = func() { _ = [1]int{}[x] } }`,
385 `func _() { f(0) }`,
386 `func _() { _ = func() { _ = [1]int{}[0] } }`,
387 },
388 {
389 "Constraints within nested functions (bad).",
390 `func f(x int) { _ = func() { _ = [1]int{}[x] } }`,
391 `func _() { f(1) }`,
392 `func _() {
393 var x int = 1
394 _ = func() { _ = [1]int{}[x] }
395 }`,
396 },
397 {
398 "Falcon violation rejects only the constant arguments (x, z).",
399 `func f(x, y, z string) string { return x[:2] + y + z[:2] }; var b string`,
400 `func _() { f("a", b, "c") }`,
401 `func _() {
402 var x, z string = "a", "c"
403 _ = x[:2] + b + z[:2]
404 }`,
405 },
406 })
407 }