go-chart/value_buffer_test.go

193 lines
4.9 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
)
2019-02-13 21:55:13 -05:00
func TestBuffer(t *testing.T) {
// 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)
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
}
2019-02-13 21:55:13 -05:00
func TestBufferClear(t *testing.T) {
// 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)
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
}
2019-02-13 21:55:13 -05:00
func TestBufferArray(t *testing.T) {
// 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()
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) {
// 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
}
})
testutil.AssertEqual(t, 16, called)
2016-07-14 12:27:23 -04:00
}
2019-02-13 21:55:13 -05:00
func TestNewBuffer(t *testing.T) {
// replaced new assertions helper
2019-02-13 21:55:13 -05:00
empty := NewValueBuffer()
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())
}
2019-02-13 21:55:13 -05:00
func TestNewBufferWithValues(t *testing.T) {
// replaced new assertions helper
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())
}
2019-02-13 21:55:13 -05:00
func TestBufferGrowth(t *testing.T) {
// replaced new assertions helper
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))
}
testutil.AssertEqual(t, 1<<10-1, values.PeekBack())
}