sorted-array.spec.ts 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /**
  2. * Copyright (c) 2017 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author David Sehnal <david.sehnal@gmail.com>
  5. */
  6. import Interval from '../interval'
  7. import SortedArray from '../sorted-array'
  8. describe('sortedArray', () => {
  9. function testI(name: string, a: Interval, b: Interval) {
  10. it(name, () => expect(Interval.areEqual(a, b)).toBe(true));
  11. }
  12. function test(name: string, a: any, b: any) {
  13. it(name, () => expect(a).toEqual(b));
  14. }
  15. const a1234 = SortedArray.ofSortedArray([1, 2, 3, 4]);
  16. const a2468 = SortedArray.ofSortedArray([2, 4, 6, 8]);
  17. test('size', SortedArray.size(a1234), 4);
  18. test('min/max', [SortedArray.min(a1234), SortedArray.max(a1234)], [1, 4]);
  19. test('start/end', [SortedArray.start(a1234), SortedArray.end(a1234)], [1, 5]);
  20. test('has', SortedArray.has(a1234, 5), false);
  21. test('has', SortedArray.has(a1234, 4), true);
  22. it('has-all', () => {
  23. for (let i = 1; i <= 4; i++) expect(SortedArray.has(a1234, i)).toBe(true);
  24. });
  25. test('indexOf', SortedArray.indexOf(a2468, 5), -1);
  26. test('indexOf', SortedArray.indexOf(a2468, 2), 0);
  27. test('getAt', a2468[1], 4);
  28. test('areEqual', SortedArray.areEqual(a2468, a2468), true);
  29. test('areEqual1', SortedArray.areEqual(a2468, SortedArray.ofUnsortedArray([4, 2, 8, 6])), true);
  30. test('areEqual2', SortedArray.areEqual(a1234, a2468), false);
  31. test('predIndex1', SortedArray.findPredecessorIndex(a1234, 5), 4);
  32. test('predIndex2', SortedArray.findPredecessorIndex(a1234, 2), 1);
  33. test('predIndex3', SortedArray.findPredecessorIndex(a2468, 4), 1);
  34. test('predIndex4', SortedArray.findPredecessorIndex(a2468, 3), 1);
  35. test('predIndexInt', SortedArray.findPredecessorIndexInInterval(a1234, 0, Interval.ofRange(2, 3)), 2);
  36. testI('findRange', SortedArray.findRange(a2468, 2, 4), Interval.ofRange(0, 1));
  37. // console.log(Interval.findPredecessorIndexInInterval(Interval.ofBounds(0, 3), 2, Interval.ofBounds(0, 3)))
  38. // console.log(SortedArray.findPredecessorIndexInInterval(SortedArray.ofSortedArray([0, 1, 2]), 2, Interval.ofBounds(0, 3)))
  39. });