Initial Commit

This commit is contained in:
SoraKatadzuma committed 2026-10-28 22:13:49 -05:00
commit 05756094ac
706 files changed
+135430

No files matched your search

+94
View File
@@ -0,0 +1,94 @@
#! @file keymap.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains the tests of the key map and the ID index: keys set, replaced, and
#! taken, as many as make it grow, and IDs found by their hash.
import std::testing;
module sakura::analyzer;
@test def key_maps_find_what_they_were_given(t: &mut tester): void {
var made := keymap::new();
t.expects(made.length == 0 && made.get(&7) is none && made.get_or(&7, 3) == 3);
# Enough keys to grow it several times, of IDs paired as the checker pairs
# them, and 0, which is a key like any other.
var fresh := true;
for i in 1..2001 {
fresh = made.insert(pair_key(i as u32, (i * 7) as u32), i as u32) && fresh;
}
t.expects(fresh && made.insert(0, 99));
t.expects(made.length == 2001 && made.capacity * 3 >= made.length * 4);
var found := true;
for i in 1..2001 {
found = found && made.get_or(&pair_key(i as u32, (i * 7) as u32), 0xFFFFFFFF) == i as u32;
}
t.expects(found && made.get_or(&0, 0) == 99);
# Inserting keeps the value held, and setting replaces it.
t.expects(!made.insert(pair_key(5, 35), 1) && made.get_or(&pair_key(5, 35), 0) == 5);
made.set(pair_key(5, 35), 1);
t.expects(made.get_or(&pair_key(5, 35), 0) == 1 && made.length == 2001);
}
@test def key_maps_find_the_rest_once_one_is_taken(t: &mut tester): void {
var made := keymap::new();
for i in 0..3000 {
made.set(i as u64 * 16, i as u32);
}
# Every third key is taken, and the rest are still found, wherever the keys
# after each taken one moved back to.
for i in 0..3000 {
if i % 3 == 0 {
made.remove(&(i as u64 * 16));
}
}
t.expects(made.length == 2000);
var right := true;
for i in 0..3000 {
val found := made.get(&(i as u64 * 16));
right = right && (i % 3 == 0 ? found is none : found.unwrap_or(0xFFFFFFFF) == i as u32);
}
t.expects(right);
made.clear();
t.expects(made.length == 0 && !made.contains(&16));
made.set(16, 4);
t.expects(made.get_or(&16, 0) == 4);
}
@test def id_indexes_walk_the_ids_of_a_hash(t: &mut tester): void {
var made := id_index::new();
t.expects(made.next(&mut made.walk(7 << 32)) == 0);
made.add(7 << 32, 1);
made.add(9 << 32, 2);
made.add(7 << 32 | 5, 4);
# A walk gives one more than each ID whose hash shares its top half, in the
# order they were added, then 0.
var walk := made.walk(7 << 32);
t.expects(made.next(&mut walk) == 2 && made.next(&mut walk) == 5 && made.next(&mut walk) == 0);
walk = made.walk(9 << 32);
t.expects(made.next(&mut walk) == 3 && made.next(&mut walk) == 0);
walk = made.walk(8 << 32);
t.expects(made.next(&mut walk) == 0);
# As many as make it grow are each still found.
for i in 0..5000 {
made.add(mix(0, i as u64), (i + 10) as u32);
}
var found := true;
for i in 0..5000 {
var each := made.walk(mix(0, i as u64));
var at := made.next(&mut each);
while at != 0 && at != (i + 11) as u32 {
at = made.next(&mut each);
}
found = found && at == (i + 11) as u32;
}
t.expects(found && made.length == 5003);
}