Click here to Skip to main content
15,879,535 members
Articles / Programming Languages / C++

Napkin

Rate me:
Please Sign up or sign in to vote.
4.38/5 (4 votes)
12 Mar 20063 min read 38.9K   66   11  
A simple logging library using generic object to streams
#ifndef PSTADE_TEST_HPP
#define PSTADE_TEST_HPP
///////////////////////////////////////////////////////////////////////////////
// PStade.Wine
//
// Copyright 2006 MB.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)

// <boost/detail/lightweight_test.hpp>
//
// Copyright (c) 2002 Peter Dimov and Multi Media Ltd.
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)


#include <pstade/print.hpp>

#if !defined(_WIN32_WCE)
	#include <sstream>
	#include <boost/current_function.hpp>
#else
	#include <boost/preprocessor/seq/cat.hpp>
	#include <boost/preprocessor/stringize.hpp>
#endif

namespace pstade {


namespace test_detail {


	inline int& error_count()
	{
		static int x = 0;
		return x;
	}


#if !defined(_WIN32_WCE)
	inline void print_failure(const char *expr, const char *file, int line, const char* function)
	{
		// WinCE has no iostreams.
		std::stringstream msg;
		msg << file << "(" << line << "): test '" << expr << "' failed in function '" << function << "'";
		pstade::print(msg.str());
		++error_count();
	}
#endif


} // namespace test_detail


inline int test_report()
{
	int count = test_detail::error_count();

	if (count == 0) {
		pstade::print("No errors detected.");
		return 0;
	}
	else {
	#if !defined(_WIN32_WCE)
		std::stringstream msg;
		msg << count << " error" << (count == 1 ? "" : "s") << " detected.";
		pstade::print(msg.str());
	#else
		pstade::print("Some errors detected.");
	#endif
		return 1;
	}
}


} // namespace pstade


#if !defined(_WIN32_WCE)

	// Topic: BOOST_PP_CAT makes a problem about __LINE__ with /ZI option.

	#define PSTADE_TEST(expr) \
		((expr) ? (void)0 : \
			pstade::test_detail::print_failure(#expr, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION)) \
	/**/

#else

	#define PSTADE_TEST_DETAIL_INFO(exp) \
		BOOST_PP_STRINGIZE( BOOST_PP_SEQ_CAT( (__FILE__)([)(__LINE__)(])(:)(exp) ) ) \
	/**/

	#define PSTADE_TEST(expr) \
		( (expr) ? (void)0 : \
			(++pstade::test_detail::error_count(), \
			pstade::print(PSTADE_TEST_DETAIL_INFO(expr))) ) \
	/**/

#endif // !defined(_WIN32_WCE)


///////////////////////////////////////////////////////////////////////////////
#endif

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Japan Japan
I am worried about my poor English...

Comments and Discussions