Skip to content

Instantly share code, notes, and snippets.

@aviggiano
Created June 7, 2024 14:52
Show Gist options
  • Save aviggiano/6f8b27d9364504d738b7ba3422259458 to your computer and use it in GitHub Desktop.
Save aviggiano/6f8b27d9364504d738b7ba3422259458 to your computer and use it in GitHub Desktop.
MathTest.t.sol
contract MathTest is Test {
function check_Math_binarySearch(uint256[] memory array, uint256 value) public {
// array is strictly increasing
for (uint256 i = 0; i < array.length - 1; i++) {
vm.assume(array[i] < array[i + 1]);
}
(uint256 low, uint256 high) = Math.binarySearch(array, value);
if (value < array[0] || value > array[array.length - 1]) {
// not found
assertEq(low, type(uint256).max, "not found");
assertEq(high, type(uint256).max, "not found");
} else {
assertGe(low, 0, "low is within bounds");
assertLe(low, array.length - 1, "low is within bounds");
assertGe(high, 0, "high is within bounds");
assertLe(high, array.length - 1, "high is within bounds");
assertLe(low, high, "low <= high");
assertLe(array[low], value, "array[low] <= value <= array[high]");
assertLe(value, array[high], "array[low] <= value <= array[high]");
if (value == array[low]) {
assertEq(low, high, "low == high if the value is in the array");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment