2019-02-13 19:09:26 -05:00
|
|
|
package chart
|
2016-07-14 12:27:23 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2018-04-05 03:42:38 -04:00
|
|
|
"github.com/blend/go-sdk/assert"
|
2016-07-14 12:27:23 -04:00
|
|
|
)
|
|
|
|
|
2019-02-13 19:09:26 -05:00
|
|
|
func TestRingBuffer(t *testing.T) {
|
2016-07-14 12:27:23 -04:00
|
|
|
assert := assert.New(t)
|
|
|
|
|
2019-02-13 19:09:26 -05:00
|
|
|
buffer := NewRingBuffer()
|
2016-07-14 12:27:23 -04:00
|
|
|
|
|
|
|
buffer.Enqueue(1)
|
|
|
|
assert.Equal(1, buffer.Len())
|
|
|
|
assert.Equal(1, buffer.Peek())
|
|
|
|
assert.Equal(1, buffer.PeekBack())
|
|
|
|
|
|
|
|
buffer.Enqueue(2)
|
|
|
|
assert.Equal(2, buffer.Len())
|
|
|
|
assert.Equal(1, buffer.Peek())
|
|
|
|
assert.Equal(2, buffer.PeekBack())
|
|
|
|
|
|
|
|
buffer.Enqueue(3)
|
|
|
|
assert.Equal(3, buffer.Len())
|
|
|
|
assert.Equal(1, buffer.Peek())
|
|
|
|
assert.Equal(3, buffer.PeekBack())
|
|
|
|
|
|
|
|
buffer.Enqueue(4)
|
|
|
|
assert.Equal(4, buffer.Len())
|
|
|
|
assert.Equal(1, buffer.Peek())
|
|
|
|
assert.Equal(4, buffer.PeekBack())
|
|
|
|
|
|
|
|
buffer.Enqueue(5)
|
|
|
|
assert.Equal(5, buffer.Len())
|
|
|
|
assert.Equal(1, buffer.Peek())
|
|
|
|
assert.Equal(5, buffer.PeekBack())
|
|
|
|
|
|
|
|
buffer.Enqueue(6)
|
|
|
|
assert.Equal(6, buffer.Len())
|
|
|
|
assert.Equal(1, buffer.Peek())
|
|
|
|
assert.Equal(6, buffer.PeekBack())
|
|
|
|
|
|
|
|
buffer.Enqueue(7)
|
|
|
|
assert.Equal(7, buffer.Len())
|
|
|
|
assert.Equal(1, buffer.Peek())
|
|
|
|
assert.Equal(7, buffer.PeekBack())
|
|
|
|
|
|
|
|
buffer.Enqueue(8)
|
|
|
|
assert.Equal(8, buffer.Len())
|
|
|
|
assert.Equal(1, buffer.Peek())
|
|
|
|
assert.Equal(8, buffer.PeekBack())
|
|
|
|
|
|
|
|
value := buffer.Dequeue()
|
|
|
|
assert.Equal(1, value)
|
|
|
|
assert.Equal(7, buffer.Len())
|
|
|
|
assert.Equal(2, buffer.Peek())
|
|
|
|
assert.Equal(8, buffer.PeekBack())
|
|
|
|
|
|
|
|
value = buffer.Dequeue()
|
|
|
|
assert.Equal(2, value)
|
|
|
|
assert.Equal(6, buffer.Len())
|
|
|
|
assert.Equal(3, buffer.Peek())
|
|
|
|
assert.Equal(8, buffer.PeekBack())
|
|
|
|
|
|
|
|
value = buffer.Dequeue()
|
|
|
|
assert.Equal(3, value)
|
|
|
|
assert.Equal(5, buffer.Len())
|
|
|
|
assert.Equal(4, buffer.Peek())
|
|
|
|
assert.Equal(8, buffer.PeekBack())
|
|
|
|
|
|
|
|
value = buffer.Dequeue()
|
|
|
|
assert.Equal(4, value)
|
|
|
|
assert.Equal(4, buffer.Len())
|
|
|
|
assert.Equal(5, buffer.Peek())
|
|
|
|
assert.Equal(8, buffer.PeekBack())
|
|
|
|
|
|
|
|
value = buffer.Dequeue()
|
|
|
|
assert.Equal(5, value)
|
|
|
|
assert.Equal(3, buffer.Len())
|
|
|
|
assert.Equal(6, buffer.Peek())
|
|
|
|
assert.Equal(8, buffer.PeekBack())
|
|
|
|
|
|
|
|
value = buffer.Dequeue()
|
|
|
|
assert.Equal(6, value)
|
|
|
|
assert.Equal(2, buffer.Len())
|
|
|
|
assert.Equal(7, buffer.Peek())
|
|
|
|
assert.Equal(8, buffer.PeekBack())
|
|
|
|
|
|
|
|
value = buffer.Dequeue()
|
|
|
|
assert.Equal(7, value)
|
|
|
|
assert.Equal(1, buffer.Len())
|
|
|
|
assert.Equal(8, buffer.Peek())
|
|
|
|
assert.Equal(8, buffer.PeekBack())
|
|
|
|
|
|
|
|
value = buffer.Dequeue()
|
|
|
|
assert.Equal(8, value)
|
|
|
|
assert.Equal(0, buffer.Len())
|
2019-02-13 19:09:26 -05:00
|
|
|
assert.Nil(buffer.Peek())
|
|
|
|
assert.Nil(buffer.PeekBack())
|
2016-07-14 12:27:23 -04:00
|
|
|
}
|
|
|
|
|
2019-02-13 19:09:26 -05:00
|
|
|
func TestRingBufferClear(t *testing.T) {
|
2016-07-14 12:27:23 -04:00
|
|
|
assert := assert.New(t)
|
|
|
|
|
2019-02-13 19:09:26 -05:00
|
|
|
buffer := NewRingBuffer()
|
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)
|
|
|
|
|
|
|
|
assert.Equal(8, buffer.Len())
|
|
|
|
|
|
|
|
buffer.Clear()
|
|
|
|
assert.Equal(0, buffer.Len())
|
2019-02-13 19:09:26 -05:00
|
|
|
assert.Nil(buffer.Peek())
|
|
|
|
assert.Nil(buffer.PeekBack())
|
2016-07-14 12:27:23 -04:00
|
|
|
}
|
|
|
|
|
2019-02-13 19:09:26 -05:00
|
|
|
func TestRingBufferContents(t *testing.T) {
|
2016-07-14 12:27:23 -04:00
|
|
|
assert := assert.New(t)
|
|
|
|
|
2019-02-13 19:09:26 -05:00
|
|
|
buffer := NewRingBuffer()
|
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 19:09:26 -05:00
|
|
|
contents := buffer.Contents()
|
2018-09-05 11:45:19 -04:00
|
|
|
assert.Len(contents, 5)
|
2016-07-14 12:27:23 -04:00
|
|
|
assert.Equal(1, contents[0])
|
|
|
|
assert.Equal(2, contents[1])
|
|
|
|
assert.Equal(3, contents[2])
|
|
|
|
assert.Equal(4, contents[3])
|
|
|
|
assert.Equal(5, contents[4])
|
|
|
|
}
|
|
|
|
|
2019-02-13 19:09:26 -05:00
|
|
|
func TestRingBufferDrain(t *testing.T) {
|
2016-07-14 12:27:23 -04:00
|
|
|
assert := assert.New(t)
|
|
|
|
|
2019-02-13 19:09:26 -05:00
|
|
|
buffer := NewRingBuffer()
|
|
|
|
buffer.Enqueue(1)
|
|
|
|
buffer.Enqueue(2)
|
|
|
|
buffer.Enqueue(3)
|
|
|
|
buffer.Enqueue(4)
|
|
|
|
buffer.Enqueue(5)
|
|
|
|
|
|
|
|
contents := buffer.Drain()
|
|
|
|
assert.Len(contents, 5)
|
|
|
|
assert.Equal(1, contents[0])
|
|
|
|
assert.Equal(2, contents[1])
|
|
|
|
assert.Equal(3, contents[2])
|
|
|
|
assert.Equal(4, contents[3])
|
|
|
|
assert.Equal(5, contents[4])
|
|
|
|
|
|
|
|
assert.Equal(0, buffer.Len())
|
|
|
|
assert.Nil(buffer.Peek())
|
|
|
|
assert.Nil(buffer.PeekBack())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRingBufferEach(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
buffer := NewRingBuffer()
|
2016-07-14 12:27:23 -04:00
|
|
|
|
|
|
|
for x := 1; x < 17; x++ {
|
2019-02-13 19:09:26 -05:00
|
|
|
buffer.Enqueue(x)
|
2016-07-14 12:27:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
called := 0
|
2019-02-13 19:09:26 -05:00
|
|
|
buffer.Each(func(v interface{}) {
|
|
|
|
if typed, isTyped := v.(int); isTyped {
|
|
|
|
if typed == (called + 1) {
|
|
|
|
called++
|
|
|
|
}
|
2016-07-14 12:27:23 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
assert.Equal(16, called)
|
|
|
|
}
|
2017-05-12 20:12:23 -04:00
|
|
|
|
2019-02-13 19:09:26 -05:00
|
|
|
func TestRingBufferEachUntil(t *testing.T) {
|
2017-05-12 20:12:23 -04:00
|
|
|
assert := assert.New(t)
|
|
|
|
|
2019-02-13 19:09:26 -05:00
|
|
|
buffer := NewRingBuffer()
|
|
|
|
|
|
|
|
for x := 1; x < 17; x++ {
|
|
|
|
buffer.Enqueue(x)
|
|
|
|
}
|
|
|
|
|
|
|
|
called := 0
|
|
|
|
buffer.EachUntil(func(v interface{}) bool {
|
|
|
|
if typed, isTyped := v.(int); isTyped {
|
|
|
|
if typed > 10 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if typed == (called + 1) {
|
|
|
|
called++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
|
|
|
|
assert.Equal(10, called)
|
2017-05-12 20:12:23 -04:00
|
|
|
}
|
|
|
|
|
2019-02-13 19:09:26 -05:00
|
|
|
func TestRingBufferReverseEachUntil(t *testing.T) {
|
2017-05-12 20:12:23 -04:00
|
|
|
assert := assert.New(t)
|
|
|
|
|
2019-02-13 19:09:26 -05:00
|
|
|
buffer := NewRingBufferWithCapacity(32)
|
|
|
|
|
|
|
|
for x := 1; x < 17; x++ {
|
|
|
|
buffer.Enqueue(x)
|
|
|
|
}
|
|
|
|
|
|
|
|
var values []int
|
|
|
|
buffer.ReverseEachUntil(func(v interface{}) bool {
|
|
|
|
if typed, isTyped := v.(int); isTyped {
|
|
|
|
if typed < 10 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
values = append(values, typed)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
panic("value is not an integer")
|
|
|
|
})
|
|
|
|
|
|
|
|
assert.Len(values, 7)
|
|
|
|
assert.Equal(16, values[0])
|
|
|
|
assert.Equal(10, values[6])
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRingBufferReverseEachUntilUndersized(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
buffer := NewRingBuffer()
|
|
|
|
|
|
|
|
for x := 1; x < 17; x++ {
|
|
|
|
buffer.Enqueue(x)
|
|
|
|
}
|
|
|
|
|
|
|
|
var values []int
|
|
|
|
buffer.ReverseEachUntil(func(v interface{}) bool {
|
|
|
|
if typed, isTyped := v.(int); isTyped {
|
|
|
|
if typed < 10 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
values = append(values, typed)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
panic("value is not an integer")
|
|
|
|
})
|
|
|
|
|
|
|
|
assert.Len(values, 7)
|
|
|
|
assert.Equal(16, values[0])
|
|
|
|
assert.Equal(10, values[6])
|
2017-05-12 20:12:23 -04:00
|
|
|
}
|
|
|
|
|
2019-02-13 19:09:26 -05:00
|
|
|
func TestRingBufferConsume(t *testing.T) {
|
2017-05-12 20:12:23 -04:00
|
|
|
assert := assert.New(t)
|
|
|
|
|
2019-02-13 19:09:26 -05:00
|
|
|
buffer := NewRingBuffer()
|
|
|
|
|
|
|
|
for x := 1; x < 17; x++ {
|
|
|
|
buffer.Enqueue(x)
|
2017-05-12 20:12:23 -04:00
|
|
|
}
|
|
|
|
|
2019-02-13 19:09:26 -05:00
|
|
|
assert.Equal(16, buffer.Len())
|
|
|
|
|
|
|
|
var called int
|
|
|
|
buffer.Consume(func(v interface{}) {
|
|
|
|
if _, isTyped := v.(int); isTyped {
|
|
|
|
called++
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
assert.Equal(16, called)
|
|
|
|
assert.Zero(buffer.Len())
|
2017-05-12 20:12:23 -04:00
|
|
|
}
|