2019-06-25 15:31:56 +02:00
|
|
|
const (
|
|
|
|
q = [1, 2, 3]
|
|
|
|
A = 8
|
|
|
|
)
|
|
|
|
|
|
|
|
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-06-29 22:49:57 +02:00
|
|
|
|
2019-07-25 14:06:26 +02:00
|
|
|
s := a.str()
|
2019-06-25 15:31:56 +02:00
|
|
|
assert s == '[1, 5, 2, 3, 4]'
|
|
|
|
assert a[1] == 5
|
|
|
|
assert a.last() == 4
|
2019-06-29 22:49:57 +02: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 15:31:56 +02:00
|
|
|
a.delete(0)
|
|
|
|
assert a.str() == '[5, 2, 3, 4]'
|
2019-06-29 22:49:57 +02:00
|
|
|
assert a.len == 4
|
|
|
|
|
2019-06-25 15:31:56 +02:00
|
|
|
a.delete(1)
|
|
|
|
assert a.str() == '[5, 3, 4]'
|
2019-06-29 22:49:57 +02:00
|
|
|
assert a.len == 3
|
2019-06-25 15:31:56 +02: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 11:26:05 +02:00
|
|
|
mut a := [0].repeat(0)
|
2019-06-25 15:31:56 +02:00
|
|
|
for i := 0; i < 10000; i++ {
|
|
|
|
a << i
|
|
|
|
}
|
|
|
|
assert a.len == 10000
|
|
|
|
assert a[234] == 234
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Chunk {
|
|
|
|
val string
|
|
|
|
}
|
|
|
|
|
|
|
|
struct K {
|
|
|
|
q []Chunk
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_empty() {
|
|
|
|
mut chunks := []Chunk{}
|
|
|
|
a := Chunk{}
|
|
|
|
assert chunks.len == 0
|
|
|
|
chunks << a
|
|
|
|
assert chunks.len == 1
|
|
|
|
chunks = []Chunk{}
|
|
|
|
assert chunks.len == 0
|
|
|
|
chunks << a
|
|
|
|
assert chunks.len == 1
|
|
|
|
}
|
2019-06-29 22:49:57 +02:00
|
|
|
|
2019-06-25 15:31:56 +02:00
|
|
|
fn test_push() {
|
|
|
|
mut a := []int
|
|
|
|
a << 1
|
|
|
|
a << 3
|
|
|
|
assert a[1] == 3
|
|
|
|
assert a.str() == '[1, 3]'
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_strings() {
|
|
|
|
a := ['a', 'b', 'c']
|
|
|
|
assert a.str() == '["a", "b", "c"]'
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_repeat() {
|
2019-09-15 11:26:05 +02:00
|
|
|
a := [0].repeat(5)
|
2019-06-25 15:31:56 +02:00
|
|
|
assert a.len == 5
|
|
|
|
assert a[0] == 0 && a[1] == 0 && a[2] == 0 && a[3] == 0 && a[4] == 0
|
2019-06-29 22:49:57 +02:00
|
|
|
|
2019-09-15 11:26:05 +02:00
|
|
|
b := [7].repeat(3)
|
2019-06-29 22:49:57 +02:00
|
|
|
assert b.len == 3
|
|
|
|
assert b[0] == 7 && b[1] == 7 && b[2] == 7
|
2019-07-04 10:46:39 +02:00
|
|
|
{
|
2019-09-15 11:26:05 +02:00
|
|
|
mut aa := [1.1].repeat(10)
|
2019-07-04 10:46:39 +02:00
|
|
|
// FIXME: assert aa[0] == 1.1 will fail, need fix
|
|
|
|
assert aa[0] == f32(1.1)
|
|
|
|
assert aa[5] == f32(1.1)
|
|
|
|
assert aa[9] == f32(1.1)
|
|
|
|
}
|
|
|
|
{
|
2019-09-15 11:26:05 +02:00
|
|
|
mut aa := [f32(1.1)].repeat(10)
|
2019-07-04 10:46:39 +02:00
|
|
|
assert aa[0] == f32(1.1)
|
|
|
|
assert aa[5] == f32(1.1)
|
|
|
|
assert aa[9] == f32(1.1)
|
|
|
|
}
|
|
|
|
{
|
2019-09-15 11:26:05 +02:00
|
|
|
aa := [f64(1.1)].repeat(10)
|
2019-07-04 10:46:39 +02:00
|
|
|
assert aa[0] == f64(1.1)
|
|
|
|
assert aa[5] == f64(1.1)
|
|
|
|
assert aa[9] == f64(1.1)
|
|
|
|
}
|
2019-06-25 15:31:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_right() {
|
|
|
|
a := [1, 2, 3, 4]
|
|
|
|
b := a.right(1)
|
2019-10-27 00:37:29 +02:00
|
|
|
c := a[1..a.len]
|
2019-10-27 07:36:04 +01:00
|
|
|
d := a[1..]
|
2019-06-25 15:31:56 +02:00
|
|
|
assert b[0] == 2
|
|
|
|
assert b[1] == 3
|
2019-10-27 00:37:29 +02:00
|
|
|
assert c[0] == 2
|
|
|
|
assert c[1] == 3
|
2019-10-27 07:36:04 +01:00
|
|
|
assert d[0] == 2
|
|
|
|
assert d[1] == 3
|
2019-06-25 15:31:56 +02:00
|
|
|
}
|
|
|
|
|
2019-10-21 13:14:28 +02:00
|
|
|
fn test_right_with_n_bigger_than_array_size() {
|
|
|
|
a := [1, 2, 3, 4]
|
|
|
|
mut b := a.right(10)
|
|
|
|
assert b.len == 0
|
|
|
|
|
|
|
|
// also check that the result of a.right
|
|
|
|
// is an array of the same type/element size as a:
|
|
|
|
b << 5
|
|
|
|
b << 6
|
|
|
|
assert b.len == 2
|
|
|
|
assert b[0] == 5
|
|
|
|
assert b[1] == 6
|
|
|
|
}
|
|
|
|
|
2019-06-25 15:31:56 +02:00
|
|
|
fn test_left() {
|
|
|
|
a := [1, 2, 3]
|
|
|
|
b := a.left(2)
|
2019-10-27 00:37:29 +02:00
|
|
|
c := a[0..2]
|
2019-10-27 07:36:04 +01:00
|
|
|
d := a[..2]
|
2019-06-25 15:31:56 +02:00
|
|
|
assert b[0] == 1
|
|
|
|
assert b[1] == 2
|
2019-10-27 00:37:29 +02:00
|
|
|
assert c[0] == 1
|
|
|
|
assert c[1] == 2
|
2019-10-27 07:36:04 +01:00
|
|
|
assert d[0] == 1
|
|
|
|
assert d[1] == 2
|
2019-06-25 15:31:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_slice() {
|
|
|
|
a := [1, 2, 3, 4]
|
|
|
|
b := a.slice(2, 4)
|
2019-06-29 22:49:57 +02:00
|
|
|
assert b.len == 2
|
2019-06-25 15:31:56 +02:00
|
|
|
assert a.slice(1, 2).len == 1
|
2019-06-29 22:49:57 +02:00
|
|
|
assert a.len == 4
|
2019-06-25 15:31:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_push_many() {
|
|
|
|
mut a := [1, 2, 3]
|
|
|
|
b := [4, 5, 6]
|
2019-09-14 22:48:30 +02:00
|
|
|
a << b
|
2019-06-25 15:31:56 +02:00
|
|
|
assert a.len == 6
|
|
|
|
assert a[0] == 1
|
|
|
|
assert a[3] == 4
|
|
|
|
assert a[5] == 6
|
|
|
|
}
|
|
|
|
|
2019-07-17 18:17:07 +02:00
|
|
|
fn test_reverse() {
|
2019-10-04 22:07:19 +02:00
|
|
|
mut a := [1, 2, 3, 4]
|
2019-07-17 18:17:07 +02:00
|
|
|
mut b := ['test', 'array', 'reverse']
|
2019-07-17 18:35:40 +02:00
|
|
|
c := a.reverse()
|
|
|
|
d := b.reverse()
|
|
|
|
for i, _ in c {
|
|
|
|
assert c[i] == a[a.len-i-1]
|
|
|
|
}
|
|
|
|
for i, _ in d {
|
|
|
|
assert d[i] == b[b.len-i-1]
|
|
|
|
}
|
2019-07-17 18:17:07 +02:00
|
|
|
}
|
2019-08-04 03:59:12 +02:00
|
|
|
|
|
|
|
const (
|
|
|
|
N = 5
|
2019-09-14 22:48:30 +02:00
|
|
|
)
|
2019-08-04 03:59:12 +02:00
|
|
|
|
|
|
|
fn test_fixed() {
|
2019-09-14 22:48:30 +02:00
|
|
|
mut nums := [4]int
|
|
|
|
assert nums[0] == 0
|
|
|
|
assert nums[1] == 0
|
|
|
|
assert nums[2] == 0
|
|
|
|
assert nums[3] == 0
|
|
|
|
nums[1] = 7
|
|
|
|
assert nums[1] == 7
|
|
|
|
nums2 := [N]int
|
|
|
|
assert nums2[N - 1] == 0
|
|
|
|
}
|
2019-08-07 12:09:24 +02:00
|
|
|
|
|
|
|
fn modify (numbers mut []int) {
|
|
|
|
numbers[0] = 777
|
|
|
|
}
|
|
|
|
|
2019-09-14 22:48:30 +02:00
|
|
|
fn test_mut_slice() {
|
2019-08-07 12:57:07 +02:00
|
|
|
mut n := [1,2,3]
|
2019-09-14 22:48:30 +02:00
|
|
|
modify(mut n.left(2))
|
|
|
|
assert n[0] == 777
|
|
|
|
modify(mut n.right(2))
|
|
|
|
assert n[2] == 777
|
2019-08-07 12:57:07 +02:00
|
|
|
println(n)
|
2019-08-07 12:09:24 +02:00
|
|
|
}
|
2019-08-12 13:41:34 +02:00
|
|
|
|
|
|
|
fn test_clone() {
|
2019-09-14 22:48:30 +02:00
|
|
|
nums := [1, 2, 3, 4, 100]
|
|
|
|
nums2 := nums.clone()
|
|
|
|
assert nums2.len == 5
|
|
|
|
assert nums2.str() == '[1, 2, 3, 4, 100]'
|
|
|
|
assert nums.slice(1, 3).str() == '[2, 3]'
|
|
|
|
}
|
|
|
|
|
2019-08-22 03:27:57 +02:00
|
|
|
fn test_doubling() {
|
|
|
|
mut nums := [1, 2, 3, 4, 5]
|
|
|
|
for i := 0; i < nums.len; i++ {
|
|
|
|
nums[i] *= 2
|
|
|
|
}
|
|
|
|
assert nums.str() == '[2, 4, 6, 8, 10]'
|
2019-08-22 06:56:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
struct Test2 {
|
|
|
|
one int
|
|
|
|
two int
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Test {
|
|
|
|
a string
|
2019-08-30 19:19:06 +02:00
|
|
|
mut:
|
2019-08-22 06:56:29 +02:00
|
|
|
b []Test2
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (t Test2) str() string {
|
|
|
|
return '{$t.one $t.two}'
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (t Test) str() string {
|
|
|
|
return '{$t.a $t.b}'
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_struct_print() {
|
|
|
|
mut a := Test {
|
|
|
|
a: 'Test',
|
|
|
|
b: []Test2
|
|
|
|
}
|
|
|
|
b := Test2 {
|
|
|
|
one: 1,
|
|
|
|
two: 2
|
|
|
|
}
|
|
|
|
a.b << b
|
|
|
|
a.b << b
|
|
|
|
assert a.str() == '{Test [{1 2}, {1 2}] }'
|
|
|
|
assert b.str() == '{1 2}'
|
|
|
|
assert a.b.str() == '[{1 2}, {1 2}]'
|
|
|
|
}
|
2019-09-28 12:52:02 +02: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-10-04 22:07:19 +02:00
|
|
|
|
|
|
|
fn test_find_index() {
|
|
|
|
// string
|
|
|
|
a := ['v', 'is', 'great']
|
|
|
|
assert a.index('v') == 0
|
|
|
|
assert a.index('is') == 1
|
|
|
|
assert a.index('gre') == -1
|
|
|
|
|
|
|
|
// 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 15:03:13 +02:00
|
|
|
|
|
|
|
fn test_multi() {
|
|
|
|
a := [[1,2,3],[4,5,6]]
|
|
|
|
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
|
2019-10-06 03:18:55 +02:00
|
|
|
//b := [ [[1,2,3],[4,5,6]], [[1,2]] ]
|
|
|
|
//assert b[0][0][0] == 1
|
2019-10-09 14:26:27 +02:00
|
|
|
}
|
2019-10-06 03:27:29 +02:00
|
|
|
|
|
|
|
fn test_in() {
|
|
|
|
a := [1,2,3]
|
|
|
|
assert 1 in a
|
|
|
|
assert 2 in a
|
|
|
|
assert 3 in a
|
|
|
|
assert !(4 in a)
|
|
|
|
assert !(0 in a)
|
2019-10-09 14:26:27 +02:00
|
|
|
}
|
2019-10-08 12:23:17 +02:00
|
|
|
|
2019-10-11 03:12:40 +02:00
|
|
|
fn sum(prev int, curr int) int {
|
|
|
|
return prev + curr
|
|
|
|
}
|
|
|
|
|
|
|
|
fn sub(prev int, curr int) int {
|
|
|
|
return prev - curr
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2019-10-17 09:44:20 +02:00
|
|
|
|
|
|
|
fn test_filter() {
|
|
|
|
a := [1, 2, 3, 4, 5, 6]
|
|
|
|
b := a.filter(it % 2 == 0)
|
2019-10-26 13:32:56 +02:00
|
|
|
assert b.len == 3
|
2019-10-17 09:44:20 +02: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'
|
|
|
|
}
|
2019-10-25 21:40:52 +02:00
|
|
|
|
2019-10-26 13:32:56 +02:00
|
|
|
fn test_map() {
|
|
|
|
a := [1, 2, 3, 4, 5, 6]
|
|
|
|
b := a.map(it * 10)
|
|
|
|
assert b.len == 6
|
|
|
|
assert b[0] == 10
|
|
|
|
assert b[1] == 20
|
|
|
|
assert b[2] == 30
|
|
|
|
c := ['v', 'is', 'awesome']
|
|
|
|
d := c.map(it.to_upper())
|
|
|
|
assert d[0] == 'V'
|
|
|
|
assert d[1] == 'IS'
|
|
|
|
assert d[2] == 'AWESOME'
|
2019-10-26 13:53:55 +02:00
|
|
|
bools := c.map(it == 'v')
|
|
|
|
assert bools.len == 3
|
|
|
|
assert bools[0] == true
|
|
|
|
assert bools[1] == false
|
|
|
|
assert bools[2] == false
|
|
|
|
|
2019-10-26 13:32:56 +02:00
|
|
|
}
|
|
|
|
|
2019-10-25 21:40:52 +02:00
|
|
|
fn test_array_str() {
|
|
|
|
numbers := [1, 2, 3]
|
|
|
|
numbers2 := [numbers, [4, 5, 6]] // dup str() bug
|
|
|
|
assert true
|
|
|
|
assert numbers.str() == '[1, 2, 3]'
|
|
|
|
assert numbers2.str() == '[[1, 2, 3], [4, 5, 6]]'
|
|
|
|
}
|