Skip to content

Commit

Permalink
test: add nonlinear tests
Browse files Browse the repository at this point in the history
  • Loading branch information
daejunpark committed Sep 25, 2023
1 parent 5926a9d commit e255131
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
18 changes: 18 additions & 0 deletions tests/expected/all.json
Original file line number Diff line number Diff line change
Expand Up @@ -778,6 +778,24 @@
"num_paths": null,
"time": null,
"num_bounded_loops": null
},
{
"name": "check_deposit(uint256,uint256,uint256)",
"exitcode": 1,
"num_models": 0,
"models": null,
"num_paths": null,
"time": null,
"num_bounded_loops": null
},
{
"name": "check_mint(uint256,uint256,uint256)",
"exitcode": 1,
"num_models": 1,
"models": null,
"num_paths": null,
"time": null,
"num_bounded_loops": null
}
],
"test/Natspec.t.sol:NatspecTestContract": [
Expand Down
2 changes: 2 additions & 0 deletions tests/foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ libs = ['lib']
# See more config options https://github.com/foundry-rs/foundry/tree/master/config
force = false
evm_version = 'shanghai'

extra_output = ["storageLayout", "metadata"]
41 changes: 41 additions & 0 deletions tests/test/Math.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,45 @@ contract MathTest {
assert(r1 == r2);
}
}

function _check_deposit(uint a, uint A1, uint S1) public pure returns (bool) {
uint s = (a * S1) / A1;

uint A2 = A1 + a;
uint S2 = S1 + s;

return (A1 / S1 <= A2 / S2); // always true
}

// NOTE: currently timeout when --smt-div is enabled; producing invalid counterexamples when --smt-div is not given
function check_deposit(uint a, uint A1, uint S1) public pure {
assert(_check_deposit(a, A1, S1)); // no counterexample
}

function test_deposit(uint a, uint A1, uint S1) public {
(bool success, bytes memory retdata) = address(this).call(abi.encodeWithSelector(this._check_deposit.selector, a, A1, S1));
if (!success) return;
assert(abi.decode(retdata, (bool)));
}

function _check_mint(uint s, uint A1, uint S1) public pure returns (bool) {
uint a = (s * A1) / S1;

uint A2 = A1 + a;
uint S2 = S1 + s;

// (A1 / S1 <= A2 / S2)
return (A1 * S2 <= A2 * S1); // can be false
}

/// @custom:halmos --smt-div
function check_mint(uint s, uint A1, uint S1) public pure {
assert(_check_mint(s, A1, S1)); // counterexamples exist
}

function test_mint(uint s, uint A1, uint S1) public {
(bool success, bytes memory retdata) = address(this).call(abi.encodeWithSelector(this._check_mint.selector, s, A1, S1));
if (!success) return;
assert(abi.decode(retdata, (bool)));
}
}

0 comments on commit e255131

Please sign in to comment.