SafeCast.test.js 5.4 KB

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