SafeMath.test.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. const shouldFail = require('../helpers/shouldFail');
  2. const { MAX_UINT256, MIN_INT256, MAX_INT256 } = require('../helpers/constants');
  3. const SafeMathMock = artifacts.require('SafeMathMock');
  4. const { BigNumber } = require('../helpers/setup');
  5. contract('SafeMath', function () {
  6. beforeEach(async function () {
  7. this.safeMath = await SafeMathMock.new();
  8. });
  9. describe('unsigned', function () {
  10. describe('add', function () {
  11. it('adds correctly', async function () {
  12. const a = new BigNumber(5678);
  13. const b = new BigNumber(1234);
  14. (await this.safeMath.addUints(a, b)).should.be.bignumber.equal(a.plus(b));
  15. });
  16. it('reverts on addition overflow', async function () {
  17. const a = MAX_UINT256;
  18. const b = new BigNumber(1);
  19. await shouldFail.reverting(this.safeMath.addUints(a, b));
  20. });
  21. });
  22. describe('sub', function () {
  23. it('subtracts correctly', async function () {
  24. const a = new BigNumber(5678);
  25. const b = new BigNumber(1234);
  26. (await this.safeMath.subUints(a, b)).should.be.bignumber.equal(a.minus(b));
  27. });
  28. it('reverts if subtraction result would be negative', async function () {
  29. const a = new BigNumber(1234);
  30. const b = new BigNumber(5678);
  31. await shouldFail.reverting(this.safeMath.subUints(a, b));
  32. });
  33. });
  34. describe('mul', function () {
  35. it('multiplies correctly', async function () {
  36. const a = new BigNumber(1234);
  37. const b = new BigNumber(5678);
  38. (await this.safeMath.mulUints(a, b)).should.be.bignumber.equal(a.times(b));
  39. });
  40. it('handles a zero product correctly (first number as zero)', async function () {
  41. const a = new BigNumber(0);
  42. const b = new BigNumber(5678);
  43. (await this.safeMath.mulUints(a, b)).should.be.bignumber.equal(a.times(b));
  44. });
  45. it('handles a zero product correctly (second number as zero)', async function () {
  46. const a = new BigNumber(5678);
  47. const b = new BigNumber(0);
  48. (await this.safeMath.mulUints(a, b)).should.be.bignumber.equal(a.times(b));
  49. });
  50. it('reverts on multiplication overflow', async function () {
  51. const a = MAX_UINT256;
  52. const b = new BigNumber(2);
  53. await shouldFail.reverting(this.safeMath.mulUints(a, b));
  54. });
  55. });
  56. describe('div', function () {
  57. it('divides correctly', async function () {
  58. const a = new BigNumber(5678);
  59. const b = new BigNumber(5678);
  60. (await this.safeMath.divUints(a, b)).should.be.bignumber.equal(a.div(b));
  61. });
  62. it('divides zero correctly', async function () {
  63. const a = new BigNumber(0);
  64. const b = new BigNumber(5678);
  65. (await this.safeMath.divUints(a, b)).should.be.bignumber.equal(0);
  66. });
  67. it('returns complete number result on non-even division', async function () {
  68. const a = new BigNumber(7000);
  69. const b = new BigNumber(5678);
  70. (await this.safeMath.divUints(a, b)).should.be.bignumber.equal(1);
  71. });
  72. it('reverts on zero division', async function () {
  73. const a = new BigNumber(5678);
  74. const b = new BigNumber(0);
  75. await shouldFail.reverting(this.safeMath.divUints(a, b));
  76. });
  77. });
  78. describe('mod', function () {
  79. describe('modulos correctly', async function () {
  80. it('when the dividend is smaller than the divisor', async function () {
  81. const a = new BigNumber(284);
  82. const b = new BigNumber(5678);
  83. (await this.safeMath.modUints(a, b)).should.be.bignumber.equal(a.mod(b));
  84. });
  85. it('when the dividend is equal to the divisor', async function () {
  86. const a = new BigNumber(5678);
  87. const b = new BigNumber(5678);
  88. (await this.safeMath.modUints(a, b)).should.be.bignumber.equal(a.mod(b));
  89. });
  90. it('when the dividend is larger than the divisor', async function () {
  91. const a = new BigNumber(7000);
  92. const b = new BigNumber(5678);
  93. (await this.safeMath.modUints(a, b)).should.be.bignumber.equal(a.mod(b));
  94. });
  95. it('when the dividend is a multiple of the divisor', async function () {
  96. const a = new BigNumber(17034); // 17034 == 5678 * 3
  97. const b = new BigNumber(5678);
  98. (await this.safeMath.modUints(a, b)).should.be.bignumber.equal(a.mod(b));
  99. });
  100. });
  101. it('reverts with a 0 divisor', async function () {
  102. const a = new BigNumber(5678);
  103. const b = new BigNumber(0);
  104. await shouldFail.reverting(this.safeMath.modUints(a, b));
  105. });
  106. });
  107. });
  108. describe('signed', function () {
  109. describe('add', function () {
  110. it('adds correctly if it does not overflow and the result is positve', async function () {
  111. const a = new BigNumber(1234);
  112. const b = new BigNumber(5678);
  113. (await this.safeMath.addUints(a, b)).should.be.bignumber.equal(a.plus(b));
  114. });
  115. it('adds correctly if it does not overflow and the result is negative', async function () {
  116. const a = MAX_INT256;
  117. const b = MIN_INT256;
  118. const result = await this.safeMath.addInts(a, b);
  119. result.should.be.bignumber.equal(a.plus(b));
  120. });
  121. it('reverts on positive addition overflow', async function () {
  122. const a = MAX_INT256;
  123. const b = new BigNumber(1);
  124. await shouldFail.reverting(this.safeMath.addInts(a, b));
  125. });
  126. it('reverts on negative addition overflow', async function () {
  127. const a = MIN_INT256;
  128. const b = new BigNumber(-1);
  129. await shouldFail.reverting(this.safeMath.addInts(a, b));
  130. });
  131. });
  132. describe('sub', function () {
  133. it('subtracts correctly if it does not overflow and the result is positive', async function () {
  134. const a = new BigNumber(5678);
  135. const b = new BigNumber(1234);
  136. const result = await this.safeMath.subInts(a, b);
  137. result.should.be.bignumber.equal(a.minus(b));
  138. });
  139. it('subtracts correctly if it does not overflow and the result is negative', async function () {
  140. const a = new BigNumber(1234);
  141. const b = new BigNumber(5678);
  142. const result = await this.safeMath.subInts(a, b);
  143. result.should.be.bignumber.equal(a.minus(b));
  144. });
  145. it('reverts on positive subtraction overflow', async function () {
  146. const a = MAX_INT256;
  147. const b = new BigNumber(-1);
  148. await shouldFail.reverting(this.safeMath.subInts(a, b));
  149. });
  150. it('reverts on negative subtraction overflow', async function () {
  151. const a = MIN_INT256;
  152. const b = new BigNumber(1);
  153. await shouldFail.reverting(this.safeMath.subInts(a, b));
  154. });
  155. });
  156. describe('mul', function () {
  157. it('multiplies correctly', async function () {
  158. const a = new BigNumber(5678);
  159. const b = new BigNumber(-1234);
  160. const result = await this.safeMath.mulInts(a, b);
  161. result.should.be.bignumber.equal(a.times(b));
  162. });
  163. it('handles a zero product correctly', async function () {
  164. const a = new BigNumber(0);
  165. const b = new BigNumber(5678);
  166. const result = await this.safeMath.mulInts(a, b);
  167. result.should.be.bignumber.equal(a.times(b));
  168. });
  169. it('reverts on multiplication overflow, positive operands', async function () {
  170. const a = MAX_INT256;
  171. const b = new BigNumber(2);
  172. await shouldFail.reverting(this.safeMath.mulInts(a, b));
  173. });
  174. it('reverts when minimum integer is multiplied by -1', async function () {
  175. const a = MIN_INT256;
  176. const b = new BigNumber(-1);
  177. await shouldFail.reverting(this.safeMath.mulInts(a, b));
  178. });
  179. it('reverts when -1 is multiplied by minimum integer', async function () {
  180. const a = new BigNumber(-1);
  181. const b = MIN_INT256;
  182. await shouldFail.reverting(this.safeMath.mulInts(a, b));
  183. });
  184. });
  185. describe('div', function () {
  186. it('divides correctly', async function () {
  187. const a = new BigNumber(-5678);
  188. const b = new BigNumber(5678);
  189. const result = await this.safeMath.divInts(a, b);
  190. result.should.be.bignumber.equal(a.div(b));
  191. });
  192. it('reverts on zero division', async function () {
  193. const a = new BigNumber(-5678);
  194. const b = new BigNumber(0);
  195. await shouldFail.reverting(this.safeMath.divInts(a, b));
  196. });
  197. it('reverts on overflow, negative second', async function () {
  198. const a = new BigNumber(MIN_INT256);
  199. const b = new BigNumber(-1);
  200. await shouldFail.reverting(this.safeMath.divInts(a, b));
  201. });
  202. });
  203. });
  204. });