SafeCast.test.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. describe('toInt256', () => {
  69. const maxUint256 = new BN('2').pow(new BN(256)).subn(1);
  70. const maxInt256 = new BN('2').pow(new BN(255)).subn(1);
  71. it('casts 0', async function () {
  72. expect(await this.safeCast.toInt256(0)).to.be.bignumber.equal('0');
  73. });
  74. it('casts 1', async function () {
  75. expect(await this.safeCast.toInt256(1)).to.be.bignumber.equal('1');
  76. });
  77. it(`casts INT256_MAX (${maxInt256})`, async function () {
  78. expect(await this.safeCast.toInt256(maxInt256)).to.be.bignumber.equal(maxInt256);
  79. });
  80. it(`reverts when casting INT256_MAX + 1 (${maxInt256.addn(1)})`, async function () {
  81. await expectRevert(
  82. this.safeCast.toInt256(maxInt256.addn(1)),
  83. 'SafeCast: value doesn\'t fit in an int256'
  84. );
  85. });
  86. it(`reverts when casting UINT256_MAX (${maxUint256})`, async function () {
  87. await expectRevert(
  88. this.safeCast.toInt256(maxUint256),
  89. 'SafeCast: value doesn\'t fit in an int256'
  90. );
  91. });
  92. });
  93. });