go-chart/value_buffer_test.go

179 lines
4.7 KiB
Go
Raw Normal View History

2019-02-13 19:09:26 -05:00
package chart
2016-07-14 12:27:23 -04:00
import (
"testing"
"github.com/wcharczuk/go-chart/v2/testutil"
2016-07-14 12:27:23 -04:00
)
2022-01-10 18:52:32 -05:00
func Test_ValueBuffer(t *testing.T) {
buffer := NewValueBuffer[int]()
2016-07-14 12:27:23 -04:00
buffer.Enqueue(1)
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)
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)
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)
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)
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)
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)
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)
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()
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()
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()
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()
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()
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()
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()
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()
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
}
2022-01-10 18:52:32 -05:00
func Test_ValueBufferClear(t *testing.T) {
buffer := NewValueBuffer[int]()
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)
testutil.AssertEqual(t, 8, buffer.Len())
2016-07-14 12:27:23 -04:00
buffer.Clear()
testutil.AssertEqual(t, 0, buffer.Len())
testutil.AssertZero(t, buffer.Peek())
testutil.AssertZero(t, buffer.PeekBack())
2016-07-14 12:27:23 -04:00
}
2022-01-10 18:52:32 -05:00
func Test_ValueBuffer_Array(t *testing.T) {
buffer := NewValueBuffer[int]()
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()
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
}
2022-01-10 18:52:32 -05:00
func Test_ValueBuffer_Each(t *testing.T) {
buffer := NewValueBuffer[float64]()
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
}
})
testutil.AssertEqual(t, 16, called)
2016-07-14 12:27:23 -04:00
}
2022-01-10 18:52:32 -05:00
func Test_NewValueBuffer(t *testing.T) {
empty := NewValueBuffer[int]()
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())
}
2022-01-10 18:52:32 -05:00
func Test_NewBuffer_withValues(t *testing.T) {
2019-02-13 21:55:13 -05:00
values := NewValueBuffer(1, 2, 3, 4, 5)
testutil.AssertNotNil(t, values)
testutil.AssertEqual(t, 5, values.Len())
testutil.AssertEqual(t, 1, values.Peek())
testutil.AssertEqual(t, 5, values.PeekBack())
}
2022-01-10 18:52:32 -05:00
func Test_ValueBuffer_growth(t *testing.T) {
values := NewValueBuffer[float64](1, 2, 3, 4, 5)
2019-02-13 21:55:13 -05:00
for i := 0; i < 1<<10; i++ {
values.Enqueue(float64(i))
}
testutil.AssertEqual(t, 1<<10-1, values.PeekBack())
}