-
-
Notifications
You must be signed in to change notification settings - Fork 125
/
avg.t.sol
91 lines (79 loc) · 3.36 KB
/
avg.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
// SPDX-License-Identifier: UNLICENSED
pragma solidity >=0.8.13 <0.9.0;
import { MAX_UD60x18, MAX_WHOLE_UD60x18, ZERO } from "src/ud60x18/Constants.sol";
import { avg } from "src/ud60x18/Math.sol";
import { UD60x18 } from "src/ud60x18/ValueType.sol";
import { UD60x18_Test } from "../../UD60x18.t.sol";
contract Avg_Test is UD60x18_Test {
function test_Avg_BothOperandsZero() external {
UD60x18 x = ZERO;
UD60x18 y = ZERO;
UD60x18 actual = avg(x, y);
UD60x18 expected = ZERO;
assertEq(actual, expected);
}
function onlyOneOperandZero_Sets() internal returns (Set[] memory) {
delete sets;
sets.push(set({ x: 0, y: 3e18, expected: 1.5e18 }));
sets.push(set({ x: 3e18, y: 0, expected: 1.5e18 }));
return sets;
}
function test_Avg_OnlyOneOperandZero() external parameterizedTest(onlyOneOperandZero_Sets()) {
UD60x18 actual = avg(s.x, s.y);
assertEq(actual, s.expected);
}
modifier neitherOperandZero() {
_;
}
function bothOperandsEven_Sets() internal returns (Set[] memory) {
delete sets;
sets.push(set({ x: 2, y: 4, expected: 3 }));
sets.push(set({ x: 2e18, y: 2e18, expected: 2e18 }));
sets.push(set({ x: 4e18, y: 8e18, expected: 6e18 }));
sets.push(set({ x: 100e18, y: 200e18, expected: 150e18 }));
sets.push(set({ x: 1e24, y: 1e25, expected: 5.5e24 }));
return sets;
}
function test_Avg_NeitherOperandZero_BothOperandsEven() external parameterizedTest(bothOperandsEven_Sets()) neitherOperandZero {
UD60x18 actual = avg(s.x, s.y);
assertEq(actual, s.expected);
}
function bothOperandsOdd_Sets() internal returns (Set[] memory) {
delete sets;
sets.push(set({ x: 1, y: 3, expected: 2 }));
sets.push(set({ x: 1e18 + 1, y: 1e18 + 1, expected: 1e18 + 1 }));
sets.push(set({ x: 3e18 + 1, y: 7e18 + 1, expected: 5e18 + 1 }));
sets.push(set({ x: 99e18 + 1, y: 199e18 + 1, expected: 149e18 + 1 }));
sets.push(set({ x: 1e24 + 1, y: 1e25 + 1, expected: 5.5e24 + 1 }));
sets.push(set({ x: MAX_UD60x18, y: MAX_UD60x18, expected: MAX_UD60x18 }));
return sets;
}
function test_Avg_NeitherOperandZero_BothOperandsOdd() external parameterizedTest(bothOperandsOdd_Sets()) neitherOperandZero {
UD60x18 actual = avg(s.x, s.y);
assertEq(actual, s.expected);
}
function oneOperandEvenTheOtherOdd_Sets() internal returns (Set[] memory) {
delete sets;
sets.push(set({ x: 1, y: 2, expected: 1 }));
sets.push(set({ x: 1e18 + 1, y: 2e18, expected: 1.5e18 }));
sets.push(set({ x: 3e18 + 1, y: 8e18, expected: 5.5e18 }));
sets.push(set({ x: 99e18, y: 200e18, expected: 149.5e18 }));
sets.push(set({ x: 1e24 + 1, y: 1e25 + 1e18, expected: 5.5e24 + 0.5e18 }));
sets.push(
set({
x: MAX_UD60x18,
y: MAX_WHOLE_UD60x18,
expected: 115792089237316195423570985008687907853269984665640564039457_292003956564819967
})
);
return sets;
}
function test_Avg_NeitherOperandZero_OneOperandEvenTheOtherOdd()
external
parameterizedTest(oneOperandEvenTheOtherOdd_Sets())
neitherOperandZero
{
UD60x18 actual = avg(s.x, s.y);
assertEq(actual, s.expected);
}
}