mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
cgen: fix iteration over shared
map (#9763)
This commit is contained in:
parent
524becd523
commit
80bd2974b4
@ -1538,7 +1538,10 @@ fn (mut g Gen) for_in_stmt(node ast.ForInStmt) {
|
||||
g.expr(node.cond)
|
||||
g.writeln(';')
|
||||
}
|
||||
arw_or_pt := if node.cond_type.is_ptr() { '->' } else { '.' }
|
||||
mut arw_or_pt := if node.cond_type.is_ptr() { '->' } else { '.' }
|
||||
if node.cond_type.has_flag(.shared_f) {
|
||||
arw_or_pt = '->val.'
|
||||
}
|
||||
idx := g.new_tmp_var()
|
||||
map_len := g.new_tmp_var()
|
||||
g.empty_line = true
|
||||
|
@ -82,3 +82,76 @@ fn new_map() map[string]f64 {
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
fn test_shared_array_iteration() {
|
||||
shared a := [12.75, -0.125, 17]
|
||||
mut n0 := 0
|
||||
mut n1 := 0
|
||||
mut n2 := 0
|
||||
rlock a {
|
||||
for i, val in a {
|
||||
match i {
|
||||
1 {
|
||||
assert val == -0.125
|
||||
n1++
|
||||
// check for order, too:
|
||||
assert n0 == 1
|
||||
}
|
||||
0 {
|
||||
assert val == 12.75
|
||||
n0++
|
||||
}
|
||||
2 {
|
||||
assert val == 17.0
|
||||
n2++
|
||||
assert n1 == 1
|
||||
}
|
||||
else {
|
||||
// this should not happen
|
||||
assert false
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// make sure we have iterated over each of the 3 keys exactly once
|
||||
assert n0 == 1
|
||||
assert n1 == 1
|
||||
assert n2 == 1
|
||||
}
|
||||
|
||||
fn test_shared_map_iteration() {
|
||||
shared m := map{
|
||||
'qwe': 12.75
|
||||
'rtz': -0.125
|
||||
'k': 17
|
||||
}
|
||||
mut n0 := 0
|
||||
mut n1 := 0
|
||||
mut n2 := 0
|
||||
rlock m {
|
||||
for k, val in m {
|
||||
match k {
|
||||
'rtz' {
|
||||
assert val == -0.125
|
||||
n0++
|
||||
}
|
||||
'qwe' {
|
||||
assert val == 12.75
|
||||
n1++
|
||||
}
|
||||
'k' {
|
||||
assert val == 17.0
|
||||
n2++
|
||||
}
|
||||
else {
|
||||
// this should not happen
|
||||
assert false
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// make sure we have iterated over each of the 3 keys exactly once
|
||||
assert n0 == 1
|
||||
assert n1 == 1
|
||||
assert n2 == 1
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user