Commit aadec552 authored by Andrea Orlat's avatar Andrea Orlat
Browse files

Class to hadle a FIFI queue added to IRALibrary

parent 1cd197c1
Loading
Loading
Loading
Loading
+95 −0
Original line number Diff line number Diff line
#ifndef FASTQUEUE_H_
#define FASTQUEUE_H_

/* **************************************************************************************************** */
/* IRA Istituto di Radioastronomia                                                                      */
/*                                                                                                      */
/* This code is under GNU General Public Licence (GPL).                                                 */
/*                                                                                                      */
/* Who                                when            What                                              */
/* Andrea Orlati(aorlati@ira.inaf.it) 06/11/2015      Creation                                          */

namespace IRA {

/**
 * This template class implements a fast queue (First In First out container). The implementation is based on a circular array and it permits to avoid re-allocation to address
 * performance issues
*/
template<class T> class CFastQueue {
public:

	/**
	 * Constructor.
	 * It construct the buffer with the specified number of elements. The array cannot be resized.
	 * @param positions the size of the buffer.
	*/
	CFastQueue(const unsigned& positions) : m_positions(positions), m_head(0), m_end(0), m_array(NULL)
	{
		m_array=new T[m_positions] ;
	}
	
	/**
	 * Destructor.
	*/
	~CFastQueue() {
		if (m_array) delete []m_array;
	}
	
	/**
	 * @return true is the queue is full
	 */
	inline bool isFull() const {
		return (m_head==((m_end+m_positions-1)%m_positions));
	}

	/**
	 * @return true is the queue is empty
	 */
	inline bool isEmpty() const {
		return (m_head==m_end);
	}

	inline unsigned size() const {
		return (m_head-m_end)%m_positions;
	}

	/**
	 * puts an element in the head of the queue.
	 * @param element new element to be added
	 * @return false if the container is full and no other elements could be added
	 */
	bool pushFront(const T& element) {
		if (isFull()) return false;
		m_array[m_head] = element;
		m_head = (m_head+ 1)%m_positions; // if full an element is overwritten
		return true;
	}

	/**
	 * pop back an element from the tail of the queue
	 * @element the next element is the queue
	 * @return false if the queue is empty and the element could not be pop
	 */
	bool  popBack(T& element) {
	    if (isEmpty()) return false;
	    element=m_array[m_end];
	    m_end=(m_end+1)%m_positions; // if it is not empty
	    return true;
	}
	
protected:
	unsigned m_positions;
	/** beggining of the array */
	unsigned m_head;
	/** end of the array */
	unsigned m_end;
	T *m_array;
private:
	CFastQueue(const CFastQueue&);
	const CFastQueue& operator =(const CFastQueue& src);
};
	
}

#endif /*FASTQUEUE_H_*/
+1 −1
Original line number Diff line number Diff line
@@ -33,7 +33,7 @@ USER_CFLAGS =
#
# Includes (.h) files (public only)
# ---------------------------------
INCLUDES = Definitions.h String.h IRATools.h Error.h SecureArea.h SerialPort.h Lecom.h \
INCLUDES = Definitions.h String.h IRATools.h Error.h SecureArea.h SerialPort.h Lecom.h FastQueue.h\
 Socket.h DataField.h DBTable.h LogFilter.h Site.h DateTime.h Timer.h SkySource.h TimeTaggedCircularArray.h \
 ScheduleTimer.h TimeoutSync.h MicroControllerBoard.h ReceiverControl.h MicroControllerBoardDef.h \
 SourceFlux.h CustomLoggerUtils.h LogDike.h FrequencyTracking.h IRA
+31 −0
Original line number Diff line number Diff line
#include "FastQueue.h"
#include <stdlib.h>

namespace IRALibraryTest {

#define FASTQUEUE_SIZE 10000

class IRALibrary_FastQueue : public ::testing::Test {
public:
	::testing::AssertionResult FastQueue_checkLimits() {
		RecordProperty("description","check the limits of the queue");
		IRA::CFastQueue<unsigned> queue(FASTQUEUE_SIZE);
		if (!queue.isEmpty()) ::testing::AssertionFailure() << " the container should be empty";
		for (unsigned i=1;i<FASTQUEUE_SIZE;i++) {
			if (!queue.pushFront(i)) {
				return ::testing::AssertionFailure() << i << " element cannot be inserted";
			}
		}
		if (queue.pushFront(1)) ::testing::AssertionFailure() << " insertion should have failed because of container limits";
		if (queue.size()!=FASTQUEUE_SIZE) ::testing::AssertionFailure() << " the size of the container is not expected";
		return ::testing::AssertionSuccess();
	}

protected:
	virtual void SetUp() {
	}
	virtual void TearDown() {
	}
};

}
+5 −0
Original line number Diff line number Diff line
#include "gtest/gtest.h"

#include "IRATools_test.i"
#include "FastQueue_test.i"


using namespace IRALibraryTest;
@@ -20,3 +21,7 @@ TEST_F(IRALibrary_IRATools, fileExists_checkExistance){
TEST_F(IRALibrary_IRATools, fileExists_checkNoExistance){
	EXPECT_TRUE(fileExists_checkNoExistance());
}

TEST_F(IRALibrary_FastQueue , FastQueue_checkLimits){
	EXPECT_TRUE(FastQueue_checkLimits());
}