Fix hashmap put all for create + test case

Closes: #1695
This commit is contained in:
Tomas Kallup
2024-12-17 23:54:35 +01:00
committed by Christoffer Lerno
parent 42c9c9894b
commit ca88afbf5b
4 changed files with 40 additions and 7 deletions

View File

@@ -470,8 +470,11 @@ fn void HashMap.put_all_for_create(&map, HashMap* other_map) @private
if (!other_map.count) return; if (!other_map.count) return;
foreach (Entry *e : other_map.table) foreach (Entry *e : other_map.table)
{ {
if (!e) continue; while (e)
map.put_for_create(e.key, e.value); {
map.put_for_create(e.key, e.value);
e = e.next;
}
} }
} }

View File

@@ -131,8 +131,11 @@ fn Map new_from_map(Map other_map, Allocator allocator = null)
if (!other_map_impl.count) return (Map)map; if (!other_map_impl.count) return (Map)map;
foreach (Entry *e : other_map_impl.table) foreach (Entry *e : other_map_impl.table)
{ {
if (!e) continue; while (e)
map._put_for_create(e.key, e.value); {
map._put_for_create(e.key, e.value);
e = e.next;
}
} }
return (Map)map; return (Map)map;
} }

View File

@@ -7,6 +7,7 @@ None
### Fixes ### Fixes
- Fix case trying to initialize a `char[*]*` from a String. - Fix case trying to initialize a `char[*]*` from a String.
- Fix Map & HashMap `put_all_for_create` not copying all elements, causing `init_from_map` to create incomplete copy.
### Stdlib changes ### Stdlib changes
- Increase BitWriter.write_bits limit up to 32 bits. - Increase BitWriter.write_bits limit up to 32 bits.

View File

@@ -4,7 +4,8 @@ import std::collections::map;
import std::sort; import std::sort;
import std::io; import std::io;
def Map = HashMap(<String, usz>); def TestHashMap = HashMap(<String, usz>);
def TestMap = Map(<String, usz>);
struct MapTest struct MapTest
{ {
@@ -15,7 +16,7 @@ def List = List(<MapTest>);
fn void map() fn void map()
{ {
Map m; TestHashMap m;
assert(!m.is_initialized()); assert(!m.is_initialized());
m.temp_init(); m.temp_init();
assert(m.is_initialized()); assert(m.is_initialized());
@@ -57,10 +58,35 @@ fn void map()
fn void map_remove() fn void map_remove()
{ {
Map m; TestHashMap m;
assert(!@ok(m.remove("A"))); assert(!@ok(m.remove("A")));
m.temp_init(); m.temp_init();
assert(!@ok(m.remove("A"))); assert(!@ok(m.remove("A")));
m.set("A", 0); m.set("A", 0);
assert(@ok(m.remove("A"))); assert(@ok(m.remove("A")));
} }
fn void map_copy()
{
TestHashMap hash_map;
hash_map.temp_init();
hash_map.set("aa", 1);
hash_map.set("b", 2);
hash_map.set("bb", 1);
TestHashMap hash_map_copy;
hash_map_copy.temp_init_from_map(&hash_map);
assert(hash_map_copy.len() == hash_map.len());
TestMap map = map::temp(<String, usz>)();
map.set("aa", 1);
map.set("b", 2);
map.set("bb", 1);
TestMap map_copy = map::temp_from_map(<String, usz>)(map);
assert(map_copy.len() == map.len());
}