24

我正在为包含static_asserts 的源代码库编写单元测试套件。我想保证这些static_asserts 在设计方面不会比他们希望做的更多或更少。所以我希望能够测试它们。

我当然可以添加无法编译的接口单元测试,导致static asserts 被多种方式违反,并评论或#if 0全部删除,我个人向用户保证,如果其中任何一个未评论,那么他们将观察到该库无法编译。

但这将是相当荒谬的。相反,我想要一些设备,在单元测试套件的上下文中,将 a 替换static_assert为等效引发的运行时异常,测试框架可以捕获并有效报告:此代码将static_assert在真实的建造。

我是否忽略了一些明显的原因,为什么这将是一个愚蠢的想法?

如果没有,怎么可能做到?宏观仪器是一种显而易见的方法,我不排除它。但也许,最好是使用模板专业化或 SFINAE 方法?

4

1 回答 1

14

由于我似乎对这个问题很感兴趣,所以我为自己想出了一个答案,头文件基本上是这样的:

exceptionized_static_assert.h

#ifndef TEST__EXCEPTIONALIZE_STATIC_ASSERT_H
#define TEST__EXCEPTIONALIZE_STATIC_ASSERT_H

/* Conditionally compilable apparatus for replacing `static_assert`
    with a runtime exception of type `exceptionalized_static_assert`
    within (portions of) a test suite.
*/
#if TEST__EXCEPTIONALIZE_STATIC_ASSERT == 1

#include <string>
#include <stdexcept>

namespace test {

struct exceptionalized_static_assert : std::logic_error
{
    exceptionalized_static_assert(char const *what)
    : std::logic_error(what){};
    virtual ~exceptionalized_static_assert() noexcept {}
};

template<bool Cond>
struct exceptionalize_static_assert;

template<>
struct exceptionalize_static_assert<true>
{
    explicit exceptionalize_static_assert(char const * reason) {
        (void)reason;
    }
};


template<>
struct exceptionalize_static_assert<false>
{
    explicit exceptionalize_static_assert(char const * reason) {
        std::string s("static_assert would fail with reason: ");
        s += reason;
        throw exceptionalized_static_assert(s.c_str());
    }
};

} // namespace test

// A macro redefinition of `static_assert`
#define static_assert(cond,gripe) \
    struct _1_test \
    : test::exceptionalize_static_assert<cond> \
    {   _1_test() : \
        test::exceptionalize_static_assert<cond>(gripe){}; \
    }; \
    _1_test _2_test

#endif // TEST__EXCEPTIONALIZE_STATIC_ASSERT == 1

#endif // EOF

此标头仅包含在测试套件中,然后它将使static_assert只有在构建测试套件时才可见的宏重新定义可见

`-DTEST__EXCEPTIONALIZE_STATIC_ASSERT=1`    

这个装置的使用可以用一个玩具模板库来勾勒出来:

我的模板.h

#ifndef MY_TEMPLATE_H
#define MY_TEMPLATE_H

#include <type_traits>

template<typename T>
struct my_template
{
    static_assert(std::is_pod<T>::value,"T must be POD in my_template<T>");

    explicit my_template(T const & t = T())
    : _t(t){}
    // ...
    template<int U>
    static int increase(int i) {
        static_assert(U != 0,"I cannot be 0 in my_template<T>::increase<I>");
        return i + U;
    }
    template<int U>
    static constexpr int decrease(int i) {
        static_assert(U != 0,"I cannot be 0 in my_template<T>::decrease<I>");
        return i - U;
    }
    // ...
    T _t;
    // ...  
};

#endif // EOF

试着想象一下代码足够大和复杂,以至于你不能随便调查它并挑选出static_asserts 并让自己确信你知道它们为什么在那里并且它们实现了它们的设计目的。您信任回归测试。

这是一个玩具回归测试套件my_template.h

测试.cpp

