Skip to content

Commit

Permalink
docs: add assertApprox... assertions. (#1107)
Browse files Browse the repository at this point in the history
* Update ds-test.md

* Update ds-test.md
  • Loading branch information
magnetto90 authored Jan 30, 2024
1 parent d2d28dc commit d3617a5
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions src/reference/ds-test.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,14 @@ function assertLeDecimal(int a, int b, uint decimals) internal;
function assertLeDecimal(int a, int b, uint decimals, string memory err) internal;
function assertLeDecimal(uint a, uint b, uint decimals) internal;
function assertLeDecimal(uint a, uint b, uint decimals, string memory err) internal;
// Asserts `a` is approximately equal to `b` with delta in absolute value.
function assertApproxEqAbs(uint256 a, uint256 b, uint256 maxDelta) internal;
function assertApproxEqAbs(uint256 a, uint256 b, uint256 maxDelta, string memory err) internal;
// Asserts `a` is approximately equal to `b` with delta in percentage, where `1e18` is 100%.
function assertApproxEqRel(uint256 a, uint256 b, uint256 maxPercentDelta) internal;
function assertApproxEqRel(uint256 a, uint256 b, uint256 maxPercentDelta, string memory err) internal;
```

### Assertion functions
Expand Down Expand Up @@ -477,6 +485,55 @@ uint256 a = 1 ether;
uint256 b = 1e18 wei;
assertLeDecimal(a, b, 18);
```

<br>

---

#### `assertApproxEqAbs`

```solidity
function assertApproxEqAbs(<type> a, <type> b, uint256 maxDelta) internal;
```

Where `<type>` can be `int`, `uint`

Asserts `a` is approximately equal to `b` with delta in absolute value.

##### Example

```solidity
function testFail () external {
uint256 a = 100;
uint256 b = 200;
assertApproxEqAbs(a, b, 90);
}
```

<br>

---

#### `assertApproxEqRel`

```solidity
function assertApproxEqRel(<type> a, <type> b, uint256 maxPercentDelta) internal;
```

Where `<type>` can be `int`, `uint`

Asserts `a` is approximately equal to `b` with delta in percentage, where `1e18` is 100%.

##### Example

```solidity
function testFail () external {
uint256 a = 100;
uint256 b = 200;
assertApproxEqRel(a, b, 0.4e18);
}
```
<br>

> ℹ️ **Information**
Expand Down

0 comments on commit d3617a5

Please sign in to comment.