Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions builtin/arrayview.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -234,10 +234,11 @@ pub fn op_as_view[T](
let len = self.length()
let end = match end {
None => len
Some(end) => end
Some(end) => if end < 0 { len + end } else { end }
}
let start = if start < 0 { len + start } else { start }
guard start >= 0 && start <= end && end <= len else {
abort("View start index out of bounds")
abort("View index out of bounds")
}
ArrayView::{ buf: self.buffer(), start, len: end - start }
}
Expand Down Expand Up @@ -289,10 +290,11 @@ pub fn op_as_view[T](
let len = self.length()
let end = match end {
None => len
Some(end) => end
Some(end) => if end < 0 { len + end } else { end }
}
let start = if start < 0 { len + start } else { start }
guard start >= 0 && start <= end && end <= len else {
abort("View start index out of bounds")
abort("View index out of bounds")
}
ArrayView::{ buf: self.buf, start: self.start + start, len: end - start }
}
Expand Down
110 changes: 109 additions & 1 deletion builtin/arrayview_test.mbt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2024 International Digital Economy Academy
// Copyright 2025 International Digital Economy Academy
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -120,3 +120,111 @@ test "arrayview_filter" {
assert_eq!(filtered.length(), 1)
assert_eq!(filtered[0], 4)
}

test "negative index1" {
let arr = [1, 2, 3]
let view = arr[-1:]
inspect!(view, content="[3]")
let view = arr[-2:]
inspect!(view, content="[2, 3]")
let view = arr[-3:]
inspect!(view, content="[1, 2, 3]")
let view = arr[:-1]
inspect!(view, content="[1, 2]")
let view = arr[:-2]
inspect!(view, content="[1]")
let view = arr[:-3]
inspect!(view, content="[]")
let view = arr[-3:-3]
inspect!(view, content="[]")
let view = arr[-3:-2]
inspect!(view, content="[1]")
let view = arr[-3:-1]
inspect!(view, content="[1, 2]")
let view = arr[-3:0]
inspect!(view, content="[]")
let view = arr[-3:1]
inspect!(view, content="[1]")
let view = arr[-3:2]
inspect!(view, content="[1, 2]")
let view = arr[-3:3]
inspect!(view, content="[1, 2, 3]")
}

test "negative index2" {
let arr = [0, 1, 2, 3, 4][1:4]
let view = arr[-1:]
inspect!(view, content="[3]")
let view = arr[-2:]
inspect!(view, content="[2, 3]")
let view = arr[-3:]
inspect!(view, content="[1, 2, 3]")
let view = arr[:-1]
inspect!(view, content="[1, 2]")
let view = arr[:-2]
inspect!(view, content="[1]")
let view = arr[:-3]
inspect!(view, content="[]")
let view = arr[-3:-3]
inspect!(view, content="[]")
let view = arr[-3:-2]
inspect!(view, content="[1]")
let view = arr[-3:-1]
inspect!(view, content="[1, 2]")
let view = arr[-3:0]
inspect!(view, content="[]")
let view = arr[-3:1]
inspect!(view, content="[1]")
let view = arr[-3:2]
inspect!(view, content="[1, 2]")
let view = arr[-3:3]
inspect!(view, content="[1, 2, 3]")
}

test "panic negative index1" {
let arr = [1, 2, 3]
let _ = arr[-4:]

}

test "panic negative index2" {
let arr = [1, 2, 3]
let _ = arr[:-4]

}

test "panic negative index3" {
let arr = [1, 2, 3]
let _ = arr[-1:-2]

}

test "panic negative index4" {
let arr = [1, 2, 3]
let _ = arr[-1:-3]

}

test "panic negative index5" {
let arr = [1, 2, 3][:]
let _ = arr[-4:]

}

test "panic negative index6" {
let arr = [1, 2, 3][:]
let _ = arr[:-4]

}

test "panic negative index7" {
let arr = [1, 2, 3][:]
let _ = arr[-1:-2]

}

test "panic negative index8" {
let arr = [1, 2, 3][:]
let _ = arr[-1:-3]

}
6 changes: 4 additions & 2 deletions builtin/bytesview.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,9 @@ pub fn op_as_view(self : Bytes, start~ : Int = 0, end? : Int) -> BytesView {
let len = self.length()
let end = match end {
None => len
Some(end) => end
Some(end) => if end < 0 { len + end } else { end }
}
let start = if start < 0 { len + start } else { start }
guard start >= 0 && start <= end && end <= len else {
abort("Invalid index for BytesView")
}
Expand All @@ -94,8 +95,9 @@ pub fn op_as_view(self : BytesView, start~ : Int = 0, end? : Int) -> BytesView {
let len = self.length()
let end = match end {
None => len
Some(end) => end
Some(end) => if end < 0 { len + end } else { end }
}
let start = if start < 0 { len + start } else { start }
guard start >= 0 && start <= end && end <= len else {
abort("Invalid index for BytesView")
}
Expand Down
208 changes: 205 additions & 3 deletions builtin/bytesview_test.mbt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2024 International Digital Economy Academy
// Copyright 2025 International Digital Economy Academy
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -46,7 +46,7 @@ test "basic3" {

test "panic invalid index1" {
let bs = b"\x00\x01\x02\x03\x04\x05"
let _ = bs[-1:]
let _ = bs[-7:]

}

Expand All @@ -64,7 +64,7 @@ test "panic invalid index3" {

test "panic invalid index4" {
let bs = b"\x00\x01\x02\x03\x04\x05"[:]
let _ = bs[-1:]
let _ = bs[:-7]

}

Expand All @@ -88,3 +88,205 @@ test "iter" {
content="[b'\\x00', b'\\x01', b'\\x02', b'\\x03', b'\\x04', b'\\x05']",
)
}

test "negative index1" {
let bs = b"\x01\x02\x03"
let bv = bs[-1:]
inspect!(
bv,
content=
#|b"\x03"
,
)
let bv = bs[-2:]
inspect!(
bv,
content=
#|b"\x02\x03"
,
)
let bv = bs[-3:]
inspect!(
bv,
content=
#|b"\x01\x02\x03"
,
)
let bv = bs[:-1]
inspect!(
bv,
content=
#|b"\x01\x02"
,
)
let bv = bs[:-2]
inspect!(
bv,
content=
#|b"\x01"
,
)
let bv = bs[:-3]
inspect!(
bv,
content=
#|b""
,
)
let bv = bs[-3:-3]
inspect!(
bv,
content=
#|b""
,
)
let bv = bs[-3:-2]
inspect!(
bv,
content=
#|b"\x01"
,
)
let bv = bs[-3:-1]
inspect!(
bv,
content=
#|b"\x01\x02"
,
)
let bv = bs[-3:0]
inspect!(
bv,
content=
#|b""
,
)
let bv = bs[-3:1]
inspect!(
bv,
content=
#|b"\x01"
,
)
let bv = bs[-3:2]
inspect!(
bv,
content=
#|b"\x01\x02"
,
)
let bv = bs[-3:3]
inspect!(
bv,
content=
#|b"\x01\x02\x03"
,
)
}

test "negative index2" {
let bs = b"\x00\x01\x02\x03\x04"[1:4]
let bv = bs[-1:]
inspect!(
bv,
content=
#|b"\x03"
,
)
let bv = bs[-2:]
inspect!(
bv,
content=
#|b"\x02\x03"
,
)
let bv = bs[-3:]
inspect!(
bv,
content=
#|b"\x01\x02\x03"
,
)
let bv = bs[:-1]
inspect!(
bv,
content=
#|b"\x01\x02"
,
)
let bv = bs[:-2]
inspect!(
bv,
content=
#|b"\x01"
,
)
let bv = bs[:-3]
inspect!(
bv,
content=
#|b""
,
)
let bv = bs[-3:-3]
inspect!(
bv,
content=
#|b""
,
)
let bv = bs[-3:-2]
inspect!(
bv,
content=
#|b"\x01"
,
)
let bv = bs[-3:-1]
inspect!(
bv,
content=
#|b"\x01\x02"
,
)
let bv = bs[-3:0]
inspect!(
bv,
content=
#|b""
,
)
let bv = bs[-3:1]
inspect!(
bv,
content=
#|b"\x01"
,
)
let bv = bs[-3:2]
inspect!(
bv,
content=
#|b"\x01\x02"
,
)
let bv = bs[-3:3]
inspect!(
bv,
content=
#|b"\x01\x02\x03"
,
)
}

test "panic negative index1" {
let bs = b"\x01\x02\x03"
let _ = bs[-1:-2]

}

test "panic negative index2" {
let bs = b"\x01\x02\x03"[:]
let _ = bs[-1:-2]

}
Loading
Loading