From 65e67988523689d217ff13213ce079f84f81bdee Mon Sep 17 00:00:00 2001 From: prascle Date: Mon, 3 Oct 2005 13:14:41 +0000 Subject: [PATCH] PR: Testing: Add Unit Tests based on cppunit prerequisite --- src/Makefile.in | 5 + src/SALOMELocalTrace/Test/Makefile.in | 51 ++++++ .../Test/SALOMELocalTraceTest.cxx | 172 ++++++++++++++++++ .../Test/SALOMELocalTraceTest.hxx | 25 +++ src/SALOMETraceCollector/Test/Makefile.in | 50 +++++ .../Test/SALOMETraceCollectorTest.cxx | 94 ++++++++++ .../Test/SALOMETraceCollectorTest.hxx | 20 ++ src/UnitTests/Makefile.in | 61 +++++++ src/UnitTests/UnitTests.cxx | 58 ++++++ src/Utils/Test/Makefile.in | 50 +++++ src/Utils/Test/UtilsTest.cxx | 101 ++++++++++ src/Utils/Test/UtilsTest.hxx | 23 +++ 12 files changed, 710 insertions(+) create mode 100644 src/SALOMELocalTrace/Test/Makefile.in create mode 100644 src/SALOMELocalTrace/Test/SALOMELocalTraceTest.cxx create mode 100644 src/SALOMELocalTrace/Test/SALOMELocalTraceTest.hxx create mode 100644 src/SALOMETraceCollector/Test/Makefile.in create mode 100644 src/SALOMETraceCollector/Test/SALOMETraceCollectorTest.cxx create mode 100644 src/SALOMETraceCollector/Test/SALOMETraceCollectorTest.hxx create mode 100644 src/UnitTests/Makefile.in create mode 100644 src/UnitTests/UnitTests.cxx create mode 100644 src/Utils/Test/Makefile.in create mode 100644 src/Utils/Test/UtilsTest.cxx create mode 100644 src/Utils/Test/UtilsTest.hxx diff --git a/src/Makefile.in b/src/Makefile.in index 5b5902e35..a826da10d 100644 --- a/src/Makefile.in +++ b/src/Makefile.in @@ -60,6 +60,11 @@ SUBDIRS = \ KERNEL_PY \ ModuleGenerator \ Communication \ + \ + SALOMELocalTrace/Test \ + SALOMETraceCollector/Test \ + Utils/Test \ + UnitTests ifeq (@mpi_ok@,yes) SUBDIRS+= MPIContainer TestMPIContainer diff --git a/src/SALOMELocalTrace/Test/Makefile.in b/src/SALOMELocalTrace/Test/Makefile.in new file mode 100644 index 000000000..4bbc1d204 --- /dev/null +++ b/src/SALOMELocalTrace/Test/Makefile.in @@ -0,0 +1,51 @@ +# SALOMELocalTrace : log on local machine +# +# Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN, +# CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# +# See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org +# +# +# +# File : Makefile.in +# Author : Paul RASCLE (EDF) +# Module : SALOME +# $Header$ + +top_srcdir=@top_srcdir@ +top_builddir=../../.. +srcdir=@srcdir@ +VPATH=.:@srcdir@:@top_srcdir@/idl + + +@COMMENCE@ + +# header files +EXPORT_HEADERS= SALOMELocalTraceTest.hxx + +EXPORT_PYSCRIPTS = + +# Libraries targets + +LIB = libSALOMELocalTraceTest.la +LIB_SRC = SALOMELocalTraceTest.cxx + +LIBS= @LIBS@ +LDFLAGS+= -lSALOMELocalTrace + + +@CONCLUDE@ diff --git a/src/SALOMELocalTrace/Test/SALOMELocalTraceTest.cxx b/src/SALOMELocalTrace/Test/SALOMELocalTraceTest.cxx new file mode 100644 index 000000000..1697ac576 --- /dev/null +++ b/src/SALOMELocalTrace/Test/SALOMELocalTraceTest.cxx @@ -0,0 +1,172 @@ + +#include "SALOMELocalTraceTest.hxx" + +#include +#include +#include +#include +#include "LocalTraceBufferPool.hxx" +#include "utilities.h" + +using namespace std; + +// --- Registers the fixture into the 'registry' + +CPPUNIT_TEST_SUITE_REGISTRATION( SALOMELocalTraceTest ); + +// ============================================================================ +/*! + * + */ +// ============================================================================ + +void +SALOMELocalTraceTest::setUp() +{ +} + +// ============================================================================ +/*! + * + */ +// ============================================================================ + +void +SALOMELocalTraceTest::tearDown() +{ +} + +#define TRACEFILE "/tmp/traceUnitTest.log" + +// ============================================================================ +/*! + * Open and close a trace on a file, test singleton + */ +// ============================================================================ + +void +SALOMELocalTraceTest::testSingletonBufferPool() +{ + // --- trace on file + char *theFileName = TRACEFILE; + + string s = "file:"; + s += theFileName; + CPPUNIT_ASSERT(! setenv("SALOME_trace",s.c_str(),1)); // 1: overwrite + + ofstream traceFile; + traceFile.open(theFileName, ios::out | ios::trunc); + CPPUNIT_ASSERT(traceFile); // file created empty, then closed + traceFile.close(); + + LocalTraceBufferPool* bp1 = LocalTraceBufferPool::instance(); + CPPUNIT_ASSERT(bp1); + LocalTraceBufferPool* bp2 = LocalTraceBufferPool::instance(); + CPPUNIT_ASSERT(bp1 == bp2); + bp1->deleteInstance(bp1); +} + + + +#define NUM_THREADS 2 +#define NUM_MESSAGES 5 +void *PrintHello(void *threadid); + +// ============================================================================ +/*! + * open a trace on console, multithread writing on file, close + */ +// ============================================================================ + +void +SALOMELocalTraceTest::testLoadBufferPoolLocal() +{ + string s = "local"; + CPPUNIT_ASSERT(! setenv("SALOME_trace",s.c_str(),1)); // 1: overwrite + + // --- numThread thread creation for trace generation. + int numThread = 2; + pthread_t threads[numThread]; + int rc, t; + for(t=0;tdeleteInstance(bp1); +} + +// ============================================================================ +/*! + * open a trace on a file, multithread writing on file, close + */ +// ============================================================================ + +void +SALOMELocalTraceTest::testLoadBufferPoolFile() +{ + char *theFileName = TRACEFILE; + + string s = "file:"; + s += theFileName; + //s = "local"; + CPPUNIT_ASSERT(! setenv("SALOME_trace",s.c_str(),1)); // 1: overwrite + + ofstream traceFile; + traceFile.open(theFileName, ios::out | ios::trunc); + CPPUNIT_ASSERT(traceFile); // file created empty, then closed + traceFile.close(); + + // --- NUM_THREADS thread creation for trace generation. + + pthread_t threads[NUM_THREADS]; + int rc, t; + for(t=0;tdeleteInstance(bp1); +} + +// ============================================================================ +/*! + * NUM_THREAD are created with function PrintHello, + * which produces NUM_MESSAGES traces. + */ +// ============================================================================ + +void *PrintHello(void *threadid) +{ + int id_thread = (int)threadid; + for (int i=0; i + +class SALOMELocalTraceTest : public CppUnit::TestFixture +{ + CPPUNIT_TEST_SUITE( SALOMELocalTraceTest ); + CPPUNIT_TEST( testSingletonBufferPool ); + CPPUNIT_TEST( testLoadBufferPoolLocal ); + CPPUNIT_TEST( testLoadBufferPoolFile ); + CPPUNIT_TEST( testLoadBufferPoolFile ); + CPPUNIT_TEST_SUITE_END(); + +public: + void setUp(); + void tearDown(); + + void testSingletonBufferPool(); + void testLoadBufferPoolLocal(); + void testLoadBufferPoolFile(); +}; + +#endif diff --git a/src/SALOMETraceCollector/Test/Makefile.in b/src/SALOMETraceCollector/Test/Makefile.in new file mode 100644 index 000000000..ffd7f8092 --- /dev/null +++ b/src/SALOMETraceCollector/Test/Makefile.in @@ -0,0 +1,50 @@ +# SALOMELocalTrace : log on local machine +# +# Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN, +# CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# +# See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org +# +# +# +# File : Makefile.in +# Author : Paul RASCLE (EDF) +# Module : SALOME +# $Header$ + +top_srcdir=@top_srcdir@ +top_builddir=../../.. +srcdir=@srcdir@ +VPATH=.:@srcdir@:@top_srcdir@/idl + + +@COMMENCE@ + +# header files +EXPORT_HEADERS= SALOMETraceCollectorTest.hxx + +EXPORT_PYSCRIPTS = + +# Libraries targets + +LIB = libSALOMETraceCollectorTest.la +LIB_SRC = SALOMETraceCollectorTest.cxx + +LIBS= @LIBS@ +LDFLAGS+= + +@CONCLUDE@ diff --git a/src/SALOMETraceCollector/Test/SALOMETraceCollectorTest.cxx b/src/SALOMETraceCollector/Test/SALOMETraceCollectorTest.cxx new file mode 100644 index 000000000..6aed2110b --- /dev/null +++ b/src/SALOMETraceCollector/Test/SALOMETraceCollectorTest.cxx @@ -0,0 +1,94 @@ + +#include "SALOMETraceCollectorTest.hxx" + +#include +#include +#include +#include +#include "LocalTraceBufferPool.hxx" +#include "utilities.h" + +using namespace std; + +// --- Registers the fixture into the 'registry' + +CPPUNIT_TEST_SUITE_REGISTRATION( SALOMETraceCollectorTest ); + +// ============================================================================ +/*! + * + */ +// ============================================================================ + +void +SALOMETraceCollectorTest::setUp() +{ +} + +// ============================================================================ +/*! + * + */ +// ============================================================================ + +void +SALOMETraceCollectorTest::tearDown() +{ +} + +#define NUM_THREADS 20 +#define NUM_MESSAGES 20 +void *PrintHello(void *threadid); + +// ============================================================================ +/*! + * open a trace on a CORBA, multithread writing on file, close + */ +// ============================================================================ + +void +SALOMETraceCollectorTest::testLoadBufferPoolCORBA() +{ + string s = "with_logger"; + CPPUNIT_ASSERT(! setenv("SALOME_trace",s.c_str(),1)); // 1: overwrite + + // --- NUM_THREADS thread creation for trace generation. + + pthread_t threads[NUM_THREADS]; + int rc, t; + for(t=0;tdeleteInstance(bp1); +} + +// ============================================================================ +/*! + * NUM_THREAD are created with function PrintHello, + * which produces NUM_MESSAGES traces. + */ +// ============================================================================ + +void *PrintHello(void *threadid) +{ + int id_thread = (int)threadid; + for (int i=0; i + +class SALOMETraceCollectorTest : public CppUnit::TestFixture +{ + CPPUNIT_TEST_SUITE( SALOMETraceCollectorTest ); + CPPUNIT_TEST( testLoadBufferPoolCORBA ); + CPPUNIT_TEST_SUITE_END(); + +public: + void setUp(); + void tearDown(); + + void testLoadBufferPoolCORBA(); +}; + +#endif diff --git a/src/UnitTests/Makefile.in b/src/UnitTests/Makefile.in new file mode 100644 index 000000000..f31b0bb35 --- /dev/null +++ b/src/UnitTests/Makefile.in @@ -0,0 +1,61 @@ +# +# Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN, +# CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# +# See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org +# +# +# +# File : Makefile.in +# Author : Paul RASCLE (EDF) +# Module : SALOME +# $Header$ + +top_srcdir=@top_srcdir@ +top_builddir=../.. +srcdir=@srcdir@ +VPATH=.:@srcdir@:@top_srcdir@/idl + + +@COMMENCE@ + +# header files +EXPORT_HEADERS= + +EXPORT_PYSCRIPTS = + +# Libraries targets + +LIB = +LIB_SRC = + +# Executables targets + +BIN = UnitTests +BIN_SRC = + + +LIBS= @LIBS@ +LDFLAGSFORBIN+= \ + -lSALOMELocalTraceTest -lSALOMELocalTrace -lSALOMEBasics \ + -lSALOMETraceCollectorTest \ + -lUtilsTest -lOpUtil \ + -lcppunit +# -lefence + + +@CONCLUDE@ diff --git a/src/UnitTests/UnitTests.cxx b/src/UnitTests/UnitTests.cxx new file mode 100644 index 000000000..0c691449b --- /dev/null +++ b/src/UnitTests/UnitTests.cxx @@ -0,0 +1,58 @@ + +#include +#include +#include +#include +#include +#include +//#include +#include +#include + +#include "SALOMELocalTraceTest.hxx" +#include "SALOMETraceCollectorTest.hxx" + +#include "UtilsTest.hxx" + +int main(int argc, char* argv[]) +{ + // Create the event manager and test controller + CPPUNIT_NS::TestResult controller; + + // Add a listener that colllects test result + CPPUNIT_NS::TestResultCollector result; + controller.addListener( &result ); + + // Add a listener that print dots as test run. +#ifdef WIN32 + CPPUNIT_NS::TextTestProgressListener progress; +#else + CPPUNIT_NS::BriefTestProgressListener progress; +#endif + controller.addListener( &progress ); + + // Get the top level suite from the registry + //CppUnit::Test *suite =CppUnit::TestFactoryRegistry::getRegistry().makeTest(); + CPPUNIT_NS::Test *suite = + CPPUNIT_NS::TestFactoryRegistry::getRegistry().makeTest(); + + // Adds the test to the list of test to run + //CppUnit::TextUi::TestRunner runner; + CPPUNIT_NS::TestRunner runner; + runner.addTest( suite ); + runner.run( controller); + // Print test in a compiler compatible format. + CPPUNIT_NS::CompilerOutputter outputter( &result, std::cerr ); + outputter.write(); + + // Change the default outputter to a compiler error format outputter + // runner.setOutputter( new CppUnit::CompilerOutputter( &runner.result(), + // std::cerr ) ); + + // Run the tests. + //bool wasSucessful = runner.run(); + bool wasSucessful = result.wasSuccessful(); + + // Return error code 1 if the one of test failed. + return wasSucessful ? 0 : 1; +} diff --git a/src/Utils/Test/Makefile.in b/src/Utils/Test/Makefile.in new file mode 100644 index 000000000..92ef4bb10 --- /dev/null +++ b/src/Utils/Test/Makefile.in @@ -0,0 +1,50 @@ +# SALOMELocalTrace : log on local machine +# +# Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN, +# CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# +# See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org +# +# +# +# File : Makefile.in +# Author : Paul RASCLE (EDF) +# Module : SALOME +# $Header$ + +top_srcdir=@top_srcdir@ +top_builddir=../../.. +srcdir=@srcdir@ +VPATH=.:@srcdir@:@top_srcdir@/idl + + +@COMMENCE@ + +# header files +EXPORT_HEADERS= UtilsTest.hxx + +EXPORT_PYSCRIPTS = + +# Libraries targets + +LIB = libUtilsTest.la +LIB_SRC = UtilsTest.cxx + +LIBS= @LIBS@ +LDFLAGS+= + +@CONCLUDE@ diff --git a/src/Utils/Test/UtilsTest.cxx b/src/Utils/Test/UtilsTest.cxx new file mode 100644 index 000000000..fd6372791 --- /dev/null +++ b/src/Utils/Test/UtilsTest.cxx @@ -0,0 +1,101 @@ + +#include "UtilsTest.hxx" + +#include +#include +#include +#include +#include "Utils_SALOME_Exception.hxx" +#include "utilities.h" + +using namespace std; + +// --- Registers the fixture into the 'registry' + +CPPUNIT_TEST_SUITE_REGISTRATION( UtilsTest ); + +#define TRACEFILE "/tmp/traceUnitTest.log" + +// ============================================================================ +/*! + * Set Trace mecanism + * - delete preexisting trace classes if any + * - set trace on file + */ +// ============================================================================ + +void +UtilsTest::setUp() +{ + LocalTraceBufferPool* bp1 = LocalTraceBufferPool::instance(); + CPPUNIT_ASSERT(bp1); + bp1->deleteInstance(bp1); + + // --- trace on file + char *theFileName = TRACEFILE; + + string s = "file:"; + s += theFileName; + CPPUNIT_ASSERT(! setenv("SALOME_trace",s.c_str(),1)); // 1: overwrite + + ofstream traceFile; + traceFile.open(theFileName, ios::out | ios::trunc); + CPPUNIT_ASSERT(traceFile); // file created empty, then closed + traceFile.close(); + + bp1 = LocalTraceBufferPool::instance(); + CPPUNIT_ASSERT(bp1); +} + +// ============================================================================ +/*! + * - delete trace classes + */ +// ============================================================================ + +void +UtilsTest::tearDown() +{ + LocalTraceBufferPool* bp1 = LocalTraceBufferPool::instance(); + CPPUNIT_ASSERT(bp1); + bp1->deleteInstance(bp1); +} + +int genExcept() +{ + throw SALOME_Exception("a message"); +}; + +// ============================================================================ +/*! + * Check basic SALOME_exception mecanism + */ +// ============================================================================ + +void +UtilsTest::testSALOME_ExceptionThrow() +{ + CPPUNIT_ASSERT_THROW(genExcept(), SALOME_Exception); +} + +// ============================================================================ +/*! + * Check message on catch + */ +// ============================================================================ + +void +UtilsTest::testSALOME_ExceptionMessage() +{ +#define EXAMPLE_EXCEPTION_MESSAGE "something for the end user" + try + { + throw SALOME_Exception(EXAMPLE_EXCEPTION_MESSAGE); + } + catch (const SALOME_Exception &ex) + { + string expectedMessage = EXAMPLE_EXCEPTION_MESSAGE; + string actualMessage = ex.what(); + CPPUNIT_ASSERT(actualMessage.find(expectedMessage) != string::npos); + } +} diff --git a/src/Utils/Test/UtilsTest.hxx b/src/Utils/Test/UtilsTest.hxx new file mode 100644 index 000000000..ffb23e190 --- /dev/null +++ b/src/Utils/Test/UtilsTest.hxx @@ -0,0 +1,23 @@ + +#ifndef _UTILSTEST_HXX_ +#define _UTILSTEST_HXX_ + +#include + +class UtilsTest : public CppUnit::TestFixture +{ + CPPUNIT_TEST_SUITE( UtilsTest ); + CPPUNIT_TEST( testSALOME_ExceptionThrow ); + CPPUNIT_TEST( testSALOME_ExceptionMessage ); + CPPUNIT_TEST_SUITE_END(); + +public: + + void setUp(); + void tearDown(); + + void testSALOME_ExceptionThrow(); + void testSALOME_ExceptionMessage(); +}; + +#endif -- 2.39.2