1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

builtin: add an array.drop(n) method (#13907)

This commit is contained in:
Nick Treleaven
2022-04-03 15:05:50 +01:00
committed by GitHub
parent a1e9cae5d2
commit 782d5374c9
2 changed files with 56 additions and 0 deletions

View File

@ -1046,6 +1046,40 @@ fn test_trim() {
assert arr.last() == 2
}
[manualfree]
fn test_drop() {
mut a := [1, 2]
a << 3 // pushing assures reallocation; a.cap now should be bigger:
assert a.cap > 3
// eprintln('>>> a.cap: $a.cap | a.len: $a.len')
a.drop(-1000)
assert a == [1, 2, 3] // a.drop( negative ) should NOT modify the array
// eprintln('>>> a.cap: $a.cap | a.len: $a.len')
a.drop(2)
assert a == [3]
assert a.cap > a.len
// eprintln('>>> a.cap: $a.cap | a.len: $a.len')
a.drop(10)
assert a == []
assert a.cap > a.len
// eprintln('>>> a.cap: $a.cap | a.len: $a.len')
a << 123
a << 456
a << 789
// eprintln('>>> a.cap: $a.cap | a.len: $a.len')
assert a == [123, 456, 789]
a.drop(10)
assert a == []
// eprintln('>>> a.cap: $a.cap | a.len: $a.len')
unsafe { a.free() } // test offset OK
}
fn test_hex() {
// array hex
st := [byte(`V`), `L`, `A`, `N`, `G`]