v2 / vlib / builtin / array_test.v
2006 lines · 1776 sloc · 40.38 KB · 88bb62a842d042a119fb8136c8570d774e68eba2
Raw
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_last_uses_in_place_fast_path_for_unique_arrays() {
82 mut a := [1, 2, 3, 4]
83 old_data := a.data
84 a.delete(a.len - 1)
85 assert a == [1, 2, 3]
86 assert a.data == old_data
87}
88
89fn test_delete_last_detaches_when_a_slice_exists() {
90 mut a := [1, 2, 3, 4]
91 b := unsafe { a[..a.len] }
92 old_data := a.data
93 a.delete(a.len - 1)
94 assert a == [1, 2, 3]
95 assert b == [1, 2, 3, 4]
96 assert a.data != old_data
97}
98
99fn test_delete_many() {
100 mut a := [1, 2, 3, 4, 5, 6, 7, 8, 9]
101 b := unsafe { a[2..6] }
102 a.delete_many(4, 3)
103 assert a == [1, 2, 3, 4, 8, 9]
104 assert b == [3, 4, 5, 6]
105 c := unsafe { a[..a.len] }
106 a.delete_many(2, 0) // this should just clone
107 a[1] = 17
108 assert a == [1, 17, 3, 4, 8, 9]
109 assert c == [1, 2, 3, 4, 8, 9]
110 a.delete_many(0, a.len)
111 assert a == []int{}
112}
113
114fn int_array_with_spare_cap() []int {
115 mut a := []int{len: 5, cap: 8}
116 for i in 0 .. a.len {
117 a[i] = i + 1
118 }
119 return a
120}
121
122fn test_delete_many_unique_arrays_use_in_place_fast_path() {
123 mut a := int_array_with_spare_cap()
124 old_data := a.data
125 a.delete_many(1, 2)
126 assert a == [1, 4, 5]
127 assert a.data == old_data
128 assert a.cap == 3
129}
130
131fn test_insert_detaches_parent_with_existing_slice() {
132 mut a := int_array_with_spare_cap()
133 mut b := unsafe { a[1..4] }
134 a.insert(0, 99)
135 assert a == [99, 1, 2, 3, 4, 5]
136 assert b == [2, 3, 4]
137 b[0] = 42
138 assert a == [99, 1, 2, 3, 4, 5]
139}
140
141fn test_slice_pop_detaches_immediately() {
142 mut a := int_array_with_spare_cap()
143 mut b := unsafe { a[1..4] }
144 last := b.pop()
145 b[0] = 20
146 b << 99
147 assert last == 4
148 assert a == [1, 2, 3, 4, 5]
149 assert b == [20, 3, 99]
150}
151
152fn test_slice_trim_detaches_immediately() {
153 mut a := int_array_with_spare_cap()
154 mut b := unsafe { a[1..4] }
155 b.trim(1)
156 b[0] = 20
157 b << 99
158 assert a == [1, 2, 3, 4, 5]
159 assert b == [20, 99]
160}
161
162fn test_slice_clear_detaches_immediately() {
163 mut a := int_array_with_spare_cap()
164 mut b := unsafe { a[1..4] }
165 b.clear()
166 b << 77
167 assert a == [1, 2, 3, 4, 5]
168 assert b == [77]
169}
170
171fn test_short() {
172 a := [1, 2, 3]
173 assert a.len == 3
174 assert a.cap == 3
175 assert a[0] == 1
176 assert a[1] == 2
177 assert a[2] == 3
178}
179
180fn test_large() {
181 mut a := [0].repeat(0)
182 for i in 0 .. 10000 {
183 a << i
184 }
185 assert a.len == 10000
186 assert a[234] == 234
187}
188
189struct Chunk {
190 val string
191}
192
193struct Kkk {
194 q []Chunk
195}
196
197fn test_empty() {
198 mut chunks := []Chunk{}
199 a := Chunk{}
200 assert chunks.len == 0
201 chunks << a
202 assert chunks.len == 1
203 chunks = []
204 assert chunks.len == 0
205 chunks << a
206 assert chunks.len == 1
207}
208
209fn test_push() {
210 mut a := []int{}
211 a << 1
212 a << 3
213 assert a[1] == 3
214 assert a.str() == '[1, 3]'
215}
216
217fn test_insert() {
218 mut a := [1, 2]
219 a.insert(0, 3)
220 assert a[0] == 3
221 assert a[2] == 2
222 assert a.len == 3
223 a.insert(1, 4)
224 assert a[1] == 4
225 assert a[2] == 1
226 assert a.len == 4
227 a.insert(4, 5)
228 assert a[4] == 5
229 assert a[3] == 2
230 assert a.len == 5
231 mut b := []f64{}
232 assert b.len == 0
233 b.insert(0, f64(1.1))
234 assert b.len == 1
235 assert b[0] == f64(1.1)
236}
237
238fn test_insert_many() {
239 mut a := [3, 4]
240 a.insert(0, [1, 2])
241 assert a == [1, 2, 3, 4]
242 b := [5, 6]
243 a.insert(1, b)
244 assert a == [1, 5, 6, 2, 3, 4]
245}
246
247fn test_prepend() {
248 mut a := []int{}
249 assert a.len == 0
250 a.prepend(1)
251 assert a.len == 1
252 assert a[0] == 1
253 mut b := []f64{}
254 assert b.len == 0
255 b.prepend(f64(1.1))
256 assert b.len == 1
257 assert b[0] == f64(1.1)
258}
259
260fn test_prepend_many() {
261 mut a := [3, 4]
262 a.prepend([1, 2])
263 assert a == [1, 2, 3, 4]
264 b := [5, 6]
265 a.prepend(b)
266 assert a == [5, 6, 1, 2, 3, 4]
267}
268
269type Strings = []string
270
271fn test_aliases_of_array_insert() {
272 mut strs := Strings(['hi'])
273 strs.insert(0, '22')
274 assert strs == ['22', 'hi']
275 strs.insert(1, ['11'])
276 assert strs == ['22', '11', 'hi']
277}
278
279fn test_aliases_of_array_prepend() {
280 mut strs := Strings(['hi'])
281 strs.prepend('22')
282 assert strs == ['22', 'hi']
283 strs.prepend(['44', '33'])
284 assert strs == ['44', '33', '22', 'hi']
285}
286
287fn test_strings() {
288 a := ['a', 'b', 'c']
289 assert a.str() == "['a', 'b', 'c']"
290}
291
292/*
293fn test_compare_ints() {
294 assert compare_ints(1, 2) == -1
295 assert compare_ints(2, 1) == 1
296 assert compare_ints(0, 0) == 0
297
298 a := 1
299 b := 2
300 assert compare_ints(a, b) == -1
301 assert compare_ints(b, a) == 1
302 assert compare_ints(a, a) == 0
303}
304*/
305
306fn test_repeat_int() {
307 a := [1234].repeat(5)
308 // dump(a)
309 assert a.len == 5
310 for x in a {
311 assert x == 1234
312 }
313}
314
315fn test_repeat_f64() {
316 a := [1.1].repeat(10)
317 // dump(a)
318 assert a.len == 10
319 assert a[0] == 1.1
320 assert a[5] == 1.1
321 assert a[9] == 1.1
322}
323
324fn test_repeat_f32() {
325 a := [f32(1.1)].repeat(10)
326 // dump(a)
327 assert a.len == 10
328 assert a[0] == f32(1.1)
329 assert a[5] == f32(1.1)
330 assert a[9] == f32(1.1)
331}
332
333fn test_repeat_i64() {
334 a := [i64(-123)].repeat(10)
335 // dump(a)
336 assert a.len == 10
337 assert a[0] == -123
338 assert a[5] == -123
339 assert a[9] == -123
340}
341
342fn test_repeat_u64() {
343 a := [u64(123)].repeat(10)
344 assert a[0] == 123
345 assert a[5] == 123
346 assert a[9] == 123
347}
348
349fn test_repeat_several_ints() {
350 a := [1, 2].repeat(2)
351 // dump(a)
352 assert a.len == 4
353 assert a[0] == 1
354 assert a[1] == 2
355 assert a[2] == 1
356 assert a[3] == 2
357}
358
359fn test_repeat_several_strings_2() {
360 a := ['1', 'abc'].repeat(2)
361 // dump(a)
362 assert a.len == 4
363 assert a[0] == '1'
364 assert a[1] == 'abc'
365 assert a[2] == '1'
366 assert a[3] == 'abc'
367}
368
369fn test_repeat_several_strings_0() {
370 mut a := ['1', 'abc'].repeat(0)
371 // dump(a)
372 assert a.len == 0
373 a << 'abc'
374 assert a[0] == 'abc'
375}
376
377fn test_deep_repeat() {
378 mut a3 := [[[1, 1], [2, 2], [3, 3]], [[4, 4], [5, 5], [6, 6]]]
379 r := a3.repeat(3)
380 // dump(r)
381 a3[1][1][0] = 17
382 assert r == [
383 [[1, 1], [2, 2], [3, 3]],
384 [[4, 4], [5, 5], [6, 6]],
385 [[1, 1], [2, 2], [3, 3]],
386 [[4, 4], [5, 5], [6, 6]],
387 [[1, 1], [2, 2], [3, 3]],
388 [[4, 4], [5, 5], [6, 6]],
389 ]
390 assert a3 == [[[1, 1], [2, 2], [3, 3]], [[4, 4], [17, 5], [6, 6]]]
391}
392
393interface RepeatClonedInterfaceValue {
394 get_value() int
395mut:
396 change()
397}
398
399struct RepeatClonedStructValue {
400mut:
401 value int
402}
403
404fn (sample RepeatClonedStructValue) get_value() int {
405 return sample.value
406}
407
408fn (mut sample RepeatClonedStructValue) change() {
409 sample.value += 2
410}
411
412fn test_repeat_clones_interface_elements() {
413 mut samples := [
414 RepeatClonedInterfaceValue(RepeatClonedStructValue{
415 value: 1
416 }),
417 RepeatClonedInterfaceValue(RepeatClonedStructValue{
418 value: 2
419 }),
420 ].repeat(2)
421 samples[0].change()
422 samples[1].change()
423 samples[1].change()
424 assert samples[0].get_value() == 3
425 assert samples[1].get_value() == 6
426 assert samples[2].get_value() == 1
427 assert samples[3].get_value() == 2
428}
429
430fn test_right() {
431 a := [1, 2, 3, 4]
432 c := a[1..a.len]
433 d := a[1..]
434 assert c[0] == 2
435 assert c[1] == 3
436 assert d[0] == 2
437 assert d[1] == 3
438}
439
440fn test_left() {
441 a := [1, 2, 3]
442 c := a[0..2]
443 d := a[..2]
444 assert c[0] == 1
445 assert c[1] == 2
446 assert d[0] == 1
447 assert d[1] == 2
448}
449
450fn test_slice() {
451 a := [1, 2, 3, 4]
452 b := a[2..4]
453 assert b.len == 2
454 assert a[1..2].len == 1
455 assert a.len == 4
456}
457
458fn test_push_many() {
459 mut a := [1, 2, 3]
460 b := [4, 5, 6]
461 a << b
462 assert a.len == 6
463 assert a[0] == 1
464 assert a[3] == 4
465 assert a[5] == 6
466}
467
468fn test_push_many_self_append_with_growth() {
469 mut a := [u8(`a`), `b`, `c`]
470 a << a
471 assert a.bytestr() == 'abcabc'
472 a << a
473 assert a.bytestr() == 'abcabcabcabc'
474}
475
476fn test_reverse() {
477 a := [1, 2, 3, 4]
478 b := ['test', 'array', 'reverse']
479 c := a.reverse()
480 println(c)
481 d := b.reverse()
482 for i, _ in c {
483 assert c[i] == a[a.len - i - 1]
484 }
485 for i, _ in d {
486 assert d[i] == b[b.len - i - 1]
487 }
488 e := []int{}
489 f := e.reverse()
490 assert f.len == 0
491}
492
493const c_n = 5
494
495struct Foooj {
496 a [5]int // c_n
497}
498
499fn test_fixed() {
500 mut nums := [4]int{}
501 // x := nums[1..3]
502 // assert x.len == 2
503 assert nums[0] == 0
504 assert nums[1] == 0
505 assert nums[2] == 0
506 assert nums[3] == 0
507 nums[1] = 7
508 assert nums[1] == 7
509 nums2 := [5]int{} // c_n
510 assert nums2[c_n - 1] == 0
511}
512
513fn modify(mut numbers []int) {
514 numbers[0] = 777
515}
516
517fn test_mut_slice() {
518 mut n := [1, 2, 3]
519 // modify(mut n)
520 modify(mut n[..2])
521 assert n[0] == 777
522 modify(mut n[2..])
523 assert n[2] == 777
524 println(n)
525}
526
527fn double_up(mut a []int) {
528 for i := 0; i < a.len; i++ {
529 a[i] = a[i] * 2
530 }
531}
532
533fn double_up_v2(mut a []int) {
534 for i, _ in a {
535 a[i] = a[i] * 2 // or val*2, doesn't matter
536 }
537}
538
539fn test_mut_arg() {
540 mut arr := [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
541 double_up(mut arr)
542 assert arr.str() == '[2, 4, 6, 8, 10, 12, 14, 16, 18, 20]'
543 arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
544 double_up_v2(mut arr)
545 assert arr.str() == '[2, 4, 6, 8, 10, 12, 14, 16, 18, 20]'
546}
547
548fn test_clone() {
549 nums := [1, 2, 3, 4, 100]
550 _ = nums
551 nums2 := nums.clone()
552 assert nums2.len == 5
553 assert nums.str() == '[1, 2, 3, 4, 100]'
554 assert nums2.str() == '[1, 2, 3, 4, 100]'
555 assert nums[1..3].str() == '[2, 3]'
556}
557
558/*
559fn test_copy() {
560 a := [1, 2, 3]
561 b := a
562 assert b[0] == 1
563 assert b[1] == 2
564 assert b[2] == 3
565}
566*/
567fn test_multi_array_clone() {
568 // 2d array_int
569 mut a2_1 := [[1, 2, 3], [4, 5, 6]]
570 mut a2_2 := a2_1.clone()
571 a2_1[0][1] = 0
572 a2_2[1][0] = 0
573 assert a2_1 == [[1, 0, 3], [4, 5, 6]]
574 assert a2_2 == [[1, 2, 3], [0, 5, 6]]
575 // 2d array_string
576 mut b2_1 := [['1', '2', '3'], ['4', '5', '6']]
577 mut b2_2 := b2_1.clone()
578 b2_1[0][1] = '0'
579 b2_2[1][0] = '0'
580 assert b2_1 == [['1', '0', '3'], ['4', '5', '6']]
581 assert b2_2 == [['1', '2', '3'], ['0', '5', '6']]
582 // 3d array_int
583 mut a3_1 := [[[1, 1], [2, 2], [3, 3]], [[4, 4], [5, 5], [6, 6]]]
584 mut a3_2 := a3_1.clone()
585 a3_1[0][0][1] = 0
586 a3_2[0][1][0] = 0
587 assert a3_1 == [[[1, 0], [2, 2], [3, 3]], [[4, 4], [5, 5],
588 [6, 6]]]
589 assert a3_2 == [[[1, 1], [0, 2], [3, 3]], [[4, 4], [5, 5],
590 [6, 6]]]
591 // 3d array_string
592 mut b3_1 := [[['1', '1'], ['2', '2'], ['3', '3']], [['4', '4'],
593 ['5', '5'], ['6', '6']]]
594 mut b3_2 := b3_1.clone()
595 b3_1[0][0][1] = '0'
596 b3_2[0][1][0] = '0'
597 assert b3_1 == [[['1', '0'], ['2', '2'], ['3', '3']], [['4', '4'],
598 ['5', '5'], ['6', '6']]]
599 assert b3_2 == [[['1', '1'], ['0', '2'], ['3', '3']], [['4', '4'],
600 ['5', '5'], ['6', '6']]]
601}
602
603fn test_doubling() {
604 mut nums := [1, 2, 3, 4, 5]
605 for i in 0 .. nums.len {
606 nums[i] *= 2
607 }
608 println(nums.str())
609 assert nums.str() == '[2, 4, 6, 8, 10]'
610}
611
612struct Test2 {
613 one int
614 two int
615}
616
617struct Test {
618 a string
619mut:
620 b []Test2
621}
622
623fn (t Test2) str() string {
624 return '{${t.one} ${t.two}}'
625}
626
627fn (t Test) str() string {
628 return '{${t.a} ${t.b}}'
629}
630
631fn test_struct_print() {
632 mut a := Test{
633 a: 'Test'
634 b: []
635 }
636 b := Test2{
637 one: 1
638 two: 2
639 }
640 a.b << b
641 a.b << b
642 assert a.str() == '{Test [{1 2}, {1 2}]}'
643 assert b.str() == '{1 2}'
644 assert a.b.str() == '[{1 2}, {1 2}]'
645}
646
647fn test_single_element() {
648 mut a := [1]
649 a << 2
650 assert a.len == 2
651 assert a[0] == 1
652 assert a[1] == 2
653 println(a)
654}
655
656fn test_find_index() {
657 // string
658 a := ['v', 'is', 'great']
659 assert a.index('v') == 0
660 assert a.index('is') == 1
661 assert a.index('gre') == -1
662 // int
663 b := [1, 2, 3, 4]
664 assert b.index(1) == 0
665 assert b.index(4) == 3
666 assert b.index(5) == -1
667 // byte
668 c := [0x22, 0x33, 0x55]
669 assert c.index(0x22) == 0
670 assert c.index(0x55) == 2
671 assert c.index(0x99) == -1
672 // char
673 d := [`a`, `b`, `c`]
674 assert d.index(`b`) == 1
675 assert d.index(`c`) == 2
676 assert d.index(`u`) == -1
677}
678
679fn test_find_last_index() {
680 // string
681 a := ['v', 'is', 'great', 'is', 'k', 'm']
682 assert a.last_index('v') == 0
683 assert a.last_index('is') == 3
684 assert a.last_index('gre') == -1
685 // int
686 b := [1, 2, 3, 4, 0, 1, 2, 3, 0, 1, 2, 3]
687 assert b.last_index(1) == 9
688 assert b.last_index(4) == 3
689 assert b.last_index(5) == -1
690 // byte
691 c := [0x22, 0x33, 0x55, 0x22, 0x44, 0x55]
692 assert c.last_index(0x22) == 3
693 assert c.last_index(0x55) == 5
694 assert c.last_index(0x99) == -1
695 // char
696 d := [`a`, `b`, `c`, `e`, `a`, `b`, `c`, `k`]
697 assert d.last_index(`b`) == 5
698 assert d.last_index(`c`) == 6
699 assert d.last_index(`u`) == -1
700}
701
702fn test_multi() {
703 a := [[1, 2, 3], [4, 5, 6]]
704 assert a.len == 2
705 assert a[0].len == 3
706 assert a[0][0] == 1
707 assert a[0][2] == 3
708 assert a[1][2] == 6
709 b := [[[1, 2, 3], [4, 5, 6]], [[1, 2]]]
710 assert b[0][0][0] == 1
711}
712
713fn test_in() {
714 a := [1, 2, 3]
715 assert 1 in a
716 assert 2 in a
717 assert 3 in a
718 assert 4 !in a
719 assert 0 !in a
720 assert 0 !in a
721 assert 4 !in a
722 b := [1, 4, 0]
723 c := [3, 6, 2, 0]
724 assert 0 in b
725 assert 0 in c
726}
727
728fn sum(prev int, curr int) int {
729 return prev + curr
730}
731
732fn sub(prev int, curr int) int {
733 return prev - curr
734}
735
736fn filter_test_helper_1(a int) bool {
737 return a > 3
738}
739
740fn test_filter() {
741 a := [1, 2, 3, 4, 5, 6]
742 b := a.filter(it % 2 == 0)
743 assert b.len == 3
744 assert b[0] == 2
745 assert b[1] == 4
746 assert b[2] == 6
747 c := ['v', 'is', 'awesome']
748 d := c.filter(it.len > 1)
749 assert d[0] == 'is'
750 assert d[1] == 'awesome'
751 ////////
752 arr := [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
753 println(arr.filter(it % 2 == 0 || it % 3 == 0))
754 assert true
755 assert [1, 2, 3].len == 3
756 mut mut_arr := [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
757 mut_arr = mut_arr.filter(it < 4)
758 assert mut_arr.len == 3
759 assert a.filter(filter_test_helper_1) == [4, 5, 6]
760 assert [1, 5, 10].filter(filter_test_helper_1) == [5, 10]
761}
762
763fn test_anon_fn_filter() {
764 filter_num := fn (i int) bool {
765 return i % 2 == 0
766 }
767 assert [1, 2, 3, 4, 5].filter(filter_num) == [2, 4]
768}
769
770fn test_anon_fn_arg_filter() {
771 a := [1, 2, 3, 4].filter(fn (i int) bool {
772 return i % 2 == 0
773 })
774 assert a == [2, 4]
775}
776
777fn map_test_helper_1(i int) int {
778 return i * i
779}
780
781fn map_test_helper_2(i int, b string) int {
782 return i + b.len
783}
784
785fn map_test_helper_3(i int, b []string) int {
786 return i + b.map(it.len)[i % b.len]
787}
788
789fn test_map() {
790 nums := [1, 2, 3, 4, 5, 6]
791 strs := ['v', 'is', 'awesome']
792 // assert nums.map() == <error>
793 // assert nums.map(it, 'excessive') == <error>
794 // identity
795 assert nums.map(it) == [1, 2, 3, 4, 5, 6]
796 assert strs.map(it) == ['v', 'is', 'awesome']
797 assert nums.map(it - it) == [0, 0, 0, 0, 0, 0]
798 assert nums.map(it - it)[0] == 0
799 // type switch
800 assert nums.map(it * 10) == [10, 20, 30, 40, 50, 60]
801 assert nums.map(it * it) == [1, 4, 9, 16, 25, 36]
802 assert nums.map('${it}') == ['1', '2', '3', '4', '5', '6']
803 assert nums.map(it % 2 == 0) == [false, true, false, true, false, true]
804 assert strs.map(it.to_upper()) == ['V', 'IS', 'AWESOME']
805 assert strs.map(it == 'awesome') == [false, false, true]
806 assert strs.map(it.len in nums) == [true, true, false]
807 assert strs.map(int(7)) == [7, 7, 7]
808 // external func
809 assert nums.map(map_test_helper_1(it)) == [1, 4, 9, 16, 25, 36]
810 assert nums.map(map_test_helper_2(it, 'bb')) == [3, 4, 5, 6, 7, 8]
811 assert nums.map(map_test_helper_3(it, strs)) == [3, 9, 4, 6, 12, 7]
812 // empty array as input
813 assert []int{len: 0}.map(it * 2) == []
814 // nested maps (where it is of same type)
815 assert nums.map(strs.map(int(7)) == [7, 7, 7]) == [true, true, true, true, true, true]
816 assert nums.map('${it}' + strs.map('a')[0]) == ['1a', '2a', '3a', '4a', '5a', '6a']
817 assert nums.map(it + strs.map(int(7))[0]) == [8, 9, 10, 11, 12, 13]
818 assert nums.map(it + strs.map(it.len)[0]) == [2, 3, 4, 5, 6, 7]
819 assert strs.map(it.len + strs.map(it.len)[0]) == [2, 3, 8]
820 // nested (different it types)
821 assert strs.map(it[nums.map(it - it)[0]]) == [u8(`v`), `i`, `a`]
822 assert nums[0..3].map('${it}' + strs.map(it)[it - 1]) == ['1v', '2is', '3awesome']
823 assert nums.map(map_test_helper_1) == [1, 4, 9, 16, 25, 36]
824 assert [1, 5, 10].map(map_test_helper_1) == [1, 25, 100]
825 assert nums == [1, 2, 3, 4, 5, 6]
826 assert strs == ['v', 'is', 'awesome']
827}
828
829fn test_anon_fn_map() {
830 add_num := fn (i int) int {
831 return i + 1
832 }
833 assert [1, 2, 3].map(add_num) == [2, 3, 4]
834}
835
836fn test_multi_anon_fn_map() {
837 a := [1, 2, 3].map(fn (i int) int {
838 return i + 1
839 })
840 b := [1, 2, 3].map(fn (i int) int {
841 return i + 2
842 })
843 assert a == [2, 3, 4]
844 assert b == [3, 4, 5]
845}
846
847fn test_anon_fn_arg_map() {
848 a := [1, 2, 3].map(fn (i int) int {
849 return i + 1
850 })
851 assert a == [2, 3, 4]
852}
853
854fn test_anon_fn_arg_different_type_map() {
855 i_to_str := fn (i int) string {
856 return i.str()
857 }
858 a := [1, 2, 3].map(i_to_str)
859 assert a == ['1', '2', '3']
860}
861
862fn test_anon_fn_inline_different_type_map() {
863 a := [1, 2, 3].map(fn (i int) string {
864 return i.str()
865 })
866 assert a == ['1', '2', '3']
867}
868
869fn test_array_str() {
870 numbers := [1, 2, 3]
871 assert numbers == [1, 2, 3]
872 numbers2 := [numbers, [4, 5, 6]] // dup str() bug
873 println(numbers2)
874 assert true
875 assert numbers.str() == '[1, 2, 3]'
876 assert numbers2.str() == '[[1, 2, 3], [4, 5, 6]]'
877}
878
879struct User {
880 age int
881 name string
882}
883
884fn test_eq() {
885 assert [5, 6, 7] != [6, 7]
886 assert [`a`, `b`] == [`a`, `b`]
887 assert [User{
888 age: 22
889 name: 'bob'
890 }] == [User{
891 age: 22
892 name: 'bob'
893 }]
894 assert [{
895 'bob': 22
896 }, {
897 'tom': 33
898 }] == [{
899 'bob': 22
900 }, {
901 'tom': 33
902 }]
903 assert [[1, 2, 3], [4]] == [[1, 2, 3], [4]]
904}
905
906fn test_fixed_array_eq() {
907 a1 := [1, 2, 3]!
908 assert a1 == [1, 2, 3]!
909 assert a1 != [2, 3, 4]!
910
911 a2 := [[1, 2]!, [3, 4]!]!
912 assert a2 == [[1, 2]!, [3, 4]!]!
913 assert a2 != [[3, 4]!, [1, 2]!]!
914
915 // TODO: fixed arrays of dynamic arrays not yet supported in ARM64 backend
916 // a3 := [[1, 2], [3, 4]]!
917 // assert a3 == [[1, 2], [3, 4]]!
918 // assert a3 != [[1, 1], [2, 2]]!
919 // a4 := [[`a`, `b`], [`c`, `d`]]!
920 // assert a4 == [[`a`, `b`], [`c`, `d`]]!
921 // assert a4 != [[`c`, `a`], [`a`, `b`]]!
922 // a5 := [['aaa', 'bbb'], ['ccc', 'ddd']]!
923 // assert a5 == [['aaa', 'bbb'], ['ccc', 'ddd']]!
924 // assert a5 != [['abc', 'def'], ['ccc', 'ddd']]!
925 // a6 := [['aaa', 'bbb']!, ['ccc', 'ddd']!]!
926 // assert a6 == [['aaa', 'bbb']!, ['ccc', 'ddd']!]!
927 // assert a6 != [['aaa', 'bbb']!, ['aaa', 'ddd']!]!
928 // a7 := [[1, 2]!, [3, 4]!]
929 // assert a7 == [[1, 2]!, [3, 4]!]
930 // assert a7 != [[2, 3]!, [1, 2]!]
931 // a8 := [['aaa', 'bbb']!, ['ccc', 'ddd']!]
932 // assert a8 == [['aaa', 'bbb']!, ['ccc', 'ddd']!]
933 // assert a8 != [['bbb', 'aaa']!, ['cccc', 'dddd']!]
934}
935
936fn test_fixed_array_literal_eq() {
937 assert [1, 2, 3]! == [1, 2, 3]!
938 // TODO: fixed array literal != comparison in ARM64 backend
939 // assert [1, 1, 1]! != [1, 2, 3]!
940 // assert [[1, 2], [3, 4]]! == [[1, 2], [3, 4]]!
941 // assert [[1, 1], [2, 2]]! != [[1, 2], [3, 4]]!
942 // assert [[1, 1]!, [2, 2]!]! == [[1, 1]!, [2, 2]!]!
943 // assert [[1, 1]!, [2, 2]!]! != [[1, 2]!, [2, 3]!]!
944 // assert [[1, 1]!, [2, 2]!] == [[1, 1]!, [2, 2]!]
945 // assert [[1, 1]!, [2, 2]!] != [[1, 2]!, [2, 3]!]
946 // vfmt off
947 assert ([1, 2, 3]!) == [1, 2, 3]!
948 // vfmt on
949}
950
951fn test_sort() {
952 mut a := ['hi', '1', '5', '3']
953 a.sort()
954 assert a == ['1', '3', '5', 'hi']
955
956 mut nums := [67, -3, 108, 42, 7]
957 nums.sort()
958 assert nums == [-3, 7, 42, 67, 108]
959
960 nums.sort(a < b)
961 assert nums == [-3, 7, 42, 67, 108]
962
963 nums.sort(b < a)
964 assert nums == [108, 67, 42, 7, -3]
965
966 mut users := [User{22, 'Peter'}, User{20, 'Bob'}, User{25, 'Alice'}]
967 users.sort(a.age < b.age)
968 assert users[0].age == 20
969 assert users[1].age == 22
970 assert users[2].age == 25
971 assert users[0].name == 'Bob'
972 assert users[1].name == 'Peter'
973 assert users[2].name == 'Alice'
974
975 users.sort(a.age > b.age)
976 assert users[0].age == 25
977 assert users[1].age == 22
978 assert users[2].age == 20
979
980 users.sort(b.age > a.age)
981 assert users[0].age == 20
982 assert users[1].age == 22
983 assert users[2].age == 25
984
985 users.sort(a.name < b.name)
986 assert users[0].name == 'Alice'
987 assert users[1].name == 'Bob'
988 assert users[2].name == 'Peter'
989}
990
991fn test_sort_preserves_relative_order_for_equal_elements() {
992 source := [User{4, 'B'}, User{4, 'A'}, User{5, 'C'}]
993
994 mut sorted := source.clone()
995 sorted.sort(a.age > b.age)
996 assert sorted[0].name == 'C'
997 assert sorted[1].name == 'B'
998 assert sorted[2].name == 'A'
999
1000 copy := source.sorted(a.age > b.age)
1001 assert copy[0].name == 'C'
1002 assert copy[1].name == 'B'
1003 assert copy[2].name == 'A'
1004}
1005
1006fn test_sort_with_compare() {
1007 mut a := ['hi', '1', '5', '3']
1008 a.sort_with_compare(fn (a &string, b &string) int {
1009 if a < b {
1010 return -1
1011 }
1012 if a > b {
1013 return 1
1014 }
1015 return 0
1016 })
1017 assert a == ['1', '3', '5', 'hi']
1018}
1019
1020fn test_sort_with_compare_preserves_relative_order_for_equal_elements() {
1021 source := [User{4, 'B'}, User{4, 'A'}, User{5, 'C'}]
1022
1023 mut sorted := source.clone()
1024 sorted.sort_with_compare(fn (a &User, b &User) int {
1025 if a.age > b.age {
1026 return -1
1027 }
1028 if a.age < b.age {
1029 return 1
1030 }
1031 return 0
1032 })
1033 assert sorted[0].name == 'C'
1034 assert sorted[1].name == 'B'
1035 assert sorted[2].name == 'A'
1036
1037 copy := source.sorted_with_compare(fn (a &User, b &User) int {
1038 if a.age > b.age {
1039 return -1
1040 }
1041 if a.age < b.age {
1042 return 1
1043 }
1044 return 0
1045 })
1046 assert copy[0].name == 'C'
1047 assert copy[1].name == 'B'
1048 assert copy[2].name == 'A'
1049}
1050
1051fn test_rune_sort() {
1052 mut bs := [`f`, `e`, `d`, `b`, `c`, `a`]
1053 bs.sort()
1054 println(bs)
1055 assert bs == [`a`, `b`, `c`, `d`, `e`, `f`]
1056
1057 bs.sort(a > b)
1058 println(bs)
1059 assert bs == [`f`, `e`, `d`, `c`, `b`, `a`]
1060
1061 bs.sort(a < b)
1062 println(bs)
1063 assert bs == [`a`, `b`, `c`, `d`, `e`, `f`]
1064}
1065
1066fn test_sort_by_different_order_of_a_b() {
1067 mut x := [1, 2, 3]
1068 x.sort(a < b)
1069 println(x)
1070 assert x == [1, 2, 3]
1071
1072 mut y := [1, 2, 3]
1073 y.sort(b < a)
1074 println(y)
1075 assert y == [3, 2, 1]
1076}
1077
1078fn test_f32_sort() {
1079 mut f := [f32(50.0), 15, 1, 79, 38, 0, 27]
1080 f.sort()
1081 assert f == [f32(0.0), 1, 15, 27, 38, 50, 79]
1082
1083 f.sort(a < b)
1084 assert f == [f32(0.0), 1, 15, 27, 38, 50, 79]
1085
1086 f.sort(b > a)
1087 assert f == [f32(0.0), 1, 15, 27, 38, 50, 79]
1088
1089 f.sort(b < a)
1090 assert f == [f32(79.0), 50, 38, 27, 15, 1, 0]
1091
1092 f.sort(a > b)
1093 assert f == [f32(79.0), 50, 38, 27, 15, 1, 0]
1094}
1095
1096fn test_f64_sort() {
1097 mut f := [50.0, 15, 1, 79, 38, 0, 27]
1098 f.sort()
1099 assert f[0] == 0.0
1100 assert f[1] == 1.0
1101 assert f[6] == 79.0
1102}
1103
1104fn test_i64_sort() {
1105 mut f := [i64(50), 15, 1, 79, 38, 0, 27]
1106 f.sort()
1107 assert f[0] == 0
1108 assert f[1] == 1
1109 assert f[6] == 79
1110}
1111
1112fn test_sort_index_expr() {
1113 mut f := [[i64(50), 48], [i64(15)], [i64(1)], [i64(79)], [i64(38)],
1114 [i64(0)], [i64(27)]]
1115 // TODO: This currently gives "indexing pointer" error without unsafe
1116 unsafe {
1117 f.sort(a[0] < b[0])
1118 }
1119 assert f == [[i64(0)], [i64(1)], [i64(15)], [i64(27)], [i64(38)],
1120 [i64(50), 48], [i64(79)]]
1121}
1122
1123fn test_a_b_paras_sort() {
1124 mut arr_i := [1, 3, 2]
1125 arr_i.sort(a < b)
1126 println(arr_i)
1127 assert arr_i == [1, 2, 3]
1128 arr_i.sort(b < a)
1129 println(arr_i)
1130 assert arr_i == [3, 2, 1]
1131
1132 mut arr_f := [1.1, 3.3, 2.2]
1133 arr_f.sort(a < b)
1134 println(arr_f)
1135 assert arr_f == [1.1, 2.2, 3.3]
1136 arr_f.sort(b < a)
1137 println(arr_f)
1138 assert arr_f == [3.3, 2.2, 1.1]
1139}
1140
1141/*
1142fn test_for_last() {
1143 numbers := [1, 2, 3, 4]
1144 mut s := '['
1145 for num in numbers {
1146 s += '${num}'
1147 if !last {
1148 s += ', '
1149
1150 }
1151 }
1152 s += ']'
1153 assert s == '[1, 2, 3, 4]'
1154}
1155*/
1156struct Foo {
1157mut:
1158 bar []int
1159}
1160
1161fn test_in_struct() {
1162 mut baz := Foo{
1163 bar: [0, 0, 0]
1164 }
1165 baz.bar[0] += 2
1166 baz.bar[0]++
1167 assert baz.bar[0] == 3
1168}
1169
1170@[direct_array_access]
1171fn test_direct_modification() {
1172 mut foo := [2, 0, 5]
1173 foo[1] = 3
1174 foo[0] *= 7
1175 foo[1]--
1176 foo[2] -= 2
1177 assert foo[0] == 14
1178 assert foo[1] == 2
1179 assert foo[2] == 3
1180}
1181
1182fn test_bools() {
1183 println('test b')
1184 mut a := [true, false]
1185 a << true
1186 println(a)
1187}
1188
1189fn test_push_many_self() {
1190 mut actual_arr := [1, 2, 3, 4]
1191 actual_arr << actual_arr
1192 expected_arr := [1, 2, 3, 4, 1, 2, 3, 4]
1193 assert actual_arr.len == expected_arr.len
1194 for i in 0 .. actual_arr.len {
1195 assert actual_arr[i] == expected_arr[i]
1196 }
1197}
1198
1199fn test_for() {
1200 nums := [1, 2, 3]
1201 mut sum := 0
1202 for num in nums {
1203 sum += num
1204 }
1205 assert sum == 6
1206}
1207
1208fn test_clear() {
1209 mut arr := [1, 2, 3]
1210 assert arr.len == 3
1211 arr.clear()
1212 assert arr.len == 0
1213 arr << 3
1214 arr << 2
1215 arr << 1
1216 arr << 0
1217 assert arr.len == 4
1218 assert arr[0] == 3
1219 assert arr[1] == 2
1220 assert arr[2] == 1
1221 assert arr[3] == 0
1222 arr.clear()
1223 assert arr.len == 0
1224}
1225
1226fn test_trim() {
1227 mut arr := [1, 2, 3, 4, 5, 6, 7, 8, 9]
1228 assert arr.len == 9
1229 arr.trim(9)
1230 assert arr.len == 9
1231 assert arr.last() == 9
1232 arr.trim(7)
1233 assert arr.len == 7
1234 assert arr.last() == 7
1235 arr.trim(2)
1236 assert arr.len == 2
1237 assert arr.last() == 2
1238}
1239
1240@[manualfree]
1241fn test_drop() {
1242 mut a := [1, 2]
1243 a << 3 // pushing assures reallocation; a.cap now should be bigger:
1244 assert a.cap > 3
1245 // eprintln('>>> a.cap: ${a.cap} | a.len: ${a.len}')
1246
1247 a.drop(-1000)
1248 assert a == [1, 2, 3] // a.drop( negative ) should NOT modify the array
1249 // eprintln('>>> a.cap: ${a.cap} | a.len: ${a.len}')
1250
1251 a.drop(2)
1252 assert a == [3]
1253 assert a.cap > a.len
1254 // eprintln('>>> a.cap: ${a.cap} | a.len: ${a.len}')
1255
1256 a.drop(10)
1257 assert a == []
1258 assert a.cap > a.len
1259 // eprintln('>>> a.cap: ${a.cap} | a.len: ${a.len}')
1260
1261 a << 123
1262 a << 456
1263 a << 789
1264 // eprintln('>>> a.cap: ${a.cap} | a.len: ${a.len}')
1265 assert a == [123, 456, 789]
1266
1267 a.drop(10)
1268 assert a == []
1269 // eprintln('>>> a.cap: ${a.cap} | a.len: ${a.len}')
1270
1271 unsafe { a.free() } // test offset OK
1272}
1273
1274fn test_hex() {
1275 // array hex
1276 st := [u8(`V`), `L`, `A`, `N`, `G`]
1277 assert st.hex() == '564c414e47'
1278 assert st.hex().len == 10
1279 st1 := [u8(0x41)].repeat(100)
1280 assert st1.hex() == '41'.repeat(100)
1281}
1282
1283fn test_left_shift_precedence() {
1284 mut arr := []int{}
1285 arr << 1 + 1
1286 arr << 1 - 1
1287 arr << 2 / 1
1288 arr << 2 * 1
1289 assert arr[0] == 2
1290 assert arr[1] == 0
1291 assert arr[2] == 2
1292 assert arr[3] == 2
1293}
1294
1295fn test_array_with_cap() {
1296 a4 := []int{len: 1, cap: 10}
1297 assert a4.len == 1
1298 assert a4.cap == 10
1299 a5 := []int{len: 1, cap: 10}
1300 assert a5.len == 1
1301 assert a5.cap == 10
1302}
1303
1304fn test_multi_array_index() {
1305 mut a := [][]int{len: 2, init: []int{len: 3, init: 0}}
1306 a[0][0] = 1
1307 assert '${a}' == '[[1, 0, 0], [0, 0, 0]]'
1308 mut b := [[0].repeat(3)].repeat(2)
1309 b[0][0] = 1
1310 assert '${b}' == '[[1, 0, 0], [0, 0, 0]]'
1311}
1312
1313fn test_multi_array_default_init_preserves_noscan_rows() {
1314 $if gcboehm_opt ? {
1315 mut matrix := [][]f64{len: 2, init: []f64{len: 3, init: 0.0}}
1316 matrix[0][0] = 1.25
1317 assert matrix[0].flags.has(.noscan_data)
1318 assert matrix[1].flags.has(.noscan_data)
1319 assert matrix[0].data != matrix[1].data
1320 assert matrix[1][0] == 0.0
1321 }
1322}
1323
1324fn test_plus_assign_string() {
1325 mut a := ['']
1326 a[0] += 'abc'
1327 assert a == ['abc']
1328}
1329
1330fn mut_arr_with_eq_in_fn(mut a []int) {
1331 if a == [1, 2, 3, 4] {
1332 a[0] = 0
1333 }
1334 if [0, 2, 3, 4] == a {
1335 a[1] = 0
1336 }
1337 if !(a != [0, 0, 3, 4]) {
1338 a[2] = 0
1339 }
1340 if !([0, 0, 0, 4] != a) {
1341 a[3] = 0
1342 }
1343}
1344
1345fn test_mut_arr_with_eq_in_fn() {
1346 mut a := [1, 2, 3, 4]
1347 mut_arr_with_eq_in_fn(mut a)
1348 assert a == [0, 0, 0, 0]
1349}
1350
1351fn array_in_mut(mut a []int) {
1352 if 1 in a {
1353 a[0] = 2
1354 }
1355}
1356
1357fn test_array_in_mut() {
1358 mut a := [1, 2]
1359 array_in_mut(mut a)
1360 assert a == [2, 2]
1361}
1362
1363// test array delete in function with mut argument
1364fn delete_nums(mut arr []int) {
1365 arr.delete(0)
1366}
1367
1368fn test_array_delete_in_mut() {
1369 mut nums := [1, 2, 3]
1370 delete_nums(mut nums)
1371 assert nums == [2, 3]
1372}
1373
1374// test array add in function with mut argument
1375fn add_nums(mut arr []int) {
1376 arr << 4
1377}
1378
1379fn test_array_add_in_mut() {
1380 mut nums := [1, 2, 3]
1381 add_nums(mut nums)
1382 assert nums == [1, 2, 3, 4]
1383}
1384
1385fn test_reverse_in_place() {
1386 mut a := [1, 2, 3, 4]
1387 a.reverse_in_place()
1388 assert a == [4, 3, 2, 1]
1389 mut b := ['a', 'b', 'c']
1390 b.reverse_in_place()
1391 assert b == ['c', 'b', 'a']
1392 mut c := [[1, 2], [3, 4], [5, 6]]
1393 c.reverse_in_place()
1394 assert c == [[5, 6], [3, 4], [1, 2]]
1395}
1396
1397fn test_array_int_pop_left() {
1398 mut a := [1, 2, 3, 4, 5]
1399 b := unsafe { a[..5] } // full slice view
1400 assert a.len == 5
1401 first := a[0]
1402 x := a.pop_left()
1403 assert first == x
1404 assert a.len == 4
1405 assert a.cap == 4
1406 y := a.pop_left()
1407 assert y == 2
1408 a[0] = 100
1409 // NOTE: update a[0] also update b[2]
1410 assert b == [1, 2, 100, 4, 5]
1411
1412 mut one_elem := [1]
1413 one := one_elem.pop_left()
1414 assert one_elem.len == 0
1415 assert one_elem.cap == 0
1416 assert one == 1
1417}
1418
1419fn test_array_string_pop_left() {
1420 mut a := ['abc', 'def', 'xyz']
1421 assert a.len == 3
1422 x := a.first()
1423 y := a.pop_left()
1424 assert x == y
1425 assert a.pop_left() == 'def'
1426 assert a.pop_left() == 'xyz'
1427 assert a.len == 0
1428 assert a.cap == 0
1429}
1430
1431fn test_array_int_pop() {
1432 mut a := [1, 2, 3, 4, 5]
1433 assert a.len == 5
1434 x := a.last()
1435 y := a.pop()
1436 assert x == y
1437 assert a.len == 4
1438 z := a.pop()
1439 assert a.len == 3
1440 assert z == 4
1441 x1 := a.pop()
1442 x2 := a.pop()
1443 final := a.pop()
1444 assert final == 1
1445}
1446
1447fn test_array_string_pop() {
1448 mut a := ['abc', 'def', 'xyz']
1449 assert a.len == 3
1450 assert a.pop() == 'xyz'
1451 assert a.pop() == 'def'
1452 assert a.pop() == 'abc'
1453 assert a.len == 0
1454 assert a.cap == 3
1455}
1456
1457fn test_array_first() {
1458 a := [3]
1459 assert a.first() == 3
1460 b := [1, 2, 3, 4]
1461 assert b.first() == 1
1462 c := ['abc', 'def']
1463 assert c.first()[0] == `a`
1464 s := [Chunk{'a'}]
1465 assert s.first().val == 'a'
1466}
1467
1468fn test_array_last() {
1469 a := [3]
1470 assert a.last() == 3
1471 b := [1, 2, 3, 4]
1472 assert b.last() == 4
1473 c := ['abc', 'def']
1474 assert c.last()[0] == `d`
1475 s := [Chunk{'a'}]
1476 assert s.last().val == 'a'
1477}
1478
1479@[direct_array_access]
1480fn test_direct_array_access() {
1481 mut a := [11, 22, 33, 44]
1482 assert a[0] == 11
1483 assert a[2] == 33
1484 x := a[0]
1485 a[0] = 21
1486 a[1] += 2
1487 a[2] = x + 3
1488 a[3] -= a[1]
1489 assert a == [21, 24, 14, 20]
1490}
1491
1492@[direct_array_access]
1493fn test_direct_array_access_via_ptr() {
1494 mut b := [11, 22, 33, 44]
1495 unsafe {
1496 mut a := &b
1497 assert a[0] == 11
1498 assert a[2] == 33
1499 x := a[0]
1500 a[0] = 21
1501 a[1] += 2
1502 a[2] = x + 3
1503 a[3] -= a[1]
1504 assert a == [21, 24, 14, 20]
1505 }
1506}
1507
1508fn test_push_arr_string_free() {
1509 mut lines := ['hi']
1510 s := 'a' + 'b'
1511 lines << s
1512 // make sure the data in the array is valid after freeing the string
1513 unsafe { s.free() }
1514
1515 println(lines)
1516 assert lines.len == 2
1517 assert lines[0] == 'hi'
1518 assert lines[1] == 'ab'
1519}
1520
1521const grid_size_1 = 2
1522const grid_size_2 = 3
1523const grid_size_3 = 4
1524const cell_value = 123
1525
1526fn test_multidimensional_array_initialization_with_consts() {
1527 // TODO: module-level constants resolve to 0 in ARM64 backend
1528 // mut data := [][][]int{len: grid_size_1, init: [][]int{len: grid_size_2, init: []int{len: grid_size_3, init: cell_value}}}
1529 // assert data.len == grid_size_1
1530}
1531
1532fn test_byteptr_vbytes() {
1533 unsafe {
1534 bp := malloc(5)
1535 bp[0] = 1
1536 bp[1] = 2
1537 bp[2] = 3
1538 bp[3] = 4
1539 bp[4] = 255
1540 bytes := bp.vbytes(5)
1541 println(bytes)
1542 assert bytes.len == 5
1543 assert bytes[0] == 1
1544 assert bytes[1] == 2
1545 assert bytes[2] == 3
1546 assert bytes[3] == 4
1547 assert bytes[4] == 255
1548 }
1549}
1550
1551fn test_voidptr_vbytes() {
1552 unsafe {
1553 bp := malloc(3)
1554 bp[0] = 4
1555 bp[1] = 5
1556 bp[2] = 6
1557 bytes := voidptr(bp).vbytes(3)
1558 assert bytes.len == 3
1559 assert bytes[0] == 4
1560 assert bytes[1] == 5
1561 assert bytes[2] == 6
1562 println(bytes)
1563 }
1564}
1565
1566fn test_multi_array_prepend() {
1567 // TODO: nested array prepend type disambiguation in ARM64 backend
1568 // mut a := [][]int{}
1569 // a.prepend([1, 2, 3])
1570 // assert a == [[1, 2, 3]]
1571}
1572
1573fn test_multi_array_insert() {
1574 // TODO: nested array insert type disambiguation in ARM64 backend
1575 // mut a := [][]int{}
1576 // a.insert(0, [1, 2, 3])
1577 // assert a == [[1, 2, 3]]
1578 mut b := [][]int{}
1579 b.insert(0, [[1, 2, 3]])
1580 assert b == [[1, 2, 3]]
1581}
1582
1583fn test_multi_array_in() {
1584 a := [[1]]
1585 println([1] in a)
1586 assert [1] in a
1587}
1588
1589fn test_any_type_array_contains() {
1590 a := [true, false]
1591 assert a.contains(true)
1592 assert true in a
1593 assert a.contains(false)
1594 assert false in a
1595 b := [i64(2), 3, 4]
1596 assert b.contains(i64(3))
1597 assert 5 !in b
1598 c := [[1], [2]]
1599 assert c.contains([1])
1600 assert [2] in c
1601 assert [3] !in c
1602}
1603
1604struct Person {
1605 name string
1606 nums []int
1607 kv map[string]string
1608}
1609
1610fn test_struct_array_of_multi_type_in() {
1611 // TODO: struct equality with nested arrays/maps not working in ARM64 backend
1612 // ivan := Person{name: 'ivan', nums: [1, 2, 3], kv: {'aaa': '111'}}
1613 // people := [Person{name: 'ivan', nums: [1, 2, 3], kv: {'aaa': '111'}}]
1614 // assert ivan in people
1615}
1616
1617fn test_struct_array_of_multi_type_index() {
1618 // TODO: struct equality with nested arrays/maps not working in ARM64 backend
1619}
1620
1621// disabled_struct_array_of_multi_type_index: requires struct eq with nested arrays/maps
1622
1623struct Coord {
1624 x int
1625 y int
1626 z int
1627}
1628
1629fn test_array_struct_contains() {
1630 mut coords := []Coord{}
1631 coord_1 := Coord{
1632 x: 1
1633 y: 2
1634 z: -1
1635 }
1636 coords << coord_1
1637 exists := coord_1 in coords
1638 not_exists := coord_1 !in coords
1639 println('`exists`: ${exists} and `not exists`: ${not_exists}')
1640 assert exists == true
1641 assert not_exists == false
1642}
1643
1644fn test_array_struct_ref_contains() {
1645 mut coords := []&Coord{}
1646 coord_1 := &Coord{
1647 x: 1
1648 y: 2
1649 z: -1
1650 }
1651 coords << coord_1
1652 exists := coord_1 in coords
1653 println(exists)
1654 assert exists == true
1655}
1656
1657fn test_array_struct_ref_index() {
1658 mut coords := []&Coord{}
1659 coord_1 := &Coord{
1660 x: 1
1661 y: 2
1662 z: -1
1663 }
1664 coords << coord_1
1665 println(coords.index(coord_1))
1666 assert coords.index(coord_1) == 0
1667}
1668
1669fn test_array_of_array_append() {
1670 mut x := [][]int{len: 4}
1671 println(x) // OK
1672 x[2] << 123 // RTE
1673 println(x)
1674 assert '${x}' == '[[], [], [123], []]'
1675}
1676
1677fn test_array_of_map_insert() {
1678 // TODO: Map_string_int_str not generated for ARM64 backend
1679 // mut x := []map[string]int{len: 4}
1680 // println(x)
1681 // x[2]['123'] = 123
1682 // assert '${x}' == "[{}, {}, {'123': 123}, {}]"
1683}
1684
1685fn test_multi_fixed_array_init() {
1686 // TODO: multi-dimensional fixed array init crashes in ARM64 backend
1687 // a := [3][3]int{}
1688 // assert '${a}' == '[[0, 0, 0], [0, 0, 0], [0, 0, 0]]'
1689}
1690
1691struct Numbers {
1692 odds []int
1693 evens []int
1694}
1695
1696fn test_array_of_multi_filter() {
1697 arr := [1, 2, 3, 4, 5]
1698 nums := Numbers{
1699 odds: arr.filter(it % 2 == 1)
1700 evens: arr.filter(it % 2 == 0)
1701 }
1702 println(nums)
1703 assert nums.odds == [1, 3, 5]
1704 assert nums.evens == [2, 4]
1705}
1706
1707fn test_array_of_multi_map() {
1708 arr := [1, 3, 5]
1709 nums := Numbers{
1710 odds: arr.map(it + 2)
1711 evens: arr.map(it * 2)
1712 }
1713 println(nums)
1714 assert nums.odds == [3, 5, 7]
1715 assert nums.evens == [2, 6, 10]
1716}
1717
1718fn test_multi_fixed_array_with_default_init() {
1719 // TODO: multi-dimensional fixed array init in ARM64 backend
1720 // a := [3][3]int{init: [3]int{init: 10}}
1721 // assert a == [[10, 10, 10]!, [10, 10, 10]!, [10, 10, 10]!]!
1722}
1723
1724struct Abc {
1725mut:
1726 x i64
1727 y i64
1728 z i64
1729}
1730
1731fn test_clone_of_same_elem_size_array() {
1732 mut arr := []Abc{}
1733 arr << Abc{1, 2, 3}
1734 arr << Abc{2, 3, 4}
1735 arr2 := arr.clone()
1736 println(arr2)
1737 assert arr2 == [Abc{1, 2, 3}, Abc{2, 3, 4}]
1738}
1739
1740pub fn example[T](mut arr []T) []T {
1741 return arr.clone()
1742}
1743
1744fn test_generic_mutable_arrays() {
1745 // TODO: generic functions not working in ARM64 backend
1746 // mut arr := [1, 2, 3]
1747 // assert example(mut arr) == [1, 2, 3]
1748}
1749
1750struct Ok {}
1751
1752fn test_inline_array_element_access() {
1753 println([Ok{}][0])
1754 a1 := [Ok{}][0]
1755 assert a1 == Ok{}
1756
1757 println([1][0])
1758 a2 := [1][0]
1759 assert a2 == 1
1760}
1761
1762//
1763
1764fn f(x int, y int) []int {
1765 return [x, y]
1766}
1767
1768fn test_2d_array_init_with_it() {
1769 // TODO: 2D array init with index expression in ARM64 backend
1770 // a := [][]int{len: 6, init: f(index, 2 * index)}
1771 // assert a == [[0, 0], [1, 2], [2, 4], [3, 6], [4, 8], [5, 10]]
1772}
1773
1774fn test_using_array_name_variable() {
1775 array := []int{len: 4, init: index}
1776 println(array)
1777 assert array == [0, 1, 2, 3]
1778}
1779
1780struct Data {
1781mut:
1782 sub_map map[int]int
1783}
1784
1785fn test_array_of_struct_with_map_field() {
1786 n := 3
1787 mut arr := []Data{len: n}
1788 for i, mut a in arr {
1789 arr[i].sub_map[i] = 1
1790 a.sub_map[i] += 1
1791 }
1792 println(arr)
1793 // Note: test_array_of_struct_with_map_field fails sporadically on windows with the default `-gc boehm_full_opt`,
1794 // but it *does not* with any other `-gc` setting. The compiler does not matter; tested with tcc, gcc, clang .
1795 // The failure is from: `assert arr[0].sub_map == { 0: 2 }`, and it seems to also depend on the previous dump() calls
1796 // in this test file. If all of them are commented, it succeeds.
1797 // 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
1798 // executed in a git bash shell. It usually fails after the first 3-5 iterations.
1799 assert arr[0].sub_map == {
1800 0: 2
1801 }
1802 assert arr[1].sub_map == {
1803 1: 2
1804 }
1805 assert arr[2].sub_map == {
1806 2: 2
1807 }
1808}
1809
1810fn test_reset() {
1811 mut a := []int{len: 5, init: index * 10}
1812 assert a == [0, 10, 20, 30, 40]
1813 unsafe { a.reset() }
1814 assert a == [0, 0, 0, 0, 0]
1815
1816 mut b := []f64{len: 5, init: f64(index) / 10.0}
1817 assert b == [0.0, 0.1, 0.2, 0.3, 0.4]
1818 unsafe { b.reset() }
1819 assert b == [0.0, 0.0, 0.0, 0.0, 0.0]
1820
1821 // TODO: string array init with index.str() in ARM64 backend
1822 // mut s := []string{len: 5, init: index.str()}
1823 // assert s == ['0', '1', '2', '3', '4']
1824}
1825
1826fn test_index_of_ints() {
1827 ia := [1, 2, 3]
1828 ii := ia.index(2)
1829 dump(ii)
1830 assert ii == 1
1831}
1832
1833fn test_index_of_strings() {
1834 sa := ['a', 'b', 'c']
1835 si := sa.index('b')
1836 dump(si)
1837 assert si == 1
1838}
1839
1840fn test_index_of_voidptrs() {
1841 pa := [voidptr(123), voidptr(45), voidptr(99)]
1842 pi := pa.index(voidptr(45))
1843 dump(pi)
1844 assert pi == 1
1845}
1846
1847fn a() {}
1848
1849fn b() {}
1850
1851fn c() {}
1852
1853fn test_index_of_fns() {
1854 fa := [a, b, c]
1855 fi := fa.index(b)
1856 dump(fi)
1857 assert fi == 1
1858}
1859
1860fn test_sorted_immutable_original_should_not_change() {
1861 a := ['hi', '1', '5', '3']
1862 b := a.sorted()
1863 assert a == ['hi', '1', '5', '3']
1864 assert b == ['1', '3', '5', 'hi']
1865}
1866
1867fn test_sorted_mutable_original_should_not_change() {
1868 mut a := ['hi', '1', '5', '3']
1869 b := a.sorted()
1870 assert a == ['hi', '1', '5', '3']
1871 assert b == ['1', '3', '5', 'hi']
1872}
1873
1874fn test_sorted_reversed() {
1875 aa := ['hi', '1', '5', '3']
1876 bb := aa.sorted(a > b)
1877 assert aa == ['hi', '1', '5', '3']
1878 assert bb == ['hi', '5', '3', '1']
1879}
1880
1881fn test_sorted_by_len() {
1882 a := ['hi', 'abc', 'a', 'zzzzz']
1883 c := a.sorted(a.len < b.len)
1884 assert c == ['a', 'hi', 'abc', 'zzzzz']
1885}
1886
1887fn test_sorted_can_be_called_on_an_array_literals() {
1888 b := [5, 1, 9].sorted()
1889 assert b == [1, 5, 9]
1890 assert [5.0, 1.2, 9.4].sorted() == [1.2, 5.0, 9.4]
1891 assert ['a', '00', 'z', 'dd', 'xyz'].sorted(a > b) == ['z', 'xyz', 'dd', 'a', '00']
1892 assert ['a', '00', 'zzzzz', 'dddd', 'xyz'].sorted(a.len > b.len) == ['zzzzz', 'dddd', 'xyz',
1893 '00', 'a']
1894 assert ['a', '00', 'zzzzz', 'dddd', 'xyz'].sorted(a.len < b.len) == ['a', '00', 'xyz', 'dddd',
1895 'zzzzz']
1896}
1897
1898fn iarr() []int {
1899 return [5, 1, 9, 1, 2]
1900}
1901
1902fn test_sorted_can_be_called_on_the_result_of_a_fn() {
1903 assert iarr().sorted() == [1, 1, 2, 5, 9]
1904 assert iarr().sorted(a > b) == [9, 5, 2, 1, 1]
1905}
1906
1907fn test_sorting_2d_arrays() {
1908 assert [[1, 2], [3, 4, 5], [2]].sorted(a.len > b.len) == [
1909 [3, 4, 5],
1910 [1, 2],
1911 [2],
1912 ]
1913 assert [[1, 2], [3, 4, 5], [2]].sorted(a.len < b.len) == [
1914 [2],
1915 [1, 2],
1916 [3, 4, 5],
1917 ]
1918 // TODO: sorted with element indexing in ARM64 backend
1919 // assert unsafe { [[1, 2], [3, 4, 5], [2]].sorted(a[0] > b[0]) } == [
1920 // [3, 4, 5],
1921 // [2],
1922 // [1, 2],
1923 // ]
1924 // assert [[1, 2], [3, 4, 5], [2]].sorted( a.reduce(sum) > b.reduce(sum) ) == ... // TODO
1925}
1926
1927fn test_sorting_3d_arrays() {
1928 assert [[][]int{}, [[2, 22], [2]], [[3, 33], [3333], [33, 34, 35]],
1929 [[444]]].sorted(a.len > b.len) == [[[3, 33], [3333], [33, 34, 35]],
1930 [[2, 22], [2]], [[444]], [][]int{}]
1931}
1932
1933fn test_sorted_with_compare() {
1934 aa := ['hi', '1', '5', '3']
1935 bb := aa.sorted_with_compare(fn (a &string, b &string) int {
1936 if a < b {
1937 return -1
1938 }
1939 if a > b {
1940 return 1
1941 }
1942 return 0
1943 })
1944 assert aa == ['hi', '1', '5', '3'], 'aa should stay unmodified'
1945 assert bb == ['1', '3', '5', 'hi'], 'bb should be sorted, according to the custom comparison callback fn'
1946}
1947
1948fn cmp_2d_int_arrays_by_first_item(a &[]int, b &[]int) int {
1949 if a[0] < b[0] {
1950 return -1
1951 }
1952 if a[0] > b[0] {
1953 return 1
1954 }
1955 return 0
1956}
1957
1958struct SortPageObject {
1959 key []u8
1960}
1961
1962fn compare_page_object_keys_asc(a &SortPageObject, b &SortPageObject) int {
1963 return a.key[0] - b.key[0]
1964}
1965
1966fn compare_page_object_keys_desc(a &SortPageObject, b &SortPageObject) int {
1967 return b.key[0] - a.key[0]
1968}
1969
1970fn test_sorted_with_compare_2d_array() {
1971 aa := [[2], [1]]
1972 bb := aa.sorted_with_compare(cmp_2d_int_arrays_by_first_item)
1973 assert aa == [[2], [1]]
1974 assert bb == [[1], [2]]
1975 mut cc := aa.clone()
1976 cc.sort_with_compare(cmp_2d_int_arrays_by_first_item)
1977 assert cc == [[1], [2]]
1978}
1979
1980fn test_sort_with_compare_small_unsigned_difference() {
1981 a := SortPageObject{
1982 key: 'A'.bytes()
1983 }
1984 b := SortPageObject{
1985 key: 'B'.bytes()
1986 }
1987 c := SortPageObject{
1988 key: 'C'.bytes()
1989 }
1990
1991 mut objects1 := [b, a]
1992 objects1.sort_with_compare(compare_page_object_keys_asc)
1993 assert objects1.map(it.key.bytestr()) == ['A', 'B']
1994
1995 mut objects2 := [a, b]
1996 objects2.sort_with_compare(compare_page_object_keys_asc)
1997 assert objects2.map(it.key.bytestr()) == ['A', 'B']
1998
1999 mut objects3 := [a, b, c]
2000 objects3.sort_with_compare(compare_page_object_keys_asc)
2001 assert objects3.map(it.key.bytestr()) == ['A', 'B', 'C']
2002
2003 mut objects4 := [a, b, c]
2004 objects4.sort_with_compare(compare_page_object_keys_desc)
2005 assert objects4.map(it.key.bytestr()) == ['C', 'B', 'A']
2006}
2007