peakcache.spec.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import PeakCache from '../src/peakcache';
  2. describe('PeakCache:', function() {
  3. var peakcache;
  4. var test_length = 200;
  5. var test_length2 = 300;
  6. var test_start = 50;
  7. var test_end = 100;
  8. var test_start2 = 100;
  9. var test_end2 = 120;
  10. var test_start3 = 120;
  11. var test_end3 = 150;
  12. var window_size = 20;
  13. function __createPeakCache() {
  14. peakcache = new PeakCache();
  15. }
  16. beforeEach(function (done) {
  17. __createPeakCache();
  18. done();
  19. });
  20. /** @test {PeakCache#addRangeToPeakCache} */
  21. it('empty cache returns full range', function() {
  22. var newranges = peakcache.addRangeToPeakCache(test_length, test_start, test_end);
  23. expect(newranges.length).toEqual(1);
  24. expect(newranges[0][0]).toEqual(test_start);
  25. expect(newranges[0][1]).toEqual(test_end);
  26. });
  27. /** @test {PeakCache#addRangeToPeakCache} */
  28. it('different length clears cache', function() {
  29. peakcache.addRangeToPeakCache(test_length, test_start, test_end);
  30. var newranges = peakcache.addRangeToPeakCache(test_length2, test_start, test_end);
  31. expect(newranges.length).toEqual(1);
  32. expect(newranges[0][0]).toEqual(test_start);
  33. expect(newranges[0][1]).toEqual(test_end);
  34. });
  35. /** @test {PeakCache#addRangeToPeakCache} */
  36. it('consecutive calls return no ranges', function() {
  37. peakcache.addRangeToPeakCache(test_length, test_start, test_end);
  38. var newranges = peakcache.addRangeToPeakCache(test_length, test_start, test_end);
  39. expect(newranges.length).toEqual(0);
  40. });
  41. /** @test {PeakCache#addRangeToPeakCache} */
  42. it('sliding window returns window sized range', function() {
  43. var newranges = peakcache.addRangeToPeakCache(test_length, test_start, test_end);
  44. expect(newranges.length).toEqual(1);
  45. expect(newranges[0][0]).toEqual(test_start);
  46. expect(newranges[0][1]).toEqual(test_end);
  47. var newranges = peakcache.addRangeToPeakCache(test_length, test_start + window_size, test_end + window_size);
  48. expect(newranges.length).toEqual(1);
  49. expect(newranges[0][0]).toEqual(test_end);
  50. expect(newranges[0][1]).toEqual(test_end + window_size);
  51. var newranges = peakcache.addRangeToPeakCache(test_length, test_start + window_size * 2, test_end + window_size * 2);
  52. expect(newranges.length).toEqual(1);
  53. expect(newranges[0][0]).toEqual(test_end + window_size);
  54. expect(newranges[0][1]).toEqual(test_end + window_size * 2);
  55. });
  56. /** @test {PeakCache#addRangeToPeakCache} */
  57. /** @test {PeakCache#getCacheRanges} */
  58. it('disjoint set creates two ranges', function() {
  59. peakcache.addRangeToPeakCache(test_length, test_start, test_end);
  60. peakcache.addRangeToPeakCache(test_length, test_start3, test_end3);
  61. var ranges = peakcache.getCacheRanges();
  62. expect(ranges.length).toEqual(2);
  63. expect(ranges[0][0]).toEqual(test_start);
  64. expect(ranges[0][1]).toEqual(test_end);
  65. expect(ranges[1][0]).toEqual(test_start3);
  66. expect(ranges[1][1]).toEqual(test_end3);
  67. });
  68. /** @test {PeakCache#addRangeToPeakCache} */
  69. /** @test {PeakCache#getCacheRanges} */
  70. it('filling in disjoint sets coalesces', function() {
  71. peakcache.addRangeToPeakCache(test_length, test_start, test_end);
  72. peakcache.addRangeToPeakCache(test_length, test_start3, test_end3);
  73. var newranges = peakcache.addRangeToPeakCache(test_length, test_start, test_end3);
  74. expect(newranges.length).toEqual(1);
  75. expect(newranges[0][0]).toEqual(test_end);
  76. expect(newranges[0][1]).toEqual(test_start3);
  77. var ranges = peakcache.getCacheRanges();
  78. expect(ranges.length).toEqual(1);
  79. expect(ranges[0][0]).toEqual(test_start);
  80. expect(ranges[0][1]).toEqual(test_end3);
  81. });
  82. /** @test {PeakCache#addRangeToPeakCache} */
  83. /** @test {PeakCache#getCacheRanges} */
  84. it('filling in disjoint sets coalesces / edge cases', function() {
  85. peakcache.addRangeToPeakCache(test_length, test_start, test_end);
  86. peakcache.addRangeToPeakCache(test_length, test_start3, test_end3);
  87. var newranges = peakcache.addRangeToPeakCache(test_length, test_start2, test_end2);
  88. expect(newranges.length).toEqual(1);
  89. expect(newranges[0][0]).toEqual(test_end);
  90. expect(newranges[0][1]).toEqual(test_start3);
  91. var ranges = peakcache.getCacheRanges();
  92. expect(ranges.length).toEqual(1);
  93. expect(ranges[0][0]).toEqual(test_start);
  94. expect(ranges[0][1]).toEqual(test_end3);
  95. });
  96. });