[Mockpp-commits] mockpp/mockpp/compat Asserter.cpp,NONE,1.1 Asserter.h,NONE,1.1 Assert.cpp,1.24,NONE
Brought to you by:
ewald-arnold
From: Ewald A. <ewa...@us...> - 2005-12-28 21:57:41
|
Update of /cvsroot/mockpp/mockpp/mockpp/compat In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv6453/mockpp/compat Added Files: Asserter.cpp Asserter.h Removed Files: Assert.cpp Assert.h Log Message: avoid name collision on win32 --- NEW FILE: Asserter.h --- /** @file @brief Handle assertions $Id: Asserter.h,v 1.1 2005/12/28 21:57:33 ewald-arnold Exp $ ***************************************************************************/ /************************************************************************** begin : Sat Dec 21 2002 copyright : (C) 2002-2005 by Ewald Arnold email : mockpp at ewald-arnold dot de This program 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 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 Lesser General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. **/ #ifndef MOCKPP_ASSERT_H #define MOCKPP_ASSERT_H #include <mockpp/mockpp.h> // always first #include <mockpp/compat/AssertionFailedError.h> namespace mockpp { /** Asserts that a condition is true. If it isn't it throws * an AssertionFailedError with the given infomation. * @ingroup grp_helper * @param srcline the line in the sourcefile * @param srcfile the name of the sourcefile * @param message a human readable description about the cause * @param condition if false, an AssertionFailedError is thrown */ void assertTrue(unsigned srcline, const char* srcfile, const String &message, bool condition); /** Asserts that a condition is true. If it isn't it throws * an AssertionFailedError. * @ingroup grp_helper * @param srcline the line in the sourcefile * @param srcfile the name of the sourcefile * @param condition if false, an AssertionFailedError is thrown */ void assertTrue(unsigned srcline, const char* srcfile, bool condition); /** Asserts that a condition is false. If it isn't it throws * an AssertionFailedError with the given message. * @ingroup grp_helper * @param srcline the line in the sourcefile * @param srcfile the name of the sourcefile * @param message a human readable description about the cause * @param condition if true, an AssertionFailedError is thrown */ void assertFalse(unsigned srcline, const char* srcfile, const String &message, bool condition); /** Asserts that a condition is false. If it isn't it throws * an AssertionFailedError. * @ingroup grp_helper * @param srcline the line in the sourcefile * @param srcfile the name of the sourcefile * @param condition if true, an AssertionFailedError is thrown */ void assertFalse(unsigned srcline, const char* srcfile, bool condition); /** Fails a test with the given message (AssertionFailedError is thrown). * @ingroup grp_helper * @param srcline the line in the sourcefile * @param srcfile the name of the sourcefile * @param message a human readable description about the cause */ void fail(unsigned srcline, const char* srcfile, const String &message); /** Fails a test with no message. * @ingroup grp_helper * @param srcline the line in the sourcefile * @param srcfile the name of the sourcefile */ void fail(unsigned srcline, const char* srcfile); /** Asserts that two doubles are equal concerning a delta. If they are not * an AssertionFailedError is thrown with the given message. If the expected * value is infinity then the delta value is ignored. * @ingroup grp_helper * @param srcline the line in the sourcefile * @param srcfile the name of the sourcefile * @param message a human readable description about the cause * @param expected the expected value * @param actual the actual value * @param delta the maximal difference which is allowed for actual from expected */ void assertDelta(unsigned srcline, const char* srcfile, const String &message, double expected, double actual, double delta); /** Asserts that two doubles are equal concerning a delta. If the expected * value is infinity then the delta value is ignored. * @ingroup grp_helper * @param srcline the line in the sourcefile * @param srcfile the name of the sourcefile * @param expected the expected value * @param actual the actual value * @param delta the maximal difference which is allowed for actual from expected */ void assertDelta(unsigned srcline, const char* srcfile, double expected, double actual, double delta); /** Asserts that two floats are equal concerning a delta. If they are not * an AssertionFailedError is thrown with the given message. If the expected * value is infinity then the delta value is ignored. * @ingroup grp_helper * @param srcline the line in the sourcefile * @param srcfile the name of the sourcefile * @param message a human readable description about the cause * @param expected the expected value * @param actual the actual value * @param delta the maximal difference which is allowed for actual from expected */ void assertDelta(unsigned srcline, const char* srcfile, const String &message, float expected, float actual, float delta); /** Asserts that two floats are equal concerning a delta. If the expected * value is infinity then the delta value is ignored. * @ingroup grp_helper * @param srcline the line in the sourcefile * @param srcfile the name of the sourcefile * @param expected the expected value * @param actual the actual value * @param delta the maximal difference which is allowed for actual from expected */ void assertDelta(unsigned srcline, const char* srcfile, float expected, float actual, float delta); /** Asserts that two c-string are equal. If they are not * an AssertionFailedError is thrown with the given message. * @ingroup grp_helper * @param srcline the line in the sourcefile * @param srcfile the name of the sourcefile * @param message a human readable description about the cause * @param expected the expected value * @param actual the actual value */ void assertEquals(unsigned srcline, const char* srcfile, const String &message, const char *expected, const char *actual); /** Asserts that two c-string are equal. * @ingroup grp_helper * @param srcline the line in the sourcefile * @param srcfile the name of the sourcefile * @param expected the expected value * @param actual the actual value */ void assertEquals(unsigned srcline, const char* srcfile, const char *expected, const char *actual); #ifdef MOCKPP_UNICODE /** Asserts that two unicode c-string are equal. If they are not * an AssertionFailedError is thrown with the given message. * @ingroup grp_helper * @param srcline the line in the sourcefile * @param srcfile the name of the sourcefile * @param message a human readable description about the cause * @param expected the expected value * @param actual the actual value */ void assertEquals(unsigned srcline, const char* srcfile, const String &message, const wchar_t *expected, const wchar_t *actual); /** Asserts that two unicode c-string are equal. * @ingroup grp_helper * @param srcline the line in the sourcefile * @param srcfile the name of the sourcefile * @param expected the expected value * @param actual the actual value */ void assertEquals(unsigned srcline, const char* srcfile, const wchar_t *expected, const wchar_t *actual); #endif /** Asserts that two values are equal. * @ingroup grp_helper * @param srcline the line in the sourcefile * @param srcfile the name of the sourcefile * @param message a human readable description about the cause * @param expected the expected value * @param actual the actual value */ template <class T> void assertEquals(unsigned srcline, const char* srcfile, const String &message, const T &expected, const T &actual) { if (expected != actual) fail(srcline, srcfile, message); } /** Asserts that two values are equal. * @ingroup grp_helper * @param srcline the line in the sourcefile * @param srcfile the name of the sourcefile * @param expected the expected value * @param actual the actual value */ template <class T> void assertEquals(unsigned srcline, const char* srcfile, const T &expected, const T &actual) { assertEquals(srcline, srcfile, MOCKPP_PCHAR(""), expected, actual); } /** Asserts that two values are equal concerning a delta. If they are not * an AssertionFailedError is thrown with the given message. * @ingroup grp_helper * @param srcline the line in the sourcefile * @param srcfile the name of the sourcefile * @param message a human readable description about the cause * @param expected the expected value * @param actual the actual value * @param delta the maximal difference which is allowed for actual from expected */ template <class T> void assertDelta(unsigned srcline, const char* srcfile, const String &message, const T &expected, const T &actual, const T &delta) { T diff = expected < actual ? actual - expected : expected - actual; if (diff > delta) fail(srcline, srcfile, message); } /** Asserts that two values are equal concerning a delta. * @ingroup grp_helper * @param srcline the line in the sourcefile * @param srcfile the name of the sourcefile * @param expected the expected value * @param actual the actual value * @param delta the maximal difference which is allowed for actual from expected */ template <class T> void assertDelta(unsigned srcline, const char* srcfile, const T &expected, const T &actual, const T &delta) { assertDelta(srcline, srcfile, MOCKPP_PCHAR(""), expected, actual, delta); } /** Asserts that a values lies between two boundary values. * @ingroup grp_helper * @param srcline the line in the sourcefile * @param srcfile the name of the sourcefile * @param message a human readable description about the cause * @param lo lower boundaray * @param up upper boundary * @param actual value */ template <class T> void assertBoundary(unsigned srcline, const char* srcfile, const String &message, const T &lo, const T &up, const T &actual) { if ((actual < lo) || (up < actual)) fail(srcline, srcfile, message); } /** Asserts that a values lies between two boundary values. * @ingroup grp_helper * @param srcline the line in the sourcefile * @param srcfile the name of the sourcefile * @param lo lower boundaray * @param up upper boundary * @param actual value */ template <class T> void assertBoundary(unsigned srcline, const char* srcfile, const T &lo, const T &up, const T &actual) { assertBoundary(srcline, srcfile, MOCKPP_PCHAR(""), lo, up, actual); } } // namespace mockpp /** Asserts that two values are equal. * @ingroup grp_helper * @param msg a human readable description about the cause * @param a first value * @param b second value */ #define MOCKPP_ASSERT_EQUALS_MESSAGE(msg, a, b) mockpp::assertEquals(__LINE__, __FILE__, msg, a, b) /** @def MOCKPP_ASSERT_EQUALS * @ingroup grp_helper * Asserts that two values are equal. * The condition is converted and passed as the according message. * @param a first value * @param b second value */ # define MOCKPP_ASSERT_EQUALS(a, b) mockpp::assertEquals(__LINE__, __FILE__, MOCKPP_PCHAR("invalid: ") \ MOCKPP_PCHAR(#a) MOCKPP_PCHAR(" == ") MOCKPP_PCHAR(#b), a, b) /** Asserts that two values are equal concerning a delta. * @ingroup grp_helper * @param msg a human readable description about the cause * @param a first value * @param b second value * @param delta maximal difference a to b */ #define MOCKPP_ASSERT_DELTA_MESSAGE(msg, a, b, delta) mockpp::assertDelta(__LINE__, __FILE__, msg, a, b, delta) /** @def MOCKPP_ASSERT_DELTA * @ingroup grp_helper * Asserts that two values are equal concerning a delta. * The condition is converted and passed as the according message. * @param a first value * @param b second value * @param delta maximal difference a to b */ # define MOCKPP_ASSERT_DELTA(a, b, delta) mockpp::assertDelta(__LINE__, __FILE__, MOCKPP_PCHAR("invalid: ") \ MOCKPP_PCHAR(#a) MOCKPP_PCHAR(" +- ") MOCKPP_PCHAR(#delta) \ MOCKPP_PCHAR(" == ") MOCKPP_PCHAR(#b), a, b, delta) /** Asserts that a values lies between two boundary values. * @ingroup grp_helper * @param msg a human readable description about the cause * @param lo lower boundaray * @param up upper boundary * @param a value */ #define MOCKPP_ASSERT_BOUNDARY_MESSAGE(msg, lo, up, a) mockpp::assertBoundary(__LINE__, __FILE__, msg, lo, up, a) /** @def MOCKPP_ASSERT_BOUNDARY * @ingroup grp_helper * Asserts that two values are equal concerning a delta. * The condition is converted and passed as the according message. * @param lo lower boundaray * @param up upper boundary * @param a value */ # define MOCKPP_ASSERT_BOUNDARY(lo, up, a) mockpp::assertBoundary(__LINE__, __FILE__, MOCKPP_PCHAR("invalid: ") \ MOCKPP_PCHAR(#lo) MOCKPP_PCHAR(" < ") MOCKPP_PCHAR(#a) \ MOCKPP_PCHAR(" < ") MOCKPP_PCHAR(#up), lo, up, a) /** Asserts that a condition is true. * @ingroup grp_helper * @param msg a human readable description about the cause * @param cond boolean condition */ #define MOCKPP_ASSERT_TRUE_MESSAGE(msg, cond) mockpp::assertTrue(__LINE__, __FILE__, msg, cond) /** @def MOCKPP_ASSERT_TRUE * @ingroup grp_helper * Asserts that a condition is true. * The condition is converted and passed as the according message. * @param cond boolean condition */ # define MOCKPP_ASSERT_TRUE(cond) mockpp::assertTrue(__LINE__, __FILE__, MOCKPP_PCHAR("!true: ") MOCKPP_PCHAR(#cond), cond) /** Asserts that a condition is false. * @ingroup grp_helper * @param msg a human readable description about the cause * @param cond boolean condition */ #define MOCKPP_ASSERT_FALSE_MESSAGE(msg, cond) mockpp::assertFalse(__LINE__, __FILE__, msg, cond) /** @def MOCKPP_ASSERT_FALSE * @ingroup grp_helper * Asserts that a condition is false. * The condition is converted and passed as the according message. * @param cond boolean condition */ # define MOCKPP_ASSERT_FALSE(cond) mockpp::assertFalse(__LINE__, __FILE__, MOCKPP_PCHAR("!false: ") MOCKPP_PCHAR(#cond), cond) /** Unconditionally throws an AssertionFailedError. * @ingroup grp_helper * @param msg a human readable description about the cause */ #define MOCKPP_FAIL(msg) mockpp::fail(__LINE__, __FILE__, msg) #endif // MOCKPP_ASSERT_H --- Assert.h DELETED --- --- Assert.cpp DELETED --- --- NEW FILE: Asserter.cpp --- /** @file @brief handle assertions $Id: Asserter.cpp,v 1.1 2005/12/28 21:57:33 ewald-arnold Exp $ ***************************************************************************/ /************************************************************************** begin : Sat Dec 21 2002 copyright : (C) 2002-2005 by Ewald Arnold email : mockpp at ewald-arnold dot de This program 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 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 Lesser General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. **/ #include <mockpp/mockpp.h> // always first #include <cmath> #include <cstring> #ifdef HAVE_CWCHAR #include <cwchar> #endif // Taken almost literally from GNU Libc sources: #if !defined(HAVE_WCSCMP) && defined(MOCKPP_UNICODE) /* Compare S1 and S2, returning less than, equal to or greater than zero if S1 is lexicographically less than, equal to or greater than S2. */ static int wcscmp (const wchar_t *s1, const wchar_t *s2) { wchar_t c1, c2; do { c1 = *s1++; c2 = *s2++; if (c1 == L'\0') return c1 - c2; } while (c1 == c2); return c1 - c2; } #endif // HAVE_WCSCMP #include <mockpp/compat/Asserter.h> namespace mockpp { void MOCKPP_EXPORT assertTrue(unsigned srcline, const char* srcfile, const String &message, bool condition) { if (!condition) fail(srcline, srcfile, message); } void MOCKPP_EXPORT assertTrue(unsigned srcline, const char* srcfile, bool condition) { assertTrue(srcline, srcfile, MOCKPP_PCHAR(""), condition); } void MOCKPP_EXPORT assertFalse(unsigned srcline, const char* srcfile, const String &message, bool condition) { assertTrue(srcline, srcfile, message, !condition); } void MOCKPP_EXPORT assertFalse(unsigned srcline, const char* srcfile, bool condition) { assertFalse(srcline, srcfile, MOCKPP_PCHAR(""), condition); } void MOCKPP_EXPORT fail(unsigned srcline, const char* srcfile, const String &message) { assertionFailed(srcline, srcfile, message); } void MOCKPP_EXPORT fail(unsigned srcline, const char* srcfile) { fail(srcline, srcfile, MOCKPP_PCHAR("")); } void MOCKPP_EXPORT assertEquals(unsigned srcline, const char* srcfile, const char *expected, const char *actual) { assertEquals(srcline, srcfile, MOCKPP_PCHAR(""), expected, actual); } void MOCKPP_EXPORT assertEquals(unsigned srcline, const char* srcfile, const String &message, const char *expected, const char *actual) { #if defined(_MSC_VER) if (strcmp(expected, actual) != 0) #else if (std::strcmp(expected, actual) != 0) #endif fail(srcline, srcfile, message); } #ifdef MOCKPP_UNICODE void MOCKPP_EXPORT assertEquals(unsigned srcline, const char* srcfile, const wchar_t *expected, const wchar_t *actual) { assertEquals(srcline, srcfile, MOCKPP_PCHAR(""), expected, actual); } void MOCKPP_EXPORT assertEquals(unsigned srcline, const char* srcfile, const String &message, const wchar_t *expected, const wchar_t *actual) { #if !defined(HAVE_STD_WCSCMP) if (wcscmp(expected, actual) != 0) #else if (std::wcscmp(expected, actual) != 0) #endif fail(srcline, srcfile, message); } #endif void MOCKPP_EXPORT assertDelta(unsigned srcline, const char* srcfile, const String &message, double expected, double actual, double delta) { // handle infinity specially since subtracting to infinite values gives NaN and the // the following test fails #ifdef HAVE_LIMITS if (expected == std::numeric_limits<double>::infinity()) { if (!(expected == actual)) fail(srcline, srcfile, message); } else #endif #if defined(_MSC_VER) if (!(fabs(expected-actual) <= fabs(delta))) // Because comparison with NaN always returns false #else if (!(std::fabs(expected-actual) <= std::fabs(delta))) // Because comparison with NaN always returns false #endif fail(srcline, srcfile, message); } void MOCKPP_EXPORT assertDelta(unsigned srcline, const char* srcfile, double expected, double actual, double delta) { assertDelta(srcline, srcfile, MOCKPP_PCHAR(""), expected, actual, delta); } void MOCKPP_EXPORT assertDelta(unsigned srcline, const char* srcfile, const String &message, float expected, float actual, float delta) { // handle infinity specially since subtracting to infinite values gives NaN and the // the following test fails #ifdef HAVE_LIMITS if (expected == std::numeric_limits<float>::infinity()) { if (!(expected == actual)) fail(srcline, srcfile, message); } else #endif #if defined(_MSC_VER) if (!(fabs(expected-actual) <= fabs(delta))) #else if (!(std::fabs(expected-actual) <= std::fabs(delta))) #endif fail(srcline, srcfile, message); } void MOCKPP_EXPORT assertDelta(unsigned srcline, const char* srcfile, float expected, float actual, float delta) { assertDelta(srcline, srcfile, MOCKPP_PCHAR(""), expected, actual, delta); } } // namespace mockpp |