SafeCast.test.js 5.7 KB

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