diff --git a/platform/features/plot/src/elements/PlotLineBuffer.js b/platform/features/plot/src/elements/PlotLineBuffer.js index 307abae666..1504fe021c 100644 --- a/platform/features/plot/src/elements/PlotLineBuffer.js +++ b/platform/features/plot/src/elements/PlotLineBuffer.js @@ -44,7 +44,7 @@ define( // Increase the size of the buffer function doubleBufferSize() { - var sz = Math.min(maxSize, buffer.length * 2), + var sz = Math.min(maxSize * 2, buffer.length * 2), canDouble = sz > buffer.length, doubled = canDouble && new Float32Array(sz); @@ -58,7 +58,7 @@ define( // Decrease the size of the buffer function halveBufferSize() { - var sz = Math.max(initialSize, buffer.length / 2), + var sz = Math.max(initialSize * 2, buffer.length / 2), canHalve = sz < buffer.length; if (canHalve) { @@ -77,6 +77,13 @@ define( getBuffer: function () { return buffer; }, + /** + * Get the number of points stored in this buffer. + * @returns {number} the number of points stored + */ + getLength: function () { + return length; + }, /** * Remove values from this buffer. * Normally, values are removed from the start @@ -116,9 +123,7 @@ define( i; // Don't allow append after the end; that doesn't make sense - if (index > length) { - index = length; - } + index = Math.min(index, length); // Resize if necessary if (sz > free) { diff --git a/platform/features/plot/test/elements/PlotLineBufferSpec.js b/platform/features/plot/test/elements/PlotLineBufferSpec.js index 5d6b294da8..3fc7b06c6e 100644 --- a/platform/features/plot/test/elements/PlotLineBufferSpec.js +++ b/platform/features/plot/test/elements/PlotLineBufferSpec.js @@ -52,6 +52,7 @@ define( expect( Array.prototype.slice.call(buffer.getBuffer()).slice(0, 12) ).toEqual([ -41, 8, -39, 0, -35, 3, -33, 9, -28, 8, -27, 11]); + expect(buffer.getLength()).toEqual(6); }); it("finds insertion indexes", function () { @@ -73,6 +74,51 @@ define( expect( Array.prototype.slice.call(buffer.getBuffer()).slice(0, 24) ).toEqual(head.concat(head).concat(tail).concat(tail)); + expect(buffer.getLength()).toEqual(12); + }); + + it("allows values to be trimmed from the start", function () { + buffer.trim(2); + expect(buffer.getLength()).toEqual(4); + expect( + Array.prototype.slice.call(buffer.getBuffer()).slice(0, 8) + ).toEqual([ -35, 3, -33, 9, -28, 8, -27, 11]); + }); + + it("ensures a maximum size", function () { + var i; + + // Should be able to insert 6 series of 6 points each + // (After that, we'll hit the test max of 40) + for (i = 1; i < 6; i += 1) { + expect(buffer.getLength()).toEqual(6 * i); + expect(buffer.insert(mockSeries, Number.POSITIVE_INFINITY)) + .toBeTruthy(); + } + + // Should be maxed out now + expect(buffer.getLength()).toEqual(36); + expect(buffer.insert(mockSeries, Number.POSITIVE_INFINITY)) + .toBeFalsy(); + expect(buffer.getLength()).toEqual(36); + + }); + + it("reduces buffer size when space is no longer needed", function () { + // Check that actual buffer is sized to the initial size + // (double TEST_INITIAL_SIZE, since two elements are needed per + // point; one for domain, one for range) + expect(buffer.getBuffer().length).toEqual(20); + // Should have 6 elements now... grow to 24 + buffer.insert(mockSeries, Number.POSITIVE_INFINITY); + buffer.insert(mockSeries, Number.POSITIVE_INFINITY); + buffer.insert(mockSeries, Number.POSITIVE_INFINITY); + // This should have doubled the actual buffer size + expect(buffer.getBuffer().length).toEqual(80); + // Remove some values + buffer.trim(20); + // Actual buffer size should have been reduced accordingly + expect(buffer.getBuffer().length).toBeLessThan(80); }); });