Arrays.test.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. require('@openzeppelin/test-helpers');
  2. const { expect } = require('chai');
  3. const AddressArraysMock = artifacts.require('AddressArraysMock');
  4. const Bytes32ArraysMock = artifacts.require('Bytes32ArraysMock');
  5. const Uint256ArraysMock = artifacts.require('Uint256ArraysMock');
  6. contract('Arrays', function () {
  7. describe('findUpperBound', function () {
  8. context('Even number of elements', function () {
  9. const EVEN_ELEMENTS_ARRAY = [11, 12, 13, 14, 15, 16, 17, 18, 19, 20];
  10. beforeEach(async function () {
  11. this.arrays = await Uint256ArraysMock.new(EVEN_ELEMENTS_ARRAY);
  12. });
  13. it('returns correct index for the basic case', async function () {
  14. expect(await this.arrays.findUpperBound(16)).to.be.bignumber.equal('5');
  15. });
  16. it('returns 0 for the first element', async function () {
  17. expect(await this.arrays.findUpperBound(11)).to.be.bignumber.equal('0');
  18. });
  19. it('returns index of the last element', async function () {
  20. expect(await this.arrays.findUpperBound(20)).to.be.bignumber.equal('9');
  21. });
  22. it('returns first index after last element if searched value is over the upper boundary', async function () {
  23. expect(await this.arrays.findUpperBound(32)).to.be.bignumber.equal('10');
  24. });
  25. it('returns 0 for the element under the lower boundary', async function () {
  26. expect(await this.arrays.findUpperBound(2)).to.be.bignumber.equal('0');
  27. });
  28. });
  29. context('Odd number of elements', function () {
  30. const ODD_ELEMENTS_ARRAY = [11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21];
  31. beforeEach(async function () {
  32. this.arrays = await Uint256ArraysMock.new(ODD_ELEMENTS_ARRAY);
  33. });
  34. it('returns correct index for the basic case', async function () {
  35. expect(await this.arrays.findUpperBound(16)).to.be.bignumber.equal('5');
  36. });
  37. it('returns 0 for the first element', async function () {
  38. expect(await this.arrays.findUpperBound(11)).to.be.bignumber.equal('0');
  39. });
  40. it('returns index of the last element', async function () {
  41. expect(await this.arrays.findUpperBound(21)).to.be.bignumber.equal('10');
  42. });
  43. it('returns first index after last element if searched value is over the upper boundary', async function () {
  44. expect(await this.arrays.findUpperBound(32)).to.be.bignumber.equal('11');
  45. });
  46. it('returns 0 for the element under the lower boundary', async function () {
  47. expect(await this.arrays.findUpperBound(2)).to.be.bignumber.equal('0');
  48. });
  49. });
  50. context('Array with gap', function () {
  51. const WITH_GAP_ARRAY = [11, 12, 13, 14, 15, 20, 21, 22, 23, 24];
  52. beforeEach(async function () {
  53. this.arrays = await Uint256ArraysMock.new(WITH_GAP_ARRAY);
  54. });
  55. it('returns index of first element in next filled range', async function () {
  56. expect(await this.arrays.findUpperBound(17)).to.be.bignumber.equal('5');
  57. });
  58. });
  59. context('Empty array', function () {
  60. beforeEach(async function () {
  61. this.arrays = await Uint256ArraysMock.new([]);
  62. });
  63. it('always returns 0 for empty array', async function () {
  64. expect(await this.arrays.findUpperBound(10)).to.be.bignumber.equal('0');
  65. });
  66. });
  67. });
  68. describe('unsafeAccess', function () {
  69. for (const { type, artifact, elements } of [
  70. {
  71. type: 'address',
  72. artifact: AddressArraysMock,
  73. elements: Array(10)
  74. .fill()
  75. .map(() => web3.utils.randomHex(20)),
  76. },
  77. {
  78. type: 'bytes32',
  79. artifact: Bytes32ArraysMock,
  80. elements: Array(10)
  81. .fill()
  82. .map(() => web3.utils.randomHex(32)),
  83. },
  84. {
  85. type: 'uint256',
  86. artifact: Uint256ArraysMock,
  87. elements: Array(10)
  88. .fill()
  89. .map(() => web3.utils.randomHex(32)),
  90. },
  91. ]) {
  92. it(type, async function () {
  93. const contract = await artifact.new(elements);
  94. for (const i in elements) {
  95. expect(await contract.unsafeAccess(i)).to.be.bignumber.equal(elements[i]);
  96. }
  97. });
  98. }
  99. });
  100. });