diff options
| author | mrb-eel <323506043+mrb-eel@users.noreply.github.com> | 2026-11-21 23:30:58 +0100 |
|---|---|---|
| committer | mrb-eel <323506043+mrb-eel@users.noreply.github.com> | 2026-11-21 23:30:58 +0100 |
| commit | a638c5acb9e343def7df4f48bfc6e04e38367352 (patch) | |
| tree | f80e702ed517910a58bcd6198364eb986c297d4f /tests/test_cfg_structure.py | |
| parent | 36ace7135e897c228eede5d1d8c28e972285cb14 (diff) | |
Cache seeded choices in the CFG structure test
Diffstat (limited to 'tests/test_cfg_structure.py')
| -rw-r--r-- | tests/test_cfg_structure.py | 13 |
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 @@ | |||
| 5 | Random graphs, random branch outcomes. For each graph the tree is interpreted and compared with a plain walk of | 5 | Random graphs, random branch outcomes. For each graph the tree is interpreted and compared with a plain walk of |
| 6 | the graph that makes the same choices. Irreducible graphs must be refused (None), reducible ones must not be. | 6 | the graph that makes the same choices. Irreducible graphs must be refused (None), reducible ones must not be. |
| 7 | """ | 7 | """ |
| 8 | from functools import cache | ||
| 8 | import os | 9 | import os |
| 9 | import random | 10 | import random |
| 10 | import sys | 11 | import sys |
| @@ -13,15 +14,23 @@ sys.path.insert(0, os.path.join(os.path.dirname(os.path.dirname(os.path.abspath( | |||
| 13 | import cfg_structure as S # noqa: E402 | 14 | import cfg_structure as S # noqa: E402 |
| 14 | 15 | ||
| 15 | 16 | ||
| 17 | @cache | ||
| 18 | def 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 | |||
| 16 | def chooser(succ, seed): | 23 | def 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 | ||