mirror of
https://github.com/c3lang/c3c.git
synced 2026-02-27 12:01:16 +00:00
support for List in quicksort; add HashMap.@each (#861)
* compiler: fix typo in error message about failed name resolution Signed-off-by: Pierre Curto <pierre.curto@gmail.com> * lib/std/sort: add List support to quicksort Signed-off-by: Pierre Curto <pierre.curto@gmail.com> * lib/std/collections: add HashMap.@each Signed-off-by: Pierre Curto <pierre.curto@gmail.com> --------- Signed-off-by: Pierre Curto <pierre.curto@gmail.com>
This commit is contained in:
56
test/unit/stdlib/collections/map.c3
Normal file
56
test/unit/stdlib/collections/map.c3
Normal file
@@ -0,0 +1,56 @@
|
||||
module map_test @test;
|
||||
import std::collections::list;
|
||||
import std::collections::map;
|
||||
import std::sort;
|
||||
import std::io;
|
||||
|
||||
def Map = HashMap(<String, usz>);
|
||||
|
||||
struct MapTest
|
||||
{
|
||||
String key;
|
||||
usz value;
|
||||
}
|
||||
def List = List(<MapTest>);
|
||||
|
||||
fn void map()
|
||||
{
|
||||
Map m;
|
||||
assert(!m.is_initialized());
|
||||
m.tinit();
|
||||
assert(m.is_initialized());
|
||||
assert(m.is_empty());
|
||||
assert(m.len() == 0);
|
||||
|
||||
m.set("a", 1);
|
||||
assert(!m.is_empty());
|
||||
assert(m.len() == 1);
|
||||
m.remove("a");
|
||||
assert(m.is_empty());
|
||||
|
||||
MapTest[] tcases = { {"key1", 0}, {"key2", 1}, {"key3", 2} };
|
||||
foreach (tc : tcases)
|
||||
{
|
||||
m.set(tc.key, tc.value);
|
||||
}
|
||||
assert(m.len() == tcases.len);
|
||||
foreach (tc : tcases)
|
||||
{
|
||||
usz v = m.get(tc.key)!!;
|
||||
assert(tc.value == v);
|
||||
}
|
||||
|
||||
List list;
|
||||
list.tinit();
|
||||
m.@each(;String key, usz value)
|
||||
{
|
||||
list.push({key, value});
|
||||
};
|
||||
assert(list.len() == tcases.len);
|
||||
quicksort(list, fn int (MapTest a, MapTest b) => (int)(a.value - b.value));
|
||||
foreach (i, tc : tcases)
|
||||
{
|
||||
assert(tc.key == list[i].key);
|
||||
assert(tc.value == list[i].value);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user