Commit fbeb840c authored by Andrea Zoli's avatar Andrea Zoli
Browse files

Using boost testing framework for InputPacketStreamFile class.

parent 57f3931d
Loading
Loading
Loading
Loading
+7 −1
Original line number Diff line number Diff line
@@ -124,6 +124,12 @@ $(shell cut $(INCLUDE_DIR)/$(VER_FILE_NAME) -f 3 > version)

####### 9) Pattern rules

test/%.o : test/%.cpp
	$(CC) $(CPPFLAGS) $(ALL_CFLAGS) -c $< -o $@

test/%: test/%.o lib
	$(CC) $(CPPFLAGS) $(ALL_CFLAGS) -o $@ $< -lboost_unit_test_framework -Llib -lpacket

%.o : %.cpp
	$(CC) $(CPPFLAGS) $(ALL_CFLAGS) -c $< -o $(OBJECTS_DIR)/$@

@@ -145,7 +151,7 @@ exe: makeobjdir main.o $(OBJECTS)
		test -d $(EXE_DESTDIR) || mkdir -p $(EXE_DESTDIR)
		$(CC) $(CPPFLAGS) $(ALL_CFLAGS) -o $(EXE_DESTDIR)/$(EXE_NAME) $(OBJECTS_DIR)/*.o $(LIBS)
	
tests: test/testsuite.o
tests: test/testInputPacketStreamFile

staticlib: makelibdir makeobjdir $(OBJECTS)	
		test -d $(LIB_DESTDIR) || mkdir -p $(LIB_DESTDIR)	
+129 −0
Original line number Diff line number Diff line
/***************************************************************************
 *     begin                : Oct 07 2013
 *     copyright            : (C) 2013 Andrea Zoli
 *     email                : zoli@iasfbo.inaf.it
***************************************************************************/

/***************************************************************************
*                                                                         *
*   This program is free software for non commercial purpose              *
*   and for public research institutes; you can redistribute it and/or    *
*   modify it under the terms of the GNU General Public License.          *
*   For commercial purpose see appropriate license terms                  *
*                                                                         *
***************************************************************************/

//#include<sstream>
//#include<fstream>
//#include<iomanip>
#include "InputPacketStreamFile.h"
#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE MyTest
#include <boost/test/unit_test.hpp>
#include<unistd.h>

const char config[] = "stream.stream";

const int NFILES=5;

const char* files[NFILES] = {	"./CAL-DFE-TE/data/10BURT.RAW",
								"./CAL-DFE-TE/data/03bursts.raw",
								"./CAL-DFE-TE/data/03grids.raw",
								"./CAL-DFE-TE/data/06grids.raw",
								"./CAL-DFE-TE/data/15grids.raw" };

const int good[NFILES] = {100, 100, 100, 100, 100};
const int bad[NFILES] = {5, 5, 5, 5, 5};

BOOST_AUTO_TEST_CASE(input_packet_stream_file)
{
	PacketLib::InputPacketStreamFile ips;

	// loading an invalid file should raise an exception
	ips.setFileNameConfig((char*)"notafile");
	BOOST_CHECK_THROW(ips.createStreamStructure(), PacketLib::PacketExceptionIO);

	// loading a valid config file should not raise an exception
	ips.setFileNameConfig((char*)config);
	BOOST_CHECK_NO_THROW(ips.createStreamStructure());

	for(int i=0; i<NFILES; i++)
	{
        ips.setInitialPosition(0L);

		// opening a valid input stream should not raise an exception
		ips.setFileNameStream((char*)files[i]);
		BOOST_CHECK_NO_THROW(ips.openInputStream());

		// testing stream EOF should not raise an exception
		bool eof;
		BOOST_CHECK_NO_THROW(eof = ips.isInputStreamEOF());

		// getting a packet from the stream should not raise an exception
		PacketLib::Packet *p;
		BOOST_CHECK_NO_THROW(p = ips.getPacketFromStream());

        long count = 0;
        long countbad = 0;

		while(!eof) {
			if(p != 0) {
				count++;
				p->deleteExternalByteStream();
			}
			else
				countbad++;

			// testing stream EOF should not raise an exception
			BOOST_CHECK_NO_THROW(eof = ips.isInputStreamEOF());

			// getting a packet from the stream should not raise an exception
			BOOST_CHECK_NO_THROW(p = ips.getPacketFromStream());
		}

		// good packets number should be equal to its known value
		BOOST_CHECK_EQUAL(count, good[i]);

		// bad packets number should be equal to its known value
		BOOST_CHECK_EQUAL(countbad, bad[i]);
	}
}

