Commit 763ece9b authored by Andrea Zoli's avatar Andrea Zoli
Browse files

Using CppUnit instead of boost unit test.

parent f8883db8
Loading
Loading
Loading
Loading
+10 −8
Original line number Diff line number Diff line
@@ -60,7 +60,7 @@ CC = gcc
CXX      = g++
#Insert the optional parameter to the compiler. The CFLAGS could be changed externally by the user
#- g3
CFLAGS   = -O2 -O0 -m64 -fPIC
CFLAGS   = -O2 -O0 -m64 -fPIC -g
#-O2 -O0 -g3
#Set INCPATH to add the inclusion paths
INCPATH = -I ./include
@@ -76,10 +76,9 @@ LIBS = -lstdc++
ifeq ($(SYSTEM), QNX)
	LIBS += -lsocket
endif
BOOST_LIBNAME = -lboost_unit_test_framework
ifneq (, $(findstring apple, $(SYSTEM)))
 	# Do apple things
	CPPFLAGS += -I$(LOCAL)/include
	CPPFLAGS += -I$(LOCAL)/include/cppunit
	LIBS += -L$(LOCAL)/lib
endif 

@@ -132,10 +131,7 @@ $(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 $@ $< $(BOOST_LIBNAME) -Llib -lpacket $(LIBS)
	$(CC) $(CPPFLAGS) $(ALL_CFLAGS) -c $< -o $@ -I /usr/include/cppunit

%.o : %.cpp
	$(CC) $(CPPFLAGS) $(ALL_CFLAGS) -c $< -o $(OBJECTS_DIR)/$@
@@ -158,7 +154,13 @@ 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/testInputPacketStreamFile
tests: test/runtests

TESTOBJS = test/InputPacketStreamFileTest.o test/runtests.o

test/runtests: $(TESTOBJS) lib
	$(CC) $(CPPFLAGS) $(ALL_CFLAGS) -o $@ $(TESTOBJS) -Llib -lpacket $(LIBS) -lcppunit


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

//#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>
#include <cppunit/TestCaller.h>
#include <cppunit/TestSuite.h>
#include "InputPacketStreamFileTest.h"

const char config[] = "p3901.stream";
const char file[] = "VC-01.P-033300.pkt";
const int good = 828;
const int bad = 5;

const int NFILES = 1;
using namespace CppUnit;

const char* files[NFILES] = { "VC-01.P-033300.pkt" };

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

BOOST_AUTO_TEST_CASE(input_packet_stream_file)
void InputPacketStreamFileTest::setUp()
{
	ips = new PacketLib::InputPacketStreamFile;
}
void InputPacketStreamFileTest::tearDown()
{
	PacketLib::InputPacketStreamFile ips;
	delete ips;
}

void InputPacketStreamFileTest::testCreateStreamStructure()
{
	// loading an invalid file should raise an exception
	ips.setFileNameConfig((char*)"notafile");
	BOOST_CHECK_THROW(ips.createStreamStructure(), PacketLib::PacketExceptionIO*);
	ips->setFileNameConfig((char*)"notafile");
	CPPUNIT_ASSERT_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());
	ips->setFileNameConfig((char*)config);
	CPPUNIT_ASSERT_NO_THROW(ips->createStreamStructure());
}

	for(int i=0; i<NFILES; i++)
