HashMap.getOrPutAssumeCapacityAdapted should set key to undefined (#9138)

* std.hash_map.HashMap: getOrPutAssumeCapacityAdapted should set key to undefined

* add test for std.hash_map.HashMap.getOrPutAdapted
This commit is contained in:
hadroncfy 2021-06-18 13:52:30 +08:00 committed by GitHub
parent 6ce8440987
commit 1f29b75f08
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1236,7 +1236,7 @@ pub fn HashMapUnmanaged(
metadata[0].fill(fingerprint); metadata[0].fill(fingerprint);
const new_key = &self.keys()[idx]; const new_key = &self.keys()[idx];
const new_value = &self.values()[idx]; const new_value = &self.values()[idx];
new_key.* = key; new_key.* = undefined;
new_value.* = undefined; new_value.* = undefined;
self.size += 1; self.size += 1;
@ -1884,6 +1884,52 @@ test "std.hash_map clone" {
} }
} }
test "std.hash_map getOrPutAdapted" {
const AdaptedContext = struct {
fn eql(self: @This(), adapted_key: []const u8, test_key: u64) bool {
return std.fmt.parseInt(u64, adapted_key, 10) catch unreachable == test_key;
}
fn hash(self: @This(), adapted_key: []const u8) u64 {
const key = std.fmt.parseInt(u64, adapted_key, 10) catch unreachable;
return (AutoContext(u64){}).hash(key);
}
};
var map = AutoHashMap(u64, u64).init(testing.allocator);
defer map.deinit();
const keys = [_][]const u8{
"1231",
"4564",
"7894",
"1132",
"65235",
"95462",
"0112305",
"00658",
"0",
"2",
};
var real_keys: [keys.len]u64 = undefined;
inline for (keys) |key_str, i| {
const result = try map.getOrPutAdapted(key_str, AdaptedContext{});
try testing.expect(!result.found_existing);
real_keys[i] = std.fmt.parseInt(u64, key_str, 10) catch unreachable;
result.key_ptr.* = real_keys[i];
result.value_ptr.* = i * 2;
}
try testing.expectEqual(map.count(), keys.len);
inline for (keys) |key_str, i| {
const result = try map.getOrPutAdapted(key_str, AdaptedContext{});
try testing.expect(result.found_existing);
try testing.expectEqual(real_keys[i], result.key_ptr.*);
try testing.expectEqual(@as(u64, i) * 2, result.value_ptr.*);
}
}
test "compile everything" { test "compile everything" {
std.testing.refAllDecls(AutoHashMap(i32, i32)); std.testing.refAllDecls(AutoHashMap(i32, i32));
std.testing.refAllDecls(StringHashMap([]const u8)); std.testing.refAllDecls(StringHashMap([]const u8));