cpptest 用列

【用列1】

 

#include "cppTest.h"

 

class failTest1 : public Test::Suite { 

    void always_fail( )  // 要测试的函数

    {  

        TEST_FAIL("This always fails!/n"); 

    } 

public: 

    failTest1() { TEST_ADD(failTest1::always_fail); } 

}; 

 

int main ( ) {

    failTest1 test1;

    Test::TextOutput output(Test::TextOutput::Terse);

    return test1.run(output) ? 1 : 0;

}

【用列2】
// cpptest自带的测试文件 //
#include <cstdlib>
#include <cstring>
#include <iostream>
#ifdef _MSC_VER
    #pragma warning (disable: 4290)
#endif
#include "cpptest.h"
using namespace std;
// Tests unconditional fail asserts
//
class FailTestSuite : public Test::Suite
{
public:
    FailTestSuite()
    {
        TEST_ADD(FailTestSuite::success)
        TEST_ADD(FailTestSuite::always_fail)
    }
private:
    void success() {}
    
    void always_fail()
    {
        // This will always fail
        //
        TEST_FAIL("unconditional fail");
    }
};
// Tests compare asserts
//
class CompareTestSuite : public Test::Suite
{
public:
    CompareTestSuite()
    {
        TEST_ADD(CompareTestSuite::success)
        TEST_ADD(CompareTestSuite::compare)
        TEST_ADD(CompareTestSuite::delta_compare)
    }
    
private:
    void success() {}
    
    void compare()
    {
        // Will succeed since the expression evaluates to true
        //
        TEST_ASSERT(1 + 1 == 2)
        
        // Will fail since the expression evaluates to false
        //
        TEST_ASSERT(0 == 1);
    }
    
    void delta_compare()
    {
        // Will succeed since the expression evaluates to true
        //
        TEST_ASSERT_DELTA(0.5, 0.7, 0.3);
        
        // Will fail since the expression evaluates to false
        //
        TEST_ASSERT_DELTA(0.5, 0.7, 0.1);
    }
};
// Tests throw asserts
//
class ThrowTestSuite : public Test::Suite
{
public:
    ThrowTestSuite()
    {
        TEST_ADD(ThrowTestSuite::success)
        TEST_ADD(ThrowTestSuite::test_throw)
    }
    
private:
    void success() {}
    
    void test_throw()
    {
        // Will fail since the none of the functions throws anything
        //
        TEST_THROWS_MSG(func(), int, "func() does not throw, expected int exception")
        TEST_THROWS_MSG(func_no_throw(), int, "func_no_throw() does not throw, expected int exception")
        TEST_THROWS_ANYTHING_MSG(func(), "func() does not throw, expected any exception")
        TEST_THROWS_ANYTHING_MSG(func_no_throw(), "func_no_throw() does not throw, expected any exception")
        
        // Will succeed since none of the functions throws anything
        //
        TEST_THROWS_NOTHING(func())
        TEST_THROWS_NOTHING(func_no_throw())
        
        // Will succeed since func_throw_int() throws an int
        //
        TEST_THROWS(func_throw_int(), int)
        TEST_THROWS_ANYTHING(func_throw_int())
        
        // Will fail since func_throw_int() throws an int (not a float)
        //
        TEST_THROWS_MSG(func_throw_int(), float, "func_throw_int() throws an int, expected a float exception")
        TEST_THROWS_NOTHING_MSG(func_throw_int(), "func_throw_int() throws an int, expected no exception at all")
    }
    
    void func() {}
    void func_no_throw() throw() {}
    void func_throw_int() throw(int) { throw 13; }
};
enum OutputType
{
    Compiler,
    Html,
    TextTerse,
    TextVerbose
};
static void
usage()
{
    cout << "usage: mytest [MODE]/n"
         << "where MODE may be one of:/n"
         << "  --compiler/n"
         << "  --html/n"
         << "  --text-terse (default)/n"
         << "  --text-verbose/n";
    exit(0);
}
static auto_ptr<Test::Output>
cmdline(int argc, char* argv[])
{
    if (argc > 2)
        usage(); // will not return
    
    Test::Output* output = 0;
    
    if (argc == 1)
        output = new Test::TextOutput(Test::TextOutput::Verbose);
    else
    {
        const char* arg = argv[1];
        if (strcmp(arg, "--compiler") == 0)
            output = new Test::CompilerOutput;
        else if (strcmp(arg, "--html") == 0)
            output =  new Test::HtmlOutput;
        else if (strcmp(arg, "--text-terse") == 0)
            output = new Test::TextOutput(Test::TextOutput::Terse);
        else if (strcmp(arg, "--text-verbose") == 0)
            output = new Test::TextOutput(Test::TextOutput::Verbose);
        else
        {
            cout << "invalid commandline argument: " << arg << endl;
            usage(); // will not return
        }
    }
    
    return auto_ptr<Test::Output>(output);
}
// Main test program
//
int
main(int argc, char* argv[])
{
    try
    {
        // Demonstrates the ability to use multiple test suites
        //
        Test::Suite ts;
        ts.add(auto_ptr<Test::Suite>(new FailTestSuite));
        ts.add(auto_ptr<Test::Suite>(new CompareTestSuite));
        ts.add(auto_ptr<Test::Suite>(new ThrowTestSuite));
        
        // Run the tests
        //
        auto_ptr<Test::Output> output(cmdline(argc, argv));
        ts.run(*output, true);
        
        Test::HtmlOutput* const html = dynamic_cast<Test::HtmlOutput*>(output.get());
            
        // 用html格式输出结果
        //if (html)
        //    html->generate(cout, true, "MyTest");
    }
    catch (...)
    {
        cout << "unexpected exception encountered/n";
        return EXIT_FAILURE;
    }
    return EXIT_SUCCESS;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值