Skip to content

Commit

Permalink
ENH: add test for commonly used circular buffer functionality.
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielbmotta committed Feb 7, 2022
1 parent 665f536 commit 0c3bc51
Showing 1 changed file with 108 additions and 3 deletions.
111 changes: 108 additions & 3 deletions testframes/test_utils_circularbuffer/test_utils_circularbuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,116 @@
*
*/

//=============================================================================================================
// INCLUDES
//=============================================================================================================

#include <utils/generics/circularbuffer.h>

//=============================================================================================================
// QT INCLUDES
//=============================================================================================================

#include <QCoreApplication>
#include <QObject>
#include <QDebug>
#include <QTest>

int main(int argc, char *argv[])
//=============================================================================================================
// USED NAMESPACES
//=============================================================================================================

using namespace UTILSLIB;

//=============================================================================================================
/**
* DECLARE CLASS TestCircularBuffer
*
* @brief The TestCircularBuffer class provides tests for verifying circular buffer functionality
*
*/

class TestCircularBuffer : public QObject
{
QCoreApplication a(argc, argv);
Q_OBJECT

private slots:
void testBufferCreationDestruction();
void testBufferPushingPopping();
void testBufferCapacity();
};

//=============================================================================================================

return a.exec();
void TestCircularBuffer::testBufferCreationDestruction()
{
//Test Constructors/Destructors
{
CircularBuffer<float> testBuffer(10);
}
{
CircularBuffer<int> *testBuffer = new CircularBuffer<int>(10);
delete testBuffer;
}
}

//=============================================================================================================

void TestCircularBuffer::testBufferPushingPopping()
{
CircularBuffer<int> testBuffer(10);

int testVal = 5;
int testArray[3] = {1, 2, 3};

//Verify Push
QVERIFY(testBuffer.push(testVal));
QVERIFY(testBuffer.push(testArray, 3));

int resultVal = 0;
int resultArray[3] = {0, 0, 0};

QVERIFY(testBuffer.pop(resultVal));
for (int i = 0; i < 3; ++i){
QVERIFY(testBuffer.pop(resultArray[i]));
}

QVERIFY(resultVal == testVal);
for (int i = 0; i < 3; ++i){
QVERIFY(resultArray[i] == resultArray[i]);
}
}

//=============================================================================================================

void TestCircularBuffer::testBufferCapacity()
{
CircularBuffer<int> testBuffer(2);
int testSink = 0;

QVERIFY(!testBuffer.pop(testSink));

testBuffer.push(5);
testBuffer.push(10);

QVERIFY(!testBuffer.push(15));

testBuffer.clear();

QVERIFY(!testBuffer.pop(testSink));

QVERIFY(testBuffer.push(20));

QVERIFY(testBuffer.pop(testSink));

QVERIFY(testSink == 20);

QVERIFY(!testBuffer.pop(testSink));
}

//=============================================================================================================
// MAIN
//=============================================================================================================

QTEST_GUILESS_MAIN(TestCircularBuffer);
#include "test_utils_circularbuffer.moc"

0 comments on commit 0c3bc51

Please sign in to comment.