From 508dc6536a689dfa45dd3c10844efde504941d04 Mon Sep 17 00:00:00 2001 From: Saxon Milton Date: Tue, 5 Dec 2017 20:18:48 +1030 Subject: [PATCH 1/2] Simplified testing file --- ringbuffer/RingBuffer_test.go | 342 +++++++++------------------------- 1 file changed, 85 insertions(+), 257 deletions(-) diff --git a/ringbuffer/RingBuffer_test.go b/ringbuffer/RingBuffer_test.go index 93fa7c5a..b368af11 100644 --- a/ringbuffer/RingBuffer_test.go +++ b/ringbuffer/RingBuffer_test.go @@ -29,28 +29,43 @@ LICENSE package ringbuffer import ( + "fmt" "testing" "time" ) +// Parameters used over the testing const ( testBufferSize = 10 testElementSize = 1 - testOverFlowSize = 5 + testOverFlowSize = 2 testDataSize = 1 testConcurrencyBufferSize = 1000 ) +// Consts to define types of actions, assertions and test ends +const ( + testWrite = 0 + testDoneWriting = 1 + testRead = 2 + testDoneReading = 3 + testEnd = 4 + assertNoError = 5 + assertError = 6 +) + +// Strings to help with error messages +const ( + noErrorFailMessage = "Should have got error!" + falseErrorFailMessage = "Should not have got error: %v" + atMessage = " Series: %v Action: %v" +) + var rb *ringBuffer -// Call when we get an error we shouldn't have -func falseErrorFail(t *testing.T, err error) { - t.Errorf("Should not have got error: %v", err) -} - -// Call when we should have got an error but didnt -func noErrorFail(t *testing.T) { - t.Errorf("Should have got error!") +// Call when the a test array has the wrong format +func wrongFmt() { + fmt.Println("Test has wrong format!") } // Test that the Make method correctly allocates memory for arbitrary buffer @@ -65,255 +80,68 @@ func TestMake(t *testing.T) { } } -// Test call to done writing when no writing has occured. -// We pass arbitrary clipSize. -func TestDoneWriting1(t *testing.T) { - if rb.DoneWriting(testDataSize) == nil { - noErrorFail(t) - } +// Format: { , , , ..., , } +var tests = [][]int{ + {testBufferSize, assertError, testDoneWriting, testEnd}, + {testBufferSize, assertNoError, testWrite, testDoneWriting, testEnd}, + {testBufferSize, assertError, testDoneReading, testEnd}, + {testBufferSize, assertError, testRead, testEnd}, + {testBufferSize, assertError, testWrite, testWrite, testEnd}, + {testBufferSize, assertNoError, testWrite, testDoneWriting, testRead, + testDoneReading, testEnd}, + {testBufferSize, assertNoError, testWrite, testDoneWriting, testWrite, + testDoneWriting, testRead, testDoneReading, testRead, testDoneReading, + testEnd}, + {testBufferSize, assertError, testWrite, testDoneWriting, testRead, + testDoneReading, testRead, testDoneReading, testEnd}, + {testOverFlowSize, assertError, testWrite, testDoneWriting, testWrite, testDoneWriting, + testWrite, testDoneWriting, testEnd}, + {testOverFlowSize, assertNoError, testWrite, testDoneWriting, testWrite, testDoneWriting, + testRead, testDoneReading, testWrite, testDoneWriting, testEnd}, + {testOverFlowSize, assertError, testWrite, testDoneWriting, testWrite, testDoneWriting, + testRead, testDoneReading, testWrite, testDoneWriting, testWrite, + testDoneWriting, testEnd}, + {testBufferSize, assertError, testWrite, testDoneWriting, testWrite, + testDoneWriting, testRead, testRead, testEnd}, } -// Test call to done writing when there has been some 'writing' -func TestDoneWriting2(t *testing.T) { - if _, err := rb.Get(); err != nil { - falseErrorFail(t, err) - } - if err := rb.DoneWriting(testDataSize); err != nil { - falseErrorFail(t, err) - } -} - -// Test call to done reading when there hasn't been any reading -func TestDoneReading1(t *testing.T) { - rb = NewRingBuffer(testBufferSize, testElementSize) - if rb.DoneReading() == nil { - noErrorFail(t) - } -} - -// Test read when there hasn't been anything written to buffer -func TestReadingWithoutWrite(t *testing.T) { - rb = NewRingBuffer(testBufferSize, testElementSize) - _, err := rb.Read() - if err == nil { - noErrorFail(t) - } -} - -// Try Writing twice without calling DoneWriting -func TestWritingTwice(t *testing.T) { - rb = NewRingBuffer(testBufferSize, testElementSize) - _, err := rb.Get() - _, err = rb.Get() - if err == nil { - noErrorFail(t) - } -} - -// Test call to done reading when there has been some reading -func TestDoneReading2(t *testing.T) { - rb = NewRingBuffer(testBufferSize, testElementSize) - _, err := rb.Get() - rb.DoneWriting(testDataSize) - _, err = rb.Read() - if err != nil { - falseErrorFail(t, err) - } - rb.DoneReading() -} - -// Testing writing then reading for single 'write' and single 'read' routines -func TestWritingAndReading1(t *testing.T) { - rb = NewRingBuffer(testBufferSize, testElementSize) - if _, err := rb.Get(); err != nil { - falseErrorFail(t, err) - } - if err := rb.DoneWriting(testDataSize); err != nil { - falseErrorFail(t, err) - } - if _, err := rb.Read(); err != nil { - falseErrorFail(t, err) - } - if err := rb.DoneReading(); err != nil { - falseErrorFail(t, err) - } -} - -// Testing two 'writes' and two 'reads' -func TestWritingAndReading2(t *testing.T) { - rb = NewRingBuffer(testBufferSize, testElementSize) - for i := 0; i < 2; i++ { - if _, err := rb.Get(); err != nil { - falseErrorFail(t, err) +// Tests series of actions performed by the ring buffer defined in array +// tests +func TestAllSorts(t *testing.T) { + for i := range tests { + rb = NewRingBuffer(tests[i][0], testElementSize) + var err error + for j := 2; tests[i][j] != testEnd; j++ { + var newErr error + switch tests[i][j] { + case testWrite: + _, newErr = rb.Get() + case testDoneWriting: + newErr = rb.DoneWriting(testDataSize) + case testRead: + _, newErr = rb.Read() + case testDoneReading: + newErr = rb.DoneReading() + default: + wrongFmt() + } + if err == nil { + err = newErr + } } - if err := rb.DoneWriting(testDataSize); err != nil { - falseErrorFail(t, err) + switch tests[i][1] { + case assertError: + if err == nil { + t.Errorf(noErrorFailMessage+atMessage, i) + } + case assertNoError: + if err != nil { + t.Errorf(falseErrorFailMessage+atMessage, err, i) + } + default: + wrongFmt() } } - for i := 0; i < 2; i++ { - if _, err := rb.Read(); err != nil { - falseErrorFail(t, err) - } - if err := rb.DoneReading(); err != nil { - falseErrorFail(t, err) - } - } -} - -// Testing one 'write' and two 'reads' -func TestWritingAndReading3(t *testing.T) { - rb = NewRingBuffer(testBufferSize, testElementSize) - if _, err := rb.Get(); err != nil { - falseErrorFail(t, err) - } - if err := rb.DoneWriting(testDataSize); err != nil { - falseErrorFail(t, err) - } - var err1 error - var err2 error - for i := 0; i < 2; i++ { - _, err1 = rb.Read() - err2 = rb.DoneReading() - } - if err1 == nil { - noErrorFail(t) - } - if err2 == nil { - noErrorFail(t) - } -} - -// Test two 'writes' and one 'read' -func TestWritingAndReading4(t *testing.T) { - rb = NewRingBuffer(testBufferSize, testElementSize) - for i := 0; i < 2; i++ { - if _, err := rb.Get(); err != nil { - falseErrorFail(t, err) - } - if err := rb.DoneWriting(testDataSize); err != nil { - falseErrorFail(t, err) - } - } - if _, err := rb.Read(); err != nil { - falseErrorFail(t, err) - } - if err := rb.DoneReading(); err != nil { - falseErrorFail(t, err) - } -} - -// Test writing past capacity -func TestWritingAndReading5(t *testing.T) { - rb = NewRingBuffer(testOverFlowSize, testElementSize) - var err1 error - var err2 error - for i := 0; i < testOverFlowSize+1; i++ { - _, err1 = rb.Get() - err2 = rb.DoneWriting(testDataSize) - } - if err1 == nil { - noErrorFail(t) - } - if err2 == nil { - noErrorFail(t) - } -} - -// Test writing to size, then read some, then 'write' to test that we go back -// to start of buffer -func TestWritingAndReading6(t *testing.T) { - rb = NewRingBuffer(testOverFlowSize, testElementSize) - var err1 error - var err2 error - for i := 0; i < testOverFlowSize; i++ { - _, err1 = rb.Get() - err2 = rb.DoneWriting(testDataSize) - } - if err1 != nil { - falseErrorFail(t, err1) - } - if err2 != nil { - falseErrorFail(t, err2) - } - for i := 0; i < 2; i++ { - _, err1 = rb.Read() - err2 = rb.DoneReading() - } - if err1 != nil { - falseErrorFail(t, err1) - } - if err2 != nil { - falseErrorFail(t, err2) - } - _, err1 = rb.Get() - err2 = rb.DoneWriting(testDataSize) - if err1 != nil { - falseErrorFail(t, err1) - } - if err2 != nil { - falseErrorFail(t, err2) - } -} - -// Now let's do the previous test again, but this time we will try to write -// past capacity -func TestWritingAndReading7(t *testing.T) { - rb = NewRingBuffer(testOverFlowSize, testElementSize) - var err1 error - var err2 error - for i := 0; i < testOverFlowSize; i++ { - _, err1 = rb.Get() - err2 = rb.DoneWriting(testDataSize) - } - if err1 != nil { - falseErrorFail(t, err1) - } - if err2 != nil { - falseErrorFail(t, err2) - } - for i := 0; i < 2; i++ { - _, err1 = rb.Read() - err2 = rb.DoneReading() - } - if err1 != nil { - falseErrorFail(t, err1) - } - if err2 != nil { - falseErrorFail(t, err2) - } - for i := 0; i < testOverFlowSize; i++ { - _, err1 = rb.Get() - err2 = rb.DoneWriting(testDataSize) - } - if err1 == nil { - noErrorFail(t) - } - if err2 == nil { - noErrorFail(t) - } -} - -// Test reading twice without call to DoneReading -func TestWritingAndReading8(t *testing.T) { - rb = NewRingBuffer(testBufferSize, 10) - var err1 error - var err2 error - for i := 0; i < 2; i++ { - _, err1 = rb.Get() - err2 = rb.DoneWriting(testDataSize) - } - if err1 != nil { - falseErrorFail(t, err1) - } - if err2 != nil { - falseErrorFail(t, err2) - } - for i := 0; i < 2; i++ { - _, err1 = rb.Read() - } - if err1 == nil { - noErrorFail(t) - } } // Let's see if we can do concurrency. (tip: run as: go test -race) to see @@ -326,10 +154,10 @@ func TestConcurrency1(t *testing.T) { if data, err1 := rb.Read(); data != nil { err2 := rb.DoneReading() if err1 != nil { - falseErrorFail(t, err1) + t.Errorf(falseErrorFailMessage, err1) } if err2 != nil { - falseErrorFail(t, err2) + t.Errorf(falseErrorFailMessage, err2) } } } @@ -339,10 +167,10 @@ func TestConcurrency1(t *testing.T) { _, err1 := rb.Get() err2 := rb.DoneWriting(testDataSize) if err1 != nil { - falseErrorFail(t, err1) + t.Errorf(falseErrorFailMessage, err1) } if err2 != nil { - falseErrorFail(t, err2) + t.Errorf(falseErrorFailMessage, err2) } } }() From e1e38bcafc3634c4f8e0b0dbc34f162ed8d8c464 Mon Sep 17 00:00:00 2001 From: Saxon Milton Date: Tue, 5 Dec 2017 20:22:34 +1030 Subject: [PATCH 2/2] Cleaned up some comments --- ringbuffer/RingBuffer_test.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/ringbuffer/RingBuffer_test.go b/ringbuffer/RingBuffer_test.go index b368af11..11849a04 100644 --- a/ringbuffer/RingBuffer_test.go +++ b/ringbuffer/RingBuffer_test.go @@ -43,7 +43,7 @@ const ( testConcurrencyBufferSize = 1000 ) -// Consts to define types of actions, assertions and test ends +// Types of actions, assertions and test ends const ( testWrite = 0 testDoneWriting = 1 @@ -54,13 +54,14 @@ const ( assertError = 6 ) -// Strings to help with error messages +// Strings to help with test fail messages const ( noErrorFailMessage = "Should have got error!" falseErrorFailMessage = "Should not have got error: %v" - atMessage = " Series: %v Action: %v" + atMessage = " Series: %v" ) +// Pointer to the ringbuffer we will be testing var rb *ringBuffer // Call when the a test array has the wrong format @@ -105,8 +106,7 @@ var tests = [][]int{ testDoneWriting, testRead, testRead, testEnd}, } -// Tests series of actions performed by the ring buffer defined in array -// tests +// Tests series of actions performed by the ring buffer defined in array tests func TestAllSorts(t *testing.T) { for i := range tests { rb = NewRingBuffer(tests[i][0], testElementSize)