| ... | ... | @@ -31,8 +31,8 @@ pub const BufMap = struct { |
| 31 | 31 | if (self.hash_map.get(key)) |entry| { |
| 32 | 32 | const value_copy = try self.copy(value); |
| 33 | 33 | errdefer self.free(value_copy); |
| 34 | | _ = try self.hash_map.put(key, value_copy); |
| 35 | | self.free(entry.value); |
| 34 | const old_value = ??(try self.hash_map.put(key, value_copy)); |
| 35 | self.free(old_value); |
| 36 | 36 | } else { |
| 37 | 37 | const key_copy = try self.copy(key); |
| 38 | 38 | errdefer self.free(key_copy); |
| ... | ... | @@ -71,3 +71,29 @@ pub const BufMap = struct { |
| 71 | 71 | return result; |
| 72 | 72 | } |
| 73 | 73 | }; |
| 74 | |
| 75 | const assert = @import("debug/index.zig").assert; |
| 76 | const heap = @import("heap.zig"); |
| 77 | |
| 78 | test "BufMap" { |
| 79 | var direct_allocator = heap.DirectAllocator.init(); |
| 80 | defer direct_allocator.deinit(); |
| 81 | |
| 82 | var bufmap = BufMap.init(&direct_allocator.allocator); |
| 83 | defer bufmap.deinit(); |
| 84 | |
| 85 | try bufmap.set("x", "1"); |
| 86 | assert(mem.eql(u8, ??bufmap.get("x"), "1")); |
| 87 | assert(1 == bufmap.count()); |
| 88 | |
| 89 | try bufmap.set("x", "2"); |
| 90 | assert(mem.eql(u8, ??bufmap.get("x"), "2")); |
| 91 | assert(1 == bufmap.count()); |
| 92 | |
| 93 | try bufmap.set("x", "3"); |
| 94 | assert(mem.eql(u8, ??bufmap.get("x"), "3")); |
| 95 | assert(1 == bufmap.count()); |
| 96 | |
| 97 | bufmap.delete("x"); |
| 98 | assert(0 == bufmap.count()); |
| 99 | } |