Register std.map with bounded U64Map<V>, exact key equality and explicit allocation/growth. Preserve returned ownership, old slots on reserve failure and original-authority refunds; support Copy values, owners and resources. Cover collision/tombstone/wrap behavior, independent dictionary traces, quota/cleanup/borrow/task rules, source/AIR parity and hosted/package CLI. Publish the API, roadmap and measured documentation cost. General key hashing/equality remains Q-037; numeric maps do not satisfy that gate.
71 lines
4.2 KiB
Python
71 lines
4.2 KiB
Python
#!/usr/bin/env python3
|
|
"""Check public map traces against independent Python dictionaries.
|
|
|
|
The oracle models bounded capacity, values and returned ownership, never
|
|
hashing, probing, tombstones or physical slots. Golden .aji traces are
|
|
checked byte-for-byte and executed by conformance.
|
|
"""
|
|
from pathlib import Path
|
|
import random
|
|
import re
|
|
|
|
ROOT = Path(__file__).resolve().parents[1] / "tests/conformance/source/map"
|
|
|
|
def main(body):
|
|
return "fn main(heap: cap<mem.alloc>) -> u64 effects [mem.alloc] {\n" + body + "\n return 42;\n}\n"
|
|
|
|
def test(name, body):
|
|
if name == "model":
|
|
body = body.replace("struct.unpack(move entry)", "struct.unpack(entry)")
|
|
body = re.sub(r"(std\.map\.(?:get|contains|len|capacity)::<[^\n(]+>\()(q)([,)])", r"\1ref \2\3", body)
|
|
text = (ROOT / (name + ".aji")).read_text()
|
|
actual = text[text.index("fn main("):]
|
|
assert actual == body + "\n", name + " disagrees with Python dictionary oracle"
|
|
|
|
for owning,seed,steps in [(False,1701,72),(True,1702,56)]:
|
|
rng=random.Random(seed);model={};capacity=2;ty='own<u64>' if owning else 'u64'
|
|
lines=[f' let map = opened::<{ty}>({capacity}, heap);',' { mut q = map;']
|
|
for step in range(steps):
|
|
key=rng.choice([0,1,8,16,24,32,2**64-1]);value=rng.randrange(1,1000)
|
|
op=rng.choice(['insert','insert','remove','contains','get'] if not owning else ['insert','insert','remove','contains'])
|
|
if step in (18,36):
|
|
capacity+=2;lines.append(f' match std.map.reserve::<{ty}>(q, {capacity}, heap) {{ std.map.ReserveResult::ok => {{}}, std.map.ReserveResult::err(error) => {{ trap; }} }}')
|
|
if op=='insert':
|
|
tag='replaced' if key in model else 'full' if len(model)==capacity else 'inserted'
|
|
expected=model.get(key,value)
|
|
if owning:lines.append(f' let input{step} = own.new({value}u64, heap) or trap;')
|
|
inp=f'move input{step}' if owning else str(value)
|
|
lines.append(f' match std.map.insert::<{ty}>(q, {key}, {inp}) {{')
|
|
for variant in ['inserted','replaced','full']:
|
|
arg='' if variant=='inserted' else '(returned)'
|
|
expr='own.take(move returned)' if owning else 'returned'
|
|
action='{}' if variant==tag=='inserted' else '{ assert('+expr+f' == {expected}u64); }}' if variant==tag else '{ trap; }'
|
|
lines.append(f' std.map.InsertResult::<{ty}>::{variant}{arg} => {action}')
|
|
lines.append(' }')
|
|
if tag!='full':model[key]=value
|
|
elif op=='remove':
|
|
expected=model.pop(key,None)
|
|
expr='own.take(move returned)' if owning else 'returned'
|
|
some='{ trap; }' if expected is None else '{ assert('+expr+f' == {expected}u64); }}'
|
|
none='{}' if expected is None else '{ trap; }'
|
|
lines.append(f' match std.map.remove::<{ty}>(q, {key}) {{ std.option.Option::<{ty}>::some(returned) => {some}, std.option.Option::<{ty}>::none => {none} }}')
|
|
elif op=='get':
|
|
expected=model.get(key);some='{ trap; }' if expected is None else f'{{ assert(returned == {expected}u64); }}';none='{}' if expected is None else '{ trap; }'
|
|
lines.append(f' match std.map.get::<u64>(q, {key}) {{ std.option.Option::<u64>::some(returned) => {some}, std.option.Option::<u64>::none => {none} }}')
|
|
else:lines.append(f' assert(std.map.contains::<{ty}>(q, {key}) == '+str(key in model).lower()+');')
|
|
lines.append(f' assert(std.map.len::<{ty}>(q) == {len(model)}usize);')
|
|
lines.append(f' var seen: [bool; {len(model) or 1}] = zero;')
|
|
for _ in model:
|
|
lines.append(f' {{ let entry = extracted::<std.map.Entry<{ty}>>(std.map.pop::<{ty}>(q)); let (key, value) = struct.unpack(move entry);')
|
|
if owning:lines.append(' let number = own.take(move value);')
|
|
else:lines.append(' let number = value;')
|
|
lines.append(' match key {')
|
|
for index,(key,value) in enumerate(sorted(model.items())):lines.append(f' {key}u64 => {{ assert(number == {value}u64); assert(not seen[{index}]); seen[{index}] = true; }}')
|
|
lines.append(' _ => { trap; } } }')
|
|
lines.append(f' assert(std.map.len::<{ty}>(q) == 0usize);')
|
|
lines.append(f' match std.map.pop::<{ty}>(q) {{ std.option.Option::<std.map.Entry<{ty}>>::none => {{}}, std.option.Option::<std.map.Entry<{ty}>>::some(unexpected) => {{ trap; }} }}')
|
|
lines+=[' }',f' released::<{ty}>(move map);']
|
|
test('model_owner' if owning else 'model',main('\n'.join(lines)))
|
|
|
|
print("map oracle traces passed (2 seeds, 128 operations; Copy and owning values)")
|