array: vfmt array_test.v

pull/7424/head
Alexander Medvednikov 2020-12-20 10:34:13 +01:00
parent 12d5720841
commit 4a5fb854e0
2 changed files with 132 additions and 162 deletions

View File

@ -59,6 +59,7 @@ const (
'vlib/gg/gg.v', 'vlib/gg/gg.v',
'vlib/os/', 'vlib/os/',
'vlib/time/', 'vlib/time/',
'v/vlib/builtin/array_test.v',
] ]
) )

View File

@ -1,3 +1,5 @@
import sync
fn test_pointer() { fn test_pointer() {
mut arr := []&int{} mut arr := []&int{}
a := 1 a := 1
@ -18,7 +20,6 @@ fn test_pointer() {
fn test_assign() { fn test_assign() {
mut arr := [2, 4, 8, 16, 32, 64, 128] mut arr := [2, 4, 8, 16, 32, 64, 128]
arr[0] = 2 arr[0] = 2
arr[1] &= 255 arr[1] &= 255
arr[2] |= 255 arr[2] |= 255
@ -26,7 +27,6 @@ fn test_assign() {
arr[4] >>= 4 arr[4] >>= 4
arr[5] %= 5 arr[5] %= 5
arr[6] ^= 3 arr[6] ^= 3
assert arr[0] == 2 assert arr[0] == 2
assert arr[1] == 4 & 255 assert arr[1] == 4 & 255
assert arr[2] == 8 | 255 assert arr[2] == 8 | 255
@ -78,7 +78,7 @@ fn test_short() {
fn test_large() { fn test_large() {
mut a := [0].repeat(0) mut a := [0].repeat(0)
for i in 0..10000 { for i in 0 .. 10000 {
a << i a << i
} }
assert a.len == 10000 assert a.len == 10000
@ -137,10 +137,10 @@ fn test_insert() {
fn test_insert_many() { fn test_insert_many() {
mut a := [3, 4] mut a := [3, 4]
a.insert(0, [1, 2]) a.insert(0, [1, 2])
assert a == [1,2,3,4] assert a == [1, 2, 3, 4]
b := [5,6] b := [5, 6]
a.insert(1, b) a.insert(1, b)
assert a == [1,5,6,2,3,4] assert a == [1, 5, 6, 2, 3, 4]
} }
fn test_prepend() { fn test_prepend() {
@ -157,12 +157,12 @@ fn test_prepend() {
} }
fn test_prepend_many() { fn test_prepend_many() {
mut a := [3,4] mut a := [3, 4]
a.prepend([1,2]) a.prepend([1, 2])
assert a == [1,2,3,4] assert a == [1, 2, 3, 4]
b := [5,6] b := [5, 6]
a.prepend(b) a.prepend(b)
assert a == [5,6,1,2,3,4] assert a == [5, 6, 1, 2, 3, 4]
} }
fn test_strings() { fn test_strings() {
@ -183,7 +183,6 @@ fn test_compare_ints() {
assert compare_ints(a, a) == 0 assert compare_ints(a, a) == 0
} }
*/ */
fn test_repeat() { fn test_repeat() {
{ {
a := [0].repeat(5) a := [0].repeat(5)
@ -264,7 +263,6 @@ fn test_slice() {
assert a.len == 4 assert a.len == 4
} }
fn test_push_many() { fn test_push_many() {
mut a := [1, 2, 3] mut a := [1, 2, 3]
b := [4, 5, 6] b := [4, 5, 6]
@ -302,8 +300,8 @@ struct Foooj {
fn test_fixed() { fn test_fixed() {
mut nums := [4]int{} mut nums := [4]int{}
//x := nums[1..3] // x := nums[1..3]
//assert x.len == 2 // assert x.len == 2
assert nums[0] == 0 assert nums[0] == 0
assert nums[1] == 0 assert nums[1] == 0
assert nums[2] == 0 assert nums[2] == 0
@ -333,21 +331,21 @@ fn test_mut_slice() {
fn double_up(mut a []int) { fn double_up(mut a []int) {
for i := 0; i < a.len; i++ { for i := 0; i < a.len; i++ {
a[i] = a[i]*2 a[i] = a[i] * 2
} }
} }
fn double_up_v2(mut a []int) { fn double_up_v2(mut a []int) {
for i, _ in a { for i, _ in a {
a[i] = a[i]*2 // or val*2, doesn't matter a[i] = a[i] * 2 // or val*2, doesn't matter
} }
} }
fn test_mut_arg() { fn test_mut_arg() {
mut arr := [1,2,3,4,5,6,7,8,9,10] mut arr := [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
double_up(mut arr) double_up(mut arr)
assert arr.str() == '[2, 4, 6, 8, 10, 12, 14, 16, 18, 20]' assert arr.str() == '[2, 4, 6, 8, 10, 12, 14, 16, 18, 20]'
arr = [1,2,3,4,5,6,7,8,9,10] arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
double_up_v2(mut arr) double_up_v2(mut arr)
assert arr.str() == '[2, 4, 6, 8, 10, 12, 14, 16, 18, 20]' assert arr.str() == '[2, 4, 6, 8, 10, 12, 14, 16, 18, 20]'
} }
@ -361,51 +359,51 @@ fn test_clone() {
assert nums.slice(1, 3).str() == '[2, 3]' assert nums.slice(1, 3).str() == '[2, 3]'
} }
fn test_copy() {
a := [1, 2, 3]
b := [3, 4, 5]
}
fn test_mutli_array_clone() { fn test_mutli_array_clone() {
// 2d array_int // 2d array_int
mut a2_1 := [[1, 2, 3], [4, 5, 6]] mut a2_1 := [[1, 2, 3], [4, 5, 6]]
mut a2_2 := a2_1.clone() mut a2_2 := a2_1.clone()
a2_1[0][1] = 0
a2_1[0][1] = 0 a2_2[1][0] = 0
a2_2[1][0] = 0 assert a2_1 == [[1, 0, 3], [4, 5, 6]]
assert a2_2 == [[1, 2, 3], [0, 5, 6]]
assert a2_1 == [[1, 0, 3], [4, 5, 6]] // 2d array_string
assert a2_2 == [[1, 2, 3], [0, 5, 6]] mut b2_1 := [['1', '2', '3'], ['4', '5', '6']]
mut b2_2 := b2_1.clone()
// 2d array_string b2_1[0][1] = '0'
mut b2_1 := [['1', '2', '3'], ['4', '5', '6']] b2_2[1][0] = '0'
mut b2_2 := b2_1.clone() assert b2_1 == [['1', '0', '3'], ['4', '5', '6']]
assert b2_2 == [['1', '2', '3'], ['0', '5', '6']]
b2_1[0][1] = '0' // 3d array_int
b2_2[1][0] = '0' mut a3_1 := [[[1, 1], [2, 2], [3, 3]],
[[4, 4], [5, 5], [6, 6]],
assert b2_1 == [['1', '0', '3'], ['4', '5', '6']] ]
assert b2_2 == [['1', '2', '3'], ['0', '5', '6']] mut a3_2 := a3_1.clone()
a3_1[0][0][1] = 0
// 3d array_int a3_2[0][1][0] = 0
mut a3_1 := [[[1,1], [2,2], [3,3]], [[4,4], [5,5], [6,6]]] assert a3_1 == [[[1, 0], [2, 2], [3, 3]], [[4, 4], [5, 5], [6, 6]]]
mut a3_2 := a3_1.clone() assert a3_2 == [[[1, 1], [0, 2], [3, 3]], [[4, 4], [5, 5], [6, 6]]]
// 3d array_string
a3_1[0][0][1] = 0 mut b3_1 := [[['1', '1'], ['2', '2'],
a3_2[0][1][0] = 0 ['3', '3'],
], [['4', '4'], ['5', '5'], ['6', '6']]]
assert a3_1 == [[[1, 0], [2, 2], [3, 3]], [[4, 4], [5, 5], [6, 6]]] mut b3_2 := b3_1.clone()
assert a3_2 == [[[1, 1], [0, 2], [3, 3]], [[4, 4], [5, 5], [6, 6]]] b3_1[0][0][1] = '0'
b3_2[0][1][0] = '0'
// 3d array_string assert b3_1 ==
mut b3_1 := [[['1','1'], ['2','2'], ['3','3']], [['4','4'], ['5','5'], ['6','6']]] [[['1', '0'], ['2', '2'], ['3', '3']], [['4', '4'], ['5', '5'], ['6', '6']]]
mut b3_2 := b3_1.clone() assert b3_2 ==
[[['1', '1'], ['0', '2'], ['3', '3']], [['4', '4'], ['5', '5'], ['6', '6']]]
b3_1[0][0][1] = '0'
b3_2[0][1][0] = '0'
assert b3_1 == [[['1','0'], ['2','2'], ['3','3']], [['4','4'], ['5','5'], ['6','6']]]
assert b3_2 == [[['1','1'], ['0','2'], ['3','3']], [['4','4'], ['5','5'], ['6','6']]]
} }
fn test_doubling() { fn test_doubling() {
mut nums := [1, 2, 3, 4, 5] mut nums := [1, 2, 3, 4, 5]
for i in 0..nums.len { for i in 0 .. nums.len {
nums[i] *= 2 nums[i] *= 2
} }
println(nums.str()) println(nums.str())
@ -428,7 +426,7 @@ fn (ta []Test2) str() string {
mut s := '[' mut s := '['
for i, t in ta { for i, t in ta {
s += t.str() s += t.str()
if i < ta.len-1 { if i < ta.len - 1 {
s += ', ' s += ', '
} }
} }
@ -543,7 +541,6 @@ fn test_reduce() {
assert g == -7 assert g == -7
} }
*/ */
fn filter_test_helper_1(a int) bool { fn filter_test_helper_1(a int) bool {
return a > 3 return a > 3
} }
@ -560,41 +557,41 @@ fn test_filter() {
assert d[0] == 'is' assert d[0] == 'is'
assert d[1] == 'awesome' assert d[1] == 'awesome'
//////// ////////
arr :=[1,2,3,4,5,6,7,8,9,10] arr := [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
println(arr.filter(it % 2 == 0 || it % 3 == 0)) println(arr.filter(it % 2 == 0 || it % 3 == 0))
assert true assert true
assert [1,2,3].len == 3 assert [1, 2, 3].len == 3
mut mut_arr := [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] mut mut_arr := [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
mut_arr = mut_arr.filter(it < 4) mut_arr = mut_arr.filter(it < 4)
assert mut_arr.len == 3 assert mut_arr.len == 3
assert a.filter(filter_test_helper_1) == [4,5,6] assert a.filter(filter_test_helper_1) == [4, 5, 6]
assert [1,5,10].filter(filter_test_helper_1) == [5,10] assert [1, 5, 10].filter(filter_test_helper_1) == [5, 10]
// TODO // TODO
//assert arr.filter(arr % 2).len == 5 // assert arr.filter(arr % 2).len == 5
} }
fn test_anon_fn_filter() { fn test_anon_fn_filter() {
filter_num := fn (i int) bool { filter_num := fn (i int) bool {
return i % 2 == 0 return i % 2 == 0
} }
assert [1,2,3,4,5].filter(filter_num) == [2,4] assert [1, 2, 3, 4, 5].filter(filter_num) == [2, 4]
} }
fn test_anon_fn_arg_filter() { fn test_anon_fn_arg_filter() {
a := [1,2,3,4].filter(fn (i int) bool { a := [1, 2, 3, 4].filter(fn (i int) bool {
return i % 2 == 0 return i % 2 == 0
}) })
assert a == [2, 4]
assert a == [2,4]
} }
fn map_test_helper_1(i int) int { fn map_test_helper_1(i int) int {
return i * i return i * i
} }
fn map_test_helper_2(i int, b string) int { fn map_test_helper_2(i int, b string) int {
return i + b.len return i + b.len
} }
fn map_test_helper_3(i int, b []string) int { fn map_test_helper_3(i int, b []string) int {
return i + b.map(it.len)[i % b.len] return i + b.map(it.len)[i % b.len]
} }
@ -602,49 +599,39 @@ fn map_test_helper_3(i int, b []string) int {
fn test_map() { fn test_map() {
nums := [1, 2, 3, 4, 5, 6] nums := [1, 2, 3, 4, 5, 6]
strs := ['v', 'is', 'awesome'] strs := ['v', 'is', 'awesome']
// assert nums.map() == <error>
//assert nums.map() == <error> // assert nums.map(it, 'excessive') == <error>
//assert nums.map(it, 'excessive') == <error>
// identity // identity
assert nums.map(it) == [1, 2, 3, 4, 5, 6] assert nums.map(it) == [1, 2, 3, 4, 5, 6]
assert strs.map(it) == ['v', 'is', 'awesome'] assert strs.map(it) == ['v', 'is', 'awesome']
assert nums.map(it - it) == [0,0,0,0,0,0] assert nums.map(it - it) == [0, 0, 0, 0, 0, 0]
assert nums.map(it - it)[0] == 0 assert nums.map(it - it)[0] == 0
// type switch // type switch
assert nums.map(it * 10) == [10, 20, 30, 40, 50, 60] assert nums.map(it * 10) == [10, 20, 30, 40, 50, 60]
assert nums.map(it * it) == [1, 4, 9, 16, 25, 36] assert nums.map(it * it) == [1, 4, 9, 16, 25, 36]
assert nums.map('$it') == ['1', '2', '3', '4', '5', '6'] assert nums.map('$it') == ['1', '2', '3', '4', '5', '6']
assert nums.map(it % 2 == 0) == [false, true, false, true, false, true] assert nums.map(it % 2 == 0) == [false, true, false, true, false, true]
assert strs.map(it.to_upper()) == ['V', 'IS', 'AWESOME'] assert strs.map(it.to_upper()) == ['V', 'IS', 'AWESOME']
assert strs.map(it == 'awesome') == [false, false, true] assert strs.map(it == 'awesome') == [false, false, true]
assert strs.map(it.len in nums) == [true, true, false] assert strs.map(it.len in nums) == [true, true, false]
assert strs.map(int(7)) == [7, 7, 7] assert strs.map(int(7)) == [7, 7, 7]
// external func // external func
assert nums.map(map_test_helper_1(it)) == [1, 4, 9, 16, 25, 36] assert nums.map(map_test_helper_1(it)) == [1, 4, 9, 16, 25, 36]
assert nums.map(map_test_helper_2(it, 'bb')) == [3, 4, 5, 6, 7, 8] assert nums.map(map_test_helper_2(it, 'bb')) == [3, 4, 5, 6, 7, 8]
assert nums.map(map_test_helper_3(it, strs)) == [3, 9, 4, 6, 12, 7] assert nums.map(map_test_helper_3(it, strs)) == [3, 9, 4, 6, 12, 7]
// empty array as input // empty array as input
assert []int{len:0}.map(it * 2) == [] assert []int{len: 0}.map(it * 2) == []
// nested maps (where it is of same type) // nested maps (where it is of same type)
assert nums.map( strs.map(int(7)) == [7, 7, 7] ) == [true, true, true, true, true, true] assert nums.map(strs.map(int(7)) == [7, 7, 7]) == [true, true, true, true, true, true]
assert nums.map( '$it' + strs.map('a')[0] ) == ['1a', '2a', '3a', '4a', '5a', '6a'] assert nums.map('$it' + strs.map('a')[0]) == ['1a', '2a', '3a', '4a', '5a', '6a']
assert nums.map( it + strs.map(int(7))[0] ) == [8, 9, 10, 11, 12, 13] assert nums.map(it + strs.map(int(7))[0]) == [8, 9, 10, 11, 12, 13]
assert nums.map( it + strs.map(it.len)[0] ) == [2, 3, 4, 5, 6, 7] assert nums.map(it + strs.map(it.len)[0]) == [2, 3, 4, 5, 6, 7]
assert strs.map( it.len + strs.map(it.len)[0] ) == [2, 3, 8] assert strs.map(it.len + strs.map(it.len)[0]) == [2, 3, 8]
// nested (different it types) // nested (different it types)
assert strs.map( it[ nums.map(it - it)[0] ] ) == [byte(`v`), `i`, `a`] assert strs.map(it[nums.map(it - it)[0]]) == [byte(`v`), `i`, `a`]
assert nums[0..3].map('$it' + strs.map(it)[it-1]) == ['1v','2is','3awesome'] assert nums[0..3].map('$it' + strs.map(it)[it - 1]) == ['1v', '2is', '3awesome']
assert nums.map(map_test_helper_1) == [1, 4, 9, 16, 25, 36]
assert nums.map(map_test_helper_1) == [1,4,9,16,25,36] assert [1, 5, 10].map(map_test_helper_1) == [1, 25, 100]
assert [1,5,10].map(map_test_helper_1) == [1,25,100]
assert nums == [1, 2, 3, 4, 5, 6] assert nums == [1, 2, 3, 4, 5, 6]
assert strs == ['v', 'is', 'awesome'] assert strs == ['v', 'is', 'awesome']
} }
@ -653,27 +640,25 @@ fn test_anon_fn_map() {
add_num := fn (i int) int { add_num := fn (i int) int {
return i + 1 return i + 1
} }
assert [1,2,3].map(add_num) == [2,3,4] assert [1, 2, 3].map(add_num) == [2, 3, 4]
} }
fn test_mutli_anon_fn_map() { fn test_mutli_anon_fn_map() {
a := [1,2,3].map(fn (i int) int { a := [1, 2, 3].map(fn (i int) int {
return i + 1 return i + 1
}) })
b := [1, 2, 3].map(fn (i int) int {
b := [1,2,3].map(fn (i int) int {
return i + 2 return i + 2
}) })
assert a == [2,3,4] assert a == [2, 3, 4]
assert b == [3,4,5] assert b == [3, 4, 5]
} }
fn test_anon_fn_arg_map() { fn test_anon_fn_arg_map() {
a := [1,2,3].map(fn (i int) int { a := [1, 2, 3].map(fn (i int) int {
return i + 1 return i + 1
}) })
assert a == [2, 3, 4]
assert a == [2,3,4]
} }
fn test_anon_fn_arg_different_type_map() { fn test_anon_fn_arg_different_type_map() {
@ -681,7 +666,6 @@ fn test_anon_fn_arg_different_type_map() {
return i.str() return i.str()
} }
a := [1, 2, 3].map(i_to_str) a := [1, 2, 3].map(i_to_str)
assert a == ['1', '2', '3'] assert a == ['1', '2', '3']
} }
@ -689,20 +673,18 @@ fn test_anon_fn_inline_different_type_map() {
a := [1, 2, 3].map(fn (i int) string { a := [1, 2, 3].map(fn (i int) string {
return i.str() return i.str()
}) })
assert a == ['1', '2', '3'] assert a == ['1', '2', '3']
} }
fn test_array_str() { fn test_array_str() {
numbers := [1, 2, 3] numbers := [1, 2, 3]
assert numbers == [1,2,3] assert numbers == [1, 2, 3]
numbers2 := [numbers, [4, 5, 6]] // dup str() bug numbers2 := [numbers, [4, 5, 6]] // dup str() bug
_ = numbers2 _ = numbers2
assert true assert true
assert numbers.str() == '[1, 2, 3]' assert numbers.str() == '[1, 2, 3]'
// QTODO // QTODO
//assert numbers2.str() == '[[1, 2, 3], [4, 5, 6]]' // assert numbers2.str() == '[[1, 2, 3], [4, 5, 6]]'
} }
fn test_eq() { fn test_eq() {
@ -713,7 +695,7 @@ fn test_eq() {
} }
struct User { struct User {
age int age int
name string name string
} }
@ -740,22 +722,23 @@ fn test_sort() {
assert nums[3] == 67 assert nums[3] == 67
assert nums[4] == 108 assert nums[4] == 108
// //
mut users := [User{22, 'Peter'}, User{20, 'Bob'}, User{25, 'Alice'}] mut users := [User{22, 'Peter'},
User{20, 'Bob'}, User{25, 'Alice'}]
users.sort(a.age < b.age) users.sort(a.age < b.age)
assert(users[0].age == 20) assert (users[0].age == 20)
assert(users[1].age == 22) assert (users[1].age == 22)
assert(users[2].age == 25) assert (users[2].age == 25)
assert(users[0].name == 'Bob') assert (users[0].name == 'Bob')
assert(users[1].name == 'Peter') assert (users[1].name == 'Peter')
assert(users[2].name == 'Alice') assert (users[2].name == 'Alice')
// //
users.sort(a.age > b.age) users.sort(a.age > b.age)
assert(users[0].age == 25) assert (users[0].age == 25)
assert(users[1].age == 22) assert (users[1].age == 22)
assert(users[2].age == 20) assert (users[2].age == 20)
// //
users.sort(a.name < b.name) // Test sorting by string fields users.sort(a.name < b.name) // Test sorting by string fields
//assert users.map(it.name).join(' ') == 'Alice Bob Peter' // assert users.map(it.name).join(' ') == 'Alice Bob Peter'
} }
fn test_f32_sort() { fn test_f32_sort() {
@ -797,8 +780,6 @@ fn test_for_last() {
assert s == '[1, 2, 3, 4]' assert s == '[1, 2, 3, 4]'
} }
*/ */
struct Foo { struct Foo {
mut: mut:
bar []int bar []int
@ -872,17 +853,17 @@ fn test_bools() {
} }
fn test_push_many_self() { fn test_push_many_self() {
mut actual_arr := [1, 2, 3, 4] mut actual_arr := [1, 2, 3, 4]
actual_arr << actual_arr actual_arr << actual_arr
expected_arr := [1, 2, 3, 4, 1, 2, 3, 4] expected_arr := [1, 2, 3, 4, 1, 2, 3, 4]
assert actual_arr.len == expected_arr.len assert actual_arr.len == expected_arr.len
for i in 0..actual_arr.len { for i in 0 .. actual_arr.len {
assert actual_arr[i] == expected_arr[i] assert actual_arr[i] == expected_arr[i]
} }
} }
fn test_for() { fn test_for() {
nums := [1,2,3] nums := [1, 2, 3]
mut sum := 0 mut sum := 0
for num in nums { for num in nums {
sum += num sum += num
@ -891,11 +872,10 @@ fn test_for() {
} }
fn test_clear() { fn test_clear() {
mut arr := [1,2,3] mut arr := [1, 2, 3]
assert arr.len == 3 assert arr.len == 3
arr.clear() arr.clear()
assert arr.len == 0 assert arr.len == 0
arr << 3 arr << 3
arr << 2 arr << 2
arr << 1 arr << 1
@ -905,36 +885,31 @@ fn test_clear() {
assert arr[1] == 2 assert arr[1] == 2
assert arr[2] == 1 assert arr[2] == 1
assert arr[3] == 0 assert arr[3] == 0
arr.clear() arr.clear()
assert arr.len == 0 assert arr.len == 0
} }
fn test_trim() { fn test_trim() {
mut arr := [1,2,3,4,5,6,7,8,9] mut arr := [1, 2, 3, 4, 5, 6, 7, 8, 9]
assert arr.len == 9 assert arr.len == 9
arr.trim(9) arr.trim(9)
assert arr.len == 9 assert arr.len == 9
assert arr.last() == 9 assert arr.last() == 9
arr.trim(7) arr.trim(7)
assert arr.len == 7 assert arr.len == 7
assert arr.last() == 7 assert arr.last() == 7
arr.trim(2) arr.trim(2)
assert arr.len == 2 assert arr.len == 2
assert arr.last() == 2 assert arr.last() == 2
} }
fn test_hex(){ fn test_hex() {
// array hex // array hex
st := [byte(`V`),`L`,`A`,`N`,`G`] st := [byte(`V`), `L`, `A`, `N`, `G`]
assert st.hex() == "564c414e47" assert st.hex() == '564c414e47'
assert st.hex().len == 10 assert st.hex().len == 10
st1 := [byte(0x41)].repeat(100) st1 := [byte(0x41)].repeat(100)
assert st1.hex() == "41".repeat(100) assert st1.hex() == '41'.repeat(100)
} }
fn test_left_shift_precendence() { fn test_left_shift_precendence() {
@ -943,7 +918,6 @@ fn test_left_shift_precendence() {
arr << 1 - 1 arr << 1 - 1
arr << 2 / 1 arr << 2 / 1
arr << 2 * 1 arr << 2 * 1
assert arr[0] == 2 assert arr[0] == 2
assert arr[1] == 0 assert arr[1] == 0
assert arr[2] == 2 assert arr[2] == 2
@ -951,19 +925,18 @@ fn test_left_shift_precendence() {
} }
fn test_array_with_cap() { fn test_array_with_cap() {
a4 := []int{cap:10, len:1 } a4 := []int{len: 1, cap: 10}
assert a4.len == 1 assert a4.len == 1
assert a4.cap == 10 assert a4.cap == 10
a5 := []int{len:1, cap:10} a5 := []int{len: 1, cap: 10}
assert a5.len == 1 assert a5.len == 1
assert a5.cap == 10 assert a5.cap == 10
} }
fn test_mutli_array_index() { fn test_mutli_array_index() {
mut a := [][]int{len:2, init: []int{len:3, init:0}} mut a := [][]int{len: 2, init: []int{len: 3, init: 0}}
a[0][0] = 1 a[0][0] = 1
assert '$a' == '[[1, 0, 0], [0, 0, 0]]' assert '$a' == '[[1, 0, 0], [0, 0, 0]]'
mut b := [[0].repeat(3)].repeat(2) mut b := [[0].repeat(3)].repeat(2)
b[0][0] = 1 b[0][0] = 1
assert '$b' == '[[1, 0, 0], [0, 0, 0]]' assert '$b' == '[[1, 0, 0], [0, 0, 0]]'
@ -976,24 +949,24 @@ fn test_plus_assign_string() {
} }
fn mut_arr_with_eq_in_fn(mut a []int) { fn mut_arr_with_eq_in_fn(mut a []int) {
if a == [1,2,3,4] { if a == [1, 2, 3, 4] {
a[0] = 0 a[0] = 0
} }
if [0,2,3,4] == a { if [0, 2, 3, 4] == a {
a[1] = 0 a[1] = 0
} }
if !(a != [0,0,3,4]) { if !(a != [0, 0, 3, 4]) {
a[2] = 0 a[2] = 0
} }
if !([0,0,0,4] != a) { if !([0, 0, 0, 4] != a) {
a[3] = 0 a[3] = 0
} }
} }
fn test_mut_arr_with_eq_in_fn() { fn test_mut_arr_with_eq_in_fn() {
mut a := [1,2,3,4] mut a := [1, 2, 3, 4]
mut_arr_with_eq_in_fn(mut a) mut_arr_with_eq_in_fn(mut a)
assert a == [0,0,0,0] assert a == [0, 0, 0, 0]
} }
fn array_in_mut(mut a []int) { fn array_in_mut(mut a []int) {
@ -1003,9 +976,9 @@ fn array_in_mut(mut a []int) {
} }
fn test_array_in_mut() { fn test_array_in_mut() {
mut a := [1,2] mut a := [1, 2]
array_in_mut(mut a) array_in_mut(mut a)
assert a == [2,2] assert a == [2, 2]
} }
// test array delete in function with mut argument // test array delete in function with mut argument
@ -1034,18 +1007,16 @@ fn test_reverse_in_place() {
mut a := [1, 2, 3, 4] mut a := [1, 2, 3, 4]
a.reverse_in_place() a.reverse_in_place()
assert a == [4, 3, 2, 1] assert a == [4, 3, 2, 1]
mut b := ['a', 'b', 'c'] mut b := ['a', 'b', 'c']
b.reverse_in_place() b.reverse_in_place()
assert b == ['c', 'b', 'a'] assert b == ['c', 'b', 'a']
mut c := [[1, 2], [3, 4], [5, 6]] mut c := [[1, 2], [3, 4], [5, 6]]
c.reverse_in_place() c.reverse_in_place()
assert c == [[5, 6], [3, 4], [1, 2]] assert c == [[5, 6], [3, 4], [1, 2]]
} }
fn test_array_int_pop() { fn test_array_int_pop() {
mut a := [1,2,3,4,5] mut a := [1, 2, 3, 4, 5]
assert a.len == 5 assert a.len == 5
x := a.last() x := a.last()
y := a.pop() y := a.pop()
@ -1094,7 +1065,7 @@ fn test_array_last() {
[direct_array_access] [direct_array_access]
fn test_direct_array_access() { fn test_direct_array_access() {
mut a := [11,22,33,44] mut a := [11, 22, 33, 44]
assert a[0] == 11 assert a[0] == 11
assert a[2] == 33 assert a[2] == 33
x := a[0] x := a[0]
@ -1107,7 +1078,7 @@ fn test_direct_array_access() {
[direct_array_access] [direct_array_access]
fn test_direct_array_access_via_ptr() { fn test_direct_array_access_via_ptr() {
mut b := [11,22,33,44] mut b := [11, 22, 33, 44]
unsafe { unsafe {
mut a := &b mut a := &b
assert a[0] == 11 assert a[0] == 11
@ -1186,7 +1157,6 @@ fn test_multi_array_prepend() {
mut a := [][]int{} mut a := [][]int{}
a.prepend([1, 2, 3]) a.prepend([1, 2, 3])
assert a == [[1, 2, 3]] assert a == [[1, 2, 3]]
mut b := [][]int{} mut b := [][]int{}
b.prepend([[1, 2, 3]]) b.prepend([[1, 2, 3]])
assert b == [[1, 2, 3]] assert b == [[1, 2, 3]]
@ -1196,7 +1166,6 @@ fn test_multi_array_insert() {
mut a := [][]int{} mut a := [][]int{}
a.insert(0, [1, 2, 3]) a.insert(0, [1, 2, 3])
assert a == [[1, 2, 3]] assert a == [[1, 2, 3]]
mut b := [][]int{} mut b := [][]int{}
b.insert(0, [[1, 2, 3]]) b.insert(0, [[1, 2, 3]])
assert b == [[1, 2, 3]] assert b == [[1, 2, 3]]