You should be familiar with the concept of [Equivalence Partitioning](/codex/equivalence-partitioning) and equivalence classes to understand boundary value analysis.
A boundary value is a value resting on the boundary of a given equivalence class. Boundary Value Analysis is a testing heuristic used to identify new inputs to test. It relies on finding boundary values of each equivalence classes and making sure they are involved in test cases.
Like Equivalence Partitioning, Boundary Value Analysis can also be applied to partitions of outputs or intermediate values.
### Worked example
Let's consider how we would test the `toString` function from OpenZeppelin contracts using Boundary Value Analysis.
-- CODE language-solidity --
/**
* @dev Converts a `uint256` to its ASCII `string` decimal representation.
*/
function toString(uint256 value) internal pure returns (string memory) {
// Inspired by OraclizeAPI's implementation - MIT licence
// https://github.com/oraclize/ethereum-api/blob/b42146b063c7d6ee1358846c198246239e9360e8/oraclizeAPI_0.4.25.sol
if (value == 0) {
return "0";
}
uint256 temp = value;
uint256 digits;
while (temp != 0) {
digits++;
temp /= 10;
}
bytes memory buffer = new bytes(digits);
while (value != 0) {
digits -= 1;
buffer[digits] = bytes1(uint8(48 + uint256(value % 10)));
value /= 10;
}
return string(buffer);
}
In [Equivalence Partitioning](/codex/equivalence-partitioning), we already identified the equivalence classes to test. Below we list their boundary values:
- `0` (boundary value `0`)
- `1-9` (boundary values `1` and `9`)
- `10+` (boundary values `10` and `MAX_UINT256`).
### Robust Boundary Value Testing
An extension of normal boundary value testing is when a minimum perturbation is tested just outside the equivalence class boundary, for example a value slightly larger than a maximum value or a value slightly lower than a minimum value.