SafeCast.test.js 5.4 KB

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