-
-
Notifications
You must be signed in to change notification settings - Fork 56
/
MathTestHalmos.t.sol
156 lines (138 loc) · 5.48 KB
/
MathTestHalmos.t.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// SPDX-License-Identifier: WTFPL
pragma solidity ^0.8.28;
import {Test} from "forge-std/Test.sol";
import {SymTest} from "halmos-cheatcodes/SymTest.sol";
import {VyperDeployer} from "utils/VyperDeployer.sol";
import {Math} from "openzeppelin/utils/math/Math.sol";
import {FixedPointMathLib} from "solady/utils/FixedPointMathLib.sol";
import {IMath} from "../interfaces/IMath.sol";
/**
* @dev Set the timeout (in milliseconds) for solving assertion violation
* conditions; `0` means no timeout.
* @custom:halmos --solver-timeout-assertion 0
*/
contract MathTestHalmos is Test, SymTest {
VyperDeployer private vyperDeployer = new VyperDeployer();
IMath private math;
/**
* @dev Set the timeout (in milliseconds) for solving branching conditions;
* `0` means no timeout.
* @custom:halmos --solver-timeout-branching 1000
*/
function setUp() public {
/**
* @dev Halmos does not currently work with the latest Vyper jump-table-based
* dispatchers: https://github.com/a16z/halmos/issues/253. For Halmos-based tests,
* we therefore disable the optimiser. Furthermore, Halmos does not currently
* work with the EVM version `cancun`: https://github.com/a16z/halmos/issues/290.
* For Halmos-based tests, we therefore use the EVM version `shanghai`.
*/
math = IMath(
vyperDeployer.deployContract(
"src/snekmate/utils/mocks/",
"math_mock",
"shanghai",
"none"
)
);
}
function testHalmosAssertUint256Average(uint256 x, uint256 y) public view {
assertEq(math.uint256_average(x, y), Math.average(x, y));
}
function testHalmosAssertInt256Average(int256 x, int256 y) public view {
assertEq(math.int256_average(x, y), FixedPointMathLib.avg(x, y));
}
function testHalmosAssertCeilDiv(uint256 x, uint256 y) public view {
assertEq(math.ceil_div(x, y), Math.ceilDiv(x, y));
}
function testHalmosAssertSignum(int256 x) public view {
int256 signum;
// solhint-disable-next-line no-inline-assembly
assembly ("memory-safe") {
signum := sub(sgt(x, 0), slt(x, 0))
}
assertEq(math.signum(x), signum);
}
/**
* @dev Currently commented out, as the timeout for the Yices 2 SMT solver does not
* work for the queries of this test, where the Yices 2 SMT solver is constantly
* running and consumes a lot of memory, causing the CI to crash due to out of memory.
*/
// function testHalmosAssertMulDiv(
// uint256 x,
// uint256 y,
// uint256 denominator,
// Math.Rounding rounding
// ) public view {
// assertEq(
// math.mul_div(x, y, denominator, Math.unsignedRoundsUp(rounding)),
// Math.mulDiv(x, y, denominator, rounding)
// );
// }
function testHalmosAssertLog2(
uint256 x,
Math.Rounding rounding
) public view {
assertEq(
math.log2(x, Math.unsignedRoundsUp(rounding)),
Math.log2(x, rounding)
);
}
function testHalmosAssertLog10(
uint256 x,
Math.Rounding rounding
) public view {
assertEq(
math.log10(x, Math.unsignedRoundsUp(rounding)),
Math.log10(x, rounding)
);
}
function testHalmosAssertLog256(
uint256 x,
Math.Rounding rounding
) public view {
assertEq(
math.log256(x, Math.unsignedRoundsUp(rounding)),
Math.log256(x, rounding)
);
}
/**
* @dev Currently commented out, as the timeout for the Yices 2 SMT solver does not
* work for the queries of this test, where the Yices 2 SMT solver is constantly
* running and consumes a lot of memory, causing the CI to crash due to out of memory.
*/
// function testHalmosAssertWadLn(int256 x) public view {
// assertEq(math.wad_ln(x), FixedPointMathLib.lnWad(x));
// }
/**
* @dev Currently commented out, as the timeout for the Yices 2 SMT solver does not
* work for the queries of this test, where the Yices 2 SMT solver is constantly
* running and consumes a lot of memory, causing the CI to crash due to out of memory.
*/
// function testHalmosAssertWadExp(int256 x) public view {
// assertEq(math.wad_exp(x), FixedPointMathLib.expWad(x));
// }
/**
* @dev Currently commented out, as the timeout for the Yices 2 SMT solver does not
* work for the queries of this test, where the Yices 2 SMT solver is constantly
* running and consumes a lot of memory, causing the CI to crash due to out of memory.
*/
// function testHalmosAssertCbrt(uint256 x, bool roundup) public view {
// if (!roundup) {
// assertEq(math.cbrt(x, roundup), FixedPointMathLib.cbrt(x));
// } else {
// assertTrue(
// math.cbrt(x, roundup) >= FixedPointMathLib.cbrt(x) &&
// math.cbrt(x, roundup) <= FixedPointMathLib.cbrt(x) + 1
// );
// }
// }
/**
* @dev Currently commented out, as the timeout for the Yices 2 SMT solver does not
* work for the queries of this test, where the Yices 2 SMT solver is constantly
* running and consumes a lot of memory, causing the CI to crash due to out of memory.
*/
// function testHalmosAssertWadCbrt(uint256 x) public view {
// assertEq(math.wad_cbrt(x), FixedPointMathLib.cbrtWad(x));
// }
}