vxx2 / vlib / v / tests / array_empty_no_alloc_test.v
46 lines · 40 sloc · 788 bytes · 8974181e7ec2c61a10841fbf77dc341461b82657
Raw
1struct Holder {
2 q []int
3}
4
5fn test_empty_array_literal_has_nil_data() {
6 a := []int{}
7 assert isnil(a.data)
8 assert a.len == 0
9 assert a.cap == 0
10}
11
12fn test_default_struct_array_field_has_nil_data() {
13 h := Holder{}
14 assert isnil(h.q.data)
15 assert h.q.len == 0
16 assert h.q.cap == 0
17}
18
19fn test_empty_array_grows_on_push() {
20 mut b := []int{}
21 assert isnil(b.data)
22 b << 42
23 assert b.len == 1
24 assert b[0] == 42
25 assert !isnil(b.data)
26}
27
28fn test_empty_array_clone_has_nil_data() {
29 a := []int{}
30 c := a.clone()
31 assert isnil(c.data)
32 assert c.len == 0
33 assert c.cap == 0
34}
35
36fn test_array_with_cap_still_allocates() {
37 d := []int{cap: 5}
38 assert !isnil(d.data)
39 assert d.cap == 5
40}
41
42fn test_array_with_len_still_allocates() {
43 e := []int{len: 3}
44 assert !isnil(e.data)
45 assert e.len == 3
46}
47