Arrays.test.js 3.1 KB

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