SafeCast.test.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. const { BN, expectRevert } = require('@openzeppelin/test-helpers');
  2. const { expect } = require('chai');
  3. const { range } = require('../../../scripts/helpers');
  4. const SafeCast = artifacts.require('$SafeCast');
  5. contract('SafeCast', async function () {
  6. beforeEach(async function () {
  7. this.safeCast = await SafeCast.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(this.safeCast.$toUint256(-1), 'SafeCast: value must be positive');
  50. });
  51. it(`reverts when casting INT256_MIN (${minInt256})`, async function () {
  52. await expectRevert(this.safeCast.$toUint256(minInt256), 'SafeCast: value must be positive');
  53. });
  54. });
  55. function testToInt(bits) {
  56. describe(`toInt${bits}`, () => {
  57. const minValue = new BN('-2').pow(new BN(bits - 1));
  58. const maxValue = new BN('2').pow(new BN(bits - 1)).subn(1);
  59. it('downcasts 0', async function () {
  60. expect(await this.safeCast[`$toInt${bits}`](0)).to.be.bignumber.equal('0');
  61. });
  62. it('downcasts 1', async function () {
  63. expect(await this.safeCast[`$toInt${bits}`](1)).to.be.bignumber.equal('1');
  64. });
  65. it('downcasts -1', async function () {
  66. expect(await this.safeCast[`$toInt${bits}`](-1)).to.be.bignumber.equal('-1');
  67. });
  68. it(`downcasts -2^${bits - 1} (${minValue})`, async function () {
  69. expect(await this.safeCast[`$toInt${bits}`](minValue)).to.be.bignumber.equal(minValue);
  70. });
  71. it(`downcasts 2^${bits - 1} - 1 (${maxValue})`, async function () {
  72. expect(await this.safeCast[`$toInt${bits}`](maxValue)).to.be.bignumber.equal(maxValue);
  73. });
  74. it(`reverts when downcasting -2^${bits - 1} - 1 (${minValue.subn(1)})`, async function () {
  75. await expectRevert(
  76. this.safeCast[`$toInt${bits}`](minValue.subn(1)),
  77. `SafeCast: value doesn't fit in ${bits} bits`,
  78. );
  79. });
  80. it(`reverts when downcasting -2^${bits - 1} - 2 (${minValue.subn(2)})`, async function () {
  81. await expectRevert(
  82. this.safeCast[`$toInt${bits}`](minValue.subn(2)),
  83. `SafeCast: value doesn't fit in ${bits} bits`,
  84. );
  85. });
  86. it(`reverts when downcasting 2^${bits - 1} (${maxValue.addn(1)})`, async function () {
  87. await expectRevert(
  88. this.safeCast[`$toInt${bits}`](maxValue.addn(1)),
  89. `SafeCast: value doesn't fit in ${bits} bits`,
  90. );
  91. });
  92. it(`reverts when downcasting 2^${bits - 1} + 1 (${maxValue.addn(2)})`, async function () {
  93. await expectRevert(
  94. this.safeCast[`$toInt${bits}`](maxValue.addn(2)),
  95. `SafeCast: value doesn't fit in ${bits} bits`,
  96. );
  97. });
  98. });
  99. }
  100. range(8, 256, 8).forEach(bits => testToInt(bits));
  101. describe('toInt256', () => {
  102. const maxUint256 = new BN('2').pow(new BN(256)).subn(1);
  103. const maxInt256 = new BN('2').pow(new BN(255)).subn(1);
  104. it('casts 0', async function () {
  105. expect(await this.safeCast.$toInt256(0)).to.be.bignumber.equal('0');
  106. });
  107. it('casts 1', async function () {
  108. expect(await this.safeCast.$toInt256(1)).to.be.bignumber.equal('1');
  109. });
  110. it(`casts INT256_MAX (${maxInt256})`, async function () {
  111. expect(await this.safeCast.$toInt256(maxInt256)).to.be.bignumber.equal(maxInt256);
  112. });
  113. it(`reverts when casting INT256_MAX + 1 (${maxInt256.addn(1)})`, async function () {
  114. await expectRevert(this.safeCast.$toInt256(maxInt256.addn(1)), "SafeCast: value doesn't fit in an int256");
  115. });
  116. it(`reverts when casting UINT256_MAX (${maxUint256})`, async function () {
  117. await expectRevert(this.safeCast.$toInt256(maxUint256), "SafeCast: value doesn't fit in an int256");
  118. });
  119. });
  120. });