Skip to content

Commit

Permalink
cgen: fix iteration over shared map (#9763)
Browse files Browse the repository at this point in the history
  • Loading branch information
UweKrueger committed Apr 16, 2021
1 parent 524becd commit 80bd297
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 1 deletion.
5 changes: 4 additions & 1 deletion vlib/v/gen/c/cgen.v
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
73 changes: 73 additions & 0 deletions vlib/v/tests/shared_map_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 80bd297

Please sign in to comment.