BOOST_AUTO_TEST_CASE(input_packet_stream_file_freerun)
{
	PacketLib::InputPacketStreamFile ips;

	// loading an invalid file should raise an exception
	ips.setFileNameConfig((char*)"notafile");
	BOOST_CHECK_THROW(ips.createStreamStructure(), PacketLib::PacketExceptionIO);

	// loading a valid config file should not raise an exception
	ips.setFileNameConfig((char*)config);
	BOOST_CHECK_NO_THROW(ips.createStreamStructure());

	for(int i=0; i<NFILES; i++)
	{
        ips.setInitialPosition(0L);

		// opening a valid input stream should not raise an exception
		ips.setFileNameStream((char*)files[i]);
		BOOST_CHECK_NO_THROW(ips.openInputStream());

		// getting a packet from the stream should not raise an exception
		PacketLib::Packet *p;
		BOOST_CHECK_NO_THROW(p = ips.getPacketFromStream());

		// executing free run should not raise an exception
		BOOST_CHECK_NO_THROW(ips.freeRun());

        long num = ips.getNumberOfFileStreamPointer();
		for(int i=0; i<num; i++) {
			PacketLib::FileStreamPointer* fsp;
			// getting a FileStreamPointer should not raise an exception
			BOOST_CHECK_NO_THROW(fsp = ips.getFileStreamPointer(i));

			// type of packet should be != 0
			BOOST_CHECK(fsp->typeOfPacket != 0);
		}
	}
}

test/testpacketlib.cpp

