Arrays.test.js 3.0 KB

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