CPUnit 0.95 (beta)
The REAL C++ port of JUnit.
/Users/Shared/Development/cpp/sourceforge/cpunit_095/src/cpunit_Assert.cpp
Go to the documentation of this file.
00001 /*
00002    Copyright (c) 2011 Daniel Bakkelund.
00003    All rights reserved.
00004 
00005    Redistribution and use in source and binary forms, with or without
00006    modification, are permitted provided that the following conditions
00007    are met:
00008     1. Redistributions of source code must retain the above copyright
00009        notice, this list of conditions and the following disclaimer.
00010     2. Redistributions in binary form must reproduce the above copyright
00011        notice, this list of conditions and the following disclaimer in the
00012        documentation and/or other materials provided with the distribution.
00013     3. Neither the name of the copyright holders nor the names of its
00014        contributors may be used to endorse or promote products derived from
00015        this software without specific prior written permission.
00016 
00017    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00018    AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019    IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020    ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00021    LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00022    CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00023    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00024    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00025    CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00026    ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
00027    THE POSSIBILITY OF SUCH DAMAGE.
00028 */
00029 
00030 
00031 
00032 #include "cpunit_Assert.hpp"
00033 #include <sstream>
00034 
00040 void cpunit::fail(const std::string &msg){
00041   std::ostringstream oss;
00042   oss<<"FAIL CALLED - "<<msg;
00043   throw AssertionException(oss.str());
00044 }
00045 
00050 void cpunit::fail(){
00051   fail("");
00052 }
00053 
00060 void cpunit::assert_true(const std::string &msg, const bool statement) {
00061   if (!statement) {
00062     std::ostringstream oss;
00063     oss<<"ASSERT TRUE FAILED - "<<msg;
00064     throw AssertionException(oss.str());
00065   }
00066 }
00067 
00073 void cpunit::assert_true(const bool statement) {
00074   assert_true("", statement);
00075 }
00076 
00083 void cpunit::assert_false(const std::string &msg, const bool statement) {
00084   if (statement) {
00085     std::ostringstream oss;
00086     oss<<"ASSERT FALSE FAILED - "<<msg;
00087     throw AssertionException(oss.str());
00088   }
00089 }
00090 
00096 void cpunit::assert_false(const bool statement) {
00097   assert_false("", statement);
00098 }
00099 
00109 void cpunit::assert_equals(const std::string &msg, const double expected, const double actual, const double error) {
00110   if (priv::abs(expected - actual) > error) {
00111     priv::fail_equals(msg, expected, actual);
00112   }
00113 }
00114 
00123 void cpunit::assert_equals(const double expected, const double actual, const double error) {
00124   assert_equals("", expected, actual, error);
00125 }
00126 
00134 void cpunit::assert_equals(const std::string &msg, const char *expected, const  char *actual) {
00135   assert_equals(msg, std::string(expected), std::string(actual));
00136 }
00137 
00144 void cpunit::assert_equals(const char *expected, const char *actual) {
00145   assert_equals("", expected, actual);
00146 }
00147   
00155 void cpunit::assert_equals(const std::string &msg, const std::string &expected, const std::string &actual) {
00156   if (!(expected == actual)) {
00157     priv::fail_equals(msg, expected, actual);
00158   }
00159 }
00160 
00167 void cpunit::assert_equals(const std::string &expected, const std::string &actual) {
00168   if (!(expected == actual)) {
00169     priv::fail_equals("", expected, actual);
00170   }
00171 }
00172   
00179 void cpunit::assert_not_null(const std::string &msg, const void *data) {
00180   if (data == NULL) {
00181     std::ostringstream oss;
00182     oss<<"ASSERT NOT NULL FAILED - "<<msg;
00183     throw AssertionException(oss.str());
00184   }
00185 }
00186 
00192 void cpunit::assert_not_null(const void *data) {
00193   assert_not_null("", data);
00194 }
00195 
00202 void cpunit::assert_null(const std::string &msg, const void *data) {
00203   if (data != NULL) {
00204     std::ostringstream oss;
00205     oss<<"ASSERT NULL FAILED - "<<msg;
00206     throw AssertionException(oss.str());
00207   }
00208 }
00209 
00215 void cpunit::assert_null(const void *data) {
00216   assert_null("", data);
00217 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines