v / vlib / builtin
Raw file | 1624 loc (1445 sloc) | 29.45 KB | Latest commit hash ef5be22f8
1fn test_pointer() {
2 mut arr := []&int{}
3 a := 1
4 b := 2
5 c := 3
6 arr << &a
7 arr << &b
8 arr << &c
9 assert *arr[0] == 1
10 arr[1] = &c
11 assert *arr[1] == 3
12 mut d_arr := [arr] // [][]&int
13 d_arr << arr
14 assert *d_arr[0][1] == 3
15 println(*d_arr[0][1])
16 assert *d_arr[1][0] == 1
17}
18
19fn test_assign() {
20 mut arr := [2, 4, 8, 16, 32, 64, 128]
21 arr[0] = 2
22 arr[1] &= 255
23 arr[2] |= 255
24 arr[3] <<= 4
25 arr[4] >>= 4
26 arr[5] %= 5
27 arr[6] ^= 3
28 assert arr[0] == 2
29 assert arr[1] == 4 & 255
30 assert arr[2] == 8 | 255
31 assert arr[3] == 16 << 4
32 assert arr[4] == 32 >> 4
33 assert arr[5] == 64 % 5
34 assert arr[6] == 128 ^ 3
35}
36
37fn test_ints() {
38 mut a := [1, 5, 2, 3]
39 assert a.len == 4
40 assert a[0] == 1
41 assert a[2] == 2
42 assert a.last() == 3
43 a << 4
44 assert a.len == 5
45 assert a[4] == 4
46 assert a.last() == 4
47 s := a.str()
48 assert s == '[1, 5, 2, 3, 4]'
49 assert a[1] == 5
50 assert a.last() == 4
51}
52
53fn test_deleting() {
54 mut a := [1, 5, 2, 3, 4]
55 assert a.len == 5
56 assert a.str() == '[1, 5, 2, 3, 4]'
57 a.delete(0)
58 assert a.str() == '[5, 2, 3, 4]'
59 assert a.len == 4
60 a.delete(1)
61 assert a.str() == '[5, 3, 4]'
62 assert a.len == 3
63 a.delete(a.len - 1)
64 assert a.str() == '[5, 3]'
65 assert a.len == 2
66}
67
68fn test_slice_delete() {
69 mut a := [1.5, 2.5, 3.25, 4.5, 5.75]
70 b := unsafe { a[2..4] }
71 a.delete(0)
72 assert a == [2.5, 3.25, 4.5, 5.75]
73 assert b == [3.25, 4.5]
74 a = [3.75, 4.25, -1.5, 2.25, 6.0]
75 c := unsafe { a[..3] }
76 a.delete(2)
77 assert a == [3.75, 4.25, 2.25, 6.0]
78 assert c == [3.75, 4.25, -1.5]
79}
80
81fn test_delete_many() {
82 mut a := [1, 2, 3, 4, 5, 6, 7, 8, 9]
83 b := unsafe { a[2..6] }
84 a.delete_many(4, 3)
85 assert a == [1, 2, 3, 4, 8, 9]
86 assert b == [3, 4, 5, 6]
87 c := unsafe { a[..a.len] }
88 a.delete_many(2, 0) // this should just clone
89 a[1] = 17
90 assert a == [1, 17, 3, 4, 8, 9]
91 assert c == [1, 2, 3, 4, 8, 9]
92 a.delete_many(0, a.len)
93 assert a == []int{}
94}
95
96fn test_short() {
97 a := [1, 2, 3]
98 assert a.len == 3
99 assert a.cap == 3
100 assert a[0] == 1
101 assert a[1] == 2
102 assert a[2] == 3
103}
104
105fn test_large() {
106 mut a := [0].repeat(0)
107 for i in 0 .. 10000 {
108 a << i
109 }
110 assert a.len == 10000
111 assert a[234] == 234
112}
113
114struct Chunk {
115 val string
116}
117
118struct Kkk {
119 q []Chunk
120}
121
122fn test_empty() {
123 mut chunks := []Chunk{}
124 a := Chunk{}
125 assert chunks.len == 0
126 chunks << a
127 assert chunks.len == 1
128 chunks = []
129 assert chunks.len == 0
130 chunks << a
131 assert chunks.len == 1
132}
133
134fn test_push() {
135 mut a := []int{}
136 a << 1
137 a << 3
138 assert a[1] == 3
139 assert a.str() == '[1, 3]'
140}
141
142fn test_insert() {
143 mut a := [1, 2]
144 a.insert(0, 3)
145 assert a[0] == 3
146 assert a[2] == 2
147 assert a.len == 3
148 a.insert(1, 4)
149 assert a[1] == 4
150 assert a[2] == 1
151 assert a.len == 4
152 a.insert(4, 5)
153 assert a[4] == 5
154 assert a[3] == 2
155 assert a.len == 5
156 mut b := []f64{}
157 assert b.len == 0
158 b.insert(0, f64(1.1))
159 assert b.len == 1
160 assert b[0] == f64(1.1)
161}
162
163fn test_insert_many() {
164 mut a := [3, 4]
165 a.insert(0, [1, 2])
166 assert a == [1, 2, 3, 4]
167 b := [5, 6]
168 a.insert(1, b)
169 assert a == [1, 5, 6, 2, 3, 4]
170}
171
172fn test_prepend() {
173 mut a := []int{}
174 assert a.len == 0
175 a.prepend(1)
176 assert a.len == 1
177 assert a[0] == 1
178 mut b := []f64{}
179 assert b.len == 0
180 b.prepend(f64(1.1))
181 assert b.len == 1
182 assert b[0] == f64(1.1)
183}
184
185fn test_prepend_many() {
186 mut a := [3, 4]
187 a.prepend([1, 2])
188 assert a == [1, 2, 3, 4]
189 b := [5, 6]
190 a.prepend(b)
191 assert a == [5, 6, 1, 2, 3, 4]
192}
193
194fn test_strings() {
195 a := ['a', 'b', 'c']
196 assert a.str() == "['a', 'b', 'c']"
197}
198
199/*
200fn test_compare_ints() {
201 assert compare_ints(1, 2) == -1
202 assert compare_ints(2, 1) == 1
203 assert compare_ints(0, 0) == 0
204
205 a := 1
206 b := 2
207 assert compare_ints(a, b) == -1
208 assert compare_ints(b, a) == 1
209 assert compare_ints(a, a) == 0
210}
211*/
212
213fn test_repeat_int() {
214 a := [1234].repeat(5)
215 // dump(a)
216 assert a.len == 5
217 for x in a {
218 assert x == 1234
219 }
220}
221
222fn test_repeat_f64() {
223 a := [1.1].repeat(10)
224 // dump(a)
225 assert a.len == 10
226 assert a[0] == 1.1
227 assert a[5] == 1.1
228 assert a[9] == 1.1
229}
230
231fn test_repeat_f32() {
232 a := [f32(1.1)].repeat(10)
233 // dump(a)
234 assert a.len == 10
235 assert a[0] == f32(1.1)
236 assert a[5] == f32(1.1)
237 assert a[9] == f32(1.1)
238}
239
240fn test_repeat_i64() {
241 a := [i64(-123)].repeat(10)
242 // dump(a)
243 assert a.len == 10
244 assert a[0] == -123
245 assert a[5] == -123
246 assert a[9] == -123
247}
248
249fn test_repeat_u64() {
250 a := [u64(123)].repeat(10)
251 assert a[0] == 123
252 assert a[5] == 123
253 assert a[9] == 123
254}
255
256fn test_repeat_several_ints() {
257 a := [1, 2].repeat(2)
258 // dump(a)
259 assert a.len == 4
260 assert a[0] == 1
261 assert a[1] == 2
262 assert a[2] == 1
263 assert a[3] == 2
264}
265
266fn test_repeat_several_strings_2() {
267 a := ['1', 'abc'].repeat(2)
268 // dump(a)
269 assert a.len == 4
270 assert a[0] == '1'
271 assert a[1] == 'abc'
272 assert a[2] == '1'
273 assert a[3] == 'abc'
274}
275
276fn test_repeat_several_strings_0() {
277 mut a := ['1', 'abc'].repeat(0)
278 // dump(a)
279 assert a.len == 0
280 a << 'abc'
281 assert a[0] == 'abc'
282}
283
284fn test_deep_repeat() {
285 mut a3 := [[[1, 1], [2, 2], [3, 3]], [[4, 4], [5, 5], [6, 6]]]
286 r := a3.repeat(3)
287 // dump(r)
288 a3[1][1][0] = 17
289 assert r == [
290 [[1, 1], [2, 2], [3, 3]],
291 [[4, 4], [5, 5], [6, 6]],
292 [[1, 1], [2, 2], [3, 3]],
293 [[4, 4], [5, 5], [6, 6]],
294 [[1, 1], [2, 2], [3, 3]],
295 [[4, 4], [5, 5], [6, 6]],
296 ]
297 assert a3 == [[[1, 1], [2, 2], [3, 3]], [[4, 4], [17, 5], [6, 6]]]
298}
299
300fn test_right() {
301 a := [1, 2, 3, 4]
302 c := a[1..a.len]
303 d := a[1..]
304 assert c[0] == 2
305 assert c[1] == 3
306 assert d[0] == 2
307 assert d[1] == 3
308}
309
310fn test_left() {
311 a := [1, 2, 3]
312 c := a[0..2]
313 d := a[..2]
314 assert c[0] == 1
315 assert c[1] == 2
316 assert d[0] == 1
317 assert d[1] == 2
318}
319
320fn test_slice() {
321 a := [1, 2, 3, 4]
322 b := a[2..4]
323 assert b.len == 2
324 assert a[1..2].len == 1
325 assert a.len == 4
326}
327
328fn test_push_many() {
329 mut a := [1, 2, 3]
330 b := [4, 5, 6]
331 a << b
332 assert a.len == 6
333 assert a[0] == 1
334 assert a[3] == 4
335 assert a[5] == 6
336}
337
338fn test_reverse() {
339 a := [1, 2, 3, 4]
340 b := ['test', 'array', 'reverse']
341 c := a.reverse()
342 println(c)
343 d := b.reverse()
344 for i, _ in c {
345 assert c[i] == a[a.len - i - 1]
346 }
347 for i, _ in d {
348 assert d[i] == b[b.len - i - 1]
349 }
350 e := []int{}
351 f := e.reverse()
352 assert f.len == 0
353}
354
355const (
356 c_n = 5
357)
358
359struct Foooj {
360 a [5]int // c_n
361}
362
363fn test_fixed() {
364 mut nums := [4]int{}
365 // x := nums[1..3]
366 // assert x.len == 2
367 assert nums[0] == 0
368 assert nums[1] == 0
369 assert nums[2] == 0
370 assert nums[3] == 0
371 nums[1] = 7
372 assert nums[1] == 7
373 nums2 := [5]int{} // c_n
374 assert nums2[c_n - 1] == 0
375}
376
377fn modify(mut numbers []int) {
378 numbers[0] = 777
379}
380
381fn test_mut_slice() {
382 mut n := [1, 2, 3]
383 // modify(mut n)
384 modify(mut n[..2])
385 assert n[0] == 777
386 modify(mut n[2..])
387 assert n[2] == 777
388 println(n)
389}
390
391fn double_up(mut a []int) {
392 for i := 0; i < a.len; i++ {
393 a[i] = a[i] * 2
394 }
395}
396
397fn double_up_v2(mut a []int) {
398 for i, _ in a {
399 a[i] = a[i] * 2 // or val*2, doesn't matter
400 }
401}
402
403fn test_mut_arg() {
404 mut arr := [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
405 double_up(mut arr)
406 assert arr.str() == '[2, 4, 6, 8, 10, 12, 14, 16, 18, 20]'
407 arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
408 double_up_v2(mut arr)
409 assert arr.str() == '[2, 4, 6, 8, 10, 12, 14, 16, 18, 20]'
410}
411
412fn test_clone() {
413 nums := [1, 2, 3, 4, 100]
414 _ = nums
415 nums2 := nums.clone()
416 assert nums2.len == 5
417 assert nums.str() == '[1, 2, 3, 4, 100]'
418 assert nums2.str() == '[1, 2, 3, 4, 100]'
419 assert nums[1..3].str() == '[2, 3]'
420}
421
422/*
423fn test_copy() {
424 a := [1, 2, 3]
425 b := a
426 assert b[0] == 1
427 assert b[1] == 2
428 assert b[2] == 3
429}
430*/
431fn test_multi_array_clone() {
432 // 2d array_int
433 mut a2_1 := [[1, 2, 3], [4, 5, 6]]
434 mut a2_2 := a2_1.clone()
435 a2_1[0][1] = 0
436 a2_2[1][0] = 0
437 assert a2_1 == [[1, 0, 3], [4, 5, 6]]
438 assert a2_2 == [[1, 2, 3], [0, 5, 6]]
439 // 2d array_string
440 mut b2_1 := [['1', '2', '3'], ['4', '5', '6']]
441 mut b2_2 := b2_1.clone()
442 b2_1[0][1] = '0'
443 b2_2[1][0] = '0'
444 assert b2_1 == [['1', '0', '3'], ['4', '5', '6']]
445 assert b2_2 == [['1', '2', '3'], ['0', '5', '6']]
446 // 3d array_int
447 mut a3_1 := [[[1, 1], [2, 2], [3, 3]], [[4, 4], [5, 5], [6, 6]]]
448 mut a3_2 := a3_1.clone()
449 a3_1[0][0][1] = 0
450 a3_2[0][1][0] = 0
451 assert a3_1 == [[[1, 0], [2, 2], [3, 3]], [[4, 4], [5, 5],
452 [6, 6]]]
453 assert a3_2 == [[[1, 1], [0, 2], [3, 3]], [[4, 4], [5, 5],
454 [6, 6]]]
455 // 3d array_string
456 mut b3_1 := [[['1', '1'], ['2', '2'], ['3', '3']], [['4', '4'],
457 ['5', '5'], ['6', '6']]]
458 mut b3_2 := b3_1.clone()
459 b3_1[0][0][1] = '0'
460 b3_2[0][1][0] = '0'
461 assert b3_1 == [[['1', '0'], ['2', '2'], ['3', '3']], [['4', '4'],
462 ['5', '5'], ['6', '6']]]
463 assert b3_2 == [[['1', '1'], ['0', '2'], ['3', '3']], [['4', '4'],
464 ['5', '5'], ['6', '6']]]
465}
466
467fn test_doubling() {
468 mut nums := [1, 2, 3, 4, 5]
469 for i in 0 .. nums.len {
470 nums[i] *= 2
471 }
472 println(nums.str())
473 assert nums.str() == '[2, 4, 6, 8, 10]'
474}
475
476struct Test2 {
477 one int
478 two int
479}
480
481struct Test {
482 a string
483mut:
484 b []Test2
485}
486
487// TODO: default array/struct str methods
488fn (ta []Test2) str() string {
489 mut s := '['
490 for i, t in ta {
491 s += t.str()
492 if i < ta.len - 1 {
493 s += ', '
494 }
495 }
496 s += ']'
497 return s
498}
499
500fn (t Test2) str() string {
501 return '{${t.one} ${t.two}}'
502}
503
504fn (t Test) str() string {
505 return '{${t.a} ${t.b}}'
506}
507
508fn test_struct_print() {
509 mut a := Test{
510 a: 'Test'
511 b: []
512 }
513 b := Test2{
514 one: 1
515 two: 2
516 }
517 a.b << b
518 a.b << b
519 assert a.str() == '{Test [{1 2}, {1 2}]}'
520 assert b.str() == '{1 2}'
521 assert a.b.str() == '[{1 2}, {1 2}]'
522}
523
524fn test_single_element() {
525 mut a := [1]
526 a << 2
527 assert a.len == 2
528 assert a[0] == 1
529 assert a[1] == 2
530 println(a)
531}
532
533fn test_find_index() {
534 // string
535 a := ['v', 'is', 'great']
536 assert a.index('v') == 0
537 assert a.index('is') == 1
538 assert a.index('gre') == -1
539 // int
540 b := [1, 2, 3, 4]
541 assert b.index(1) == 0
542 assert b.index(4) == 3
543 assert b.index(5) == -1
544 // byte
545 c := [0x22, 0x33, 0x55]
546 assert c.index(0x22) == 0
547 assert c.index(0x55) == 2
548 assert c.index(0x99) == -1
549 // char
550 d := [`a`, `b`, `c`]
551 assert d.index(`b`) == 1
552 assert d.index(`c`) == 2
553 assert d.index(`u`) == -1
554}
555
556fn test_multi() {
557 a := [[1, 2, 3], [4, 5, 6]]
558 assert a.len == 2
559 assert a[0].len == 3
560 assert a[0][0] == 1
561 assert a[0][2] == 3
562 assert a[1][2] == 6
563 // TODO
564 // b := [ [[1,2,3],[4,5,6]], [[1,2]] ]
565 // assert b[0][0][0] == 1
566}
567
568fn test_in() {
569 a := [1, 2, 3]
570 assert 1 in a
571 assert 2 in a
572 assert 3 in a
573 assert 4 !in a
574 assert 0 !in a
575 assert 0 !in a
576 assert 4 !in a
577 b := [1, 4, 0]
578 c := [3, 6, 2, 0]
579 assert 0 in b
580 assert 0 in c
581}
582
583fn sum(prev int, curr int) int {
584 return prev + curr
585}
586
587fn sub(prev int, curr int) int {
588 return prev - curr
589}
590
591fn filter_test_helper_1(a int) bool {
592 return a > 3
593}
594
595fn test_filter() {
596 a := [1, 2, 3, 4, 5, 6]
597 b := a.filter(it % 2 == 0)
598 assert b.len == 3
599 assert b[0] == 2
600 assert b[1] == 4
601 assert b[2] == 6
602 c := ['v', 'is', 'awesome']
603 d := c.filter(it.len > 1)
604 assert d[0] == 'is'
605 assert d[1] == 'awesome'
606 ////////
607 arr := [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
608 println(arr.filter(it % 2 == 0 || it % 3 == 0))
609 assert true
610 assert [1, 2, 3].len == 3
611 mut mut_arr := [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
612 mut_arr = mut_arr.filter(it < 4)
613 assert mut_arr.len == 3
614 assert a.filter(filter_test_helper_1) == [4, 5, 6]
615 assert [1, 5, 10].filter(filter_test_helper_1) == [5, 10]
616 // TODO
617 // assert arr.filter(arr % 2).len == 5
618}
619
620fn test_anon_fn_filter() {
621 filter_num := fn (i int) bool {
622 return i % 2 == 0
623 }
624 assert [1, 2, 3, 4, 5].filter(filter_num) == [2, 4]
625}
626
627fn test_anon_fn_arg_filter() {
628 a := [1, 2, 3, 4].filter(fn (i int) bool {
629 return i % 2 == 0
630 })
631 assert a == [2, 4]
632}
633
634fn map_test_helper_1(i int) int {
635 return i * i
636}
637
638fn map_test_helper_2(i int, b string) int {
639 return i + b.len
640}
641
642fn map_test_helper_3(i int, b []string) int {
643 return i + b.map(it.len)[i % b.len]
644}
645
646fn test_map() {
647 nums := [1, 2, 3, 4, 5, 6]
648 strs := ['v', 'is', 'awesome']
649 // assert nums.map() == <error>
650 // assert nums.map(it, 'excessive') == <error>
651 // identity
652 assert nums.map(it) == [1, 2, 3, 4, 5, 6]
653 assert strs.map(it) == ['v', 'is', 'awesome']
654 assert nums.map(it - it) == [0, 0, 0, 0, 0, 0]
655 assert nums.map(it - it)[0] == 0
656 // type switch
657 assert nums.map(it * 10) == [10, 20, 30, 40, 50, 60]
658 assert nums.map(it * it) == [1, 4, 9, 16, 25, 36]
659 assert nums.map('${it}') == ['1', '2', '3', '4', '5', '6']
660 assert nums.map(it % 2 == 0) == [false, true, false, true, false, true]
661 assert strs.map(it.to_upper()) == ['V', 'IS', 'AWESOME']
662 assert strs.map(it == 'awesome') == [false, false, true]
663 assert strs.map(it.len in nums) == [true, true, false]
664 assert strs.map(int(7)) == [7, 7, 7]
665 // external func
666 assert nums.map(map_test_helper_1(it)) == [1, 4, 9, 16, 25, 36]
667 assert nums.map(map_test_helper_2(it, 'bb')) == [3, 4, 5, 6, 7, 8]
668 assert nums.map(map_test_helper_3(it, strs)) == [3, 9, 4, 6, 12, 7]
669 // empty array as input
670 assert []int{len: 0}.map(it * 2) == []
671 // nested maps (where it is of same type)
672 assert nums.map(strs.map(int(7)) == [7, 7, 7]) == [true, true, true, true, true, true]
673 assert nums.map('${it}' + strs.map('a')[0]) == ['1a', '2a', '3a', '4a', '5a', '6a']
674 assert nums.map(it + strs.map(int(7))[0]) == [8, 9, 10, 11, 12, 13]
675 assert nums.map(it + strs.map(it.len)[0]) == [2, 3, 4, 5, 6, 7]
676 assert strs.map(it.len + strs.map(it.len)[0]) == [2, 3, 8]
677 // nested (different it types)
678 assert strs.map(it[nums.map(it - it)[0]]) == [u8(`v`), `i`, `a`]
679 assert nums[0..3].map('${it}' + strs.map(it)[it - 1]) == ['1v', '2is', '3awesome']
680 assert nums.map(map_test_helper_1) == [1, 4, 9, 16, 25, 36]
681 assert [1, 5, 10].map(map_test_helper_1) == [1, 25, 100]
682 assert nums == [1, 2, 3, 4, 5, 6]
683 assert strs == ['v', 'is', 'awesome']
684}
685
686fn test_anon_fn_map() {
687 add_num := fn (i int) int {
688 return i + 1
689 }
690 assert [1, 2, 3].map(add_num) == [2, 3, 4]
691}
692
693fn test_multi_anon_fn_map() {
694 a := [1, 2, 3].map(fn (i int) int {
695 return i + 1
696 })
697 b := [1, 2, 3].map(fn (i int) int {
698 return i + 2
699 })
700 assert a == [2, 3, 4]
701 assert b == [3, 4, 5]
702}
703
704fn test_anon_fn_arg_map() {
705 a := [1, 2, 3].map(fn (i int) int {
706 return i + 1
707 })
708 assert a == [2, 3, 4]
709}
710
711fn test_anon_fn_arg_different_type_map() {
712 i_to_str := fn (i int) string {
713 return i.str()
714 }
715 a := [1, 2, 3].map(i_to_str)
716 assert a == ['1', '2', '3']
717}
718
719fn test_anon_fn_inline_different_type_map() {
720 a := [1, 2, 3].map(fn (i int) string {
721 return i.str()
722 })
723 assert a == ['1', '2', '3']
724}
725
726fn test_array_str() {
727 numbers := [1, 2, 3]
728 assert numbers == [1, 2, 3]
729 numbers2 := [numbers, [4, 5, 6]] // dup str() bug
730 println(numbers2)
731 assert true
732 assert numbers.str() == '[1, 2, 3]'
733 // QTODO
734 // assert numbers2.str() == '[[1, 2, 3], [4, 5, 6]]'
735}
736
737struct User {
738 age int
739 name string
740}
741
742fn test_eq() {
743 assert [5, 6, 7] != [6, 7]
744 assert [`a`, `b`] == [`a`, `b`]
745 assert [User{
746 age: 22
747 name: 'bob'
748 }] == [User{
749 age: 22
750 name: 'bob'
751 }]
752 assert [{
753 'bob': 22
754 }, {
755 'tom': 33
756 }] == [{
757 'bob': 22
758 }, {
759 'tom': 33
760 }]
761 assert [[1, 2, 3], [4]] == [[1, 2, 3], [4]]
762}
763
764fn test_fixed_array_eq() {
765 a1 := [1, 2, 3]!
766 assert a1 == [1, 2, 3]!
767 assert a1 != [2, 3, 4]!
768
769 a2 := [[1, 2]!, [3, 4]!]!
770 assert a2 == [[1, 2]!, [3, 4]!]!
771 assert a2 != [[3, 4]!, [1, 2]!]!
772
773 a3 := [[1, 2], [3, 4]]!
774 assert a3 == [[1, 2], [3, 4]]!
775 assert a3 != [[1, 1], [2, 2]]!
776
777 a4 := [[`a`, `b`], [`c`, `d`]]!
778 assert a4 == [[`a`, `b`], [`c`, `d`]]!
779 assert a4 != [[`c`, `a`], [`a`, `b`]]!
780
781 a5 := [['aaa', 'bbb'], ['ccc', 'ddd']]!
782 assert a5 == [['aaa', 'bbb'], ['ccc', 'ddd']]!
783 assert a5 != [['abc', 'def'], ['ccc', 'ddd']]!
784
785 a6 := [['aaa', 'bbb']!, ['ccc', 'ddd']!]!
786 assert a6 == [['aaa', 'bbb']!, ['ccc', 'ddd']!]!
787 assert a6 != [['aaa', 'bbb']!, ['aaa', 'ddd']!]!
788
789 a7 := [[1, 2]!, [3, 4]!]
790 assert a7 == [[1, 2]!, [3, 4]!]
791 assert a7 != [[2, 3]!, [1, 2]!]
792
793 a8 := [['aaa', 'bbb']!, ['ccc', 'ddd']!]
794 assert a8 == [['aaa', 'bbb']!, ['ccc', 'ddd']!]
795 assert a8 != [['bbb', 'aaa']!, ['cccc', 'dddd']!]
796}
797
798fn test_fixed_array_literal_eq() {
799 assert [1, 2, 3]! == [1, 2, 3]!
800 assert [1, 1, 1]! != [1, 2, 3]!
801
802 assert [[1, 2], [3, 4]]! == [[1, 2], [3, 4]]!
803 assert [[1, 1], [2, 2]]! != [[1, 2], [3, 4]]!
804
805 assert [[1, 1]!, [2, 2]!]! == [[1, 1]!, [2, 2]!]!
806 assert [[1, 1]!, [2, 2]!]! != [[1, 2]!, [2, 3]!]!
807
808 assert [[1, 1]!, [2, 2]!] == [[1, 1]!, [2, 2]!]
809 assert [[1, 1]!, [2, 2]!] != [[1, 2]!, [2, 3]!]
810}
811
812fn test_sort() {
813 mut a := ['hi', '1', '5', '3']
814 a.sort()
815 assert a == ['1', '3', '5', 'hi']
816
817 mut nums := [67, -3, 108, 42, 7]
818 nums.sort()
819 assert nums == [-3, 7, 42, 67, 108]
820
821 nums.sort(a < b)
822 assert nums == [-3, 7, 42, 67, 108]
823
824 nums.sort(b < a)
825 assert nums == [108, 67, 42, 7, -3]
826
827 mut users := [User{22, 'Peter'}, User{20, 'Bob'}, User{25, 'Alice'}]
828 users.sort(a.age < b.age)
829 assert users[0].age == 20
830 assert users[1].age == 22
831 assert users[2].age == 25
832 assert users[0].name == 'Bob'
833 assert users[1].name == 'Peter'
834 assert users[2].name == 'Alice'
835
836 users.sort(a.age > b.age)
837 assert users[0].age == 25
838 assert users[1].age == 22
839 assert users[2].age == 20
840
841 users.sort(b.age > a.age)
842 assert users[0].age == 20
843 assert users[1].age == 22
844 assert users[2].age == 25
845
846 users.sort(a.name < b.name)
847 assert users[0].name == 'Alice'
848 assert users[1].name == 'Bob'
849 assert users[2].name == 'Peter'
850}
851
852fn test_sort_with_compare() {
853 mut a := ['hi', '1', '5', '3']
854 a.sort_with_compare(fn (a &string, b &string) int {
855 if a < b {
856 return -1
857 }
858 if a > b {
859 return 1
860 }
861 return 0
862 })
863 assert a == ['1', '3', '5', 'hi']
864}
865
866fn test_rune_sort() {
867 mut bs := [`f`, `e`, `d`, `b`, `c`, `a`]
868 bs.sort()
869 println(bs)
870 assert bs == [`a`, `b`, `c`, `d`, `e`, `f`]
871
872 bs.sort(a > b)
873 println(bs)
874 assert bs == [`f`, `e`, `d`, `c`, `b`, `a`]
875
876 bs.sort(a < b)
877 println(bs)
878 assert bs == [`a`, `b`, `c`, `d`, `e`, `f`]
879}
880
881fn test_sort_by_different_order_of_a_b() {
882 mut x := [1, 2, 3]
883 x.sort(a < b)
884 println(x)
885 assert x == [1, 2, 3]
886
887 mut y := [1, 2, 3]
888 y.sort(b < a)
889 println(y)
890 assert y == [3, 2, 1]
891}
892
893fn test_f32_sort() {
894 mut f := [f32(50.0), 15, 1, 79, 38, 0, 27]
895 f.sort()
896 assert f == [f32(0.0), 1, 15, 27, 38, 50, 79]
897
898 f.sort(a < b)
899 assert f == [f32(0.0), 1, 15, 27, 38, 50, 79]
900
901 f.sort(b > a)
902 assert f == [f32(0.0), 1, 15, 27, 38, 50, 79]
903
904 f.sort(b < a)
905 assert f == [f32(79.0), 50, 38, 27, 15, 1, 0]
906
907 f.sort(a > b)
908 assert f == [f32(79.0), 50, 38, 27, 15, 1, 0]
909}
910
911fn test_f64_sort() {
912 mut f := [50.0, 15, 1, 79, 38, 0, 27]
913 f.sort()
914 assert f[0] == 0.0
915 assert f[1] == 1.0
916 assert f[6] == 79.0
917}
918
919fn test_i64_sort() {
920 mut f := [i64(50), 15, 1, 79, 38, 0, 27]
921 f.sort()
922 assert f[0] == 0
923 assert f[1] == 1
924 assert f[6] == 79
925}
926
927fn test_sort_index_expr() {
928 mut f := [[i64(50), 48], [i64(15)], [i64(1)], [i64(79)], [i64(38)],
929 [i64(0)], [i64(27)]]
930 // TODO This currently gives "indexing pointer" error without unsafe
931 unsafe {
932 f.sort(a[0] < b[0])
933 }
934 assert f == [[i64(0)], [i64(1)], [i64(15)], [i64(27)], [i64(38)],
935 [i64(50), 48], [i64(79)]]
936}
937
938fn test_a_b_paras_sort() {
939 mut arr_i := [1, 3, 2]
940 arr_i.sort(a < b)
941 println(arr_i)
942 assert arr_i == [1, 2, 3]
943 arr_i.sort(b < a)
944 println(arr_i)
945 assert arr_i == [3, 2, 1]
946
947 mut arr_f := [1.1, 3.3, 2.2]
948 arr_f.sort(a < b)
949 println(arr_f)
950 assert arr_f == [1.1, 2.2, 3.3]
951 arr_f.sort(b < a)
952 println(arr_f)
953 assert arr_f == [3.3, 2.2, 1.1]
954}
955
956/*
957fn test_for_last() {
958 numbers := [1, 2, 3, 4]
959 mut s := '['
960 for num in numbers {
961 s += '$num'
962 if !last {
963 s += ', '
964
965 }
966 }
967 s += ']'
968 assert s == '[1, 2, 3, 4]'
969}
970*/
971struct Foo {
972mut:
973 bar []int
974}
975
976fn test_in_struct() {
977 mut baz := Foo{
978 bar: [0, 0, 0]
979 }
980 baz.bar[0] += 2
981 baz.bar[0]++
982 assert baz.bar[0] == 3
983}
984
985[direct_array_access]
986fn test_direct_modification() {
987 mut foo := [2, 0, 5]
988 foo[1] = 3
989 foo[0] *= 7
990 foo[1]--
991 foo[2] -= 2
992 assert foo[0] == 14
993 assert foo[1] == 2
994 assert foo[2] == 3
995}
996
997fn test_bools() {
998 println('test b')
999 mut a := [true, false]
1000 a << true
1001 println(a)
1002}
1003
1004fn test_push_many_self() {
1005 mut actual_arr := [1, 2, 3, 4]
1006 actual_arr << actual_arr
1007 expected_arr := [1, 2, 3, 4, 1, 2, 3, 4]
1008 assert actual_arr.len == expected_arr.len
1009 for i in 0 .. actual_arr.len {
1010 assert actual_arr[i] == expected_arr[i]
1011 }
1012}
1013
1014fn test_for() {
1015 nums := [1, 2, 3]
1016 mut sum := 0
1017 for num in nums {
1018 sum += num
1019 }
1020 assert sum == 6
1021}
1022
1023fn test_clear() {
1024 mut arr := [1, 2, 3]
1025 assert arr.len == 3
1026 arr.clear()
1027 assert arr.len == 0
1028 arr << 3
1029 arr << 2
1030 arr << 1
1031 arr << 0
1032 assert arr.len == 4
1033 assert arr[0] == 3
1034 assert arr[1] == 2
1035 assert arr[2] == 1
1036 assert arr[3] == 0
1037 arr.clear()
1038 assert arr.len == 0
1039}
1040
1041fn test_trim() {
1042 mut arr := [1, 2, 3, 4, 5, 6, 7, 8, 9]
1043 assert arr.len == 9
1044 arr.trim(9)
1045 assert arr.len == 9
1046 assert arr.last() == 9
1047 arr.trim(7)
1048 assert arr.len == 7
1049 assert arr.last() == 7
1050 arr.trim(2)
1051 assert arr.len == 2
1052 assert arr.last() == 2
1053}
1054
1055[manualfree]
1056fn test_drop() {
1057 mut a := [1, 2]
1058 a << 3 // pushing assures reallocation; a.cap now should be bigger:
1059 assert a.cap > 3
1060 // eprintln('>>> a.cap: $a.cap | a.len: $a.len')
1061
1062 a.drop(-1000)
1063 assert a == [1, 2, 3] // a.drop( negative ) should NOT modify the array
1064 // eprintln('>>> a.cap: $a.cap | a.len: $a.len')
1065
1066 a.drop(2)
1067 assert a == [3]
1068 assert a.cap > a.len
1069 // eprintln('>>> a.cap: $a.cap | a.len: $a.len')
1070
1071 a.drop(10)
1072 assert a == []
1073 assert a.cap > a.len
1074 // eprintln('>>> a.cap: $a.cap | a.len: $a.len')
1075
1076 a << 123
1077 a << 456
1078 a << 789
1079 // eprintln('>>> a.cap: $a.cap | a.len: $a.len')
1080 assert a == [123, 456, 789]
1081
1082 a.drop(10)
1083 assert a == []
1084 // eprintln('>>> a.cap: $a.cap | a.len: $a.len')
1085
1086 unsafe { a.free() } // test offset OK
1087}
1088
1089fn test_hex() {
1090 // array hex
1091 st := [u8(`V`), `L`, `A`, `N`, `G`]
1092 assert st.hex() == '564c414e47'
1093 assert st.hex().len == 10
1094 st1 := [u8(0x41)].repeat(100)
1095 assert st1.hex() == '41'.repeat(100)
1096}
1097
1098fn test_left_shift_precendence() {
1099 mut arr := []int{}
1100 arr << 1 + 1
1101 arr << 1 - 1
1102 arr << 2 / 1
1103 arr << 2 * 1
1104 assert arr[0] == 2
1105 assert arr[1] == 0
1106 assert arr[2] == 2
1107 assert arr[3] == 2
1108}
1109
1110fn test_array_with_cap() {
1111 a4 := []int{len: 1, cap: 10}
1112 assert a4.len == 1
1113 assert a4.cap == 10
1114 a5 := []int{len: 1, cap: 10}
1115 assert a5.len == 1
1116 assert a5.cap == 10
1117}
1118
1119fn test_multi_array_index() {
1120 mut a := [][]int{len: 2, init: []int{len: 3, init: 0}}
1121 a[0][0] = 1
1122 assert '${a}' == '[[1, 0, 0], [0, 0, 0]]'
1123 mut b := [[0].repeat(3)].repeat(2)
1124 b[0][0] = 1
1125 assert '${b}' == '[[1, 0, 0], [0, 0, 0]]'
1126}
1127
1128fn test_plus_assign_string() {
1129 mut a := ['']
1130 a[0] += 'abc'
1131 assert a == ['abc']
1132}
1133
1134fn mut_arr_with_eq_in_fn(mut a []int) {
1135 if a == [1, 2, 3, 4] {
1136 a[0] = 0
1137 }
1138 if [0, 2, 3, 4] == a {
1139 a[1] = 0
1140 }
1141 if !(a != [0, 0, 3, 4]) {
1142 a[2] = 0
1143 }
1144 if !([0, 0, 0, 4] != a) {
1145 a[3] = 0
1146 }
1147}
1148
1149fn test_mut_arr_with_eq_in_fn() {
1150 mut a := [1, 2, 3, 4]
1151 mut_arr_with_eq_in_fn(mut a)
1152 assert a == [0, 0, 0, 0]
1153}
1154
1155fn array_in_mut(mut a []int) {
1156 if 1 in a {
1157 a[0] = 2
1158 }
1159}
1160
1161fn test_array_in_mut() {
1162 mut a := [1, 2]
1163 array_in_mut(mut a)
1164 assert a == [2, 2]
1165}
1166
1167// test array delete in function with mut argument
1168fn delete_nums(mut arr []int) {
1169 arr.delete(0)
1170}
1171
1172fn test_array_delete_in_mut() {
1173 mut nums := [1, 2, 3]
1174 delete_nums(mut nums)
1175 assert nums == [2, 3]
1176}
1177
1178// test array add in function with mut argument
1179fn add_nums(mut arr []int) {
1180 arr << 4
1181}
1182
1183fn test_array_add_in_mut() {
1184 mut nums := [1, 2, 3]
1185 add_nums(mut nums)
1186 assert nums == [1, 2, 3, 4]
1187}
1188
1189fn test_reverse_in_place() {
1190 mut a := [1, 2, 3, 4]
1191 a.reverse_in_place()
1192 assert a == [4, 3, 2, 1]
1193 mut b := ['a', 'b', 'c']
1194 b.reverse_in_place()
1195 assert b == ['c', 'b', 'a']
1196 mut c := [[1, 2], [3, 4], [5, 6]]
1197 c.reverse_in_place()
1198 assert c == [[5, 6], [3, 4], [1, 2]]
1199}
1200
1201fn test_array_int_pop() {
1202 mut a := [1, 2, 3, 4, 5]
1203 assert a.len == 5
1204 x := a.last()
1205 y := a.pop()
1206 assert x == y
1207 assert a.len == 4
1208 z := a.pop()
1209 assert a.len == 3
1210 assert z == 4
1211 x1 := a.pop()
1212 x2 := a.pop()
1213 final := a.pop()
1214 assert final == 1
1215}
1216
1217fn test_array_string_pop() {
1218 mut a := ['abc', 'def', 'xyz']
1219 assert a.len == 3
1220 assert a.pop() == 'xyz'
1221 assert a.pop() == 'def'
1222 assert a.pop() == 'abc'
1223 assert a.len == 0
1224 assert a.cap == 3
1225}
1226
1227fn test_array_first() {
1228 a := [3]
1229 assert a.first() == 3
1230 b := [1, 2, 3, 4]
1231 assert b.first() == 1
1232 c := ['abc', 'def']
1233 assert c.first()[0] == `a`
1234 s := [Chunk{'a'}]
1235 assert s.first().val == 'a'
1236}
1237
1238fn test_array_last() {
1239 a := [3]
1240 assert a.last() == 3
1241 b := [1, 2, 3, 4]
1242 assert b.last() == 4
1243 c := ['abc', 'def']
1244 assert c.last()[0] == `d`
1245 s := [Chunk{'a'}]
1246 assert s.last().val == 'a'
1247}
1248
1249[direct_array_access]
1250fn test_direct_array_access() {
1251 mut a := [11, 22, 33, 44]
1252 assert a[0] == 11
1253 assert a[2] == 33
1254 x := a[0]
1255 a[0] = 21
1256 a[1] += 2
1257 a[2] = x + 3
1258 a[3] -= a[1]
1259 assert a == [21, 24, 14, 20]
1260}
1261
1262[direct_array_access]
1263fn test_direct_array_access_via_ptr() {
1264 mut b := [11, 22, 33, 44]
1265 unsafe {
1266 mut a := &b
1267 assert a[0] == 11
1268 assert a[2] == 33
1269 x := a[0]
1270 a[0] = 21
1271 a[1] += 2
1272 a[2] = x + 3
1273 a[3] -= a[1]
1274 assert a == [21, 24, 14, 20]
1275 }
1276}
1277
1278fn test_push_arr_string_free() {
1279 mut lines := ['hi']
1280 s := 'a' + 'b'
1281 lines << s
1282 // make sure the data in the array is valid after freeing the string
1283 unsafe { s.free() }
1284 //
1285 println(lines)
1286 assert lines.len == 2
1287 assert lines[0] == 'hi'
1288 assert lines[1] == 'ab'
1289}
1290
1291const (
1292 grid_size_1 = 2
1293 grid_size_2 = 3
1294 grid_size_3 = 4
1295 cell_value = 123
1296)
1297
1298fn test_multidimensional_array_initialization_with_consts() {
1299 mut data := [][][]int{len: grid_size_1, init: [][]int{len: grid_size_2, init: []int{len: grid_size_3, init: cell_value}}}
1300 assert data.len == grid_size_1
1301 assert data[0].len == grid_size_2
1302 assert data[0][0].len == grid_size_3
1303 assert data[0][0][0] == cell_value
1304 assert data[1][1][1] == cell_value
1305}
1306
1307fn test_byteptr_vbytes() {
1308 unsafe {
1309 bp := malloc(5)
1310 bp[0] = 1
1311 bp[1] = 2
1312 bp[2] = 3
1313 bp[3] = 4
1314 bp[4] = 255
1315 bytes := bp.vbytes(5)
1316 println(bytes)
1317 assert bytes.len == 5
1318 assert bytes[0] == 1
1319 assert bytes[1] == 2
1320 assert bytes[2] == 3
1321 assert bytes[3] == 4
1322 assert bytes[4] == 255
1323 }
1324}
1325
1326fn test_voidptr_vbytes() {
1327 unsafe {
1328 bp := malloc(3)
1329 bp[0] = 4
1330 bp[1] = 5
1331 bp[2] = 6
1332 bytes := voidptr(bp).vbytes(3)
1333 assert bytes.len == 3
1334 assert bytes[0] == 4
1335 assert bytes[1] == 5
1336 assert bytes[2] == 6
1337 println(bytes)
1338 }
1339}
1340
1341fn test_multi_array_prepend() {
1342 mut a := [][]int{}
1343 a.prepend([1, 2, 3])
1344 assert a == [[1, 2, 3]]
1345 mut b := [][]int{}
1346 b.prepend([[1, 2, 3]])
1347 assert b == [[1, 2, 3]]
1348}
1349
1350fn test_multi_array_insert() {
1351 mut a := [][]int{}
1352 a.insert(0, [1, 2, 3])
1353 assert a == [[1, 2, 3]]
1354 mut b := [][]int{}
1355 b.insert(0, [[1, 2, 3]])
1356 assert b == [[1, 2, 3]]
1357}
1358
1359fn test_multi_array_in() {
1360 a := [[1]]
1361 println([1] in a)
1362 assert [1] in a
1363}
1364
1365fn test_any_type_array_contains() {
1366 a := [true, false]
1367 assert a.contains(true)
1368 assert true in a
1369 assert a.contains(false)
1370 assert false in a
1371 b := [i64(2), 3, 4]
1372 assert b.contains(i64(3))
1373 assert 5 !in b
1374 c := [[1], [2]]
1375 assert c.contains([1])
1376 assert [2] in c
1377 assert [3] !in c
1378}
1379
1380struct Person {
1381 name string
1382 nums []int
1383 kv map[string]string
1384}
1385
1386fn test_struct_array_of_multi_type_in() {
1387 ivan := Person{
1388 name: 'ivan'
1389 nums: [1, 2, 3]
1390 kv: {
1391 'aaa': '111'
1392 }
1393 }
1394 people := [
1395 Person{
1396 name: 'ivan'
1397 nums: [1, 2, 3]
1398 kv: {
1399 'aaa': '111'
1400 }
1401 },
1402 Person{
1403 name: 'bob'
1404 nums: [2]
1405 kv: {
1406 'bbb': '222'
1407 }
1408 },
1409 ]
1410 println(ivan in people)
1411 assert ivan in people
1412}
1413
1414fn test_struct_array_of_multi_type_index() {
1415 ivan := Person{
1416 name: 'ivan'
1417 nums: [1, 2, 3]
1418 kv: {
1419 'aaa': '111'
1420 }
1421 }
1422 people := [
1423 Person{
1424 name: 'ivan'
1425 nums: [1, 2, 3]
1426 kv: {
1427 'aaa': '111'
1428 }
1429 },
1430 Person{
1431 name: 'bob'
1432 nums: [2]
1433 kv: {
1434 'bbb': '222'
1435 }
1436 },
1437 ]
1438 println(people.index(ivan))
1439 assert people.index(ivan) == 0
1440}
1441
1442struct Coord {
1443 x int
1444 y int
1445 z int
1446}
1447
1448fn test_array_struct_contains() {
1449 mut coords := []Coord{}
1450 coord_1 := Coord{
1451 x: 1
1452 y: 2
1453 z: -1
1454 }
1455 coords << coord_1
1456 exists := coord_1 in coords
1457 not_exists := coord_1 !in coords
1458 println('`exists`: ${exists} and `not exists`: ${not_exists}')
1459 assert exists == true
1460 assert not_exists == false
1461}
1462
1463fn test_array_struct_ref_contains() {
1464 mut coords := []&Coord{}
1465 coord_1 := &Coord{
1466 x: 1
1467 y: 2
1468 z: -1
1469 }
1470 coords << coord_1
1471 exists := coord_1 in coords
1472 println(exists)
1473 assert exists == true
1474}
1475
1476fn test_array_struct_ref_index() {
1477 mut coords := []&Coord{}
1478 coord_1 := &Coord{
1479 x: 1
1480 y: 2
1481 z: -1
1482 }
1483 coords << coord_1
1484 println(coords.index(coord_1))
1485 assert coords.index(coord_1) == 0
1486}
1487
1488fn test_array_of_array_append() {
1489 mut x := [][]int{len: 4}
1490 println(x) // OK
1491 x[2] << 123 // RTE
1492 println(x)
1493 assert '${x}' == '[[], [], [123], []]'
1494}
1495
1496fn test_array_of_map_insert() {
1497 mut x := []map[string]int{len: 4}
1498 println(x) // OK
1499 x[2]['123'] = 123 // RTE
1500 println(x)
1501 assert '${x}' == "[{}, {}, {'123': 123}, {}]"
1502}
1503
1504fn test_multi_fixed_array_init() {
1505 a := [3][3]int{}
1506 assert '${a}' == '[[0, 0, 0], [0, 0, 0], [0, 0, 0]]'
1507}
1508
1509struct Numbers {
1510 odds []int
1511 evens []int
1512}
1513
1514fn test_array_of_multi_filter() {
1515 arr := [1, 2, 3, 4, 5]
1516 nums := Numbers{
1517 odds: arr.filter(it % 2 == 1)
1518 evens: arr.filter(it % 2 == 0)
1519 }
1520 println(nums)
1521 assert nums.odds == [1, 3, 5]
1522 assert nums.evens == [2, 4]
1523}
1524
1525fn test_array_of_multi_map() {
1526 arr := [1, 3, 5]
1527 nums := Numbers{
1528 odds: arr.map(it + 2)
1529 evens: arr.map(it * 2)
1530 }
1531 println(nums)
1532 assert nums.odds == [3, 5, 7]
1533 assert nums.evens == [2, 6, 10]
1534}
1535
1536fn test_multi_fixed_array_with_default_init() {
1537 a := [3][3]int{init: [3]int{init: 10}}
1538 println(a)
1539 assert a == [[10, 10, 10]!, [10, 10, 10]!, [10, 10, 10]!]!
1540}
1541
1542struct Abc {
1543mut:
1544 x i64
1545 y i64
1546 z i64
1547}
1548
1549fn test_clone_of_same_elem_size_array() {
1550 mut arr := []Abc{}
1551 arr << Abc{1, 2, 3}
1552 arr << Abc{2, 3, 4}
1553 arr2 := arr.clone()
1554 println(arr2)
1555 assert arr2 == [Abc{1, 2, 3}, Abc{2, 3, 4}]
1556}
1557
1558pub fn example[T](mut arr []T) []T {
1559 return arr.clone()
1560}
1561
1562fn test_generic_mutable_arrays() {
1563 mut arr := [1, 2, 3]
1564 assert example(mut arr) == [1, 2, 3]
1565}
1566
1567struct Ok {}
1568
1569fn test_inline_array_element_access() {
1570 println([Ok{}][0])
1571 a1 := [Ok{}][0]
1572 assert a1 == Ok{}
1573
1574 println([1][0])
1575 a2 := [1][0]
1576 assert a2 == 1
1577}
1578
1579//
1580
1581fn f(x int, y int) []int {
1582 return [x, y]
1583}
1584
1585fn test_2d_array_init_with_it() {
1586 a := [][]int{len: 6, init: f(index, 2 * index)}
1587 assert a == [[0, 0], [1, 2], [2, 4], [3, 6], [4, 8], [5, 10]]
1588}
1589
1590fn test_using_array_name_variable() {
1591 array := []int{len: 4, init: index}
1592 println(array)
1593 assert array == [0, 1, 2, 3]
1594}
1595
1596struct Data {
1597mut:
1598 sub_map map[int]int
1599}
1600
1601fn test_array_of_struct_with_map_field() {
1602 n := 3
1603 mut arr := []Data{len: n}
1604 for i, mut a in arr {
1605 arr[i].sub_map[i] = 1
1606 a.sub_map[i] += 1
1607 }
1608 println(arr)
1609 // Note: test_array_of_struct_with_map_field fails sporadically on windows with the default `-gc boehm_full_opt`,
1610 // but it *does not* with any other `-gc` setting. The compiler does not matter; tested with tcc, gcc, clang .
1611 // The failure is from: `assert arr[0].sub_map == { 0: 2 }`, and it seems to also depend on the previous dump() calls
1612 // in this test file. If all of them are commented, it succeeds.
1613 // Tested with: rm vlib/builtin/array_test ; xtime v -keepc vlib/builtin/array_test.v ; for((i=0;i<100;i++)); do echo ">>>>>>>>>>>>>>>>>>>>>>>> $i"; ./vlib/builtin/array_test || break ; echo "done with $i"; done
1614 // executed in a git bash shell. It usually fails after the first 3-5 iterations.
1615 assert arr[0].sub_map == {
1616 0: 2
1617 }
1618 assert arr[1].sub_map == {
1619 1: 2
1620 }
1621 assert arr[2].sub_map == {
1622 2: 2
1623 }
1624}