SafeMath.test.js 13 KB

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