spinn-off: collecting some frequently used bits to support unit testing

This commit is contained in:
Fischlurch 2009-05-30 23:49:02 +02:00
parent 20a1268683
commit e0ad992f30
7 changed files with 254 additions and 0 deletions

1
.gitignore vendored
View file

@ -16,6 +16,7 @@ scripts/*
configure
config.log
aclocal.m4
autom4te.cache
semantic.cache
wiki/backups/*
doc/devel/draw/*.png

View file

@ -43,6 +43,7 @@ liblumiera_la_SOURCES = \
$(liblumiera_la_srcdir)/query.cpp \
$(liblumiera_la_srcdir)/streamtype.cpp \
$(liblumiera_la_srcdir)/test/testoption.cpp \
$(liblumiera_la_srcdir)/test/test-helper.cpp \
$(liblumiera_la_srcdir)/test/suite.cpp \
$(liblumiera_la_srcdir)/cmdline.cpp \
$(liblumiera_la_srcdir)/logging.cpp
@ -92,5 +93,6 @@ noinst_HEADERS += \
$(liblumiera_la_srcdir)/test/mockinjector.hpp \
$(liblumiera_la_srcdir)/test/suite.hpp \
$(liblumiera_la_srcdir)/test/testoption.hpp \
$(liblumiera_la_srcdir)/test/test-helper.hpp \
$(liblumiera_la_srcdir)/test/run.hpp

View file

@ -0,0 +1,46 @@
/*
Test-Helper - collection of functions supporting unit testing
Copyright (C) Lumiera.org
2009, Hermann Vosseler <Ichthyostega@web.de>
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.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
* *****************************************************/
#include "lib/test/test-helper.hpp"
//#include "lib/error.hpp"
#include <boost/format.hpp>
#include <typeinfo>
using boost::format;
using boost::str;
namespace lib {
namespace test{
string
showSizeof (size_t siz, Symbol name)
{
static format fmt ("sizeof( %s ) %|30t|= %3d");
return str (fmt % (name? name:"?") % siz);
}
}} // namespace lib::test

View file

@ -0,0 +1,101 @@
/*
TEST-HELPER.hpp - collection of functions supporting unit testing
Copyright (C) Lumiera.org
2009, Hermann Vosseler <Ichthyostega@web.de>
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.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#ifndef LIB_TEST_TESTHELPER_H
#define LIB_TEST_TESTHELPER_H
#include "include/symbol.hpp"
#include <typeinfo>
#include <string>
namespace lib {
namespace test{
using lumiera::Symbol;
using std::string;
/** get a sensible display for a type or object
* @param obj object of the type in question
* @param name optional name to be used literally
* @return either the literal name without any further magic,
* or the result of compile-time or run time
* type identification as implemented by the compiler.
*/
template<typename T>
const char*
showType (T const& obj, Symbol name=0)
{
return name? name : typeid(obj).name();
}
/** get a sensible display for a type
* @param name optional name to be used literally
* @return either the literal name without any further magic,
* or the result of compile-time or run time
* type identification as implemented by the compiler.
*/
template<typename T>
const char*
showType (Symbol name=0)
{
return name? name : typeid(T).name();
}
/** for printing sizeof().
* prints the given size and name litterally, without any further magic */
string
showSizeof (size_t siz, Symbol name);
/** for printing sizeof(), trying to figure out the type name automatically */
template<typename T>
string
showSizeof(Symbol name=0)
{
return showSizeof (sizeof (T), showType<T> (name));
}
template<typename T>
string
showSizeof(T const& obj, Symbol name=0)
{
return showSizeof (sizeof (obj), showType (obj,name));
}
template<typename T>
string
showSizeof(T *obj, Symbol name=0)
{
return obj? showSizeof (*obj, name)
: showSizeof<T> (name);
}
}} // namespace lib::test
#endif

View file

@ -366,6 +366,12 @@ return: 0
END
TEST "Test support functions" TestHelper_test <<END
out: Displaying types and sizes....
out: --> Testgroup=ALL
END
TEST "TestOption_test" TestOption_test <<END
out: Testing invocation with cmdline: ...
out: --> Testgroup=ALL

View file

@ -69,6 +69,7 @@ test_lib_SOURCES = \
$(testlib_srcdir)/thread-wrapper-join-test.cpp \
$(testlib_srcdir)/test/cmdlinewrappertest.cpp \
$(testlib_srcdir)/test/testoptiontest.cpp \
$(testlib_srcdir)/test/test-helper-test.cpp \
$(testlib_srcdir)/vectortransfertest.cpp \
$(testlib_srcdir)/visitingtoolconcept.cpp \
$(testlib_srcdir)/visitingtoolextendedtest.cpp \

View file

@ -0,0 +1,97 @@
/*
TestHelper(Test) - validate the unittest support functions
Copyright (C) Lumiera.org
2008, Hermann Vosseler <Ichthyostega@web.de>
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.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
* *****************************************************/
#include "lib/test/run.hpp"
#include "lib/test/test-helper.hpp"
#include <iostream>
using std::cout;
using std::endl;
namespace lib {
namespace test{
namespace test{
template<class T>
class Wrmrmpft
{
T tt_;
};
struct Murpf { };
/*************************************************
* verifies the proper working of helper functions
* frequently used within the Lumiera testsuite.
* @see test-helper.hpp
*/
class TestHelper_test : public Test
{
void
run (Arg)
{
checkTypeDisplay();
}
/** @test prints "sizeof()" including some type name. */
void
checkTypeDisplay ()
{
std::cout << "Displaying types and sizes....\n";
typedef Wrmrmpft<Murpf> Wrmpf1;
typedef Wrmrmpft<char[2]> Wrmpf2;
typedef Wrmrmpft<char[3]> Wrmpf3;
Wrmpf1 rmpf1;
Wrmpf2 rmpf2;
Wrmpf3 rmpf3;
Murpf murpf;
ASSERT (1 == sizeof (rmpf1));
ASSERT (2 == sizeof (rmpf2));
ASSERT (3 == sizeof (rmpf3));
cout << showSizeof(42,"theUniverse") << endl;
cout << showSizeof<char>("just a char") << endl;
cout << showSizeof(murpf) << endl;
cout << showSizeof(rmpf1) << endl;
cout << showSizeof(rmpf2) << endl;
cout << showSizeof<Wrmpf3>() << endl;
Wrmpf1 *p1 = &rmpf1;
Wrmpf1 *p2 = 0;
cout << showSizeof(p1) << endl;
cout << showSizeof(p2) << endl;
}
};
LAUNCHER (TestHelper_test, "unit common");
}}} // namespace lib::test::test