property-based-testing/examples/probe_property_fixture.py
Version 82fe82262526.bb1 · CC-BY-SA-4.0. This preview displays packaged text and does not execute code. Treat the contents as untrusted instructions.
← Return to resource and package checksum
"""BB Skills synthetic property checks; original fixture, CC BY-SA 4.0.
License: https://creativecommons.org/licenses/by-sa/4.0/; packaged full legal text.
No upstream evaluation harness, AI client, project dependency edit or live data.
"""
from pathlib import Path
import base64,binascii,json,unicodedata,sys,platform,hashlib,datetime as dt
from collections import Counter
import hypothesis
from hypothesis import given,settings,example,find,strategies as st
root=Path(__file__).parent
cfg=settings(max_examples=200,database=None,derandomize=True,deadline=None)
search=settings(max_examples=600,database=None,derandomize=True,deadline=None)
counts={};unique={};cases=[]
def seen(name,value):
counts[name]=counts.get(name,0)+1;unique.setdefault(name,set()).add(repr(value))
def passed(name,**data):cases.append({'name':name,'status':'passed',**data})
bytes_domain=st.binary(max_size=512)
scalar_text=st.text(alphabet=st.characters(blacklist_categories=('Cs',)),max_size=32)
objects=st.dictionaries(scalar_text,st.one_of(st.integers(-100000,100000),st.booleans(),scalar_text),max_size=16)
integers=st.lists(st.integers(-1000,1000),max_size=64)
@cfg
@given(bytes_domain)
@example(b'')
@example(b'\x00\xff\x00')
def check_bytes(data):
seen('base64_roundtrip',data);assert base64.b64decode(base64.b64encode(data),validate=True)==data
@cfg
@given(objects)
@example({})
@example({'combining':'e\u0301','zero':0})
def check_json(data):
seen('json_utf8_roundtrip',data);assert json.loads(json.dumps(data,ensure_ascii=False).encode('utf-8'))==data
@cfg
@given(integers)
@example([])
@example([0,0,-1])
def check_sort(data):
seen('sort_order_and_multiplicity',data);result=sorted(data)
assert Counter(result)==Counter(data) and len(result)==len(data)
assert all(a<=b for a,b in zip(result,result[1:]))
@cfg
@given(scalar_text)
@example('')
@example('e\u0301')
def check_nfc(data):
seen('NFC_idempotence_and_canonical_preservation',data);result=unicodedata.normalize('NFC',data)
assert unicodedata.normalize('NFC',result)==result
assert unicodedata.normalize('NFD',result)==unicodedata.normalize('NFD',data)
for fn in [check_bytes,check_json,check_sort,check_nfc]:
fn();name=list(counts)[-1];assert counts[name]>=200;passed(name,examples_executed=counts[name],distinct_inputs=len(unique[name]),domain='bounded synthetic inputs; no production data')
# Independent negative controls: deliberate fixture defects, not defects found in the skill.
bad_bytes=lambda x:base64.b64decode(base64.b64encode(x.rstrip(b'\0')),validate=True)
x=find(st.binary(max_size=64),lambda x:bad_bytes(x)!=x,settings=search);assert x and bad_bytes(x)!=x
passed('negative_control_trailing_zero_loss',counterexample_hex=x.hex(),decoded_hex=bad_bytes(x).hex(),classification='intentional fixture implementation defect')
x=find(integers,lambda x:Counter(sorted(set(x)))!=Counter(x),settings=search);assert len(x)>len(set(x))
passed('negative_control_sort_deduplication',counterexample=x,actual=sorted(set(x)),classification='intentional fixture implementation defect')
x=find(st.dictionaries(scalar_text,st.integers(),min_size=1,max_size=8),lambda x:json.loads(json.dumps(dict(list(x.items())[:-1])))!=x,settings=search)
passed('negative_control_json_field_loss',counterexample=x,actual=dict(list(x.items())[:-1]),classification='intentional fixture implementation defect')
x=find(scalar_text,lambda x:unicodedata.normalize('NFD','')!=unicodedata.normalize('NFD',x),settings=search);assert x
passed('negative_control_constant_normalizer',counterexample=x,actual='',classification='intentional fixture implementation defect')
assert (lambda s:'')((lambda s:'')(x))==(lambda s:'')(x)
passed('weak_idempotence_accepts_constant',counterexample=x,meaning='idempotence alone misses this preservation defect')
for raw in [b'?',b'A',b'===',b'A===',b'%%%%']:
try:base64.b64decode(raw,validate=True)
except binascii.Error:passed('strict_base64_invalid_'+raw.hex(),input_ascii=raw.decode(),classification='documented error path')
else:raise AssertionError('Invalid input accepted')
try:json.dumps({'text':'\ud800'},ensure_ascii=False).encode('utf-8')
except UnicodeEncodeError:passed('surrogate_outside_scalar_text_domain',classification='out-of-domain strategy example, not an in-domain encoder bug')
else:raise AssertionError('Unexpected surrogate encoding')
assert len(cases)==15 and all(c['status']=='passed' for c in cases)
result={'executed_at':dt.datetime.now(dt.UTC).isoformat(),'python':platform.python_version(),'platform':platform.platform(),'hypothesis':hypothesis.__version__,'probe_sha256':hashlib.sha256(Path(__file__).read_bytes()).hexdigest(),'cases':cases,'observations':len(cases),'generated_families':4,'property_examples':counts,'native_fixture_tested':True,'runtime_tested':False,'scope':'Native Hypothesis on four synthetic property families and four intentional negative controls, five invalid encodings and an out-of-domain surrogate. No AI client, whole skill, upstream harness, smart-contract engine, paid model or production input.'}
(root/'native-property-evidence.json').write_text(json.dumps(result,ensure_ascii=True,indent=2),encoding='utf-8');print(json.dumps(result,ensure_ascii=True))