v2 / vlib / v / tests / fns / mut_closure_fixed_array_thread_test.v
39 lines · 33 sloc · 859 bytes · 689f363cc938bd171256792987aabdba10588f77
Raw
1module main
2
3const fixed_array_size = 4
4
5fn forward_threads(threads [fixed_array_size]thread) thread {
6 return threads[0]
7}
8
9fn worker_thread() {}
10
11fn test_mut_closure_capture_can_update_fixed_array_elements() {
12 mut values := [fixed_array_size]int{}
13 update := fn [mut values] () {
14 values[0] = 42
15 values[3] = 7
16 }
17 update()
18 assert values[0] == 42
19 assert values[3] == 7
20}
21
22fn test_mut_closure_capture_of_fixed_array_sees_late_updates() {
23 mut values := [fixed_array_size]int{}
24 read := fn [mut values] () int {
25 return values[0]
26 }
27 values[0] = 99
28 assert read() == 99
29}
30
31fn test_mut_closure_capture_of_fixed_array_of_threads_sees_late_updates() {
32 mut threads := [fixed_array_size]thread{}
33 read := fn [mut threads] () thread {
34 return forward_threads(threads)
35 }
36 threads[0] = spawn worker_thread()
37 assert read() == threads[0]
38 threads[0].wait()
39}
40