Skip to content

Commit 613831b

Browse files
committed
Python: add test for post-update loop flow
1 parent c26c68c commit 613831b

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# All functions starting with "test_" should run and execute `print("OK")` exactly once.
2+
# This can be checked by running validTest.py.
3+
4+
import sys
5+
import os
6+
7+
sys.path.append(os.path.dirname(os.path.dirname((__file__))))
8+
from testlib import expects
9+
10+
# These are defined so that we can evaluate the test code.
11+
NONSOURCE = "not a source"
12+
SOURCE = "source"
13+
14+
15+
def is_source(x):
16+
return x == "source" or x == b"source" or x == 42 or x == 42.0 or x == 42j
17+
18+
19+
def SINK(x):
20+
if is_source(x):
21+
print("OK")
22+
else:
23+
print("Unexpected flow", x)
24+
25+
26+
def SINK_F(x):
27+
if is_source(x):
28+
print("Unexpected flow", x)
29+
else:
30+
print("OK")
31+
32+
# ------------------------------------------------------------------------------
33+
# Actual tests
34+
# ------------------------------------------------------------------------------
35+
36+
def test_while():
37+
x = NONSOURCE
38+
n = 2
39+
while n > 0:
40+
if n == 1:
41+
SINK(x) #$ flow="SOURCE, l:+1 -> x"
42+
x = SOURCE
43+
n -= 1
44+
45+
class MyObj(object):
46+
def __init__(self, foo):
47+
self.foo = foo
48+
49+
def setFoo(obj, x):
50+
obj.foo = x
51+
52+
def test_while_obj():
53+
myobj = MyObj(NONSOURCE)
54+
n = 2
55+
while n > 0:
56+
if n == 1:
57+
SINK(myobj.foo) #$ flow="SOURCE, l:+1 -> myobj.foo"
58+
setFoo(myobj, SOURCE)
59+
n -= 1
60+
61+
def setAndTestFoo(obj, x, test):
62+
if test:
63+
# This flow is not found, if self-loops are broken at the SSA level.
64+
SINK(obj.foo) #$ flow="SOURCE, l:+7 -> obj.foo"
65+
obj.foo = x
66+
67+
def test_while_obj_sink():
68+
myobj = MyObj(NONSOURCE)
69+
n = 2
70+
while n > 0:
71+
setAndTestFoo(myobj, SOURCE, n == 1)
72+
n -= 1
73+

python/ql/test/experimental/dataflow/validTest.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ def check_tests_valid_after_version(testFile, version):
6565
check_tests_valid("coverage.argumentPassing")
6666
check_tests_valid("coverage.datamodel")
6767
check_tests_valid("coverage.test_builtins")
68+
check_tests_valid("coverage.loops")
6869
check_tests_valid("coverage-py2.classes")
6970
check_tests_valid("coverage-py3.classes")
7071
check_tests_valid("variable-capture.in")

0 commit comments

Comments
 (0)