SafeCast.test.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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`, [255, -1]);
  53. });
  54. it(`reverts when casting INT256_MIN (${minInt256})`, async function () {
  55. await expectRevertCustomError(this.safeCast.$toUint256(minInt256), `SafeCastOverflowedIntToUint`, [
  56. 255,
  57. minInt256,
  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 expectRevertCustomError(
  82. this.safeCast[`$toInt${bits}`](minValue.subn(1)),
  83. `SafeCastOverflowedIntDowncast`,
  84. [bits, minValue.subn(1)],
  85. );
  86. });
  87. it(`reverts when downcasting -2^${bits - 1} - 2 (${minValue.subn(2)})`, async function () {
  88. await expectRevertCustomError(
  89. this.safeCast[`$toInt${bits}`](minValue.subn(2)),
  90. `SafeCastOverflowedIntDowncast`,
  91. [bits, minValue.subn(2)],
  92. );
  93. });
  94. it(`reverts when downcasting 2^${bits - 1} (${maxValue.addn(1)})`, async function () {
  95. await expectRevertCustomError(
  96. this.safeCast[`$toInt${bits}`](maxValue.addn(1)),
  97. `SafeCastOverflowedIntDowncast`,
  98. [bits, maxValue.addn(1)],
  99. );
  100. });
  101. it(`reverts when downcasting 2^${bits - 1} + 1 (${maxValue.addn(2)})`, async function () {
  102. await expectRevertCustomError(
  103. this.safeCast[`$toInt${bits}`](maxValue.addn(2)),
  104. `SafeCastOverflowedIntDowncast`,
  105. [bits, maxValue.addn(2)],
  106. );
  107. });
  108. });
  109. }
  110. range(8, 256, 8).forEach(bits => testToInt(bits));
  111. describe('toInt256', () => {
  112. const maxUint256 = new BN('2').pow(new BN(256)).subn(1);
  113. const maxInt256 = new BN('2').pow(new BN(255)).subn(1);
  114. it('casts 0', async function () {
  115. expect(await this.safeCast.$toInt256(0)).to.be.bignumber.equal('0');
  116. });
  117. it('casts 1', async function () {
  118. expect(await this.safeCast.$toInt256(1)).to.be.bignumber.equal('1');
  119. });
  120. it(`casts INT256_MAX (${maxInt256})`, async function () {
  121. expect(await this.safeCast.$toInt256(maxInt256)).to.be.bignumber.equal(maxInt256);
  122. });
  123. it(`reverts when casting INT256_MAX + 1 (${maxInt256.addn(1)})`, async function () {
  124. await expectRevertCustomError(this.safeCast.$toInt256(maxInt256.addn(1)), 'SafeCastOverflowedUintToInt', [
  125. 255,
  126. maxInt256.addn(1),
  127. ]);
  128. });
  129. it(`reverts when casting UINT256_MAX (${maxUint256})`, async function () {
  130. await expectRevertCustomError(this.safeCast.$toInt256(maxUint256), 'SafeCastOverflowedUintToInt', [
  131. 255,
  132. maxUint256,
  133. ]);
  134. });
  135. });
  136. });