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