v / vlib / builtin
Raw file | 78 loc (66 sloc) | 1.86 KB | Latest commit hash 278c08704
1fn test_gated_arrays() {
2 a := [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
3 assert a#[-1..] == [9]
4 assert a#[..-9] == [0]
5 assert a#[-9..-7] == [1, 2]
6 assert a#[-2..] == [8, 9]
7
8 // fixed array
9 a1 := [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]!
10 assert a1#[-1..] == [9]
11 assert a1#[..-9] == [0]
12 assert a1#[-9..-7] == [1, 2]
13 assert a1#[-2..] == [8, 9]
14
15 // empty array
16 assert a#[-3..-4] == [] // start > end
17 assert a#[20..] == [] // start > array.len
18 assert a#[-20..-10] == [] // start+len < 0
19 assert a#[20..-9] == [] // start > end && start > end
20}
21
22fn test_gated_strings() {
23 a := '0123456789'
24 assert a#[-1..] == '9'
25 assert a#[..-9] == '0'
26 assert a#[-9..-7] == '12'
27 assert a#[-2..] == '89'
28
29 // empty string
30 assert a#[-3..-4] == '' // start > end
31 assert a#[20..] == '' // start > array.len
32 assert a#[-20..-10] == '' // start+len < 0
33 assert a#[20..-9] == '' // start > end && start > end
34
35 //
36 // test negative indexes in slices from github discussion
37 //
38 s := '0123456789'
39
40 // normal behaviour
41 assert s#[1..3] == '12'
42 assert s#[..3] == '012'
43 assert s#[8..] == '89'
44
45 // negative indexes behaviour
46 assert s#[-2..] == '89'
47 assert s#[..-8] == '01'
48 assert s#[2..-2] == '234567'
49 assert s#[-12..-16] == ''
50 assert s#[-8..-2] == '234567'
51
52 // out of bound both indexes
53 assert s#[12..14] == ''
54 assert s#[-12..16] == '0123456789'
55}
56
57fn test_gated_mixed_strings() {
58 //
59 // test negative indexes in slices
60 //
61 a := [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
62
63 // normal behaviour
64 assert a#[1..3].str() == '[1, 2]'
65 assert a#[..3].str() == '[0, 1, 2]'
66 assert a#[8..].str() == '[8, 9]'
67
68 // negative indexes behaviour
69 assert a#[-2..].str() == '[8, 9]'
70 assert a#[..-8].str() == '[0, 1]'
71 assert a#[2..-2].str() == '[2, 3, 4, 5, 6, 7]'
72 assert a#[-12..-16].str() == '[]'
73 assert a#[-8..-2].str() == '[2, 3, 4, 5, 6, 7]'
74
75 // out of bound both indexes
76 assert a#[12..14].str() == '[]'
77 assert a#[-12..16].str() == a.str()
78}