#include "exceptionalized_static_assert.h"
#include "my_template.h"
#include <iostream>

template<typename T, int I>
struct a_test_template
{
    a_test_template(){};
    my_template<T> _specimen;
    //...
    bool pass = true;
};

template<typename T, int I>
struct another_test_template
{
    another_test_template(int i) {
        my_template<T> specimen;
        auto j = specimen.template increase<I>(i);
        //...
        (void)j;
    }
    bool pass = true;
};

template<typename T, int I>
struct yet_another_test_template
{
    yet_another_test_template(int i) {
        my_template<T> specimen;
        auto j = specimen.template decrease<I>(i);
        //...
        (void)j;
    }
    bool pass = true;
};

using namespace std;

int main()
{
    unsigned tests = 0;
    unsigned passes = 0;

    cout << "Test: " << ++tests << endl;    
    a_test_template<int,0> t0;
    passes += t0.pass;
    cout << "Test: " << ++tests << endl;    
    another_test_template<int,1> t1(1);
    passes += t1.pass;
    cout << "Test: " << ++tests << endl;    
    yet_another_test_template<int,1> t2(1);
    passes += t2.pass;
#if TEST__EXCEPTIONALIZE_STATIC_ASSERT == 1
    try {
        // Cannot instantiate my_template<T> with non-POD T
        using type = a_test_template<int,0>;
        cout << "Test: " << ++tests << endl;
        a_test_template<type,0> specimen;

    }
    catch(test::exceptionalized_static_assert const & esa) {
        ++passes;
        cout << esa.what() << endl;
    }
    try {
        // Cannot call my_template<T>::increase<I> with I == 0
        cout << "Test: " << ++tests << endl;
        another_test_template<int,0>(1);
    }
    catch(test::exceptionalized_static_assert const & esa) {
        ++passes;
        cout << esa.what() << endl;
    }
    try {
        // Cannot call my_template<T>::decrease<I> with I == 0
        cout << "Test: " << ++tests << endl;
        yet_another_test_template<int,0>(1);
    }
    catch(test::exceptionalized_static_assert const & esa) {
        ++passes;
        cout << esa.what() << endl;
    }
#endif // TEST__EXCEPTIONALIZE_STATIC_ASSERT == 1
    cout << "Passed " << passes << " out of " << tests << " tests" << endl;
    cout << (passes == tests ? "*** Success :)" : "*** Failure :(") << endl; 
    return 0;
}

// EOF

test.cpp至少可以使用 gcc 6.1、clang 3.8 和 option -std=c++14或 VC++ 19.10.24631.0 和 option进行编译/std:c++latest。首先这样做而不定义 TEST__EXCEPTIONALIZE_STATIC_ASSERT (或定义它= 0)。然后运行,输出应该是:

Test: 1
Test: 2
Test: 3
Passed 3 out of 3 tests
*** Success :)

如果你然后重复,但编译-DTEST__EXCEPTIONALIZE_STATIC_ASSERT=1

Test: 1
Test: 2
Test: 3
Test: 4
static_assert would fail with reason: T must be POD in my_template<T>
Test: 5
static_assert would fail with reason: I cannot be 0 in my_template<T>::increase<I>
Test: 6
static_assert would fail with reason: I cannot be 0 in my_template<T>::decrease<I>
Passed 6 out of 6 tests
*** Success :)

try/catch显然,在静态断言测试用例中对块进行重复编码是乏味的,但是在设置一个真实且受人尊敬的单元测试框架时,人们会期望它会打包异常测试设备以在您的视线之外生成这样的东西。例如,在 googletest 中,您可以编写以下代码:

TYPED_TEST(t_my_template,insist_non_zero_increase)
{
    ASSERT_THROW(TypeParam::template increase<0>(1),
        exceptionalized_static_assert);
}

现在我可以回到我对世界末日日期的计算 :)

于 2013-07-02T16:58:28.280 回答