void InputPacketStreamFileTest::testOpenInputStream()
{
        ips.setInitialPosition(0L);
/*	// loading an invalid file should raise an exception
	ips->setFileNameConfig((char*)"notafile");
	CPPUNIT_ASSERT_THROW(ips->createStreamStructure(), PacketLib::PacketExceptionIO*);

	// loading a valid config file should not raise an exception
	ips->setFileNameConfig((char*)config);
	CPPUNIT_ASSERT_NO_THROW(ips->createStreamStructure());*/

    ips->setInitialPosition(0L);

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

	// testing stream EOF should not raise an exception
	bool eof;
		BOOST_CHECK_NO_THROW(eof = ips.isInputStreamEOF());
	CPPUNIT_ASSERT_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());
	CPPUNIT_ASSERT_NO_THROW(p = ips->getPacketFromStream());

        long count = 0;
        long countbad = 0;
    int count = 0;
    int countbad = 0;

	while(!eof) {
		if(p != 0) {
@@ -71,55 +80,29 @@ BOOST_AUTO_TEST_CASE(input_packet_stream_file)
			countbad++;

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

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

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

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

BOOST_AUTO_TEST_CASE(input_packet_stream_file_freerun)
{
	PacketLib::InputPacketStreamFile ips;
/*Test* InputPacketStreamFileTest::suite() {
	TestSuite* testSuite = new TestSuite("InputPacketStreamFileTest");
  
	// loading an invalid file should raise an exception
	ips.setFileNameConfig((char*)"notafile");
	BOOST_CHECK_THROW(ips.createStreamStructure(), PacketLib::PacketExceptionIO);
	// add the tests
	testSuite->addTest( new TestCaller<InputPacketStreamFileTest>(
							"testCreateStreamStructure",
							&InputPacketStreamFileTest::testCreateStreamStructure) );

	// 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);
		}
	}
}
	testSuite->addTest ( new TestCaller<InputPacketStreamFileTest>(
							"testOpenInputStream",
							&InputPacketStreamFileTest::testOpenInputStream) );
	return testSuite;
}*/
+42 −0
Original line number Diff line number Diff line
/***************************************************************************
 *     begin                : Oct 08 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                  *
*                                                                         *
***************************************************************************/

#ifndef INPUTPACKETSTREAMFILE_H
#define INPUTPACKETSTREAMFILE_H

#include <cppunit/TestFixture.h>
#include <cppunit/extensions/HelperMacros.h>
#include "InputPacketStreamFile.h"

class InputPacketStreamFileTest : public CppUnit::TestFixture {

	CPPUNIT_TEST_SUITE(InputPacketStreamFileTest);
	CPPUNIT_TEST(testCreateStreamStructure);
	CPPUNIT_TEST(testOpenInputStream);
	CPPUNIT_TEST_SUITE_END(); 

public: 
    void setUp();
    void tearDown();

protected:
	void testCreateStreamStructure();
	void testOpenInputStream();

private:
	PacketLib::InputPacketStreamFile* ips;
};

#endif

test/runtests.cpp

0 → 100644
+48 −0
Original line number Diff line number Diff line
#include <cppunit/TestRunner.h>
#include <cppunit/TestResult.h>
#include <cppunit/TestResultCollector.h>
#include <cppunit/BriefTestProgressListener.h>
#include <cppunit/CompilerOutputter.h>
#include <cppunit/XmlOutputter.h>
#include "InputPacketStreamFileTest.h"

/*int main(int argc, char* argv[]) {
	CppUnit::TestRunner runner;
	runner.addTest("InputPacketStreamFile", InputPacketStreamFileTest::suite());
	runner.run(argc, argv);

	return 0;
}*/
using namespace CppUnit;

CPPUNIT_TEST_SUITE_REGISTRATION(InputPacketStreamFileTest);

int main(int argc, char* argv[])
{
	TestResult testresult;

	// register listener for collecting the test-results
	TestResultCollector collectedresults;
	testresult.addListener(&collectedresults);

	// register listener for per-test progress output
	BriefTestProgressListener progress;
	testresult.addListener(&progress);

	// insert test-suite at test-runner by registry
	TestRunner testrunner;
	testrunner.addTest(TestFactoryRegistry::getRegistry().makeTest());
	testrunner.run(testresult);

	// output results in compiler-format
	CompilerOutputter compileroutputter(&collectedresults, std::cerr);
	compileroutputter.write();

	// Output XML for Jenkins CPPunit plugin
	ofstream xmlFileOut("cppTestBasicMathResults.xml");
	XmlOutputter xmlOut(&collectedresults, xmlFileOut);
	xmlOut.write();
	
	// return 0 if tests were successful
	return collectedresults.wasSuccessful() ? 0 : 1;
}