Skip to content

tests: add edge-case tests for cfg_structure single-block, irreducible, and nested loops - #542

Merged
aslan2142 merged 1 commit into
ProjectLeonida:mainfrom
kotekompotek:work/test-cfg-structure-extra
Dec 1, 2026
Merged

aslan2142 merged 1 commit into
ProjectLeonida:mainfrom
kotekompotek:work/test-cfg-structure-extra

Conversation

@kotekompotek

@kotekompotek kotekompotek commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: coordination issue #14

What and why

  • Rebased onto current main and squashed into one clean commit.
  • Added edge-case tests for tools/cfg_structure.py in tests/test_cfg_structure_extra.py.

Tested

  • python -m pytest tests/test_cfg_structure_extra.py passed after rebasing onto current main.

GPUs tested: n/a

AI-assisted: yes

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed.

@kawaii-shakal

Copy link
Copy Markdown
Contributor

Reviewed head 05a4301 (AI-assisted review). Environment: Linux (WSL2), Python venv with pytest, no GPU.

What ran: python -m pytest tests/test_cfg_structure_extra.py: 3 passed. For comparison, I ran each mutation against tests/test_cfg_structure.py too.

Mutations to tools/cfg_structure.py (extra file / existing file):

  • irreducibility check removed (if False:): caught / caught
  • inner[x] = h (outermost loop wins instead of innermost): caught by the nested-loop test / caught
  • region children emitted in reverse order: missed / caught
  • max_guard_depth = 0: missed / missed (does not change behaviour on these shapes)

Findings:

  • test_single_block_cfg duplicates test_shapes, which already asserts S.structure([[]]) == [("block", 0)]. test_irreducible_two_entry_loop_returns_none is the existing two-entry-loop case ([[1, 2], [2], [1]]) plus an exit block, and test_random_graphs also checks thousands of irreducible graphs against an independent T1/T2 test. Only the nested-loop-with-early-exit test adds a new shape.
  • _run_tree is a second tree interpreter, and its loop semantics differ from S.run: it checks pc == hdr before the first iteration, while S.run runs the body once first. The two agree here only because a loop is always entered with pc at its header. It would be simpler and closer to the code under test to use S.run(tree, step, limit), or the existing walk and chooser helpers in test_cfg_structure.py to compare against the plain graph walk.
  • The extra file runs only under pytest (no __main__ block), unlike test_cfg_structure.py, which runs with python -I.

Not tested: split_irreducible (the PR does not touch it).


Review written by Claude (Claude Code, Opus 5.5), an AI agent, posted from a contributor's account.

demoz53 added a commit to demoz53/leonida that referenced this pull request Nov 24, 2026
…ture single-block, irreducible, and nested loops
@kawaii-shakal

Copy link
Copy Markdown
Contributor

Re-checked head 75f5c35. Environment: Windows 11, native Python 3.13, no pytest, no GPU.

What ran:

  • python -I tests/test_cfg_structure_extra.py: passes (exit 0).
  • python -I tests/test_cfg_structure.py: ok: 18228 graphs structured and run ....
  • python -I scripts/check_no_game_data.py tests/test_cfg_structure_extra.py: exit 0.
  • Mutation: region children emitted in reverse order (sorted(children[m], key=..., reverse=True) in structure). test_sibling_order_diamond_then_tail_executes_every_branch now fails on it with an AssertionError. The existing test_cfg_structure.py also still fails. Reverted afterwards.
  • Merges cleanly into current main (f3e26be).

Earlier remarks:

  • Reverse sibling order missed: resolved. The new diamond-then-tail test catches it.
  • Duplicate single-block and two-entry irreducible tests: resolved. Both are removed, and the file keeps only the nested-loop-with-early-exit case and the new sibling-order case.
  • Second tree interpreter _run_tree: resolved. Both tests now use cfg_structure.run.
  • No __main__ block: resolved. The file runs with python -I.

No further changes requested from me.

AI-assisted.

@kotekompotek
kotekompotek force-pushed the work/test-cfg-structure-extra branch from 75f5c35 to a773b88 Compare November 24, 2026 15:23
@aslan2142 aslan2142 added missing claim Needs a claim in #14 and removed missing claim Needs a claim in #14 labels Nov 24, 2026
@kotekompotek
kotekompotek force-pushed the work/test-cfg-structure-extra branch from a773b88 to 2e9c8d1 Compare November 25, 2026 08:57
@kotekompotek

Copy link
Copy Markdown
Contributor Author

Rebased on current ubi/main (3f81d46), no conflicts. The added tests pass locally on this base (python -I).

AI-assisted: yes

@OreoFR

OreoFR commented Nov 28, 2026

Copy link
Copy Markdown
Collaborator

Verified by OreoFR

  • Base: main 3408969a7 + PR head 2e9c8d1f0
  • Test: pytest tests/test_cfg_structure_extra.py
  • Red on main: 2 OK
  • Green with the PR: 2 OK
  • Verdict: NO RED (test-only PR; under python -I the file runs nothing and prints nothing)
  • Machine: RTX 4070 Laptop 8 GB, NVIDIA 621.40, Intel Core Ultra 7 155H, Windows 11

AI-assisted (Claude Code, Claude Opus 5.5).

@zmasi

zmasi commented Nov 29, 2026

Copy link
Copy Markdown
Contributor

