Strings.test.js 5.3 KB

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