|
| 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 | + |
0 commit comments