2009-05-30 23:49:02 +02:00
|
|
|
/*
|
|
|
|
|
TEST-HELPER.hpp - collection of functions supporting unit testing
|
2010-12-17 23:28:49 +01:00
|
|
|
|
2009-05-30 23:49:02 +02:00
|
|
|
Copyright (C) Lumiera.org
|
|
|
|
|
2009, Hermann Vosseler <Ichthyostega@web.de>
|
2010-12-17 23:28:49 +01:00
|
|
|
|
2009-05-30 23:49:02 +02:00
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
|
modify it under the terms of the GNU General Public License as
|
2010-12-17 23:28:49 +01:00
|
|
|
published by the Free Software Foundation; either version 2 of
|
|
|
|
|
the License, or (at your option) any later version.
|
|
|
|
|
|
2009-05-30 23:49:02 +02:00
|
|
|
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.
|
2010-12-17 23:28:49 +01:00
|
|
|
|
2009-05-30 23:49:02 +02:00
|
|
|
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.
|
2010-12-17 23:28:49 +01:00
|
|
|
|
2009-05-30 23:49:02 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
2015-11-27 19:24:00 +01:00
|
|
|
/** @file test-helper.hpp
|
|
|
|
|
** A collection of frequently used helper functions to support unit testing.
|
2016-01-08 08:20:59 +01:00
|
|
|
** Some are test data generators, some are diagnostics helpers to produce readable
|
|
|
|
|
** output. Some of these support meta programming to figure out the \em actual
|
2015-11-27 19:24:00 +01:00
|
|
|
** reference kind (value, lvalue, rvalue) of a template parameter instantiation.
|
2016-01-08 08:20:59 +01:00
|
|
|
** For GNU compatible compilers, we expose also the interface to the internal
|
2015-12-27 03:16:49 +01:00
|
|
|
** ABI for [demangling type names](\ref demangleCxx).
|
2015-11-27 19:24:00 +01:00
|
|
|
**
|
|
|
|
|
** @note this header is included into a large number of tests.
|
|
|
|
|
** @see TestHelper_test
|
|
|
|
|
** @see TestHelperDemangling_test
|
|
|
|
|
**
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
2015-11-28 18:36:35 +01:00
|
|
|
#ifndef LIB_TEST_TEST_HELPER_H
|
|
|
|
|
#define LIB_TEST_TEST_HELPER_H
|
2009-05-30 23:49:02 +02:00
|
|
|
|
|
|
|
|
|
2009-09-24 23:02:40 +02:00
|
|
|
#include "lib/symbol.hpp"
|
2011-05-16 08:37:36 +02:00
|
|
|
#include "lib/time/timevalue.hpp"
|
2016-01-05 23:34:53 +01:00
|
|
|
#include "lib/format-obj.hpp"
|
2009-05-30 23:49:02 +02:00
|
|
|
|
2014-09-22 03:37:07 +02:00
|
|
|
#include <boost/lexical_cast.hpp>
|
2009-05-30 23:49:02 +02:00
|
|
|
#include <typeinfo>
|
2009-07-13 01:16:30 +02:00
|
|
|
#include <cstdlib>
|
2014-09-22 03:37:07 +02:00
|
|
|
#include <string>
|
2009-05-30 23:49:02 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace lib {
|
|
|
|
|
namespace test{
|
|
|
|
|
|
2009-09-24 23:02:40 +02:00
|
|
|
using lib::Literal;
|
2009-05-30 23:49:02 +02:00
|
|
|
using std::string;
|
2009-07-13 01:16:30 +02:00
|
|
|
using std::rand;
|
2016-01-05 23:34:53 +01:00
|
|
|
using lib::meta::demangleCxx;
|
2009-05-30 23:49:02 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-04-25 01:40:39 +02:00
|
|
|
|
2009-05-30 23:49:02 +02:00
|
|
|
/** for printing sizeof().
|
2009-06-07 03:22:08 +02:00
|
|
|
* prints the given size and name literally, without any further magic */
|
2009-05-30 23:49:02 +02:00
|
|
|
string
|
2016-01-08 08:20:59 +01:00
|
|
|
showSizeof (size_t siz, string name);
|
2009-05-30 23:49:02 +02:00
|
|
|
|
2016-01-08 08:20:59 +01:00
|
|
|
/** for printing sizeof(), possibly figuring out the type name automatically
|
|
|
|
|
* @param name when given, this name will be used for display,
|
|
|
|
|
* instead of auto detecting the type
|
|
|
|
|
*/
|
2009-05-30 23:49:02 +02:00
|
|
|
template<typename T>
|
2009-07-13 01:16:30 +02:00
|
|
|
inline string
|
2016-01-08 08:20:59 +01:00
|
|
|
showSizeof (T const* obj =0, const char* name =0)
|
2009-05-30 23:49:02 +02:00
|
|
|
{
|
2016-01-08 08:20:59 +01:00
|
|
|
return showSizeof (obj? sizeof(*obj) : sizeof(T),
|
|
|
|
|
name? name : util::typeStr(obj));
|
2009-05-30 23:49:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename T>
|
2009-07-13 01:16:30 +02:00
|
|
|
inline string
|
2016-01-08 08:20:59 +01:00
|
|
|
showSizeof (T const& obj, const char* name=0)
|
2009-05-30 23:49:02 +02:00
|
|
|
{
|
2016-01-08 08:20:59 +01:00
|
|
|
return showSizeof (&obj, name);
|
2009-05-30 23:49:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename T>
|
2009-07-13 01:16:30 +02:00
|
|
|
inline string
|
2016-01-08 08:20:59 +01:00
|
|
|
showSizeof (const char* name)
|
2009-05-30 23:49:02 +02:00
|
|
|
{
|
2016-01-08 08:20:59 +01:00
|
|
|
return showSizeof<T> (nullptr, name);
|
2009-05-30 23:49:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2009-07-13 01:16:30 +02:00
|
|
|
|
2014-09-22 03:37:07 +02:00
|
|
|
/** helper to discern the kind of reference of the argument type */
|
|
|
|
|
template<typename R>
|
|
|
|
|
string
|
|
|
|
|
showRefKind()
|
|
|
|
|
{
|
|
|
|
|
return std::is_lvalue_reference<R>::value? "REF"
|
|
|
|
|
: std::is_rvalue_reference<R>::value? "MOV"
|
|
|
|
|
: "VAL";
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-23 03:36:51 +02:00
|
|
|
/** helper for investigating a variadic argument pack
|
|
|
|
|
* @warning always spell out the template arguments explicitly
|
|
|
|
|
* when invoking this diagnostics, e.g. \c showVariadicTypes<ARGS...>(args...)
|
|
|
|
|
* otherwise the template argument matching for functions might mess up the
|
|
|
|
|
* kind of reference you'll see in the diagnostics.
|
|
|
|
|
* @see test-helper-variadic-test.cpp
|
|
|
|
|
*/
|
|
|
|
|
template<typename... EMPTY>
|
2014-09-22 03:37:07 +02:00
|
|
|
inline string
|
|
|
|
|
showVariadicTypes ()
|
|
|
|
|
{
|
|
|
|
|
return " :.";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename X, typename... XS>
|
2014-09-23 03:36:51 +02:00
|
|
|
inline string
|
2014-09-22 03:37:07 +02:00
|
|
|
showVariadicTypes (X const& x, XS const&... xs)
|
|
|
|
|
{
|
|
|
|
|
return " :---#"
|
|
|
|
|
+ boost::lexical_cast<string>(1 + sizeof...(xs))
|
2016-01-08 08:20:59 +01:00
|
|
|
+ " -- Type: " + util::typeStr(x)
|
2014-09-22 03:37:07 +02:00
|
|
|
+ " " + showRefKind<X>()
|
|
|
|
|
+ " Address* " + boost::lexical_cast<string>(&x)
|
|
|
|
|
+ "\n"
|
2014-09-23 03:36:51 +02:00
|
|
|
+ showVariadicTypes<XS...> (xs...);
|
2014-09-22 03:37:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-09-23 03:36:51 +02:00
|
|
|
|
2014-09-22 03:37:07 +02:00
|
|
|
/** create a random but not insane Time value */
|
2011-05-16 08:37:36 +02:00
|
|
|
inline lib::time::Time
|
2009-07-13 01:16:30 +02:00
|
|
|
randTime ()
|
|
|
|
|
{
|
2015-08-29 21:27:33 +02:00
|
|
|
return lib::time::Time (500 * (rand() % 2), (rand() % 600) + 1);
|
2009-07-13 01:16:30 +02:00
|
|
|
}
|
|
|
|
|
|
2009-10-30 03:53:51 +01:00
|
|
|
/** create garbage string of given length
|
|
|
|
|
* @return string containing arbitrary lower case letters and numbers
|
|
|
|
|
*/
|
|
|
|
|
string randStr (size_t len);
|
|
|
|
|
|
2009-07-13 01:16:30 +02:00
|
|
|
|
2009-05-30 23:49:02 +02:00
|
|
|
}} // namespace lib::test
|
2009-06-07 03:22:08 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* === test helper macros === */
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Macro to verify a statement indeed raises an exception.
|
|
|
|
|
* If no exception is thrown, the #NOTREACHED macro will trigger
|
|
|
|
|
* an assertion failure. In case of an exception, the #lumiera_error
|
|
|
|
|
* state is checked, cleared and verified.
|
|
|
|
|
*/
|
2010-10-12 02:38:18 +02:00
|
|
|
#define VERIFY_ERROR(ERROR_ID, ERRONEOUS_STATEMENT) \
|
|
|
|
|
try \
|
|
|
|
|
{ \
|
|
|
|
|
ERRONEOUS_STATEMENT ; \
|
|
|
|
|
NOTREACHED("expected '%s' failure in: %s", \
|
|
|
|
|
#ERROR_ID, #ERRONEOUS_STATEMENT); \
|
|
|
|
|
} \
|
|
|
|
|
catch (...) \
|
|
|
|
|
{ \
|
|
|
|
|
CHECK (lumiera_error_expect (LUMIERA_ERROR_##ERROR_ID));\
|
2009-06-07 03:22:08 +02:00
|
|
|
}
|
|
|
|
|
|
2015-11-28 18:36:35 +01:00
|
|
|
#endif /*LIB_TEST_TEST_HELPER_H*/
|