| 1 | fn array_mut_slice(mut a []int) { |
| 2 | assert a[1..3].map(it) == [3, 5] |
| 3 | } |
| 4 | |
| 5 | fn test_array_mut_slice() { |
| 6 | mut a := [1, 3, 5, 7, 9] |
| 7 | array_mut_slice(mut a) |
| 8 | } |
| 9 | |
| 10 | fn test_array_slice_clone() { |
| 11 | arr := [1, 2, 3, 4, 5] |
| 12 | cl := arr[1..].clone() |
| 13 | assert cl == [2, 3, 4, 5] |
| 14 | } |
| 15 | |
| 16 | fn test_array_slice_clone2() { |
| 17 | arr := [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] |
| 18 | cl := arr[1..].clone()[2..].clone() |
| 19 | assert cl == [4, 5, 6, 7, 8, 9, 10] |
| 20 | } |
| 21 | |
| 22 | fn access_slice_attribute(mut arr []int) int { |
| 23 | slice := arr[..arr.len - 1] |
| 24 | return slice.len |
| 25 | } |
| 26 | |
| 27 | fn test_access_slice_attribute() { |
| 28 | mut arr := [1, 2, 3, 4, 5] |
| 29 | assert access_slice_attribute(mut arr) == 4 |
| 30 | } |
| 31 | |
| 32 | fn fixed_array_slice(a [3]int) { |
| 33 | assert a[0..] == [1, 2, 3] |
| 34 | assert a[..a.len] == [1, 2, 3] |
| 35 | } |
| 36 | |
| 37 | fn mut_fixed_array_slice(mut a [3]int) { |
| 38 | assert a[0..] == [1, 2, 3] |
| 39 | assert a[..a.len] == [1, 2, 3] |
| 40 | } |
| 41 | |
| 42 | fn test_fixed_array_slice() { |
| 43 | fixed_array1 := [1, 2, 3]! |
| 44 | arr1 := fixed_array1[0..] |
| 45 | assert arr1 == [1, 2, 3] |
| 46 | fixed_array2 := [[1, 2], [2, 3], [3, 4], [4, 5]]! |
| 47 | arr2 := fixed_array2[0..] |
| 48 | assert arr2 == [[1, 2], [2, 3], [3, 4], [4, 5]] |
| 49 | mut arr := [1, 2, 3]! |
| 50 | fixed_array_slice(arr) |
| 51 | mut_fixed_array_slice(mut arr) |
| 52 | } |
| 53 | |
| 54 | fn pointer_array_slice(mut a []int) { |
| 55 | assert a[0..] == [1, 2, 3] |
| 56 | assert a[..a.len] == [1, 2, 3] |
| 57 | } |
| 58 | |
| 59 | fn test_pointer_array_slice() { |
| 60 | mut arr := [1, 2, 3] |
| 61 | pointer_array_slice(mut arr) |
| 62 | } |
| 63 | |
| 64 | fn test_push_to_orig() { |
| 65 | mut orig := [1, 2, 3, 4] |
| 66 | slice := orig[1..3] |
| 67 | for _ in 0 .. 1000 { |
| 68 | orig << 9 |
| 69 | } |
| 70 | orig[2] = 7 |
| 71 | slice2 := orig[1..3] |
| 72 | assert slice == [2, 3] || slice == [2, 7] |
| 73 | assert slice2 == [2, 7] |
| 74 | } |
| 75 | |
| 76 | fn test_self_slice_push() { |
| 77 | mut a := [1, 2, 3] |
| 78 | a = a[1..] |
| 79 | a << 4 |
| 80 | assert a == [2, 3, 4] |
| 81 | } |
| 82 | |
| 83 | fn test_slice_push_child() { |
| 84 | mut a := [1.0, 2.0625, 3.5, -7.75, 7.125, 8.4375, 0.5] |
| 85 | mut b := unsafe { a[2..6] } // `b` is initially created as reference |
| 86 | mut c := unsafe { b[1..3] } // `c` is initially created as references to `a` and `b` |
| 87 | b << -2.25 // `b` should be reallocated, so `a` doesn't change |
| 88 | c[1] = -13.5 // this should change `c` and `a` but not `b` |
| 89 | assert c == [-7.75, -13.5] |
| 90 | assert a == [1.0, 2.0625, 3.5, -7.75, -13.5, 8.4375, 0.5] |
| 91 | assert b == [3.5, -7.75, 7.125, 8.4375, -2.25] |
| 92 | } |
| 93 | |
| 94 | fn test_predictable_reallocation_parent() { |
| 95 | mut a := []i64{len: 4, cap: 6, init: -25} |
| 96 | mut b := unsafe { a[1..3] } |
| 97 | b[1] = -5238543910438573201 |
| 98 | assert a == [i64(-25), -25, -5238543910438573201, -25] |
| 99 | a << 5 |
| 100 | b[1] = 13 |
| 101 | assert a == [i64(-25), -25, 13, -25, 5] |
| 102 | a << -7 |
| 103 | b[0] = 8 |
| 104 | assert a == [i64(-25), 8, 13, -25, 5, -7] |
| 105 | a << 9 // here `a` will be reallocated as `cap` is exceeded |
| 106 | b[1] = -19 // `a` will not change any more |
| 107 | assert a == [i64(-25), 8, 13, -25, 5, -7, 9] |
| 108 | assert b == [i64(8), -19] |
| 109 | } |
| 110 | |