• [boost] : lightweight_test库


    lightweight_test轻量级单元测试框架, 只支持最基本的单元测试, 不支持测试用例, 测试套件的概念, 简单小巧, 适合要求不高或者快速测试的工作.

    基本用法

    需要包含头文件#include <boost/core/lightweight_test.hpp>
    lightweight_test库定义了五个断言宏

    BOOST_TEST(e) 断言表达式成立
    BOOST_ERROR(s) 直接断言失败, 输出消息s
    BOOST_TEST_EQ(e1, e2) 断言两个表达式相等
    BOOST_TEST_NE(e1, e2) 断言两个表达式不等
    BOOST_TEST_THROWS(e, ex) 断言表达式e抛出异常ex
    // Copyright (c) 2015
    // Author: Chrono Law
    #include <iostream>
    using namespace std;
    
    #include <boost/smart_ptr.hpp> // shared_ptr
    #include <boost/core/lightweight_test.hpp>
    
    int main()
    {
        auto p = make_shared<int>(10);
    
        BOOST_TEST(*p == 10);
        BOOST_TEST(p.unique());
        BOOST_TEST(p);
    
        BOOST_TEST_EQ(p.use_count(), 1);
        BOOST_TEST_NE(*p, 20);
    
        p.reset();
        BOOST_TEST(!p);
    
        BOOST_TEST_THROWS(*p, std::runtime_error);
        BOOST_ERROR("error accured!!");
    
        return boost::report_errors(); // 输出测试报告
    }
    

    输出以下信息:

    lightweight_test.cpp(23): Exception 'std::runtime_error' not thrown in function 'int main()'
    lightweight_test.cpp(24): error accured!! in function 'int main()'
    2 errors detected.
    

    注意: 在测试结束时必须调用boost::report_errors(), 否则会发生BOOST_ASSERT断言错误.

    测试元编程

    lightweight_test提供对元编程的有限支持, 需要包含头文件#include <boost/core/lightweight_test_trait.hpp>

    // Copyright (c) 2015
    // Author: Chrono Law
    #include <type_traits> // is_integral<T>, is_function<T>
    #include <iostream>
    using namespace std;
    
    #include <boost/core/lightweight_test.hpp>
    #include <boost/core/lightweight_test_trait.hpp>
    
    int main()
    {
        BOOST_TEST_TRAIT_TRUE((is_integral<int>));
        BOOST_TEST_TRAIT_FALSE((is_function<int>));
    
        return boost::report_errors();
    }
    
  • 相关阅读:
    LOJ #6008. 「网络流 24 题」餐巾计划
    P2144 [FJOI2007]轮状病毒
    随记
    1010: [HNOI2008]玩具装箱toy(斜率优化)
    HDU 3507 Print Article(斜率优化)
    4819: [Sdoi2017]新生舞会(分数规划)
    POJ 2976 Dropping tests(01分数规划)
    spoj 104 Highways(Matrix-tree定理)
    dp专练
    4152: [AMPPZ2014]The Captain
  • 原文地址:https://www.cnblogs.com/moon1992/p/7479716.html
Copyright © 2020-2023  润新知