upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/tests/test_cfg_structure.py
diff options
context:
space:
mode:
authormrb-eel <323506043+mrb-eel@users.noreply.github.com>2026-11-21 23:30:58 +0100
committermrb-eel <323506043+mrb-eel@users.noreply.github.com>2026-11-21 23:30:58 +0100
commita638c5acb9e343def7df4f48bfc6e04e38367352 (patch)
treef80e702ed517910a58bcd6198364eb986c297d4f /tests/test_cfg_structure.py
parent36ace7135e897c228eede5d1d8c28e972285cb14 (diff)
Cache seeded choices in the CFG structure test
Diffstat (limited to 'tests/test_cfg_structure.py')
-rw-r--r--tests/test_cfg_structure.py13
1 files changed, 11 insertions, 2 deletions
diff --git a/tests/test_cfg_structure.py b/tests/test_cfg_structure.py
index 6185d6a81..6aa50a77c 100644
--- a/tests/test_cfg_structure.py
+++ b/tests/test_cfg_structure.py
@@ -5,6 +5,7 @@
5Random graphs, random branch outcomes. For each graph the tree is interpreted and compared with a plain walk of 5Random graphs, random branch outcomes. For each graph the tree is interpreted and compared with a plain walk of
6the graph that makes the same choices. Irreducible graphs must be refused (None), reducible ones must not be. 6the graph that makes the same choices. Irreducible graphs must be refused (None), reducible ones must not be.
7""" 7"""
8from functools import cache
8import os 9import os
9import random 10import random
10import sys 11import sys
@@ -13,15 +14,23 @@ sys.path.insert(0, os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(
13import cfg_structure as S # noqa: E402 14import cfg_structure as S # noqa: E402
14 15
15 16
17@cache
18def branch_index(seed, block, visit, count):
19 # The choice depends only on these numbers, so different graphs can reuse it.
20 return random.Random(f"{seed}/{block}/{visit}").randrange(count)
21
22
16def chooser(succ, seed): 23def chooser(succ, seed):
17 """Branch outcomes that depend only on the block and how often it has run, so two runs that execute the 24 """Branch outcomes that depend only on the block and how often it has run, so two runs that execute the
18 same blocks make the same choices.""" 25 same blocks make the same choices."""
19 visits = {} 26 visits = {}
20 27
21 def choose(b): 28 def choose(b):
29 options = succ[b]
30 if len(options) < 2:
31 return options[0] if options else S.END
22 k = visits[b] = visits.get(b, 0) + 1 32 k = visits[b] = visits.get(b, 0) + 1
23 options = list(succ[b]) or [S.END] 33 return options[branch_index(seed, b, k, len(options))]
24 return options[random.Random(f"{seed}/{b}/{k}").randrange(len(options))]
25 return choose 34 return choose
26 35
27 36