2017-05-12 17:17:43 -04:00
|
|
|
package seq
|
2016-07-14 12:27:23 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/blendlabs/go-assert"
|
|
|
|
)
|
|
|
|
|
2017-04-30 00:12:39 -04:00
|
|
|
func TestBuffer(t *testing.T) {
|
2016-07-14 12:27:23 -04:00
|
|
|
assert := assert.New(t)
|
|
|
|
|
2017-04-30 00:12:39 -04:00
|
|
|
buffer := NewBuffer()
|
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())
|
2017-04-28 19:07:36 -04:00
|
|
|
assert.Zero(buffer.Peek())
|
|
|
|
assert.Zero(buffer.PeekBack())
|
2016-07-14 12:27:23 -04:00
|
|
|
}
|
|
|
|
|
2017-04-30 00:12:39 -04:00
|
|
|
func TestBufferClear(t *testing.T) {
|
2016-07-14 12:27:23 -04:00
|
|
|
assert := assert.New(t)
|
|
|
|
|
2017-04-30 00:12:39 -04:00
|
|
|
buffer := NewBuffer()
|
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())
|
2017-04-28 19:07:36 -04:00
|
|
|
assert.Zero(buffer.Peek())
|
|
|
|
assert.Zero(buffer.PeekBack())
|
2016-07-14 12:27:23 -04:00
|
|
|
}
|
|
|
|
|
2017-04-30 00:12:39 -04:00
|
|
|
func TestBufferArray(t *testing.T) {
|
2016-07-14 12:27:23 -04:00
|
|
|
assert := assert.New(t)
|
|
|
|
|
2017-04-30 00:12:39 -04:00
|
|
|
buffer := NewBuffer()
|
2016-07-14 12:27:23 -04:00
|
|
|
buffer.Enqueue(1)
|
|
|
|
buffer.Enqueue(2)
|
|
|
|
buffer.Enqueue(3)
|
|
|
|
buffer.Enqueue(4)
|
|
|
|
buffer.Enqueue(5)
|
|
|
|
|
2017-04-28 19:07:36 -04:00
|
|
|
contents := buffer.Array()
|
2016-07-14 12:27:23 -04:00
|
|
|
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])
|
|
|
|
}
|
|
|
|
|
2017-04-30 00:12:39 -04:00
|
|
|
func TestBufferEach(t *testing.T) {
|
2016-07-14 12:27:23 -04:00
|
|
|
assert := assert.New(t)
|
|
|
|
|
2017-04-30 00:12:39 -04:00
|
|
|
buffer := NewBuffer()
|
2016-07-14 12:27:23 -04:00
|
|
|
|
|
|
|
for x := 1; x < 17; x++ {
|
2017-04-28 19:07:36 -04:00
|
|
|
buffer.Enqueue(float64(x))
|
2016-07-14 12:27:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
called := 0
|
2017-04-28 19:07:36 -04:00
|
|
|
buffer.Each(func(_ int, v float64) {
|
|
|
|
if v == float64(called+1) {
|
|
|
|
called++
|
2016-07-14 12:27:23 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
assert.Equal(16, called)
|
|
|
|
}
|
2017-04-28 19:07:36 -04:00
|
|
|
|
2017-04-30 00:12:39 -04:00
|
|
|
func TestNewBuffer(t *testing.T) {
|
2017-04-28 19:07:36 -04:00
|
|
|
assert := assert.New(t)
|
|
|
|
|
2017-04-30 00:12:39 -04:00
|
|
|
empty := NewBuffer()
|
2017-04-28 19:07:36 -04:00
|
|
|
assert.NotNil(empty)
|
|
|
|
assert.Zero(empty.Len())
|
2017-04-30 01:44:17 -04:00
|
|
|
assert.Equal(bufferDefaultCapacity, empty.Capacity())
|
2017-04-28 19:07:36 -04:00
|
|
|
assert.Zero(empty.Peek())
|
|
|
|
assert.Zero(empty.PeekBack())
|
|
|
|
}
|
|
|
|
|
2017-04-30 00:12:39 -04:00
|
|
|
func TestNewBufferWithValues(t *testing.T) {
|
2017-04-28 19:07:36 -04:00
|
|
|
assert := assert.New(t)
|
|
|
|
|
2017-04-30 00:12:39 -04:00
|
|
|
values := NewBuffer(1, 2, 3, 4, 5)
|
2017-04-28 19:07:36 -04:00
|
|
|
assert.NotNil(values)
|
2017-04-30 00:12:39 -04:00
|
|
|
assert.Equal(5, values.Len())
|
2017-04-28 19:07:36 -04:00
|
|
|
assert.Equal(1, values.Peek())
|
2017-04-30 00:12:39 -04:00
|
|
|
assert.Equal(5, values.PeekBack())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBufferGrowth(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
values := NewBuffer(1, 2, 3, 4, 5)
|
|
|
|
for i := 0; i < 1<<10; i++ {
|
|
|
|
values.Enqueue(float64(i))
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(1<<10-1, values.PeekBack())
|
2017-04-28 19:07:36 -04:00
|
|
|
}
|