[B]asic module for [unit] program testing:
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Defines
Classes | Namespaces | Defines | Typedefs | Functions
BUnit.h File Reference

[B]asic module for [unit] program testing. More...

#include <list>
#include <string>
#include <sstream>
#include <cstring>
#include <typeinfo>
#include <algorithm>
#include <math.h>

Go to the source code of this file.

Classes

class  TestCaseFailure
 Private class to hold test cases that didn´t succeed. More...
class  TestCase
 Every test case is an instance derived from this abstract class. More...
class  TestSuite< TestCase >
 Test collection. More...

Namespaces

namespace  std
 Defined by the C++ estándar library.
namespace  ECCI
 Escuela de Ciencias de la Computación e Informática.

Defines

#define English_dox   "Doxygen English documentation"
 Doxygen English documentation.
#define BUnit_h
#define English_dox   "Doxygen English documentation"
 Doxygen English documentation.
#define TEST_BUnit()
 [BUnit] ==> BUnit.h
#define BUnit_TEST(cond)   testThis( cond, #cond, __FILE__, __LINE__ )
 [BUnit] Executes test "cond and records its result.
#define BUnit_TEST_Msg(msg, cond)   testThis( cond, msg, __FILE__, __LINE__ )
 [BUnit] Macro similar to BUnit_TEST() that uses mesaje "msg".
#define BUnit_FAILURE(msg)   recordFailure( msg, __FILE__, __LINE__ )
 [BUnit] Records as "failure" the result of a test.
#define BUnit_SUCCESS()   recordSuccess()
 [BUnit] Records as "successful" the result of a test.
#define BUnit_EQUAL(expected, actual)   testThis( (expected) == (actual), #expected " == " #actual, __FILE__, __LINE__ )
 [BUnit] Executes the test to determine if (expected == actual).
#define BUnit_EQUAL_Msg(MSG, expected, actual)   testThis( (expected) == (actual), MSG, __FILE__, __LINE__ )
 [BUnit] Executes the test to determine if (expected == actual).
#define BUnit_DEFINE_check_ok
 If this macro exist functions for check_ok() will be defined for the basic types.
#define CPPUNIT_BUnit()
 [CppUnit] Macros propios de CppUnit http://cppunit.sourceforge.net/doc/lastest
#define CPPUNIT_ASSERT(condition)   BUnit_TEST(condition)
 [CppUnit] Assertions that a condition is true.
#define CPPUNIT_ASSERT_MESSAGE(message, condition)   assertTrue_Msg(message,condition)
 [CppUnit] Assertion with a user specified message.
#define CPPUNIT_FAIL(message)   BUnit_FAILURE(message)
 [CppUnit] Fails with the specified message.
#define CPPUNIT_ASSERT_EQUAL(expected, actual)   BUnit_EQUAL(expected, actual)
 [CppUnit] Asserts that two values are equals.
#define CPPUNIT_ASSERT_EQUAL_MESSAGE(message, expected, actual)   BUnit_EQUAL_Msg(message, expected, actual)
 [CppUnit] Asserts that two values are equals, provides additional messafe on failure.
#define CPPUNIT_ASSERT_DOUBLES_EQUAL(expected, actual, delta)   assertEquals_Delta(expected, actual, delta)
 [CppUnit] Macro for primitive value comparisons.
#define CPPUNIT_ASSERT_THROW(expression, ExceptionType)
 [CppUnit] Asserts that the given expression throws an exception of the specified type.
#define CPPUNIT_ASSERT_NO_THROW(expression)
 [CppUnit] Asserts that the given expression does not throw any exceptions.
#define CPPUNIT_ASSERT_ASSERTION_FAIL(assertion)   CPPUNIT_ASSERT_THROW( assertion, CPPUNIT_NS::Exception )
 [CppUnit] Asserts that an assertion fail.
#define CPPUNIT_ASSERT_ASSERTION_PASS(assertion)   CPPUNIT_ASSERT_NO_THROW( assertion )
 [CppUnit] Asserts that an assertion pass.
#define JUnit_BUnit()
#define assertEquals(EXPECTED, ACTUAL)   BUnit_EQUAL(EXPECTED, ACTUAL)
 [JUnit] Macros propios de JUnit http://junit.sourceforge.net/javadoc/junit/framework/Assert.html Asserts that two objects are equal.
