SafeCast.test.js 5.7 KB

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