zoukankan      html  css  js  c++  java
  • Boost.Test ASSERT断言用法

    使用文件

    包含头文件即可:assert.hpp

    主要就是5个断言:

    BOOST_ASSERT
    BOOST_ASSERT_MSG
    BOOST_VERIFY
    BOOST_VERIFY_MSG
    BOOST_ASSERT_IS_VOID

    官方文档:

    BOOST_ASSERT
    BOOST_ASSERT_MSG
    BOOST_VERIFY
    BOOST_VERIFY_MSG
    BOOST_ASSERT_IS_VOID

    BOOST_ASSERT

    The header <boost/assert.hpp> defines the macro BOOST_ASSERT, which is similar to the standard assert macro defined in <cassert>. The macro is intended to be used in both Boost libraries and user code.

    头文件<boost/assert.hpp>定义了宏BOOST_ASSERT,它类似于中定义的标准断言宏。宏的目的是在Boost库和用户代码中使用。

    这个宏,在定义了NDEBUG时,会被直接禁用掉

    • By default, BOOST_ASSERT(expr) expands to assert(expr).

    默认的,BOOST_ASSERT(expr) 会扩展成 assert(expr).

    • If the macro BOOST_DISABLE_ASSERTS is defined when <boost/assert.hpp> is included, BOOST_ASSERT(expr) expands to ((void)0), regardless of whether the macro NDEBUG is defined. This allows users to selectively disable BOOST_ASSERT without affecting the definition of the standard assert.

    如果在包含头文件 <boost/assert.hpp> 时,定义了宏boost_disable_assert, BOOST_ASSERT(expr)扩展到((void)0),不管是否定义了宏NDEBUG。这允许用户选择性地禁用BOOST_ASSERT,而不影响标准断言的定义。

    • If the macro BOOST_ENABLE_ASSERT_HANDLER is defined when <boost/assert.hpp> is included, BOOST_ASSERT(expr) expands to

    包含头文件<boost/assert.hpp>的时候,如果定义了宏:BOOST_ENABLE_ASSERT_HANDLER,

    BOOST_ASSERT(expr)将会扩展为:

    1. (BOOST_LIKELY(!!(expr))?((void)0):::boost::assertion_failed(#expr, BOOST_CURRENT_FUNCTION, __FILE__, __LINE__))
     

    That is, it evaluates expr and if it's false, calls ::boost::assertion_failed(#expr, BOOST_CURRENT_FUNCTION, __FILE__, __LINE__). This is true regardless of whether NDEBUG is defined.

    boost::assertion_failed is declared in <boost/assert.hpp> as

    1. namespace boost
    2. {
    3. void assertion_failed(charconst* expr,charconst* function,charconst* file,long line);
    4. }
     

    but it is never defined. The user is expected to supply an appropriate definition.

    • If the macro BOOST_ENABLE_ASSERT_DEBUG_HANDLER is defined when <boost/assert.hpp> is included, BOOST_ASSERT(expr) expands to ((void)0) when NDEBUGis defined. Otherwise the behavior is as if BOOST_ENABLE_ASSERT_HANDLER has been defined.

    As is the case with <cassert><boost/assert.hpp> can be included multiple times in a single translation unit. BOOST_ASSERT will be redefined each time as specified above.

    BOOST_ASSERT_MSG

    The macro BOOST_ASSERT_MSG is similar to BOOST_ASSERT, but it takes an additional argument, a character literal, supplying an error message.

    • By default, BOOST_ASSERT_MSG(expr,msg) expands to assert((expr)&&(msg)).

    • If the macro BOOST_DISABLE_ASSERTS is defined when <boost/assert.hpp> is included, BOOST_ASSERT_MSG(expr,msg) expands to ((void)0), regardless of whether the macro NDEBUG is defined.

    • If the macro BOOST_ENABLE_ASSERT_HANDLER is defined when <boost/assert.hpp> is included, BOOST_ASSERT_MSG(expr,msg) expands to

    (BOOST_LIKELY(!!(expr))? ((void)0): ::boost::assertion_failed_msg(#expr, msg, BOOST_CURRENT_FUNCTION, __FILE__, __LINE__))

    This is true regardless of whether NDEBUG is defined.

    boost::assertion_failed_msg is declared in <boost/assert.hpp> as

    1. namespace boost
    2. {
    3. void assertion_failed_msg(charconst* expr,charconst* msg,charconst* function,charconst* file,long line);
    4. }
     

    but it is never defined. The user is expected to supply an appropriate definition.

    • If the macro BOOST_ENABLE_ASSERT_DEBUG_HANDLER is defined when <boost/assert.hpp> is included, BOOST_ASSERT_MSG(expr) expands to ((void)0) when NDEBUG is defined. Otherwise the behavior is as if BOOST_ENABLE_ASSERT_HANDLER has been defined.

    As is the case with <cassert><boost/assert.hpp> can be included multiple times in a single translation unit. BOOST_ASSERT_MSG will be redefined each time as specified above.

    BOOST_VERIFY

    The macro BOOST_VERIFY has the same behavior as BOOST_ASSERT, except that the expression that is passed to BOOST_VERIFY is always evaluated. This is useful when the asserted expression has desirable side effects; it can also help suppress warnings about unused variables when the only use of the variable is inside an assertion.

    • If the macro BOOST_DISABLE_ASSERTS is defined when <boost/assert.hpp> is included, BOOST_VERIFY(expr) expands to ((void)(expr)).

    • If the macro BOOST_ENABLE_ASSERT_HANDLER is defined when <boost/assert.hpp> is included, BOOST_VERIFY(expr) expands to BOOST_ASSERT(expr).

    • Otherwise, BOOST_VERIFY(expr) expands to ((void)(expr)) when NDEBUG is defined, to BOOST_ASSERT(expr) when it's not.

    BOOST_VERIFY_MSG

    The macro BOOST_VERIFY_MSG is similar to BOOST_VERIFY, with an additional parameter, an error message.

    • If the macro BOOST_DISABLE_ASSERTS is defined when <boost/assert.hpp> is included, BOOST_VERIFY_MSG(expr,msg) expands to ((void)(expr)).

    • If the macro BOOST_ENABLE_ASSERT_HANDLER is defined when <boost/assert.hpp> is included, BOOST_VERIFY_MSG(expr,msg) expands to BOOST_ASSERT_MSG(expr,msg).

    • Otherwise, BOOST_VERIFY_MSG(expr,msg) expands to ((void)(expr)) when NDEBUG is defined, to BOOST_ASSERT_MSG(expr,msg) when it's not.


     

    BOOST_ASSERT_IS_VOID

    The macro BOOST_ASSERT_IS_VOID is defined when BOOST_ASSERT and BOOST_ASSERT_MSG, are expanded to ((void)0). This macro is useful to avoid compiling and potentially running code that is only intended to prepare data to be used in the assertion.

    void MyContainer::erase(iterator i)
    {
      //Some sanity checks, data must be ordered
      #ifndef BOOST_ASSERT_IS_VOID
         if(i != c.begin()){
            iterator prev = i;
            --prev;
            BOOST_ASSERT(*prev < *i);
         }
         else if(i != c.end()){
            iterator next = i;
            ++next;
            BOOST_ASSERT(*i < *next);
         }
      #endif
      this->erase_impl(i);
    }
    

    • By default, BOOST_ASSERT_IS_VOID is defined if NDEBUG is defined.

    • If the macro BOOST_DISABLE_ASSERTS is defined BOOST_ASSERT_IS_VOID is always defined.

    • If the macro BOOST_ENABLE_ASSERT_HANDLER is defined BOOST_ASSERT_IS_VOID is never defined.

    • If the macro BOOST_ENABLE_ASSERT_DEBUG_HANDLER, then BOOST_ASSERT_IS_VOID is defined when NDEBUG is defined.

  • 相关阅读:
    Notepad++如何对比文件 Notepad++对比两个文件代码方法
    如何识别图片中的文字
    如何用DOS命令查看占用某端口的程序及PID号
    java使用POI获取sheet、行数、列数
    程序中的.htaccess文件是做什么的
    阿里云服务器配置https(总结)
    legend3---19、要更多的从服务器端控制元素的显示和隐藏,而不要是页面端
    Laravel 中 Session 的使用问题(dd()导致laravel中session取值问题)
    legend3---lamp.sh常用操作
    阿里云服务器发送邮件:Connection could not be established with host smtp.qq.com [Connection timed out #110]
  • 原文地址:https://www.cnblogs.com/xujintao/p/8325238.html
Copyright © 2011-2022 走看看