forked from OpenZeppelin/openzeppelin-contracts
-
Notifications
You must be signed in to change notification settings - Fork 1
/
RoundedDivMath.test.js
127 lines (106 loc) · 4.97 KB
/
RoundedDivMath.test.js
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
const RoundedDivMathMock = artifacts.require('RoundedDivMathMock');
contract('RoundedDivMath', function () {
beforeEach(async function () {
this.math = await RoundedDivMathMock.new();
});
describe('roundedDiv', function () {
it('should return correct results', async function () {
assert.equal(await this.math.roundedDiv(49, 100), 0);
assert.equal(await this.math.roundedDiv(50, 100), 1);
assert.equal(await this.math.roundedDiv(51, 100), 1);
assert.equal(await this.math.roundedDiv(149, 100), 1);
assert.equal(await this.math.roundedDiv(150, 100), 2);
assert.equal(await this.math.roundedDiv(151, 100), 2);
assert.equal(await this.math.roundedDiv(249, 100), 2);
assert.equal(await this.math.roundedDiv(250, 100), 2);
assert.equal(await this.math.roundedDiv(251, 100), 3);
// divide by odd number
assert.equal(await this.math.bankersRoundedDiv(49, 101), 0);
});
it('should throw if divided by null', async function () {
await this.math.roundedDiv(49, 0).should.be.rejectedWith('revert');
});
});
describe('bankersRoundedDiv', function () {
it('should return correct results', async function () {
assert.equal(await this.math.bankersRoundedDiv(49, 100), 0);
assert.equal(await this.math.bankersRoundedDiv(50, 100), 0);
assert.equal(await this.math.bankersRoundedDiv(51, 100), 1);
assert.equal(await this.math.bankersRoundedDiv(149, 100), 1);
assert.equal(await this.math.bankersRoundedDiv(150, 100), 2);
assert.equal(await this.math.bankersRoundedDiv(151, 100), 2);
assert.equal(await this.math.bankersRoundedDiv(249, 100), 2);
assert.equal(await this.math.bankersRoundedDiv(250, 100), 2);
assert.equal(await this.math.bankersRoundedDiv(251, 100), 3);
// divide by odd number
assert.equal(await this.math.bankersRoundedDiv(49, 101), 0);
});
it('should throw if divided by null', async function () {
await this.math.roundedDiv(49, 0).should.be.rejectedWith('revert');
});
});
describe('unitsToCentsRounded (roundedDiv)', function () {
it('should return correct results', async function () {
// 0.49 cents -> 0 cents
// 0.5 cents -> 0 cents
// 0.51 cents -> 1 cents
// 1.49 cents -> 1 cents
// 1.5 cents -> 1 cents
// 1.51 cents -> 2 cents
// 2.49 cents -> 2 cents
// 2.5 cents -> 2 cents
// 2.51 cents -> 3 cents
assert.equal(await this.math.unitsToCentsRounded(49), 0);
assert.equal(await this.math.unitsToCentsRounded(50), 1);
assert.equal(await this.math.unitsToCentsRounded(51), 1);
assert.equal(await this.math.unitsToCentsRounded(149), 1);
assert.equal(await this.math.unitsToCentsRounded(150), 2);
assert.equal(await this.math.unitsToCentsRounded(151), 2);
assert.equal(await this.math.unitsToCentsRounded(249), 2);
assert.equal(await this.math.unitsToCentsRounded(250), 3);
assert.equal(await this.math.unitsToCentsRounded(251), 3);
// 24.9 cents -> 25 cents
// 25.0 cents -> 25 cents
// 25.1 cents -> 25 cents
// 25.15 cents -> 25 cents
assert.equal(await this.math.unitsToCentsRounded(2490), 25);
assert.equal(await this.math.unitsToCentsRounded(2500), 25);
assert.equal(await this.math.unitsToCentsRounded(2510), 25);
assert.equal(await this.math.unitsToCentsRounded(2515), 25);
// 5000 cents -> 5000 cents
assert.equal(await this.math.unitsToCentsRounded(500000), 5000);
});
});
describe('unitsToCentsBankers (bankersRoundedDiv)', function () {
it('should return correct results', async function () {
// 0.49 cents -> 0 cents
// 0.5 cents -> 0 cents
// 0.51 cents -> 1 cents
// 1.49 cents -> 1 cents
// 1.5 cents -> 2 cents
// 1.51 cents -> 2 cents
// 2.49 cents -> 2 cents
// 2.5 cents -> 2 cents
// 2.51 cents -> 3 cents
assert.equal(await this.math.unitsToCentsBankers(49), 0);
assert.equal(await this.math.unitsToCentsBankers(50), 0);
assert.equal(await this.math.unitsToCentsBankers(51), 1);
assert.equal(await this.math.unitsToCentsBankers(149), 1);
assert.equal(await this.math.unitsToCentsBankers(150), 2);
assert.equal(await this.math.unitsToCentsBankers(151), 2);
assert.equal(await this.math.unitsToCentsBankers(249), 2);
assert.equal(await this.math.unitsToCentsBankers(250), 2);
assert.equal(await this.math.unitsToCentsBankers(251), 3);
// 24.9 cents -> 25 cents
// 25.0 cents -> 25 cents
// 25.1 cents -> 25 cents
// 25.15 cents -> 25 cents
assert.equal(await this.math.unitsToCentsBankers(2490), 25);
assert.equal(await this.math.unitsToCentsBankers(2500), 25);
assert.equal(await this.math.unitsToCentsBankers(2510), 25);
assert.equal(await this.math.unitsToCentsBankers(2515), 25);
// 5000 cents -> 5000 cents
assert.equal(await this.math.unitsToCentsBankers(500000), 5000);
});
});
});