code wiki / (root) / map_test.nx

map_test.nx source

↩ module page · 62 lines · 1699 B

1// map_test.nx -- Robin-hood hashmap self-test. 2 3import "syscalls.nx" 4import "map.nx" 5 6func main() -> i64 { 7 let m: *Map = map_new(0) 8 if map_len(m) != 0 { return 1 } 9 10 // Basic insert + get. 11 map_insert(m, 100, 1000) 12 map_insert(m, 200, 2000) 13 map_insert(m, 300, 3000) 14 if map_len(m) != 3 { return 2 } 15 16 let out_raw: *u8 = sys_mmap(16) 17 let out: *i64 = out_raw as *i64 18 *out = 0 19 if map_get(m, 100, out) != 1 { return 3 } 20 if *out != 1000 { return 4 } 21 if map_get(m, 200, out) != 1 { return 5 } 22 if *out != 2000 { return 6 } 23 if map_get(m, 300, out) != 1 { return 7 } 24 if *out != 3000 { return 8 } 25 26 // Miss. 27 if map_get(m, 999, out) != 0 { return 9 } 28 29 // Overwrite. 30 map_insert(m, 200, 20000) 31 if map_get(m, 200, out) != 1 { return 10 } 32 if *out != 20000 { return 11 } 33 if map_len(m) != 3 { return 12 } 34 35 // Growth: insert enough entries to cross 0.75 load factor and 36 // verify each key stays findable. 37 var i: i64 = 0 38 while i < 100 { 39 map_insert(m, 1000 + i, 7000 + i) 40 i = i + 1 41 } 42 if map_len(m) != 103 { return 13 } 43 i = 0 44 while i < 100 { 45 if map_get(m, 1000 + i, out) != 1 { return 20 + i } 46 if *out != 7000 + i { return 200 + i } 47 i = i + 1 48 } 49 50 // Remove. 51 if map_remove(m, 100) != 1 { return 500 } 52 if map_len(m) != 102 { return 501 } 53 if map_get(m, 100, out) != 0 { return 502 } 54 // Other keys still present. 55 if map_get(m, 200, out) != 1 { return 503 } 56 if *out != 20000 { return 504 } 57 58 // Remove miss. 59 if map_remove(m, 999) != 0 { return 505 } 60 61 return 0 62}