|
| 1 | +"""Comparisons and evaluation order. |
| 2 | +
|
| 3 | +Comparison operands are evaluated left-to-right, followed by the comparison |
| 4 | +node itself. Annotations record the *run-time* evaluation order, so this file |
| 5 | +self-validates under CPython (``python3 test_comparison.py``). |
| 6 | +
|
| 7 | +Python short-circuits *chained* comparisons (in ``a < b < c`` the operand ``c`` |
| 8 | +is skipped once ``a < b`` is false), whereas the control-flow graph |
| 9 | +conservatively evaluates every operand. That divergence cannot be expressed in |
| 10 | +a single annotation, so the affected CFG queries report it (captured in the |
| 11 | +ConsecutiveTimestamps and NewCfgConsecutivePredecessorTimestamps .expected |
| 12 | +files). |
| 13 | +""" |
| 14 | + |
| 15 | +from timer import test, dead |
| 16 | + |
| 17 | + |
| 18 | +@test |
| 19 | +def test_two(t): |
| 20 | + # Both operands, then the comparison. |
| 21 | + (1 @ t[0] < 0 @ t[1]) @ t[2] |
| 22 | + |
| 23 | + |
| 24 | +@test |
| 25 | +def test_three(t): |
| 26 | + # Chained comparison, all comparisons hold: operands left-to-right, then |
| 27 | + # the comparison. Run time and CFG agree. |
| 28 | + (1 @ t[0] < 2 @ t[1] < 3 @ t[2]) @ t[3] |
| 29 | + |
| 30 | + |
| 31 | +@test |
| 32 | +def test_three_short_circuit(t): |
| 33 | + # ``1 > 2`` is false, so at run time Python short-circuits and never |
| 34 | + # evaluates ``3``; the comparison is reached at timestamp 2. The CFG still |
| 35 | + # evaluates ``3`` (dead at run time), which is why the CFG queries report a |
| 36 | + # gap around this comparison. |
| 37 | + (1 @ t[0] > 2 @ t[1] < 3 @ t[dead(2)]) @ t[2] |
0 commit comments