TEST(CircularBuffer, OverlapPushBackFull) { CircularBuffer<int, 5> cb; cb.pushBack(0); cb.pushBack(1); cb.pushBack(2); ASSERT_FALSE(cb.empty()); ASSERT_EQ(cb.size(), 3); ASSERT_EQ(cb.popFront(), 0); ASSERT_EQ(cb.popFront(), 1); ASSERT_EQ(cb.popFront(), 2); cb.pushBack(0); cb.pushBack(1); cb.pushBack(2); cb.pushBack(3); cb.pushBack(4); ASSERT_FALSE(cb.empty()); ASSERT_EQ(cb.size(), 5); ASSERT_EQ(cb.popFront(), 0); ASSERT_EQ(cb.popFront(), 1); ASSERT_EQ(cb.popFront(), 2); ASSERT_EQ(cb.popFront(), 3); ASSERT_EQ(cb.popFront(), 4); ASSERT_TRUE(cb.empty()); ASSERT_EQ(cb.size(), 0); }
TEST(CircularBuffer, SimplePushBackPop) { CircularBuffer<int, 5> cb; cb.pushBack(123); ASSERT_FALSE(cb.empty()); ASSERT_EQ(cb.size(), 1); ASSERT_EQ(cb.popFront(), 123); ASSERT_TRUE(cb.empty()); ASSERT_EQ(cb.size(), 0); }
/* This function calculates the average of values in cBuf */ void updateAverage( INTYPE data) { double tempVal = 0; float mulFactor = 0; INTYPE oldElement = 0; if( !stableStream ) { /* Add data to Buffer */ cBuf.push_back(data); mulFactor = 1/(double)(cBuf.size()); /* If number of elements in the buffer < total buffer capacity * Calculate the average using loops */ for(int i = 0; i < (int)cBuf.size(); i++) { tempVal += cBuf[i]; } tempVal = tempVal * mulFactor; currentAverage = tempVal; stableStream = (cBuf.size() == cBuf.capacity()); } else { /* Efficient way of calculating average in case the buffer is full * Every calculation will update the current average value. * To get new average: * new_average = * current_average + * ( new_element - oldest_element ) * mulFactor * * The CircularBuffer at this point is Full * */ oldElement = cBuf.peek_front(); /* Adding new data Element */ cBuf.push_back(data, OVERWRITE_OLD_DATA); /* We now have a pointer to the old element in the buffer */ tempVal = currentAverage; tempVal = tempVal + ( ( data - oldElement ) / ( cBuf.capacity() ) ); currentAverage = tempVal; } }
TEST(CircularBuffer, Default) { CircularBuffer<int, 5> cb; ASSERT_TRUE(cb.empty()); ASSERT_EQ(cb.size(), 0); }
TEST(CircularBuffer, SimplePushFrontFull) { CircularBuffer<int, 5> cb; cb.pushFront(0); cb.pushFront(1); cb.pushFront(2); cb.pushFront(3); cb.pushFront(4); ASSERT_FALSE(cb.empty()); ASSERT_EQ(cb.size(), 5); ASSERT_EQ(cb.popFront(), 4); ASSERT_EQ(cb.popFront(), 3); ASSERT_EQ(cb.popFront(), 2); ASSERT_EQ(cb.popFront(), 1); ASSERT_EQ(cb.popFront(), 0); ASSERT_TRUE(cb.empty()); ASSERT_EQ(cb.size(), 0); }
TEST(CircularBuffer, OverlapPushFrontPop) { CircularBuffer<int, 5> cb; cb.pushFront(0); cb.pushFront(1); cb.pushFront(2); cb.pushFront(3); ASSERT_FALSE(cb.empty()); ASSERT_EQ(cb.size(), 4); ASSERT_EQ(cb.popFront(), 3); ASSERT_EQ(cb.popFront(), 2); ASSERT_EQ(cb.popFront(), 1); ASSERT_EQ(cb.popFront(), 0); ASSERT_TRUE(cb.empty()); ASSERT_EQ(cb.size(), 0); cb.pushFront(0); cb.pushFront(1); cb.pushFront(2); cb.pushFront(3); ASSERT_FALSE(cb.empty()); ASSERT_EQ(cb.size(), 4); ASSERT_EQ(cb.popFront(), 3); ASSERT_EQ(cb.popFront(), 2); ASSERT_EQ(cb.popFront(), 1); ASSERT_EQ(cb.popFront(), 0); ASSERT_TRUE(cb.empty()); ASSERT_EQ(cb.size(), 0); }
void Plot::setSettings( const Settings &s ) { if ( d_timerId >= 0 ) killTimer( d_timerId ); d_timerId = startTimer( s.updateInterval ); d_grid->setPen( s.grid.pen ); d_grid->setVisible( s.grid.pen.style() != Qt::NoPen ); CircularBuffer *buffer = ( CircularBuffer * )d_curve->data(); if ( s.curve.numPoints != buffer->size() || s.curve.functionType != d_settings.curve.functionType ) { switch( s.curve.functionType ) { case Settings::Wave: buffer->setFunction( wave ); break; case Settings::Noise: buffer->setFunction( noise ); break; default: buffer->setFunction( NULL ); } buffer->fill( d_interval, s.curve.numPoints ); } d_curve->setPen( s.curve.pen ); d_curve->setBrush( s.curve.brush ); d_curve->setPaintAttribute( QwtPlotCurve::ClipPolygons, s.curve.paintAttributes & QwtPlotCurve::ClipPolygons ); d_curve->setRenderHint( QwtPlotCurve::RenderAntialiased, s.curve.renderHint & QwtPlotCurve::RenderAntialiased ); canvas()->setAttribute( Qt::WA_PaintOnScreen, s.canvas.paintOnScreen ); canvas()->setPaintAttribute( QwtPlotCanvas::BackingStore, s.canvas.useBackingStore ); canvas()->setPaintAttribute( QwtPlotCanvas::ImmediatePaint, s.canvas.immediatePaint ); QwtPainter::setPolylineSplitting( s.curve.lineSplitting ); d_settings = s; }
int main() { { // Test default constructor CircularBuffer buf; std::cout << buf.size() << "\n"; } { // Test constructor taking 1 arg: size of buffer CircularBuffer buf(32); std::cout << buf.size() << "\n"; } { // Test constructor taking std::vector std::vector<int> v = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0}; CircularBuffer buf(v); std::cout << buf.size() << "\n"; } { // Test copy constructor std::vector<int> v = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0}; CircularBuffer buf(v); CircularBuffer buf2(buf); std::cout << buf.size() << ", " << buf2.size() << "\n"; } { // Test move constructor std::vector<int> v = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0}; CircularBuffer buf(v); CircularBuffer buf2(std::move(buf)); std::cout << buf.size() << ", " << buf2.size() << "\n"; } { // Test copy assignment std::vector<int> v = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0}; CircularBuffer buf(v); CircularBuffer buf2(3); std::cout << buf.size() << ", " << buf2.size() << " -> "; buf2 = buf; std::cout << buf.size() << ", " << buf2.size() << "\n"; } { // Test move assignment std::vector<int> v = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0}; CircularBuffer buf(v); CircularBuffer buf2(3); std::cout << buf.size() << ", " << buf2.size() << " -> "; buf2 = std::move(buf); std::cout << buf.size() << ", " << buf2.size() << "\n"; } { // Test indexing CircularBuffer buf(10); buf[0] = buf[2] = buf[4] = buf[6] = buf[8] = 1; buf[1] = buf[3] = buf[5] = buf[7] = buf[9] = 2; std::cout << buf.at(1) << " " << buf[6] << "\n"; std::cout << buf.at(32) << " " << buf[127] << "\n"; } { // Test indexing const object const CircularBuffer buf({1, 2, 3, 4, 5}); std::cout << buf.at(1) << " " << buf[8] << "\n"; } { // Test size (already used above) CircularBuffer buf(3); if (buf.size() != 3) std::cout << "Incorrect size!\n"; else std::cout << "Correct size!\n"; } { // Test clear CircularBuffer buf(5); std::cout << buf.size() << " -> "; buf.clear(); std::cout << buf.size() << "\n"; } { // Test swap CircularBuffer buf({1, 2, 3, 4, 5}); CircularBuffer buf2({6, 7, 8, 9, 0}); std::cout << buf[0] << " " << buf[4] << " : " << buf2[0] << " " << buf2[4] << " -> "; buf.swap(buf2); std::cout << buf[0] << " " << buf[4] << " : " << buf2[0] << " " << buf2[4] << "\n"; } { // Test insert CircularBuffer buf; buf.insert(0, 1); buf.insert(0, 2); buf.insert(0, 3); std::cout << buf.size() << " " << buf[2] << "\n"; buf.insert(200, 4); std::cout << buf.size() << " " << buf[2] << "\n"; } { // Test move-insert CircularBuffer buf; int x = 3; buf.insert(0, std::move(x)); int y = 2; buf.insert(0, std::move(y)); std::cout << buf[0] << " " << buf[1] << " " << buf[2] << "\n"; } { // Test erase CircularBuffer buf({1, 2, 3, 4}); std::cout << buf[0] << " " << buf.size() << " -> "; buf.erase(0); std::cout << buf[0] << " " << buf.size() << "\n"; } return 0; }
int main ( int argc, char ** argv ) { size_t buffsize = DEFAULT_CIRBUFFER_SIZE; size_t maxsize = MAX_CIRBUFFER_SIZE; if ( argc == 2 ) buffsize = StringUtils::FromString<size_t>(argv[1]); CircularBuffer * buff = new CircularBuffer(buffsize); std::string bstr = "0123456789"; int count = buffsize / bstr.length(); std::cout << " buffer capacity = " << buff->size() << ", max is " << maxsize << ", string '" << bstr << std::endl << "', count is " << count << std::endl << std::endl; while ( buff->writeAvailable() >= bstr.length() ) buff->write(bstr.c_str(), bstr.length()); char * out = (char*) calloc(bstr.length(), sizeof(char)); std::cout << " dataAvail in buffer = " << buff->readAvailable() << std::endl; for ( int i = 0; i < (count / 2); ++i ) { buff->read(out, bstr.length()); } std::cout << " read: '" << out << "'" << std::endl; buffsize = buffsize - (count / 2); std::cout << " resizing to " << buffsize << std::endl; if ( ! buff->resize(buffsize) ) std::cout << "RESIZE FAILED" << std::endl; std::cout << " buffer capacity = " << buff->size() << std::endl << " fullDataAvail = " << buff->readAvailable() << " dataAvail = " << buff->readPtrAvailable() << std::endl << " fullSpaceAvail = " << buff->writeAvailable() << " spaceAvail = " << buff->writePtrAvailable() << std::endl << std::endl; int c = 0; while ( buff->writePtrAvailable() >= bstr.length() ) { buff->write(bstr.c_str(), bstr.length()); c++; } std::cout << " write count = " << c << std::endl << std::endl; std::cout << " buffer capacity = " << buff->size() << std::endl << " fullDataAvail = " << buff->readAvailable() << " dataAvail = " << buff->readPtrAvailable() << std::endl << " fullSpaceAvail = " << buff->writeAvailable() << " spaceAvail = " << buff->writePtrAvailable() << std::endl << std::endl; buffsize = buff->size() + bstr.length(); std::cout << " resizing to " << buffsize << std::endl; if ( ! buff->resize(buffsize) ) std::cout << " RESIZE FAILED " << std::endl; std::cout << " buffer capacity = " << buff->size() << std::endl << " fullDataAvail = " << buff->readAvailable() << " dataAvail = " << buff->readPtrAvailable() << std::endl << " fullSpaceAvail = " << buff->writeAvailable() << " spaceAvail = " << buff->writePtrAvailable() << std::endl << std::endl; ::free(out); delete buff; return 0; }
void Plot::setSettings( const Settings &s ) { if ( d_timerId >= 0 ) killTimer( d_timerId ); d_timerId = startTimer( s.updateInterval ); d_grid->setPen( s.grid.pen ); d_grid->setVisible( s.grid.pen.style() != Qt::NoPen ); CircularBuffer *buffer = static_cast<CircularBuffer *>( d_curve->data() ); if ( s.curve.numPoints != buffer->size() || s.curve.functionType != d_settings.curve.functionType ) { switch( s.curve.functionType ) { case Settings::Wave: buffer->setFunction( wave ); break; case Settings::Noise: buffer->setFunction( noise ); break; default: buffer->setFunction( NULL ); } buffer->fill( d_interval, s.curve.numPoints ); } d_curve->setPen( s.curve.pen ); d_curve->setBrush( s.curve.brush ); d_curve->setPaintAttribute( QwtPlotCurve::ClipPolygons, s.curve.paintAttributes & QwtPlotCurve::ClipPolygons ); d_curve->setPaintAttribute( QwtPlotCurve::FilterPoints, s.curve.paintAttributes & QwtPlotCurve::FilterPoints ); d_curve->setRenderHint( QwtPlotItem::RenderAntialiased, s.curve.renderHint & QwtPlotItem::RenderAntialiased ); #ifndef QWT_NO_OPENGL if ( s.canvas.openGL ) { QwtPlotGLCanvas *plotCanvas = qobject_cast<QwtPlotGLCanvas *>( canvas() ); if ( plotCanvas == NULL ) { plotCanvas = new GLCanvas(); plotCanvas->setPalette( QColor( "khaki" ) ); setCanvas( plotCanvas ); } } else #endif { QwtPlotCanvas *plotCanvas = qobject_cast<QwtPlotCanvas *>( canvas() ); if ( plotCanvas == NULL ) { plotCanvas = new QwtPlotCanvas(); plotCanvas->setFrameStyle( QFrame::Box | QFrame::Plain ); plotCanvas->setLineWidth( 1 ); plotCanvas->setPalette( Qt::white ); setCanvas( plotCanvas ); } plotCanvas->setAttribute( Qt::WA_PaintOnScreen, s.canvas.paintOnScreen ); plotCanvas->setPaintAttribute( QwtPlotCanvas::BackingStore, s.canvas.useBackingStore ); plotCanvas->setPaintAttribute( QwtPlotCanvas::ImmediatePaint, s.canvas.immediatePaint ); } QwtPainter::setPolylineSplitting( s.curve.lineSplitting ); d_settings = s; }