-
Notifications
You must be signed in to change notification settings - Fork 155
Expand file tree
/
Copy pathSafeMathInt.ts
More file actions
256 lines (218 loc) · 8.6 KB
/
SafeMathInt.ts
File metadata and controls
256 lines (218 loc) · 8.6 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import { ethers } from 'hardhat'
import { Contract } from 'ethers'
import { expect } from 'chai'
describe('SafeMathInt', () => {
const MIN_INT256 = ethers.BigNumber.from(-2).pow(255)
const MAX_INT256 = ethers.BigNumber.from(2).pow(255).sub(1)
let safeMathInt: Contract
beforeEach(async function () {
// deploy contract
const factory = await ethers.getContractFactory('SafeMathIntMock')
safeMathInt = await factory.deploy()
await safeMathInt.deployed()
})
describe('add', function () {
it('adds correctly', async function () {
const a = ethers.BigNumber.from(5678)
const b = ethers.BigNumber.from(1234)
await expect(safeMathInt.add(a, b))
.to.emit(safeMathInt, 'ReturnValueInt256')
.withArgs(a.add(b))
})
it('should fail on addition overflow', async function () {
const a = MAX_INT256
const b = ethers.BigNumber.from(1)
await expect(safeMathInt.add(a, b)).to.be.reverted
await expect(safeMathInt.add(b, a)).to.be.reverted
})
it('should fail on addition overflow, swapped args', async function () {
const a = ethers.BigNumber.from(1)
const b = MAX_INT256
await expect(safeMathInt.add(a, b)).to.be.reverted
await expect(safeMathInt.add(b, a)).to.be.reverted
})
it('should fail on addition negative overflow', async function () {
const a = MIN_INT256
const b = ethers.BigNumber.from(-1)
await expect(safeMathInt.add(a, b)).to.be.reverted
await expect(safeMathInt.add(b, a)).to.be.reverted
})
})
describe('sub', function () {
it('subtracts correctly', async function () {
const a = ethers.BigNumber.from(5678)
const b = ethers.BigNumber.from(1234)
await expect(safeMathInt.sub(a, b))
.to.emit(safeMathInt, 'ReturnValueInt256')
.withArgs(a.sub(b))
})
it('should fail on subtraction overflow', async function () {
const a = MAX_INT256
const b = ethers.BigNumber.from(-1)
await expect(safeMathInt.sub(a, b)).to.be.reverted
})
it('should fail on subtraction negative overflow', async function () {
const a = MIN_INT256
const b = ethers.BigNumber.from(1)
await expect(safeMathInt.sub(a, b)).to.be.reverted
})
})
describe('mul', function () {
it('multiplies correctly', async function () {
const a = ethers.BigNumber.from(1234)
const b = ethers.BigNumber.from(5678)
await expect(safeMathInt.mul(a, b))
.to.emit(safeMathInt, 'ReturnValueInt256')
.withArgs(a.mul(b))
})
it('handles a zero product correctly', async function () {
const a = ethers.BigNumber.from(0)
const b = ethers.BigNumber.from(5678)
await expect(safeMathInt.mul(a, b))
.to.emit(safeMathInt, 'ReturnValueInt256')
.withArgs(a.mul(b))
})
it('should fail on multiplication overflow', async function () {
const a = MAX_INT256
const b = ethers.BigNumber.from(2)
await expect(safeMathInt.mul(a, b)).to.be.reverted
await expect(safeMathInt.mul(b, a)).to.be.reverted
})
it('should fail on multiplication negative overflow', async function () {
const a = MIN_INT256
const b = ethers.BigNumber.from(2)
await expect(safeMathInt.mul(a, b)).to.be.reverted
await expect(safeMathInt.mul(b, a)).to.be.reverted
})
it('should fail on multiplication between -1 and MIN_INT256', async function () {
const a = MIN_INT256
const b = ethers.BigNumber.from(-1)
await expect(safeMathInt.mul(a, b)).to.be.reverted
await expect(safeMathInt.mul(b, a)).to.be.reverted
})
})
describe('div', function () {
it('divides correctly', async function () {
const a = ethers.BigNumber.from(5678)
const b = ethers.BigNumber.from(5678)
await expect(safeMathInt.div(a, b))
.to.emit(safeMathInt, 'ReturnValueInt256')
.withArgs(a.div(b))
})
it('should fail on zero division', async function () {
const a = ethers.BigNumber.from(5678)
const b = ethers.BigNumber.from(0)
await expect(safeMathInt.div(a, b)).to.be.reverted
})
it('should fail when MIN_INT256 is divided by -1', async function () {
const a = ethers.BigNumber.from(MIN_INT256)
const b = ethers.BigNumber.from(-1)
await expect(safeMathInt.div(a, b)).to.be.reverted
})
})
describe('abs', function () {
it('works for 0', async function () {
await expect(safeMathInt.abs(0))
.to.emit(safeMathInt, 'ReturnValueInt256')
.withArgs(0)
})
it('works on positive numbers', async function () {
await expect(safeMathInt.abs(100))
.to.emit(safeMathInt, 'ReturnValueInt256')
.withArgs(100)
})
it('works on negative numbers', async function () {
await expect(safeMathInt.abs(-100))
.to.emit(safeMathInt, 'ReturnValueInt256')
.withArgs(100)
})
it('fails on overflow condition', async function () {
await expect(safeMathInt.abs(MIN_INT256)).to.be.reverted
})
})
describe('twoPower', function () {
const decimals18 = ethers.BigNumber.from('1000000000000000000')
const decimals10 = ethers.BigNumber.from('10000000000')
it('2^0', async function () {
const e = ethers.BigNumber.from(0)
const one = ethers.BigNumber.from(1).mul(decimals18)
await expect(safeMathInt.twoPower(e, one))
.to.emit(safeMathInt, 'ReturnValueInt256')
.withArgs(one)
})
it('2^1', async function () {
const e = ethers.BigNumber.from(1).mul(decimals18)
const one = ethers.BigNumber.from(1).mul(decimals18)
const result = ethers.BigNumber.from(2).mul(decimals18)
;(await expect(safeMathInt.twoPower(e, one))).to
.emit(safeMathInt, 'ReturnValueInt256')
.withArgs(result)
})
it('2^30', async function () {
const e = ethers.BigNumber.from(30).mul(decimals18)
const one = ethers.BigNumber.from(1).mul(decimals18)
const result = ethers.BigNumber.from(2 ** 30).mul(decimals18)
;(await expect(safeMathInt.twoPower(e, one))).to
.emit(safeMathInt, 'ReturnValueInt256')
.withArgs(result)
})
it('2^2.5', async function () {
const e = ethers.BigNumber.from('25000000000')
const one = ethers.BigNumber.from(1).mul(decimals10)
const result = ethers.BigNumber.from('56568542494')
;(await expect(safeMathInt.twoPower(e, one))).to
.emit(safeMathInt, 'ReturnValueInt256')
.withArgs(result)
})
it('2^2.25', async function () {
const e = ethers.BigNumber.from('22500000000')
const one = ethers.BigNumber.from(1).mul(decimals10)
const result = ethers.BigNumber.from('47568284600')
;(await expect(safeMathInt.twoPower(e, one))).to
.emit(safeMathInt, 'ReturnValueInt256')
.withArgs(result)
})
it('2^-2.25', async function () {
const e = ethers.BigNumber.from('-22500000000')
const one = ethers.BigNumber.from(1).mul(decimals10)
const result = ethers.BigNumber.from('2102241038')
;(await expect(safeMathInt.twoPower(e, one))).to
.emit(safeMathInt, 'ReturnValueInt256')
.withArgs(result)
})
it('2^-0.6', async function () {
const e = ethers.BigNumber.from('-6000000000')
const one = ethers.BigNumber.from(1).mul(decimals10)
const result = ethers.BigNumber.from('6626183216')
;(await expect(safeMathInt.twoPower(e, one))).to
.emit(safeMathInt, 'ReturnValueInt256')
.withArgs(result)
})
it('2^2.96875', async function () {
const e = ethers.BigNumber.from('29687500000')
const one = ethers.BigNumber.from(1).mul(decimals10)
const result = ethers.BigNumber.from('78285764964')
;(await expect(safeMathInt.twoPower(e, one))).to
.emit(safeMathInt, 'ReturnValueInt256')
.withArgs(result)
})
it('2^2.99', async function () {
const e = ethers.BigNumber.from('29900000000')
const one = ethers.BigNumber.from(1).mul(decimals10)
const result = ethers.BigNumber.from('78285764964')
;(await expect(safeMathInt.twoPower(e, one))).to
.emit(safeMathInt, 'ReturnValueInt256')
.withArgs(result)
})
it('should fail on too small exponents', async function () {
const e = ethers.BigNumber.from('-1011000000000')
const one = ethers.BigNumber.from(1).mul(decimals10)
await expect(safeMathInt.twoPower(e, one)).to.be.reverted
})
it('should fail on too large exponents', async function () {
const e = ethers.BigNumber.from('1011000000000')
const one = ethers.BigNumber.from(1).mul(decimals10)
await expect(safeMathInt.twoPower(e, one)).to.be.reverted
})
})
})