Files
aji/tests/conformance/source/hashmap/owner.aji
T
dev 7bb5bd7931 cli: add safe owning general-key hash maps
Use explicit static hash/equality functions, dense complete key/value entries
and Copy probe indices. Preserve returned ownership and allocation failure
state without unsafe code or new runtime instructions.

Add normative contracts, API/reference budgets, 33 conformance cases, independent
Copy/owner dict traces and 55 CLI/package/profile checks. Full corpus: 947 passing.
Collection-every-step scheduler stress remains in progress, recorded in RFC/plan.
2026-10-07 10:55:09 +03:00

46 lines
2.9 KiB
Rust

// expect: run
// result: 42
// phase: 6
// spec: docs/spec/16-standard-library.md §2.14
// grant: heap=mem.alloc:1024
module test.hashmap
import std.hashmap;
import std.option;
fn opened<K, V, H: fn(ref<K>) -> u64 effects [], E: fn(ref<K>, ref<K>) -> bool effects []>(capacity: usize, heap: cap<mem.alloc>) -> std.hashmap.HashMap<K, V, fn H, fn E> effects [mem.alloc] {
match std.hashmap.new::<K, V, fn H, fn E>(capacity, heap) {
std.hashmap.NewResult::<K, V, fn H, fn E>::ok(map) => { return move map; }
std.hashmap.NewResult::<K, V, fn H, fn E>::err(error) => { trap; }
}
}
fn inserted<K, V>(result: std.hashmap.InsertResult<K,V>) -> void effects [] {
match move result { std.hashmap.InsertResult::<K,V>::inserted => {}, std.hashmap.InsertResult::<K,V>::full(value) => { trap; }, std.hashmap.InsertResult::<K,V>::replaced(value) => { trap; } }
}
fn extracted<T>(value: std.option.Option<T>) -> T effects [] {
match move value { std.option.Option::<T>::some(item) => { return move item; }, std.option.Option::<T>::none => { trap; } }
}
fn released<K, V, H: fn(ref<K>) -> u64 effects [], E: fn(ref<K>, ref<K>) -> bool effects []>(map: std.hashmap.HashMap<K, V, fn H, fn E>) -> void effects [] {
match std.hashmap.free::<K, V, fn H, fn E>(move map) { std.hashmap.FreeResult::<K, V, fn H, fn E>::ok => {}, std.hashmap.FreeResult::<K, V, fn H, fn E>::not_empty(value) => { trap; } }
}
struct Key { id: u64, owner: own<u64> }
fn hash(key: ref<Key>) -> u64 effects [] { return 3; }
fn equal(a: ref<Key>, b: ref<Key>) -> bool effects [] { return a.id == b.id; }
fn consumed(entry: std.hashmap.Entry<Key,own<u64>>) -> u64 effects [] {
let (key, value) = struct.unpack(move entry); let (id, owner) = struct.unpack(move key);
assert(own.take(move owner) == id); return own.take(move value);
}
fn main(heap: cap<mem.alloc>) -> u64 effects [mem.alloc] {
let map = opened::<Key, own<u64>, fn hash, fn equal>(2, heap);
{ mut q = map;
let k1 = Key { id: 1, owner: own.new(1u64, heap) or trap }; let v1 = own.new(11u64, heap) or trap;
inserted::<Key,own<u64>>(std.hashmap.insert::<Key, own<u64>, fn hash, fn equal>(q, move k1, move v1));
let k2 = Key { id: 2, owner: own.new(2u64, heap) or trap }; let v2 = own.new(22u64, heap) or trap;
inserted::<Key,own<u64>>(std.hashmap.insert::<Key, own<u64>, fn hash, fn equal>(q, move k2, move v2));
let query = Key { id: 1, owner: own.new(1u64, heap) or trap };
assert(consumed(extracted::<std.hashmap.Entry<Key,own<u64>>>(std.hashmap.remove::<Key, own<u64>, fn hash, fn equal>(q, ref query))) == 11u64);
let (id, owner) = struct.unpack(move query); own.take(move owner);
match std.hashmap.reserve::<Key, own<u64>, fn hash, fn equal>(q, 3, heap) { std.hashmap.ReserveResult::ok => {}, std.hashmap.ReserveResult::err(error) => { trap; } }
assert(consumed(extracted::<std.hashmap.Entry<Key,own<u64>>>(std.hashmap.pop::<Key, own<u64>, fn hash, fn equal>(q))) == 22u64);
}
released::<Key, own<u64>, fn hash, fn equal>(move map); return 42;
}