#define assertEquals_Msg(MSG, EXPECTED, ACTUAL)   BUnit_EQUAL_Msg(MSG, EXPECTED, ACTUAL)
 Asserts that two objects are equal (with message).
#define assertTrue(CONDITION)   testThis( CONDITION, #CONDITION, __FILE__, __LINE__ )
 [JUnit] Asserts that a condition is true.
#define assertTrue_Msg(MSG,CONDITION)   testThis( CONDITION, MSG, __FILE__, __LINE__ )
 [JUnit] Asserts that a condition is true (with message).
#define assertFalse(CONDITION)   testThis( !(CONDITION), "!(" #CONDITION ")", __FILE__, __LINE__ )
 [JUnit] Asserts that a condition is false.
#define assertFalse_Msg(MSG, CONDITION)   testThis( !(CONDITION), MSG, __FILE__, __LINE__ )
 [JUnit] Asserts that a condition is false (with message).
#define assertEquals_Delta(EXPECTED, ACTUAL, DELTA)
 [JUnit] Asserts that two doubles are equal concerning a delta.
#define assertEquals_Delta_Msg(MSG, EXPECTED, ACTUAL, DELTA)
 [JUnit] Asserts that two doubles are equal concerning a delta (with message).
#define assertNull(OBJECT)   testThis( 0==&(OBJECT), "assertNull(" #OBJECT ")", __FILE__, __LINE__ )
 [JUnit] Asserts that an object is null.
#define assertNotNull(OBJECT)   testThis( 0!=&(OBJECT), "assertNotNull(" #OBJECT ")", __FILE__, __LINE__ )
 [JUnit] Asserts that an object isn't null.
#define assertSame(THIS, THAT)
 [JUnit] Asserts that two objects refer to the same object.
#define assertNotSame(THIS, THAT)
 Asserts that two objects do not refer to the same object.
#define fail_Msg(MSG)   BUnit_FAILURE(MSG)
 [JUnit] Fails a test with the given message.

Typedefs

typedef TestCase TestCase_is_base_for_TestSuite
 Synonym for TestCase.
typedef TestCase TestFixture
 Sets the environment for the test run.

Functions

template<class TestCase >
void do_toString (const TestCase *tc, std::basic_ostringstream< char > &ost)
 Adds to ost the string of al unsuccessful test from *tc.
template<class TestCase >
void do_toXML (const TestCase *tc, std::basic_ostringstream< char > &ost)
 Adds to ost the string of al unsuccessful test from *tc in XML format.

Detailed Description

[B]asic module for [unit] program testing.

Author:
Adolfo Di Mare adolf.nosp@m.o@di.nosp@m.-mare.nosp@m..com
Date:
2008

Definition in file BUnit.h.


Define Documentation

#define English_dox   "Doxygen English documentation"

Doxygen English documentation.

Definition at line 227 of file BUnit.h.

#define BUnit_h

Definition at line 52 of file BUnit.h.

#define English_dox   "Doxygen English documentation"

Doxygen English documentation.

Definition at line 227 of file BUnit.h.

#define TEST_BUnit ( )

[BUnit] ==> BUnit.h

Definition at line 1118 of file BUnit.h.

#define BUnit_TEST (   cond)    testThis( cond, #cond, __FILE__, __LINE__ )

