Hash maps now copy keys if keys are copyable.

This commit is contained in:
Christoffer Lerno
2023-08-10 21:14:24 +02:00
parent 356b6bb1b7
commit 3e765a3f3e
8 changed files with 80 additions and 18 deletions

View File

@@ -0,0 +1,30 @@
module test;
import std::io;
import std::collections::map;
fn void! copy_map() @test
{
TrackingAllocator alloc;
alloc.init(mem::heap());
io::printfn("Heap mem: %d", alloc.allocated());
mem::@scoped(&alloc)
{
HashMap(<String, int>) x;
x.init();
DString y;
y.append("hello");
x.set(y.as_str(), 123);
y.append("bye");
x.set(y.as_str(), 333);
y.clear();
y.append("bye");
x.set(y.as_str(), 444);
assert(x.get("hello")! == 123);
assert(x.get("hellobye")! == 333);
assert(x.get("bye")! == 444);
assert(alloc.allocated() > 0);
x.free();
y.free();
assert(alloc.allocated() == 0);
};
}