⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unit_test_result.cpp

📁 boost库提供标准的C++ API 配合dev c++使用,功能更加强大
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//  (C) Copyright Gennadiy Rozental 2001-2003.
//  Use, modification, and distribution are subject to the 
//  Boost Software License, Version 1.0. (See accompanying file 
//  LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

//  See http://www.boost.org/libs/test for the library home page.
//
//  File        : $RCSfile: unit_test_result.cpp,v $
//
//  Version     : $Revision: 1.22 $
//
//  Description : implements Unit Test Result reporting facility. Note that majority of 
//  implementation is hidden in this file using pimple idiom.
// ***************************************************************************

// Boost.Test
#include <boost/test/unit_test_result.hpp>
#include <boost/test/unit_test_suite.hpp>
#include <boost/test/detail/unit_test_parameters.hpp>

// BOOST
#include <boost/config.hpp>
#include <boost/scoped_ptr.hpp>
#include <boost/cstdlib.hpp>

// STL
#include <list>
#include <algorithm>
#include <cassert>
#include <iostream>
#include <iomanip>
#include <cmath>

# ifdef BOOST_NO_STDC_NAMESPACE
namespace std { using ::log10; using ::strncmp; }
# endif

namespace boost {

namespace unit_test_framework {

typedef unit_test_result* unit_test_result_ptr;

// ************************************************************************** //
// **************               report_formatter               ************** //
// ************************************************************************** //

class report_formatter {
public:
    // Destructor
    virtual         ~report_formatter() {}

    virtual void    start_result_report( std::ostream& where_to ) = 0;
    virtual void    finish_result_report( std::ostream& where_to ) = 0;

    virtual void    start_test_case_report( std::ostream& where_to, std::size_t indent, 
                                            std::string const& test_case_name, bool case_suite, bool failed ) = 0;

    virtual void    start_confirmation_report( std::ostream& where_to,
                                               std::string const& test_case_name, bool case_suite, bool failed,
                                               unit_test_counter num_failed, unit_test_counter num_expected ) = 0;

    virtual void    finish_test_case_report( std::ostream& where_to, std::size_t indent, 
                                             std::string const& test_case_name, bool case_suite, bool aborted ) = 0;

    virtual void    report_sub_test_cases_stat( std::ostream& where_to, std::size_t indent,
                                                unit_test_counter num_passed, unit_test_counter num_failed ) = 0;

    virtual void    report_assertions_stat( std::ostream& where_to, std::size_t indent,
                                            unit_test_counter num_passed, 
                                            unit_test_counter num_failed,
                                            unit_test_counter num_expected ) = 0;

};

//____________________________________________________________________________//

// ************************************************************************** //
// **************             hrf_report_formatter             ************** //
// ************************************************************************** //

class hrf_report_formatter : public report_formatter {
public:
    void    start_result_report( std::ostream& /* where_to */ ) {}
    void    finish_result_report( std::ostream& /* where_to */ ) {}

    void    start_test_case_report( std::ostream& where_to, std::size_t indent,
                                    std::string const& test_case_name, bool case_suite, bool failed )
    {
        where_to << "\n" << std::setw( indent ) << "" << "Test " << cs_name( case_suite ) << " " << quote( test_case_name )
                 << (failed ? " failed with:\n" : " passed with:\n");

    }

    void    start_confirmation_report( std::ostream& where_to,
                                       std::string const& test_case_name, bool case_suite, bool failed,
                                       unit_test_counter num_failed, unit_test_counter num_expected )
    {
        if( failed ) {
            if( num_failed == 0 ) {
                where_to << "\n*** errors detected in test " << cs_name( case_suite ) << " " << quote( test_case_name )
                         << "; see standard output for details\n";
                return;
            }


            where_to << "\n*** " << num_failed << ps_name( num_failed != 1, " failure" ) << " detected";
            
            if( num_expected > 0 )
                where_to << " (" << num_expected << ps_name( num_expected != 1, " failure" ) << " expected)";
            
            where_to << " in test " << cs_name( case_suite ) << " " << quote( test_case_name ) << "\n";
        }
        else
            where_to << "\n*** No errors detected\n";
    }

    void    finish_test_case_report( std::ostream& where_to, std::size_t indent, 
                                     std::string const& test_case_name, bool case_suite, bool aborted )
    {
        if( aborted )
            where_to << std::setw( indent ) << "" << "Test " << cs_name( case_suite ) << " " << quote( test_case_name )
                 << " was aborted due to uncaught exception, user assert or system error\n";

        where_to.flush();
    }

    void    report_sub_test_cases_stat( std::ostream& where_to, std::size_t indent, 
                                        unit_test_counter num_passed, unit_test_counter num_failed )
    {
        unit_test_counter total_test_cases = num_passed + num_failed;
        std::size_t       width = static_cast<std::size_t>( std::log10( (float)std::max( num_passed, num_failed ) ) ) + 1;

        where_to << std::setw( indent ) << "" << std::setw( width ) << num_passed
                 << " test " << ps_name( num_passed != 1, "case" ) << " out of " << total_test_cases << " passed\n"
                 << std::setw( indent ) << "" << std::setw( width ) << num_failed
                 << " test " << ps_name( num_failed != 1, "case" ) << " out of " << total_test_cases << " failed\n";

    }

