sorted-array.spec.ts 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /**
  2. * Copyright (c) 2017-2019 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. function compareArrays(a: ArrayLike<number>, b: ArrayLike<number>) {
  16. expect(a.length).toBe(b.length);
  17. for (let i = 0; i < a.length; i++) expect(a[i]).toBe(b[i]);
  18. }
  19. const a1234 = SortedArray.ofSortedArray([1, 2, 3, 4]);
  20. const a2468 = SortedArray.ofSortedArray([2, 4, 6, 8]);
  21. test('size', SortedArray.size(a1234), 4);
  22. test('min/max', [SortedArray.min(a1234), SortedArray.max(a1234)], [1, 4]);
  23. test('start/end', [SortedArray.start(a1234), SortedArray.end(a1234)], [1, 5]);
  24. test('has', SortedArray.has(a1234, 5), false);
  25. test('has', SortedArray.has(a1234, 4), true);
  26. it('has-all', () => {
  27. for (let i = 1; i <= 4; i++) expect(SortedArray.has(a1234, i)).toBe(true);
  28. });
  29. test('indexOf', SortedArray.indexOf(a2468, 5), -1);
  30. test('indexOf', SortedArray.indexOf(a2468, 2), 0);
  31. test('getAt', a2468[1], 4);
  32. test('areEqual', SortedArray.areEqual(a2468, a2468), true);
  33. test('areEqual1', SortedArray.areEqual(a2468, SortedArray.ofUnsortedArray([4, 2, 8, 6])), true);
  34. test('areEqual2', SortedArray.areEqual(a1234, a2468), false);
  35. test('predIndex1', SortedArray.findPredecessorIndex(a1234, 5), 4);
  36. test('predIndex2', SortedArray.findPredecessorIndex(a1234, 2), 1);
  37. test('predIndex3', SortedArray.findPredecessorIndex(a2468, 4), 1);
  38. test('predIndex4', SortedArray.findPredecessorIndex(a2468, 3), 1);
  39. test('predIndexInt', SortedArray.findPredecessorIndexInInterval(a1234, 0, Interval.ofRange(2, 3)), 2);
  40. testI('findRange', SortedArray.findRange(a2468, 2, 4), Interval.ofRange(0, 1));
  41. it('deduplicate', () => {
  42. compareArrays(SortedArray.deduplicate(SortedArray.ofSortedArray([1, 1, 1, 1])), [1]);
  43. compareArrays(SortedArray.deduplicate(SortedArray.ofSortedArray([1, 1, 2, 2, 3, 4])), [1, 2, 3, 4]);
  44. compareArrays(SortedArray.deduplicate(SortedArray.ofSortedArray([1, 2, 3])), [1, 2, 3]);
  45. });
  46. it('indicesOf', () => {
  47. compareArrays(SortedArray.indicesOf(SortedArray.ofSortedArray([10, 11, 12]), SortedArray.ofSortedArray([10, 12, 14])), [0, 2]);
  48. });
  49. it('indicesOf 2', () => {
  50. compareArrays(
  51. SortedArray.indicesOf(
  52. SortedArray.ofSortedArray([0, 1, 2, 3, 4, 8, 9, 10]),
  53. SortedArray.ofSortedArray([1, 3, 4, 9, 10])
  54. ),
  55. [1, 3, 4, 6, 7]
  56. );
  57. });
  58. test('intersectionSize', SortedArray.intersectionSize(a1234, a2468), 2);
  59. it('union1', () => {
  60. compareArrays(
  61. SortedArray.union(
  62. SortedArray.ofSortedArray([830, 831, 832, 833, 834, 836, 837, 838, 839, 840, 841, 842, 843]),
  63. SortedArray.ofSortedArray([835])
  64. ),
  65. SortedArray.ofSortedArray([830, 831, 832, 833, 834, 835, 836, 837, 838, 839, 840, 841, 842, 843])
  66. );
  67. });
  68. it('union2', () => {
  69. compareArrays(
  70. SortedArray.union(
  71. SortedArray.ofSortedArray([830, 832, 833]),
  72. SortedArray.ofSortedArray([831])
  73. ),
  74. SortedArray.ofSortedArray([830, 831, 832, 833])
  75. );
  76. });
  77. it('union3ab', () => {
  78. compareArrays(
  79. SortedArray.union(
  80. SortedArray.ofSortedArray([830, 831, 832, 833, 834, 835]),
  81. SortedArray.ofSortedArray([836, 837, 838, 839, 840, 841, 842, 843])
  82. ),
  83. SortedArray.ofSortedArray([830, 831, 832, 833, 834, 835, 836, 837, 838, 839, 840, 841, 842, 843])
  84. );
  85. });
  86. it('union3ba', () => {
  87. compareArrays(
  88. SortedArray.union(
  89. SortedArray.ofSortedArray([836, 837, 838, 839, 840, 841, 842, 843]),
  90. SortedArray.ofSortedArray([830, 831, 832, 833, 834, 835])
  91. ),
  92. SortedArray.ofSortedArray([830, 831, 832, 833, 834, 835, 836, 837, 838, 839, 840, 841, 842, 843])
  93. );
  94. });
  95. it('union4', () => {
  96. compareArrays(
  97. SortedArray.union(
  98. SortedArray.ofSortedArray([1, 3, 5, 7, 9]),
  99. SortedArray.ofSortedArray([2, 4, 6, 8])
  100. ),
  101. SortedArray.ofSortedArray([1, 2, 3, 4, 5, 6, 7, 8, 9])
  102. );
  103. });
  104. it('union5', () => {
  105. compareArrays(
  106. SortedArray.union(
  107. SortedArray.ofSortedArray([2, 3, 4, 20, 21, 22]),
  108. SortedArray.ofSortedArray([10, 11, 12])
  109. ),
  110. SortedArray.ofSortedArray([2, 3, 4, 10, 11, 12, 20, 21, 22])
  111. );
  112. });
  113. it('union6', () => {
  114. compareArrays(
  115. SortedArray.union(
  116. SortedArray.ofSortedArray([768, 769, 770, 771, 772, 773, 774, 775, 776, 819, 820, 821, 822, 823, 824, 825, 826, 827, 828, 829, 1811, 1812, 1813, 1814, 1815, 1816, 1817, 1818, 1819]),
  117. SortedArray.ofSortedArray([1751, 1752, 1753, 1754, 1755, 1756, 1757, 1758])
  118. ),
  119. SortedArray.ofSortedArray([768, 769, 770, 771, 772, 773, 774, 775, 776, 819, 820, 821, 822, 823, 824, 825, 826, 827, 828, 829, 1751, 1752, 1753, 1754, 1755, 1756, 1757, 1758, 1811, 1812, 1813, 1814, 1815, 1816, 1817, 1818, 1819])
  120. );
  121. });
  122. it('union7', () => {
  123. compareArrays(
  124. SortedArray.union(
  125. SortedArray.ofSortedArray([3766, 3767, 3768, 3770, 3773, 3780, 3783, 3787, 3797]),
  126. SortedArray.ofSortedArray([3769, 3790, 3794])
  127. ),
  128. SortedArray.ofSortedArray([3766, 3767, 3768, 3769, 3770, 3773, 3780, 3783, 3787, 3790, 3794, 3797])
  129. );
  130. });
  131. it('isSubset', () => {
  132. expect(SortedArray.isSubset(
  133. SortedArray.ofSortedArray([1271, 1272, 1273, 1274, 1275, 1276, 1277, 1278, 1279, 1280, 1281, 1282, 1283, 1284, 1285, 1286, 1287, 1288, 1289, 1290, 1291, 1292, 1293, 1294, 1295]),
  134. SortedArray.ofSortedArray([1271, 1272, 1274, 1275, 1276, 1278, 1280, 1282, 1284, 1286, 1288, 1290, 1292, 1294])
  135. )).toBe(true);
  136. });
  137. });