Arrays.test.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. require('@openzeppelin/test-helpers');
  2. const { expect } = require('chai');
  3. const ArraysImpl = artifacts.require('ArraysImpl');
  4. contract('Arrays', function (accounts) {
  5. describe('findUpperBound', 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('returns correct index for the basic case', async function () {
  12. expect(await this.arrays.findUpperBound(16)).to.be.bignumber.equal('5');
  13. });
  14. it('returns 0 for the first element', async function () {
  15. expect(await this.arrays.findUpperBound(11)).to.be.bignumber.equal('0');
  16. });
  17. it('returns index of the last element', async function () {
  18. expect(await this.arrays.findUpperBound(20)).to.be.bignumber.equal('9');
  19. });
  20. it('returns 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('returns 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('returns correct index for the basic case', async function () {
  33. expect(await this.arrays.findUpperBound(16)).to.be.bignumber.equal('5');
  34. });
  35. it('returns 0 for the first element', async function () {
  36. expect(await this.arrays.findUpperBound(11)).to.be.bignumber.equal('0');
  37. });
  38. it('returns index of the last element', async function () {
  39. expect(await this.arrays.findUpperBound(21)).to.be.bignumber.equal('10');
  40. });
  41. it('returns 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('returns 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('returns 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('always returns 0 for empty array', async function () {
  62. expect(await this.arrays.findUpperBound(10)).to.be.bignumber.equal('0');
  63. });
  64. });
  65. });
  66. });