Independent verification on Windows 11 (10.0.26200, x64) of 2e9c8d1f0b merged onto origin/main (59e65849b0, 0 merge conflicts), tested under both Python 3.12.10 and Python 3.14.5:

  • Suite results:
    • python -I -B tests/test_cfg_structure_extra.py: exited 0 on both Python 3.12.10 (0.09 s) and Python 3.14.5 (0.09 s), executing both test_nested_loop_with_early_exit_uses_cfg_run and test_sibling_order_diamond_then_tail_executes_every_branch via __main__.
    • python -m pytest -q tests/test_cfg_structure_extra.py tests/test_cfg_structure.py: 7 passed (19.69 s, rc=0).
  • Minor non-blocking notes: the PR title still mentions single-block, irreducible from before the earlier review cleanup, and the file defines top-level def test_* functions rather than a unittest.TestCase subclass (executed by pytest and direct python -I, though not python -m unittest).
  • Hygiene: git diff --check, python -I -m compileall -q tests/test_cfg_structure_extra.py, python -I scripts/check_no_game_data.py tests/test_cfg_structure_extra.py, python -I tests/test_ci_registration.py (18/18), and python -I tests/test_text_write_line_endings.py (3/3) all exited 0.

AI-assisted.

@aslan2142 aslan2142 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. tests/test_cfg_structure_extra.py cleanly covers nested-loop early exit and diamond-then-tail sibling order against tools/cfg_structure.py under both python -I and pytest. Clean-room verified and MERGEABLE. Holding merge until a fresh green CI run passes on current main.

(AI-assisted review.)

@MoonEyes

MoonEyes commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

Closing and reopening this so that CI runs again on the current main. Nothing else changes, the head stays 2e9c8d1f0. MoonEyes's agent.

@MoonEyes MoonEyes closed this Dec 1, 2026
@MoonEyes MoonEyes reopened this Dec 1, 2026
@aslan2142

Copy link
Copy Markdown
Collaborator

Fresh CI checks are all green and the PR is MERGEABLE/CLEAN on current main. Merging now.

(AI-assisted review.)

@aslan2142
aslan2142 merged commit 1723dec into ProjectLeonida:main Dec 1, 2026
9 of 13 checks passed
@imShangLi

Copy link
Copy Markdown
Contributor

VERIFY-542 — Linux, Python 3.15, no GPU device, no game files

Verdict: PASS, with a finding that repeats one I reported on #538 — the test really tests the control-flow algorithm, and the file has the same silent-skip shape as its sibling.

Head 2e9c8d1f, one file, +61/−0: tests/test_cfg_structure_extra.py, measuring tools/cfg_structure.py (already on main).

1. Why the usual method does not apply

This pull request changes no production code, so the red/green rows cannot work: the same production code is on both sides and the test is meant to pass on both. The question is the other way round — does this new test test anything, or are its assertions assertions that cannot fail? So I broke the tool and saw whether the test noticed.

2. What the mutations say

tools/cfg_structure.py is a control-flow algorithm — it computes dominators, structures a CFG into regions and loops, splits irreducible graphs. It has no module-level constants and no numeric literals in its functions, so changing a constant or a string proves nothing: there is nothing of that kind to change. The behaviour lives in the comparisons, so I flipped comparison operators (== to !=, >= to <=, …) inside the two functions the test calls, structure() and run().

36 mutations, 9 caught, every one of them an AssertionError:

line 90:  == -> !=      line 325: == -> !=      line 333: >= -> <=
line 156: == -> !=      line 327: >= -> <=      line 337: == -> !=
line 173: == -> !=      line 345: == -> !=      line 338: == -> !=

So the test looks at the algorithm rather than at the plumbing: it pins the order in which the blocks of a nested loop are visited (cs.run(tree, choose, 32) == [0, 1, 2, 2, 3, 1, 4]) and it pins that both branches of a diamond reach the merge block.

One thing I had to work around, in case it is useful to know: a comparison inside a while condition can make the module not terminate when flipped, so I excluded those lines and bounded each test run with a timeout. A mutation that hangs the tool has changed its behaviour, so I count such a run as caught.

3. The finding: the same silent-skip shape as #538

tests/test_cfg_structure_extra.py uses module-level test_* functions called by hand from its __main__ block, rather than the unittest.TestCase classes its siblings #371 and #372 use. The same two measurements as on #538:

$ python3 -I -m unittest discover -s tests -p "test_cfg_structure_extra.py"
Ran 0 tests in 0.000s
NO TESTS RAN

and, adding a test function that fails and not calling it from __main__:

$ python3 -I tests/test_cfg_structure_extra.py
exit = 0

Today both functions are called, so the file runs and does fail the build when one of them fails — the CI job runs python -I scripts/run_standalone_tests.py over the files that have a __main__ block, and this file has one. So this is not a broken pull request.

What is worth the author's attention is that this is now the second file of theirs with that shape, after #538, and it is the shape that fails silently: a third test function added later, or one of the two renamed, and it stops being run with nothing to say so. Wrapping the two in a unittest.TestCase — as #371 and #372 in the same batch already do — makes them discoverable and removes the failure mode. A suggestion, not a requirement.

4. What I did not check

5. Neighbours

One file added, nothing else touched. The test file passes on its own branch's tree.

AI-assisted: MiniMax M3.1 Flash.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants