v2 / vlib / v / tests / builtin_arrays / array_init_test.v
320 lines · 274 sloc · 8.21 KB · d444f9c25dea51ee70d1a8ec896b34484370e791
Raw
1struct Init {
2 len int
3}
4
5fn test_array_init() {
6 b := [1, 2, 3]
7 mut a := []int{cap: b.len}
8 a << 1
9 assert '${a}, ${a.len}, ${a.cap}' == '[1], 1, 3'
10
11 c := Init{
12 len: 3
13 }
14 mut d := []string{cap: c.len}
15 d << 'aaa'
16 d << 'bbb'
17 assert '${d}, ${d.len}, ${d.cap}' == "['aaa', 'bbb'], 2, 3"
18}
19
20fn test_nested_array_init() {
21 mut a := [][]int{}
22 mut b := [][][]string{cap: 10}
23 mut c := [][][]string{len: 3, init: [][]string{len: 2}}
24 // mut c := [][][]string{len: 2, init: [][]string{len: 2, init: []string{len: 2, init: 'hello'}}}
25 a << [1, 2]
26 a << [3, 4]
27 b << [['foo', 'bar'], ['baz']]
28 b << [['qux']]
29
30 assert '${a}' == '[[1, 2], [3, 4]]'
31 assert '${b}' == "[[['foo', 'bar'], ['baz']], [['qux']]]"
32 assert '${c}' == '[[[], []], [[], []], [[], []]]'
33}
34
35fn test_array_init_with_default() {
36 a1 := []int{len: 4, init: 2}
37 assert '${a1}' == '[2, 2, 2, 2]'
38
39 a2 := []int{len: 3, init: 12345}
40 assert '${a2}' == '[12345, 12345, 12345]'
41
42 b1 := []string{len: 3, init: 'abc'}
43 assert '${b1}' == "['abc', 'abc', 'abc']"
44
45 b2 := []string{len: 2, init: '111'}
46 assert '${b2}' == "['111', '111']"
47}
48
49fn test_array_init_with_len_no_default() {
50 a1 := []int{len: 4}
51 assert '${a1}' == '[0, 0, 0, 0]'
52
53 a2 := []string{len: 4}
54 assert '${a2}' == "['', '', '', '']"
55
56 a3 := []bool{len: 3}
57 assert '${a3}' == '[false, false, false]'
58}
59
60fn test_array_int_full_options() {
61 println('Test array of int values')
62
63 a := []int{len: 2, cap: 10} // this creates an array with 2 items, initial capacity 10, default value of array type
64 println('array a: length: ${a.len}, capacity: ${a.cap}, content: ${a}')
65 assert a.len == 2
66 assert a.cap == 10
67 assert a.cap >= a.len
68 assert a.str() == '[0, 0]'
69
70 b := []int{len: 10, cap: 100, init: 1} // this creates an array with 10 one and initial capacity 100 elements, value given
71 _ = b.clone() // discard result variable, sample
72 println('array b: length: ${b.len}, capacity: ${b.cap}, content: ${b}')
73 assert b.len == 10
74 assert b.cap == 100
75 assert b.cap >= b.len
76 assert b.str() == '[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]'
77
78 mut c := []int{len: 2, cap: 10, init: 1} // this creates an array with 2 one and initial capacity 10 elements, value given
79 _ = c.clone() // discard result variable, sample
80 println('array c: length: ${c.len}, capacity: ${c.cap}, content: ${c}')
81 assert c.len == 2
82 assert c.cap == 10
83 assert c.cap >= c.len
84 assert c.str() == '[1, 1]'
85 // add some items to the array, to check limits
86 c << [3, 4, 5, 6, 7, 8, 9, 10] // add 8 items, from another array
87 // update one item, to have the right number in the sequence ...
88 c[1] = 2
89 println('array c: length: ${c.len}, capacity: ${c.cap}, content now: ${c}')
90 assert c.len == 10
91 assert c.cap == 10
92 assert c.cap >= c.len
93 assert c.str() == '[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]'
94 // add some more item after initial capacity, to ensure it's expandable
95 c << [11, 12, 13, 14, 15, 16]
96 println('array c: length: ${c.len}, capacity: ${c.cap}, content now: ${c}')
97 assert c.len == 16
98 assert c.cap >= c.len
99 assert c.str() == '[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]'
100}
101
102fn test_array_string_full_options() {
103 println('Test array of string values')
104
105 a := []string{len: 2, cap: 10} // this creates an array with 2 items, initial capacity 10, default value of array type
106 println('array a: length: ${a.len}, capacity: ${a.cap}') // ok
107 println('array a: length: ${a.len}, capacity: ${a.cap}, content: "${a}"')
108 assert a.len == 2
109 assert a.cap == 10
110 assert a.cap >= a.len
111 assert a.str() == "['', '']"
112
113 b := []string{len: 10, cap: 100, init: 'b'} // this creates an array with 10 'b', initial capacity 100 elements, value given
114 _ = b.clone() // discard result variable, sample
115 println('array b: length: ${b.len}, capacity: ${b.cap}') // ok
116 println('array b: length: ${b.len}, capacity: ${b.cap}, content: ${b}')
117 assert b.len == 10
118 assert b.cap == 100
119 assert b.cap >= b.len
120 assert b.str() == "['b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b']"
121
122 mut c := []string{len: 2, cap: 10, init: 'c'} // this creates an array with 2 'c' and initial capacity 10 elements, value given
123 _ = c.clone() // discard result variable, sample
124 println('array c: length: ${c.len}, capacity: ${c.cap}, content: ${c}')
125 assert c.len == 2
126 assert c.cap == 10
127 assert c.cap >= c.len
128 assert c.str() == "['c', 'c']"
129 // add some items to the array, to check limits
130 c << ['c', 'c', 'c', 'c', 'c', 'c', 'c', 'c'] // add 8 items, from another array
131 // update some items, to have the right number in the sequence ...
132 c[0] = 'a'
133 c[1] = 'b'
134 println('array c: length: ${c.len}, capacity: ${c.cap}, content now: ${c}')
135 assert c.len == 10
136 assert c.cap == 10
137 assert c.cap >= c.len
138 assert c.str() == "['a', 'b', 'c', 'c', 'c', 'c', 'c', 'c', 'c', 'c']"
139 // add some more item after initial capacity, to ensure all is good
140 c << ['11', '12', '13', '14', '15', '16']
141 // c << ['11', nil, '13', '14', '15', '16'] // check later if/how to handle this ...
142 println('array c: length: ${c.len}, capacity: ${c.cap}, content now: ${c}')
143 assert c.len == 16
144 assert c.cap >= c.len
145 assert c.str() == "['a', 'b', 'c', 'c', 'c', 'c', 'c', 'c', 'c', 'c', '11', '12', '13', '14', '15', '16']"
146}
147
148struct MyStruct {
149pub mut:
150 ar []f32
151}
152
153fn test_array_init_in_struct_field() {
154 m := MyStruct{
155 ar: []f32{len: 4, init: 1.2}
156 }
157 println(m)
158 assert m.ar.str() == '[1.2, 1.2, 1.2, 1.2]'
159}
160
161struct Aaa {
162pub mut:
163 a []int
164}
165
166struct IndexedDefaultArrayField {
167 field []int = []int{len: 1, init: index}
168}
169
170fn test_array_init_cast_type_in_struct_field() {
171 size := u32(5)
172 st := &Aaa{
173 a: []int{len: int(size)}
174 }
175 println(st)
176 assert st.a.str() == '[0, 0, 0, 0, 0]'
177}
178
179fn test_array_of_struct_with_indexed_array_field_default() {
180 st := []IndexedDefaultArrayField{len: 1}
181 assert st.len == 1
182 assert st[0].field == [0]
183}
184
185fn test_multi_dimensional_array_init() {
186 a := [][]int{len: 2, init: []int{len: 4, init: 2}}
187 assert '${a}' == '[[2, 2, 2, 2], [2, 2, 2, 2]]'
188
189 b := [][]string{len: 3, init: []string{len: 2, init: 'abc'}}
190 assert '${b}' == "[['abc', 'abc'], ['abc', 'abc'], ['abc', 'abc']]"
191
192 c := [][]f64{len: 2, init: []f64{len: 2, init: 2.2}}
193 assert '${c}' == '[[2.2, 2.2], [2.2, 2.2]]'
194
195 d := [][]int{len: 3, init: []int{len: 2}}
196 assert '${d}' == '[[0, 0], [0, 0], [0, 0]]'
197
198 e := [][]string{len: 2, init: []string{len: 3}}
199 assert '${e}' == "[['', '', ''], ['', '', '']]"
200
201 f := [][]int{len: 3}
202 assert '${f}' == '[[], [], []]'
203}
204
205fn test_array_init_direct_call() {
206 assert []int{len: 2, init: 0}.len == 2
207 assert []int{len: 3, init: 1}.map(it * 2) == [2, 2, 2]
208}
209
210// test array init with sumtype
211type Alphabet = Abc | Xyz
212
213struct Abc {
214 val int
215}
216
217struct Xyz {
218 val int
219}
220
221fn test_array_init_with_sumtype() {
222 a := [Alphabet(Abc{1}), Xyz{2}]
223 a0 := a[0]
224 a1 := a[1]
225 match a0 {
226 Abc {
227 assert a0.val == 1
228 }
229 else {
230 assert false
231 }
232 }
233
234 match a1 {
235 Xyz {
236 assert a1.val == 2
237 }
238 else {
239 assert false
240 }
241 }
242}
243
244fn test_array_init_inferred_from_option() {
245 a := read() or { [] }
246 x := 1
247 b := read() or {
248 match x {
249 1 {
250 ['1']
251 }
252 else {
253 ['2']
254 }
255 }
256 }
257 println(a)
258 println(b)
259}
260
261fn read() ![]string {
262 return error('failed')
263}
264
265fn test_multi_array_update_data() {
266 mut a := [][][]int{len: 2, init: [][]int{len: 3, init: []int{len: 2}}}
267 a[0][1][1] = 2
268 println(a)
269 assert '${a}' == '[[[0, 0], [0, 2], [0, 0]], [[0, 0], [0, 0], [0, 0]]]'
270}
271
272fn test_array_of_map_with_len_no_default() {
273 mut arr := []map[int]int{len: 3}
274 arr[0][0] = 0
275 arr[1][1] = 1
276 arr[2][2] = 2
277 println(arr)
278 assert arr == [{
279 0: 0
280 }, {
281 1: 1
282 }, {
283 2: 2
284 }]
285}
286
287fn empty_array_from_generic_typ[T]() []T {
288 return []T.typ{}
289}
290
291fn test_array_init_from_typeof() {
292 fixed := [1, 2, 3]!
293 dyn := []typeof(fixed[0]){}
294 assert typeof(dyn).name == '[]int'
295 assert dyn.len == 0
296}
297
298fn test_array_init_from_generic_typ() {
299 ints := empty_array_from_generic_typ[int]()
300 assert typeof(ints).name == '[]int'
301 assert ints.len == 0
302}
303
304fn test_nested_array_init_in_if_expr_with_index() {
305 map_test := [][][]int{len: 10, init: if index == 0 || index == 9 {
306 [][]int{len: 10, init: [1]}
307 } else {
308 [][]int{len: 10, init: if index == 0 || index == 9 { [1] } else { [] }}
309 }}
310
311 assert map_test.len == 10
312 assert map_test[0].len == 10
313 assert map_test[0][0] == [1]
314 assert map_test[0][9] == [1]
315 assert map_test[1].len == 10
316 assert map_test[1][0] == [1]
317 assert map_test[1][1] == []int{}
318 assert map_test[1][9] == [1]
319 assert map_test[9][5] == [1]
320}
321