diff options
| author | Andrew Kelley <superjoe30@gmail.com> | 2018-04-10 23:37:08 -0400 |
|---|---|---|
| committer | Andrew Kelley <superjoe30@gmail.com> | 2018-04-10 23:37:08 -0400 |
| commit | 19e0ed5d3e303771e672f8cec42adb67a13fa3af (patch) | |
| tree | db96e9ca0f4c387e68455b288df3ba3917a44822 /std/buf_map.zig | |
| parent | 2ec1cec92d018d25f720c43a7586a12eafd2cbeb (diff) | |
| parent | 9e8519b7a2c765b427f85f0aaa456256785eceb7 (diff) | |
| download | zig-19e0ed5d3e303771e672f8cec42adb67a13fa3af.tar.gz zig-19e0ed5d3e303771e672f8cec42adb67a13fa3af.zip | |
Merge branch 'fix879' of https://github.com/bnoordhuis/zig into bnoordhuis-fix879
Diffstat (limited to 'std/buf_map.zig')
| -rw-r--r-- | std/buf_map.zig | 30 |
1 files changed, 28 insertions, 2 deletions
diff --git a/std/buf_map.zig b/std/buf_map.zig index a58df4b2db..7e2ea99f1a 100644 --- a/std/buf_map.zig +++ b/std/buf_map.zig @@ -31,8 +31,8 @@ pub const BufMap = struct { if (self.hash_map.get(key)) |entry| { const value_copy = try self.copy(value); errdefer self.free(value_copy); - _ = try self.hash_map.put(key, value_copy); - self.free(entry.value); + const old_value = ??(try self.hash_map.put(key, value_copy)); + self.free(old_value); } else { const key_copy = try self.copy(key); errdefer self.free(key_copy); @@ -71,3 +71,29 @@ pub const BufMap = struct { return result; } }; + +const assert = @import("debug/index.zig").assert; +const heap = @import("heap.zig"); + +test "BufMap" { + var direct_allocator = heap.DirectAllocator.init(); + defer direct_allocator.deinit(); + + var bufmap = BufMap.init(&direct_allocator.allocator); + defer bufmap.deinit(); + + try bufmap.set("x", "1"); + assert(mem.eql(u8, ??bufmap.get("x"), "1")); + assert(1 == bufmap.count()); + + try bufmap.set("x", "2"); + assert(mem.eql(u8, ??bufmap.get("x"), "2")); + assert(1 == bufmap.count()); + + try bufmap.set("x", "3"); + assert(mem.eql(u8, ??bufmap.get("x"), "3")); + assert(1 == bufmap.count()); + + bufmap.delete("x"); + assert(0 == bufmap.count()); +} |
