Skip to content

Commit

Permalink
♻️ Fix tests (#1080)
Browse files Browse the repository at this point in the history
  • Loading branch information
Vectorized authored Sep 18, 2024
1 parent 6b5cf98 commit a3da4b3
Showing 1 changed file with 25 additions and 6 deletions.
31 changes: 25 additions & 6 deletions test/DynamicArrayLib.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -201,14 +201,33 @@ contract DynamicArrayLibTest is SoladyTest {
assertEq(a.slice(1, 1).hash(), DynamicArrayLib.p().hash());
}

function testDynamicArraySlice(uint256[] calldata data, uint256 start, uint256 end) public {
function testDynamicArraySlice(uint256[] memory data, uint256 start, uint256 end) public {
DynamicArrayLib.DynamicArray memory a;
a.data = data;
start = _bound(start, 0, a.data.length);
end = _bound(end, 0, a.data.length);
if (end < start) {
(start, end) = (end, start);
unchecked {
start = _bound(start, 0, a.data.length + 2);
end = _bound(end, 0, a.data.length + 2);
DynamicArrayLib.DynamicArray memory slice = a.slice(start, end);
_checkMemory(slice.data);
assertEq(slice.data, _sliceOriginal(data, start, end));
}
}

function _sliceOriginal(uint256[] memory a, uint256 start, uint256 end)
internal
pure
returns (uint256[] memory result)
{
if (start > a.length) start = a.length;
if (end > a.length) end = a.length;
unchecked {
if (start < end) {
uint256 n = end - start;
result = new uint256[](n);
for (uint256 i; i != n; ++i) {
result[i] = a[start + i];
}
}
}
assertEq(a.slice(start, end).asUint256Array(), data[start:end]);
}
}

0 comments on commit a3da4b3

Please sign in to comment.