2019-02-13 19:09:26 -05:00
|
|
|
package chart
|
2016-07-14 12:27:23 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2020-11-22 19:45:10 -05:00
|
|
|
"github.com/wcharczuk/go-chart/v2/testutil"
|
2016-07-14 12:27:23 -04:00
|
|
|
)
|
|
|
|
|
2019-02-13 21:55:13 -05:00
|
|
|
func TestBuffer(t *testing.T) {
|
2020-11-22 19:45:10 -05:00
|
|
|
// replaced new assertions helper
|
2016-07-14 12:27:23 -04:00
|
|
|
|
2019-02-13 21:55:13 -05:00
|
|
|
buffer := NewValueBuffer()
|
2016-07-14 12:27:23 -04:00
|
|
|
|
|
|
|
buffer.Enqueue(1)
|
2020-11-22 19:45:10 -05:00
|
|
|
testutil.AssertEqual(t, 1, buffer.Len())
|
|
|
|
testutil.AssertEqual(t, 1, buffer.Peek())
|
|
|
|
testutil.AssertEqual(t, 1, buffer.PeekBack())
|
2016-07-14 12:27:23 -04:00
|
|
|
|
|
|
|
buffer.Enqueue(2)
|
2020-11-22 19:45:10 -05:00
|
|
|
testutil.AssertEqual(t, 2, buffer.Len())
|
|
|
|
testutil.AssertEqual(t, 1, buffer.Peek())
|
|
|
|
testutil.AssertEqual(t, 2, buffer.PeekBack())
|
2016-07-14 12:27:23 -04:00
|
|
|
|
|
|
|
buffer.Enqueue(3)
|
2020-11-22 19:45:10 -05:00
|
|
|
testutil.AssertEqual(t, 3, buffer.Len())
|
|
|
|
testutil.AssertEqual(t, 1, buffer.Peek())
|
|
|
|
testutil.AssertEqual(t, 3, buffer.PeekBack())
|
2016-07-14 12:27:23 -04:00
|
|
|
|
|
|
|
buffer.Enqueue(4)
|
2020-11-22 19:45:10 -05:00
|
|
|
testutil.AssertEqual(t, 4, buffer.Len())
|
|
|
|
testutil.AssertEqual(t, 1, buffer.Peek())
|
|
|
|
testutil.AssertEqual(t, 4, buffer.PeekBack())
|
2016-07-14 12:27:23 -04:00
|
|
|
|
|
|
|
buffer.Enqueue(5)
|
2020-11-22 19:45:10 -05:00
|
|
|
testutil.AssertEqual(t, 5, buffer.Len())
|
|
|
|
testutil.AssertEqual(t, 1, buffer.Peek())
|
|
|
|
testutil.AssertEqual(t, 5, buffer.PeekBack())
|
2016-07-14 12:27:23 -04:00
|
|
|
|
|
|
|
buffer.Enqueue(6)
|
2020-11-22 19:45:10 -05:00
|
|
|
testutil.AssertEqual(t, 6, buffer.Len())
|
|
|
|
testutil.AssertEqual(t, 1, buffer.Peek())
|
|
|
|
testutil.AssertEqual(t, 6, buffer.PeekBack())
|
2016-07-14 12:27:23 -04:00
|
|
|
|
|
|
|
buffer.Enqueue(7)
|
2020-11-22 19:45:10 -05:00
|
|
|
testutil.AssertEqual(t, 7, buffer.Len())
|
|
|
|
testutil.AssertEqual(t, 1, buffer.Peek())
|
|
|
|
testutil.AssertEqual(t, 7, buffer.PeekBack())
|
2016-07-14 12:27:23 -04:00
|
|
|
|
|
|
|
buffer.Enqueue(8)
|
2020-11-22 19:45:10 -05:00
|
|
|
testutil.AssertEqual(t, 8, buffer.Len())
|
|
|
|
testutil.AssertEqual(t, 1, buffer.Peek())
|
|
|
|
testutil.AssertEqual(t, 8, buffer.PeekBack())
|
2016-07-14 12:27:23 -04:00
|
|
|
|
|
|
|
value := buffer.Dequeue()
|
2020-11-22 19:45:10 -05:00
|
|
|
testutil.AssertEqual(t, 1, value)
|
|
|
|
testutil.AssertEqual(t, 7, buffer.Len())
|
|
|
|
testutil.AssertEqual(t, 2, buffer.Peek())
|
|
|
|
testutil.AssertEqual(t, 8, buffer.PeekBack())
|
2016-07-14 12:27:23 -04:00
|
|
|
|
|
|
|
value = buffer.Dequeue()
|
2020-11-22 19:45:10 -05:00
|
|
|
testutil.AssertEqual(t, 2, value)
|
|
|
|
testutil.AssertEqual(t, 6, buffer.Len())
|
|
|
|
testutil.AssertEqual(t, 3, buffer.Peek())
|
|
|
|
testutil.AssertEqual(t, 8, buffer.PeekBack())
|
2016-07-14 12:27:23 -04:00
|
|
|
|
|
|
|
value = buffer.Dequeue()
|
2020-11-22 19:45:10 -05:00
|
|
|
testutil.AssertEqual(t, 3, value)
|
|
|
|
testutil.AssertEqual(t, 5, buffer.Len())
|
|
|
|
testutil.AssertEqual(t, 4, buffer.Peek())
|
|
|
|
testutil.AssertEqual(t, 8, buffer.PeekBack())
|
2016-07-14 12:27:23 -04:00
|
|
|
|
|
|
|
value = buffer.Dequeue()
|
2020-11-22 19:45:10 -05:00
|
|
|
testutil.AssertEqual(t, 4, value)
|
|
|
|
testutil.AssertEqual(t, 4, buffer.Len())
|
|
|
|
testutil.AssertEqual(t, 5, buffer.Peek())
|
|
|
|
testutil.AssertEqual(t, 8, buffer.PeekBack())
|
2016-07-14 12:27:23 -04:00
|
|
|
|
|
|
|
value = buffer.Dequeue()
|
2020-11-22 19:45:10 -05:00
|
|
|
testutil.AssertEqual(t, 5, value)
|
|
|
|
testutil.AssertEqual(t, 3, buffer.Len())
|
|
|
|
testutil.AssertEqual(t, 6, buffer.Peek())
|
|
|
|
testutil.AssertEqual(t, 8, buffer.PeekBack())
|
2016-07-14 12:27:23 -04:00
|
|
|
|
|
|
|
value = buffer.Dequeue()
|
2020-11-22 19:45:10 -05:00
|
|
|
testutil.AssertEqual(t, 6, value)
|
|
|
|
testutil.AssertEqual(t, 2, buffer.Len())
|
|
|
|
testutil.AssertEqual(t, 7, buffer.Peek())
|
|
|
|
testutil.AssertEqual(t, 8, buffer.PeekBack())
|
2016-07-14 12:27:23 -04:00
|
|
|
|
|
|
|
value = buffer.Dequeue()
|
2020-11-22 19:45:10 -05:00
|
|
|
testutil.AssertEqual(t, 7, value)
|
|
|
|
testutil.AssertEqual(t, 1, buffer.Len())
|
|
|
|
testutil.AssertEqual(t, 8, buffer.Peek())
|
|
|
|
testutil.AssertEqual(t, 8, buffer.PeekBack())
|
2016-07-14 12:27:23 -04:00
|
|
|
|
|
|
|
value = buffer.Dequeue()
|
2020-11-22 19:45:10 -05:00
|
|
|
testutil.AssertEqual(t, 8, value)
|
|
|
|
testutil.AssertEqual(t, 0, buffer.Len())
|
|
|
|
testutil.AssertZero(t, buffer.Peek())
|
|
|
|
testutil.AssertZero(t, buffer.PeekBack())
|
2016-07-14 12:27:23 -04:00
|
|
|
}
|
|
|
|
|
2019-02-13 21:55:13 -05:00
|
|
|
func TestBufferClear(t *testing.T) {
|
2020-11-22 19:45:10 -05:00
|
|
|
// replaced new assertions helper
|
2016-07-14 12:27:23 -04:00
|
|
|
|
2019-02-13 21:55:13 -05:00
|
|
|
buffer := NewValueBuffer()
|
2016-07-14 12:27:23 -04:00
|
|
|
buffer.Enqueue(1)
|
|
|
|
buffer.Enqueue(1)
|
|
|
|
buffer.Enqueue(1)
|
|
|
|
buffer.Enqueue(1)
|
|
|
|
buffer.Enqueue(1)
|
|
|
|
buffer.Enqueue(1)
|
|
|
|
buffer.Enqueue(1)
|
|
|
|
buffer.Enqueue(1)
|
|
|
|
|
2020-11-22 19:45:10 -05:00
|
|
|
testutil.AssertEqual(t, 8, buffer.Len())
|
2016-07-14 12:27:23 -04:00
|
|
|
|
|
|
|
buffer.Clear()
|
2020-11-22 19:45:10 -05:00
|
|
|
testutil.AssertEqual(t, 0, buffer.Len())
|
|
|
|
testutil.AssertZero(t, buffer.Peek())
|
|
|
|
testutil.AssertZero(t, buffer.PeekBack())
|
2016-07-14 12:27:23 -04:00
|
|
|
}
|
|
|
|
|
2019-02-13 21:55:13 -05:00
|
|
|
func TestBufferArray(t *testing.T) {
|
2020-11-22 19:45:10 -05:00
|
|
|
// replaced new assertions helper
|
2016-07-14 12:27:23 -04:00
|
|
|
|
2019-02-13 21:55:13 -05:00
|
|
|
buffer := NewValueBuffer()
|
2016-07-14 12:27:23 -04:00
|
|
|
buffer.Enqueue(1)
|
|
|
|
buffer.Enqueue(2)
|
|
|
|
buffer.Enqueue(3)
|
|
|
|
buffer.Enqueue(4)
|
|
|
|
buffer.Enqueue(5)
|
|
|
|
|
2019-02-13 21:55:13 -05:00
|
|
|
contents := buffer.Array()
|
2020-11-22 19:45:10 -05:00
|
|
|
testutil.AssertLen(t, contents, 5)
|
|
|
|
testutil.AssertEqual(t, 1, contents[0])
|
|
|
|
testutil.AssertEqual(t, 2, contents[1])
|
|
|
|
testutil.AssertEqual(t, 3, contents[2])
|
|
|
|
testutil.AssertEqual(t, 4, contents[3])
|
|
|
|
testutil.AssertEqual(t, 5, contents[4])
|
2016-07-14 12:27:23 -04:00
|
|
|
}
|
|
|
|
|
2019-02-13 21:55:13 -05:00
|
|
|
func TestBufferEach(t *testing.T) {
|
2020-11-22 19:45:10 -05:00
|
|
|
// replaced new assertions helper
|
2016-07-14 12:27:23 -04:00
|
|
|
|
2019-02-13 21:55:13 -05:00
|
|
|
buffer := NewValueBuffer()
|
2016-07-14 12:27:23 -04:00
|
|
|
|
|
|
|
for x := 1; x < 17; x++ {
|
2019-02-13 21:55:13 -05:00
|
|
|
buffer.Enqueue(float64(x))
|
2016-07-14 12:27:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
called := 0
|
2019-02-13 21:55:13 -05:00
|
|
|
buffer.Each(func(_ int, v float64) {
|
|
|
|
if v == float64(called+1) {
|
|
|
|
called++
|
2016-07-14 12:27:23 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2020-11-22 19:45:10 -05:00
|
|
|
testutil.AssertEqual(t, 16, called)
|
2016-07-14 12:27:23 -04:00
|
|
|
}
|
2017-05-12 20:12:23 -04:00
|
|
|
|
2019-02-13 21:55:13 -05:00
|
|
|
func TestNewBuffer(t *testing.T) {
|
2020-11-22 19:45:10 -05:00
|
|
|
// replaced new assertions helper
|
2017-05-12 20:12:23 -04:00
|
|
|
|
2019-02-13 21:55:13 -05:00
|
|
|
empty := NewValueBuffer()
|
2020-11-22 19:45:10 -05:00
|
|
|
testutil.AssertNotNil(t, empty)
|
|
|
|
testutil.AssertZero(t, empty.Len())
|
|
|
|
testutil.AssertEqual(t, bufferDefaultCapacity, empty.Capacity())
|
|
|
|
testutil.AssertZero(t, empty.Peek())
|
|
|
|
testutil.AssertZero(t, empty.PeekBack())
|
2017-05-12 20:12:23 -04:00
|
|
|
}
|
|
|
|
|
2019-02-13 21:55:13 -05:00
|
|
|
func TestNewBufferWithValues(t *testing.T) {
|
2020-11-22 19:45:10 -05:00
|
|
|
// replaced new assertions helper
|
2017-05-12 20:12:23 -04:00
|
|
|
|
2019-02-13 21:55:13 -05:00
|
|
|
values := NewValueBuffer(1, 2, 3, 4, 5)
|
2020-11-22 19:45:10 -05:00
|
|
|
testutil.AssertNotNil(t, values)
|
|
|
|
testutil.AssertEqual(t, 5, values.Len())
|
|
|
|
testutil.AssertEqual(t, 1, values.Peek())
|
|
|
|
testutil.AssertEqual(t, 5, values.PeekBack())
|
2017-05-12 20:12:23 -04:00
|
|
|
}
|
|
|
|
|
2019-02-13 21:55:13 -05:00
|
|
|
func TestBufferGrowth(t *testing.T) {
|
2020-11-22 19:45:10 -05:00
|
|
|
// replaced new assertions helper
|
2017-05-12 20:12:23 -04:00
|
|
|
|
2019-02-13 21:55:13 -05:00
|
|
|
values := NewValueBuffer(1, 2, 3, 4, 5)
|
|
|
|
for i := 0; i < 1<<10; i++ {
|
|
|
|
values.Enqueue(float64(i))
|
2017-05-12 20:12:23 -04:00
|
|
|
}
|
|
|
|
|
2020-11-22 19:45:10 -05:00
|
|
|
testutil.AssertEqual(t, 1<<10-1, values.PeekBack())
|
2017-05-12 20:12:23 -04:00
|
|
|
}
|