v2 / vlib / v / tests / fns / go_array_wait_test.v
52 lines · 47 sloc · 940 bytes · 99be39cbd15d4bbb5ab14d2f870199908c00bc8d
Raw
1// vtest retry: 3
2
3fn f(x f64) f64 {
4 y := x * x
5 return y
6}
7
8fn test_array_thread_f64_wait() {
9 mut r := []thread f64{cap: 10}
10 for i in 0 .. 10 {
11 r << spawn f(f64(i) + 0.5)
12 }
13 x := r.wait()
14 assert x == [0.25, 2.25, 6.25, 12.25, 20.25, 30.25, 42.25, 56.25, 72.25, 90.25]
15}
16
17fn g(shared a []int, i int) {
18 lock a {
19 a[i] *= a[i] + 1
20 }
21}
22
23fn test_array_thread_void_wait() {
24 shared a := [2, 3, 5, 7, 11, 13, 17]
25 t := [
26 spawn g(shared a, 0),
27 spawn g(shared a, 3),
28 spawn g(shared a, 6),
29 spawn g(shared a, 2),
30 spawn g(shared a, 1),
31 spawn g(shared a, 5),
32 spawn g(shared a, 4),
33 ]
34 println('threads started')
35 t.wait()
36 rlock a {
37 assert a == [6, 12, 30, 56, 132, 182, 306]
38 }
39}
40
41fn test_void_thread_decl() {
42 shared a := [2, 3, 9]
43 mut t1 := spawn g(shared a, 0)
44 mut tarr := []thread{len: 2}
45 tarr[0] = spawn g(shared a, 1)
46 tarr[1] = spawn g(shared a, 2)
47 t1.wait()
48 tarr.wait()
49 rlock a {
50 assert a == [6, 12, 90]
51 }
52}
53