Arrays.test.js 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. const { contract } = require('@openzeppelin/test-environment');
  2. require('@openzeppelin/test-helpers');
  3. const { expect } = require('chai');
  4. const ArraysImpl = contract.fromArtifact('ArraysImpl');
  5. describe('Arrays', function () {
  6. describe('findUpperBound', function () {
  7. context('Even number of elements', function () {
  8. const EVEN_ELEMENTS_ARRAY = [11, 12, 13, 14, 15, 16, 17, 18, 19, 20];
  9. beforeEach(async function () {
  10. this.arrays = await ArraysImpl.new(EVEN_ELEMENTS_ARRAY);
  11. });
  12. it('returns correct index for the basic case', async function () {
  13. expect(await this.arrays.findUpperBound(16)).to.be.bignumber.equal('5');
  14. });
  15. it('returns 0 for the first element', async function () {
  16. expect(await this.arrays.findUpperBound(11)).to.be.bignumber.equal('0');
  17. });
  18. it('returns index of the last element', async function () {
  19. expect(await this.arrays.findUpperBound(20)).to.be.bignumber.equal('9');
  20. });
  21. it('returns first index after last element if searched value is over the upper boundary', async function () {
  22. expect(await this.arrays.findUpperBound(32)).to.be.bignumber.equal('10');
  23. });
  24. it('returns 0 for the element under the lower boundary', async function () {
  25. expect(await this.arrays.findUpperBound(2)).to.be.bignumber.equal('0');
  26. });
  27. });
  28. context('Odd number of elements', function () {
  29. const ODD_ELEMENTS_ARRAY = [11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21];
  30. beforeEach(async function () {
  31. this.arrays = await ArraysImpl.new(ODD_ELEMENTS_ARRAY);
  32. });
  33. it('returns correct index for the basic case', async function () {
  34. expect(await this.arrays.findUpperBound(16)).to.be.bignumber.equal('5');
  35. });
  36. it('returns 0 for the first element', async function () {
  37. expect(await this.arrays.findUpperBound(11)).to.be.bignumber.equal('0');
  38. });
  39. it('returns index of the last element', async function () {
  40. expect(await this.arrays.findUpperBound(21)).to.be.bignumber.equal('10');
  41. });
  42. it('returns first index after last element if searched value is over the upper boundary', async function () {
  43. expect(await this.arrays.findUpperBound(32)).to.be.bignumber.equal('11');
  44. });
  45. it('returns 0 for the element under the lower boundary', async function () {
  46. expect(await this.arrays.findUpperBound(2)).to.be.bignumber.equal('0');
  47. });
  48. });
  49. context('Array with gap', function () {
  50. const WITH_GAP_ARRAY = [11, 12, 13, 14, 15, 20, 21, 22, 23, 24];
  51. beforeEach(async function () {
  52. this.arrays = await ArraysImpl.new(WITH_GAP_ARRAY);
  53. });
  54. it('returns index of first element in next filled range', async function () {
  55. expect(await this.arrays.findUpperBound(17)).to.be.bignumber.equal('5');
  56. });
  57. });
  58. context('Empty array', function () {
  59. beforeEach(async function () {
  60. this.arrays = await ArraysImpl.new([]);
  61. });
  62. it('always returns 0 for empty array', async function () {
  63. expect(await this.arrays.findUpperBound(10)).to.be.bignumber.equal('0');
  64. });
  65. });
  66. });
  67. });