Corrected syntax error in RingBuffer and also improved testing file.

This commit is contained in:
Saxon Milton 2017-12-03 20:05:46 +10:30
parent 3a5a9f51fc
commit 7da19e1f89
2 changed files with 45 additions and 52 deletions

View File

@ -55,7 +55,7 @@ New returns the addres of a new ring buffer with the parameters specified.
It initialises fields and allocates the required dataMemory. It initialises fields and allocates the required dataMemory.
*/ */
func NewRingBuffer(bufferSize int, elementSize int) (rb *ringBuffer) { func NewRingBuffer(bufferSize int, elementSize int) (rb *ringBuffer) {
if size <= 0 || elementSize <= 0 { if bufferSize <= 0 || elementSize <= 0 {
return nil return nil
} }
rb = new(ringBuffer) rb = new(ringBuffer)
@ -63,14 +63,13 @@ func NewRingBuffer(bufferSize int, elementSize int) (rb *ringBuffer) {
for i := range rb.dataMemory { for i := range rb.dataMemory {
rb.dataMemory[i] = make([]byte, elementSize) rb.dataMemory[i] = make([]byte, elementSize)
} }
rb.sizeMemory = make([]int, size) rb.sizeMemory = make([]int, bufferSize)
rb.size = bufferSize rb.size = bufferSize
rb.noOfElements = 0 rb.noOfElements = 0
rb.first = -1 rb.first = -1
rb.last = -1 rb.last = -1
rb.currentlyWriting = false rb.currentlyWriting = false
rb.currentlyReading = false rb.currentlyReading = false
rb.sizeChannel = make(chan int, size)
rb.mutex = sync.Mutex{} rb.mutex = sync.Mutex{}
return return
} }

View File

@ -33,6 +33,14 @@ import (
"time" "time"
) )
const (
testBufferSize = 10
testElementSize = 1
testOverFlowSize = 5
testDataSize = 1
testConcurrencyBufferSize = 1000;
)
var rb *ringBuffer var rb *ringBuffer
// Call when we get an error we shouldn't have // Call when we get an error we shouldn't have
@ -48,11 +56,11 @@ func noErrorFail(t *testing.T) {
// Test that the Make method correctly allocates memory for arbitrary buffer // Test that the Make method correctly allocates memory for arbitrary buffer
// size of 10 and arbitrary element size of 10 // size of 10 and arbitrary element size of 10
func TestMake(t *testing.T) { func TestMake(t *testing.T) {
rb = NewRingBuffer(10, 10) rb = NewRingBuffer(testBufferSize, testElementSize)
if len(rb.dataMemory) != 10 { if len(rb.dataMemory) != testBufferSize {
t.Errorf("Len of buffer is wrong!") t.Errorf("Len of buffer is wrong!")
} }
if len(rb.dataMemory[0]) != 10 { if len(rb.dataMemory[0]) != testElementSize {
t.Errorf("Len of individual element is wrong!") t.Errorf("Len of individual element is wrong!")
} }
} }
@ -60,7 +68,7 @@ func TestMake(t *testing.T) {
// Test call to done writing when no writing has occured. // Test call to done writing when no writing has occured.
// We pass arbitrary clipSize. // We pass arbitrary clipSize.
func TestDoneWriting1(t *testing.T) { func TestDoneWriting1(t *testing.T) {
if rb.DoneWriting(1) == nil { if rb.DoneWriting(testDataSize) == nil {
noErrorFail(t) noErrorFail(t)
} }
} }
@ -70,28 +78,14 @@ func TestDoneWriting2(t *testing.T) {
if _, err := rb.Get(); err != nil { if _, err := rb.Get(); err != nil {
falseErrorFail(t, err) falseErrorFail(t, err)
} }
if err := rb.DoneWriting(1); err != nil { if err := rb.DoneWriting(testDataSize); err != nil {
falseErrorFail(t, err) falseErrorFail(t, err)
} }
} }
// Try to 'write' to the buffer when full. Set buffer size to arbitrary number of 3.
// We write over buffer size of 3 (write 4 times)
func TestWritingWhenFull(t *testing.T) {
rb = NewRingBuffer(3, 10)
var err error
for i := 0; i < 4; i++ {
_, err = rb.Get()
rb.DoneWriting(1)
}
if err == nil {
noErrorFail(t)
}
}
// Test call to done reading when there hasn't been any reading // Test call to done reading when there hasn't been any reading
func TestDoneReading1(t *testing.T) { func TestDoneReading1(t *testing.T) {
rb = NewRingBuffer(10, 10) rb = NewRingBuffer(testBufferSize, testElementSize)
if rb.DoneReading() == nil { if rb.DoneReading() == nil {
noErrorFail(t) noErrorFail(t)
} }
@ -99,7 +93,7 @@ func TestDoneReading1(t *testing.T) {
// Test read when there hasn't been anything written to buffer // Test read when there hasn't been anything written to buffer
func TestReadingWithoutWrite(t *testing.T) { func TestReadingWithoutWrite(t *testing.T) {
rb = NewRingBuffer(10, 10) rb = NewRingBuffer(testBufferSize, testElementSize)
_, err := rb.Read() _, err := rb.Read()
if err == nil { if err == nil {
noErrorFail(t) noErrorFail(t)
@ -108,7 +102,7 @@ func TestReadingWithoutWrite(t *testing.T) {
// Try Writing twice without calling DoneWriting // Try Writing twice without calling DoneWriting
func TestWritingTwice(t *testing.T) { func TestWritingTwice(t *testing.T) {
rb = NewRingBuffer(10, 10) rb = NewRingBuffer(testBufferSize, testElementSize)
_, err := rb.Get() _, err := rb.Get()
_, err = rb.Get() _, err = rb.Get()
if err == nil { if err == nil {
@ -118,9 +112,9 @@ func TestWritingTwice(t *testing.T) {
// Test call to done reading when there has been some reading // Test call to done reading when there has been some reading
func TestDoneReading2(t *testing.T) { func TestDoneReading2(t *testing.T) {
rb = NewRingBuffer(10, 10) rb = NewRingBuffer(testBufferSize, testElementSize)
_, err := rb.Get() _, err := rb.Get()
rb.DoneWriting(1) rb.DoneWriting(testDataSize)
_, err = rb.Read() _, err = rb.Read()
if err != nil { if err != nil {
falseErrorFail(t, err) falseErrorFail(t, err)
@ -130,11 +124,11 @@ func TestDoneReading2(t *testing.T) {
// Testing writing then reading for single 'write' and single 'read' routines // Testing writing then reading for single 'write' and single 'read' routines
func TestWritingAndReading1(t *testing.T) { func TestWritingAndReading1(t *testing.T) {
rb = NewRingBuffer(10, 10) rb = NewRingBuffer(testBufferSize, testElementSize)
if _, err := rb.Get(); err != nil { if _, err := rb.Get(); err != nil {
falseErrorFail(t, err) falseErrorFail(t, err)
} }
if err := rb.DoneWriting(1); err != nil { if err := rb.DoneWriting(testDataSize); err != nil {
falseErrorFail(t, err) falseErrorFail(t, err)
} }
if _, err := rb.Read(); err != nil { if _, err := rb.Read(); err != nil {
@ -147,12 +141,12 @@ func TestWritingAndReading1(t *testing.T) {
// Testing two 'writes' and two 'reads' // Testing two 'writes' and two 'reads'
func TestWritingAndReading2(t *testing.T) { func TestWritingAndReading2(t *testing.T) {
rb = NewRingBuffer(10, 10) rb = NewRingBuffer(testBufferSize, testElementSize)
for i := 0; i < 2; i++ { for i := 0; i < 2; i++ {
if _, err := rb.Get(); err != nil { if _, err := rb.Get(); err != nil {
falseErrorFail(t, err) falseErrorFail(t, err)
} }
if err := rb.DoneWriting(1); err != nil { if err := rb.DoneWriting(testDataSize); err != nil {
falseErrorFail(t, err) falseErrorFail(t, err)
} }
} }
@ -168,11 +162,11 @@ func TestWritingAndReading2(t *testing.T) {
// Testing one 'write' and two 'reads' // Testing one 'write' and two 'reads'
func TestWritingAndReading3(t *testing.T) { func TestWritingAndReading3(t *testing.T) {
rb = NewRingBuffer(10, 10) rb = NewRingBuffer(testBufferSize, testElementSize)
if _, err := rb.Get(); err != nil { if _, err := rb.Get(); err != nil {
falseErrorFail(t, err) falseErrorFail(t, err)
} }
if err := rb.DoneWriting(1); err != nil { if err := rb.DoneWriting(testDataSize); err != nil {
falseErrorFail(t, err) falseErrorFail(t, err)
} }
var err1 error var err1 error
@ -191,12 +185,12 @@ func TestWritingAndReading3(t *testing.T) {
// Test two 'writes' and one 'read' // Test two 'writes' and one 'read'
func TestWritingAndReading4(t *testing.T) { func TestWritingAndReading4(t *testing.T) {
rb = NewRingBuffer(10, 10) rb = NewRingBuffer(testBufferSize, testElementSize)
for i := 0; i < 2; i++ { for i := 0; i < 2; i++ {
if _, err := rb.Get(); err != nil { if _, err := rb.Get(); err != nil {
falseErrorFail(t, err) falseErrorFail(t, err)
} }
if err := rb.DoneWriting(1); err != nil { if err := rb.DoneWriting(testDataSize); err != nil {
falseErrorFail(t, err) falseErrorFail(t, err)
} }
} }
@ -210,12 +204,12 @@ func TestWritingAndReading4(t *testing.T) {
// Test writing past capacity // Test writing past capacity
func TestWritingAndReading5(t *testing.T) { func TestWritingAndReading5(t *testing.T) {
rb = NewRingBuffer(4, 10) rb = NewRingBuffer(testOverFlowSize, testElementSize)
var err1 error var err1 error
var err2 error var err2 error
for i := 0; i < 5; i++ { for i := 0; i < testOverFlowSize+1; i++ {
_, err1 = rb.Get() _, err1 = rb.Get()
err2 = rb.DoneWriting(1) err2 = rb.DoneWriting(testDataSize)
} }
if err1 == nil { if err1 == nil {
noErrorFail(t) noErrorFail(t)
@ -228,12 +222,12 @@ func TestWritingAndReading5(t *testing.T) {
// Test writing to size, then read some, then 'write' to test that we go back // Test writing to size, then read some, then 'write' to test that we go back
// to start of buffer // to start of buffer
func TestWritingAndReading6(t *testing.T) { func TestWritingAndReading6(t *testing.T) {
rb = NewRingBuffer(4, 10) rb = NewRingBuffer(testOverFlowSize, 10)
var err1 error var err1 error
var err2 error var err2 error
for i := 0; i < 4; i++ { for i := 0; i < testOverFlowSize; i++ {
_, err1 = rb.Get() _, err1 = rb.Get()
err2 = rb.DoneWriting(1) err2 = rb.DoneWriting(testDataSize)
} }
if err1 != nil { if err1 != nil {
falseErrorFail(t, err1) falseErrorFail(t, err1)
@ -252,7 +246,7 @@ func TestWritingAndReading6(t *testing.T) {
falseErrorFail(t, err2) falseErrorFail(t, err2)
} }
_, err1 = rb.Get() _, err1 = rb.Get()
err2 = rb.DoneWriting(1) err2 = rb.DoneWriting(testDataSize)
if err1 != nil { if err1 != nil {
falseErrorFail(t, err1) falseErrorFail(t, err1)
} }
@ -264,12 +258,12 @@ func TestWritingAndReading6(t *testing.T) {
// Now let's do the previous test again, but this time we will try to write // Now let's do the previous test again, but this time we will try to write
// past capacity // past capacity
func TestWritingAndReading7(t *testing.T) { func TestWritingAndReading7(t *testing.T) {
rb = NewRingBuffer(4, 10) rb = NewRingBuffer(testOverFlowSize, testElementSize)
var err1 error var err1 error
var err2 error var err2 error
for i := 0; i < 4; i++ { for i := 0; i < testOverFlowSize; i++ {
_, err1 = rb.Get() _, err1 = rb.Get()
err2 = rb.DoneWriting(1) err2 = rb.DoneWriting(testDataSize)
} }
if err1 != nil { if err1 != nil {
falseErrorFail(t, err1) falseErrorFail(t, err1)
@ -287,9 +281,9 @@ func TestWritingAndReading7(t *testing.T) {
if err2 != nil { if err2 != nil {
falseErrorFail(t, err2) falseErrorFail(t, err2)
} }
for i := 0; i < 3; i++ { for i := 0; i < testOverFlowSize; i++ {
_, err1 = rb.Get() _, err1 = rb.Get()
err2 = rb.DoneWriting(1) err2 = rb.DoneWriting(testDataSize)
} }
if err1 == nil { if err1 == nil {
noErrorFail(t) noErrorFail(t)
@ -301,12 +295,12 @@ func TestWritingAndReading7(t *testing.T) {
// Test reading twice without call to DoneReading // Test reading twice without call to DoneReading
func TestWritingAndReading8(t *testing.T) { func TestWritingAndReading8(t *testing.T) {
rb = NewRingBuffer(4, 10) rb = NewRingBuffer(testBufferSize, 10)
var err1 error var err1 error
var err2 error var err2 error
for i := 0; i < 4; i++ { for i := 0; i < 2; i++ {
_, err1 = rb.Get() _, err1 = rb.Get()
err2 = rb.DoneWriting(1) err2 = rb.DoneWriting(testDataSize)
} }
if err1 != nil { if err1 != nil {
falseErrorFail(t, err1) falseErrorFail(t, err1)
@ -326,7 +320,7 @@ func TestWritingAndReading8(t *testing.T) {
// any data race issues. We will continously write and read at concurrently // any data race issues. We will continously write and read at concurrently
// time for a second // time for a second
func TestConcurrency1(t *testing.T) { func TestConcurrency1(t *testing.T) {
rb = NewRingBuffer(1000, 10) rb = NewRingBuffer(testConcurrencyBufferSize, testElementSize)
go func() { go func() {
for i := 0; i < 100; i++ { for i := 0; i < 100; i++ {
if data, err1 := rb.Read(); data != nil { if data, err1 := rb.Read(); data != nil {
@ -343,7 +337,7 @@ func TestConcurrency1(t *testing.T) {
go func() { go func() {
for i := 0; i < 100; i++ { for i := 0; i < 100; i++ {
_, err1 := rb.Get() _, err1 := rb.Get()
err2 := rb.DoneWriting(1) err2 := rb.DoneWriting(testDataSize)
if err1 != nil { if err1 != nil {
falseErrorFail(t, err1) falseErrorFail(t, err1)
} }