import sys
import os
from collections import Counter
script_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
os.chdir(script_dir)
fn = r'./modules/styles.py'
funcname = 'select_from_weighted_list'
ns = {'Dict': dict, 'random': __import__('random')}
tries = 10000
tolerance_pct = 2.0
tests = [
["", { '': 100 } ],
[ "red|blonde|black", { 'black': 33, 'red': 33, 'blonde': 33 } ],
[ "red||black", { 'black': 33, 'red': 33, '': 33 } ],
[ "red|blonde|blonde", { 'red': 33, 'blonde': 66 } ],
[ "red:0.1|blonde:0.9", { 'red': 10, 'blonde': 90 } ],
[ "red:1|blonde:2|black:5", { 'red': 12.5, 'blonde': 25, 'black': 62.5 } ],
[ "red:0|blonde|black:0", { 'blonde': 100 } ],
[ "red:0.5|blonde|black:0.3|brown", { 'red': 0.5, 'blonde': 1.0, 'black': 0.3, 'brown': 1.0 } ],
[ "red:2|blonde|black", { 'red': 50, 'blonde': 25, 'black': 25 } ],
[ "red:0.5|(blonde:1.3)", { 'red': 50, '(blonde:1.3)': 100 } ],
[ "red:0.5|(blonde:1.3):0.5", { 'red': 50, '(blonde:1.3)': 50 } ],
[ "red:0.5|[stuff:1.3]", { 'red': 50, '[stuff:1.3]': 100 } ],
[ "red:0.5|<lora:1.0>", { 'red': 50, '<lora:1.0>': 100 } ],
[ "red|<lora:test:1.0>|black", { 'black': 33, 'red': 33, '<lora:test:1.0>': 33 } ],
[ "red|blonde <lora:test:1.0>|black", { 'black': 33, 'red': 33, 'blonde <lora:test:1.0>': 33 } ],
[ "red|blonde, <lora:test:1.0>|black", { 'black': 33, 'red': 33, 'blonde, <lora:test:1.0>': 33 } ],
[ "red|blonde, <lora:test:1.0>|black:2", { 'black': 50, 'red': 25, 'blonde, <lora:test:1.0>': 25 } ],
]
with open(fn, 'r', encoding='utf-8') as f:
src = f.read()
start = src.find('def ' + funcname)
if start == -1:
print('Function not found')
sys.exit(1)
next_def = src.find('\ndef ', start+1)
next_class = src.find('\nclass ', start+1)
end_candidates = [i for i in (next_def, next_class) if i != -1]
end = min(end_candidates) if end_candidates else len(src)
func_src = src[start:end]
exec(func_src, ns)
func = ns.get(funcname)
if func is None:
print('Failed to extract function')
sys.exit(1)
print('Running' , tries, 'isolated quick tests for ' + funcname + ':\n')
for t in tests:
print('Input:', t[0])
samples = [func(t[0]) for _ in range(tries)]
c = Counter(samples)
print(" Expected:", dict(t[1]))
print(" Samples:", dict(c))
expected_pct = t[1]
expected_keys = set(expected_pct.keys())
actual_keys = set(c.keys())
missing = expected_keys - actual_keys
unexpected = actual_keys - expected_keys
if missing or unexpected:
if missing:
print(" Missing: ", sorted(missing))
if unexpected:
print(" Unexpected: ", sorted(unexpected))
print(" Result: FAIL keys")
continue
failures = []
W = sum(expected_pct.values())
deviations = {}
for k, pct in expected_pct.items():
pct = pct / W
expected_count = tries * pct
actual_count = c.get(k, 0)
actual_pct = actual_count / tries
deviation_pct = abs(actual_pct - pct)
deviations[k] = deviation_pct
if deviation_pct > tolerance_pct / 100.0:
failures.append(
f"{k}: expected {pct:.1f}%, got {actual_pct:.1f}% "
f"({actual_count}/{tries})"
)
print(" Deviations:", {k: f"{v*100:.2f}%" for k, v in deviations.items()})
if failures:
for line in failures:
print(" " + line)
print(" Result: FAIL distribution")
else:
print(" Result: PASS")