    void    report_assertions_stat( std::ostream& where_to, std::size_t indent,
                                    unit_test_counter num_passed, unit_test_counter num_failed, unit_test_counter num_expected )
    {
        unit_test_counter total_assertions = num_passed + num_failed;
        std::size_t       width            = total_assertions > 0 
                                               ? static_cast<std::size_t>( std::log10( (float)std::max( num_passed, num_failed ) ) ) + 1
                                               : 1;
        
        where_to << std::setw( indent ) << "" << std::setw( width ) << num_passed 
                 << ps_name( num_passed != 1, " assertion" ) << " out of " << total_assertions << " passed\n"
                 << std::setw( indent ) << "" << std::setw( width ) << num_failed
                 << ps_name( num_failed != 1, " assertion" ) << " out of " << total_assertions << " failed\n";

        if( num_expected > 0 )
            where_to << std::setw( indent ) << "" << "while " << num_expected 
                     << ps_name( num_expected != 1, " failure" ) <<  " expected\n";
    }

private:
    static  std::string ps_name( bool p_s, std::string singular_form )
    { 
        return p_s ? singular_form.append( "s" ) : singular_form;
    }
    static  std::string cs_name( bool c_s )
    { 
        return c_s ? "case" : "suite";
    }
    static  std::string quote( std::string const& name )
    {
        return std::string( "\"" ).append( name ).append( "\"");
    }
};

// ************************************************************************** //
// **************             xml_report_formatter             ************** //
// ************************************************************************** //

class xml_report_formatter : public report_formatter {
public:
    void    start_result_report( std::ostream& where_to )
    {
        where_to << "<TestResult>\n";
    }
    void    finish_result_report( std::ostream& where_to )
    {
        where_to << "</TestResult>\n";
    }

    void    start_test_case_report( std::ostream& where_to, std::size_t indent, 
                                    std::string const& test_case_name, bool case_suite, bool failed )
    {
        where_to << std::setw( indent ) << ""
                 << "<" << ( case_suite ? "TestCase" : "TestSuite" ) 
                 << " name=\"" << test_case_name << '\"'
                 << " result=" << (failed ? "\"failed\"" : "\"passed\"" ) << ">\n";
    }

    void    start_confirmation_report( std::ostream& where_to,
                                               std::string const& test_case_name, bool case_suite, bool failed,
                                               unit_test_counter num_failed, unit_test_counter num_expected )
    {
        where_to << "<" << ( case_suite ? "TestCase" : "TestSuite" ) 
                 << " name=\"" << test_case_name << '\"'
                 << " result=" << (failed ? "\"failed\"" : "\"passed\"" );
        if( failed )
            where_to << " num_of_failures=" << num_failed
                     << " expected_failures=" << num_expected;

        where_to << ">\n";
    }

    void    finish_test_case_report( std::ostream& where_to, std::size_t indent, 
                                     std::string const& /* test_case_name */, bool case_suite, bool aborted )
    {
        if( aborted ) {
            where_to << std::setw( indent+2 ) << ""
                     << "<" << "aborted" 
                     << " reason=" << "\"due to uncaught exception, user assert or system error\""
                     << "/>\n";
        }

        where_to << std::setw( indent ) << ""
                 << "</" << ( case_suite ? "TestCase" : "TestSuite" ) << ">\n";
    }

    void    report_sub_test_cases_stat( std::ostream& where_to, std::size_t indent,
                                        unit_test_counter num_passed, unit_test_counter num_failed )
    {
        where_to << std::setw( indent+2 ) << ""
                 << "<SubTestCases"
                 << " passed=\"" << num_passed << '\"'
                 << " failed=\"" << num_failed << '\"'
                 << "/>\n";
    }

    void    report_assertions_stat( std::ostream& where_to, std::size_t indent,
                                    unit_test_counter num_passed, 
                                    unit_test_counter num_failed,
                                    unit_test_counter num_expected )
    {
        where_to << std::setw( indent+2 ) << ""
                 << "<Asssertions"
                 << " passed=\"" << num_passed << '\"'
                 << " failed=\"" << num_failed << '\"'
                 << " expected_failures=\"" << num_expected << '\"'
                 << "/>\n";
    }
};

// ************************************************************************** //
// **************               unit_test_result               ************** //
// ************************************************************************** //

struct unit_test_result::Impl {
    unit_test_result_ptr            m_parent;
    std::list<unit_test_result_ptr> m_children;
    unit_test_counter               m_assertions_passed;
    unit_test_counter               m_assertions_failed;
    unit_test_counter               m_expected_failures;
    unit_test_counter               m_test_cases_passed;
    unit_test_counter               m_test_cases_failed;
    bool                            m_exception_caught;
    std::string                     m_test_case_name;

    static boost::scoped_ptr<unit_test_result>  m_head;
    static unit_test_result_ptr                 m_curr;
    static boost::scoped_ptr<report_formatter>  m_report_formatter;

    bool                            has_failed()
    {
        return m_test_cases_failed != 0 || m_assertions_failed != m_expected_failures || m_exception_caught;
    }
    int                             result_code()
    { 
        return has_failed() 
                    ? ( (m_assertions_failed != 0) 
                            ? boost::exit_test_failure 
                            : boost::exit_exception_failure )
                    : boost::exit_success; 
    }
};

boost::scoped_ptr<unit_test_result> unit_test_result::Impl::m_head;
unit_test_result_ptr                unit_test_result::Impl::m_curr = unit_test_result_ptr();
boost::scoped_ptr<report_formatter> unit_test_result::Impl::m_report_formatter( new hrf_report_formatter );

//____________________________________________________________________________//

unit_test_result::unit_test_result( unit_test_result_ptr parent, std::string const& test_case_name, unit_test_counter exp_fail )
: m_pimpl( new Impl )
{
    m_pimpl->m_parent            = parent;
    m_pimpl->m_test_case_name    = test_case_name;

    m_pimpl->m_assertions_passed = 0;
    m_pimpl->m_assertions_failed = 0;
    m_pimpl->m_expected_failures = exp_fail;
    m_pimpl->m_test_cases_passed = 0;
    m_pimpl->m_test_cases_failed = 0;
    m_pimpl->m_exception_caught  = false;

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -