Skip to content

Latest commit

 

History

History
41 lines (32 loc) · 1.49 KB

File metadata and controls

41 lines (32 loc) · 1.49 KB

Statement Testing

Statement Testing is a heuristic to identify new test cases. The existing test suite is analyzed for coverage. If a statement is not covered in the test suite, a new test case can be developed which evaluates this statement. Statement coverage is a stricter form of line coverage.

Worked example

Consider the OpenZeppelin tryMul function below:

/**
 * @dev Returns the multiplication of two unsigned integers, with an overflow flag.
 *
 * _Available since v3.4._
 */
function tryMul(uint256 a, uint256 b) internal pure returns (bool, uint256) {
    unchecked {
        // Gas optimization: this is cheaper than requiring 'a' not being zero, but the
        // benefit is lost if 'b' is also tested.
        // See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522
        if (a == 0) return (true, 0);
        uint256 c = a * b;
        if (c / a != b) return (false, 0);
        return (true, c);
    }
}

This code has the following statements:

  • a == 0
  • return (true, 0)
  • uint256 c = a * b
  • c / a != b
  • return (false, 0)
  • return (true, c) .

Note that there can be multiple statements per line in the above example (unlike line coverage).

Tools