SafeMath.test.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. const { BN, constants, expectRevert } = require('@openzeppelin/test-helpers');
  2. const { MAX_UINT256 } = constants;
  3. const { expect } = require('chai');
  4. const SafeMathMock = artifacts.require('SafeMathMock');
  5. function expectStruct (value, expected) {
  6. for (const key in expected) {
  7. if (BN.isBN(value[key])) {
  8. expect(value[key]).to.be.bignumber.equal(expected[key]);
  9. } else {
  10. expect(value[key]).to.be.equal(expected[key]);
  11. }
  12. }
  13. }
  14. contract('SafeMath', function (accounts) {
  15. beforeEach(async function () {
  16. this.safeMath = await SafeMathMock.new();
  17. });
  18. async function testCommutative (fn, lhs, rhs, expected, ...extra) {
  19. expect(await fn(lhs, rhs, ...extra)).to.be.bignumber.equal(expected);
  20. expect(await fn(rhs, lhs, ...extra)).to.be.bignumber.equal(expected);
  21. }
  22. async function testFailsCommutative (fn, lhs, rhs, reason, ...extra) {
  23. await expectRevert(fn(lhs, rhs, ...extra), reason);
  24. await expectRevert(fn(rhs, lhs, ...extra), reason);
  25. }
  26. async function testCommutativeIterable (fn, lhs, rhs, expected, ...extra) {
  27. expectStruct(await fn(lhs, rhs, ...extra), expected);
  28. expectStruct(await fn(rhs, lhs, ...extra), expected);
  29. }
  30. describe('with flag', function () {
  31. describe('add', function () {
  32. it('adds correctly', async function () {
  33. const a = new BN('5678');
  34. const b = new BN('1234');
  35. testCommutativeIterable(this.safeMath.tryAdd, a, b, { flag: true, value: a.add(b) });
  36. });
  37. it('reverts on addition overflow', async function () {
  38. const a = MAX_UINT256;
  39. const b = new BN('1');
  40. testCommutativeIterable(this.safeMath.tryAdd, a, b, { flag: false, value: '0' });
  41. });
  42. });
  43. describe('sub', function () {
  44. it('subtracts correctly', async function () {
  45. const a = new BN('5678');
  46. const b = new BN('1234');
  47. expectStruct(await this.safeMath.trySub(a, b), { flag: true, value: a.sub(b) });
  48. });
  49. it('reverts if subtraction result would be negative', async function () {
  50. const a = new BN('1234');
  51. const b = new BN('5678');
  52. expectStruct(await this.safeMath.trySub(a, b), { flag: false, value: '0' });
  53. });
  54. });
  55. describe('mul', function () {
  56. it('multiplies correctly', async function () {
  57. const a = new BN('1234');
  58. const b = new BN('5678');
  59. testCommutativeIterable(this.safeMath.tryMul, a, b, { flag: true, value: a.mul(b) });
  60. });
  61. it('multiplies by zero correctly', async function () {
  62. const a = new BN('0');
  63. const b = new BN('5678');
  64. testCommutativeIterable(this.safeMath.tryMul, a, b, { flag: true, value: a.mul(b) });
  65. });
  66. it('reverts on multiplication overflow', async function () {
  67. const a = MAX_UINT256;
  68. const b = new BN('2');
  69. testCommutativeIterable(this.safeMath.tryMul, a, b, { flag: false, value: '0' });
  70. });
  71. });
  72. describe('div', function () {
  73. it('divides correctly', async function () {
  74. const a = new BN('5678');
  75. const b = new BN('5678');
  76. expectStruct(await this.safeMath.tryDiv(a, b), { flag: true, value: a.div(b) });
  77. });
  78. it('divides zero correctly', async function () {
  79. const a = new BN('0');
  80. const b = new BN('5678');
  81. expectStruct(await this.safeMath.tryDiv(a, b), { flag: true, value: a.div(b) });
  82. });
  83. it('returns complete number result on non-even division', async function () {
  84. const a = new BN('7000');
  85. const b = new BN('5678');
  86. expectStruct(await this.safeMath.tryDiv(a, b), { flag: true, value: a.div(b) });
  87. });
  88. it('reverts on division by zero', async function () {
  89. const a = new BN('5678');
  90. const b = new BN('0');
  91. expectStruct(await this.safeMath.tryDiv(a, b), { flag: false, value: '0' });
  92. });
  93. });
  94. describe('mod', function () {
  95. describe('modulos correctly', async function () {
  96. it('when the dividend is smaller than the divisor', async function () {
  97. const a = new BN('284');
  98. const b = new BN('5678');
  99. expectStruct(await this.safeMath.tryMod(a, b), { flag: true, value: a.mod(b) });
  100. });
  101. it('when the dividend is equal to the divisor', async function () {
  102. const a = new BN('5678');
  103. const b = new BN('5678');
  104. expectStruct(await this.safeMath.tryMod(a, b), { flag: true, value: a.mod(b) });
  105. });
  106. it('when the dividend is larger than the divisor', async function () {
  107. const a = new BN('7000');
  108. const b = new BN('5678');
  109. expectStruct(await this.safeMath.tryMod(a, b), { flag: true, value: a.mod(b) });
  110. });
  111. it('when the dividend is a multiple of the divisor', async function () {
  112. const a = new BN('17034'); // 17034 == 5678 * 3
  113. const b = new BN('5678');
  114. expectStruct(await this.safeMath.tryMod(a, b), { flag: true, value: a.mod(b) });
  115. });
  116. });
  117. it('reverts with a 0 divisor', async function () {
  118. const a = new BN('5678');
  119. const b = new BN('0');
  120. expectStruct(await this.safeMath.tryMod(a, b), { flag: false, value: '0' });
  121. });
  122. });
  123. });
  124. describe('with default revert message', function () {
  125. describe('add', function () {
  126. it('adds correctly', async function () {
  127. const a = new BN('5678');
  128. const b = new BN('1234');
  129. await testCommutative(this.safeMath.doAdd, a, b, a.add(b));
  130. });
  131. it('reverts on addition overflow', async function () {
  132. const a = MAX_UINT256;
  133. const b = new BN('1');
  134. await testFailsCommutative(this.safeMath.doAdd, a, b, 'SafeMath: addition overflow');
  135. });
  136. });
  137. describe('sub', function () {
  138. it('subtracts correctly', async function () {
  139. const a = new BN('5678');
  140. const b = new BN('1234');
  141. expect(await this.safeMath.doSub(a, b)).to.be.bignumber.equal(a.sub(b));
  142. });
  143. it('reverts if subtraction result would be negative', async function () {
  144. const a = new BN('1234');
  145. const b = new BN('5678');
  146. await expectRevert(this.safeMath.doSub(a, b), 'SafeMath: subtraction overflow');
  147. });
  148. });
  149. describe('mul', function () {
  150. it('multiplies correctly', async function () {
  151. const a = new BN('1234');
  152. const b = new BN('5678');
  153. await testCommutative(this.safeMath.doMul, a, b, a.mul(b));
  154. });
  155. it('multiplies by zero correctly', async function () {
  156. const a = new BN('0');
  157. const b = new BN('5678');
  158. await testCommutative(this.safeMath.doMul, a, b, '0');
  159. });
  160. it('reverts on multiplication overflow', async function () {
  161. const a = MAX_UINT256;
  162. const b = new BN('2');
  163. await testFailsCommutative(this.safeMath.doMul, a, b, 'SafeMath: multiplication overflow');
  164. });
  165. });
  166. describe('div', function () {
  167. it('divides correctly', async function () {
  168. const a = new BN('5678');
  169. const b = new BN('5678');
  170. expect(await this.safeMath.doDiv(a, b)).to.be.bignumber.equal(a.div(b));
  171. });
  172. it('divides zero correctly', async function () {
  173. const a = new BN('0');
  174. const b = new BN('5678');
  175. expect(await this.safeMath.doDiv(a, b)).to.be.bignumber.equal('0');
  176. });
  177. it('returns complete number result on non-even division', async function () {
  178. const a = new BN('7000');
  179. const b = new BN('5678');
  180. expect(await this.safeMath.doDiv(a, b)).to.be.bignumber.equal('1');
  181. });
  182. it('reverts on division by zero', async function () {
  183. const a = new BN('5678');
  184. const b = new BN('0');
  185. await expectRevert(this.safeMath.doDiv(a, b), 'SafeMath: division by zero');
  186. });
  187. });
  188. describe('mod', function () {
  189. describe('modulos correctly', async function () {
  190. it('when the dividend is smaller than the divisor', async function () {
  191. const a = new BN('284');
  192. const b = new BN('5678');
  193. expect(await this.safeMath.doMod(a, b)).to.be.bignumber.equal(a.mod(b));
  194. });
  195. it('when the dividend is equal to the divisor', async function () {
  196. const a = new BN('5678');
  197. const b = new BN('5678');
  198. expect(await this.safeMath.doMod(a, b)).to.be.bignumber.equal(a.mod(b));
  199. });
  200. it('when the dividend is larger than the divisor', async function () {
  201. const a = new BN('7000');
  202. const b = new BN('5678');
  203. expect(await this.safeMath.doMod(a, b)).to.be.bignumber.equal(a.mod(b));
  204. });
  205. it('when the dividend is a multiple of the divisor', async function () {
  206. const a = new BN('17034'); // 17034 == 5678 * 3
  207. const b = new BN('5678');
  208. expect(await this.safeMath.doMod(a, b)).to.be.bignumber.equal(a.mod(b));
  209. });
  210. });
  211. it('reverts with a 0 divisor', async function () {
  212. const a = new BN('5678');
  213. const b = new BN('0');
  214. await expectRevert(this.safeMath.doMod(a, b), 'SafeMath: modulo by zero');
  215. });
  216. });
  217. });
  218. describe('with custom revert message', function () {
  219. describe('sub', function () {
  220. it('subtracts correctly', async function () {
  221. const a = new BN('5678');
  222. const b = new BN('1234');
  223. expect(await this.safeMath.subWithMessage(a, b, 'MyErrorMessage')).to.be.bignumber.equal(a.sub(b));
  224. });
  225. it('reverts if subtraction result would be negative', async function () {
  226. const a = new BN('1234');
  227. const b = new BN('5678');
  228. await expectRevert(this.safeMath.subWithMessage(a, b, 'MyErrorMessage'), 'MyErrorMessage');
  229. });
  230. });
  231. describe('div', function () {
  232. it('divides correctly', async function () {
  233. const a = new BN('5678');
  234. const b = new BN('5678');
  235. expect(await this.safeMath.divWithMessage(a, b, 'MyErrorMessage')).to.be.bignumber.equal(a.div(b));
  236. });
  237. it('divides zero correctly', async function () {
  238. const a = new BN('0');
  239. const b = new BN('5678');
  240. expect(await this.safeMath.divWithMessage(a, b, 'MyErrorMessage')).to.be.bignumber.equal('0');
  241. });
  242. it('returns complete number result on non-even division', async function () {
  243. const a = new BN('7000');
  244. const b = new BN('5678');
  245. expect(await this.safeMath.divWithMessage(a, b, 'MyErrorMessage')).to.be.bignumber.equal('1');
  246. });
  247. it('reverts on division by zero', async function () {
  248. const a = new BN('5678');
  249. const b = new BN('0');
  250. await expectRevert(this.safeMath.divWithMessage(a, b, 'MyErrorMessage'), 'MyErrorMessage');
  251. });
  252. });
  253. describe('mod', function () {
  254. describe('modulos correctly', async function () {
  255. it('when the dividend is smaller than the divisor', async function () {
  256. const a = new BN('284');
  257. const b = new BN('5678');
  258. expect(await this.safeMath.modWithMessage(a, b, 'MyErrorMessage')).to.be.bignumber.equal(a.mod(b));
  259. });
  260. it('when the dividend is equal to the divisor', async function () {
  261. const a = new BN('5678');
  262. const b = new BN('5678');
  263. expect(await this.safeMath.modWithMessage(a, b, 'MyErrorMessage')).to.be.bignumber.equal(a.mod(b));
  264. });
  265. it('when the dividend is larger than the divisor', async function () {
  266. const a = new BN('7000');
  267. const b = new BN('5678');
  268. expect(await this.safeMath.modWithMessage(a, b, 'MyErrorMessage')).to.be.bignumber.equal(a.mod(b));
  269. });
  270. it('when the dividend is a multiple of the divisor', async function () {
  271. const a = new BN('17034'); // 17034 == 5678 * 3
  272. const b = new BN('5678');
  273. expect(await this.safeMath.modWithMessage(a, b, 'MyErrorMessage')).to.be.bignumber.equal(a.mod(b));
  274. });
  275. });
  276. it('reverts with a 0 divisor', async function () {
  277. const a = new BN('5678');
  278. const b = new BN('0');
  279. await expectRevert(this.safeMath.modWithMessage(a, b, 'MyErrorMessage'), 'MyErrorMessage');
  280. });
  281. });
  282. });
  283. describe('memory leakage', function () {
  284. it('add', async function () {
  285. expect(await this.safeMath.addMemoryCheck()).to.be.bignumber.equal('0');
  286. });
  287. it('sub', async function () {
  288. expect(await this.safeMath.subMemoryCheck()).to.be.bignumber.equal('0');
  289. });
  290. it('mul', async function () {
  291. expect(await this.safeMath.mulMemoryCheck()).to.be.bignumber.equal('0');
  292. });
  293. it('div', async function () {
  294. expect(await this.safeMath.divMemoryCheck()).to.be.bignumber.equal('0');
  295. });
  296. it('mod', async function () {
  297. expect(await this.safeMath.modMemoryCheck()).to.be.bignumber.equal('0');
  298. });
  299. });
  300. });