Is it possible to use BOOST_PARAM_TEST_CASE with automatic registration on boost::test?

前端 未结 6 840
名媛妹妹
名媛妹妹 2021-02-14 03:50

Is it possible to mix up the BOOST_AUTO_TEST_CASE and BOOST_AUTO_TEST_CASE_TEMPLATE macros with the BOOST_PARAM_TEST_CASE in any way? I\'m

6条回答
  •  忘了有多久
    2021-02-14 04:41

    The solution provided by @Omnifarious works works, but requires a C++11 compiler.

    Adapting that solution for a C++03 compiler:

    #include 
    #include 
    
    #define BOOST_FIXTURE_PARAM_TEST_CASE( test_name, F, P, mbegin, mend )  \
    struct test_name : public F                                             \
    {                                                                       \
        typedef P param_t;                                                  \
        void test_method(const param_t &);                                  \
    };                                                                      \
                                                                            \
    void BOOST_AUTO_TC_INVOKER( test_name )(const test_name::param_t ¶m) \
    {                                                                       \
        test_name t;                                                        \
        t.test_method(param);                                               \
    }                                                                       \
                                                                            \
    BOOST_AUTO_TU_REGISTRAR( test_name )(                                   \
        boost::unit_test::make_test_case(                                   \
           &BOOST_AUTO_TC_INVOKER( test_name ), #test_name,                 \
           (mbegin), (mend)));                                              \
                                                                            \
    void test_name::test_method(const param_t ¶m)                       \
    
    // *******
    
    #define BOOST_AUTO_PARAM_TEST_CASE( test_name, param_type, mbegin, mend )           \
       BOOST_FIXTURE_PARAM_TEST_CASE( test_name,                            \
                                      BOOST_AUTO_TEST_CASE_FIXTURE,         \
                                      param_type,                           \
                                      mbegin, mend)
    

    This solution is slightly different is usage. Since there is no declspec in C++03, the type of the parameter object cannot be automatically deduced. We must pass it in as a parameter to BOOST_AUTO_PARAM_TEST_CASE:

    class FooTestParam                           
    {    
    public:                        
        std::string mS;    
    
        FooTestParam (int n)    
        {            
            std::stringstream ss;    
            ss << n;         
            mS = ss.str();                            
        }    
    };         
    
    FooTestParam fooParams [] =    
    {         
        FooTestParam (42),    
        FooTestParam (314)    
    };           
    
    BOOST_AUTO_PARAM_TEST_CASE (TestFoo, FooTestParam, fooParams, fooParams + 2)        
    {                                                                                   
        const std::string testVal = param.mS;                                           
    }                                                                                   
    
    BOOST_AUTO_TEST_CASE (TestAddressField)                                             
    {                                                                                   
        const uint32_t raw = 0x0100007f;    // 127.0.0.1                                
        const uint8_t expected[4] = {127, 0, 0, 1};                                     
        const Mdi::AddressField& field = *reinterpret_cast  (&raw);    
        for (size_t i = 0; i < 4; ++i)                                                  
            BOOST_CHECK_EQUAL (field[i], expected[i]);                                  
    }                                                                                   
    

提交回复
热议问题