/
avg.ts
131 lines (117 loc) · 4.75 KB
/
avg.ts
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
import { BigNumber } from "@ethersproject/bignumber";
import { expect } from "chai";
import fp from "evm-fp";
import forEach from "mocha-each";
import { MAX_SD59x18, MAX_WHOLE_SD59x18, MIN_SD59x18, MIN_WHOLE_SD59x18 } from "../../../helpers/constants";
import { bn } from "../../../helpers/numbers";
import { avg } from "../../shared/mirrors";
export default function shouldBehaveLikeAvg(): void {
context("when both operands are zero", function () {
it("returns 0", async function () {
const x: BigNumber = bn("0");
const y: BigNumber = bn("0");
const expected: BigNumber = bn("0");
expect(expected).to.equal(await this.contracts.prbMathSd59x18.doAvg(x, y));
expect(expected).to.equal(await this.contracts.prbMathSd59x18Typed.doAvg(x, y));
});
});
context("when one operand is zero and the other is not zero", function () {
const testSets = [
[fp("-4"), fp("0")],
[fp("0"), fp("-4")],
[fp("0"), fp("4")],
[fp("4"), fp("0")],
];
forEach(testSets).it("takes %e and %e and returns the correct value", async function (x: BigNumber, y: BigNumber) {
const expected: BigNumber = avg(x, y);
expect(expected).to.equal(await this.contracts.prbMathSd59x18.doAvg(x, y));
expect(expected).to.equal(await this.contracts.prbMathSd59x18Typed.doAvg(x, y));
});
});
context("when one operand is negative and the other is positive", function () {
const testSets = [
[fp(MIN_WHOLE_SD59x18), fp(MAX_WHOLE_SD59x18)],
[fp("-4"), fp("4")],
[fp("-2"), fp("8")],
[fp("4"), fp("-4")],
[fp("8"), fp("-2")],
[fp(MIN_SD59x18), fp(MAX_SD59x18)],
];
forEach(testSets).it("takes %e and %e and returns the correct value", async function (x: BigNumber, y: BigNumber) {
const expected: BigNumber = avg(x, y);
expect(expected).to.equal(await this.contracts.prbMathSd59x18.doAvg(x, y));
expect(expected).to.equal(await this.contracts.prbMathSd59x18Typed.doAvg(x, y));
});
});
context("when both operands are negative", function () {
const testSets = [
[fp(MIN_WHOLE_SD59x18), fp(MIN_SD59x18)],
[fp("1e-18"), fp("3e-18")],
[fp("100"), fp("200")],
[fp("4"), fp("8")],
[fp("1"), fp("1")],
[fp("1"), fp("2")],
];
forEach(testSets).it("takes %e and %e and returns the correct value", async function (x: BigNumber, y: BigNumber) {
const expected: BigNumber = avg(x, y);
expect(expected).to.equal(await this.contracts.prbMathSd59x18.doAvg(x, y));
expect(expected).to.equal(await this.contracts.prbMathSd59x18Typed.doAvg(x, y));
});
});
context("when both operands are positive", function () {
context("when both operands are odd", function () {
const testSets = [
[fp("1e-18"), fp("3e-18")],
[fp("1"), fp("1")],
[fp("3"), fp("7")],
[fp("99"), fp("199")],
[fp("1000000000000000001"), fp("10000000000000000001")],
[fp(MAX_SD59x18), fp(MAX_SD59x18)],
];
forEach(testSets).it(
"takes %e and %e and returns the correct value",
async function (x: BigNumber, y: BigNumber) {
const expected: BigNumber = avg(x, y);
expect(expected).to.equal(await this.contracts.prbMathSd59x18.doAvg(x, y));
expect(expected).to.equal(await this.contracts.prbMathSd59x18Typed.doAvg(x, y));
},
);
});
context("when both operands are even", function () {
const testSets = [
[fp("2e-18"), fp("4e-18")],
[fp("2"), fp("2")],
[fp("4"), fp("8")],
[fp("100"), fp("200")],
[fp("1e18"), fp("1e19")],
[fp(MIN_WHOLE_SD59x18), fp(MIN_WHOLE_SD59x18)],
];
forEach(testSets).it(
"takes %e and %e and returns the correct value",
async function (x: BigNumber, y: BigNumber) {
const expected: BigNumber = avg(x, y);
expect(expected).to.equal(await this.contracts.prbMathSd59x18.doAvg(x, y));
expect(expected).to.equal(await this.contracts.prbMathSd59x18Typed.doAvg(x, y));
},
);
});
context("when one operand is even and the other is odd", function () {
const testSets = [
[fp("1e-18"), fp("2e-18")],
[fp("1"), fp("2")],
[fp("3"), fp("8")],
[fp("99"), fp("200")],
[fp("1e18"), fp("10000000000000000001")],
[fp(MAX_WHOLE_SD59x18), fp(MAX_SD59x18)],
];
forEach(testSets).it(
"takes %e and %e and returns the correct value",
async function (x: BigNumber, y: BigNumber) {
const expected: BigNumber = avg(x, y);
expect(expected).to.equal(await this.contracts.prbMathSd59x18.doAvg(x, y));
expect(expected).to.equal(await this.contracts.prbMathSd59x18Typed.doAvg(x, y));
},
);
});
});
}