[BUnit] Executes test "cond and records its result.

  • It is more elegant to use assertTrue() for the same purpose.
  • If test "cond" is successful method TestCase::recordSuccess() is invoked.
  • If test "cond" is not successful, methodTestCase::recordFailure() is invoked.
        {{  // test::BUnit_macro()
            class MyTest : public TestCase {
            public:
                bool run() {
                    testThis(   2 == 2, "2 == 2", __FILE__, __LINE__ );         // sucess
                    BUnit_TEST( 2 == 2 ); // same thing using macro BUnit_TEST()
                    testThis(   1 == 2, "1 == 2", __FILE__, __LINE__ );         // failure #1
                    assertTrue( 1 == 2 ); // same thing using BUnit_TEST()      // failure #2
                    {   // con macros
                        std::vector<int> V; // "V[]" is empty ==> there is no "V[2]"
                        try {
                            int i = V.at(2);  // "V[2]" does not exits
                            BUnit_FAILURE("V.at(2)"); // failure: "at()" didn´t rise the exception
                        }
                        catch (std::logic_error&) {
                            BUnit_SUCCESS(); // correct: "V.at(2)" throws "logic_error"
                        }                    // because "V[]" is empty
                    }
                    {   // no macros
                        std::vector<int> V;
                        try {
                            int i = V.at(2);
                            recordFailure("V.at(2)" , __FILE__ , __LINE__ );
                        }
                        catch (std::logic_error&) {
                            recordSuccess();
                        }
                    }
                    return wasSuccessful(); // Returns "false"
                }
            }; // MyTest
            MyTest thisTest;
            assertTrue( thisTest.wasSuccessful() ); // run() has not been executed
            thisTest.run(); // 2 failures
            assertTrue( thisTest.failureCount() == 2 );
            assertTrue( thisTest.toXML() != "" );
            assertTrue( ! thisTest.wasSuccessful() );
        }}
    
    See also:
    test_BUnit::test_BUnit_macro()

Definition at line 1144 of file BUnit.h.

#define BUnit_TEST_Msg (   msg,
  cond 
)    testThis( cond, msg, __FILE__, __LINE__ )

[BUnit] Macro similar to BUnit_TEST() that uses mesaje "msg".

  • Mensaje "msg" must be a literal (char*) string or and object that can be converted into a (const char *).

Definition at line 1156 of file BUnit.h.

#define BUnit_FAILURE (   msg)    recordFailure( msg, __FILE__, __LINE__ )

[BUnit] Records as "failure" the result of a test.

  • The client programmer determined that the test did not succeed and wants to record this fact.
  • It is implemented as a macro wrapper to invoke method TestCase::recordFailure().
    See also:
    BUnit_TEST()
        if (22==33) {
            BUnit_FAILURE("! (22==33)"); // records failure
        }
    

Definition at line 1181 of file BUnit.h.

#define BUnit_SUCCESS ( )    recordSuccess()

[BUnit] Records as "successful" the result of a test.

  • The client programmer determined that the test did succeed and wants to record this fact.
  • Usually used after catching an exception that should have been raised as result of the test.
  • It is implemented as a macro wrapper to invoke method TestCase::recordSuccess().
    See also:
    BUnit_TEST()

Definition at line 1205 of file BUnit.h.

#define BUnit_EQUAL (   expected,
  actual 
)    testThis( (expected) == (actual), #expected " == " #actual, __FILE__, __LINE__ )

[BUnit] Executes the test to determine if (expected == actual).

Definition at line 1221 of file BUnit.h.

#define BUnit_EQUAL_Msg (   MSG,
  expected,
  actual 
)    testThis( (expected) == (actual), MSG, __FILE__, __LINE__ )

[BUnit] Executes the test to determine if (expected == actual).

Definition at line 1243 of file BUnit.h.

If this macro exist functions for check_ok() will be defined for the basic types.

Definition at line 1882 of file BUnit.h.

#define CPPUNIT_BUnit ( )

[CppUnit] Macros propios de CppUnit http://cppunit.sourceforge.net/doc/lastest

Definition at line 1903 of file BUnit.h.

#define CPPUNIT_ASSERT (   condition)    BUnit_TEST(condition)

[CppUnit] Assertions that a condition is true.

See also:
http://cppunit.sourceforge.net/doc/lastest/group___assertions.html#ga0

Definition at line 1908 of file BUnit.h.

#define CPPUNIT_ASSERT_MESSAGE (   message,
  condition 
)    assertTrue_Msg(message,condition)

[CppUnit] Assertion with a user specified message.

See also:
http://cppunit.sourceforge.net/doc/lastest/group___assertions.html#ga1

Definition at line 1912 of file BUnit.h.

#define CPPUNIT_FAIL (   message)    BUnit_FAILURE(message)

[CppUnit] Fails with the specified message.

See also:
http://cppunit.sourceforge.net/doc/lastest/group___assertions.html#ga2

Definition at line 1916 of file BUnit.h.

#define CPPUNIT_ASSERT_EQUAL (   expected,
  actual 
)    BUnit_EQUAL(expected, actual)

[CppUnit] Asserts that two values are equals.

