Skip to content

Commit 5599ca1

Browse files
authored
Fix ResizableArray bounds check (#838)
Fixed a bug where the bounds check of ResizableArray used `&&` instead of `||` for the bounds check condition, leading to it never `raise()`ing OutOfBounds. Added a Test to check OutOfBounds on the upper and lower conditions.
1 parent b09c05e commit 5599ca1

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
ResizableArrayTests
22
✓ usage as stack
3+
✓ out of bounds check
34

4-
1 pass
5+
2 pass
56
0 fail
6-
1 tests total
7+
2 tests total

examples/stdlib/resizable_array/resizable_array.effekt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,19 @@ def main() = {
2727
assert(a.popRight(), 1)
2828
assert(a.popRight(), 1)
2929
}
30+
test("out of bounds check") {
31+
def checkOutOfBounds(arr: ResizableArray[Int], index: Int) = {
32+
with on[OutOfBounds].default { () }
33+
arr.get(index)
34+
assertTrue(false, "not out of bounds")
35+
}
36+
with on[OutOfBounds].default { assertTrue(false, "out of bounds") }
37+
val a = resizableArray()
38+
a.checkOutOfBounds(-1)
39+
a.add(1)
40+
a.add(2)
41+
a.checkOutOfBounds(2)
42+
}
3043
};
3144
()
3245
}

libraries/common/resizable_array.effekt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def resizableArray[T](): ResizableArray[T] = resizableArray(8)
2929

3030
/// Throw an OutOfBounds exception if index is not a valid index into arr
3131
def boundsCheck[T](arr: ResizableArray[T], index: Int): Unit / Exception[OutOfBounds] = {
32-
if (index < 0 && index >= arr.size) {
32+
if (index < 0 || index >= arr.size) {
3333
do raise(OutOfBounds(), "Array index out of bounds: " ++ show(index))
3434
}
3535
}

0 commit comments

Comments
 (0)