deleted100644 → 0
+0 −156
Original line number Diff line number Diff line
/***************************************************************************
                          testpacketlib.cpp  -  description
                             -------------------
    begin                : Fri Feb 22 2002
    copyright            : (C) 2013 by Andrea Bulgarelli
    email                : bulgarelli@iasbo.inaf.it
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include "testpacketlib.h"



TestPacketLib::TestPacketLib() {
    ips = new InputPacketStreamFile();
}

TestPacketLib::~TestPacketLib() {
    delete ips;
}

char* TestPacketLib::initStructure(char* fileNameConfig) {
    try {

        ips->setFileNameConfig(fileNameConfig);
        ips->createStreamStructure();

        //delete this line
//	Packet* p = ips.getPacketType(1);
//  int dim = p->getDimension();


        return 0;
    }
    catch(PacketExceptionIO* e) {
        return (char*) e->geterror();
    }
    catch(PacketExceptionFileFormat* e) {
        return (char*) e->geterror();
    }


}


bool TestPacketLib::TestSuiteType1(char* nameOfTestSuite, char* fileNameConfig, char** file) {
    char* ret;
    bool ret2 = true;

    try {
        if(OUTPUT) cout << "--------------------------------------------------" << endl;
        if(OUTPUT) cout << "START OF " << nameOfTestSuite << " with the file " << fileNameConfig << endl;
        ret=initStructure(fileNameConfig);
        if(ret != 0) {
            cout << ret;
            return false;
        }
        if(OUTPUT) cout << "OK: Stream structure created" << endl;
        //for(int j=0; j<3; j++) {
        for(int i=0; file[i] != 0; i++)
            if(!test2(file[i]))
                ret2 = false;
        //}
        if(!ret2)
            return false;
        else
            return true;
    }
    catch(PacketExceptionIO* e) {
        if(OUTPUT) cout << "ERROR: " << (char*) e->geterror();
        return false;
    }
    catch(PacketExceptionFileFormat* e) {
        if(OUTPUT) cout << "ERROR: " << (char*) e->geterror();
        return false;
    }
}

bool TestPacketLib::test(char* file) {
    try {
        long index ;
        cout << "TEST: " << file << endl;
        ips->setFileNameStream(file);
        ips->setInitialPosition(0L);
        ips->openInputStream();
        if(OUTPUT) cout << "OK: Data file opened" << endl;
        if(!ips->freeRun()) {
            if(OUTPUT) cout << "ERROR: freeRun failed";
            return false;
        }
        index = ips->getNumberOfFileStreamPointer();
        if(OUTPUT) cout << "OK: construction of FileStreamPointer. Found " << index << " packet" << endl;
        for(int i=0; i< index; i++)  {
            FileStreamPointer* fsp = ips->getFileStreamPointer(i);
            if(!fsp->typeOfPacket) {
                if(OUTPUT) cout << "WARNING: packet not recognized" << endl;
                return false;
            }
        }
        if(OUTPUT) cout << "OK: all the packet are recognized" << endl;
        return true;
    }
    catch(PacketExceptionIO* e) {
        if(OUTPUT) cout << "ERROR: " << (char*) e->geterror();
        return false;
    }
    catch(PacketExceptionFileFormat* e) {
        if(OUTPUT) cout << "ERROR: " << (char*) e->geterror();
        return false;
    }
}


bool TestPacketLib::test2(char* file) {
    try {
        cout << "TEST: " << file << endl;
        ips->setFileNameStream(file);
        ips->setInitialPosition(0L);
        ips->openInputStream();
        if(OUTPUT) cout << "OK: Data file opened" << endl;
        long count = 0;
        long countbad = 0;
        Packet* p;
        while(!ips->isInputStreamEOF()) {
            p = ips->getPacketFromStream();
            if(p != 0) {
                count++;
                p->deleteExternalByteStream();
            }
            else if(!ips->isInputStreamEOF()) {
                countbad++;
            }
        }
        if(p != 0)
            p->deleteExternalByteStream();
        cout << "Bad: " << countbad << endl;
        cout << "OK: " << count << endl;
        return true;
    }
    catch(PacketExceptionIO* e) {
        if(OUTPUT) cout << "ERROR: " << (char*) e->geterror();
        return false;
    }
    catch(PacketExceptionFileFormat* e) {
        if(OUTPUT) cout << "ERROR: " << (char*) e->geterror();
        return false;
    }
}

test/testpacketlib.h

deleted100644 → 0
+0 −47
Original line number Diff line number Diff line
/***************************************************************************
                          testpacketlib.h  -  description
                             -------------------
    begin                : Fri Feb 22 2002
    copyright            : (C) 2013 by Andrea Bulgarelli
    email                : bulgarelli@iasfbo.inaf.it
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#ifndef TESTPACKETLIB_H
#define TESTPACKETLIB_H
#include "PacketLibDefinition.h"
#include "Utility.h"
#include "InputPacketStreamFile.h"
#include "FileStreamPointer.h"
#include "PacketExceptionFileFormat.h"
#define OUTPUT 1

using namespace PacketLib;



class TestPacketLib {
public:
    TestPacketLib();
    ~TestPacketLib();


    bool TestSuiteType1(char* nameOfTestSuite, char* fileNameConfig, char** file);

private: // Private attributes

    InputPacketStreamFile* ips;
    char* initStructure(char* fileNameConfig);
    bool test(char* file);
    bool test2(char* file);
};

#endif

test/testsuite.cpp

deleted100644 → 0
+0 −292
Original line number Diff line number Diff line
/***************************************************************************
                          main.cpp  -  description
                             -------------------
    begin                : Fri Jan 18 11:12:04 CET 2002
    copyright            : (C) 2002 by Andrea Bulgarelli
    email                : bulgarelli@tesre.bo.cnr.it
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/


#include "PacketLibDefinition.h"
#include "Utility.h"
#include "InputPacketStreamFile.h"
#include "FileStreamPointer.h"
#include "PacketExceptionFileFormat.h"
#include "Output.h"
#include "OutputFile.h"
#include "MemoryBuffer.h"
#include "testpacketlib.h"
#include <unistd.h>
#include <time.h>

clock_t start;
clock_t end;
time_t timevar1;
time_t  timevar2;	
word ssc = 0;



Output* output;





void report() {
	cout << "-------------------------------------------" << endl;
  printf("Time0: %6.4f\n", (double)(clock() - (double)start) / CLOCKS_PER_SEC);
	//cout << "Time1: " << (double)(clock() - (double)start) / CLOCKS_PER_SEC << endl;
 	time(&timevar2);
	cout << "Time: " << timevar2-timevar1 << endl;
	cout << "Number of ByteStream without memory allocation (responsibility): " << ByteStream::count_object2 << " mem allocated: " << ByteStream::count_object2 * sizeof(ByteStream) << endl;
	cout << "Number of ByteStream deleted: " << ByteStream::count_object_deleted2 << " mem free: " << ByteStream::count_object_deleted2 * (long)sizeof(ByteStream) << endl;

	cout << "Number of ByteStream with memory allocation (responsibility): " << ByteStream::count_object << " mem allocated: " << ByteStream::count_object * sizeof(ByteStream) << endl;
	cout << "Number of ByteStream external deleted: " << ByteStream::count_object_deleted << " mem free: " << (long)(ByteStream::count_object_deleted * (long)sizeof(ByteStream)) << endl;


	cout << "Number of byte read by File class: " << File::byte_read <<  endl;
	cout << "Number of char read by File class: " << File::char_read <<  endl;
}




int main(int argc, char *argv[])
{
long count = 0;
long countbad = 0;


  /*
MemoryBuffer* mm = new MemoryBuffer();
	mm->loadBuffer("./CAL-DFE-TE/stream.stream");
	int dim = mm->getBufferDimension();
	for(int i=0; i<dim; i++)
		cout << mm->getbuffer() << endl;
  cout << "--" << endl;

	mm->saveBuffer("./CAL-DFE-TE/pippo.txt");

return 0;  	
*/
try {

InputPacketStreamFile* ips = new InputPacketStreamFile();
/*InputPacketStream* ips2 = new InputPacketStream();
InputPacketStream* ips3 = new InputPacketStream();
InputPacketStream* ips4 = new InputPacketStream();*/
//cout << Utility::extractFileName("/data1/archive/raw/science/0506/cer05060_011018.hrt");
//const char* c = new const char[1048576];
//sleep(1);
TestPacketLib* tpl = new TestPacketLib;
TestPacketLib* tp2 = new TestPacketLib;
TestPacketLib* tp3 = new TestPacketLib;
//TEST SUITE 1



const char** t1 = new const char*[6];
	t1[0]="./CAL-DFE-TE/data/10BURT.RAW";
  t1[1]="./CAL-DFE-TE/data/03bursts.raw";
	t1[2]="./CAL-DFE-TE/data/03grids.raw";
  t1[3]="./CAL-DFE-TE/data/06grids.raw";
	t1[4]="./CAL-DFE-TE/data/15grids.raw";
	t1[5]=0;

const char** t2 = new const char*[20];
  t2[0]="./CAL-CSIBarsTE/data/0508/cer05080_011018.hrt";
	t2[1]="./CAL-CSIBarsTE/data/0508/cer05089_011019.hrt";
  t2[2]="./CAL-CSIBarsTE/data/0508/cer05084_011018_.hrt";
	t2[3]="./CAL-CSIBarsTE/data/0508/cer05087_011019__.hrt";
	t2[4]="./CAL-CSIBarsTE/data/0500/cer05008_011018.hrt";
  t2[5]="./CAL-CSIBarsTE/data/0508/cer05085_011018_.hrt";
	t2[6]="./CAL-CSIBarsTE/data/0508/cer05081_011018.hrt";
	t2[7]="./CAL-CSIBarsTE/data/0508/cer05082_011018.hrt";
	t2[8]="./CAL-CSIBarsTE/data/0508/cer05083_011018.hrt";
	t2[9]="./CAL-CSIBarsTE/data/0508/cer05088_011019.hrt";

  t2[10]="./CAL-CSIBarsTE/data/0508/cer05080_011018.hrt";
	t2[11]="./CAL-CSIBarsTE/data/0508/cer05089_011019.hrt";
  t2[12]="./CAL-CSIBarsTE/data/0508/cer05084_011018_.hrt";
	t2[13]="./CAL-CSIBarsTE/data/0508/cer05087_011019__.hrt";
	t2[14]="./CAL-CSIBarsTE/data/0500/cer05008_011018.hrt";
	t2[15]="./CAL-CSIBarsTE/data/0508/cer05081_011018.hrt";
	t2[16]="./CAL-CSIBarsTE/data/0508/cer05082_011018.hrt";
	t2[17]="./CAL-CSIBarsTE/data/0508/cer05083_011018.hrt";
	t2[18]="./CAL-CSIBarsTE/data/0508/cer05088_011019.hrt";

	t2[10]=0;


  //t2[0]="./CAL-CSIBarsTE/data/0508/cer05084_011018_.hrt";
	//t2[1]=0;

const char** t3 = new const char*[6];
  t3[0]="./Proto-MCAL-TE/data/vme_burst_telemetry_file.raw";
	t3[1]="./Proto-MCAL-TE/data/ite00024_020326.prt";
  /*t3[2]="./data/0508/cer05084_011018_.hrt";
	t3[3]="./data/0508/cer05087_011019__.hrt";
	t2[4]="15grids.raw";*/
	t3[2]=0;


//goto notest;
  //sleep(1);

//delete[] c;
//goto notest;
  bool ret1, ret2, ret3;	
	if(!(ret1=tpl->TestSuiteType1((char*)"TEST SUITE 1",(char*)"./CAL-DFE-TE/stream.stream", (char**)t1)))
		cout << "TEST SUITE 1 FAILED" << endl;
  else
		cout << "TEST SUITE 1 OK" << endl;

	if(!(ret2=tp2->TestSuiteType1((char*)"TEST SUITE 2",(char*)"./CAL-CSIBarsTE/CAL-CSIBarsTE.stream", (char**)t2)))
		cout << "TEST SUITE 2 FAILED" << endl;
  else
		cout << "TEST SUITE 2 OK" << endl;		

	if(!(ret3=tp3->TestSuiteType1((char*)"TEST SUITE 3",(char*)"./Proto-MCAL-TE/stream.stream", (char**)t3)))
		cout << "TEST SUITE 3 FAILED" << endl;
  else
		cout << "TEST SUITE 3 OK" << endl;		

//	delete tpl;
	//delete tp2;
	//delete tp3;
 	if(!ret1)
		cout << "TEST SUITE 1 FAILED" << endl;
  else
		cout << "TEST SUITE 1 OK" << endl;
 	if(!ret2)
		cout << "TEST SUITE 2 FAILED" << endl;
  else
		cout << "TEST SUITE 2 OK" << endl;
 	if(!ret3)
		cout << "TEST SUITE 3 FAILED" << endl;
  else
		cout << "TEST SUITE 3 OK" << endl;


  report();
// 	tm_int=localtime(&timevar1);


delete[] t1;
delete[] t2;
delete[] t3;

return 0;

//notest:

	ips->setFileNameConfig("./CAL-DFE-TE/stream.stream");
  ips->createStreamStructure();
	ips->setFileNameStream((char*)"./CAL-DFE-TE/data/03bursts.raw");
	ips->openInputStream();

	while(!ips->isInputStreamEOF()) {
		Packet* p = ips->getPacketFromStream();
   	if(p != 0)
			count++;
		else
			if(!ips->isInputStreamEOF())
				countbad++;
	}
	cout << "Bad: " << countbad << endl;
	cout << "OK: " << count << endl;	
	delete ips;

delete tpl;
delete[] t1;
delete[] t2;
delete[] t3;

  cout << "end of program" << endl;

return 0;
//notest:

{
	char in;
	int index = 0;
	char* c;
  char** cc;
	InputPacketStreamFile ips("./CAL-DFE-TE/stream.stream", "/home/agile/data-testsuite/CAL-DFE-TE/10BURT.RAW", 0L) ;
  //string* fileNameConfig = new string();
  ips.createStreamStructure();
	ips.openInputStream();
  //FileStream* f = ips.inputStream;
	//cout << "EOF: " << f->isEOF() << endl;
	ips.freeRun();
	while(in != EOF) {		
		Packet* p = ips.getPacketFromFileStreamPointer(index);
		FileStreamPointer* fsp = ips.getFileStreamPointer(index);
		cout << "-------------------------------------------" << endl;
		cout << "Position in file byte: " << fsp->pointerStart << endl;
    cout << "Dimension of packet: " << p->getDimension() << endl;
			cout << (p->getName()) << endl;
			cout << "Prefix hex:" << endl;
			c = p->prefix->printStreamInHexadecimal();		
			cout << c << endl;			
			cout << "Header hex:" << endl;			
			c = p->header->getByteStream()->printStreamInHexadecimal();		
			cout << c << endl;
			cout << p->header->getByteStream()->getValue(1,2) << endl;
			cout << "Header fields:" << endl;
			cc = p->header->printValue();
			for(int i=0; cc[i] != 0; i++)
				cout << cc[i] << endl;

			cout << "Data field header hex:" << endl;
			c =  p->dataField->dataFieldHeader->getByteStream()->printStreamInHexadecimal();
			cout << c << endl;
			cout << "Data field header fields:" << endl;
			cc =  p->dataField->dataFieldHeader->printValue();
			for(int i=0; cc[i] != 0; i++)
				cout << cc[i] << endl;	

			//cout << "Source Data field hex:" << endl;
			//c =  p->dataField->sourceDataField->stream->printStreamInHexadecimal();
			//cout << c << endl;
			/*cout << "Source Data field fields:" << endl;
			cc =  p->dataField->sourceDataField->printValue();
			for(int i=0; cc[i] != 0; i++)
				cout << cc[i] << endl;	
        */

		cin >> &in;
		switch(in) {
    case 'n': index++; break;
		case 'p': index--; break;
		case 'q': return 0; break;
		}

			//	index++;
		cout << "INDEX: " << index << endl;
	}
  PRINTDEBUG("stream closed");
	return 0;
}
}
catch(PacketExceptionIO* e) {
	cout << e->geterror();
}
catch(PacketExceptionFileFormat* e) {
	cout << e->geterror();
}

cout << "End";
exit(0);
}