See also:
http://cppunit.sourceforge.net/doc/lastest/group___assertions.html#ga3

Definition at line 1920 of file BUnit.h.

#define CPPUNIT_ASSERT_EQUAL_MESSAGE (   message,
  expected,
  actual 
)    BUnit_EQUAL_Msg(message, expected, actual)

[CppUnit] Asserts that two values are equals, provides additional messafe on failure.

See also:
http://cppunit.sourceforge.net/doc/lastest/group___assertions.html#ga4

Definition at line 1924 of file BUnit.h.

#define CPPUNIT_ASSERT_DOUBLES_EQUAL (   expected,
  actual,
  delta 
)    assertEquals_Delta(expected, actual, delta)

[CppUnit] Macro for primitive value comparisons.

See also:
http://cppunit.sourceforge.net/doc/lastest/group___assertions.html#ga5

Definition at line 1929 of file BUnit.h.

#define CPPUNIT_ASSERT_THROW (   expression,
  ExceptionType 
)
Value:
do {                                            \
            bool cpputExceptionThrown_ = false;         \
            try {                                       \
                expression;                             \
            } catch ( const ExceptionType & ) {         \
                cpputExceptionThrown_ = true;           \
            }                                           \
                                                        \
            if ( cpputExceptionThrown_ ) {              \
                break;                                  \
            }                                           \
            BUnit_FAILURE(                              \
                "Expected exception: " #ExceptionType ) \
        } while ( false )

[CppUnit] Asserts that the given expression throws an exception of the specified type.

See also:
http://cppunit.sourceforge.net/doc/lastest/group___assertions.html#ga6

Definition at line 1934 of file BUnit.h.

#define CPPUNIT_ASSERT_NO_THROW (   expression)
Value:
do {                                                  \
            try {                                             \
                expression;                                   \
            } catch ( ... ) {                                 \
                BUnit_FAILURE("Unexpected exception caught"); \
            }                                                 \
        } while ( false )

[CppUnit] Asserts that the given expression does not throw any exceptions.

See also:
http://cppunit.sourceforge.net/doc/lastest/group___assertions.html#ga7

Definition at line 1952 of file BUnit.h.

#define CPPUNIT_ASSERT_ASSERTION_FAIL (   assertion)    CPPUNIT_ASSERT_THROW( assertion, CPPUNIT_NS::Exception )

[CppUnit] Asserts that an assertion fail.

See also:
http://cppunit.sourceforge.net/doc/lastest/group___assertions.html#ga8

Definition at line 1963 of file BUnit.h.

#define CPPUNIT_ASSERT_ASSERTION_PASS (   assertion)    CPPUNIT_ASSERT_NO_THROW( assertion )

[CppUnit] Asserts that an assertion pass.

See also:
http://cppunit.sourceforge.net/doc/lastest/group___assertions.html#ga9

Definition at line 1968 of file BUnit.h.

#define JUnit_BUnit ( )

Definition at line 1971 of file BUnit.h.

#define assertEquals (   EXPECTED,
  ACTUAL 
)    BUnit_EQUAL(EXPECTED, ACTUAL)
#define assertEquals_Msg (   MSG,
  EXPECTED,
  ACTUAL 
)    BUnit_EQUAL_Msg(MSG, EXPECTED, ACTUAL)
#define assertTrue (   CONDITION)    testThis( CONDITION, #CONDITION, __FILE__, __LINE__ )

[JUnit] Asserts that a condition is true.

See also:
http://junit.sourceforge.net/javadoc/junit/framework/Assert.html#assertTrue(boolean)

Definition at line 1984 of file BUnit.h.

#define assertTrue_Msg (   MSG,
  CONDITION 
)    testThis( CONDITION, MSG, __FILE__, __LINE__ )

[JUnit] Asserts that a condition is true (with message).

See also:
http://junit.sourceforge.net/javadoc/junit/framework/Assert.html#assertTrue(java.lang.String,%20boolean)

Definition at line 1988 of file BUnit.h.

#define assertFalse (   CONDITION)    testThis( !(CONDITION), "!(" #CONDITION ")", __FILE__, __LINE__ )

[JUnit] Asserts that a condition is false.

See also:
http://junit.sourceforge.net/javadoc/junit/framework/Assert.html#assertFalse(boolean)

