v2 / vlib / arrays / parallel / parallel_test.v
67 lines · 61 sloc · 1.42 KB · a80bc2331450fc28c900097f8afafe173f161d27
Raw
1// vtest build: !musl?
2import arrays.parallel
3import rand
4import time
5
6fn test_parallel_run_with_empty_arrays() {
7 parallel.run([]int{}, fn (x int) {})
8 parallel.run([]u8{}, fn (x u8) {})
9 parallel.run([]u32{}, fn (x u32) {}, workers: 1000)
10 assert true
11}
12
13fn test_parallel_amap_with_empty_arrays() {
14 assert parallel.amap([]int{}, fn (x int) u8 {
15 return 0
16 }) == []
17 assert parallel.amap([]u8{}, fn (x u8) int {
18 return 0
19 }) == []
20 assert parallel.amap([]u8{}, fn (x u8) int {
21 return 0
22 }, workers: 1000) == []
23 assert true
24}
25
26fn test_parallel_run() {
27 counters := []int{len: 10, init: index}
28 dump(counters)
29 mut res := []string{len: 10}
30 mut pres := &res
31 parallel.run(counters, fn [mut pres] (i int) {
32 delay := rand.intn(250) or { 250 }
33 time.sleep(delay * time.millisecond)
34 unsafe {
35 pres[i] = 'task ${i}, delay=${delay}ms'
36 }
37 assert true
38 })
39 dump(res)
40 assert res.len == counters.len
41}
42
43fn test_parallel_amap() {
44 input := [1, 2, 3, 4, 5, 6, 7, 8, 9]
45 dump(input)
46 dump(input.len)
47 output := parallel.amap(input, fn (i int) int {
48 delay := rand.intn(250) or { 250 }
49 time.sleep(delay * time.millisecond)
50 return i * i
51 })
52 dump(output)
53 dump(output.len)
54 assert input.len == output.len
55
56 for i, _ in output {
57 assert output[i] == input[i] * input[i]
58 }
59
60 // unordered output validation
61 assert output.len == input.len
62 op_sorted := output.sorted()
63 dump(op_sorted)
64 for i, op in op_sorted {
65 assert op == input[i] * input[i]
66 }
67}
68