segmentation.spec.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 OrderedSet from '../ordered-set'
  7. import Interval from '../interval'
  8. import Segmentation from '../segmentation'
  9. describe('segments', () => {
  10. const data = OrderedSet.ofSortedArray([4, 9, 10, 11, 14, 15, 16]);
  11. const segs = Segmentation.create([0, 4, 10, 12, 13, 15, 25]);
  12. it('size', () => expect(Segmentation.count(segs)).toBe(6));
  13. it('project', () => {
  14. const p = Segmentation.projectValue(segs, data, 4);
  15. expect(p).toBe(Interval.ofBounds(0, 2))
  16. });
  17. it('ofOffsetts', () => {
  18. const p = Segmentation.ofOffsets([10, 12], Interval.ofBounds(10, 14));
  19. expect(p.segments).toEqual(new Int32Array([0, 2, 4]))
  20. });
  21. it('map', () => {
  22. const segs = Segmentation.create([0, 1, 2]);
  23. expect(segs.segmentMap).toEqual(new Int32Array([0, 1]));
  24. expect(Segmentation.getSegment(segs, 0)).toBe(0);
  25. expect(Segmentation.getSegment(segs, 1)).toBe(1);
  26. });
  27. it('iteration', () => {
  28. const it = Segmentation.transientSegments(segs, data);
  29. const t = Object.create(null);
  30. let count = 0;
  31. while (it.hasNext) {
  32. count++;
  33. const s = it.move();
  34. for (let j = s.start; j < s.end; j++) {
  35. const x = t[s.index];
  36. const v = OrderedSet.getAt(data, j);
  37. if (!x) t[s.index] = [v];
  38. else x[x.length] = v;
  39. }
  40. }
  41. expect(t).toEqual({ 1: [4, 9], 2: [10, 11], 4: [14], 5: [15, 16] });
  42. expect(count).toBe(4);
  43. });
  44. it('units', () => {
  45. const data = OrderedSet.ofBounds(0, 4);
  46. const segs = Segmentation.create([0, 1, 2, 3, 4]);
  47. const it = Segmentation.transientSegments(segs, data, { index: 0, start: 2, end: 4 });
  48. const t = Object.create(null);
  49. let count = 0;
  50. while (it.hasNext) {
  51. count++;
  52. const s = it.move();
  53. for (let j = s.start; j < s.end; j++) {
  54. const x = t[s.index];
  55. const v = OrderedSet.getAt(data, j);
  56. if (!x) t[s.index] = [v];
  57. else x[x.length] = v;
  58. }
  59. }
  60. expect(t).toEqual({ 2: [2], 3: [3] });
  61. expect(count).toBe(2);
  62. });
  63. it('iteration range', () => {
  64. const segs = Segmentation.create([0, 2, 4]);
  65. const dataRange = OrderedSet.ofBounds(0, 4);
  66. const it = Segmentation.transientSegments(segs, dataRange);
  67. const t = Object.create(null);
  68. let count = 0;
  69. while (it.hasNext) {
  70. count++;
  71. const s = it.move();
  72. for (let j = s.start; j < s.end; j++) {
  73. const x = t[s.index];
  74. const v = OrderedSet.getAt(dataRange, j);
  75. if (!x) t[s.index] = [v];
  76. else x[x.length] = v;
  77. }
  78. }
  79. expect(count).toBe(2);
  80. expect(t).toEqual({ 0: [0, 1], 1: [2, 3] });
  81. });
  82. it('iteration range 1', () => {
  83. const segs = Segmentation.create([0, 2, 4]);
  84. const dataRange = OrderedSet.ofBounds(0, 4);
  85. const it = Segmentation.transientSegments(segs, dataRange, { index: 0, start: 2, end: 4 });
  86. const t = Object.create(null);
  87. let count = 0;
  88. while (it.hasNext) {
  89. count++;
  90. const s = it.move();
  91. for (let j = s.start; j < s.end; j++) {
  92. const x = t[s.index];
  93. const v = OrderedSet.getAt(dataRange, j);
  94. if (!x) t[s.index] = [v];
  95. else x[x.length] = v;
  96. }
  97. }
  98. expect(count).toBe(1);
  99. expect(t).toEqual({ 1: [2, 3] });
  100. });
  101. });