2019-12-18 03:26:04 +03:00
|
|
|
fn test_pointer() {
|
2020-04-26 17:25:54 +03:00
|
|
|
mut arr := []&int{}
|
2019-12-18 03:26:04 +03:00
|
|
|
a := 1
|
|
|
|
b := 2
|
|
|
|
c := 3
|
|
|
|
arr << &a
|
|
|
|
arr << &b
|
|
|
|
arr << &c
|
|
|
|
assert *arr[0] == 1
|
|
|
|
arr[1] = &c
|
|
|
|
assert *arr[1] == 3
|
2019-12-22 01:44:16 +03:00
|
|
|
mut d_arr := [arr] // [][]&int
|
|
|
|
d_arr << arr
|
|
|
|
assert *d_arr[0][1] == 3
|
2020-03-27 00:09:25 +03:00
|
|
|
println(*d_arr[0][1])
|
2019-12-22 01:44:16 +03:00
|
|
|
assert *d_arr[1][0] == 1
|
2019-12-18 03:26:04 +03:00
|
|
|
}
|
|
|
|
|
2020-04-14 21:32:06 +03:00
|
|
|
fn test_assign() {
|
2020-04-26 12:56:27 +03:00
|
|
|
mut arr := [2, 4, 8, 16, 32, 64, 128]
|
2020-04-14 21:32:06 +03:00
|
|
|
|
|
|
|
arr[0] = 2
|
|
|
|
arr[1] &= 255
|
|
|
|
arr[2] |= 255
|
|
|
|
arr[3] <<= 4
|
|
|
|
arr[4] >>= 4
|
|
|
|
arr[5] %= 5
|
|
|
|
arr[6] ^= 3
|
|
|
|
|
|
|
|
assert arr[0] == 2
|
|
|
|
assert arr[1] == 4 & 255
|
|
|
|
assert arr[2] == 8 | 255
|
|
|
|
assert arr[3] == 16 << 4
|
|
|
|
assert arr[4] == 32 >> 4
|
|
|
|
assert arr[5] == 64 % 5
|
|
|
|
assert arr[6] == 128 ^ 3
|
|
|
|
}
|
|
|
|
|
2019-06-25 16:31:56 +03:00
|
|
|
fn test_ints() {
|
|
|
|
mut a := [1, 5, 2, 3]
|
|
|
|
assert a.len == 4
|
|
|
|
assert a[0] == 1
|
|
|
|
assert a[2] == 2
|
|
|
|
assert a.last() == 3
|
|
|
|
a << 4
|
|
|
|
assert a.len == 5
|
|
|
|
assert a[4] == 4
|
|
|
|
assert a.last() == 4
|
2019-07-25 15:06:26 +03:00
|
|
|
s := a.str()
|
2019-06-25 16:31:56 +03:00
|
|
|
assert s == '[1, 5, 2, 3, 4]'
|
|
|
|
assert a[1] == 5
|
|
|
|
assert a.last() == 4
|
2019-06-29 23:49:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_deleting() {
|
|
|
|
mut a := [1, 5, 2, 3, 4]
|
|
|
|
assert a.len == 5
|
|
|
|
assert a.str() == '[1, 5, 2, 3, 4]'
|
2019-06-25 16:31:56 +03:00
|
|
|
a.delete(0)
|
|
|
|
assert a.str() == '[5, 2, 3, 4]'
|
2019-06-29 23:49:57 +03:00
|
|
|
assert a.len == 4
|
2019-06-25 16:31:56 +03:00
|
|
|
a.delete(1)
|
|
|
|
assert a.str() == '[5, 3, 4]'
|
2019-06-29 23:49:57 +03:00
|
|
|
assert a.len == 3
|
2020-01-21 20:49:30 +03:00
|
|
|
a.delete(a.len - 1)
|
2019-10-31 21:50:20 +03:00
|
|
|
assert a.str() == '[5, 3]'
|
|
|
|
assert a.len == 2
|
2019-06-25 16:31:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_short() {
|
|
|
|
a := [1, 2, 3]
|
|
|
|
assert a.len == 3
|
|
|
|
assert a.cap == 3
|
|
|
|
assert a[0] == 1
|
|
|
|
assert a[1] == 2
|
|
|
|
assert a[2] == 3
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_large() {
|
2019-09-15 12:26:05 +03:00
|
|
|
mut a := [0].repeat(0)
|
2020-02-24 19:55:16 +03:00
|
|
|
for i in 0..10000 {
|
2019-06-25 16:31:56 +03:00
|
|
|
a << i
|
|
|
|
}
|
|
|
|
assert a.len == 10000
|
|
|
|
assert a[234] == 234
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Chunk {
|
|
|
|
val string
|
|
|
|
}
|
|
|
|
|
2020-05-27 19:12:34 +03:00
|
|
|
struct Kkk {
|
2019-06-25 16:31:56 +03:00
|
|
|
q []Chunk
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_empty() {
|
2020-04-26 12:56:27 +03:00
|
|
|
mut chunks := []Chunk{}
|
2019-06-25 16:31:56 +03:00
|
|
|
a := Chunk{}
|
|
|
|
assert chunks.len == 0
|
|
|
|
chunks << a
|
|
|
|
assert chunks.len == 1
|
2019-11-14 09:53:05 +03:00
|
|
|
chunks = []
|
2019-06-25 16:31:56 +03:00
|
|
|
assert chunks.len == 0
|
|
|
|
chunks << a
|
|
|
|
assert chunks.len == 1
|
|
|
|
}
|
2019-06-29 23:49:57 +03:00
|
|
|
|
2019-06-25 16:31:56 +03:00
|
|
|
fn test_push() {
|
2020-04-26 12:56:27 +03:00
|
|
|
mut a := []int{}
|
2019-06-25 16:31:56 +03:00
|
|
|
a << 1
|
|
|
|
a << 3
|
|
|
|
assert a[1] == 3
|
|
|
|
assert a.str() == '[1, 3]'
|
|
|
|
}
|
|
|
|
|
2019-11-11 17:18:32 +03:00
|
|
|
fn test_insert() {
|
2020-01-21 20:49:30 +03:00
|
|
|
mut a := [1, 2]
|
|
|
|
a.insert(0, 3)
|
2020-06-18 01:22:34 +03:00
|
|
|
assert a[0] == 3
|
|
|
|
assert a[2] == 2
|
|
|
|
assert a.len == 3
|
|
|
|
a.insert(1, 4)
|
|
|
|
assert a[1] == 4
|
|
|
|
assert a[2] == 1
|
|
|
|
assert a.len == 4
|
|
|
|
a.insert(4, 5)
|
|
|
|
assert a[4] == 5
|
|
|
|
assert a[3] == 2
|
|
|
|
assert a.len == 5
|
|
|
|
mut b := []f64{}
|
|
|
|
assert b.len == 0
|
|
|
|
b.insert(0, f64(1.1))
|
|
|
|
assert b.len == 1
|
|
|
|
assert b[0] == f64(1.1)
|
2020-01-21 20:49:30 +03:00
|
|
|
}
|
2020-06-18 01:22:34 +03:00
|
|
|
|
2020-06-18 13:08:11 +03:00
|
|
|
fn test_insert_many() {
|
|
|
|
mut a := [3, 4]
|
|
|
|
a.insert(0, [1, 2])
|
|
|
|
assert a == [1,2,3,4]
|
|
|
|
b := [5,6]
|
|
|
|
a.insert(1, b)
|
|
|
|
assert a == [1,5,6,2,3,4]
|
|
|
|
}
|
|
|
|
|
2020-06-18 01:22:34 +03:00
|
|
|
fn test_prepend() {
|
|
|
|
mut a := []int{}
|
|
|
|
assert a.len == 0
|
|
|
|
a.prepend(1)
|
|
|
|
assert a.len == 1
|
|
|
|
assert a[0] == 1
|
|
|
|
mut b := []f64{}
|
|
|
|
assert b.len == 0
|
|
|
|
b.prepend(f64(1.1))
|
|
|
|
assert b.len == 1
|
|
|
|
assert b[0] == f64(1.1)
|
|
|
|
}
|
|
|
|
|
2020-06-18 13:08:11 +03:00
|
|
|
fn test_prepend_many() {
|
|
|
|
mut a := [3,4]
|
|
|
|
a.prepend([1,2])
|
|
|
|
assert a == [1,2,3,4]
|
|
|
|
b := [5,6]
|
|
|
|
a.prepend(b)
|
|
|
|
assert a == [5,6,1,2,3,4]
|
|
|
|
}
|
|
|
|
|
2019-06-25 16:31:56 +03:00
|
|
|
fn test_strings() {
|
|
|
|
a := ['a', 'b', 'c']
|
2020-04-27 15:48:28 +03:00
|
|
|
assert a.str() == "['a', 'b', 'c']"
|
2019-06-25 16:31:56 +03:00
|
|
|
}
|
|
|
|
|
2020-01-21 20:49:30 +03:00
|
|
|
/*
|
2019-10-31 21:50:20 +03:00
|
|
|
fn test_compare_ints() {
|
|
|
|
assert compare_ints(1, 2) == -1
|
|
|
|
assert compare_ints(2, 1) == 1
|
|
|
|
assert compare_ints(0, 0) == 0
|
2019-10-31 15:47:56 +03:00
|
|
|
|
2019-10-31 21:50:20 +03:00
|
|
|
a := 1
|
|
|
|
b := 2
|
|
|
|
assert compare_ints(a, b) == -1
|
|
|
|
assert compare_ints(b, a) == 1
|
|
|
|
assert compare_ints(a, a) == 0
|
|
|
|
}
|
2020-01-21 20:49:30 +03:00
|
|
|
*/
|
|
|
|
|
2019-10-31 21:50:20 +03:00
|
|
|
fn test_repeat() {
|
2019-07-04 11:46:39 +03:00
|
|
|
{
|
2019-10-31 21:50:20 +03:00
|
|
|
a := [0].repeat(5)
|
|
|
|
assert a.len == 5
|
|
|
|
assert a[0] == 0 && a[1] == 0 && a[2] == 0 && a[3] == 0 && a[4] == 0
|
|
|
|
}
|
|
|
|
{
|
|
|
|
a := [1.1].repeat(10)
|
2020-01-21 20:49:30 +03:00
|
|
|
assert a[0] == 1.1
|
|
|
|
assert a[5] == 1.1
|
|
|
|
assert a[9] == 1.1
|
2019-10-31 21:50:20 +03:00
|
|
|
}
|
|
|
|
{
|
2020-01-21 20:49:30 +03:00
|
|
|
a := [i64(-123)].repeat(10)
|
|
|
|
assert a[0] == -123
|
|
|
|
assert a[5] == -123
|
|
|
|
assert a[9] == -123
|
|
|
|
}
|
|
|
|
{
|
|
|
|
a := [u64(123)].repeat(10)
|
|
|
|
assert a[0] == 123
|
|
|
|
assert a[5] == 123
|
|
|
|
assert a[9] == 123
|
2019-10-31 21:50:20 +03:00
|
|
|
}
|
|
|
|
{
|
2020-03-19 09:24:49 +03:00
|
|
|
a := [1.1].repeat(10)
|
|
|
|
assert a[0] == 1.1
|
|
|
|
assert a[5] == 1.1
|
|
|
|
assert a[9] == 1.1
|
2019-10-31 21:50:20 +03:00
|
|
|
}
|
|
|
|
{
|
|
|
|
a := [1, 2].repeat(2)
|
|
|
|
assert a[0] == 1
|
|
|
|
assert a[1] == 2
|
|
|
|
assert a[2] == 1
|
|
|
|
assert a[3] == 2
|
2019-07-04 11:46:39 +03:00
|
|
|
}
|
|
|
|
{
|
2019-10-31 21:50:20 +03:00
|
|
|
a := ['1', 'abc'].repeat(2)
|
|
|
|
assert a[0] == '1'
|
|
|
|
assert a[1] == 'abc'
|
|
|
|
assert a[2] == '1'
|
|
|
|
assert a[3] == 'abc'
|
2019-07-04 11:46:39 +03:00
|
|
|
}
|
|
|
|
{
|
2019-10-31 21:50:20 +03:00
|
|
|
mut a := ['1', 'abc'].repeat(0)
|
|
|
|
assert a.len == 0
|
|
|
|
a << 'abc'
|
|
|
|
assert a[0] == 'abc'
|
2019-07-04 11:46:39 +03:00
|
|
|
}
|
2019-06-25 16:31:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_right() {
|
|
|
|
a := [1, 2, 3, 4]
|
2019-10-27 01:37:29 +03:00
|
|
|
c := a[1..a.len]
|
2019-10-27 09:36:04 +03:00
|
|
|
d := a[1..]
|
2019-10-27 01:37:29 +03:00
|
|
|
assert c[0] == 2
|
|
|
|
assert c[1] == 3
|
2019-10-27 09:36:04 +03:00
|
|
|
assert d[0] == 2
|
|
|
|
assert d[1] == 3
|
2019-06-25 16:31:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_left() {
|
|
|
|
a := [1, 2, 3]
|
2019-10-27 01:37:29 +03:00
|
|
|
c := a[0..2]
|
2019-10-27 09:36:04 +03:00
|
|
|
d := a[..2]
|
2019-10-27 01:37:29 +03:00
|
|
|
assert c[0] == 1
|
|
|
|
assert c[1] == 2
|
2019-10-27 09:36:04 +03:00
|
|
|
assert d[0] == 1
|
|
|
|
assert d[1] == 2
|
2019-06-25 16:31:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_slice() {
|
|
|
|
a := [1, 2, 3, 4]
|
|
|
|
b := a.slice(2, 4)
|
2019-06-29 23:49:57 +03:00
|
|
|
assert b.len == 2
|
2019-06-25 16:31:56 +03:00
|
|
|
assert a.slice(1, 2).len == 1
|
2019-06-29 23:49:57 +03:00
|
|
|
assert a.len == 4
|
2019-06-25 16:31:56 +03:00
|
|
|
}
|
|
|
|
|
2020-03-31 17:47:55 +03:00
|
|
|
|
2019-06-25 16:31:56 +03:00
|
|
|
fn test_push_many() {
|
|
|
|
mut a := [1, 2, 3]
|
|
|
|
b := [4, 5, 6]
|
2019-09-14 23:48:30 +03:00
|
|
|
a << b
|
2019-06-25 16:31:56 +03:00
|
|
|
assert a.len == 6
|
|
|
|
assert a[0] == 1
|
|
|
|
assert a[3] == 4
|
|
|
|
assert a[5] == 6
|
|
|
|
}
|
|
|
|
|
2019-07-17 19:17:07 +03:00
|
|
|
fn test_reverse() {
|
2019-11-06 06:57:04 +03:00
|
|
|
a := [1, 2, 3, 4]
|
|
|
|
b := ['test', 'array', 'reverse']
|
2019-07-17 19:35:40 +03:00
|
|
|
c := a.reverse()
|
2020-03-18 20:54:08 +03:00
|
|
|
println(c)
|
2019-07-17 19:35:40 +03:00
|
|
|
d := b.reverse()
|
2020-01-21 20:49:30 +03:00
|
|
|
for i, _ in c {
|
|
|
|
assert c[i] == a[a.len - i - 1]
|
2019-07-17 19:35:40 +03:00
|
|
|
}
|
|
|
|
for i, _ in d {
|
2020-01-21 20:49:30 +03:00
|
|
|
assert d[i] == b[b.len - i - 1]
|
2019-07-17 19:35:40 +03:00
|
|
|
}
|
2020-04-26 12:56:27 +03:00
|
|
|
e := []int{}
|
2020-02-29 22:44:02 +03:00
|
|
|
f := e.reverse()
|
|
|
|
assert f.len == 0
|
2019-07-17 19:17:07 +03:00
|
|
|
}
|
2019-08-04 04:59:12 +03:00
|
|
|
|
|
|
|
const (
|
2020-05-22 18:36:09 +03:00
|
|
|
c_n = 5
|
2019-09-14 23:48:30 +03:00
|
|
|
)
|
2019-08-04 04:59:12 +03:00
|
|
|
|
2019-12-06 02:11:39 +03:00
|
|
|
struct Foooj {
|
2020-05-22 18:36:09 +03:00
|
|
|
a [5]int // c_n
|
2019-12-06 02:11:39 +03:00
|
|
|
}
|
|
|
|
|
2019-08-04 04:59:12 +03:00
|
|
|
fn test_fixed() {
|
2019-09-14 23:48:30 +03:00
|
|
|
mut nums := [4]int
|
2020-03-31 18:16:12 +03:00
|
|
|
//x := nums[1..3]
|
|
|
|
//assert x.len == 2
|
2019-09-14 23:48:30 +03:00
|
|
|
assert nums[0] == 0
|
|
|
|
assert nums[1] == 0
|
|
|
|
assert nums[2] == 0
|
|
|
|
assert nums[3] == 0
|
|
|
|
nums[1] = 7
|
|
|
|
assert nums[1] == 7
|
2020-05-22 18:36:09 +03:00
|
|
|
nums2 := [5]int // c_n
|
|
|
|
assert nums2[c_n - 1] == 0
|
2019-09-14 23:48:30 +03:00
|
|
|
}
|
2019-08-07 13:09:24 +03:00
|
|
|
|
2020-06-04 11:35:40 +03:00
|
|
|
fn modify(mut numbers []int) {
|
2020-01-21 20:49:30 +03:00
|
|
|
numbers[0] = 777
|
2019-08-07 13:09:24 +03:00
|
|
|
}
|
|
|
|
|
2019-09-14 23:48:30 +03:00
|
|
|
fn test_mut_slice() {
|
2020-03-27 00:09:25 +03:00
|
|
|
/*
|
|
|
|
QTODO
|
2020-01-21 20:49:30 +03:00
|
|
|
mut n := [1, 2, 3]
|
2020-03-25 16:24:48 +03:00
|
|
|
//modify(mut n)
|
2019-10-27 10:03:15 +03:00
|
|
|
modify(mut n[..2])
|
2019-09-14 23:48:30 +03:00
|
|
|
assert n[0] == 777
|
2019-10-27 10:03:15 +03:00
|
|
|
modify(mut n[2..])
|
2019-09-14 23:48:30 +03:00
|
|
|
assert n[2] == 777
|
2019-08-07 13:57:07 +03:00
|
|
|
println(n)
|
2020-03-27 00:09:25 +03:00
|
|
|
*/
|
2019-08-07 13:09:24 +03:00
|
|
|
}
|
2019-08-12 14:41:34 +03:00
|
|
|
|
2020-06-04 11:35:40 +03:00
|
|
|
fn double_up(mut a []int) {
|
2020-04-04 15:09:58 +03:00
|
|
|
for i := 0; i < a.len; i++ {
|
|
|
|
a[i] = a[i]*2
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-04 11:35:40 +03:00
|
|
|
fn double_up_v2(mut a []int) {
|
2020-05-01 01:29:54 +03:00
|
|
|
for i, _ in a {
|
2020-04-04 15:09:58 +03:00
|
|
|
a[i] = a[i]*2 // or val*2, doesn't matter
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_mut_arg() {
|
|
|
|
mut arr := [1,2,3,4,5,6,7,8,9,10]
|
|
|
|
double_up(mut arr)
|
|
|
|
assert arr.str() == '[2, 4, 6, 8, 10, 12, 14, 16, 18, 20]'
|
|
|
|
arr = [1,2,3,4,5,6,7,8,9,10]
|
|
|
|
double_up_v2(mut arr)
|
|
|
|
assert arr.str() == '[2, 4, 6, 8, 10, 12, 14, 16, 18, 20]'
|
|
|
|
}
|
|
|
|
|
2019-08-12 14:41:34 +03:00
|
|
|
fn test_clone() {
|
2019-09-14 23:48:30 +03:00
|
|
|
nums := [1, 2, 3, 4, 100]
|
|
|
|
nums2 := nums.clone()
|
|
|
|
assert nums2.len == 5
|
2020-03-26 13:32:29 +03:00
|
|
|
assert nums.str() == '[1, 2, 3, 4, 100]'
|
2019-09-14 23:48:30 +03:00
|
|
|
assert nums2.str() == '[1, 2, 3, 4, 100]'
|
|
|
|
assert nums.slice(1, 3).str() == '[2, 3]'
|
|
|
|
}
|
|
|
|
|
2020-06-19 14:32:55 +03:00
|
|
|
fn test_mutli_array_clone() {
|
2020-06-20 03:35:22 +03:00
|
|
|
// 2d array_int
|
|
|
|
mut a2_1 := [[1, 2, 3], [4, 5, 6]]
|
|
|
|
mut a2_2 := a2_1.clone()
|
2020-06-19 14:32:55 +03:00
|
|
|
|
2020-06-20 03:35:22 +03:00
|
|
|
a2_1[0][1] = 0
|
|
|
|
a2_2[1][0] = 0
|
2020-06-19 14:32:55 +03:00
|
|
|
|
2020-06-20 03:35:22 +03:00
|
|
|
assert a2_1 == [[1, 0, 3], [4, 5, 6]]
|
|
|
|
assert a2_2 == [[1, 2, 3], [0, 5, 6]]
|
|
|
|
|
|
|
|
// 2d array_string
|
|
|
|
mut b2_1 := [['1', '2', '3'], ['4', '5', '6']]
|
|
|
|
mut b2_2 := b2_1.clone()
|
|
|
|
|
|
|
|
b2_1[0][1] = '0'
|
|
|
|
b2_2[1][0] = '0'
|
|
|
|
|
|
|
|
assert b2_1 == [['1', '0', '3'], ['4', '5', '6']]
|
|
|
|
assert b2_2 == [['1', '2', '3'], ['0', '5', '6']]
|
|
|
|
|
|
|
|
// 3d array_int
|
|
|
|
mut a3_1 := [[[1,1], [2,2], [3,3]], [[4,4], [5,5], [6,6]]]
|
|
|
|
mut a3_2 := a3_1.clone()
|
|
|
|
|
|
|
|
a3_1[0][0][1] = 0
|
|
|
|
a3_2[0][1][0] = 0
|
|
|
|
|
|
|
|
assert a3_1 == [[[1, 0], [2, 2], [3, 3]], [[4, 4], [5, 5], [6, 6]]]
|
|
|
|
assert a3_2 == [[[1, 1], [0, 2], [3, 3]], [[4, 4], [5, 5], [6, 6]]]
|
|
|
|
|
|
|
|
// 3d array_string
|
|
|
|
mut b3_1 := [[['1','1'], ['2','2'], ['3','3']], [['4','4'], ['5','5'], ['6','6']]]
|
|
|
|
mut b3_2 := b3_1.clone()
|
|
|
|
|
|
|
|
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']]]
|
2020-06-19 14:32:55 +03:00
|
|
|
}
|
|
|
|
|
2019-08-22 04:27:57 +03:00
|
|
|
fn test_doubling() {
|
|
|
|
mut nums := [1, 2, 3, 4, 5]
|
2020-02-24 19:55:16 +03:00
|
|
|
for i in 0..nums.len {
|
2019-08-22 04:27:57 +03:00
|
|
|
nums[i] *= 2
|
|
|
|
}
|
2020-03-27 00:09:25 +03:00
|
|
|
println(nums.str())
|
2019-08-22 04:27:57 +03:00
|
|
|
assert nums.str() == '[2, 4, 6, 8, 10]'
|
2019-08-22 07:56:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
struct Test2 {
|
|
|
|
one int
|
|
|
|
two int
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Test {
|
|
|
|
a string
|
2019-08-30 20:19:06 +03:00
|
|
|
mut:
|
2019-08-22 07:56:29 +03:00
|
|
|
b []Test2
|
|
|
|
}
|
|
|
|
|
2020-03-27 12:47:24 +03:00
|
|
|
// TODO: default array/struct str methods
|
2020-05-02 16:26:58 +03:00
|
|
|
fn (ta []Test2) str() string {
|
2020-03-27 12:47:24 +03:00
|
|
|
mut s := '['
|
|
|
|
for i, t in ta {
|
|
|
|
s += t.str()
|
|
|
|
if i < ta.len-1 {
|
|
|
|
s += ', '
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s += ']'
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2020-05-02 16:26:58 +03:00
|
|
|
fn (t Test2) str() string {
|
2019-08-22 07:56:29 +03:00
|
|
|
return '{$t.one $t.two}'
|
|
|
|
}
|
|
|
|
|
2020-05-02 16:26:58 +03:00
|
|
|
fn (t Test) str() string {
|
2019-08-22 07:56:29 +03:00
|
|
|
return '{$t.a $t.b}'
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_struct_print() {
|
2020-01-21 20:49:30 +03:00
|
|
|
mut a := Test{
|
|
|
|
a: 'Test'
|
2019-11-14 10:00:22 +03:00
|
|
|
b: []
|
2019-08-22 07:56:29 +03:00
|
|
|
}
|
2020-01-21 20:49:30 +03:00
|
|
|
b := Test2{
|
|
|
|
one: 1
|
2019-08-22 07:56:29 +03:00
|
|
|
two: 2
|
|
|
|
}
|
|
|
|
a.b << b
|
|
|
|
a.b << b
|
2020-04-23 05:23:19 +03:00
|
|
|
assert a.str() == '{Test [{1 2}, {1 2}]}'
|
|
|
|
assert b.str() == '{1 2}'
|
2019-08-22 07:56:29 +03:00
|
|
|
assert a.b.str() == '[{1 2}, {1 2}]'
|
|
|
|
}
|
2019-09-28 13:52:02 +03:00
|
|
|
|
|
|
|
fn test_single_element() {
|
|
|
|
mut a := [1]
|
|
|
|
a << 2
|
|
|
|
assert a.len == 2
|
|
|
|
assert a[0] == 1
|
|
|
|
assert a[1] == 2
|
|
|
|
println(a)
|
2019-12-06 02:11:39 +03:00
|
|
|
}
|
2019-10-04 23:07:19 +03:00
|
|
|
|
|
|
|
fn test_find_index() {
|
|
|
|
// string
|
|
|
|
a := ['v', 'is', 'great']
|
|
|
|
assert a.index('v') == 0
|
|
|
|
assert a.index('is') == 1
|
2020-01-21 20:49:30 +03:00
|
|
|
assert a.index('gre') == -1
|
2019-10-04 23:07:19 +03:00
|
|
|
// int
|
|
|
|
b := [1, 2, 3, 4]
|
|
|
|
assert b.index(1) == 0
|
|
|
|
assert b.index(4) == 3
|
|
|
|
assert b.index(5) == -1
|
|
|
|
// byte
|
|
|
|
c := [0x22, 0x33, 0x55]
|
|
|
|
assert c.index(0x22) == 0
|
|
|
|
assert c.index(0x55) == 2
|
|
|
|
assert c.index(0x99) == -1
|
|
|
|
// char
|
|
|
|
d := [`a`, `b`, `c`]
|
|
|
|
assert d.index(`b`) == 1
|
|
|
|
assert d.index(`c`) == 2
|
|
|
|
assert d.index(`u`) == -1
|
|
|
|
}
|
2019-10-05 16:03:13 +03:00
|
|
|
|
|
|
|
fn test_multi() {
|
2020-01-21 20:49:30 +03:00
|
|
|
a := [[1, 2, 3], [4, 5, 6]]
|
2019-10-05 16:03:13 +03:00
|
|
|
assert a.len == 2
|
|
|
|
assert a[0].len == 3
|
|
|
|
assert a[0][0] == 1
|
|
|
|
assert a[0][2] == 3
|
|
|
|
assert a[1][2] == 6
|
|
|
|
// TODO
|
2020-01-21 20:49:30 +03:00
|
|
|
// b := [ [[1,2,3],[4,5,6]], [[1,2]] ]
|
|
|
|
// assert b[0][0][0] == 1
|
2019-10-09 15:26:27 +03:00
|
|
|
}
|
2019-10-06 04:27:29 +03:00
|
|
|
|
|
|
|
fn test_in() {
|
2020-01-21 20:49:30 +03:00
|
|
|
a := [1, 2, 3]
|
2019-10-06 04:27:29 +03:00
|
|
|
assert 1 in a
|
|
|
|
assert 2 in a
|
|
|
|
assert 3 in a
|
|
|
|
assert !(4 in a)
|
|
|
|
assert !(0 in a)
|
2020-04-12 00:01:50 +03:00
|
|
|
assert 0 !in a
|
|
|
|
assert 4 !in a
|
2020-05-27 06:42:48 +03:00
|
|
|
b := [1, 4, 0]
|
|
|
|
c := [3, 6, 2, 0]
|
|
|
|
assert 0 in b
|
|
|
|
assert 0 in c
|
2019-10-09 15:26:27 +03:00
|
|
|
}
|
2019-10-08 13:23:17 +03:00
|
|
|
|
2019-10-11 04:12:40 +03:00
|
|
|
fn sum(prev int, curr int) int {
|
|
|
|
return prev + curr
|
|
|
|
}
|
|
|
|
|
|
|
|
fn sub(prev int, curr int) int {
|
|
|
|
return prev - curr
|
|
|
|
}
|
|
|
|
|
2020-02-18 20:13:34 +03:00
|
|
|
/*
|
2019-10-11 04:12:40 +03:00
|
|
|
fn test_reduce() {
|
|
|
|
a := [1, 2, 3, 4, 5]
|
|
|
|
b := a.reduce(sum, 0)
|
|
|
|
c := a.reduce(sum, 5)
|
|
|
|
d := a.reduce(sum, -1)
|
|
|
|
assert b == 15
|
|
|
|
assert c == 20
|
|
|
|
assert d == 14
|
|
|
|
e := [1, 2, 3]
|
|
|
|
f := e.reduce(sub, 0)
|
|
|
|
g := e.reduce(sub, -1)
|
|
|
|
assert f == -6
|
|
|
|
assert g == -7
|
|
|
|
}
|
2020-02-18 20:13:34 +03:00
|
|
|
*/
|
2019-10-17 10:44:20 +03:00
|
|
|
|
2020-05-20 15:28:33 +03:00
|
|
|
fn filter_test_helper_1(a int) bool {
|
|
|
|
return a > 3
|
|
|
|
}
|
|
|
|
|
2019-10-17 10:44:20 +03:00
|
|
|
fn test_filter() {
|
|
|
|
a := [1, 2, 3, 4, 5, 6]
|
|
|
|
b := a.filter(it % 2 == 0)
|
2019-10-26 14:32:56 +03:00
|
|
|
assert b.len == 3
|
2019-10-17 10:44:20 +03:00
|
|
|
assert b[0] == 2
|
|
|
|
assert b[1] == 4
|
|
|
|
assert b[2] == 6
|
|
|
|
c := ['v', 'is', 'awesome']
|
|
|
|
d := c.filter(it.len > 1)
|
|
|
|
assert d[0] == 'is'
|
|
|
|
assert d[1] == 'awesome'
|
2020-04-12 00:01:50 +03:00
|
|
|
////////
|
|
|
|
arr :=[1,2,3,4,5,6,7,8,9,10]
|
|
|
|
println(arr.filter(it % 2 == 0 || it % 3 == 0))
|
|
|
|
assert true
|
|
|
|
assert [1,2,3].len == 3
|
2020-04-12 00:32:00 +03:00
|
|
|
mut mut_arr := [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
|
|
|
mut_arr = mut_arr.filter(it < 4)
|
|
|
|
assert mut_arr.len == 3
|
2020-05-20 15:28:33 +03:00
|
|
|
assert a.filter(filter_test_helper_1) == [4,5,6]
|
|
|
|
assert [1,5,10].filter(filter_test_helper_1) == [5,10]
|
2020-04-12 00:32:00 +03:00
|
|
|
|
2020-04-12 00:01:50 +03:00
|
|
|
// TODO
|
|
|
|
//assert arr.filter(arr % 2).len == 5
|
2019-12-06 02:11:39 +03:00
|
|
|
}
|
2019-10-25 22:40:52 +03:00
|
|
|
|
2020-06-10 14:18:59 +03:00
|
|
|
fn test_anon_fn_filter() {
|
|
|
|
filter_num := fn (i int) bool {
|
|
|
|
return i % 2 == 0
|
|
|
|
}
|
|
|
|
assert [1,2,3,4,5].filter(filter_num) == [2,4]
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_anon_fn_arg_filter() {
|
|
|
|
a := [1,2,3,4].filter(fn (i int) bool {
|
|
|
|
return i % 2 == 0
|
|
|
|
})
|
|
|
|
|
|
|
|
assert a == [2,4]
|
|
|
|
}
|
|
|
|
|
2020-05-02 16:26:58 +03:00
|
|
|
fn map_test_helper_1(i int) int {
|
|
|
|
return i * i
|
|
|
|
}
|
|
|
|
fn map_test_helper_2(i int, b string) int {
|
|
|
|
return i + b.len
|
|
|
|
}
|
|
|
|
fn map_test_helper_3(i int, b []string) int {
|
|
|
|
return i + b.map(it.len)[i % b.len]
|
|
|
|
}
|
|
|
|
|
2019-10-26 14:32:56 +03:00
|
|
|
fn test_map() {
|
2020-05-01 01:29:54 +03:00
|
|
|
nums := [1, 2, 3, 4, 5, 6]
|
|
|
|
strs := ['v', 'is', 'awesome']
|
|
|
|
|
2020-05-02 16:26:58 +03:00
|
|
|
//assert nums.map() == <error>
|
|
|
|
//assert nums.map(it, 'excessive') == <error>
|
|
|
|
|
|
|
|
// identity
|
|
|
|
assert nums.map(it) == [1, 2, 3, 4, 5, 6]
|
|
|
|
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
|
|
|
|
|
|
|
|
// type switch
|
2020-05-01 01:29:54 +03:00
|
|
|
assert nums.map(it * 10) == [10, 20, 30, 40, 50, 60]
|
2020-05-02 16:26:58 +03:00
|
|
|
assert nums.map(it * it) == [1, 4, 9, 16, 25, 36]
|
2020-05-01 01:29:54 +03:00
|
|
|
assert nums.map('$it') == ['1', '2', '3', '4', '5', '6']
|
|
|
|
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 == 'awesome') == [false, false, true]
|
2020-05-02 16:26:58 +03:00
|
|
|
assert strs.map(it.len in nums) == [true, true, false]
|
2020-05-27 06:42:48 +03:00
|
|
|
assert strs.map(int(7)) == [7, 7, 7]
|
2020-05-01 01:29:54 +03:00
|
|
|
|
2020-05-02 16:26:58 +03:00
|
|
|
// external func
|
|
|
|
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_3(it, strs)) == [3, 9, 4, 6, 12, 7]
|
|
|
|
|
|
|
|
// empty array as input
|
|
|
|
assert []int{len:0}.map(it * 2) == []
|
|
|
|
|
|
|
|
// nested maps (where it is of same type)
|
2020-05-27 06:42:48 +03:00
|
|
|
assert nums.map( strs.map(int(7)) == [7, 7, 7] ) == [true, true, true, true, true, true]
|
2020-05-02 16:26:58 +03:00
|
|
|
assert nums.map( '$it' + strs.map('a')[0] ) == ['1a', '2a', '3a', '4a', '5a', '6a']
|
2020-05-27 06:42:48 +03:00
|
|
|
assert nums.map( it + strs.map(int(7))[0] ) == [8, 9, 10, 11, 12, 13]
|
2020-05-02 16:26:58 +03:00
|
|
|
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]
|
|
|
|
|
|
|
|
// nested (different it types)
|
|
|
|
assert strs.map( it[ nums.map(it - it)[0] ] ) == [`v`, `i`, `a`]
|
|
|
|
assert nums[0..3].map('$it' + strs.map(it)[it-1]) == ['1v','2is','3awesome']
|
|
|
|
|
2020-05-20 15:28:33 +03:00
|
|
|
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]
|
|
|
|
|
2020-05-01 01:29:54 +03:00
|
|
|
assert nums == [1, 2, 3, 4, 5, 6]
|
|
|
|
assert strs == ['v', 'is', 'awesome']
|
2019-12-06 02:11:39 +03:00
|
|
|
}
|
2019-10-26 14:32:56 +03:00
|
|
|
|
2020-06-09 11:14:10 +03:00
|
|
|
fn test_anon_fn_map() {
|
|
|
|
add_num := fn (i int) int {
|
|
|
|
return i + 1
|
|
|
|
}
|
|
|
|
assert [1,2,3].map(add_num) == [2,3,4]
|
|
|
|
}
|
|
|
|
|
2020-06-10 14:18:45 +03:00
|
|
|
fn test_mutli_anon_fn_map() {
|
|
|
|
a := [1,2,3].map(fn (i int) int {
|
|
|
|
return i + 1
|
|
|
|
})
|
|
|
|
|
|
|
|
b := [1,2,3].map(fn (i int) int {
|
|
|
|
return i + 2
|
|
|
|
})
|
|
|
|
assert a == [2,3,4]
|
|
|
|
assert b == [3,4,5]
|
|
|
|
}
|
|
|
|
|
2020-06-09 13:50:56 +03:00
|
|
|
fn test_anon_fn_arg_map() {
|
|
|
|
a := [1,2,3].map(fn (i int) int {
|
|
|
|
return i + 1
|
|
|
|
})
|
|
|
|
|
|
|
|
assert a == [2,3,4]
|
|
|
|
}
|
|
|
|
|
2019-10-25 22:40:52 +03:00
|
|
|
fn test_array_str() {
|
|
|
|
numbers := [1, 2, 3]
|
2020-04-23 05:23:19 +03:00
|
|
|
assert numbers == [1,2,3]
|
2019-10-25 22:40:52 +03:00
|
|
|
numbers2 := [numbers, [4, 5, 6]] // dup str() bug
|
2020-06-08 01:47:04 +03:00
|
|
|
_ = numbers2
|
2019-10-25 22:40:52 +03:00
|
|
|
assert true
|
|
|
|
assert numbers.str() == '[1, 2, 3]'
|
2020-04-23 05:23:19 +03:00
|
|
|
// QTODO
|
|
|
|
//assert numbers2.str() == '[[1, 2, 3], [4, 5, 6]]'
|
|
|
|
|
2019-10-29 14:26:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_eq() {
|
2020-02-04 10:29:50 +03:00
|
|
|
/*
|
2020-01-21 20:49:30 +03:00
|
|
|
assert [5, 6, 7].eq([6, 7]) == false
|
|
|
|
assert [`a`, `b`].eq([`a`, `b`]) == true
|
2020-02-04 10:29:50 +03:00
|
|
|
*/
|
2019-10-29 14:26:00 +03:00
|
|
|
}
|
2019-11-05 17:15:19 +03:00
|
|
|
|
2019-11-19 00:27:27 +03:00
|
|
|
fn test_sort() {
|
|
|
|
mut a := ['hi', '1', '5', '3']
|
|
|
|
a.sort()
|
|
|
|
assert a[0] == '1'
|
|
|
|
assert a[1] == '3'
|
|
|
|
assert a[2] == '5'
|
|
|
|
assert a[3] == 'hi'
|
|
|
|
//
|
|
|
|
mut nums := [67, -3, 108, 42, 7]
|
|
|
|
nums.sort()
|
|
|
|
assert nums[0] == -3
|
|
|
|
assert nums[1] == 7
|
|
|
|
assert nums[2] == 42
|
|
|
|
assert nums[3] == 67
|
|
|
|
assert nums[4] == 108
|
2019-12-06 02:11:39 +03:00
|
|
|
}
|
2019-11-19 00:27:27 +03:00
|
|
|
|
2020-01-21 20:49:30 +03:00
|
|
|
fn test_f32_sort() {
|
2020-03-19 09:24:49 +03:00
|
|
|
mut f := [f32(50.0), 15, 1, 79, 38, 0, 27]
|
2020-01-21 20:49:30 +03:00
|
|
|
f.sort_with_compare(compare_f32)
|
|
|
|
assert f[0] == 0.0
|
|
|
|
assert f[1] == 1.0
|
|
|
|
assert f[6] == 79.0
|
|
|
|
}
|
2019-11-19 00:27:27 +03:00
|
|
|
|
2020-01-21 20:49:30 +03:00
|
|
|
fn test_f64_sort() {
|
2020-03-19 09:24:49 +03:00
|
|
|
mut f := [50.0, 15, 1, 79, 38, 0, 27]
|
2020-01-21 20:49:30 +03:00
|
|
|
f.sort_with_compare(compare_f64)
|
|
|
|
assert f[0] == 0.0
|
|
|
|
assert f[1] == 1.0
|
|
|
|
assert f[6] == 79.0
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_i64_sort() {
|
|
|
|
mut f := [i64(50), 15, 1, 79, 38, 0, 27]
|
|
|
|
f.sort_with_compare(compare_i64)
|
|
|
|
assert f[0] == 0
|
|
|
|
assert f[1] == 1
|
|
|
|
assert f[6] == 79
|
|
|
|
}
|
2019-11-19 00:27:27 +03:00
|
|
|
|
2019-11-06 18:19:03 +03:00
|
|
|
/*
|
2019-11-05 17:15:19 +03:00
|
|
|
fn test_for_last() {
|
|
|
|
numbers := [1, 2, 3, 4]
|
|
|
|
mut s := '['
|
|
|
|
for num in numbers {
|
|
|
|
s += '$num'
|
|
|
|
if !last {
|
|
|
|
s += ', '
|
2019-12-06 02:11:39 +03:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2019-11-05 17:15:19 +03:00
|
|
|
s += ']'
|
|
|
|
assert s == '[1, 2, 3, 4]'
|
2019-12-06 02:11:39 +03:00
|
|
|
}
|
2019-11-06 18:19:03 +03:00
|
|
|
*/
|
2019-12-27 06:09:09 +03:00
|
|
|
|
2020-01-21 20:49:30 +03:00
|
|
|
|
2019-12-27 06:09:09 +03:00
|
|
|
struct Foo {
|
2020-01-21 20:49:30 +03:00
|
|
|
mut:
|
2019-12-27 06:09:09 +03:00
|
|
|
bar []int
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_in_struct() {
|
|
|
|
mut baz := Foo{
|
|
|
|
bar: [0, 0, 0]
|
|
|
|
}
|
|
|
|
baz.bar[0] += 2
|
|
|
|
baz.bar[0]++
|
|
|
|
assert baz.bar[0] == 3
|
|
|
|
}
|
2020-01-09 14:00:39 +03:00
|
|
|
|
|
|
|
fn test_bools() {
|
|
|
|
println('test b')
|
|
|
|
mut a := [true, false]
|
|
|
|
a << true
|
|
|
|
println(a)
|
|
|
|
}
|
2020-01-20 00:15:37 +03:00
|
|
|
|
|
|
|
fn test_push_many_self() {
|
|
|
|
mut actual_arr := [1, 2, 3, 4]
|
|
|
|
actual_arr << actual_arr
|
|
|
|
expected_arr := [1, 2, 3, 4, 1, 2, 3, 4]
|
|
|
|
assert actual_arr.len == expected_arr.len
|
2020-02-24 19:55:16 +03:00
|
|
|
for i in 0..actual_arr.len {
|
2020-01-20 00:15:37 +03:00
|
|
|
assert actual_arr[i] == expected_arr[i]
|
|
|
|
}
|
|
|
|
}
|
2020-01-21 05:22:18 +03:00
|
|
|
|
|
|
|
fn test_for() {
|
|
|
|
nums := [1,2,3]
|
|
|
|
mut sum := 0
|
2020-03-25 16:24:48 +03:00
|
|
|
for num in nums {
|
2020-01-21 05:22:18 +03:00
|
|
|
sum += num
|
|
|
|
}
|
|
|
|
assert sum == 6
|
|
|
|
}
|
2020-01-28 00:31:48 +03:00
|
|
|
|
|
|
|
fn test_clear() {
|
|
|
|
mut arr := [1,2,3]
|
|
|
|
assert arr.len == 3
|
|
|
|
arr.clear()
|
|
|
|
assert arr.len == 0
|
2020-02-04 10:29:50 +03:00
|
|
|
|
2020-01-28 00:31:48 +03:00
|
|
|
arr << 3
|
|
|
|
arr << 2
|
|
|
|
arr << 1
|
|
|
|
arr << 0
|
|
|
|
assert arr.len == 4
|
|
|
|
assert arr[0] == 3
|
|
|
|
assert arr[1] == 2
|
|
|
|
assert arr[2] == 1
|
|
|
|
assert arr[3] == 0
|
2020-02-04 10:29:50 +03:00
|
|
|
|
2020-01-28 00:31:48 +03:00
|
|
|
arr.clear()
|
|
|
|
assert arr.len == 0
|
2020-01-22 23:34:38 +03:00
|
|
|
}
|
2020-02-17 22:31:40 +03:00
|
|
|
|
|
|
|
fn test_trim() {
|
|
|
|
mut arr := [1,2,3,4,5,6,7,8,9]
|
|
|
|
assert arr.len == 9
|
2020-02-18 20:13:34 +03:00
|
|
|
|
2020-02-17 22:31:40 +03:00
|
|
|
arr.trim(9)
|
|
|
|
assert arr.len == 9
|
|
|
|
assert arr.last() == 9
|
2020-02-18 20:13:34 +03:00
|
|
|
|
2020-02-17 22:31:40 +03:00
|
|
|
arr.trim(7)
|
|
|
|
assert arr.len == 7
|
|
|
|
assert arr.last() == 7
|
2020-02-18 20:13:34 +03:00
|
|
|
|
2020-02-17 22:31:40 +03:00
|
|
|
arr.trim(2)
|
|
|
|
assert arr.len == 2
|
|
|
|
assert arr.last() == 2
|
|
|
|
}
|
2020-03-11 02:38:11 +03:00
|
|
|
|
|
|
|
fn test_hex(){
|
|
|
|
// array hex
|
|
|
|
st := [byte(`V`),`L`,`A`,`N`,`G`]
|
|
|
|
assert st.hex() == "564c414e47"
|
|
|
|
assert st.hex().len == 10
|
|
|
|
|
|
|
|
st1 := [byte(0x41)].repeat(100)
|
|
|
|
assert st1.hex() == "41".repeat(100)
|
|
|
|
}
|
2020-04-14 19:09:59 +03:00
|
|
|
|
2020-04-14 20:38:11 +03:00
|
|
|
fn test_left_shift_precendence() {
|
2020-04-26 12:56:27 +03:00
|
|
|
mut arr := []int{}
|
2020-04-14 20:38:11 +03:00
|
|
|
arr << 1 + 1
|
|
|
|
arr << 1 - 1
|
|
|
|
arr << 2 / 1
|
|
|
|
arr << 2 * 1
|
|
|
|
|
|
|
|
assert arr[0] == 2
|
|
|
|
assert arr[1] == 0
|
|
|
|
assert arr[2] == 2
|
|
|
|
assert arr[3] == 2
|
|
|
|
}
|
|
|
|
|
2020-04-14 19:09:59 +03:00
|
|
|
fn test_array_with_cap() {
|
2020-04-26 18:28:43 +03:00
|
|
|
a4 := []int{cap:10, len:1 }
|
|
|
|
assert a4.len == 1
|
|
|
|
assert a4.cap == 10
|
2020-04-26 18:52:27 +03:00
|
|
|
a5 := []int{len:1, cap:10}
|
|
|
|
assert a5.len == 1
|
|
|
|
assert a5.cap == 10
|
2020-04-14 19:09:59 +03:00
|
|
|
}
|
2020-06-12 02:24:25 +03:00
|
|
|
|
|
|
|
fn test_mutli_array_index() {
|
|
|
|
mut a := [][]int{len:2, init: []int{len:3, init:0}}
|
|
|
|
a[0][0] = 1
|
|
|
|
assert '$a' == '[[1, 0, 0], [0, 0, 0]]'
|
2020-06-12 12:42:26 +03:00
|
|
|
|
|
|
|
mut b := [[0].repeat(3)].repeat(2)
|
|
|
|
b[0][0] = 1
|
|
|
|
assert '$b' == '[[1, 0, 0], [0, 0, 0]]'
|
2020-06-12 02:24:25 +03:00
|
|
|
}
|
2020-06-28 12:16:16 +03:00
|
|
|
|
|
|
|
fn test_plus_assign_string() {
|
|
|
|
mut a := ['']
|
|
|
|
a[0] += 'abc'
|
|
|
|
assert a == ['abc']
|
|
|
|
}
|
2020-07-04 12:36:33 +03:00
|
|
|
|
|
|
|
fn mut_arr_with_eq_in_fn(mut a []int) {
|
|
|
|
if a == [1,2,3,4] {
|
|
|
|
a[0] = 0
|
|
|
|
}
|
|
|
|
if [0,2,3,4] == a {
|
2020-07-04 13:12:44 +03:00
|
|
|
a[1] = 0
|
2020-07-04 12:36:33 +03:00
|
|
|
}
|
|
|
|
if !(a != [0,0,3,4]) {
|
2020-07-04 13:12:44 +03:00
|
|
|
a[2] = 0
|
2020-07-04 12:36:33 +03:00
|
|
|
}
|
|
|
|
if !([0,0,0,4] != a) {
|
|
|
|
a[3] = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_mut_arr_with_eq_in_fn() {
|
|
|
|
mut a := [1,2,3,4]
|
|
|
|
mut_arr_with_eq_in_fn(mut a)
|
|
|
|
assert a == [0,0,0,0]
|
|
|
|
}
|
2020-07-04 13:12:44 +03:00
|
|
|
|
|
|
|
fn array_in_mut(mut a []int) {
|
|
|
|
if 1 in a {
|
|
|
|
a[0] = 2
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_array_in_mut() {
|
|
|
|
mut a := [1,2]
|
|
|
|
array_in_mut(mut a)
|
|
|
|
assert a == [2,2]
|
|
|
|
}
|
2020-07-10 19:04:51 +03:00
|
|
|
|
|
|
|
// test array delete in function with mut argument
|
|
|
|
fn delete_nums(mut arr []int) {
|
|
|
|
arr.delete(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_array_delete_in_mut() {
|
|
|
|
mut nums := [1, 2, 3]
|
|
|
|
delete_nums(mut nums)
|
|
|
|
assert nums == [2, 3]
|
|
|
|
}
|
|
|
|
|
|
|
|
// test array add in function with mut argument
|
|
|
|
fn add_nums(mut arr []int) {
|
|
|
|
arr << 4
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_array_add_in_mut() {
|
|
|
|
mut nums := [1, 2, 3]
|
|
|
|
add_nums(mut nums)
|
|
|
|
assert nums == [1, 2, 3, 4]
|
|
|
|
}
|
2020-07-11 14:17:11 +03:00
|
|
|
|
|
|
|
fn test_reverse_in_place() {
|
|
|
|
mut a := [1, 2, 3, 4]
|
|
|
|
a.reverse_in_place()
|
|
|
|
assert a == [4, 3, 2, 1]
|
|
|
|
|
|
|
|
mut b := ['a', 'b', 'c']
|
|
|
|
b.reverse_in_place()
|
|
|
|
assert b == ['c', 'b', 'a']
|
|
|
|
|
|
|
|
mut c := [[1, 2], [3, 4], [5, 6]]
|
|
|
|
c.reverse_in_place()
|
|
|
|
assert c == [[5, 6], [3, 4], [1, 2]]
|
|
|
|
}
|
2020-07-14 19:55:44 +03:00
|
|
|
|
|
|
|
fn test_array_int_pop() {
|
|
|
|
mut a := [1,2,3,4,5]
|
|
|
|
assert a.len == 5
|
|
|
|
x := a.last()
|
|
|
|
y := a.pop()
|
|
|
|
assert x == y
|
|
|
|
assert a.len == 4
|
|
|
|
z := a.pop()
|
|
|
|
assert a.len == 3
|
|
|
|
assert z == 4
|
|
|
|
a.pop()
|
|
|
|
a.pop()
|
|
|
|
final := a.pop()
|
|
|
|
assert final == 1
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_array_string_pop() {
|
|
|
|
mut a := ['abc', 'def', 'xyz']
|
|
|
|
assert a.len == 3
|
|
|
|
assert a.pop() == 'xyz'
|
|
|
|
assert a.pop() == 'def'
|
|
|
|
assert a.pop() == 'abc'
|
|
|
|
assert a.len == 0
|
|
|
|
assert a.cap == 3
|
|
|
|
}
|