map: another fix + test

pull/3157/head
Alexander Medvednikov 2019-12-19 21:17:12 +03:00
parent 26f5d34e64
commit 2096018b00
2 changed files with 28 additions and 11 deletions

View File

@ -57,6 +57,7 @@ fn (m mut map) insert(n mut mapnode, key string, val voidptr) {
C.memcpy(n.val, val, m.element_size)
if n.is_empty {
m.size++
n.is_empty = false
}
return
}

View File

@ -91,39 +91,39 @@ fn test_various_map_value() {
mut m1 := map[string]int
m1['test'] = 1
assert m1['test'] == 1
mut m2 := map[string]string
m2['test'] = 'test'
assert m2['test'] == 'test'
mut m3 := map[string]i8
m3['test'] = i8(0)
assert m3['test'] == i8(0)
mut m4 := map[string]i16
m4['test'] = i16(0)
assert m4['test'] == i16(0)
mut m7 := map[string]u16
m7['test'] = u16(0)
assert m7['test'] == u16(0)
mut m8 := map[string]u32
m8['test'] = u32(0)
assert m8['test'] == u32(0)
mut m9 := map[string]bool
m9['test'] = true
assert m9['test'] == true
mut m10 := map[string]byte
m10['test'] = byte(0)
assert m10['test'] == byte(0)
mut m11 := map[string]f32
m11['test'] = f32(0.0)
assert m11['test'] == f32(0.0)
mut m12 := map[string]f64
m12['test'] = f64(0.0)
assert m12['test'] == f64(0.0)
@ -177,7 +177,7 @@ fn test_delete() {
println(m['two']) // => 0
assert 'two' in m == false
println('two' in m) // => true, on Linux and Windows <-- wrong !
}
}
/*
fn test_ref() {
@ -186,6 +186,22 @@ fn test_ref() {
mut one := &m['one']
one++
println(*one)
}
*/
fn test_delete_size() {
arr := ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
mut m := map[string]int
for _ in 0..10 {
for i in 0..10 {
m[arr[i]] = i
}
assert(m.size == 10)
println(m.size)
for i in 0..10 {
m.delete(arr[i])
}
}
}