-
Notifications
You must be signed in to change notification settings - Fork 14
/
TestBits.cpp
68 lines (58 loc) · 1.88 KB
/
TestBits.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <cctest/cctest.h>
#include <cub/algo/bits.h>
USING_CUB_NS
FIXTURE(BitsTest)
{
TEST("should calc the correct bit mask by macro")
{
ASSERT_EQ(0, BIT_MASK(0));
ASSERT_EQ(1, BIT_MASK(1));
ASSERT_EQ(3, BIT_MASK(2));
ASSERT_EQ(0x7F, BIT_MASK(7));
ASSERT_EQ(0xFF, BIT_MASK(8));
ASSERT_EQ(0x7FFF, BIT_MASK(15));
ASSERT_EQ(0xFFFF, BIT_MASK(16));
ASSERT_EQ(0x7FFFFFFF, BIT_MASK(31));
ASSERT_EQ(0xFFFFFFFF, BIT_MASK(32));
ASSERT_EQ(0x7FFFFFFFFFFFFFFF, BIT_MASK(63));
}
TEST("should calc the correct bit mask by template function")
{
ASSERT_EQ(0, bit_mask<U8>(0));
ASSERT_EQ(1, bit_mask<U8>(1));
ASSERT_EQ(0x3, bit_mask<U8>(2));
ASSERT_EQ(0x7F, bit_mask<U8>(7));
ASSERT_EQ(0xFF, bit_mask<U8>(8));
ASSERT_EQ(0x7FFF, bit_mask<U16>(15));
ASSERT_EQ(0xFFFF, bit_mask<U16>(16));
ASSERT_EQ(0x7FFFFFFF, bit_mask<U32>(31));
ASSERT_EQ(0xFFFFFFFF, bit_mask<U64>(32));
ASSERT_EQ(0x7FFFFFFFFFFFFFFF, bit_mask<U64>(63));
}
TEST("should get the bits value by macro")
{
ASSERT_EQ(0x2, GET_BITS_VALUE(0xaa, 2, 2));
ASSERT_EQ(0x1, GET_BITS_VALUE(0xaa, 3, 2));
ASSERT_EQ(0x5, GET_BITS_VALUE(0xaa, 3, 3));
ASSERT_EQ(0x1, GET_BITS_VALUE(0xaa, 7, 4));
ASSERT_EQ(0xc, GET_BITS_VALUE(0xccaaaaaaaaaaaaaa, 60, 4));
}
TEST("should get the bits value by template function")
{
ASSERT_EQ(0x2, bit_value(0xaa, 2, 2));
ASSERT_EQ(0x1, bit_value(0xaa, 3, 2));
ASSERT_EQ(0x5, bit_value(0xaa, 3, 3));
ASSERT_EQ(0x1, bit_value(0xaa, 7, 4));
ASSERT_EQ(0xc, bit_value(0xccaaaaaaaaaaaaaa, 60, 4));
}
TEST("should get the given bit value by macro")
{
ASSERT_FALSE(IS_BIT_ON(0xaa, 2));
ASSERT_TRUE(IS_BIT_ON(0xccaaaaaaaaaaaaaa, 63));
}
TEST("should get the given bit value by template function")
{
ASSERT_FALSE(is_bit_on(0xaa, 2));
ASSERT_TRUE(is_bit_on(0xccaaaaaaaaaaaaaa, 63));
}
};