C++11 模板元编程-TLP测试框架-1.测试断言,用例

转载https://www.jianshu.com/p/b56d59f77d53

#define ASSERT_TRUE(T) \
static_assert(__value(T), "Assert Failed: expect "#T" be true, but be false!")

#define ASSERT_FALSE(T)                 \
static_assert(!(__value(T)), "Assert Failed: expect "#T" be false, but be true!")

#define ASSERT_EQ(T, Expected)          \
static_assert(__value(__is_eq(T, Expected)), 
"Assert Failed: expect "#T" be equal to "#Expected"!")

#define ASSERT_NE(T, Expected)          \
static_assert(!(__value(__is_eq(T, Expected))), 
"Assert Failed: expect "#T" be not equal to "#Expected"!")

// example
ASSERT_TRUE(__bool(true));
ASSERT_TRUE(__not(__false()));
ASSERT_FALSE(__or(__true(), __false()));

ASSERT_EQ(__int(0), __int(0));
ASSERT_NE(__int(0), __int(1));
ASSERT_EQ(__if(__true(), int, char), int);
ASSERT_EQ(__if(__false(), int, char), char);

// NullType仅有类声明,所以不能实例化。NullType被TLP库用于各种计算返回的无效值中。
// 对此有一个元函数__valid()专门用于判断表达式的值是否为NullType
struct NullType;
#define __null() NullType

2.测试用例: 

// 编译器自动为类生成一个文件内不重复的类名。
#define TEST(name) struct UNIQUE_NAME(tlp_test_)

TEST(“operator add on int type”)
{
    using num1 = __int(10);
    using num2 = __int(2);
    ASSERT_EQ(__add(num1, num2), __int(12));
};    // 后花括号后面需要以一个分号结束

3.测试套件

#define FIXTURE(name) namespace tlp_fixture_##name

FIXTURE(TestMetaFunctionInFixture)
{
    template<typename T, typename U>
    using LargerType = __if(__bool(sizeof(T) > sizeof(U)), T, U);

    struct TwoBytesType { char dummy[2]; };

    TEST("int should be larger than two bytes")
    {
        ASSERT_EQ(LargerType<int, TwoBytesType>::Result, int);
    };

    TEST("char should be smaller than two bytes")
    {
        ASSERT_EQ(LargerType<char, TwoBytesType>::Result, TwoBytesType);
    };
}

猜你喜欢

转载自blog.csdn.net/Hu_yilang/article/details/88061372
今日推荐