// expect: run // result: 42 // phase: 6 // spec: docs/spec/16-standard-library.md ยง2.14 // grant: heap=mem.alloc:251 module test.hashmap import std.hashmap; import std.option; fn opened) -> u64 effects [], E: fn(ref, ref) -> bool effects []>(capacity: usize, heap: cap) -> std.hashmap.HashMap effects [mem.alloc] { match std.hashmap.new::(capacity, heap) { std.hashmap.NewResult::::ok(map) => { return move map; } std.hashmap.NewResult::::err(error) => { trap; } } } fn inserted(result: std.hashmap.InsertResult) -> void effects [] { match move result { std.hashmap.InsertResult::::inserted => {}, std.hashmap.InsertResult::::full(value) => { trap; }, std.hashmap.InsertResult::::replaced(value) => { trap; } } } fn extracted(value: std.option.Option) -> T effects [] { match move value { std.option.Option::::some(item) => { return move item; }, std.option.Option::::none => { trap; } } } fn released) -> u64 effects [], E: fn(ref, ref) -> bool effects []>(map: std.hashmap.HashMap) -> void effects [] { match std.hashmap.free::(move map) { std.hashmap.FreeResult::::ok => {}, std.hashmap.FreeResult::::not_empty(value) => { trap; } } } struct Key { id: u64 } fn hash(key: ref) -> u64 effects [] { return 3; } fn equal(a: ref, b: ref) -> bool effects [] { return a.id == b.id; } fn check(heap: cap) -> void effects [mem.alloc] { let map = opened::(1, heap); { mut q = map; inserted::(std.hashmap.insert::(q, Key { id: 1 }, 42)); } let empty = std.hashmap.Bucket { hash: 0, index: 0, state: std.hashmap.BucketState::empty }; var before: [std.hashmap.Bucket; 2] = [empty; 2]; { ref q = map; ref index = q.buckets; ref buckets = buffer.view(index); before[0] = buckets[0]; before[1] = buckets[1]; } { mut q = map; match std.hashmap.reserve::(q, 2, heap) { std.hashmap.ReserveResult::err(error) => { match error { alloc_error::budget_exceeded => {}, alloc_error::out_of_memory => { trap; } } }, std.hashmap.ReserveResult::ok => { trap; } } assert(std.hashmap.capacity::(ref q) == 1usize); } { ref q = map; ref index = q.buckets; ref buckets = buffer.view(index); for i in 0usize..2usize { assert(buckets[i].hash == before[i].hash); assert(buckets[i].index == before[i].index); match before[i].state { std.hashmap.BucketState::empty => { match buckets[i].state { std.hashmap.BucketState::empty => {}, std.hashmap.BucketState::deleted => { trap; }, std.hashmap.BucketState::occupied => { trap; } } }, std.hashmap.BucketState::deleted => { trap; }, std.hashmap.BucketState::occupied => { match buckets[i].state { std.hashmap.BucketState::occupied => {}, std.hashmap.BucketState::empty => { trap; }, std.hashmap.BucketState::deleted => { trap; } } } } } } { mut q = map; let entry = extracted::>(std.hashmap.pop::(q)); assert(entry.value == 42u64); } released::(move map); let reused = own.new([0u8; 125], heap) or trap; own.free(move reused); } fn main(heap: cap) -> u64 effects [mem.alloc] { let early = cap.narrow.budget(heap, 125usize) or trap; check(early); let late = cap.narrow.budget(heap, 126usize) or trap; check(late); return 42; }