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
const { BN, constants, expectRevert } = require('@openzeppelin/test-helpers');
const { MAX_INT256, MIN_INT256 } = constants;
const { expect } = require('chai');
const SignedSafeMathMock = artifacts.require('SignedSafeMathMock');
contract('SignedSafeMath', function (accounts) {
beforeEach(async function () {
this.safeMath = await SignedSafeMathMock.new();
});
async function testCommutative (fn, lhs, rhs, expected) {
expect(await fn(lhs, rhs)).to.be.bignumber.equal(expected);
expect(await fn(rhs, lhs)).to.be.bignumber.equal(expected);
}
async function testFailsCommutative (fn, lhs, rhs) {
await expectRevert.unspecified(fn(lhs, rhs));
await expectRevert.unspecified(fn(rhs, lhs));
}
describe('add', function () {
it('adds correctly if it does not overflow and the result is positive', async function () {
const a = new BN('1234');
const b = new BN('5678');
await testCommutative(this.safeMath.add, a, b, a.add(b));
});
it('adds correctly if it does not overflow and the result is negative', async function () {
const a = MAX_INT256;
const b = MIN_INT256;
await testCommutative(this.safeMath.add, a, b, a.add(b));
});
it('reverts on positive addition overflow', async function () {
const a = MAX_INT256;
const b = new BN('1');
await testFailsCommutative(this.safeMath.add, a, b);
});
it('reverts on negative addition overflow', async function () {
const a = MIN_INT256;
const b = new BN('-1');
await testFailsCommutative(this.safeMath.add, a, b);
});
});
describe('sub', function () {
it('subtracts correctly if it does not overflow and the result is positive', async function () {
const a = new BN('5678');
const b = new BN('1234');
const result = await this.safeMath.sub(a, b);
expect(result).to.be.bignumber.equal(a.sub(b));
});
it('subtracts correctly if it does not overflow and the result is negative', async function () {
const a = new BN('1234');
const b = new BN('5678');
const result = await this.safeMath.sub(a, b);
expect(result).to.be.bignumber.equal(a.sub(b));
});
it('reverts on positive subtraction overflow', async function () {
const a = MAX_INT256;
const b = new BN('-1');
await expectRevert.unspecified(this.safeMath.sub(a, b));
});
it('reverts on negative subtraction overflow', async function () {
const a = MIN_INT256;
const b = new BN('1');
await expectRevert.unspecified(this.safeMath.sub(a, b));
});
});
describe('mul', function () {
it('multiplies correctly', async function () {
const a = new BN('5678');
const b = new BN('-1234');
await testCommutative(this.safeMath.mul, a, b, a.mul(b));
});
it('multiplies by zero correctly', async function () {
const a = new BN('0');
const b = new BN('5678');
await testCommutative(this.safeMath.mul, a, b, '0');
});
it('reverts on multiplication overflow, positive operands', async function () {
const a = MAX_INT256;
const b = new BN('2');
await testFailsCommutative(this.safeMath.mul, a, b);
});
it('reverts when minimum integer is multiplied by -1', async function () {
const a = MIN_INT256;
const b = new BN('-1');
await testFailsCommutative(this.safeMath.mul, a, b);
});
});
describe('div', function () {
it('divides correctly', async function () {
const a = new BN('-5678');
const b = new BN('5678');
const result = await this.safeMath.div(a, b);
expect(result).to.be.bignumber.equal(a.div(b));
});
it('divides zero correctly', async function () {
const a = new BN('0');
const b = new BN('5678');
expect(await this.safeMath.div(a, b)).to.be.bignumber.equal('0');
});
it('returns complete number result on non-even division', async function () {
const a = new BN('7000');
const b = new BN('5678');
expect(await this.safeMath.div(a, b)).to.be.bignumber.equal('1');
});
it('reverts on division by zero', async function () {
const a = new BN('-5678');
const b = new BN('0');
await expectRevert.unspecified(this.safeMath.div(a, b));
});
it('reverts on overflow, negative second', async function () {
const a = new BN(MIN_INT256);
const b = new BN('-1');
await expectRevert.unspecified(this.safeMath.div(a, b));
});
});
});