Strings.test.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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('addresses', function () {
  96. const addresses = [
  97. '0xa9036907dccae6a1e0033479b12e837e5cf5a02f', // Random address
  98. '0x0000e0ca771e21bd00057f54a68c30d400000000', // Leading and trailing zeros
  99. // EIP-55 reference
  100. '0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed',
  101. '0xfB6916095ca1df60bB79Ce92cE3Ea74c37c5d359',
  102. '0xdbF03B407c01E7cD3CBea99509d93f8DDDC8C6FB',
  103. '0xD1220A0cf47c7B9Be7A2E6BA89F429762e7b9aDb',
  104. '0xfb6916095ca1df60bb79ce92ce3ea74c37c5d359',
  105. '0x52908400098527886E0F7030069857D2E4169EE7',
  106. '0x8617E340B3D01FA5F11F306F4090FD50E238070D',
  107. '0xde709f2102306220921060314715629080e2fb77',
  108. '0x27b1fdb04752bbc536007a920d24acb045561c26',
  109. '0x5aaeb6053f3e94c9b9a09f33669435e7ef1beaed',
  110. '0xfB6916095ca1df60bB79Ce92cE3Ea74c37c5d359',
  111. '0xdbF03B407c01E7cD3CBea99509d93f8DDDC8C6FB',
  112. '0xD1220A0cf47c7B9Be7A2E6BA89F429762e7b9aDb',
  113. ];
  114. describe('toHexString', function () {
  115. for (const addr of addresses) {
  116. it(`converts ${addr}`, async function () {
  117. expect(await this.mock.getFunction('$toHexString(address)')(addr)).to.equal(addr.toLowerCase());
  118. });
  119. }
  120. });
  121. describe('toChecksumHexString', function () {
  122. for (const addr of addresses) {
  123. it(`converts ${addr}`, async function () {
  124. expect(await this.mock.getFunction('$toChecksumHexString(address)')(addr)).to.equal(
  125. ethers.getAddress(addr.toLowerCase()),
  126. );
  127. });
  128. }
  129. });
  130. });
  131. describe('equal', function () {
  132. it('compares two empty strings', async function () {
  133. expect(await this.mock.$equal('', '')).to.be.true;
  134. });
  135. it('compares two equal strings', async function () {
  136. expect(await this.mock.$equal('a', 'a')).to.be.true;
  137. });
  138. it('compares two different strings', async function () {
  139. expect(await this.mock.$equal('a', 'b')).to.be.false;
  140. });
  141. it('compares two different strings of different lengths', async function () {
  142. expect(await this.mock.$equal('a', 'aa')).to.be.false;
  143. expect(await this.mock.$equal('aa', 'a')).to.be.false;
  144. });
  145. it('compares two different large strings', async function () {
  146. const str1 = 'a'.repeat(201);
  147. const str2 = 'a'.repeat(200) + 'b';
  148. expect(await this.mock.$equal(str1, str2)).to.be.false;
  149. });
  150. it('compares two equal large strings', async function () {
  151. const str1 = 'a'.repeat(201);
  152. const str2 = 'a'.repeat(201);
  153. expect(await this.mock.$equal(str1, str2)).to.be.true;
  154. });
  155. });
  156. });