Changeset 930


Ignore:
Timestamp:
Sep 8, 2011, 11:02:16 PM (8 years ago)
Author:
sam
Message:

lolunit: add several helper macros for various comparison checks.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/lol/unit.h

    r928 r930  
    202202    void TestCaseName()
    203203
    204 #define LOLUNIT_ASSERT_GENERIC(message, cond) \
     204#define LOLUNIT_ASSERT_GENERIC(msg, cond) \
    205205    do { \
    206206        m_asserts++; \
     
    214214            m_errorlog << "assertion failed" << std::endl; \
    215215            m_errorlog << "- Expression: " << #cond << std::endl; \
    216             m_errorlog << message; \
     216            m_errorlog << msg; \
    217217            m_failure = true; \
    218218            return; \
     
    220220    } while(!True())
    221221
    222 #define LOLUNIT_ASSERT_EQUAL_GENERIC(message, a, b) \
     222#define LOLUNIT_ASSERT_OP(op, modifier, opdesc, msg, a, b) \
    223223    do { \
    224224        m_asserts++; \
    225         if (True() && (a) != (b)) \
     225        if (True() && !modifier((a) op (b))) \
    226226        { \
    227227            m_errorlog << std::endl << std::endl; \
     
    230230                       << " (F) line: " << __LINE__ << " " \
    231231                       << __FILE__ << std::endl; \
    232             m_errorlog << "equality assertion failed" << std::endl; \
     232            m_errorlog << opdesc << " assertion failed" << std::endl; \
    233233            m_errorlog << "- Expected: " << #a << " = " << (a) << std::endl; \
    234234            m_errorlog << "- Actual  : " << #b << " = " << (b) << std::endl; \
    235             m_errorlog << message; \
     235            m_errorlog << msg; \
    236236            m_errorlog << m_context.str(); \
    237237            m_failure = true; \
     
    240240    } while(!True())
    241241
    242 #define LOLUNIT_ASSERT_DOUBLES_EQUAL_GENERIC(message, a, b, t) \
     242#define LOLUNIT_MSG(msg) \
     243    "- " << msg << std::endl
     244
     245#define LOLUNIT_ASSERT_DOUBLES_EQUAL_GENERIC(msg, a, b, t) \
    243246    do { \
    244247        m_asserts++; \
     
    257260            m_errorlog << "- Delta   : " << (t) << std::endl; \
    258261            m_errorlog << std::setprecision(old_prec); \
    259             m_errorlog << message; \
     262            m_errorlog << msg; \
    260263            m_errorlog << m_context.str(); \
    261264            m_failure = true; \
     
    264267    } while(!True())
    265268
    266 #define LOLUNIT_FAIL(message) \
     269#define LOLUNIT_FAIL(msg) \
    267270    do { \
    268271        m_asserts++; \
     
    273276                   << __FILE__ << std::endl; \
    274277        m_errorlog << "forced failure" << std::endl; \
    275         m_errorlog << "- " << message << std::endl; \
     278        m_errorlog << "- " << msg << std::endl; \
    276279        m_errorlog << m_context.str(); \
    277280        m_failure = true; \
     
    284287        m_context << "- Context : " << #n << " = " << (n) << std::endl; \
    285288    } while(!True())
    286 
    287289#define LOLUNIT_UNSET_CONTEXT(n) \
    288290    m_context.str("")
     
    290292#define LOLUNIT_ASSERT(cond) \
    291293    LOLUNIT_ASSERT_GENERIC("", cond)
    292 
    293 #define LOLUNIT_ASSERT_MESSAGE(message, cond) \
    294     LOLUNIT_ASSERT_GENERIC("- " << message << std::endl, cond)
     294#define LOLUNIT_ASSERT_MESSAGE(m, cond) \
     295    LOLUNIT_ASSERT_GENERIC(LOLUNIT_MSG(m), cond)
     296
    295297
    296298#define LOLUNIT_ASSERT_EQUAL(a, b) \
    297     LOLUNIT_ASSERT_EQUAL_GENERIC("", a, b)
    298 
    299 #define LOLUNIT_ASSERT_EQUAL_MESSAGE(message, a, b) \
    300     LOLUNIT_ASSERT_EQUAL_GENERIC("- " << message << std::endl, a, b)
     299    LOLUNIT_ASSERT_OP(==, !!, "equality", "", a, b)
     300#define LOLUNIT_ASSERT_EQUAL_MESSAGE(m, a, b) \
     301    LOLUNIT_ASSERT_OP(==, !!, "equality", LOLUNIT_MSG(m), a, b)
     302#define LOLUNIT_ASSERT_DIFFERENT(a, b) \
     303    LOLUNIT_ASSERT_OP(!=, !!, "inequality", "", a, b)
     304#define LOLUNIT_ASSERT_DIFFERENT_MESSAGE(m, a, b) \
     305    LOLUNIT_ASSERT_OP(!=, !!, "inequality", LOLUNIT_MSG(m), a, b)
     306#define LOLUNIT_ASSERT_LESS(a, b) \
     307    LOLUNIT_ASSERT_OP(<, !!, "less than", "", a, b)
     308#define LOLUNIT_ASSERT_LESS_MESSAGE(m, a, b) \
     309    LOLUNIT_ASSERT_OP(<, !!, "less than", LOLUNIT_MSG(m), a, b)
     310#define LOLUNIT_ASSERT_LEQUAL(a, b) \
     311    LOLUNIT_ASSERT_OP(<=, !!, "less than or equal", "", a, b)
     312#define LOLUNIT_ASSERT_LEQUAL_MESSAGE(m, a, b) \
     313    LOLUNIT_ASSERT_OP(<=, !!, "less than or equal", LOLUNIT_MSG(m), a, b)
     314#define LOLUNIT_ASSERT_GREATER(a, b) \
     315    LOLUNIT_ASSERT_OP(>, !!, "greater than", "", a, b)
     316#define LOLUNIT_ASSERT_GREATER_MESSAGE(m, a, b) \
     317    LOLUNIT_ASSERT_OP(>, !!, "greater than", LOLUNIT_MSG(m), a, b)
     318#define LOLUNIT_ASSERT_GEQUAL(a, b) \
     319    LOLUNIT_ASSERT_OP(>=, !!, "greater than or equal", "", a, b)
     320#define LOLUNIT_ASSERT_GEQUAL_MESSAGE(m, a, b) \
     321    LOLUNIT_ASSERT_OP(>=, !!, "greater than or equal", LOLUNIT_MSG(m), a, b)
     322
     323
     324#define LOLUNIT_ASSERT_NOT_EQUAL(a, b) \
     325    LOLUNIT_ASSERT_OP(==, !, "not equality", "", a, b)
     326#define LOLUNIT_ASSERT_NOT_EQUAL_MESSAGE(m, a, b) \
     327    LOLUNIT_ASSERT_OP(==, !, "not equality", LOLUNIT_MSG(m), a, b)
     328#define LOLUNIT_ASSERT_NOT_DIFFERENT(a, b) \
     329    LOLUNIT_ASSERT_OP(!=, !, "not inequality", "", a, b)
     330#define LOLUNIT_ASSERT_NOT_DIFFERENT_MESSAGE(m, a, b) \
     331    LOLUNIT_ASSERT_OP(!=, !, "not inequality", LOLUNIT_MSG(m), a, b)
     332#define LOLUNIT_ASSERT_NOT_LESS(a, b) \
     333    LOLUNIT_ASSERT_OP(<, !, "not less than", "", a, b)
     334#define LOLUNIT_ASSERT_NOT_LESS_MESSAGE(m, a, b) \
     335    LOLUNIT_ASSERT_OP(<, !, "not less than", LOLUNIT_MSG(m), a, b)
     336#define LOLUNIT_ASSERT_NOT_LEQUAL(a, b) \
     337    LOLUNIT_ASSERT_OP(<=, !, "not less than or equal", "", a, b)
     338#define LOLUNIT_ASSERT_NOT_LEQUAL_MESSAGE(m, a, b) \
     339    LOLUNIT_ASSERT_OP(<=, !, "not less than or equal", LOLUNIT_MSG(m), a, b)
     340#define LOLUNIT_ASSERT_NOT_GREATER(a, b) \
     341    LOLUNIT_ASSERT_OP(>, !, "not greater than", "", a, b)
     342#define LOLUNIT_ASSERT_NOT_GREATER_MESSAGE(m, a, b) \
     343    LOLUNIT_ASSERT_OP(>, !, "not greater than", LOLUNIT_MSG(m), a, b)
     344#define LOLUNIT_ASSERT_NOT_GEQUAL(a, b) \
     345    LOLUNIT_ASSERT_OP(>=, !, "not greater than or equal", "", a, b)
     346#define LOLUNIT_ASSERT_NOT_GEQUAL_MESSAGE(m, a, b) \
     347    LOLUNIT_ASSERT_OP(>=, !, "not greater than or equal", LOLUNIT_MSG(m), a, b)
    301348
    302349#define LOLUNIT_ASSERT_DOUBLES_EQUAL(a, b, t) \
    303350    LOLUNIT_ASSERT_DOUBLES_EQUAL_GENERIC("", a, b, t)
    304 
    305 #define LOLUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(message, a, b, t) \
    306     LOLUNIT_ASSERT_DOUBLES_EQUAL_GENERIC("- " << message << std::endl, a, b, t)
     351#define LOLUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(msg, a, b, t) \
     352    LOLUNIT_ASSERT_DOUBLES_EQUAL_GENERIC("- " << msg << std::endl, a, b, t)
    307353
    308354} /* namespace lol */
Note: See TracChangeset for help on using the changeset viewer.