Arrays.test.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. const { ethers } = require('hardhat');
  2. const { expect } = require('chai');
  3. const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
  4. const { generators } = require('../helpers/random');
  5. const { capitalize } = require('../../scripts/helpers');
  6. const { TYPES } = require('../../scripts/generate/templates/Arrays.opts');
  7. // See https://en.cppreference.com/w/cpp/algorithm/lower_bound
  8. const lowerBound = (array, value) => {
  9. const i = array.findIndex(element => value <= element);
  10. return i == -1 ? array.length : i;
  11. };
  12. // See https://en.cppreference.com/w/cpp/algorithm/upper_bound
  13. const upperBound = (array, value) => {
  14. const i = array.findIndex(element => value < element);
  15. return i == -1 ? array.length : i;
  16. };
  17. const bigintSign = x => (x > 0n ? 1 : x < 0n ? -1 : 0);
  18. const comparator = (a, b) => bigintSign(ethers.toBigInt(a) - ethers.toBigInt(b));
  19. const hasDuplicates = array => array.some((v, i) => array.indexOf(v) != i);
  20. describe('Arrays', function () {
  21. const fixture = async () => {
  22. return { mock: await ethers.deployContract('$Arrays') };
  23. };
  24. beforeEach(async function () {
  25. Object.assign(this, await loadFixture(fixture));
  26. });
  27. describe('search', function () {
  28. for (const [title, { array, tests }] of Object.entries({
  29. 'Even number of elements': {
  30. array: [11n, 12n, 13n, 14n, 15n, 16n, 17n, 18n, 19n, 20n],
  31. tests: {
  32. 'basic case': 16n,
  33. 'first element': 11n,
  34. 'last element': 20n,
  35. 'searched value is over the upper boundary': 32n,
  36. 'searched value is under the lower boundary': 2n,
  37. },
  38. },
  39. 'Odd number of elements': {
  40. array: [11n, 12n, 13n, 14n, 15n, 16n, 17n, 18n, 19n, 20n, 21n],
  41. tests: {
  42. 'basic case': 16n,
  43. 'first element': 11n,
  44. 'last element': 21n,
  45. 'searched value is over the upper boundary': 32n,
  46. 'searched value is under the lower boundary': 2n,
  47. },
  48. },
  49. 'Array with gap': {
  50. array: [11n, 12n, 13n, 14n, 15n, 20n, 21n, 22n, 23n, 24n],
  51. tests: {
  52. 'search value in gap': 17n,
  53. },
  54. },
  55. 'Array with duplicated elements': {
  56. array: [0n, 10n, 10n, 10n, 10n, 10n, 10n, 10n, 20n],
  57. tests: {
  58. 'search value is duplicated': 10n,
  59. },
  60. },
  61. 'Array with duplicated first element': {
  62. array: [10n, 10n, 10n, 10n, 10n, 10n, 10n, 20n],
  63. tests: {
  64. 'search value is duplicated first element': 10n,
  65. },
  66. },
  67. 'Array with duplicated last element': {
  68. array: [0n, 10n, 10n, 10n, 10n, 10n, 10n, 10n],
  69. tests: {
  70. 'search value is duplicated last element': 10n,
  71. },
  72. },
  73. 'Empty array': {
  74. array: [],
  75. tests: {
  76. 'always returns 0 for empty array': 10n,
  77. },
  78. },
  79. })) {
  80. describe(title, function () {
  81. const fixture = async () => {
  82. return { instance: await ethers.deployContract('Uint256ArraysMock', [array]) };
  83. };
  84. beforeEach(async function () {
  85. Object.assign(this, await loadFixture(fixture));
  86. });
  87. for (const [name, input] of Object.entries(tests)) {
  88. describe(name, function () {
  89. it('[deprecated] findUpperBound', async function () {
  90. // findUpperBound does not support duplicated
  91. if (hasDuplicates(array)) {
  92. expect(await this.instance.findUpperBound(input)).to.equal(upperBound(array, input) - 1);
  93. } else {
  94. expect(await this.instance.findUpperBound(input)).to.equal(lowerBound(array, input));
  95. }
  96. });
  97. it('lowerBound', async function () {
  98. expect(await this.instance.lowerBound(input)).to.equal(lowerBound(array, input));
  99. expect(await this.instance.lowerBoundMemory(array, input)).to.equal(lowerBound(array, input));
  100. });
  101. it('upperBound', async function () {
  102. expect(await this.instance.upperBound(input)).to.equal(upperBound(array, input));
  103. expect(await this.instance.upperBoundMemory(array, input)).to.equal(upperBound(array, input));
  104. });
  105. });
  106. }
  107. });
  108. }
  109. });
  110. for (const type of TYPES) {
  111. const elements = Array.from({ length: 10 }, generators[type]);
  112. describe(type, function () {
  113. const fixture = async () => {
  114. return { instance: await ethers.deployContract(`${capitalize(type)}ArraysMock`, [elements]) };
  115. };
  116. beforeEach(async function () {
  117. Object.assign(this, await loadFixture(fixture));
  118. });
  119. describe('sort', function () {
  120. for (const length of [0, 1, 2, 8, 32, 128]) {
  121. describe(`${type}[] of length ${length}`, function () {
  122. beforeEach(async function () {
  123. this.array = Array.from({ length }, generators[type]);
  124. });
  125. afterEach(async function () {
  126. const expected = Array.from(this.array).sort(comparator);
  127. const reversed = Array.from(expected).reverse();
  128. expect(await this.instance.sort(this.array)).to.deep.equal(expected);
  129. expect(await this.instance.sortReverse(this.array)).to.deep.equal(reversed);
  130. });
  131. it('sort array', async function () {
  132. // nothing to do here, beforeEach and afterEach already take care of everything.
  133. });
  134. if (length > 1) {
  135. it('sort array for identical elements', async function () {
  136. // duplicate the first value to all elements
  137. this.array.fill(this.array.at(0));
  138. });
  139. it('sort already sorted array', async function () {
  140. // pre-sort the elements
  141. this.array.sort(comparator);
  142. });
  143. it('sort reversed array', async function () {
  144. // pre-sort in reverse order
  145. this.array.sort(comparator).reverse();
  146. });
  147. it('sort almost sorted array', async function () {
  148. // pre-sort + rotate (move the last element to the front) for an almost sorted effect
  149. this.array.sort(comparator);
  150. this.array.unshift(this.array.pop());
  151. });
  152. }
  153. });
  154. }
  155. });
  156. describe('unsafeAccess', function () {
  157. describe('storage', function () {
  158. for (const i in elements) {
  159. it(`unsafeAccess within bounds #${i}`, async function () {
  160. expect(await this.instance.unsafeAccess(i)).to.equal(elements[i]);
  161. });
  162. }
  163. it('unsafeAccess outside bounds', async function () {
  164. await expect(this.instance.unsafeAccess(elements.length)).to.not.be.rejected;
  165. });
  166. it('unsafeSetLength changes the length or the array', async function () {
  167. const newLength = generators.uint256();
  168. expect(await this.instance.length()).to.equal(elements.length);
  169. await expect(this.instance.unsafeSetLength(newLength)).to.not.be.rejected;
  170. expect(await this.instance.length()).to.equal(newLength);
  171. });
  172. });
  173. describe('memory', function () {
  174. const fragment = `$unsafeMemoryAccess(${type}[] arr, uint256 pos)`;
  175. for (const i in elements) {
  176. it(`unsafeMemoryAccess within bounds #${i}`, async function () {
  177. expect(await this.mock[fragment](elements, i)).to.equal(elements[i]);
  178. });
  179. }
  180. it('unsafeMemoryAccess outside bounds', async function () {
  181. await expect(this.mock[fragment](elements, elements.length)).to.not.be.rejected;
  182. });
  183. it('unsafeMemoryAccess loop around', async function () {
  184. for (let i = 251n; i < 256n; ++i) {
  185. expect(await this.mock[fragment](elements, 2n ** i - 1n)).to.equal(BigInt(elements.length));
  186. expect(await this.mock[fragment](elements, 2n ** i + 0n)).to.equal(elements[0]);
  187. expect(await this.mock[fragment](elements, 2n ** i + 1n)).to.equal(elements[1]);
  188. }
  189. });
  190. });
  191. });
  192. });
  193. }
  194. });