如何对位操作逻辑进行单元测试
How to unit test bit manipulation logic
我有一种将 RGBA 转换为 BGRA 的方法。下面是方法
unsigned int ConvertRGBAToBGRA(unsigned int v) {
unsigned char r = (v)& 0xFF;
unsigned char g = (v >> 8) & 0xFF;
unsigned char b = (v >> 16) & 0xFF;
unsigned char a = (v >> 24) & 0xFF;
return (a << 24) | (r << 16) | (g << 8) | b;
};
我怎样才能很好地进行单元测试?有没有办法我可以读回这些位并以某种方式对该方法进行单元测试?
我正在使用googletests
您可以通过指针映射到数组来将值拆分为四个字节。然后交换字节。
uint8_t* pV= reinterpret_cast<uint8_t*>(&V);
uint8_t Swap= pV[1]; pV[1]= pV[3]; pV[3]= Swap;
受@Yves Daoust 评论的启发,你为什么不能像下面这样写一系列检查?您可以使用 C++14 的漂亮格式:
unsigned int ConvertRGBAToBGRA(unsigned int v) {
unsigned char r = (v)&0xFF;
unsigned char g = (v >> 8) & 0xFF;
unsigned char b = (v >> 16) & 0xFF;
unsigned char a = (v >> 24) & 0xFF;
return (a << 24) | (r << 16) | (g << 8) | b;
};
TEST(ConvertRGBAToBGRATest, Test1) {
EXPECT_EQ(ConvertRGBAToBGRA(0x12'34'56'78), 0x12'78'56'34);
EXPECT_EQ(ConvertRGBAToBGRA(0x12'78'56'34), 0x12'34'56'78);
EXPECT_EQ(ConvertRGBAToBGRA(0x11'11'11'11), 0x11'11'11'11);
EXPECT_EQ(ConvertRGBAToBGRA(0x00'00'00'00), 0x00'00'00'00);
EXPECT_EQ(ConvertRGBAToBGRA(0xAa'Bb'Cc'Dd), 0xAa'Dd'Cc'Bb);
EXPECT_EQ(ConvertRGBAToBGRA(ConvertRGBAToBGRA(0x12'34'56'78)), 0x12'34'56'78);
EXPECT_EQ(ConvertRGBAToBGRA(ConvertRGBAToBGRA(0x12'78'56'34)), 0x12'78'56'34);
EXPECT_EQ(ConvertRGBAToBGRA(ConvertRGBAToBGRA(0x11'11'11'11)), 0x11'11'11'11);
EXPECT_EQ(ConvertRGBAToBGRA(ConvertRGBAToBGRA(0x00'00'00'00)), 0x00'00'00'00);
EXPECT_EQ(ConvertRGBAToBGRA(ConvertRGBAToBGRA(0xAa'Bb'Cc'Dd)), 0xAa'Bb'Cc'Dd);
}
实例:https://godbolt.org/z/eEajYYYsf
您还可以定义一个 custom matcher 并使用 EXPECT_THAT
宏:
// A custom matcher for comparing BGRA and RGBA.
MATCHER_P(IsBgraOf, n, "") {
return ((n & 0xFF000000) == (arg & 0xFF000000)) &&
((n & 0x00FF0000) == ((arg << 16) & 0x00FF0000)) &&
((n & 0x0000FF00) == (arg & 0x0000FF00));
}
TEST(ConvertRGBAToBGRATest, WithExpectThat) {
EXPECT_THAT(ConvertRGBAToBGRA(0x12'34'56'78), IsBgraOf(0x12'34'56'78));
EXPECT_THAT(ConvertRGBAToBGRA(0x12'78'56'34), IsBgraOf(0x12'78'56'34));
EXPECT_THAT(ConvertRGBAToBGRA(0xAa'Bb'Cc'Dd), IsBgraOf(0xAa'Bb'Cc'Dd));
EXPECT_THAT(ConvertRGBAToBGRA(0x00'00'00'00), IsBgraOf(0x00'00'00'00));
EXPECT_THAT(ConvertRGBAToBGRA(0x11'11'11'11), IsBgraOf(0x11'11'11'11));
}
我有一种将 RGBA 转换为 BGRA 的方法。下面是方法
unsigned int ConvertRGBAToBGRA(unsigned int v) {
unsigned char r = (v)& 0xFF;
unsigned char g = (v >> 8) & 0xFF;
unsigned char b = (v >> 16) & 0xFF;
unsigned char a = (v >> 24) & 0xFF;
return (a << 24) | (r << 16) | (g << 8) | b;
};
我怎样才能很好地进行单元测试?有没有办法我可以读回这些位并以某种方式对该方法进行单元测试?
我正在使用googletests
您可以通过指针映射到数组来将值拆分为四个字节。然后交换字节。
uint8_t* pV= reinterpret_cast<uint8_t*>(&V);
uint8_t Swap= pV[1]; pV[1]= pV[3]; pV[3]= Swap;
受@Yves Daoust 评论的启发,你为什么不能像下面这样写一系列检查?您可以使用 C++14 的漂亮格式:
unsigned int ConvertRGBAToBGRA(unsigned int v) {
unsigned char r = (v)&0xFF;
unsigned char g = (v >> 8) & 0xFF;
unsigned char b = (v >> 16) & 0xFF;
unsigned char a = (v >> 24) & 0xFF;
return (a << 24) | (r << 16) | (g << 8) | b;
};
TEST(ConvertRGBAToBGRATest, Test1) {
EXPECT_EQ(ConvertRGBAToBGRA(0x12'34'56'78), 0x12'78'56'34);
EXPECT_EQ(ConvertRGBAToBGRA(0x12'78'56'34), 0x12'34'56'78);
EXPECT_EQ(ConvertRGBAToBGRA(0x11'11'11'11), 0x11'11'11'11);
EXPECT_EQ(ConvertRGBAToBGRA(0x00'00'00'00), 0x00'00'00'00);
EXPECT_EQ(ConvertRGBAToBGRA(0xAa'Bb'Cc'Dd), 0xAa'Dd'Cc'Bb);
EXPECT_EQ(ConvertRGBAToBGRA(ConvertRGBAToBGRA(0x12'34'56'78)), 0x12'34'56'78);
EXPECT_EQ(ConvertRGBAToBGRA(ConvertRGBAToBGRA(0x12'78'56'34)), 0x12'78'56'34);
EXPECT_EQ(ConvertRGBAToBGRA(ConvertRGBAToBGRA(0x11'11'11'11)), 0x11'11'11'11);
EXPECT_EQ(ConvertRGBAToBGRA(ConvertRGBAToBGRA(0x00'00'00'00)), 0x00'00'00'00);
EXPECT_EQ(ConvertRGBAToBGRA(ConvertRGBAToBGRA(0xAa'Bb'Cc'Dd)), 0xAa'Bb'Cc'Dd);
}
实例:https://godbolt.org/z/eEajYYYsf
您还可以定义一个 custom matcher 并使用 EXPECT_THAT
宏:
// A custom matcher for comparing BGRA and RGBA.
MATCHER_P(IsBgraOf, n, "") {
return ((n & 0xFF000000) == (arg & 0xFF000000)) &&
((n & 0x00FF0000) == ((arg << 16) & 0x00FF0000)) &&
((n & 0x0000FF00) == (arg & 0x0000FF00));
}
TEST(ConvertRGBAToBGRATest, WithExpectThat) {
EXPECT_THAT(ConvertRGBAToBGRA(0x12'34'56'78), IsBgraOf(0x12'34'56'78));
EXPECT_THAT(ConvertRGBAToBGRA(0x12'78'56'34), IsBgraOf(0x12'78'56'34));
EXPECT_THAT(ConvertRGBAToBGRA(0xAa'Bb'Cc'Dd), IsBgraOf(0xAa'Bb'Cc'Dd));
EXPECT_THAT(ConvertRGBAToBGRA(0x00'00'00'00), IsBgraOf(0x00'00'00'00));
EXPECT_THAT(ConvertRGBAToBGRA(0x11'11'11'11), IsBgraOf(0x11'11'11'11));
}