v2 / vlib / v / tests / builtin_arrays / array_count_test.v
36 lines · 29 sloc · 753 bytes · 1eb3867f4e164f55d252bc3305e8b6b6decac5e7
Raw
1fn test_main() {
2 a := []int{len: 10, init: index}
3 assert a.count(it % 2) == 5
4
5 b := [10]int{init: index}
6 assert a.count(it % 2) == 5
7}
8
9fn test_zero() {
10 a := []int{len: 10, init: index}
11 assert a.count(it == 1000) == 0
12
13 b := [10]int{init: index}
14 assert a.count(it == 1000) == 0
15}
16
17fn test_struct() {
18 struct Abc {
19 x int
20 y int
21 z string
22 }
23
24 a := [Abc{}, Abc{1, 2, 'abc'}, Abc{100, 2, 'def'}, Abc{0, 0, 'a'}]
25
26 assert dump(a.count(it.z.starts_with('a'))) == 2
27 assert dump(a.count(it.y == 2)) == 2
28 assert dump(a.count(it.z.len == 1)) == 1
29 assert dump(a.count(it.z.len < 3)) == 2
30
31 sa := ['aa', 'bb', 'ccc']
32 dump(sa)
33 assert dump(sa.count(it.len < 3)) == 2
34 assert dump(sa.count(it == 'aa')) == 1
35 assert dump(sa.count(it.len == 3)) == 1
36}
37