Definition at line 1993 of file BUnit.h.

#define assertFalse_Msg (   MSG,
  CONDITION 
)    testThis( !(CONDITION), MSG, __FILE__, __LINE__ )

[JUnit] Asserts that a condition is false (with message).

See also:
http://junit.sourceforge.net/javadoc/junit/framework/Assert.html#assertFalse(java.lang.String,%20boolean)

Definition at line 1997 of file BUnit.h.

#define assertEquals_Delta (   EXPECTED,
  ACTUAL,
  DELTA 
)
Value:
testThis( fabs( double(EXPECTED) -  double(ACTUAL) ) < double(DELTA), \
        "|"  #EXPECTED "-" #ACTUAL "| < " #DELTA,  __FILE__, __LINE__ )

[JUnit] Asserts that two doubles are equal concerning a delta.

See also:
http://junit.sourceforge.net/javadoc/junit/framework/Assert.html#assertEquals(double,%20double,%20double)

Definition at line 2003 of file BUnit.h.

#define assertEquals_Delta_Msg (   MSG,
  EXPECTED,
  ACTUAL,
  DELTA 
)
Value:
testThis( fabs( double(EXPECTED) -  double(ACTUAL) ) < double(DELTA), \
        MSG, __FILE__, __LINE__ )

[JUnit] Asserts that two doubles are equal concerning a delta (with message).

See also:
http://junit.sourceforge.net/javadoc/junit/framework/Assert.html#assertEquals(java.lang.String,%20double,%20double,%20double)

Definition at line 2009 of file BUnit.h.

#define assertNull (   OBJECT)    testThis( 0==&(OBJECT), "assertNull(" #OBJECT ")", __FILE__, __LINE__ )

[JUnit] Asserts that an object is null.

See also:
http://junit.sourceforge.net/javadoc/junit/framework/Assert.html#assertNull(java.lang.Object)

Definition at line 2015 of file BUnit.h.

#define assertNotNull (   OBJECT)    testThis( 0!=&(OBJECT), "assertNotNull(" #OBJECT ")", __FILE__, __LINE__ )

[JUnit] Asserts that an object isn't null.

See also:
http://junit.sourceforge.net/javadoc/junit/framework/Assert.html#assertNotNull(java.lang.Object)

Definition at line 2019 of file BUnit.h.

#define assertSame (   THIS,
  THAT 
)
Value:
testThis( &(THIS)==&(THAT), \
                   "assertSame("    #THIS ", " #THAT ")", __FILE__, __LINE__ )

[JUnit] Asserts that two objects refer to the same object.

See also:
http://junit.sourceforge.net/javadoc/junit/framework/Assert.html#assertSame(java.lang.Object,%20java.lang.Object)

Definition at line 2024 of file BUnit.h.

#define assertNotSame (   THIS,
  THAT 
)
Value:
testThis( &(THIS)!=&(THAT), \
                   "assertNotSame(" #THIS ", " #THAT ")", __FILE__, __LINE__ )

Asserts that two objects do not refer to the same object.

See also:
http://junit.sourceforge.net/javadoc/junit/framework/Assert.html#assertNotSame(java.lang.Object,%20java.lang.Object)

Definition at line 2029 of file BUnit.h.

#define fail_Msg (   MSG)    BUnit_FAILURE(MSG)

[JUnit] Fails a test with the given message.

See also:
http://junit.sourceforge.net/javadoc/junit/framework/Assert.html#fail(java.lang.String)

Definition at line 2042 of file BUnit.h.


Typedef Documentation

Synonym for TestCase.

Definition at line 183 of file BUnit.h.

Sets the environment for the test run.

This clase exists to improve compatibility with JUnit.

See also:
TestCase::setUp()

Definition at line 791 of file BUnit.h.


Function Documentation

template<class TestCase >
void do_toString ( const TestCase tc,
std::basic_ostringstream< char > &  ost 
)

Adds to ost the string of al unsuccessful test from *tc.

Definition at line 1050 of file BUnit.h.

template<class TestCase >
void do_toXML ( const TestCase tc,
std::basic_ostringstream< char > &  ost 
)

Adds to ost the string of al unsuccessful test from *tc in XML format.

Definition at line 1097 of file BUnit.h.