diff options
| author | ItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com> | 2026-11-21 17:38:33 -0500 |
|---|---|---|
| committer | ItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com> | 2026-11-21 17:38:33 -0500 |
| commit | a4e435fac2e630f1e9ba8df0f8f29e46d603b456 (patch) | |
| tree | cd4c6ca346e758bf1740f60a608ada90fc0c4a4f /tests/test_cfg_structure.py | |
| parent | 8183bb5610d7e5e704bed067b3d812d8a79612f6 (diff) | |
| parent | 338ff3354bc2c4c5922213404e7b4fcf11330dcc (diff) | |
Merge pull request #276 from eIBart0
Diffstat (limited to 'tests/test_cfg_structure.py')
| -rw-r--r-- | tests/test_cfg_structure.py | 60 |
1 files changed, 57 insertions, 3 deletions
diff --git a/tests/test_cfg_structure.py b/tests/test_cfg_structure.py index 6aa50a77c..1e4d748be 100644 --- a/tests/test_cfg_structure.py +++ b/tests/test_cfg_structure.py | |||
| @@ -90,13 +90,38 @@ def blocks_in(tree): | |||
| 90 | 90 | ||
| 91 | def test_random_graphs(): | 91 | def test_random_graphs(): |
| 92 | rng = random.Random(20261121) | 92 | rng = random.Random(20261121) |
| 93 | structured = refused = loops = 0 | 93 | structured = refused = loops = split_ok = split_cap = 0 |
| 94 | for case in range(20000): | 94 | for case in range(20000): |
| 95 | succ = random_graph(rng) | 95 | succ = random_graph(rng) |
| 96 | tree = S.structure(succ) | 96 | tree = S.structure(succ) |
| 97 | assert (tree is not None) == reducible(succ), f"reducibility disagrees for {succ}" | 97 | assert (tree is not None) == reducible(succ), f"reducibility disagrees for {succ}" |
| 98 | if tree is None: | 98 | if tree is None: |
| 99 | refused += 1 | 99 | refused += 1 |
| 100 | res = S.split_irreducible(succ) | ||
| 101 | if res is not None: | ||
| 102 | new_succ, orig = res | ||
| 103 | assert len(new_succ) <= min(2 * len(succ), len(succ) + 64) | ||
| 104 | stree = S.structure(new_succ) | ||
| 105 | assert stree is not None, f"split_irreducible returned irreducible graph for {succ}" | ||
| 106 | splaced = blocks_in(stree) | ||
| 107 | assert len(splaced) == len(set(splaced)), f"a split block is placed twice: {succ}" | ||
| 108 | for seed in range(6): | ||
| 109 | want = walk(succ, chooser(succ, seed), 200) | ||
| 110 | visits = {} | ||
| 111 | |||
| 112 | def split_choose(b, _orig=orig, _ns=new_succ, _seed=seed, _v=visits): | ||
| 113 | ob = _orig[b] | ||
| 114 | k = _v[ob] = _v.get(ob, 0) + 1 | ||
| 115 | opts = _ns[b] | ||
| 116 | if not opts: | ||
| 117 | return S.END | ||
| 118 | return opts[random.Random(f"{_seed}/{ob}/{k}").randrange(len(opts))] | ||
| 119 | |||
| 120 | got = [orig[b] for b in S.run(stree, split_choose, 200)] | ||
| 121 | assert got == want, f"split graph {succ} seed {seed}: tree ran {got}, graph runs {want}" | ||
| 122 | split_ok += 1 | ||
| 123 | else: | ||
| 124 | split_cap += 1 | ||
| 100 | continue | 125 | continue |
| 101 | structured += 1 | 126 | structured += 1 |
| 102 | loops += '"loop"' in repr(tree).replace("'", '"') | 127 | loops += '"loop"' in repr(tree).replace("'", '"') |
| @@ -107,7 +132,8 @@ def test_random_graphs(): | |||
| 107 | got = S.run(tree, chooser(succ, seed), 200) | 132 | got = S.run(tree, chooser(succ, seed), 200) |
| 108 | assert got == want, f"graph {succ} seed {seed}: tree ran {got}, graph runs {want}" | 133 | assert got == want, f"graph {succ} seed {seed}: tree ran {got}, graph runs {want}" |
| 109 | assert structured > 5000 and refused > 1000 and loops > 2000, (structured, refused, loops) | 134 | assert structured > 5000 and refused > 1000 and loops > 2000, (structured, refused, loops) |
| 110 | return structured, refused, loops | 135 | assert split_ok > 1000 and split_ok + split_cap == refused, (split_ok, split_cap, refused) |
| 136 | return structured, refused, loops, split_ok, split_cap | ||
| 111 | 137 | ||
| 112 | 138 | ||
| 113 | def test_shapes(): | 139 | def test_shapes(): |
| @@ -118,8 +144,36 @@ def test_shapes(): | |||
| 118 | ("guard", 1, [("block", 1)]), ("guard", 3, [("block", 3)])] | 144 | ("guard", 1, [("block", 1)]), ("guard", 3, [("block", 3)])] |
| 119 | assert S.structure([[0, 1], []]) == [("loop", 0, [("block", 0)]), ("guard", 1, [("block", 1)])] | 145 | assert S.structure([[0, 1], []]) == [("loop", 0, [("block", 0)]), ("guard", 1, [("block", 1)])] |
| 120 | assert S.structure([[1, 2], [2], [1]]) is None # two-entry loop | 146 | assert S.structure([[1, 2], [2], [1]]) is None # two-entry loop |
| 147 | ns, orig = S.split_irreducible([[1, 2], [2], [1]]) | ||
| 148 | assert S.structure(ns) is not None and len(ns) == 4 and orig == [0, 1, 2, 2] | ||
| 149 | # Nested irreducible cycle {2, 3} inside an outer reducible loop headed by 1. | ||
| 150 | nested = [[1], [2, 3], [3, 4], [2, 4], [1, 5], []] | ||
| 151 | assert S.structure(nested) is None | ||
| 152 | ns2, orig2 = S.split_irreducible(nested) | ||
| 153 | assert S.structure(ns2) is not None and len(ns2) == 7 | ||
| 154 | # Regression for single-node infinite unrolling: SCC {1, 2, 3} with entries 1 (h) and 2 (e), plus an | ||
| 155 | # internal subcycle 2 <-> 3 that does not pass through 1. Single-node cloning bounces 2' -> 3 -> 3' -> 2 -> ... | ||
| 156 | # until hitting the cap; closed-region cloning duplicates R = {2, 3} once (+2 blocks, 5 -> 7). | ||
| 157 | subcycle = [[1, 2], [3, 4], [3], [1, 2], []] | ||
| 158 | assert S.structure(subcycle) is None | ||
| 159 | ns3, orig3 = S.split_irreducible(subcycle) | ||
| 160 | assert S.structure(ns3) is not None and len(ns3) == 7 | ||
| 161 | for seed in range(6): | ||
| 162 | want = walk(subcycle, chooser(subcycle, seed), 200) | ||
| 163 | visits = {} | ||
| 164 | |||
| 165 | def sc_choose(b, _o=orig3, _ns=ns3, _s=seed, _v=visits): | ||
| 166 | ob = _o[b] | ||
| 167 | k = _v[ob] = _v.get(ob, 0) + 1 | ||
| 168 | opts = _ns[b] | ||
| 169 | return S.END if not opts else opts[random.Random(f"{_s}/{ob}/{k}").randrange(len(opts))] | ||
| 170 | |||
| 171 | assert [orig3[b] for b in S.run(S.structure(ns3), sc_choose, 200)] == want | ||
| 172 | # Cap enforcement: max_splits=0 must refuse an irreducible graph cleanly. | ||
| 173 | assert S.split_irreducible([[1, 2], [2], [1]], max_splits=0) is None | ||
| 121 | 174 | ||
| 122 | 175 | ||
| 123 | if __name__ == "__main__": | 176 | if __name__ == "__main__": |
| 124 | test_shapes() | 177 | test_shapes() |
| 125 | print("ok: %d graphs structured and run, %d irreducible refused, %d with loops" % test_random_graphs()) | 178 | print("ok: %d graphs structured and run, %d irreducible (%d split-structured, %d capped), %d with loops" % |
| 179 | (lambda s, r, l, ok, cap: (s, r, ok, cap, l))(*test_random_graphs())) | ||