Strings.test.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. const { ethers } = require('hardhat');
  2. const { expect } = require('chai');
  3. const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
  4. async function fixture() {
  5. const mock = await ethers.deployContract('$Strings');
  6. return { mock };
  7. }
  8. describe('Strings', function () {
  9. before(async function () {
  10. Object.assign(this, await loadFixture(fixture));
  11. });
  12. describe('toString', function () {
  13. const values = [
  14. 0n,
  15. 7n,
  16. 10n,
  17. 99n,
  18. 100n,
  19. 101n,
  20. 123n,
  21. 4132n,
  22. 12345n,
  23. 1234567n,
  24. 1234567890n,
  25. 123456789012345n,
  26. 12345678901234567890n,
  27. 123456789012345678901234567890n,
  28. 1234567890123456789012345678901234567890n,
  29. 12345678901234567890123456789012345678901234567890n,
  30. 123456789012345678901234567890123456789012345678901234567890n,
  31. 1234567890123456789012345678901234567890123456789012345678901234567890n,
  32. ];
  33. describe('uint256', function () {
  34. it('converts MAX_UINT256', async function () {
  35. const value = ethers.MaxUint256;
  36. expect(await this.mock.$toString(value)).to.equal(value.toString(10));
  37. });
  38. for (const value of values) {
  39. it(`converts ${value}`, async function () {
  40. expect(await this.mock.$toString(value)).to.equal(value);
  41. });
  42. }
  43. });
  44. describe('int256', function () {
  45. it('converts MAX_INT256', async function () {
  46. const value = ethers.MaxInt256;
  47. expect(await this.mock.$toStringSigned(value)).to.equal(value.toString(10));
  48. });
  49. it('converts MIN_INT256', async function () {
  50. const value = ethers.MinInt256;
  51. expect(await this.mock.$toStringSigned(value)).to.equal(value.toString(10));
  52. });
  53. for (const value of values) {
  54. it(`convert ${value}`, async function () {
  55. expect(await this.mock.$toStringSigned(value)).to.equal(value);
  56. });
  57. it(`convert negative ${value}`, async function () {
  58. const negated = -value;
  59. expect(await this.mock.$toStringSigned(negated)).to.equal(negated.toString(10));
  60. });
  61. }
  62. });
  63. });
  64. describe('toHexString', function () {
  65. it('converts 0', async function () {
  66. expect(await this.mock.getFunction('$toHexString(uint256)')(0n)).to.equal('0x00');
  67. });
  68. it('converts a positive number', async function () {
  69. expect(await this.mock.getFunction('$toHexString(uint256)')(0x4132n)).to.equal('0x4132');
  70. });
  71. it('converts MAX_UINT256', async function () {
  72. expect(await this.mock.getFunction('$toHexString(uint256)')(ethers.MaxUint256)).to.equal(
  73. `0x${ethers.MaxUint256.toString(16)}`,
  74. );
  75. });
  76. });
  77. describe('toHexString fixed', function () {
  78. it('converts a positive number (long)', async function () {
  79. expect(await this.mock.getFunction('$toHexString(uint256,uint256)')(0x4132n, 32n)).to.equal(
  80. '0x0000000000000000000000000000000000000000000000000000000000004132',
  81. );
  82. });
  83. it('converts a positive number (short)', async function () {
  84. const length = 1n;
  85. await expect(this.mock.getFunction('$toHexString(uint256,uint256)')(0x4132n, length))
  86. .to.be.revertedWithCustomError(this.mock, `StringsInsufficientHexLength`)
  87. .withArgs(0x4132, length);
  88. });
  89. it('converts MAX_UINT256', async function () {
  90. expect(await this.mock.getFunction('$toHexString(uint256,uint256)')(ethers.MaxUint256, 32n)).to.equal(
  91. `0x${ethers.MaxUint256.toString(16)}`,
  92. );
  93. });
  94. });
  95. describe('toHexString address', function () {
  96. it('converts a random address', async function () {
  97. const addr = '0xa9036907dccae6a1e0033479b12e837e5cf5a02f';
  98. expect(await this.mock.getFunction('$toHexString(address)')(addr)).to.equal(addr);
  99. });
  100. it('converts an address with leading zeros', async function () {
  101. const addr = '0x0000e0ca771e21bd00057f54a68c30d400000000';
  102. expect(await this.mock.getFunction('$toHexString(address)')(addr)).to.equal(addr);
  103. });
  104. });
  105. describe('equal', function () {
  106. it('compares two empty strings', async function () {
  107. expect(await this.mock.$equal('', '')).to.be.true;
  108. });
  109. it('compares two equal strings', async function () {
  110. expect(await this.mock.$equal('a', 'a')).to.be.true;
  111. });
  112. it('compares two different strings', async function () {
  113. expect(await this.mock.$equal('a', 'b')).to.be.false;
  114. });
  115. it('compares two different strings of different lengths', async function () {
  116. expect(await this.mock.$equal('a', 'aa')).to.be.false;
  117. expect(await this.mock.$equal('aa', 'a')).to.be.false;
  118. });
  119. it('compares two different large strings', async function () {
  120. const str1 = 'a'.repeat(201);
  121. const str2 = 'a'.repeat(200) + 'b';
  122. expect(await this.mock.$equal(str1, str2)).to.be.false;
  123. });
  124. it('compares two equal large strings', async function () {
  125. const str1 = 'a'.repeat(201);
  126. const str2 = 'a'.repeat(201);
  127. expect(await this.mock.$equal(str1, str2)).to.be.true;
  128. });
  129. });
  130. });