SafeCast.test.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. const { BN, expectRevert } = require('@openzeppelin/test-helpers');
  2. const { expect } = require('chai');
  3. const SafeCastMock = artifacts.require('SafeCastMock');
  4. contract('SafeCast', async (accounts) => {
  5. beforeEach(async function () {
  6. this.safeCast = await SafeCastMock.new();
  7. });
  8. function testToUint (bits) {
  9. describe(`toUint${bits}`, () => {
  10. const maxValue = new BN('2').pow(new BN(bits)).subn(1);
  11. it('downcasts 0', async function () {
  12. expect(await this.safeCast[`toUint${bits}`](0)).to.be.bignumber.equal('0');
  13. });
  14. it('downcasts 1', async function () {
  15. expect(await this.safeCast[`toUint${bits}`](1)).to.be.bignumber.equal('1');
  16. });
  17. it(`downcasts 2^${bits} - 1 (${maxValue})`, async function () {
  18. expect(await this.safeCast[`toUint${bits}`](maxValue)).to.be.bignumber.equal(maxValue);
  19. });
  20. it(`reverts when downcasting 2^${bits} (${maxValue.addn(1)})`, async function () {
  21. await expectRevert(
  22. this.safeCast[`toUint${bits}`](maxValue.addn(1)),
  23. `SafeCast: value doesn't fit in ${bits} bits`,
  24. );
  25. });
  26. it(`reverts when downcasting 2^${bits} + 1 (${maxValue.addn(2)})`, async function () {
  27. await expectRevert(
  28. this.safeCast[`toUint${bits}`](maxValue.addn(2)),
  29. `SafeCast: value doesn't fit in ${bits} bits`,
  30. );
  31. });
  32. });
  33. }
  34. [8, 16, 32, 64, 128].forEach(bits => testToUint(bits));
  35. describe('toUint256', () => {
  36. const maxInt256 = new BN('2').pow(new BN(255)).subn(1);
  37. const minInt256 = new BN('2').pow(new BN(255)).neg();
  38. const maxUint256 = new BN('2').pow(new BN(256)).subn(1);
  39. it('casts 0', async function () {
  40. expect(await this.safeCast.toUint256(0)).to.be.bignumber.equal('0');
  41. });
  42. it('casts 1', async function () {
  43. expect(await this.safeCast.toUint256(1)).to.be.bignumber.equal('1');
  44. });
  45. it(`casts INT256_MAX (${maxInt256})`, async function () {
  46. expect(await this.safeCast.toUint256(maxInt256)).to.be.bignumber.equal(maxInt256);
  47. });
  48. it('reverts when casting -1', async function () {
  49. await expectRevert(
  50. this.safeCast.toUint256(-1),
  51. 'SafeCast: value must be positive',
  52. );
  53. });
  54. it(`reverts when casting INT256_MIN (${minInt256})`, async function () {
  55. await expectRevert(
  56. this.safeCast.toUint256(minInt256),
  57. 'SafeCast: value must be positive',
  58. );
  59. });
  60. it(`reverts when casting UINT256_MAX (${maxUint256})`, async function () {
  61. await expectRevert(
  62. this.safeCast.toUint256(maxUint256),
  63. 'SafeCast: value must be positive',
  64. );
  65. });
  66. });
  67. function testToInt (bits) {
  68. describe(`toInt${bits}`, () => {
  69. const minValue = new BN('-2').pow(new BN(bits - 1));
  70. const maxValue = new BN('2').pow(new BN(bits - 1)).subn(1);
  71. it('downcasts 0', async function () {
  72. expect(await this.safeCast[`toInt${bits}`](0)).to.be.bignumber.equal('0');
  73. });
  74. it('downcasts 1', async function () {
  75. expect(await this.safeCast[`toInt${bits}`](1)).to.be.bignumber.equal('1');
  76. });
  77. it('downcasts -1', async function () {
  78. expect(await this.safeCast[`toInt${bits}`](-1)).to.be.bignumber.equal('-1');
  79. });
  80. it(`downcasts -2^${bits - 1} (${minValue})`, async function () {
  81. expect(await this.safeCast[`toInt${bits}`](minValue)).to.be.bignumber.equal(minValue);
  82. });
  83. it(`downcasts 2^${bits - 1} - 1 (${maxValue})`, async function () {
  84. expect(await this.safeCast[`toInt${bits}`](maxValue)).to.be.bignumber.equal(maxValue);
  85. });
  86. it(`reverts when downcasting -2^${bits - 1} - 1 (${minValue.subn(1)})`, async function () {
  87. await expectRevert(
  88. this.safeCast[`toInt${bits}`](minValue.subn(1)),
  89. `SafeCast: value doesn't fit in ${bits} bits`,
  90. );
  91. });
  92. it(`reverts when downcasting -2^${bits - 1} - 2 (${minValue.subn(2)})`, async function () {
  93. await expectRevert(
  94. this.safeCast[`toInt${bits}`](minValue.subn(2)),
  95. `SafeCast: value doesn't fit in ${bits} bits`,
  96. );
  97. });
  98. it(`reverts when downcasting 2^${bits - 1} (${maxValue.addn(1)})`, async function () {
  99. await expectRevert(
  100. this.safeCast[`toInt${bits}`](maxValue.addn(1)),
  101. `SafeCast: value doesn't fit in ${bits} bits`,
  102. );
  103. });
  104. it(`reverts when downcasting 2^${bits - 1} + 1 (${maxValue.addn(2)})`, async function () {
  105. await expectRevert(
  106. this.safeCast[`toInt${bits}`](maxValue.addn(2)),
  107. `SafeCast: value doesn't fit in ${bits} bits`,
  108. );
  109. });
  110. });
  111. }
  112. [8, 16, 32, 64, 128].forEach(bits => testToInt(bits));
  113. describe('toInt256', () => {
  114. const maxUint256 = new BN('2').pow(new BN(256)).subn(1);
  115. const maxInt256 = new BN('2').pow(new BN(255)).subn(1);
  116. it('casts 0', async function () {
  117. expect(await this.safeCast.toInt256(0)).to.be.bignumber.equal('0');
  118. });
  119. it('casts 1', async function () {
  120. expect(await this.safeCast.toInt256(1)).to.be.bignumber.equal('1');
  121. });
  122. it(`casts INT256_MAX (${maxInt256})`, async function () {
  123. expect(await this.safeCast.toInt256(maxInt256)).to.be.bignumber.equal(maxInt256);
  124. });
  125. it(`reverts when casting INT256_MAX + 1 (${maxInt256.addn(1)})`, async function () {
  126. await expectRevert(
  127. this.safeCast.toInt256(maxInt256.addn(1)),
  128. 'SafeCast: value doesn\'t fit in an int256',
  129. );
  130. });
  131. it(`reverts when casting UINT256_MAX (${maxUint256})`, async function () {
  132. await expectRevert(
  133. this.safeCast.toInt256(maxUint256),
  134. 'SafeCast: value doesn\'t fit in an int256',
  135. );
  136. });
  137. });
  138. });