diff --git a/include/chaiscript/dispatchkit/bootstrap_stl.hpp b/include/chaiscript/dispatchkit/bootstrap_stl.hpp index 7592c511..f56304d7 100644 --- a/include/chaiscript/dispatchkit/bootstrap_stl.hpp +++ b/include/chaiscript/dispatchkit/bootstrap_stl.hpp @@ -130,7 +130,7 @@ namespace chaiscript::bootstrap::standard_library { auto itr = container.begin(); auto end = container.end(); - if (pos < 0 || std::distance(itr, end) < (pos - 1)) { + if (pos < 0 || std::distance(itr, end) <= pos) { throw std::range_error("Cannot erase past end of range"); } diff --git a/unittests/vector_erase_at.chai b/unittests/vector_erase_at.chai index 348cd819..1e5bde73 100644 --- a/unittests/vector_erase_at.chai +++ b/unittests/vector_erase_at.chai @@ -1,3 +1,24 @@ auto x = [1, 2, 3] x.erase_at(1) assert_equal([1,3], x); + +try { + // We expect this to throw because of erasing an out of bounds index + x.erase_at(2) + assert_true(false) +} catch (e) { + assert_true(true) +} + +try { + // We expect this to throw because of erasing an out of bounds index + x.erase_at(-1) + assert_true(false) +} catch (e) { + assert_true(true) +} + +x.erase_at(0) +assert_equal([3], x) +x.erase_at(0) +assert_equal([], x) diff --git a/unittests/vector_insert_at.chai b/unittests/vector_insert_at.chai index 1b42e1cf..3ad9ec5c 100644 --- a/unittests/vector_insert_at.chai +++ b/unittests/vector_insert_at.chai @@ -1,3 +1,25 @@ auto x = [1, 2, 3] x.insert_at(1, 6) assert_equal([1,6,2,3], x); + +try { + // We expect this to throw because of inserting an out of bounds index + x.insert_at(5, 55) + assert_true(false) +} catch (e) { + assert_true(true) +} + +// Inserting to the end should be allowed +x.insert_at(4, 44) + +try { + // We expect this to throw because of inserting an out of bounds index + x.insert_at(-1, 111) + assert_true(false) +} catch (e) { + assert_true(true) +} + +x.insert_at(0, 100) +assert_equal([100, 1, 6, 2, 3, 44], x)