Arrays.test.js 2.9 KB

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