Strings.test.js 5.2 KB

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