Arrays.test.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. const ArraysImpl = artifacts.require('ArraysImpl');
  2. const BigNumber = web3.BigNumber;
  3. require('chai')
  4. .use(require('chai-bignumber')(BigNumber))
  5. .should();
  6. contract('Arrays', 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('should return correct index for the basic case', async function () {
  13. (await this.arrays.findUpperBound(16)).should.be.bignumber.equal(5);
  14. });
  15. it('should return 0 for the first element', async function () {
  16. (await this.arrays.findUpperBound(11)).should.be.bignumber.equal(0);
  17. });
  18. it('should return index of the last element', async function () {
  19. (await this.arrays.findUpperBound(20)).should.be.bignumber.equal(9);
  20. });
  21. it('should return first index after last element if searched value is over the upper boundary', async function () {
  22. (await this.arrays.findUpperBound(32)).should.be.bignumber.equal(10);
  23. });
  24. it('should return 0 for the element under the lower boundary', async function () {
  25. (await this.arrays.findUpperBound(2)).should.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('should return correct index for the basic case', async function () {
  34. (await this.arrays.findUpperBound(16)).should.be.bignumber.equal(5);
  35. });
  36. it('should return 0 for the first element', async function () {
  37. (await this.arrays.findUpperBound(11)).should.be.bignumber.equal(0);
  38. });
  39. it('should return index of the last element', async function () {
  40. (await this.arrays.findUpperBound(21)).should.be.bignumber.equal(10);
  41. });
  42. it('should return first index after last element if searched value is over the upper boundary', async function () {
  43. (await this.arrays.findUpperBound(32)).should.be.bignumber.equal(11);
  44. });
  45. it('should return 0 for the element under the lower boundary', async function () {
  46. (await this.arrays.findUpperBound(2)).should.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('should return index of first element in next filled range', async function () {
  55. (await this.arrays.findUpperBound(17)).should.be.bignumber.equal(5);
  56. });
  57. });
  58. context('Empty array', function () {
  59. beforeEach(async function () {
  60. this.arrays = await ArraysImpl.new([]);
  61. });
  62. it('should always return 0 for empty array', async function () {
  63. (await this.arrays.findUpperBound(10)).should.be.bignumber.equal(0);
  64. });
  65. });
  66. });